~ubuntu-branches/ubuntu/breezy/ufraw/breezy-backports

« back to all changes in this revision

Viewing changes to ufraw_routines.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2004-12-22 09:14:48 UTC
  • Revision ID: james.westby@ubuntu.com-20041222091448-rdxlth42ivq6gh5n
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   UFRaw - Unidentified Flying Raw
 
3
   Raw photo loader plugin for The GIMP
 
4
   by udi Fuchs,
 
5
 
 
6
   based on the gimp plug-in by Pawel T. Jochym jochym at ifj edu pl,
 
7
   
 
8
   based on the gimp plug-in by Dave Coffin
 
9
   http://www.cybercom.net/~dcoffin/
 
10
 
 
11
   UFRaw is licensed under the GNU General Public License.
 
12
   It uses "dcraw" code to do the actual raw decoding.
 
13
*/
 
14
 
 
15
#include <stdio.h>
 
16
#include <stdlib.h> /* needed for getenv() */
 
17
#include <string.h>
 
18
#include <math.h>
 
19
#include <gtk/gtk.h>
 
20
#include "ufraw.h"
 
21
#include "blackbody.h"
 
22
 
 
23
void set_curve(char *curve, int maxValue, int curveType, curve_data *cp)
 
24
{
 
25
        int i, ci, blackPoint, whitePoint;
 
26
        float x, val, lam;
 
27
 
 
28
        blackPoint = cp->black * maxValue;
 
29
        whitePoint = maxValue / pow(2, cp->exposure);
 
30
        for (i=0; i<blackPoint+1; i++)
 
31
                curve[i] = 0;
 
32
        if (curveType==gamma_curve || curveType==log_curve) {
 
33
            lam = pow(cp->gamma, -3) - 1;
 
34
            if (lam==0) lam = 0.01;
 
35
            for (; i<whitePoint && i<maxValue; i++) {
 
36
                x = (float)(i-blackPoint)/(whitePoint-blackPoint);
 
37
                curve[i] = ( curveType==log_curve ?
 
38
                        log(1+lam*x)/log(1+lam) : pow(x, cp->gamma) ) *
 
39
                        (1+cp->shadow*exp(-x/cp->depth)) /
 
40
                        (1+cp->shadow*exp(-1/cp->depth)) * (MAXOUT+1);
 
41
            }
 
42
        } else {
 
43
            for (ci=0; i<whitePoint && i<maxValue; i++) {
 
44
                x = (float)(i-blackPoint)/(whitePoint-blackPoint);
 
45
                if ( ci+1 < cp->curveSize &&
 
46
                                x*cp->indexSize >= cp->curve[ci+1].index) ci++;
 
47
                val = ( cp->curve[ci].value +
 
48
                        (float)(cp->curve[ci+1].value-cp->curve[ci].value) *
 
49
                        (x*cp->indexSize - cp->curve[ci].index) /
 
50
                        (cp->curve[ci+1].index - cp->curve[ci].index) )/MAXOUT;
 
51
        /* It seems that (int)f is not always the same as (int)floor(f) */
 
52
                curve[i] = MIN( floor(
 
53
//                      ( pow(val, 1-cp->contrast) ) *
 
54
                        ( cp->contrast==0 ? val :
 
55
                            log(1+cp->contrast*val) / log(1+cp->contrast) ) *
 
56
                        ( 1 + cp->shadow*exp(-val/cp->depth) ) /
 
57
                        ( 1 + cp->shadow*exp(-1/cp->depth) )*MAXOUT), MAXOUT);
 
58
            }
 
59
        }
 
60
        for (; i<maxValue; i++)
 
61
                curve[i] = curve[i-1];
 
62
}
 
63
 
 
64
void curve_convert(curve_data *cp, int curveSize, guint8 *curve)
 
65
{
 
66
        int index, ci, i;
 
67
        index = 0;
 
68
        ci = 0;
 
69
        cp->indexSize = curveSize;
 
70
        cp->curve[ci].index = 0;
 
71
        cp->curve[ci].value = curve[0];
 
72
        ci++;
 
73
        for (i=0; i<curveSize && ci<256; i++) {
 
74
                /* if the curve "steps up" record that point */
 
75
                if (curve[i]>curve[index]) {
 
76
                        /* if current point and previous point are on the same
 
77
                         * line, remove previous point. Strait line condition:
 
78
                         * ([ci-1].v-[ci-2].v)/([ci-1].i-[ci-2].i) ==
 
79
                         * ([ci].v-[ci-2].v)/([ci].i-[ci-2].i) */
 
80
                        if (ci>1 &&
 
81
                                (cp->curve[ci-1].value - cp->curve[ci-2].value)*
 
82
                                (i - cp->curve[ci-2].index) ==
 
83
                                (curve[i] - cp->curve[ci-2].value)*
 
84
                                (cp->curve[ci-1].index - cp->curve[ci-2].index))
 
85
                                        ci--;
 
86
                        cp->curve[ci].index = i;
 
87
                        cp->curve[ci].value = curve[i];
 
88
                        ci++;
 
89
                        index = i;
 
90
                }
 
91
        }
 
92
        cp->curveSize = ci;
 
93
        /* last point is only needed internaly */
 
94
        cp->curve[ci].index = curveSize-1;
 
95
        cp->curve[ci].value = curve[curveSize-1];
 
96
}
 
97
 
 
98
inline void saturate(int p[3], float *satTable, int maxValue)
 
