1
/*
2
  Laplacian Pyramid
3
  Copyright (C) 2006 Yangli Hector Yee
4

            
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.
8

            
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.
12

            
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
15
*/
16

            
17
#include "lpyramid.h"
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21

            
22
struct _lpyramid {
23
    /* Successively blurred versions of the original image */
24
    float *levels[MAX_PYR_LEVELS];
25

            
26
    int width;
27
    int height;
28
};
29

            
30
static void
31
10458
convolve (lpyramid_t *pyramid, float *a, const float *b)
32
/* convolves image b with the filter kernel and stores it in a */
33
{
34
    int y,x,i,j;
35
10458
    const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
36
10458
    int width = pyramid->width;
37
10458
    int height = pyramid->height;
38

            
39
1688260
    for (y=0; y<height; y++) {
40
814088226
	for (x=0; x<width; x++) {
41
812410424
	    float sum = 0.f;
42
4874462544
	    for (j=-2; j<=2; j++) {
43
4062052120
		float sum_i = 0.f;
44
4062052120
		int ny=y+j;
45
4062052120
		if (ny<0) ny=-ny;
46
4062052120
		if (ny>=height) ny=2*height - ny - 1;
47
4062052120
		ny *= width;
48
24372312720
		for (i=-2; i<=2; i++) {
49
20310260600
		    int nx=x+i;
50
20310260600
		    if (nx<0) nx=-nx;
51
20310260600
		    if (nx>=width) nx=2*width - nx - 1;
52
20310260600
		    sum_i += Kernel[i+2] * b[ny + nx];
53
		}
54
4062052120
		sum += sum_i * Kernel[j+2];
55
	    }
56
812410424
	    *a++ = sum;
57
	}
58
    }
59
10458
}
60

            
61
/*
62
 * Construction/Destruction
63
 */
64

            
65
lpyramid_t *
66
1494
lpyramid_create (float *image, int width, int height)
67
{
68
    lpyramid_t *pyramid;
69
    int i;
70

            
71
1494
    pyramid = malloc (sizeof (lpyramid_t));
72
1494
    if (pyramid == NULL) {
73
	fprintf (stderr, "Out of memory.\n");
74
	exit (1);
75
    }
76
1494
    pyramid->width = width;
77
1494
    pyramid->height = height;
78

            
79
    /* Make the Laplacian pyramid by successively
80
     * copying the earlier levels and blurring them */
81
13446
    for (i=0; i<MAX_PYR_LEVELS; i++) {
82
11952
	pyramid->levels[i] = malloc (width * height * sizeof (float));
83
11952
	if (pyramid->levels[i] == NULL) {
84
	    fprintf (stderr, "Out of memory.\n");
85
	    exit (1);
86
	}
87
11952
	if (i == 0) {
88
1494
	    memcpy (pyramid->levels[i], image, width * height * sizeof (float));
89
	} else {
90
10458
	    convolve(pyramid, pyramid->levels[i], pyramid->levels[i - 1]);
91
	}
92
    }
93

            
94
1494
    return pyramid;
95
}
96

            
97
void
98
1494
lpyramid_destroy (lpyramid_t *pyramid)
99
{
100
    int i;
101

            
102
13446
    for (i=0; i<MAX_PYR_LEVELS; i++)
103
11952
	free (pyramid->levels[i]);
104

            
105
1494
    free (pyramid);
106
1494
}
107

            
108
float
109
2321172640
lpyramid_get_value (lpyramid_t *pyramid, int x, int y, int level)
110
{
111
2321172640
    int index = x + y * pyramid->width;
112
2321172640
    int l = level;
113
2321172640
    if (l > MAX_PYR_LEVELS)
114
        l = MAX_PYR_LEVELS;
115
2321172640
    return pyramid->levels[l][index];
116
}