Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / pdiff / lpyramid.c
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 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     const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
36     int width = pyramid->width;
37     int height = pyramid->height;
38
39     for (y=0; y<height; y++) {
40         for (x=0; x<width; x++) {
41             float sum = 0.f;
42             for (j=-2; j<=2; j++) {
43                 float sum_i = 0.f;
44                 int ny=y+j;
45                 if (ny<0) ny=-ny;
46                 if (ny>=height) ny=2*height - ny - 1;
47                 ny *= width;
48                 for (i=-2; i<=2; i++) {
49                     int nx=x+i;
50                     if (nx<0) nx=-nx;
51                     if (nx>=width) nx=2*width - nx - 1;
52                     sum_i += Kernel[i+2] * b[ny + nx];
53                 }
54                 sum += sum_i * Kernel[j+2];
55             }
56             *a++ = sum;
57         }
58     }
59 }
60
61 /*
62  * Construction/Destruction
63  */
64
65 lpyramid_t *
66 lpyramid_create (float *image, int width, int height)
67 {
68     lpyramid_t *pyramid;
69     int i;
70
71     pyramid = malloc (sizeof (lpyramid_t));
72     if (pyramid == NULL) {
73         fprintf (stderr, "Out of memory.\n");
74         exit (1);
75     }
76     pyramid->width = width;
77     pyramid->height = height;
78
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");
85             exit (1);
86         }
87         if (i == 0) {
88             memcpy (pyramid->levels[i], image, width * height * sizeof (float));
89         } else {
90             convolve(pyramid, pyramid->levels[i], pyramid->levels[i - 1]);
91         }
92     }
93
94     return pyramid;
95 }
96
97 void
98 lpyramid_destroy (lpyramid_t *pyramid)
99 {
100     int i;
101
102     for (i=0; i<MAX_PYR_LEVELS; i++)
103         free (pyramid->levels[i]);
104
105     free (pyramid);
106 }
107
108 float
109 lpyramid_get_value (lpyramid_t *pyramid, int x, int y, int level)
110 {
111     int index = x + y * pyramid->width;
112     int l = level;
113     if (l > MAX_PYR_LEVELS)
114         l = MAX_PYR_LEVELS;
115     return pyramid->levels[level][index];
116 }