99
{
 
100
        int maxc, midc, minc;
 
101
        float sat, hue;
 
102
 
 
103
        if (p[0] > p[1] && p[0] > p[2]) {
 
104
                maxc = 0;
 
105
                if (p[1] > p[2]) { midc = 1; minc = 2; }
 
106
                else { midc = 2; minc = 1; }
 
107
        } else if (p[1] > p[2]) {
 
108
                maxc = 1;
 
109
                if (p[0] > p[2]) { midc = 0; minc = 2; }
 
110
                else { midc = 2; minc = 0; }
 
111
        } else {
 
112
                maxc = 2;
 
113
                if (p[0] > p[1]) { midc = 0; minc = 1; }
 
114
                else { midc = 1; minc = 0; }
 
115
        }
 
116
        if (p[maxc]!=p[minc]) {
 
117
                /* oldSaturation = (max-min) / max */
 
118
                /* newSaturation = 1 - pow(1 - oldSaturation, saturate) */
 
119
                sat = 1 - satTable[MIN(p[minc], maxValue)] /
 
120
                        satTable[MIN(p[maxc], maxValue)];
 
121
                hue = (float)(p[midc]-p[minc])/(p[maxc]-p[minc]);
 
122
                p[maxc] = MIN(p[maxc], maxValue);
 
123
                p[minc] = p[maxc]*(1-sat);
 
124
                p[midc] = p[maxc]*(1-sat+sat*hue); 
 
125
                /* It is also possble to define oldSaturation = max-min */
 
126
                /* but it doesn't work at the moment */
 
127
/*              sat = (1 - satTable[maxValue-MIN(p[maxc]-p[minc],maxValue)]/
 
128
                                satTable[maxValue] ) * maxValue;
 
129
                hue = (float)(p[midc]-p[minc])/(p[maxc]-p[minc]);
 
130
                p[maxc] = MIN(p[maxc], maxValue);
 
131
                p[minc] = p[maxc] - sat;
 
132
                p[midc] = p[maxc]+sat*(hue-1); 
 
133
*/      }
 
134
}
 
135
 
 
136
void pow_table(float *table, int size, float saturation)
 
137
{
 
138
        int i;
 
139
 
 
140
        for (i=0; i<size; i++) table[i] = pow(i, saturation);
 
141
}
 
142
 
 
143
void pixbuf_reverse(int x, int y, guchar *pixbuf, int width, int height,
 
144
                int rowstride)
 
145
{
 
146
        int c;
 
147
        
 
148
        if (x>0 && y>0 && x<width && y<height)
 
149
                for (c=0; c<3; c++)
 
150
                    pixbuf[y*rowstride+3*x+c] =
 
151
                                (pixbuf[y*rowstride+3*x+c] +128) % 256;
 
152
}
 
153
 
 
154
void set_wb(float rgbWB[3], float temperature, float green)
 
155
{
 
156
        int t, c;
 
157
        
 
158
        t = temperature/10-200;
 
159
        for (c=0; c<3; c++)
 
160
                rgbWB[c] = 1/bbWB[t][c];
 
161
        rgbWB[1] *= green;
 
162
        rgbWB[0] /= rgbWB[1];
 
163
        rgbWB[2] /= rgbWB[1];
 
164
        rgbWB[1] = 1.0;
 
165
}
 
166
 
 
167
void RGB_to_temperature(float *rgb, float *temperature, float *green)
 
168
{
 
169
        int l, r, m;
 
170
        double rbRatio;
 
171
        
 
172
        rbRatio = rgb[0]/rgb[2];
 
173
 
 
174
        for (l=0, r=sizeof(bbWB)/(sizeof(float)*3), m=(l+r)/2;
 
175
                r-l>1 ; m=(l+r)/2) {
 
176
                if (bbWB[m][0]/bbWB[m][2] > rbRatio) 
 
177
                        l = m;
 
178
                else
 
179
                        r = m;
 
180
        }
 
181
        *temperature = m*10+2000;
 
182
        *green = (bbWB[m][1]/bbWB[m][0])/(rgb[1]/rgb[0]);
 
183
#ifdef DEBUG
 
184
        fprintf(stderr,
 
185
                "RGB_to_temperature: Temperature=%d K, green component=%f\n",
 
186
                        (int)*temperature, *green);
 
187
#endif
 
188
}
 
189
 
 
190
void save_path(const char *filename, char *path)
 
191
{
 
192
        int i;
 
193
 
 
194
        strcpy(path, filename);
 
195
        for (i=strlen(path);
 
196
                i>0 && path[i]!='/' && path[i]!='\\'; i--)
 
197
                        path[i]=0;
 
198
}
 
199
 
 
200
void set_path(GtkFileSelection *file_selector, char *path)
 
201
{
 
202
        const char *def;
 
203
 
 
204
        if (path[0]!='\0')
 
205
                gtk_file_selection_set_filename(file_selector, path);
 
206
        else if ( (def=getenv("UFRAW_CURVES"))!=NULL )
 
207
                gtk_file_selection_set_filename(file_selector, def);
 
208
#ifdef WIN32
 
209
        else if ( getenv("PWD")==NULL )
 
210
        {
 
211
                const char *homedrive, *homepath;
 
212
                char home[200];
 
213
                homedrive = getenv("HOMEDRIVE");
 
214
                homepath = getenv("HOMEPATH");
 
215
                sprintf(home, "%s%s\\", homedrive, homepath);
 
216
                gtk_file_selection_set_filename(file_selector, home);
 
217
        }
 
218
#endif
 
219
}
 
220