File indexing completed on 2025-01-05 03:56:54
0001 /* -*- C++ -*- 0002 * Copyright 2019-2021 LibRaw LLC (info@libraw.org) 0003 * 0004 LibRaw uses code from dcraw.c -- Dave Coffin's raw photo decoder, 0005 dcraw.c is copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net. 0006 LibRaw do not use RESTRICTED code from dcraw.c 0007 0008 LibRaw is free software; you can redistribute it and/or modify 0009 it under the terms of the one of two licenses as you choose: 0010 0011 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1 0012 (See file LICENSE.LGPL provided in LibRaw distribution archive for details). 0013 0014 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 0015 (See file LICENSE.CDDL provided in LibRaw distribution archive for details). 0016 0017 */ 0018 0019 #include "../../internal/dcraw_defs.h" 0020 0021 inline uint32_t abs32(int32_t x) 0022 { 0023 // Branchless version. 0024 uint32_t sm = x >> 31; 0025 return (uint32_t) ((x + sm) ^ sm); 0026 } 0027 0028 inline uint32_t min32(uint32_t x, uint32_t y) 0029 { 0030 return x < y ? x : y; 0031 } 0032 0033 inline uint32_t max32(uint32_t x, uint32_t y) 0034 { 0035 return x > y ? x : y; 0036 } 0037 0038 inline uint32_t constain32(uint32_t x, uint32_t l, uint32_t u) 0039 { 0040 return x < l ? l : (x > u ? u : x); 0041 } 0042 0043 int unsigned_cmp(const void *a, const void *b) 0044 { 0045 if (!a || !b) 0046 return 0; 0047 0048 return *(unsigned *)a > *(unsigned *)b ? 1 : (*(unsigned *)a < *(unsigned *)b ? -1 : 0); 0049 } 0050 0051 int LibRaw::p1rawc(unsigned row, unsigned col, unsigned& count) 0052 { 0053 return (row < raw_height && col < raw_width) ? (++count, RAW(row, col)) : 0; 0054 } 0055 0056 int LibRaw::p1raw(unsigned row, unsigned col) 0057 { 0058 return (row < raw_height && col < raw_width) ? RAW(row, col) : 0; 0059 } 0060 0061 0062 // DNG SDK version of fixing pixels in bad column using averages sets 0063 // corrected not to use pixels in the same column 0064 void LibRaw::phase_one_fix_col_pixel_avg(unsigned row, unsigned col) 0065 { 0066 static const int8_t dir[3][8][2] = { 0067 { {-2,-2}, {-2, 2}, {2,-2}, {2, 2}, { 0, 0}, { 0, 0}, {0, 0}, {0, 0} }, 0068 { {-2,-4}, {-4,-2}, {2,-4}, {4,-2}, {-2, 4}, {-4, 2}, {2, 4}, {4, 2} }, 0069 { {-4,-4}, {-4, 4}, {4,-4}, {4, 4}, { 0, 0}, { 0, 0}, {0, 0}, {0, 0} } }; 0070 0071 for (int set=0; set < 3; ++set) 0072 { 0073 uint32_t total = 0; 0074 uint32_t count = 0; 0075 for (int i = 0; i < 8; ++i) 0076 { 0077 if (!dir[set][i][0] && !dir[set][i][1]) 0078 break; 0079 0080 total += p1rawc(row+dir[set][i][0], col+dir[set][i][1], count); 0081 } 0082 0083 if (count) 0084 { 0085 RAW(row,col) = (uint16_t)((total + (count >> 1)) / count); 0086 break; 0087 } 0088 } 0089 } 0090 0091 // DNG SDK version of fixing pixels in bad column using gradient prediction 0092 void LibRaw::phase_one_fix_pixel_grad(unsigned row, unsigned col) 0093 { 0094 static const int8_t grad_sets[7][12][2] = { 0095 { {-4,-2}, { 4, 2}, {-3,-1}, { 1, 1}, {-1,-1}, { 3, 1}, 0096 {-4,-1}, { 0, 1}, {-2,-1}, { 2, 1}, { 0,-1}, { 4, 1} }, 0097 { {-2,-2}, { 2, 2}, {-3,-1}, {-1, 1}, {-1,-1}, { 1, 1}, 0098 { 1,-1}, { 3, 1}, {-2,-1}, { 0, 1}, { 0,-1}, { 2, 1} }, 0099 { {-2,-4}, { 2, 4}, {-1,-3}, { 1, 1}, {-1,-1}, { 1, 3}, 0100 {-2,-1}, { 0, 3}, {-1,-2}, { 1, 2}, { 0,-3}, { 2, 1} }, 0101 { { 0,-2}, { 0, 2}, {-1,-1}, {-1, 1}, { 1,-1}, { 1, 1}, 0102 {-1,-2}, {-1, 2}, { 0,-1}, { 0,-1}, { 1,-2}, { 1, 2} }, 0103 { {-2, 4}, { 2,-4}, {-1, 3}, { 1,-1}, {-1, 1}, { 1,-3}, 0104 {-2, 1}, { 0,-3}, {-1, 2}, { 1,-2}, { 0, 3}, { 2,-1} }, 0105 { {-2, 2}, { 2,-2}, {-3, 1}, {-1,-1}, {-1, 1}, { 1,-1}, 0106 { 1, 1}, { 3,-1}, {-2, 1}, { 0,-1}, { 0, 1}, { 2,-1} }, 0107 { {-4, 2}, { 4,-2}, {-3, 1}, { 1,-1}, {-1, 1}, { 3,-1}, 0108 {-4, 1}, { 0,-1}, {-2, 1}, { 2,-1}, { 0, 1}, { 4,-1} } }; 0109 0110 uint32_t est[7], grad[7]; 0111 uint32_t lower = min32(p1raw(row,col-2), p1raw(row, col+2)); 0112 uint32_t upper = max32(p1raw(row,col-2), p1raw(row, col+2)); 0113 uint32_t minGrad = 0xFFFFFFFF; 0114 for (int i = 0; i<7; ++i) 0115 { 0116 est[i] = p1raw(row+grad_sets[i][0][0], col+grad_sets[i][0][1]) + 0117 p1raw(row+grad_sets[i][1][0], col+grad_sets[i][1][1]); 0118 grad[i] = 0; 0119 for (int j=0; j<12; j+=2) 0120 grad[i] += abs32(p1raw(row+grad_sets[i][j][0], col+grad_sets[i][j][1]) - 0121 p1raw(row+grad_sets[i][j+1][0], col+grad_sets[i][j+1][1])); 0122 minGrad = min32(minGrad, grad[i]); 0123 } 0124 0125 uint32_t limit = (minGrad * 3) >> 1; 0126 uint32_t total = 0; 0127 uint32_t count = 0; 0128 for (int i = 0; i<7; ++i) 0129 if (grad[i] <= limit) 0130 { 0131 total += est[i]; 0132 count += 2; 0133 } 0134 RAW(row, col) = constain32((total + (count >> 1)) / count, lower, upper); 0135 } 0136 0137 void LibRaw::phase_one_flat_field(int is_float, int nc) 0138 { 0139 ushort head[8]; 0140 unsigned wide, high, y, x, c, rend, cend, row, col; 0141 float *mrow, num, mult[4]; 0142 0143 read_shorts(head, 8); 0144 if (head[2] == 0 || head[3] == 0 || head[4] == 0 || head[5] == 0) 0145 return; 0146 wide = head[2] / head[4] + (head[2] % head[4] != 0); 0147 high = head[3] / head[5] + (head[3] % head[5] != 0); 0148 mrow = (float *)calloc(nc * wide, sizeof *mrow); 0149 for (y = 0; y < high; y++) 0150 { 0151 checkCancel(); 0152 for (x = 0; x < wide; x++) 0153 for (c = 0; c < (unsigned)nc; c += 2) 0154 { 0155 num = is_float ? getreal(LIBRAW_EXIFTAG_TYPE_FLOAT) : get2() / 32768.0; 0156 if (y == 0) 0157 mrow[c * wide + x] = num; 0158 else 0159 mrow[(c + 1) * wide + x] = (num - mrow[c * wide + x]) / head[5]; 0160 } 0161 if (y == 0) 0162 continue; 0163 rend = head[1] + y * head[5]; 0164 for (row = rend - head[5]; 0165 row < raw_height && row < rend && row < unsigned(head[1] + head[3] - head[5]); 0166 row++) 0167 { 0168 for (x = 1; x < wide; x++) 0169 { 0170 for (c = 0; c < (unsigned)nc; c += 2) 0171 { 0172 mult[c] = mrow[c * wide + x - 1]; 0173 mult[c + 1] = (mrow[c * wide + x] - mult[c]) / head[4]; 0174 } 0175 cend = head[0] + x * head[4]; 0176 for (col = cend - head[4]; 0177 col < raw_width && col < cend && col < unsigned(head[0] + head[2] - head[4]); 0178 col++) 0179 { 0180 c = nc > 2 ? FC(row - top_margin, col - left_margin) : 0; 0181 if (!(c & 1)) 0182 { 0183 c = RAW(row, col) * mult[c]; 0184 RAW(row, col) = LIM(c, 0, 65535); 0185 } 0186 for (c = 0; c < (unsigned)nc; c += 2) 0187 mult[c] += mult[c + 1]; 0188 } 0189 } 0190 for (x = 0; x < wide; x++) 0191 for (c = 0; c < (unsigned)nc; c += 2) 0192 mrow[c * wide + x] += mrow[(c + 1) * wide + x]; 0193 } 0194 } 0195 free(mrow); 0196 } 0197 0198 int LibRaw::phase_one_correct() 0199 { 0200 unsigned entries, tag, data, save, col, row, type; 0201 int len, i, j, k, cip, sum; 0202 #if 0 0203 int val[4], dev[4], max; 0204 #endif 0205 int head[9], diff, mindiff = INT_MAX, off_412 = 0; 0206 /* static */ const signed char dir[12][2] = { 0207 {-1, -1}, {-1, 1}, {1, -1}, {1, 1}, {-2, 0}, {0, -2}, 0208 {0, 2}, {2, 0}, {-2, -2}, {-2, 2}, {2, -2}, {2, 2}}; 0209 float poly[8], num, cfrac, frac, mult[2], *yval[2] = {NULL, NULL}; 0210 ushort *xval[2]; 0211 int qmult_applied = 0, qlin_applied = 0; 0212 std::vector<unsigned> badCols; 0213 0214 if (!meta_length) 0215 return 0; 0216 fseek(ifp, meta_offset, SEEK_SET); 0217 order = get2(); 0218 fseek(ifp, 6, SEEK_CUR); 0219 fseek(ifp, meta_offset + get4(), SEEK_SET); 0220 entries = get4(); 0221 get4(); 0222 0223 try 0224 { 0225 while (entries--) 0226 { 0227 checkCancel(); 0228 tag = get4(); 0229 len = get4(); 0230 data = get4(); 0231 save = ftell(ifp); 0232 fseek(ifp, meta_offset + data, SEEK_SET); 0233 #if 1 0234 if (ifp->eof()) 0235 { 0236 // skip bad or unknown tag 0237 fseek(ifp, save, SEEK_SET); 0238 continue; 0239 } 0240 #endif 0241 if (tag == 0x0400) 0242 { /* Sensor defects */ 0243 while ((len -= 8) >= 0) 0244 { 0245 col = get2(); 0246 row = get2(); 0247 type = get2(); 0248 get2(); 0249 if (col >= raw_width) 0250 continue; 0251 if (type == 131 || type == 137) /* Bad column */ 0252 #if 0 0253 // Original code by Dave Coffin - it works better by 0254 // not employing special logic for G1 channel below. 0255 // Alternatively this column remap (including G1 channel 0256 // logic) should be called prior to black subtraction 0257 // unlike other corrections 0258 for (row = 0; row < raw_height; row++) 0259 { 0260 if (FC(row - top_margin, col - left_margin)==1) 0261 { 0262 for (sum = i = 0; i < 4; i++) 0263 sum += val[i] = p1raw(row + dir[i][0], col + dir[i][1]); 0264 for (max = i = 0; i < 4; i++) 0265 { 0266 dev[i] = abs((val[i] << 2) - sum); 0267 if (dev[max] < dev[i]) 0268 max = i; 0269 } 0270 RAW(row, col) = (sum - val[max]) / 3.0 + 0.5; 0271 } 0272 else 0273 { 0274 for (sum = 0, i = 8; i < 12; i++) 0275 sum += p1raw(row + dir[i][0], col + dir[i][1]); 0276 RAW(row, col) = 0277 0.5 + sum * 0.0732233 + 0278 (p1raw(row, col - 2) + p1raw(row, col + 2)) * 0.3535534; 0279 } 0280 } 0281 #else 0282 // accumulae bad columns to be sorted later 0283 badCols.push_back(col); 0284 #endif 0285 else if (type == 129) 0286 { /* Bad pixel */ 0287 if (row >= raw_height) 0288 continue; 0289 j = (FC(row - top_margin, col - left_margin) != 1) * 4; 0290 unsigned count = 0; 0291 for (sum = 0, i = j; i < j + 8; i++) 0292 sum += p1rawc(row + dir[i][0], col + dir[i][1], count); 0293 if (count) 0294 RAW(row, col) = (sum + (count >> 1)) / count; 0295 } 0296 } 0297 } 0298 else if (tag == 0x0419) 0299 { /* Polynomial curve - output calibraion */ 0300 for (get4(), i = 0; i < 8; i++) 0301 poly[i] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT); 0302 poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1; 0303 for (i = 0; i < 0x10000; i++) 0304 { 0305 num = (poly[5] * i + poly[3]) * i + poly[1]; 0306 curve[i] = LIM(num, 0, 65535); 0307 } 0308 goto apply; /* apply to right half */ 0309 } 0310 else if (tag == 0x041a) 0311 { /* Polynomial curve */ 0312 for (i = 0; i < 4; i++) 0313 poly[i] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT); 0314 for (i = 0; i < 0x10000; i++) 0315 { 0316 for (num = 0, j = 4; j--;) 0317 num = num * i + poly[j]; 0318 curve[i] = LIM(num + i, 0, 65535); 0319 } 0320 apply: /* apply to whole image */ 0321 for (row = 0; row < raw_height; row++) 0322 { 0323 checkCancel(); 0324 for (col = (tag & 1) * ph1.split_col; col < raw_width; col++) 0325 RAW(row, col) = curve[RAW(row, col)]; 0326 } 0327 } 0328 else if (tag == 0x0401) 0329 { /* All-color flat fields - luma calibration*/ 0330 phase_one_flat_field(1, 2); 0331 } 0332 else if (tag == 0x0416 || tag == 0x0410) 0333 { 0334 // 0x410 - luma calibration 0335 phase_one_flat_field(0, 2); 0336 } 0337 else if (tag == 0x040b) 0338 { /* Red+blue flat field - croma calibration */ 0339 phase_one_flat_field(0, 4); 0340 } 0341 else if (tag == 0x0412) 0342 { 0343 fseek(ifp, 36, SEEK_CUR); 0344 diff = abs(get2() - ph1.tag_21a); 0345 if (mindiff > diff) 0346 { 0347 mindiff = diff; 0348 off_412 = ftell(ifp) - 38; 0349 } 0350 } 0351 else if (tag == 0x041f && !qlin_applied) 0352 { /* Quadrant linearization */ 0353 ushort lc[2][2][16], ref[16]; 0354 int qr, qc; 0355 bool baddiv = false; 0356 for (qr = 0; qr < 2; qr++) 0357 for (qc = 0; qc < 2; qc++) 0358 { 0359 for (i = 0; i < 16; i++) 0360 lc[qr][qc][i] = get4(); 0361 if (lc[qr][qc][15] == 0) 0362 baddiv = true; 0363 } 0364 if(baddiv) 0365 continue; 0366 for (i = 0; i < 16; i++) 0367 { 0368 int v = 0; 0369 for (qr = 0; qr < 2; qr++) 0370 for (qc = 0; qc < 2; qc++) 0371 v += lc[qr][qc][i]; 0372 ref[i] = (v + 2) >> 2; 0373 } 0374 for (qr = 0; qr < 2; qr++) 0375 { 0376 for (qc = 0; qc < 2; qc++) 0377 { 0378 int cx[19], cf[19]; 0379 for (i = 0; i < 16; i++) 0380 { 0381 cx[1 + i] = lc[qr][qc][i]; 0382 cf[1 + i] = ref[i]; 0383 } 0384 cx[0] = cf[0] = 0; 0385 cx[17] = cf[17] = ((unsigned int)ref[15] * 65535) / lc[qr][qc][15]; 0386 cf[18] = cx[18] = 65535; 0387 cubic_spline(cx, cf, 19); 0388 0389 for (row = (qr ? ph1.split_row : 0); 0390 row < unsigned(qr ? raw_height : ph1.split_row); row++) 0391 { 0392 checkCancel(); 0393 for (col = (qc ? ph1.split_col : 0); 0394 col < unsigned(qc ? raw_width : ph1.split_col); col++) 0395 RAW(row, col) = curve[RAW(row, col)]; 0396 } 0397 } 0398 } 0399 qlin_applied = 1; 0400 } 0401 else if (tag == 0x041e && !qmult_applied) 0402 { /* Quadrant multipliers - output calibraion */ 0403 float qmult[2][2] = {{1, 1}, {1, 1}}; 0404 get4(); 0405 get4(); 0406 get4(); 0407 get4(); 0408 qmult[0][0] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT); 0409 get4(); 0410 get4(); 0411 get4(); 0412 get4(); 0413 get4(); 0414 qmult[0][1] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT); 0415 get4(); 0416 get4(); 0417 get4(); 0418 qmult[1][0] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT); 0419 get4(); 0420 get4(); 0421 get4(); 0422 qmult[1][1] = 1.0 + getreal(LIBRAW_EXIFTAG_TYPE_FLOAT); 0423 for (row = 0; row < raw_height; row++) 0424 { 0425 checkCancel(); 0426 for (col = 0; col < raw_width; col++) 0427 { 0428 i = qmult[row >= (unsigned)ph1.split_row][col >= (unsigned)ph1.split_col] * 0429 RAW(row, col); 0430 RAW(row, col) = LIM(i, 0, 65535); 0431 } 0432 } 0433 qmult_applied = 1; 0434 } 0435 else if (tag == 0x0431 && !qmult_applied) 0436 { /* Quadrant combined - four tile gain calibration */ 0437 ushort lc[2][2][7], ref[7]; 0438 int qr, qc; 0439 for (i = 0; i < 7; i++) 0440 ref[i] = get4(); 0441 for (qr = 0; qr < 2; qr++) 0442 for (qc = 0; qc < 2; qc++) 0443 for (i = 0; i < 7; i++) 0444 lc[qr][qc][i] = get4(); 0445 for (qr = 0; qr < 2; qr++) 0446 { 0447 for (qc = 0; qc < 2; qc++) 0448 { 0449 int cx[9], cf[9]; 0450 for (i = 0; i < 7; i++) 0451 { 0452 cx[1 + i] = ref[i]; 0453 cf[1 + i] = ((unsigned)ref[i] * lc[qr][qc][i]) / 10000; 0454 } 0455 cx[0] = cf[0] = 0; 0456 cx[8] = cf[8] = 65535; 0457 cubic_spline(cx, cf, 9); 0458 for (row = (qr ? ph1.split_row : 0); 0459 row < unsigned(qr ? raw_height : ph1.split_row); row++) 0460 { 0461 checkCancel(); 0462 for (col = (qc ? ph1.split_col : 0); 0463 col < unsigned(qc ? raw_width : ph1.split_col); col++) 0464 RAW(row, col) = curve[RAW(row, col)]; 0465 } 0466 } 0467 } 0468 qmult_applied = 1; 0469 qlin_applied = 1; 0470 } 0471 fseek(ifp, save, SEEK_SET); 0472 } 0473 if (!badCols.empty()) 0474 { 0475 qsort(badCols.data(), badCols.size(), sizeof(unsigned), unsigned_cmp); 0476 bool prevIsolated = true; 0477 for (i = 0; i < (int)badCols.size(); ++i) 0478 { 0479 bool nextIsolated = i == ((int)(badCols.size()-1)) || badCols[i+1]>badCols[i]+4; 0480 for (row = 0; row < raw_height; ++row) 0481 if (prevIsolated && nextIsolated) 0482 phase_one_fix_pixel_grad(row,badCols[i]); 0483 else 0484 phase_one_fix_col_pixel_avg(row,badCols[i]); 0485 prevIsolated = nextIsolated; 0486 } 0487 } 0488 if (off_412) 0489 { 0490 fseek(ifp, off_412, SEEK_SET); 0491 for (i = 0; i < 9; i++) 0492 head[i] = get4() & 0x7fff; 0493 yval[0] = (float *)calloc(head[1] * head[3] + head[2] * head[4], 6); 0494 yval[1] = (float *)(yval[0] + head[1] * head[3]); 0495 xval[0] = (ushort *)(yval[1] + head[2] * head[4]); 0496 xval[1] = (ushort *)(xval[0] + head[1] * head[3]); 0497 get2(); 0498 for (i = 0; i < 2; i++) 0499 for (j = 0; j < head[i + 1] * head[i + 3]; j++) 0500 yval[i][j] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT); 0501 for (i = 0; i < 2; i++) 0502 for (j = 0; j < head[i + 1] * head[i + 3]; j++) 0503 xval[i][j] = get2(); 0504 for (row = 0; row < raw_height; row++) 0505 { 0506 checkCancel(); 0507 for (col = 0; col < raw_width; col++) 0508 { 0509 cfrac = (float)col * head[3] / raw_width; 0510 cfrac -= cip = cfrac; 0511 num = RAW(row, col) * 0.5; 0512 for (i = cip; i < cip + 2; i++) 0513 { 0514 for (k = j = 0; j < head[1]; j++) 0515 if (num < xval[0][k = head[1] * i + j]) 0516 break; 0517 frac = (j == 0 || j == head[1]) 0518 ? 0 0519 : (xval[0][k] - num) / (xval[0][k] - xval[0][k - 1]); 0520 mult[i - cip] = yval[0][k - 1] * frac + yval[0][k] * (1 - frac); 0521 } 0522 i = ((mult[0] * (1 - cfrac) + mult[1] * cfrac) * row + num) * 2; 0523 RAW(row, col) = LIM(i, 0, 65535); 0524 } 0525 } 0526 free(yval[0]); 0527 } 0528 } 0529 catch (...) 0530 { 0531 if (yval[0]) 0532 free(yval[0]); 0533 return LIBRAW_CANCELLED_BY_CALLBACK; 0534 } 0535 return 0; 0536 } 0537 0538 void LibRaw::phase_one_load_raw() 0539 { 0540 int a, b, i; 0541 ushort akey, bkey, t_mask; 0542 0543 fseek(ifp, ph1.key_off, SEEK_SET); 0544 akey = get2(); 0545 bkey = get2(); 0546 t_mask = ph1.format == 1 ? 0x5555 : 0x1354; 0547 if (ph1.black_col || ph1.black_row) 0548 { 0549 imgdata.rawdata.ph1_cblack = 0550 (short(*)[2])calloc(raw_height * 2, sizeof(ushort)); 0551 imgdata.rawdata.ph1_rblack = 0552 (short(*)[2])calloc(raw_width * 2, sizeof(ushort)); 0553 if (ph1.black_col) 0554 { 0555 fseek(ifp, ph1.black_col, SEEK_SET); 0556 read_shorts((ushort *)imgdata.rawdata.ph1_cblack[0], raw_height * 2); 0557 } 0558 if (ph1.black_row) 0559 { 0560 fseek(ifp, ph1.black_row, SEEK_SET); 0561 read_shorts((ushort *)imgdata.rawdata.ph1_rblack[0], raw_width * 2); 0562 } 0563 } 0564 fseek(ifp, data_offset, SEEK_SET); 0565 read_shorts(raw_image, raw_width * raw_height); 0566 if (ph1.format) 0567 for (i = 0; i < raw_width * raw_height; i += 2) 0568 { 0569 a = raw_image[i + 0] ^ akey; 0570 b = raw_image[i + 1] ^ bkey; 0571 raw_image[i + 0] = (a & t_mask) | (b & ~t_mask); 0572 raw_image[i + 1] = (b & t_mask) | (a & ~t_mask); 0573 } 0574 } 0575 0576 unsigned LibRaw::ph1_bithuff(int nbits, ushort *huff) 0577 { 0578 #ifndef LIBRAW_NOTHREADS 0579 #define bitbuf tls->ph1_bits.bitbuf 0580 #define vbits tls->ph1_bits.vbits 0581 #else 0582 static UINT64 bitbuf = 0; 0583 static int vbits = 0; 0584 #endif 0585 unsigned c; 0586 0587 if (nbits == -1) 0588 return bitbuf = vbits = 0; 0589 if (nbits == 0) 0590 return 0; 0591 if (vbits < nbits) 0592 { 0593 bitbuf = bitbuf << 32 | get4(); 0594 vbits += 32; 0595 } 0596 c = bitbuf << (64 - vbits) >> (64 - nbits); 0597 if (huff) 0598 { 0599 vbits -= huff[c] >> 8; 0600 return (uchar)huff[c]; 0601 } 0602 vbits -= nbits; 0603 return c; 0604 #ifndef LIBRAW_NOTHREADS 0605 #undef bitbuf 0606 #undef vbits 0607 #endif 0608 } 0609 0610 void LibRaw::phase_one_load_raw_c() 0611 { 0612 static const int length[] = {8, 7, 6, 9, 11, 10, 5, 12, 14, 13}; 0613 int *offset, len[2], pred[2], row, col, i, j; 0614 ushort *pixel; 0615 short(*c_black)[2], (*r_black)[2]; 0616 if (ph1.format == 6) 0617 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0618 0619 pixel = (ushort *)calloc(raw_width * 3 + raw_height * 4, 2); 0620 offset = (int *)(pixel + raw_width); 0621 fseek(ifp, strip_offset, SEEK_SET); 0622 for (row = 0; row < raw_height; row++) 0623 offset[row] = get4(); 0624 c_black = (short(*)[2])(offset + raw_height); 0625 fseek(ifp, ph1.black_col, SEEK_SET); 0626 if (ph1.black_col) 0627 read_shorts((ushort *)c_black[0], raw_height * 2); 0628 r_black = c_black + raw_height; 0629 fseek(ifp, ph1.black_row, SEEK_SET); 0630 if (ph1.black_row) 0631 read_shorts((ushort *)r_black[0], raw_width * 2); 0632 0633 // Copy data to internal copy (ever if not read) 0634 if (ph1.black_col || ph1.black_row) 0635 { 0636 imgdata.rawdata.ph1_cblack = 0637 (short(*)[2])calloc(raw_height * 2, sizeof(ushort)); 0638 memmove(imgdata.rawdata.ph1_cblack, (ushort *)c_black[0], 0639 raw_height * 2 * sizeof(ushort)); 0640 imgdata.rawdata.ph1_rblack = 0641 (short(*)[2])calloc(raw_width * 2, sizeof(ushort)); 0642 memmove(imgdata.rawdata.ph1_rblack, (ushort *)r_black[0], 0643 raw_width * 2 * sizeof(ushort)); 0644 } 0645 0646 for (i = 0; i < 256; i++) 0647 curve[i] = i * i / 3.969 + 0.5; 0648 try 0649 { 0650 for (row = 0; row < raw_height; row++) 0651 { 0652 checkCancel(); 0653 fseek(ifp, data_offset + offset[row], SEEK_SET); 0654 ph1_bits(-1); 0655 pred[0] = pred[1] = 0; 0656 for (col = 0; col < raw_width; col++) 0657 { 0658 if (col >= (raw_width & -8)) 0659 len[0] = len[1] = 14; 0660 else if ((col & 7) == 0) 0661 for (i = 0; i < 2; i++) 0662 { 0663 for (j = 0; j < 5 && !ph1_bits(1); j++) 0664 ; 0665 if (j--) 0666 len[i] = length[j * 2 + ph1_bits(1)]; 0667 } 0668 if ((i = len[col & 1]) == 14) 0669 pixel[col] = pred[col & 1] = ph1_bits(16); 0670 else 0671 pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1)); 0672 if (pred[col & 1] >> 16) 0673 derror(); 0674 if (ph1.format == 5 && pixel[col] < 256) 0675 pixel[col] = curve[pixel[col]]; 0676 } 0677 if (ph1.format == 8) 0678 memmove(&RAW(row, 0), &pixel[0], raw_width * 2); 0679 else 0680 for (col = 0; col < raw_width; col++) 0681 RAW(row, col) = pixel[col] << 2; 0682 } 0683 } 0684 catch (...) 0685 { 0686 free(pixel); 0687 throw; 0688 } 0689 free(pixel); 0690 maximum = 0xfffc - ph1.t_black; 0691 } 0692 0693 void LibRaw::hasselblad_load_raw() 0694 { 0695 struct jhead jh; 0696 int shot, row, col, *back[5]={0,0,0,0,0}, 0697 len[2], diff[12], pred, sh, f, c; 0698 unsigned s; 0699 unsigned upix, urow, ucol; 0700 ushort *ip; 0701 0702 if (!ljpeg_start(&jh, 0)) 0703 return; 0704 order = 0x4949; 0705 ph1_bits(-1); 0706 try 0707 { 0708 back[4] = (int *)calloc(raw_width, 3 * sizeof **back); 0709 FORC3 back[c] = back[4] + c * raw_width; 0710 cblack[6] >>= sh = tiff_samples > 1; 0711 shot = LIM(shot_select, 1, tiff_samples) - 1; 0712 for (row = 0; row < raw_height; row++) 0713 { 0714 checkCancel(); 0715 FORC4 back[(c + 3) & 3] = back[c]; 0716 for (col = 0; col < raw_width; col += 2) 0717 { 0718 for (s = 0; s < tiff_samples * 2; s += 2) 0719 { 0720 FORC(2) len[c] = ph1_huff(jh.huff[0]); 0721 FORC(2) 0722 { 0723 diff[s + c] = ph1_bits(len[c]); 0724 if (len[c] > 0 && (diff[s + c] & (1 << (len[c] - 1))) == 0) 0725 diff[s + c] -= (1 << len[c]) - 1; 0726 if (diff[s + c] == 65535) 0727 diff[s + c] = -32768; 0728 } 0729 } 0730 for (s = col; s < unsigned(col + 2); s++) 0731 { 0732 pred = 0x8000 + load_flags; 0733 if (col) 0734 pred = back[2][s - 2]; 0735 if (col && row > 1) 0736 switch (jh.psv) 0737 { 0738 case 11: 0739 pred += back[0][s] / 2 - back[0][s - 2] / 2; 0740 break; 0741 } 0742 f = (row & 1) * 3 ^ ((col + s) & 1); 0743 FORC(int(tiff_samples)) 0744 { 0745 pred += diff[(s & 1) * tiff_samples + c]; 0746 upix = pred >> sh & 0xffff; 0747 if (raw_image && c == shot) 0748 RAW(row, s) = upix; 0749 if (image) 0750 { 0751 urow = row - top_margin + (c & 1); 0752 ucol = col - left_margin - ((c >> 1) & 1); 0753 ip = &image[urow * width + ucol][f]; 0754 if (urow < height && ucol < width) 0755 *ip = c < 4 ? upix : (*ip + upix) >> 1; 0756 } 0757 } 0758 back[2][s] = pred; 0759 } 0760 } 0761 } 0762 } 0763 catch (...) 0764 { 0765 if(back[4]) 0766 free(back[4]); 0767 ljpeg_end(&jh); 0768 throw; 0769 } 0770 if(back[4]) 0771 free(back[4]); 0772 ljpeg_end(&jh); 0773 if (image) 0774 mix_green = 1; 0775 } 0776 0777 void LibRaw::leaf_hdr_load_raw() 0778 { 0779 ushort *pixel = 0; 0780 unsigned tile = 0, r, c, row, col; 0781 0782 if (!filters || !raw_image) 0783 { 0784 if (!image) 0785 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0786 pixel = (ushort *)calloc(raw_width, sizeof *pixel); 0787 } 0788 try 0789 { 0790 FORC(tiff_samples) 0791 for (r = 0; r < raw_height; r++) 0792 { 0793 checkCancel(); 0794 if (r % tile_length == 0) 0795 { 0796 fseek(ifp, data_offset + 4 * tile++, SEEK_SET); 0797 fseek(ifp, get4(), SEEK_SET); 0798 } 0799 if (filters && c != shot_select) 0800 continue; 0801 if (filters && raw_image) 0802 pixel = raw_image + r * raw_width; 0803 read_shorts(pixel, raw_width); 0804 if (!filters && image && (row = r - top_margin) < height) 0805 for (col = 0; col < width && col + left_margin < raw_width; col++) 0806 image[row * width + col][c] = pixel[col + left_margin]; 0807 } 0808 } 0809 catch (...) 0810 { 0811 if (!filters) 0812 free(pixel); 0813 throw; 0814 } 0815 if (!filters) 0816 { 0817 maximum = 0xffff; 0818 raw_color = 1; 0819 free(pixel); 0820 } 0821 } 0822 0823 void LibRaw::unpacked_load_raw_FujiDBP() 0824 /* 0825 for Fuji DBP for GX680, aka DX-2000 0826 DBP_tile_width = 688; 0827 DBP_tile_height = 3856; 0828 DBP_n_tiles = 8; 0829 */ 0830 { 0831 int scan_line, tile_n; 0832 int nTiles; 0833 0834 nTiles = 8; 0835 tile_width = raw_width / nTiles; 0836 0837 ushort *tile; 0838 tile = (ushort *)calloc(raw_height, tile_width * 2); 0839 0840 for (tile_n = 0; tile_n < nTiles; tile_n++) 0841 { 0842 read_shorts(tile, tile_width * raw_height); 0843 for (scan_line = 0; scan_line < raw_height; scan_line++) 0844 { 0845 memcpy(&raw_image[scan_line * raw_width + tile_n * tile_width], 0846 &tile[scan_line * tile_width], tile_width * 2); 0847 } 0848 } 0849 free(tile); 0850 fseek(ifp, -2, SEEK_CUR); // avoid EOF error 0851 } 0852 0853 void LibRaw::sinar_4shot_load_raw() 0854 { 0855 ushort *pixel; 0856 unsigned shot, row, col, r, c; 0857 0858 if (raw_image) 0859 { 0860 shot = LIM(shot_select, 1, 4) - 1; 0861 fseek(ifp, data_offset + shot * 4, SEEK_SET); 0862 fseek(ifp, get4(), SEEK_SET); 0863 unpacked_load_raw(); 0864 return; 0865 } 0866 if (!image) 0867 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0868 pixel = (ushort *)calloc(raw_width, sizeof *pixel); 0869 try 0870 { 0871 for (shot = 0; shot < 4; shot++) 0872 { 0873 checkCancel(); 0874 fseek(ifp, data_offset + shot * 4, SEEK_SET); 0875 fseek(ifp, get4(), SEEK_SET); 0876 for (row = 0; row < raw_height; row++) 0877 { 0878 read_shorts(pixel, raw_width); 0879 if ((r = row - top_margin - (shot >> 1 & 1)) >= height) 0880 continue; 0881 for (col = 0; col < raw_width; col++) 0882 { 0883 if ((c = col - left_margin - (shot & 1)) >= width) 0884 continue; 0885 image[r * width + c][(row & 1) * 3 ^ (~col & 1)] = pixel[col]; 0886 } 0887 } 0888 } 0889 } 0890 catch (...) 0891 { 0892 free(pixel); 0893 throw; 0894 } 0895 free(pixel); 0896 mix_green = 1; 0897 } 0898 0899 void LibRaw::imacon_full_load_raw() 0900 { 0901 if (!image) 0902 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0903 int row, col; 0904 0905 unsigned short *buf = 0906 (unsigned short *)malloc(width * 3 * sizeof(unsigned short)); 0907 0908 for (row = 0; row < height; row++) 0909 { 0910 checkCancel(); 0911 read_shorts(buf, width * 3); 0912 unsigned short(*rowp)[4] = &image[row * width]; 0913 for (col = 0; col < width; col++) 0914 { 0915 rowp[col][0] = buf[col * 3]; 0916 rowp[col][1] = buf[col * 3 + 1]; 0917 rowp[col][2] = buf[col * 3 + 2]; 0918 rowp[col][3] = 0; 0919 } 0920 } 0921 free(buf); 0922 }