1 /* -*- C++ -*-
2 * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
3 *
4 LibRaw uses code from dcraw.c -- Dave Coffin's raw photo decoder,
5 dcraw.c is copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net.
6 LibRaw do not use RESTRICTED code from dcraw.c
7
8 LibRaw is free software; you can redistribute it and/or modify
9 it under the terms of the one of two licenses as you choose:
10
11 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
12 (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
13
14 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
15 (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
16
17 */
18
19 #include "../../internal/dcraw_defs.h"
20
abs32(int32_t x)21 inline uint32_t abs32(int32_t x)
22 {
23 // Branchless version.
24 uint32_t sm = x >> 31;
25 return (uint32_t) ((x + sm) ^ sm);
26 }
27
min32(uint32_t x,uint32_t y)28 inline uint32_t min32(uint32_t x, uint32_t y)
29 {
30 return x < y ? x : y;
31 }
32
max32(uint32_t x,uint32_t y)33 inline uint32_t max32(uint32_t x, uint32_t y)
34 {
35 return x > y ? x : y;
36 }
37
constain32(uint32_t x,uint32_t l,uint32_t u)38 inline uint32_t constain32(uint32_t x, uint32_t l, uint32_t u)
39 {
40 return x < l ? l : (x > u ? u : x);
41 }
42
unsigned_cmp(const void * a,const void * b)43 int unsigned_cmp(const void *a, const void *b)
44 {
45 if (!a || !b)
46 return 0;
47
48 return *(unsigned *)a > *(unsigned *)b ? 1 : (*(unsigned *)a < *(unsigned *)b ? -1 : 0);
49 }
50
p1rawc(unsigned row,unsigned col,unsigned & count)51 int LibRaw::p1rawc(unsigned row, unsigned col, unsigned& count)
52 {
53 return (row < raw_height && col < raw_width) ? (++count, RAW(row, col)) : 0;
54 }
55
p1raw(unsigned row,unsigned col)56 int LibRaw::p1raw(unsigned row, unsigned col)
57 {
58 return (row < raw_height && col < raw_width) ? RAW(row, col) : 0;
59 }
60
61
62 // DNG SDK version of fixing pixels in bad column using averages sets
63 // corrected not to use pixels in the same column
phase_one_fix_col_pixel_avg(unsigned row,unsigned col)64 void LibRaw::phase_one_fix_col_pixel_avg(unsigned row, unsigned col)
65 {
66 static const int8_t dir[3][8][2] = {
67 { {-2,-2}, {-2, 2}, {2,-2}, {2, 2}, { 0, 0}, { 0, 0}, {0, 0}, {0, 0} },
68 { {-2,-4}, {-4,-2}, {2,-4}, {4,-2}, {-2, 4}, {-4, 2}, {2, 4}, {4, 2} },
69 { {-4,-4}, {-4, 4}, {4,-4}, {4, 4}, { 0, 0}, { 0, 0}, {0, 0}, {0, 0} } };
70
71 for (int set=0; set < 3; ++set)
72 {
73 uint32_t total = 0;
74 uint32_t count = 0;
75 for (int i = 0; i < 8; ++i)
76 {
77 if (!dir[set][i][0] && !dir[set][i][1])
78 break;
79
80 total += p1rawc(row+dir[set][i][0], col+dir[set][i][1], count);
81 }
82
83 if (count)
84 {
85 RAW(row,col) = (uint16_t)((total + (count >> 1)) / count);
86 break;
87 }
88 }
89 }
90
91 // DNG SDK version of fixing pixels in bad column using gradient prediction
phase_one_fix_pixel_grad(unsigned row,unsigned col)92 void LibRaw::phase_one_fix_pixel_grad(unsigned row, unsigned col)
93 {
94 static const int8_t grad_sets[7][12][2] = {
95 { {-4,-2}, { 4, 2}, {-3,-1}, { 1, 1}, {-1,-1}, { 3, 1},
96 {-4,-1}, { 0, 1}, {-2,-1}, { 2, 1}, { 0,-1}, { 4, 1} },
97 { {-2,-2}, { 2, 2}, {-3,-1}, {-1, 1}, {-1,-1}, { 1, 1},
98 { 1,-1}, { 3, 1}, {-2,-1}, { 0, 1}, { 0,-1}, { 2, 1} },
99 { {-2,-4}, { 2, 4}, {-1,-3}, { 1, 1}, {-1,-1}, { 1, 3},
100 {-2,-1}, { 0, 3}, {-1,-2}, { 1, 2}, { 0,-3}, { 2, 1} },
101 { { 0,-2}, { 0, 2}, {-1,-1}, {-1, 1}, { 1,-1}, { 1, 1},
102 {-1,-2}, {-1, 2}, { 0,-1}, { 0,-1}, { 1,-2}, { 1, 2} },
103 { {-2, 4}, { 2,-4}, {-1, 3}, { 1,-1}, {-1, 1}, { 1,-3},
104 {-2, 1}, { 0,-3}, {-1, 2}, { 1,-2}, { 0, 3}, { 2,-1} },
105 { {-2, 2}, { 2,-2}, {-3, 1}, {-1,-1}, {-1, 1}, { 1,-1},
106 { 1, 1}, { 3,-1}, {-2, 1}, { 0,-1}, { 0, 1}, { 2,-1} },
107 { {-4, 2}, { 4,-2}, {-3, 1}, { 1,-1}, {-1, 1}, { 3,-1},
108 {-4, 1}, { 0,-1}, {-2, 1}, { 2,-1}, { 0, 1}, { 4,-1} } };
109
110 uint32_t est[7], grad[7];
111 uint32_t lower = min32(p1raw(row,col-2), p1raw(row, col+2));
112 uint32_t upper = max32(p1raw(row,col-2), p1raw(row, col+2));
113 uint32_t minGrad = 0xFFFFFFFF;
114 for (int i = 0; i<7; ++i)
115 {
116 est[i] = p1raw(row+grad_sets[i][0][0], col+grad_sets[i][0][1]) +
117 p1raw(row+grad_sets[i][1][0], col+grad_sets[i][1][1]);
118 grad[i] = 0;
119 for (int j=0; j<12; j+=2)
120 grad[i] += abs32(p1raw(row+grad_sets[i][j][0], col+grad_sets[i][j][1]) -
121 p1raw(row+grad_sets[i][j+1][0], col+grad_sets[i][j+1][1]));
122 minGrad = min32(minGrad, grad[i]);
123 }
124
125 uint32_t limit = (minGrad * 3) >> 1;
126 uint32_t total = 0;
127 uint32_t count = 0;
128 for (int i = 0; i<7; ++i)
129 if (grad[i] <= limit)
130 {
131 total += est[i];
132 count += 2;
133 }
134 RAW(row, col) = constain32((total + (count >> 1)) / count, lower, upper);
135 }
136
phase_one_flat_field(int is_float,int nc)137 void LibRaw::phase_one_flat_field(int is_float, int nc)
138 {
139 ushort head[8];
140 unsigned wide, high, y, x, c, rend, cend, row, col;
141 float *mrow, num, mult[4];
142
143 read_shorts(head, 8);
144 if (head[2] == 0 || head[3] == 0 || head[4] == 0 || head[5] == 0)
145 return;
146 wide = head[2] / head[4] + (head[2] % head[4] != 0);
147 high = head[3] / head[5] + (head[3] % head[5] != 0);
148 mrow = (float *)calloc(nc * wide, sizeof *mrow);
149 merror(mrow, "phase_one_flat_field()");
150 for (y = 0; y < high; y++)
151 {
152 checkCancel();
153 for (x = 0; x < wide; x++)
154 for (c = 0; c < (unsigned)nc; c += 2)
155 {
156 num = is_float ? getreal(LIBRAW_EXIFTAG_TYPE_FLOAT) : get2() / 32768.0;
157 if (y == 0)
158 mrow[c * wide + x] = num;
159 else
160 mrow[(c + 1) * wide + x] = (num - mrow[c * wide + x]) / head[5];
161 }
162 if (y == 0)
163 continue;
164 rend = head[1] + y * head[5];
165 for (row = rend - head[5];
166 row < raw_height && row < rend && row < unsigned(head[1] + head[3] - head[5]);
167 row++)
168 {
169 for (x = 1; x < wide; x++)
170 {
171 for (c = 0; c < (unsigned)nc; c += 2)
172 {
173 mult[c] = mrow[c * wide + x - 1];
174 mult[c + 1] = (mrow[c * wide + x] - mult[c]) / head[4];
175 }
176 cend = head[0] + x * head[4];
177 for (col = cend - head[4];
178 col < raw_width && col < cend && col < unsigned(head[0] + head[2] - head[4]);
179 col++)
180 {
181 c = nc > 2 ? FC(row - top_margin, col - left_margin) : 0;
182 if (!(c & 1))
183 {
184 c = RAW(row, col) * mult[c];
185 RAW(row, col) = LIM(c, 0, 65535);
186 }
187 for (c = 0; c < (unsigned)nc; c += 2)
188 mult[c] += mult[c + 1];
189 }
190 }
191 for (x = 0; x < wide; x++)
192 for (c = 0; c < (unsigned)nc; c += 2)
193 mrow[c * wide + x] += mrow[(c + 1) * wide + x];
194 }
195 }
196 free(mrow);
197 }
198
phase_one_correct()199 int LibRaw::phase_one_correct()
200 {
201 unsigned entries, tag, data, save, col, row, type;
202 int len, i, j, k, cip, sum;
203 #if 0
204 int val[4], dev[4], max;
205 #endif
206 int head[9], diff, mindiff = INT_MAX, off_412 = 0;
207 /* static */ const signed char dir[12][2] = {
208 {-1, -1}, {-1, 1}, {1, -1}, {1, 1}, {-2, 0}, {0, -2},
209 {0, 2}, {2, 0}, {-2, -2}, {-2, 2}, {2, -2}, {2, 2}};
210 float poly[8], num, cfrac, frac, mult[2], *yval[2] = {NULL, NULL};
211 ushort *xval[2];
212 int qmult_applied = 0, qlin_applied = 0;
213 std::vector<unsigned> badCols;
214
215 if (!meta_length)
216 return 0;
217 fseek(ifp, meta_offset, SEEK_SET);
218 order = get2();
219 fseek(ifp, 6, SEEK_CUR);
220 fseek(ifp, meta_offset + get4(), SEEK_SET);
221 entries = get4();
222 get4();
223
224 try
225 {
226 while (entries--)
227 {
228 checkCancel();
229 tag = get4();
230 len = get4();
231 data = get4();
232 save = ftell(ifp);
233 fseek(ifp, meta_offset + data, SEEK_SET);
234 if (tag == 0x0400)
235 { /* Sensor defects */
236 while ((len -= 8) >= 0)
237 {
238 col = get2();
239 row = get2();
240 type = get2();
241 get2();
242 if (col >= raw_width)
243 continue;
244 if (type == 131 || type == 137) /* Bad column */
245 #if 0
246 // Original code by Dave Coffin - it works better by
247 // not employing special logic for G1 channel below.
248 // Alternatively this column remap (including G1 channel
249 // logic) should be called prior to black subtraction
250 // unlike other corrections
251 for (row = 0; row < raw_height; row++)
252 {
253 if (FC(row - top_margin, col - left_margin)==1)
254 {
255 for (sum = i = 0; i < 4; i++)
256 sum += val[i] = p1raw(row + dir[i][0], col + dir[i][1]);
257 for (max = i = 0; i < 4; i++)
258 {
259 dev[i] = abs((val[i] << 2) - sum);
260 if (dev[max] < dev[i])
261 max = i;
262 }
263 RAW(row, col) = (sum - val[max]) / 3.0 + 0.5;
264 }
265 else
266 {
267 for (sum = 0, i = 8; i < 12; i++)
268 sum += p1raw(row + dir[i][0], col + dir[i][1]);
269 RAW(row, col) =
270 0.5 + sum * 0.0732233 +
271 (p1raw(row, col - 2) + p1raw(row, col + 2)) * 0.3535534;
272 }
273 }
274 #else
275 // accumulae bad columns to be sorted later
276 badCols.push_back(col);
277 #endif
278 else if (type == 129)
279 { /* Bad pixel */
280 if (row >= raw_height)
281 continue;
282 j = (FC(row - top_margin, col - left_margin) != 1) * 4;
283 unsigned count = 0;
284 for (sum = 0, i = j; i < j + 8; i++)
285 sum += p1rawc(row + dir[i][0], col + dir[i][1], count);
286 if (count)
287 RAW(row, col) = (sum + (count >> 1)) / count;
288 }
289 }
290 }
291 else if (tag == 0x0419)
292 { /* Polynomial curve - output calibraion */
293 for (get4(), i = 0; i < 8; i++)
294 poly[i] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
295 poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
296 for (i = 0; i < 0x10000; i++)
297 {
298 num = (poly[5] * i + poly[3]) * i + poly[1];
299 curve[i] = LIM(num, 0, 65535);
300 }
301 goto apply; /* apply to right half */
302 }
303 else if (tag == 0x041a)
304 { /* Polynomial curve */
305 for (i = 0; i < 4; i++)
306 poly[i] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
307 for (i = 0; i < 0x10000; i++)
308 {
309 for (num = 0, j = 4; j--;)
310 num = num * i + poly[j];
311 curve[i] = LIM(num + i, 0, 65535);
312 }
313 apply: /* apply to whole image */
314 for (row = 0; row < raw_height; row++)
315 {
316 checkCancel();
317 for (col = (tag & 1) * ph1.split_col; col < raw_width; col++)
318 RAW(row, col) = curve[RAW(row, col)];
319 }
320 }
321 else if (tag == 0x0401)
322 { /* All-color flat fields - luma calibration*/
323 phase_one_flat_field(1, 2);
324 }
325 else if (tag == 0x0416 || tag == 0x0410)
326 {
327 // 0x410 - luma calibration
328 phase_one_flat_field(0, 2);
329 }
330 else if (tag == 0x040b)
331 { /* Red+blue flat field - croma calibration */
332 phase_one_flat_field(0, 4);
333 }
334 else if (tag == 0x0412)
335 {
336 fseek(ifp, 36, SEEK_CUR);
337 diff = abs(get2() - ph1.tag_21a);
338 if (mindiff > diff)
339 {
340 mindiff = diff;
341 off_412 = ftell(ifp) - 38;
342 }
343 }
344 else if (tag == 0x041f && !qlin_applied)
345 { /* Quadrant linearization */
346 ushort lc[2][2][16], ref[16];
347 int qr, qc;
348 for (qr = 0; qr < 2; qr++)
349 for (qc = 0; qc < 2; qc++)
350 for (i = 0; i < 16; i++)
351 lc[qr][qc][i] = get4();
352 for (i = 0; i < 16; i++)
353 {
354 int v = 0;
355 for (qr = 0; qr < 2; qr++)
356 for (qc = 0; qc < 2; qc++)
357 v += lc[qr][qc][i];
358 ref[i] = (v + 2) >> 2;
359 }
360 for (qr = 0; qr < 2; qr++)
361 {
362 for (qc = 0; qc < 2; qc++)
363 {
364 int cx[19], cf[19];
365 for (i = 0; i < 16; i++)
366 {
367 cx[1 + i] = lc[qr][qc][i];
368 cf[1 + i] = ref[i];
369 }
370 cx[0] = cf[0] = 0;
371 cx[17] = cf[17] = ((unsigned int)ref[15] * 65535) / lc[qr][qc][15];
372 cf[18] = cx[18] = 65535;
373 cubic_spline(cx, cf, 19);
374
375 for (row = (qr ? ph1.split_row : 0);
376 row < unsigned(qr ? raw_height : ph1.split_row); row++)
377 {
378 checkCancel();
379 for (col = (qc ? ph1.split_col : 0);
380 col < unsigned(qc ? raw_width : ph1.split_col); col++)
381 RAW(row, col) = curve[RAW(row, col)];
382 }
383 }
384 }
385 qlin_applied = 1;
386 }
387 else if (tag == 0x041e && !qmult_applied)
388 { /* Quadrant multipliers - output calibraion */
389 float qmult[2][2] = {{1, 1}, {1, 1}};
390 get4();
391 get4();
392 get4();
393 get4();
394 qmult[0][0] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
395 get4();
396 get4();
397 get4();
398 get4();
399 get4();
400 qmult[0][1] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
401 get4();
402 get4();
403 get4();
404 qmult[1][0] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
405 get4();
406 get4();
407 get4();
408 qmult[1][1] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
409 for (row = 0; row < raw_height; row++)
410 {
411 checkCancel();
412 for (col = 0; col < raw_width; col++)
413 {
414 i = qmult[row >= (unsigned)ph1.split_row][col >= (unsigned)ph1.split_col] *
415 RAW(row, col);
416 RAW(row, col) = LIM(i, 0, 65535);
417 }
418 }
419 qmult_applied = 1;
420 }
421 else if (tag == 0x0431 && !qmult_applied)
422 { /* Quadrant combined - four tile gain calibration */
423 ushort lc[2][2][7], ref[7];
424 int qr, qc;
425 for (i = 0; i < 7; i++)
426 ref[i] = get4();
427 for (qr = 0; qr < 2; qr++)
428 for (qc = 0; qc < 2; qc++)
429 for (i = 0; i < 7; i++)
430 lc[qr][qc][i] = get4();
431 for (qr = 0; qr < 2; qr++)
432 {
433 for (qc = 0; qc < 2; qc++)
434 {
435 int cx[9], cf[9];
436 for (i = 0; i < 7; i++)
437 {
438 cx[1 + i] = ref[i];
439 cf[1 + i] = ((unsigned)ref[i] * lc[qr][qc][i]) / 10000;
440 }
441 cx[0] = cf[0] = 0;
442 cx[8] = cf[8] = 65535;
443 cubic_spline(cx, cf, 9);
444 for (row = (qr ? ph1.split_row : 0);
445 row < unsigned(qr ? raw_height : ph1.split_row); row++)
446 {
447 checkCancel();
448 for (col = (qc ? ph1.split_col : 0);
449 col < unsigned(qc ? raw_width : ph1.split_col); col++)
450 RAW(row, col) = curve[RAW(row, col)];
451 }
452 }
453 }
454 qmult_applied = 1;
455 qlin_applied = 1;
456 }
457 fseek(ifp, save, SEEK_SET);
458 }
459 if (!badCols.empty())
460 {
461 qsort(badCols.data(), badCols.size(), sizeof(unsigned), unsigned_cmp);
462 bool prevIsolated = true;
463 for (i = 0; i < (int)badCols.size(); ++i)
464 {
465 bool nextIsolated = i == ((int)(badCols.size()-1)) || badCols[i+1]>badCols[i]+4;
466 for (row = 0; row < raw_height; ++row)
467 if (prevIsolated && nextIsolated)
468 phase_one_fix_pixel_grad(row,badCols[i]);
469 else
470 phase_one_fix_col_pixel_avg(row,badCols[i]);
471 prevIsolated = nextIsolated;
472 }
473 }
474 if (off_412)
475 {
476 fseek(ifp, off_412, SEEK_SET);
477 for (i = 0; i < 9; i++)
478 head[i] = get4() & 0x7fff;
479 yval[0] = (float *)calloc(head[1] * head[3] + head[2] * head[4], 6);
480 merror(yval[0], "phase_one_correct()");
481 yval[1] = (float *)(yval[0] + head[1] * head[3]);
482 xval[0] = (ushort *)(yval[1] + head[2] * head[4]);
483 xval[1] = (ushort *)(xval[0] + head[1] * head[3]);
484 get2();
485 for (i = 0; i < 2; i++)
486 for (j = 0; j < head[i + 1] * head[i + 3]; j++)
487 yval[i][j] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
488 for (i = 0; i < 2; i++)
489 for (j = 0; j < head[i + 1] * head[i + 3]; j++)
490 xval[i][j] = get2();
491 for (row = 0; row < raw_height; row++)
492 {
493 checkCancel();
494 for (col = 0; col < raw_width; col++)
495 {
496 cfrac = (float)col * head[3] / raw_width;
497 cfrac -= cip = cfrac;
498 num = RAW(row, col) * 0.5;
499 for (i = cip; i < cip + 2; i++)
500 {
501 for (k = j = 0; j < head[1]; j++)
502 if (num < xval[0][k = head[1] * i + j])
503 break;
504 frac = (j == 0 || j == head[1])
505 ? 0
506 : (xval[0][k] - num) / (xval[0][k] - xval[0][k - 1]);
507 mult[i - cip] = yval[0][k - 1] * frac + yval[0][k] * (1 - frac);
508 }
509 i = ((mult[0] * (1 - cfrac) + mult[1] * cfrac) * row + num) * 2;
510 RAW(row, col) = LIM(i, 0, 65535);
511 }
512 }
513 free(yval[0]);
514 }
515 }
516 catch (...)
517 {
518 if (yval[0])
519 free(yval[0]);
520 return LIBRAW_CANCELLED_BY_CALLBACK;
521 }
522 return 0;
523 }
524
phase_one_load_raw()525 void LibRaw::phase_one_load_raw()
526 {
527 int a, b, i;
528 ushort akey, bkey, t_mask;
529
530 fseek(ifp, ph1.key_off, SEEK_SET);
531 akey = get2();
532 bkey = get2();
533 t_mask = ph1.format == 1 ? 0x5555 : 0x1354;
534 if (ph1.black_col || ph1.black_row)
535 {
536 imgdata.rawdata.ph1_cblack =
537 (short(*)[2])calloc(raw_height * 2, sizeof(ushort));
538 merror(imgdata.rawdata.ph1_cblack, "phase_one_load_raw()");
539 imgdata.rawdata.ph1_rblack =
540 (short(*)[2])calloc(raw_width * 2, sizeof(ushort));
541 merror(imgdata.rawdata.ph1_rblack, "phase_one_load_raw()");
542 if (ph1.black_col)
543 {
544 fseek(ifp, ph1.black_col, SEEK_SET);
545 read_shorts((ushort *)imgdata.rawdata.ph1_cblack[0], raw_height * 2);
546 }
547 if (ph1.black_row)
548 {
549 fseek(ifp, ph1.black_row, SEEK_SET);
550 read_shorts((ushort *)imgdata.rawdata.ph1_rblack[0], raw_width * 2);
551 }
552 }
553 fseek(ifp, data_offset, SEEK_SET);
554 read_shorts(raw_image, raw_width * raw_height);
555 if (ph1.format)
556 for (i = 0; i < raw_width * raw_height; i += 2)
557 {
558 a = raw_image[i + 0] ^ akey;
559 b = raw_image[i + 1] ^ bkey;
560 raw_image[i + 0] = (a & t_mask) | (b & ~t_mask);
561 raw_image[i + 1] = (b & t_mask) | (a & ~t_mask);
562 }
563 }
564
ph1_bithuff(int nbits,ushort * huff)565 unsigned LibRaw::ph1_bithuff(int nbits, ushort *huff)
566 {
567 #ifndef LIBRAW_NOTHREADS
568 #define bitbuf tls->ph1_bits.bitbuf
569 #define vbits tls->ph1_bits.vbits
570 #else
571 static UINT64 bitbuf = 0;
572 static int vbits = 0;
573 #endif
574 unsigned c;
575
576 if (nbits == -1)
577 return bitbuf = vbits = 0;
578 if (nbits == 0)
579 return 0;
580 if (vbits < nbits)
581 {
582 bitbuf = bitbuf << 32 | get4();
583 vbits += 32;
584 }
585 c = bitbuf << (64 - vbits) >> (64 - nbits);
586 if (huff)
587 {
588 vbits -= huff[c] >> 8;
589 return (uchar)huff[c];
590 }
591 vbits -= nbits;
592 return c;
593 #ifndef LIBRAW_NOTHREADS
594 #undef bitbuf
595 #undef vbits
596 #endif
597 }
598
phase_one_load_raw_c()599 void LibRaw::phase_one_load_raw_c()
600 {
601 static const int length[] = {8, 7, 6, 9, 11, 10, 5, 12, 14, 13};
602 int *offset, len[2], pred[2], row, col, i, j;
603 ushort *pixel;
604 short(*c_black)[2], (*r_black)[2];
605 if (ph1.format == 6)
606 throw LIBRAW_EXCEPTION_IO_CORRUPT;
607
608 pixel = (ushort *)calloc(raw_width * 3 + raw_height * 4, 2);
609 merror(pixel, "phase_one_load_raw_c()");
610 offset = (int *)(pixel + raw_width);
611 fseek(ifp, strip_offset, SEEK_SET);
612 for (row = 0; row < raw_height; row++)
613 offset[row] = get4();
614 c_black = (short(*)[2])(offset + raw_height);
615 fseek(ifp, ph1.black_col, SEEK_SET);
616 if (ph1.black_col)
617 read_shorts((ushort *)c_black[0], raw_height * 2);
618 r_black = c_black + raw_height;
619 fseek(ifp, ph1.black_row, SEEK_SET);
620 if (ph1.black_row)
621 read_shorts((ushort *)r_black[0], raw_width * 2);
622
623 // Copy data to internal copy (ever if not read)
624 if (ph1.black_col || ph1.black_row)
625 {
626 imgdata.rawdata.ph1_cblack =
627 (short(*)[2])calloc(raw_height * 2, sizeof(ushort));
628 merror(imgdata.rawdata.ph1_cblack, "phase_one_load_raw_c()");
629 memmove(imgdata.rawdata.ph1_cblack, (ushort *)c_black[0],
630 raw_height * 2 * sizeof(ushort));
631 imgdata.rawdata.ph1_rblack =
632 (short(*)[2])calloc(raw_width * 2, sizeof(ushort));
633 merror(imgdata.rawdata.ph1_rblack, "phase_one_load_raw_c()");
634 memmove(imgdata.rawdata.ph1_rblack, (ushort *)r_black[0],
635 raw_width * 2 * sizeof(ushort));
636 }
637
638 for (i = 0; i < 256; i++)
639 curve[i] = i * i / 3.969 + 0.5;
640 try
641 {
642 for (row = 0; row < raw_height; row++)
643 {
644 checkCancel();
645 fseek(ifp, data_offset + offset[row], SEEK_SET);
646 ph1_bits(-1);
647 pred[0] = pred[1] = 0;
648 for (col = 0; col < raw_width; col++)
649 {
650 if (col >= (raw_width & -8))
651 len[0] = len[1] = 14;
652 else if ((col & 7) == 0)
653 for (i = 0; i < 2; i++)
654 {
655 for (j = 0; j < 5 && !ph1_bits(1); j++)
656 ;
657 if (j--)
658 len[i] = length[j * 2 + ph1_bits(1)];
659 }
660 if ((i = len[col & 1]) == 14)
661 pixel[col] = pred[col & 1] = ph1_bits(16);
662 else
663 pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
664 if (pred[col & 1] >> 16)
665 derror();
666 if (ph1.format == 5 && pixel[col] < 256)
667 pixel[col] = curve[pixel[col]];
668 }
669 if (ph1.format == 8)
670 memmove(&RAW(row, 0), &pixel[0], raw_width * 2);
671 else
672 for (col = 0; col < raw_width; col++)
673 RAW(row, col) = pixel[col] << 2;
674 }
675 }
676 catch (...)
677 {
678 free(pixel);
679 throw;
680 }
681 free(pixel);
682 maximum = 0xfffc - ph1.t_black;
683 }
684
hasselblad_load_raw()685 void LibRaw::hasselblad_load_raw()
686 {
687 struct jhead jh;
688 int shot, row, col, *back[5]={0,0,0,0,0},
689 len[2], diff[12], pred, sh, f, c;
690 unsigned s;
691 unsigned upix, urow, ucol;
692 ushort *ip;
693
694 if (!ljpeg_start(&jh, 0))
695 return;
696 order = 0x4949;
697 ph1_bits(-1);
698 try
699 {
700 back[4] = (int *)calloc(raw_width, 3 * sizeof **back);
701 merror(back[4], "hasselblad_load_raw()");
702 FORC3 back[c] = back[4] + c * raw_width;
703 cblack[6] >>= sh = tiff_samples > 1;
704 shot = LIM(shot_select, 1, tiff_samples) - 1;
705 for (row = 0; row < raw_height; row++)
706 {
707 checkCancel();
708 FORC4 back[(c + 3) & 3] = back[c];
709 for (col = 0; col < raw_width; col += 2)
710 {
711 for (s = 0; s < tiff_samples * 2; s += 2)
712 {
713 FORC(2) len[c] = ph1_huff(jh.huff[0]);
714 FORC(2)
715 {
716 diff[s + c] = ph1_bits(len[c]);
717 if (len[c] > 0 && (diff[s + c] & (1 << (len[c] - 1))) == 0)
718 diff[s + c] -= (1 << len[c]) - 1;
719 if (diff[s + c] == 65535)
720 diff[s + c] = -32768;
721 }
722 }
723 for (s = col; s < unsigned(col + 2); s++)
724 {
725 pred = 0x8000 + load_flags;
726 if (col)
727 pred = back[2][s - 2];
728 if (col && row > 1)
729 switch (jh.psv)
730 {
731 case 11:
732 pred += back[0][s] / 2 - back[0][s - 2] / 2;
733 break;
734 }
735 f = (row & 1) * 3 ^ ((col + s) & 1);
736 FORC(int(tiff_samples))
737 {
738 pred += diff[(s & 1) * tiff_samples + c];
739 upix = pred >> sh & 0xffff;
740 if (raw_image && c == shot)
741 RAW(row, s) = upix;
742 if (image)
743 {
744 urow = row - top_margin + (c & 1);
745 ucol = col - left_margin - ((c >> 1) & 1);
746 ip = &image[urow * width + ucol][f];
747 if (urow < height && ucol < width)
748 *ip = c < 4 ? upix : (*ip + upix) >> 1;
749 }
750 }
751 back[2][s] = pred;
752 }
753 }
754 }
755 }
756 catch (...)
757 {
758 if(back[4])
759 free(back[4]);
760 ljpeg_end(&jh);
761 throw;
762 }
763 if(back[4])
764 free(back[4]);
765 ljpeg_end(&jh);
766 if (image)
767 mix_green = 1;
768 }
769
leaf_hdr_load_raw()770 void LibRaw::leaf_hdr_load_raw()
771 {
772 ushort *pixel = 0;
773 unsigned tile = 0, r, c, row, col;
774
775 if (!filters || !raw_image)
776 {
777 if (!image)
778 throw LIBRAW_EXCEPTION_IO_CORRUPT;
779 pixel = (ushort *)calloc(raw_width, sizeof *pixel);
780 merror(pixel, "leaf_hdr_load_raw()");
781 }
782 try
783 {
784 FORC(tiff_samples)
785 for (r = 0; r < raw_height; r++)
786 {
787 checkCancel();
788 if (r % tile_length == 0)
789 {
790 fseek(ifp, data_offset + 4 * tile++, SEEK_SET);
791 fseek(ifp, get4(), SEEK_SET);
792 }
793 if (filters && c != shot_select)
794 continue;
795 if (filters && raw_image)
796 pixel = raw_image + r * raw_width;
797 read_shorts(pixel, raw_width);
798 if (!filters && image && (row = r - top_margin) < height)
799 for (col = 0; col < width && col + left_margin < raw_width; col++)
800 image[row * width + col][c] = pixel[col + left_margin];
801 }
802 }
803 catch (...)
804 {
805 if (!filters)
806 free(pixel);
807 throw;
808 }
809 if (!filters)
810 {
811 maximum = 0xffff;
812 raw_color = 1;
813 free(pixel);
814 }
815 }
816
unpacked_load_raw_FujiDBP()817 void LibRaw::unpacked_load_raw_FujiDBP()
818 /*
819 for Fuji DBP for GX680, aka DX-2000
820 DBP_tile_width = 688;
821 DBP_tile_height = 3856;
822 DBP_n_tiles = 8;
823 */
824 {
825 int scan_line, tile_n;
826 int nTiles;
827
828 nTiles = 8;
829 tile_width = raw_width / nTiles;
830
831 ushort *tile;
832 tile = (ushort *)calloc(raw_height, tile_width * 2);
833
834 for (tile_n = 0; tile_n < nTiles; tile_n++)
835 {
836 read_shorts(tile, tile_width * raw_height);
837 for (scan_line = 0; scan_line < raw_height; scan_line++)
838 {
839 memcpy(&raw_image[scan_line * raw_width + tile_n * tile_width],
840 &tile[scan_line * tile_width], tile_width * 2);
841 }
842 }
843 free(tile);
844 fseek(ifp, -2, SEEK_CUR); // avoid EOF error
845 }
846
sinar_4shot_load_raw()847 void LibRaw::sinar_4shot_load_raw()
848 {
849 ushort *pixel;
850 unsigned shot, row, col, r, c;
851
852 if (raw_image)
853 {
854 shot = LIM(shot_select, 1, 4) - 1;
855 fseek(ifp, data_offset + shot * 4, SEEK_SET);
856 fseek(ifp, get4(), SEEK_SET);
857 unpacked_load_raw();
858 return;
859 }
860 if (!image)
861 throw LIBRAW_EXCEPTION_IO_CORRUPT;
862 pixel = (ushort *)calloc(raw_width, sizeof *pixel);
863 merror(pixel, "sinar_4shot_load_raw()");
864 try
865 {
866 for (shot = 0; shot < 4; shot++)
867 {
868 checkCancel();
869 fseek(ifp, data_offset + shot * 4, SEEK_SET);
870 fseek(ifp, get4(), SEEK_SET);
871 for (row = 0; row < raw_height; row++)
872 {
873 read_shorts(pixel, raw_width);
874 if ((r = row - top_margin - (shot >> 1 & 1)) >= height)
875 continue;
876 for (col = 0; col < raw_width; col++)
877 {
878 if ((c = col - left_margin - (shot & 1)) >= width)
879 continue;
880 image[r * width + c][(row & 1) * 3 ^ (~col & 1)] = pixel[col];
881 }
882 }
883 }
884 }
885 catch (...)
886 {
887 free(pixel);
888 throw;
889 }
890 free(pixel);
891 mix_green = 1;
892 }
893
imacon_full_load_raw()894 void LibRaw::imacon_full_load_raw()
895 {
896 if (!image)
897 throw LIBRAW_EXCEPTION_IO_CORRUPT;
898 int row, col;
899
900 unsigned short *buf =
901 (unsigned short *)malloc(width * 3 * sizeof(unsigned short));
902 merror(buf, "imacon_full_load_raw");
903
904 for (row = 0; row < height; row++)
905 {
906 checkCancel();
907 read_shorts(buf, width * 3);
908 unsigned short(*rowp)[4] = &image[row * width];
909 for (col = 0; col < width; col++)
910 {
911 rowp[col][0] = buf[col * 3];
912 rowp[col][1] = buf[col * 3 + 1];
913 rowp[col][2] = buf[col * 3 + 2];
914 rowp[col][3] = 0;
915 }
916 }
917 free(buf);
918 }
919