2
UFRaw - Unidentified Flying Raw
3
Raw photo loader plugin for The GIMP
6
based on the gimp plug-in by Pawel T. Jochym jochym at ifj edu pl,
8
based on the gimp plug-in by Dave Coffin
9
http://www.cybercom.net/~dcoffin/
11
UFRaw is licensed under the GNU General Public License.
12
It uses "dcraw" code to do the actual raw decoding.
16
#include <stdlib.h> /* needed for getenv() */
21
#include "blackbody.h"
23
void set_curve(char *curve, int maxValue, int curveType, curve_data *cp)
25
int i, ci, blackPoint, whitePoint;
28
blackPoint = cp->black * maxValue;
29
whitePoint = maxValue / pow(2, cp->exposure);
30
for (i=0; i<blackPoint+1; i++)
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);
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);
60
for (; i<maxValue; i++)
61
curve[i] = curve[i-1];
64
void curve_convert(curve_data *cp, int curveSize, guint8 *curve)
69
cp->indexSize = curveSize;
70
cp->curve[ci].index = 0;
71
cp->curve[ci].value = curve[0];
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) */
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))
86
cp->curve[ci].index = i;
87
cp->curve[ci].value = curve[i];
93
/* last point is only needed internaly */
94
cp->curve[ci].index = curveSize-1;
95
cp->curve[ci].value = curve[curveSize-1];
98
inline void saturate(int p[3], float *satTable, int maxValue)
100
int maxc, midc, minc;
103
if (p[0] > p[1] && p[0] > p[2]) {
105
if (p[1] > p[2]) { midc = 1; minc = 2; }
106
else { midc = 2; minc = 1; }
107
} else if (p[1] > p[2]) {
109
if (p[0] > p[2]) { midc = 0; minc = 2; }
110
else { midc = 2; minc = 0; }
113
if (p[0] > p[1]) { midc = 0; minc = 1; }
114
else { midc = 1; minc = 0; }
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);
136
void pow_table(float *table, int size, float saturation)
140
for (i=0; i<size; i++) table[i] = pow(i, saturation);
143
void pixbuf_reverse(int x, int y, guchar *pixbuf, int width, int height,
148
if (x>0 && y>0 && x<width && y<height)
150
pixbuf[y*rowstride+3*x+c] =
151
(pixbuf[y*rowstride+3*x+c] +128) % 256;
154
void set_wb(float rgbWB[3], float temperature, float green)
158
t = temperature/10-200;
160
rgbWB[c] = 1/bbWB[t][c];
162
rgbWB[0] /= rgbWB[1];
163
rgbWB[2] /= rgbWB[1];
167
void RGB_to_temperature(float *rgb, float *temperature, float *green)
172
rbRatio = rgb[0]/rgb[2];
174
for (l=0, r=sizeof(bbWB)/(sizeof(float)*3), m=(l+r)/2;
176
if (bbWB[m][0]/bbWB[m][2] > rbRatio)
181
*temperature = m*10+2000;
182
*green = (bbWB[m][1]/bbWB[m][0])/(rgb[1]/rgb[0]);
185
"RGB_to_temperature: Temperature=%d K, green component=%f\n",
186
(int)*temperature, *green);
190
void save_path(const char *filename, char *path)
194
strcpy(path, filename);
196
i>0 && path[i]!='/' && path[i]!='\\'; i--)
200
void set_path(GtkFileSelection *file_selector, char *path)
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);
209
else if ( getenv("PWD")==NULL )
211
const char *homedrive, *homepath;
213
homedrive = getenv("HOMEDRIVE");
214
homepath = getenv("HOMEPATH");
215
sprintf(home, "%s%s\\", homedrive, homepath);
216
gtk_file_selection_set_filename(file_selector, home);