~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to extra/libkdcraw/libraw/demosaic-pack-GPL2/ahd_interpolate_mod.c

  • Committer: Package Import Robot
  • Author(s): Felix Geyer, Rohan Garg, Philip Muškovac, Felix Geyer
  • Date: 2011-09-23 18:18:55 UTC
  • mfrom: (1.2.36 upstream)
  • Revision ID: package-import@ubuntu.com-20110923181855-ifs67wxkugshev9k
Tags: 2:2.1.1-0ubuntu1
[ Rohan Garg ]
* New upstream release (LP: #834190)
  - debian/control
    + Build with libqtwebkit-dev
 - debian/kipi-plugins-common
    + Install libkvkontakte required by kipi-plugins
 - debian/digikam
    + Install panoramagui

[ Philip Muškovac ]
* New upstream release
  - debian/control:
    + Add libcv-dev, libcvaux-dev, libhighgui-dev, libboost-graph1.46-dev,
      libksane-dev, libxml2-dev, libxslt-dev, libqt4-opengl-dev, libqjson-dev,
      libgpod-dev and libqca2-dev to build-deps
    + Add packages for kipi-plugins, libmediawiki, libkface, libkgeomap and
      libkvkontakte
  - debian/rules:
    + Don't build with gphoto2 since it doesn't build with it.
  - Add kubuntu_fix_test_linking.diff to fix linking of the dngconverter test
  - update install files
  - update kubuntu_01_mysqld_executable_name.diff for new cmake layout
    and rename to kubuntu_mysqld_executable_name.diff
* Fix typo in digikam-data description (LP: #804894)
* Fix Vcs links

[ Felix Geyer ]
* Move library data files to the new packages libkface-data, libkgeomap-data
  and libkvkontakte-data.
* Override version of the embedded library packages to 1.0~digikam<version>.
* Exclude the library packages from digikam-dbg to prevent file conflicts in
  the future.
* Call dh_install with --list-missing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file was taken from modified dcraw published by Paul Lee
 
2
   on January 23, 2009, taking dcraw ver.8.90/rev.1.417
 
3
   as basis.
 
4
   http://sites.google.com/site/demosaicalgorithms/modified-dcraw
 
5
 
 
6
   As modified dcraw source code was published, the release under
 
7
   GPL Version 2 or later option could be applied, so this file
 
8
   is taken under this premise.
 
9
*/
 
10
 
 
11
/*
 
12
    Copyright (C) 2009 Paul Lee
 
13
 
 
14
    This program is free software; you can redistribute it and/or modify
 
15
    it under the terms of the GNU General Public License as published by
 
16
    the Free Software Foundation; either version 2 of the License, or
 
17
    (at your option) any later version.
 
18
 
 
19
    This program is distributed in the hope that it will be useful,
 
20
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
    GNU General Public License for more details.
 
23
 
 
24
    You should have received a copy of the GNU General Public License along
 
25
    with this program; if not, write to the Free Software Foundation, Inc.,
 
26
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
27
*/
 
28
 
 
29
/*
 
30
   Adaptive Homogeneity-Directed interpolation is based on
 
31
   the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
 
32
 */
 
33
/*
 
34
Adaptive Homogeneity-Directed interpolation is based on
 
35
the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
 
36
 
 
37
AHD interpolation with built-in anti-aliasing feature
 
38
*/
 
39
#define TS 256          /* Tile Size */
 
40
 
 
41
void CLASS ahd_interpolate_mod()
 
42
{
 
43
        int i, j, k, top, left, row, col, tr, tc, c, d, val, hm[2];
 
44
        ushort (*pix)[4], (*rix)[3];
 
45
        static const int dir[4] = { -1, 1, -TS, TS };
 
46
        unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
 
47
        float r, cbrt[0x10000], xyz[3], xyz_cam[3][4];
 
48
        ushort (*rgb)[TS][TS][3];
 
49
        short (*lab)[TS][TS][3], (*lix)[3];
 
50
        char (*homo)[TS][TS], *buffer;
 
51
 
 
52
#ifdef DCRAW_VERBOSE
 
53
        if (verbose) fprintf (stderr,_("AHD interpolation (modified for anti-aliasing)...\n"));
 
54
#endif
 
55
 
 
56
        for (i=0; i < 0x10000; i++) {
 
57
                r = i / 65535.0;
 
58
                cbrt[i] = r > 0.008856 ? pow((double)r,1/3.0) : 7.787*r + 16/116.0;
 
59
        }
 
60
        for (i=0; i < 3; i++)
 
61
                for (j=0; j < colors; j++)
 
62
                        for (xyz_cam[i][j] = k=0; k < 3; k++)
 
63
                                xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
 
64
 
 
65
        border_interpolate(6);
 
66
        buffer = (char *) malloc (26*TS*TS);            /* 1664 kB */
 
67
        merror (buffer, "ahd_interpolate()");
 
68
        rgb  = (ushort(*)[TS][TS][3]) buffer;
 
69
        lab  = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
 
70
        homo = (char  (*)[TS][TS])   (buffer + 24*TS*TS);
 
71
 
 
72
        for (top=3; top < height-6; top += TS-7)
 
73
                for (left=3; left < width-6; left += TS-7) {
 
74
 
 
75
                        /*  Interpolate green horizontally and vertically:              */
 
76
                        for (row = top; row < top+TS && row < height-3; row++) {
 
77
                                col = left + (FC(row,left) & 1);
 
78
                                for (c = FC(row,col); col < left+TS && col < width-3; col+=2) {
 
79
                                        pix = image + row*width+col;
 
80
                                        val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2
 
81
                                                - pix[-2][c] - pix[2][c] + 2) >> 2;
 
82
                                        if (val < 0 || val > 65535) {
 
83
                                                val = (pix[-3][1] + pix[3][1] +
 
84
                                                        18*(2*pix[0][c] - pix[-2][c] - pix[2][c]) +
 
85
                                                        63*(pix[-1][1] + pix[1][1]) + 64) >> 7;
 
86
                                                if (val < 0 || val > 65535) {
 
87
                                                        val = (4*(pix[-1][1] + pix[1][1]) +
 
88
                                                                2*pix[0][c]-pix[-2][c]-pix[2][c] + 4) >> 3;
 
89
                                                        if (val < 0 || val > 65535)
 
90
                                                                val = (pix[-1][1] + pix[1][1] + 1) >> 1; }}
 
91
                                        rgb[0][row-top][col-left][1] = val;
 
92
                                        val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2
 
93
                                                - pix[-2*width][c] - pix[2*width][c] + 2) >> 2;
 
94
                                        if (val < 0 || val > 65535) {
 
95
                                                val = (pix[-3*width][1] + pix[3*width][1] +
 
96
                                                        18*(2*pix[0][c] - pix[-2*width][c] - pix[2*width][c]) +
 
97
                                                        63*(pix[-width][1] + pix[width][1]) + 64) >> 7;
 
98
                                                if (val < 0 || val > 65535) {
 
99
                                                        val = (4*(pix[-width][1] + pix[width][1]) +
 
100
                                                                2*pix[0][c]-pix[-2*width][c]-pix[2*width][c] + 4) >> 3;
 
101
                                                        if (val < 0 || val > 65535)
 
102
                                                                val = (pix[-width][1] + pix[width][1] + 1) >> 1; }}
 
103
                                        rgb[1][row-top][col-left][1] = val;
 
104
                                }
 
105
                        }
 
106
                        /*  Interpolate red and blue, and convert to CIELab:            */
 
107
                        for (d=0; d < 2; d++)
 
108
                                for (row=top+1; row < top+TS-1 && row < height-4; row++)
 
109
                                        for (col=left+1; col < left+TS-1 && col < width-4; col++) {
 
110
                                                pix = image + row*width+col;
 
111
                                                rix = &rgb[d][row-top][col-left];
 
112
                                                lix = &lab[d][row-top][col-left];
 
113
                                                if ((c = 2 - FC(row,col)) == 1) {
 
114
                                                        c = FC(row+1,col);
 
115
                                                        val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
 
116
                                                        - rix[-1][1] - rix[1][1] + 1) >> 1);
 
117
                                                        if (val < 0 || val > 65535)
 
118
                                                                val = (pix[-1][2-c] + pix[1][2-c] + 1) >> 1;
 
119
                                                        rix[0][2-c] = val;
 
120
                                                        val = pix[0][1] + (( pix[-width][c] + pix[width][c]
 
121
                                                        - rix[-TS][1] - rix[TS][1] + 1) >> 1);
 
122
                                                        if (val < 0 || val > 65535)
 
123
                                                                val = (pix[-width][c] + pix[width][c] + 1) >> 1;
 
124
                                                } else {
 
125
                                                        val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
 
126
                                                        + pix[+width-1][c] + pix[+width+1][c]
 
127
                                                        - rix[-TS-1][1] - rix[-TS+1][1]
 
128
                                                        - rix[+TS-1][1] - rix[+TS+1][1] + 2) >> 2);
 
