-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurves.c
403 lines (351 loc) · 12.6 KB
/
curves.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "platform.h"
#include "conf.h"
#include "stdlib.h"
#include "raw.h"
#ifdef OPT_CURVES
#include "curves.h"
/*
Expands curves features
RGB curves: ouput = curve[input]
L cruves: output = curve[L] *(input-black) + black
RGB curves and L curves are 4 colors curves
L curves contain multiplicator factor (value = multiplicator * 10^12)
DR curves: output = curve[L] *(input-black) + black
DR curves are gray scale only
There are 2 DR curves +1EV and +2EV gain
Auto DR automatically calculate the +1EV or +2EV base on
EV under exposure selected by user to avoid highlight burnout
(using Zebra)
External function added: shooting_get_ev_correction1 (added in generic/shooting)
Modified gui.c for curve enable
3/28/09: Fix black holes, and column skipping, use Green as luminance in Luminance curve
*/
unsigned short *curve_data = NULL;
#define CURVE_SIZE 1024
#define CURVE_BYTES (CURVE_SIZE*sizeof(unsigned short))
#define BLACK 32
#define LIM( a ) (((a)>1023) ? 1023: a )
#define RBLACK( a ) (((a)>BLACK) ? ((a)-BLACK) : 0)
#define CURVE0( a, b ) (((a) * curve0[b]) >> 12)
#define CURVE1( a, b ) (((a) * curve1[b]) >> 12)
#define CURVE2( a, b ) (((a) * curve2[b]) >> 12)
#define CURVE3( a, b ) (((a) * curve3[b]) >> 12)
typedef enum {
CURVE_NONE,
CURVE_CUSTOM,
CURVE_SYSTEM,
}CURVE_TYPE;
CURVE_TYPE current_curve_type = CURVE_NONE;
static void curve_free_data() {
free(curve_data);
curve_data = NULL;
current_curve_type = CURVE_NONE;
}
// TODO we should avoid re-loading the same file
static int curve_load_data(const char *name,CURVE_TYPE curve_type) {
unsigned size;
FILE *fd;
if (current_curve_type != curve_type) {
curve_free_data();
}
if (curve_type == CURVE_NONE) {
return 1;
}
if(!name || !*name) {
curve_free_data();
return 0;
}
if( curve_type == CURVE_CUSTOM ) {
size = CURVE_BYTES*4;
}
else {
size = CURVE_BYTES*2;
}
if (!curve_data)
curve_data=malloc(size);
if(!curve_data) {
curve_free_data();
return 0;
}
fd = fopen(name, "r+b");
if (!fd) {
curve_free_data();
return 0;
}
fread(curve_data, 1, size, fd);
fclose(fd);
current_curve_type = curve_type;
return 1;
}
void curve_set_mode(int value) {
if((value>=0) && (value<=4)) conf.curve_enable=value;
curve_init_mode();
}
void curve_init_mode() {
switch(conf.curve_enable) {
case 1: // custom - ensure alloc and load conf.curve_file
curve_load_data(conf.curve_file,CURVE_CUSTOM);
break;
case 2: // system - ensure alloc and load syscurve
case 3:
case 4:
curve_load_data("A/CHDK/SYSCURVES.CVF",CURVE_SYSTEM);
break;
default:
conf.curve_enable = 0;
case 0: // disabled - free
curve_free_data();
}
}
// TODO border pixels should not be hard coded
void curveRGB_apply() {
int i,j;
unsigned short pixVal0, pixVal1, pixVal2, col;
unsigned char *src;
unsigned short *curve0 = curve_data;
unsigned short *curve1 = curve0 + CURVE_SIZE;
unsigned short *curve2 = curve1 + CURVE_SIZE;
unsigned short *curve3 = curve2 + CURVE_SIZE;
if (current_curve_type != CURVE_CUSTOM)
return;
// Set pointer to picture raw data in memory
src = (unsigned char *) get_raw_image_addr();
// Loop through picture rows
for (i=CAM_RAW_ROWS; i;i-=2){
// Loop through picture columns
for (j=CAM_RAW_ROWPIX; j; j-=8, src+=10){
pixVal0=((0x3fc&(((unsigned short)(src[1]))<<2)) | (src[0] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[0]))<<4)) | (src[3] >> 4));
pixVal0 = curve0[pixVal0];
pixVal1 = curve1[pixVal1];
*(src+1) = (unsigned char) ((pixVal0>>2)); // 0
*src = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); //0, 1
pixVal2=((0x3c0&(((unsigned short)(src[3]))<<6)) | (src[2] >> 2));
pixVal0=((0x300&(((unsigned short)(src[2]))<<8)) | (src[5]));
pixVal2 = curve0[pixVal2];
pixVal0 = curve1[pixVal0];
*(src+3) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); //1,2
*(src+2) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); //2,3 =>(2,0)
*(src+5) = (unsigned char) ((pixVal0)); //3 (=>0)
pixVal0=((0x3fc&(((unsigned short)(src[4]))<<2)) | (src[7] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[7]))<<4)) | (src[6] >> 4));
pixVal0 = curve0[pixVal0];
pixVal1 = curve1[pixVal1];
*(src+4) = (unsigned char) ((pixVal0>>2)); // 4 => 0
*(src+7) = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); // 4,5 => (0,1)
pixVal2=((0x3c0&(((unsigned short)(src[6]))<<6)) | (src[9] >> 2));
pixVal0=((0x300&(((unsigned short)(src[9]))<<8)) | (src[8]));
pixVal2 = curve0[pixVal2];
pixVal0 = curve1[pixVal0];
*(src+6) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); // 5,6 => (1,2)
*(src+9) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); // 6,7 =>(2,0)
*(src+8) = (unsigned char) ((pixVal0)); //7 (=>0) }
}
for (j=CAM_RAW_ROWPIX;j; j-=8, src+=10){
pixVal0=((0x3fc&(((unsigned short)(src[1]))<<2)) | (src[0] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[0]))<<4)) | (src[3] >> 4));
pixVal0 = curve2[pixVal0];
pixVal1 = curve3[pixVal1];
*(src+1) = (unsigned char) ((pixVal0>>2)); // 0
*src = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); //0, 1
pixVal2=((0x3c0&(((unsigned short)(src[3]))<<6)) | (src[2] >> 2));
pixVal0=((0x300&(((unsigned short)(src[2]))<<8)) | (src[5]));
pixVal2 = curve2[pixVal2];
pixVal0 = curve3[pixVal0];
*(src+3) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); //1,2
*(src+2) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); //2,3 =>(2,0)
*(src+5) = (unsigned char) ((pixVal0)); //3 (=>0)
pixVal0=((0x3fc&(((unsigned short)(src[4]))<<2)) | (src[7] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[7]))<<4)) | (src[6] >> 4));
pixVal0 = curve2[pixVal0];
pixVal1 = curve3[pixVal1];
*(src+4) = (unsigned char) ((pixVal0>>2)); // 4 => 0
*(src+7) = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); // 4,5 => (0,1)
pixVal2=((0x3c0&(((unsigned short)(src[6]))<<6)) | (src[9] >> 2));
pixVal0=((0x300&(((unsigned short)(src[9]))<<8)) | (src[8]));
pixVal2 = curve2[pixVal2];
pixVal0 = curve3[pixVal0];
*(src+6) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); // 5,6 => (1,2)
*(src+9) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); // 6,7 =>(2,0)
*(src+8) = (unsigned char) ((pixVal0)); //7 (=>0)
}
}
}
void curveL_apply(unsigned sys_index) {
int i,j;
unsigned short pixVal0, pixVal1, pixVal2, col;
unsigned char *src;
unsigned short *curve0;
unsigned short *curve1;
unsigned short *curve2;
unsigned short *curve3;
if (current_curve_type == CURVE_SYSTEM) {
if (sys_index > 1)
return;
curve0 = curve1 = curve2 = curve3 = curve_data + sys_index*CURVE_SIZE;
}
else if(current_curve_type == CURVE_CUSTOM) {
curve0 = curve_data;
curve1 = curve0 + CURVE_SIZE;
curve2 = curve1 + CURVE_SIZE;
curve3 = curve2 + CURVE_SIZE;
} else {
return;
}
// Set pointer to picture raw data in memory
src = (unsigned char *) get_raw_image_addr();
// Loop through picture rows
for (i=CAM_RAW_ROWS; i;i-=2){
// Loop through picture columns
for (j=CAM_RAW_ROWPIX; j; j-=8, src+=10){
pixVal0=((0x3fc&(((unsigned short)(src[1]))<<2)) | (src[0] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[0]))<<4)) | (src[3] >> 4));
if (pixVal1) {
pixVal1 = RBLACK( pixVal1 );
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
pixVal0 = CURVE0( pixVal0, pixVal1 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
pixVal1 = CURVE1( pixVal1, pixVal1 ) + BLACK;
pixVal1 = LIM( pixVal1 );
}
*(src+1) = (unsigned char) ((pixVal0>>2)); // 0
*src = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); //0, 1
pixVal2=((0x3c0&(((unsigned short)(src[3]))<<6)) | (src[2] >> 2));
pixVal0=((0x300&(((unsigned short)(src[2]))<<8)) | (src[5]));
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
if (pixVal2) {
pixVal2 = RBLACK( pixVal2 );
pixVal2 = CURVE0( pixVal2, pixVal0 ) + BLACK;
pixVal2 = LIM( pixVal2 );
}
pixVal0 = CURVE1( pixVal0, pixVal0 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
*(src+3) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); //1,2
*(src+2) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); //2,3 =>(2,0)
*(src+5) = (unsigned char) ((pixVal0)); //3 (=>0)
pixVal0=((0x3fc&(((unsigned short)(src[4]))<<2)) | (src[7] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[7]))<<4)) | (src[6] >> 4));
if (pixVal1) {
pixVal1 = RBLACK( pixVal1 );
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
pixVal0 = CURVE0( pixVal0, pixVal1 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
pixVal1 = CURVE1( pixVal1, pixVal1 ) + BLACK;
pixVal1 = LIM( pixVal1 );
}
*(src+4) = (unsigned char) ((pixVal0>>2)); // 4 => 0
*(src+7) = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); // 4,5 => (0,1)
pixVal2=((0x3c0&(((unsigned short)(src[6]))<<6)) | (src[9] >> 2));
pixVal0=((0x300&(((unsigned short)(src[9]))<<8)) | (src[8]));
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
if (pixVal2) {
pixVal2 = RBLACK( pixVal2 );
pixVal2 = CURVE0( pixVal2, pixVal0 ) + BLACK;
pixVal2 = LIM( pixVal2 );
}
pixVal0 = CURVE1( pixVal0, pixVal0 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
*(src+6) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); // 5,6 => (1,2)
*(src+9) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); // 6,7 =>(2,0)
*(src+8) = (unsigned char) ((pixVal0)); //7 (=>0)
}
for (j=CAM_RAW_ROWPIX;j; j-=8, src+=10){
pixVal0=((0x3fc&(((unsigned short)(src[1]))<<2)) | (src[0] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[0]))<<4)) | (src[3] >> 4));
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
if (pixVal1) {
pixVal1 = RBLACK( pixVal1 );
pixVal1 = CURVE3( pixVal1, pixVal0 ) + BLACK;
pixVal1 = LIM( pixVal1 );
}
pixVal0 = CURVE2( pixVal0, pixVal0 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
*(src+1) = (unsigned char) ((pixVal0>>2)); // 0
*src = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); //0, 1
pixVal2=((0x3c0&(((unsigned short)(src[3]))<<6)) | (src[2] >> 2));
pixVal0=((0x300&(((unsigned short)(src[2]))<<8)) | (src[5]));
if (pixVal2) {
pixVal2 = RBLACK( pixVal2 );
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
pixVal0 = CURVE3( pixVal0, pixVal2 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
pixVal2 = CURVE2( pixVal2, pixVal2 ) + BLACK;
pixVal2 = LIM( pixVal2 );
}
*(src+3) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); //1,2
*(src+2) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); //2,3 =>(2,0)
*(src+5) = (unsigned char) ((pixVal0)); //3 (=>0)
pixVal0=((0x3fc&(((unsigned short)(src[4]))<<2)) | (src[7] >> 6));
pixVal1=((0x3f0&(((unsigned short)(src[7]))<<4)) | (src[6] >> 4));
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
if (pixVal1) {
pixVal1 = RBLACK( pixVal1 );
pixVal1 = CURVE3( pixVal1, pixVal0 ) + BLACK;
pixVal1 = LIM( pixVal1 );
}
pixVal0 = CURVE2( pixVal0, pixVal0 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
*(src+4) = (unsigned char) ((pixVal0>>2)); // 4 => 0
*(src+7) = (unsigned char) ((pixVal0<<6)|(pixVal1>>4)); // 4,5 => (0,1)
pixVal2=((0x3c0&(((unsigned short)(src[6]))<<6)) | (src[9] >> 2));
pixVal0=((0x300&(((unsigned short)(src[9]))<<8)) | (src[8]));
if (pixVal2) {
pixVal2 = RBLACK( pixVal2 );
if (pixVal0) {
pixVal0 = RBLACK( pixVal0 );
pixVal0 = CURVE3( pixVal0, pixVal2 ) + BLACK;
pixVal0 = LIM( pixVal0 );
}
pixVal2 = CURVE2( pixVal2, pixVal2 ) + BLACK;
pixVal2 = LIM( pixVal2 );
}
*(src+6) = (unsigned char) ((pixVal1<<4)|(pixVal2>>6)); // 5,6 => (1,2)
*(src+9) = (unsigned char) ((pixVal2<<2)|(pixVal0>>8)); // 6,7 =>(2,0)
*(src+8) = (unsigned char) ((pixVal0)); //7 (=>0)
}
}
}
void curve_apply() {
short EVbias = shooting_get_ev_correction1();
switch(conf.curve_enable) {
case 0:
break;
case 1: // Custom
if (current_curve_type == CURVE_CUSTOM){
if(curve_data[0] > 1023){ // L curve contains multiplicator factors (0 Luminance has factor 1)
curveL_apply(0);
} else {
curveRGB_apply();
}
};
break;
case 2:
case 3: // +1EV, +2EV
if (current_curve_type == CURVE_SYSTEM) curveL_apply( conf.curve_enable & 1 );
break;
case 4: // Auto DR
if (current_curve_type == CURVE_SYSTEM) {
if (EVbias <-128) { // x4 for Bias from "-1 2/3" and below
curveL_apply(1);
} else if(EVbias<-32){ // x2 for Bias from "-1 1/3" to "-2/3"
curveL_apply(0);
}
}
break;
}
}
#endif