~inkscape.dev/inkscape-rendertest/trunk

« back to all changes in this revision

Viewing changes to perceptualdiff-1.1.1-alphamod/LPyramid.cpp

  • Committer: Johan B. C. Engelen
  • Date: 2014-11-12 22:14:29 UTC
  • Revision ID: j.b.c.engelen@alumnus.utwente.nl-20141112221429-ml7tzwn300a7qvkn
Add modified PerceptualDiff code (alpha fix)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
15
*/
 
16
 
 
17
#include "LPyramid.h"
 
18
 
 
19
 
 
20
//////////////////////////////////////////////////////////////////////
 
21
// Construction/Destruction
 
22
//////////////////////////////////////////////////////////////////////
 
23
 
 
24
LPyramid::LPyramid(float *image, int width, int height) :
 
25
        Width(width),
 
26
        Height(height)
 
27
{
 
28
        // Make the Laplacian pyramid by successively
 
29
        // copying the earlier levels and blurring them
 
30
        for (int i=0; i<MAX_PYR_LEVELS; i++) {
 
31
                if (i == 0) {
 
32
                        Levels[i] = Copy(image);
 
33
                } else {
 
34
                        Levels[i] = new float[Width * Height];
 
35
                        Convolve(Levels[i], Levels[i - 1]);
 
36
                }
 
37
        }
 
38
}
 
39
 
 
40
LPyramid::~LPyramid()
 
41
{
 
42
        for (int i=0; i<MAX_PYR_LEVELS; i++) {
 
43
                if (Levels[i]) delete Levels[i];
 
44
        }
 
45
}
 
46
 
 
47
float *LPyramid::Copy(float *img)
 
48
{
 
49
        int max = Width * Height;
 
50
        float *out = new float[max];
 
51
        for (int i = 0; i < max; i++) out[i] = img[i];
 
52
        
 
53
        return out;
 
54
}
 
55
 
 
56
void LPyramid::Convolve(float *a, float *b)
 
57
// convolves image b with the filter kernel and stores it in a
 
58
{
 
59
        int y,x,i,j,nx,ny;
 
60
        const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
 
61
 
 
62
        for (y=0; y<Height; y++) {
 
63
                for (x=0; x<Width; x++) {
 
64
                        int index = y * Width + x;
 
65
                        a[index] = 0.0f;
 
66
                        for (i=-2; i<=2; i++) {
 
67
                                for (j=-2; j<=2; j++) {
 
68
                                        nx=x+i;
 
69
                                        ny=y+j;
 
70
                                        if (nx<0) nx=-nx;
 
71
                                        if (ny<0) ny=-ny;
 
72
                                        if (nx>=Width) nx=2*Width-nx-1;
 
73
                                        if (ny>=Height) ny=2*Height-ny-1;
 
74
                                        a[index] += Kernel[i+2] * Kernel[j+2] * b[ny * Width + nx];
 
75
                                } 
 
76
                        }
 
77
                }
 
78
        }
 
79
}
 
80
 
 
81
float LPyramid::Get_Value(int x, int y, int level)
 
82
{
 
83
        int index = x + y * Width;
 
84
        int l = level;
 
85
        if (l > MAX_PYR_LEVELS) l = MAX_PYR_LEVELS;
 
86
        return Levels[level][index];
 
87
}
 
88
 
 
89