129
                                                        if (val < 0 || val > 65535)
 
130
                                                                val = (pix[-width-1][c] + pix[-width+1][c] +
 
131
                                                                pix[ width-1][c] + pix[ width+1][c] + 2) >> 2; }
 
132
                                                rix[0][c] = val;
 
133
                                                c = FC(row,col);
 
134
                                                rix[0][c] = pix[0][c];
 
135
                                                xyz[0] = xyz[1] = xyz[2] = 0.5;
 
136
                                                FORCC {
 
137
                                                        xyz[0] += xyz_cam[0][c] * rix[0][c];
 
138
                                                        xyz[1] += xyz_cam[1][c] * rix[0][c];
 
139
                                                        xyz[2] += xyz_cam[2][c] * rix[0][c];
 
140
                                                }
 
141
                                                xyz[0] = cbrt[CLIP((int) xyz[0])];
 
142
                                                xyz[1] = cbrt[CLIP((int) xyz[1])];
 
143
                                                xyz[2] = cbrt[CLIP((int) xyz[2])];
 
144
                                                lix[0][0] = 64 * (116 * xyz[1] - 16);
 
145
                                                lix[0][1] = 64 * 500 * (xyz[0] - xyz[1]);
 
146
                                                lix[0][2] = 64 * 200 * (xyz[1] - xyz[2]);
 
