2006-12-14 00:30:54 -08:00
|
|
|
/*
|
|
|
|
|
Laplacian Pyramid
|
|
|
|
|
Copyright (C) 2006 Yangli Hector Yee
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
|
GNU General Public License as published by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
|
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with this program;
|
2010-04-27 10:17:23 +02:00
|
|
|
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
|
2006-12-14 00:30:54 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "lpyramid.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
struct _lpyramid {
|
2007-10-16 15:29:59 +01:00
|
|
|
/* Successively blurred versions of the original image */
|
2006-12-14 00:30:54 -08:00
|
|
|
float *levels[MAX_PYR_LEVELS];
|
|
|
|
|
|
|
|
|
|
int width;
|
|
|
|
|
int height;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
2007-10-16 15:29:59 +01:00
|
|
|
convolve (lpyramid_t *pyramid, float *a, const float *b)
|
2006-12-14 00:30:54 -08:00
|
|
|
/* convolves image b with the filter kernel and stores it in a */
|
|
|
|
|
{
|
2007-10-16 15:29:59 +01:00
|
|
|
int y,x,i,j;
|
2006-12-14 00:30:54 -08:00
|
|
|
const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
|
|
|
|
|
int width = pyramid->width;
|
|
|
|
|
int height = pyramid->height;
|
|
|
|
|
|
|
|
|
|
for (y=0; y<height; y++) {
|
|
|
|
|
for (x=0; x<width; x++) {
|
2007-10-16 15:29:59 +01:00
|
|
|
float sum = 0.f;
|
|
|
|
|
for (j=-2; j<=2; j++) {
|
|
|
|
|
float sum_i = 0.f;
|
|
|
|
|
int ny=y+j;
|
|
|
|
|
if (ny<0) ny=-ny;
|
|
|
|
|
if (ny>=height) ny=2*height - ny - 1;
|
|
|
|
|
ny *= width;
|
|
|
|
|
for (i=-2; i<=2; i++) {
|
|
|
|
|
int nx=x+i;
|
2006-12-14 00:30:54 -08:00
|
|
|
if (nx<0) nx=-nx;
|
2007-03-11 21:55:19 +00:00
|
|
|
if (nx>=width) nx=2*width - nx - 1;
|
2007-10-16 15:29:59 +01:00
|
|
|
sum_i += Kernel[i+2] * b[ny + nx];
|
2006-12-14 00:30:54 -08:00
|
|
|
}
|
2007-10-16 15:29:59 +01:00
|
|
|
sum += sum_i * Kernel[j+2];
|
2006-12-14 00:30:54 -08:00
|
|
|
}
|
2007-10-16 15:29:59 +01:00
|
|
|
*a++ = sum;
|
2006-12-14 00:30:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Construction/Destruction
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
lpyramid_t *
|
|
|
|
|
lpyramid_create (float *image, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
lpyramid_t *pyramid;
|
|
|
|
|
int i;
|
|
|
|
|
|
2006-12-14 04:14:24 -08:00
|
|
|
pyramid = malloc (sizeof (lpyramid_t));
|
2006-12-14 00:30:54 -08:00
|
|
|
if (pyramid == NULL) {
|
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
pyramid->width = width;
|
|
|
|
|
pyramid->height = height;
|
|
|
|
|
|
|
|
|
|
/* Make the Laplacian pyramid by successively
|
|
|
|
|
* copying the earlier levels and blurring them */
|
|
|
|
|
for (i=0; i<MAX_PYR_LEVELS; i++) {
|
2006-12-14 04:14:24 -08:00
|
|
|
pyramid->levels[i] = malloc (width * height * sizeof (float));
|
2006-12-14 00:30:54 -08:00
|
|
|
if (pyramid->levels[i] == NULL) {
|
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
memcpy (pyramid->levels[i], image, width * height * sizeof (float));
|
|
|
|
|
} else {
|
|
|
|
|
convolve(pyramid, pyramid->levels[i], pyramid->levels[i - 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pyramid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
lpyramid_destroy (lpyramid_t *pyramid)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i=0; i<MAX_PYR_LEVELS; i++)
|
|
|
|
|
free (pyramid->levels[i]);
|
2007-03-02 09:32:14 -08:00
|
|
|
|
|
|
|
|
free (pyramid);
|
2006-12-14 00:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float
|
|
|
|
|
lpyramid_get_value (lpyramid_t *pyramid, int x, int y, int level)
|
|
|
|
|
{
|
|
|
|
|
int index = x + y * pyramid->width;
|
|
|
|
|
int l = level;
|
|
|
|
|
if (l > MAX_PYR_LEVELS)
|
|
|
|
|
l = MAX_PYR_LEVELS;
|
|
|
|
|
return pyramid->levels[level][index];
|
|
|
|
|
}
|