3 Copyright (C) 2006 Yangli Hector Yee
5 This program is free software; you can redistribute it and/or modify it under the terms of the
6 GNU General Public License as published by the Free Software Foundation; either version 2 of the License,
7 or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with this program;
14 if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
23 /* Successively blurred versions of the original image */
24 float *levels[MAX_PYR_LEVELS];
31 convolve (lpyramid_t *pyramid, float *a, const float *b)
32 /* convolves image b with the filter kernel and stores it in a */
35 const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
36 int width = pyramid->width;
37 int height = pyramid->height;
39 for (y=0; y<height; y++) {
40 for (x=0; x<width; x++) {
42 for (j=-2; j<=2; j++) {
46 if (ny>=height) ny=2*height - ny - 1;
48 for (i=-2; i<=2; i++) {
51 if (nx>=width) nx=2*width - nx - 1;
52 sum_i += Kernel[i+2] * b[ny + nx];
54 sum += sum_i * Kernel[j+2];
62 * Construction/Destruction
66 lpyramid_create (float *image, int width, int height)
71 pyramid = malloc (sizeof (lpyramid_t));
72 if (pyramid == NULL) {
73 fprintf (stderr, "Out of memory.\n");
76 pyramid->width = width;
77 pyramid->height = height;
79 /* Make the Laplacian pyramid by successively
80 * copying the earlier levels and blurring them */
81 for (i=0; i<MAX_PYR_LEVELS; i++) {
82 pyramid->levels[i] = malloc (width * height * sizeof (float));
83 if (pyramid->levels[i] == NULL) {
84 fprintf (stderr, "Out of memory.\n");
88 memcpy (pyramid->levels[i], image, width * height * sizeof (float));
90 convolve(pyramid, pyramid->levels[i], pyramid->levels[i - 1]);
98 lpyramid_destroy (lpyramid_t *pyramid)
102 for (i=0; i<MAX_PYR_LEVELS; i++)
103 free (pyramid->levels[i]);
109 lpyramid_get_value (lpyramid_t *pyramid, int x, int y, int level)
111 int index = x + y * pyramid->width;
113 if (l > MAX_PYR_LEVELS)
115 return pyramid->levels[level][index];