147
                                        }
 
148
                                        /*  Build homogeneity maps from the CIELab images:              */
 
149
                                        memset (homo, 0, 2*TS*TS);
 
150
                                        for (row=top+2; row < top+TS-2 && row < height-5; row++) {
 
151
                                                tr = row-top;
 
152
                                                for (col=left+2; col < left+TS-2 && col < width-5; col++) {
 
153
                                                        tc = col-left;
 
154
                                                        for (d=0; d < 2; d++) {
 
155
                                                                lix = &lab[d][tr][tc];
 
156
                                                                for (i=0; i < 4; i++) {
 
157
                                                                        ldiff[d][i] = ABS(lix[0][0]-lix[dir[i]][0]);
 
158
                                                                        abdiff[d][i] = SQR(lix[0][1]-lix[dir[i]][1])
 
159
                                                                                + SQR(lix[0][2]-lix[dir[i]][2]);
 
160
                                                                }
 
161
                                                        }
 
162
                                                        leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
 
163
                                                                MAX(ldiff[1][2],ldiff[1][3]));
 
164
                                                        abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
 
165
                                                                MAX(abdiff[1][2],abdiff[1][3]));
 
166
                                                        for (d=0; d < 2; d++)
 
167
                                                                for (i=0; i < 4; i++)
 
168
                                                                        if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
 
169
                                                                                homo[d][tr][tc]++;
 
170
                                                }
 
171
                                        }
 
172
                                        /*  Combine the most homogenous pixels for the final result:    */
 
173
                                        for (row=top+3; row < top+TS-3 && row < height-6; row++) {
 
174
                                                tr = row-top;
 
175
                                                for (col=left+3; col < left+TS-3 && col < width-6; col++) {
 
176
                                                        tc = col-left;
 
177
                                                        for (d=0; d < 2; d++)
 
178
                                                                for (hm[d]=0, i=tr-1; i <= tr+1; i++)
 
179
                                                                        for (j=tc-1; j <= tc+1; j++)
 
180
                                                                                hm[d] += homo[d][i][j];
 
181
                                                        if (hm[0] != hm[1])
 
182
                                                                FORC3 image[row*width+col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
 
183
                                                        else
 
184
                                                                FORC3 image[row*width+col][c] =
 
185
                                                                (rgb[0][tr][tc][c] + rgb[1][tr][tc][c] + 1) >> 1;
 
186
                                                }
 
187
                                        }
 
188
                }
 
189
                free (buffer);
 
190
 
 
191
}
 
192
#undef TS