File indexing completed on 2025-01-05 03:56:51
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 #include "../../internal/libraw_cameraids.h" 0021 0022 unsigned LibRaw::getbithuff(int nbits, ushort *huff) 0023 { 0024 #ifdef LIBRAW_NOTHREADS 0025 static unsigned bitbuf = 0; 0026 static int vbits = 0, reset = 0; 0027 #else 0028 #define bitbuf tls->getbits.bitbuf 0029 #define vbits tls->getbits.vbits 0030 #define reset tls->getbits.reset 0031 #endif 0032 unsigned c; 0033 0034 if (nbits > 25) 0035 return 0; 0036 if (nbits < 0) 0037 return bitbuf = vbits = reset = 0; 0038 if (nbits == 0 || vbits < 0) 0039 return 0; 0040 while (!reset && vbits < nbits && (c = fgetc(ifp)) != (unsigned)EOF && 0041 !(reset = zero_after_ff && c == 0xff && fgetc(ifp))) 0042 { 0043 bitbuf = (bitbuf << 8) + (uchar)c; 0044 vbits += 8; 0045 } 0046 c = vbits == 0 ? 0 : bitbuf << (32 - vbits) >> (32 - nbits); 0047 if (huff) 0048 { 0049 vbits -= huff[c] >> 8; 0050 c = (uchar)huff[c]; 0051 } 0052 else 0053 vbits -= nbits; 0054 if (vbits < 0) 0055 derror(); 0056 return c; 0057 #ifndef LIBRAW_NOTHREADS 0058 #undef bitbuf 0059 #undef vbits 0060 #undef reset 0061 #endif 0062 } 0063 0064 /* 0065 Construct a decode tree according the specification in *source. 0066 The first 16 bytes specify how many codes should be 1-bit, 2-bit 0067 3-bit, etc. Bytes after that are the leaf values. 0068 0069 For example, if the source is 0070 0071 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, 0072 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff }, 0073 0074 then the code is 0075 0076 00 0x04 0077 010 0x03 0078 011 0x05 0079 100 0x06 0080 101 0x02 0081 1100 0x07 0082 1101 0x01 0083 11100 0x08 0084 11101 0x09 0085 11110 0x00 0086 111110 0x0a 0087 1111110 0x0b 0088 1111111 0xff 0089 */ 0090 ushort *LibRaw::make_decoder_ref(const uchar **source) 0091 { 0092 int max, len, h, i, j; 0093 const uchar *count; 0094 ushort *huff; 0095 0096 count = (*source += 16) - 17; 0097 for (max = 16; max && !count[max]; max--) 0098 ; 0099 huff = (ushort *)calloc(1 + (1 << max), sizeof *huff); 0100 huff[0] = max; 0101 for (h = len = 1; len <= max; len++) 0102 for (i = 0; i < count[len]; i++, ++*source) 0103 for (j = 0; j < 1 << (max - len); j++) 0104 if (h <= 1 << max) 0105 huff[h++] = len << 8 | **source; 0106 return huff; 0107 } 0108 0109 ushort *LibRaw::make_decoder(const uchar *source) 0110 { 0111 return make_decoder_ref(&source); 0112 } 0113 0114 void LibRaw::crw_init_tables(unsigned table, ushort *huff[2]) 0115 { 0116 static const uchar first_tree[3][29] = { 0117 {0, 1, 4, 2, 3, 1, 2, 0, 0, 0, 0118 0, 0, 0, 0, 0, 0, 0x04, 0x03, 0x05, 0x06, 0119 0x02, 0x07, 0x01, 0x08, 0x09, 0x00, 0x0a, 0x0b, 0xff}, 0120 {0, 2, 2, 3, 1, 1, 1, 1, 2, 0, 0121 0, 0, 0, 0, 0, 0, 0x03, 0x02, 0x04, 0x01, 0122 0x05, 0x00, 0x06, 0x07, 0x09, 0x08, 0x0a, 0x0b, 0xff}, 0123 {0, 0, 6, 3, 1, 1, 2, 0, 0, 0, 0124 0, 0, 0, 0, 0, 0, 0x06, 0x05, 0x07, 0x04, 0125 0x08, 0x03, 0x09, 0x02, 0x00, 0x0a, 0x01, 0x0b, 0xff}, 0126 }; 0127 static const uchar second_tree[3][180] = { 0128 {0, 2, 2, 2, 1, 4, 2, 1, 2, 5, 1, 1, 0129 0, 0, 0, 139, 0x03, 0x04, 0x02, 0x05, 0x01, 0x06, 0x07, 0x08, 0130 0x12, 0x13, 0x11, 0x14, 0x09, 0x15, 0x22, 0x00, 0x21, 0x16, 0x0a, 0xf0, 0131 0x23, 0x17, 0x24, 0x31, 0x32, 0x18, 0x19, 0x33, 0x25, 0x41, 0x34, 0x42, 0132 0x35, 0x51, 0x36, 0x37, 0x38, 0x29, 0x79, 0x26, 0x1a, 0x39, 0x56, 0x57, 0133 0x28, 0x27, 0x52, 0x55, 0x58, 0x43, 0x76, 0x59, 0x77, 0x54, 0x61, 0xf9, 0134 0x71, 0x78, 0x75, 0x96, 0x97, 0x49, 0xb7, 0x53, 0xd7, 0x74, 0xb6, 0x98, 0135 0x47, 0x48, 0x95, 0x69, 0x99, 0x91, 0xfa, 0xb8, 0x68, 0xb5, 0xb9, 0xd6, 0136 0xf7, 0xd8, 0x67, 0x46, 0x45, 0x94, 0x89, 0xf8, 0x81, 0xd5, 0xf6, 0xb4, 0137 0x88, 0xb1, 0x2a, 0x44, 0x72, 0xd9, 0x87, 0x66, 0xd4, 0xf5, 0x3a, 0xa7, 0138 0x73, 0xa9, 0xa8, 0x86, 0x62, 0xc7, 0x65, 0xc8, 0xc9, 0xa1, 0xf4, 0xd1, 0139 0xe9, 0x5a, 0x92, 0x85, 0xa6, 0xe7, 0x93, 0xe8, 0xc1, 0xc6, 0x7a, 0x64, 0140 0xe1, 0x4a, 0x6a, 0xe6, 0xb3, 0xf1, 0xd3, 0xa5, 0x8a, 0xb2, 0x9a, 0xba, 0141 0x84, 0xa4, 0x63, 0xe5, 0xc5, 0xf3, 0xd2, 0xc4, 0x82, 0xaa, 0xda, 0xe4, 0142 0xf2, 0xca, 0x83, 0xa3, 0xa2, 0xc3, 0xea, 0xc2, 0xe2, 0xe3, 0xff, 0xff}, 0143 {0, 2, 2, 1, 4, 1, 4, 1, 3, 3, 1, 0, 0144 0, 0, 0, 140, 0x02, 0x03, 0x01, 0x04, 0x05, 0x12, 0x11, 0x06, 0145 0x13, 0x07, 0x08, 0x14, 0x22, 0x09, 0x21, 0x00, 0x23, 0x15, 0x31, 0x32, 0146 0x0a, 0x16, 0xf0, 0x24, 0x33, 0x41, 0x42, 0x19, 0x17, 0x25, 0x18, 0x51, 0147 0x34, 0x43, 0x52, 0x29, 0x35, 0x61, 0x39, 0x71, 0x62, 0x36, 0x53, 0x26, 0148 0x38, 0x1a, 0x37, 0x81, 0x27, 0x91, 0x79, 0x55, 0x45, 0x28, 0x72, 0x59, 0149 0xa1, 0xb1, 0x44, 0x69, 0x54, 0x58, 0xd1, 0xfa, 0x57, 0xe1, 0xf1, 0xb9, 0150 0x49, 0x47, 0x63, 0x6a, 0xf9, 0x56, 0x46, 0xa8, 0x2a, 0x4a, 0x78, 0x99, 0151 0x3a, 0x75, 0x74, 0x86, 0x65, 0xc1, 0x76, 0xb6, 0x96, 0xd6, 0x89, 0x85, 0152 0xc9, 0xf5, 0x95, 0xb4, 0xc7, 0xf7, 0x8a, 0x97, 0xb8, 0x73, 0xb7, 0xd8, 0153 0xd9, 0x87, 0xa7, 0x7a, 0x48, 0x82, 0x84, 0xea, 0xf4, 0xa6, 0xc5, 0x5a, 0154 0x94, 0xa4, 0xc6, 0x92, 0xc3, 0x68, 0xb5, 0xc8, 0xe4, 0xe5, 0xe6, 0xe9, 0155 0xa2, 0xa3, 0xe3, 0xc2, 0x66, 0x67, 0x93, 0xaa, 0xd4, 0xd5, 0xe7, 0xf8, 0156 0x88, 0x9a, 0xd7, 0x77, 0xc4, 0x64, 0xe2, 0x98, 0xa5, 0xca, 0xda, 0xe8, 0157 0xf3, 0xf6, 0xa9, 0xb2, 0xb3, 0xf2, 0xd2, 0x83, 0xba, 0xd3, 0xff, 0xff}, 0158 {0, 0, 6, 2, 1, 3, 3, 2, 5, 1, 2, 2, 0159 8, 10, 0, 117, 0x04, 0x05, 0x03, 0x06, 0x02, 0x07, 0x01, 0x08, 0160 0x09, 0x12, 0x13, 0x14, 0x11, 0x15, 0x0a, 0x16, 0x17, 0xf0, 0x00, 0x22, 0161 0x21, 0x18, 0x23, 0x19, 0x24, 0x32, 0x31, 0x25, 0x33, 0x38, 0x37, 0x34, 0162 0x35, 0x36, 0x39, 0x79, 0x57, 0x58, 0x59, 0x28, 0x56, 0x78, 0x27, 0x41, 0163 0x29, 0x77, 0x26, 0x42, 0x76, 0x99, 0x1a, 0x55, 0x98, 0x97, 0xf9, 0x48, 0164 0x54, 0x96, 0x89, 0x47, 0xb7, 0x49, 0xfa, 0x75, 0x68, 0xb6, 0x67, 0x69, 0165 0xb9, 0xb8, 0xd8, 0x52, 0xd7, 0x88, 0xb5, 0x74, 0x51, 0x46, 0xd9, 0xf8, 0166 0x3a, 0xd6, 0x87, 0x45, 0x7a, 0x95, 0xd5, 0xf6, 0x86, 0xb4, 0xa9, 0x94, 0167 0x53, 0x2a, 0xa8, 0x43, 0xf5, 0xf7, 0xd4, 0x66, 0xa7, 0x5a, 0x44, 0x8a, 0168 0xc9, 0xe8, 0xc8, 0xe7, 0x9a, 0x6a, 0x73, 0x4a, 0x61, 0xc7, 0xf4, 0xc6, 0169 0x65, 0xe9, 0x72, 0xe6, 0x71, 0x91, 0x93, 0xa6, 0xda, 0x92, 0x85, 0x62, 0170 0xf3, 0xc5, 0xb2, 0xa4, 0x84, 0xba, 0x64, 0xa5, 0xb3, 0xd2, 0x81, 0xe5, 0171 0xd3, 0xaa, 0xc4, 0xca, 0xf2, 0xb1, 0xe4, 0xd1, 0x83, 0x63, 0xea, 0xc3, 0172 0xe2, 0x82, 0xf1, 0xa3, 0xc2, 0xa1, 0xc1, 0xe3, 0xa2, 0xe1, 0xff, 0xff}}; 0173 if (table > 2) 0174 table = 2; 0175 huff[0] = make_decoder(first_tree[table]); 0176 huff[1] = make_decoder(second_tree[table]); 0177 } 0178 0179 /* 0180 Return 0 if the image starts with compressed data, 0181 1 if it starts with uncompressed low-order bits. 0182 0183 In Canon compressed data, 0xff is always followed by 0x00. 0184 */ 0185 int LibRaw::canon_has_lowbits() 0186 { 0187 uchar test[0x4000]; 0188 int ret = 1, i; 0189 0190 fseek(ifp, 0, SEEK_SET); 0191 fread(test, 1, sizeof test, ifp); 0192 for (i = 540; i < int(sizeof test - 1); i++) 0193 if (test[i] == 0xff) 0194 { 0195 if (test[i + 1]) 0196 return 1; 0197 ret = 0; 0198 } 0199 return ret; 0200 } 0201 0202 void LibRaw::canon_load_raw() 0203 { 0204 ushort *pixel, *prow, *huff[2]; 0205 int nblocks, lowbits, i, c, row, r, val; 0206 INT64 save; 0207 int block, diffbuf[64], leaf, len, diff, carry = 0, pnum = 0, base[2]; 0208 0209 crw_init_tables(tiff_compress, huff); 0210 lowbits = canon_has_lowbits(); 0211 if (!lowbits) 0212 maximum = 0x3ff; 0213 fseek(ifp, 540 + lowbits * raw_height * raw_width / 4, SEEK_SET); 0214 zero_after_ff = 1; 0215 getbits(-1); 0216 try 0217 { 0218 for (row = 0; row < raw_height; row += 8) 0219 { 0220 checkCancel(); 0221 pixel = raw_image + row * raw_width; 0222 nblocks = MIN(8, raw_height - row) * raw_width >> 6; 0223 for (block = 0; block < nblocks; block++) 0224 { 0225 memset(diffbuf, 0, sizeof diffbuf); 0226 for (i = 0; i < 64; i++) 0227 { 0228 leaf = gethuff(huff[i > 0]); 0229 if (leaf == 0 && i) 0230 break; 0231 if (leaf == 0xff) 0232 continue; 0233 i += leaf >> 4; 0234 len = leaf & 15; 0235 if (len == 0) 0236 continue; 0237 diff = getbits(len); 0238 if ((diff & (1 << (len - 1))) == 0) 0239 diff -= (1 << len) - 1; 0240 if (i < 64) 0241 diffbuf[i] = diff; 0242 } 0243 diffbuf[0] += carry; 0244 carry = diffbuf[0]; 0245 for (i = 0; i < 64; i++) 0246 { 0247 if (pnum++ % raw_width == 0) 0248 base[0] = base[1] = 512; 0249 if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10) 0250 derror(); 0251 } 0252 } 0253 if (lowbits) 0254 { 0255 save = ftell(ifp); 0256 fseek(ifp, 26 + row * raw_width / 4, SEEK_SET); 0257 for (prow = pixel, i = 0; i < raw_width * 2; i++) 0258 { 0259 c = fgetc(ifp); 0260 for (r = 0; r < 8; r += 2, prow++) 0261 { 0262 val = (*prow << 2) + ((c >> r) & 3); 0263 if (raw_width == 2672 && val < 512) 0264 val += 2; 0265 *prow = val; 0266 } 0267 } 0268 fseek(ifp, save, SEEK_SET); 0269 } 0270 } 0271 } 0272 catch (...) 0273 { 0274 FORC(2) free(huff[c]); 0275 throw; 0276 } 0277 FORC(2) free(huff[c]); 0278 } 0279 0280 int LibRaw::ljpeg_start(struct jhead *jh, int info_only) 0281 { 0282 ushort c, tag, len; 0283 int cnt = 0; 0284 std::vector<uchar> data_buffer(0x10000); 0285 uchar* data = &data_buffer[0]; 0286 const uchar *dp; 0287 0288 memset(jh, 0, sizeof *jh); 0289 jh->restart = INT_MAX; 0290 if (fread(data, 2, 1, ifp) != 1 || data[1] != 0xd8) 0291 return 0; 0292 do 0293 { 0294 if (feof(ifp)) 0295 return 0; 0296 if (cnt++ > 1024) 0297 return 0; // 1024 tags limit 0298 if (fread(data, 2, 2, ifp) != 2) 0299 return 0; 0300 tag = data[0] << 8 | data[1]; 0301 len = (data[2] << 8 | data[3]) - 2; 0302 if (tag <= 0xff00) 0303 return 0; 0304 if (fread(data, 1, len, ifp) != len) 0305 return 0; 0306 switch (tag) 0307 { 0308 case 0xffc3: // start of frame; lossless, Huffman 0309 jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3; 0310 case 0xffc1: 0311 case 0xffc0: 0312 jh->algo = tag & 0xff; 0313 jh->bits = data[0]; 0314 jh->high = data[1] << 8 | data[2]; 0315 jh->wide = data[3] << 8 | data[4]; 0316 jh->clrs = data[5] + jh->sraw; 0317 if (len == 9 && !dng_version) 0318 getc(ifp); 0319 break; 0320 case 0xffc4: // define Huffman tables 0321 if (info_only) 0322 break; 0323 for (dp = data; dp < data + len && !((c = *dp++) & -20);) 0324 jh->free[c] = jh->huff[c] = make_decoder_ref(&dp); 0325 break; 0326 case 0xffda: // start of scan 0327 jh->psv = data[1 + data[0] * 2]; 0328 jh->bits -= data[3 + data[0] * 2] & 15; 0329 break; 0330 case 0xffdb: 0331 FORC(64) jh->quant[c] = data[c * 2 + 1] << 8 | data[c * 2 + 2]; 0332 break; 0333 case 0xffdd: 0334 jh->restart = data[0] << 8 | data[1]; 0335 } 0336 } while (tag != 0xffda); 0337 if (jh->bits > 16 || jh->clrs > 6 || !jh->bits || !jh->high || !jh->wide || 0338 !jh->clrs) 0339 return 0; 0340 if (info_only) 0341 return 1; 0342 if (!jh->huff[0]) 0343 return 0; 0344 FORC(19) if (!jh->huff[c + 1]) jh->huff[c + 1] = jh->huff[c]; 0345 if (jh->sraw) 0346 { 0347 FORC(4) jh->huff[2 + c] = jh->huff[1]; 0348 FORC(jh->sraw) jh->huff[1 + c] = jh->huff[0]; 0349 } 0350 jh->row = (ushort *)calloc(jh->wide * jh->clrs, 16); 0351 return zero_after_ff = 1; 0352 } 0353 0354 void LibRaw::ljpeg_end(struct jhead *jh) 0355 { 0356 int c; 0357 FORC4 if (jh->free[c]) free(jh->free[c]); 0358 free(jh->row); 0359 } 0360 0361 int LibRaw::ljpeg_diff(ushort *huff) 0362 { 0363 int len, diff; 0364 if (!huff) 0365 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0366 0367 len = gethuff(huff); 0368 if (len == 16 && (!dng_version || dng_version >= 0x1010000)) 0369 return -32768; 0370 diff = getbits(len); 0371 0372 0373 if ((diff & (1 << (len - 1))) == 0) 0374 diff -= (1 << len) - 1; 0375 return diff; 0376 } 0377 0378 ushort *LibRaw::ljpeg_row(int jrow, struct jhead *jh) 0379 { 0380 int col, c, diff, pred, spred = 0; 0381 ushort mark = 0, *row[3]; 0382 0383 // Use the optimized, unrolled version if possible. 0384 if (!jh->sraw) 0385 return ljpeg_row_unrolled(jrow, jh); 0386 0387 if (jh->restart != 0 && jrow * jh->wide % jh->restart == 0) 0388 { 0389 FORC(6) jh->vpred[c] = 1 << (jh->bits - 1); 0390 if (jrow) 0391 { 0392 fseek(ifp, -2, SEEK_CUR); 0393 do 0394 mark = (mark << 8) + (c = fgetc(ifp)); 0395 while (c != EOF && mark >> 4 != 0xffd); 0396 } 0397 getbits(-1); 0398 } 0399 FORC3 row[c] = jh->row + jh->wide * jh->clrs * ((jrow + c) & 1); 0400 for (col = 0; col < jh->wide; col++) 0401 FORC(jh->clrs) 0402 { 0403 diff = ljpeg_diff(jh->huff[c]); 0404 if (jh->sraw && c <= jh->sraw && (col | c)) 0405 pred = spred; 0406 else if (col) 0407 pred = row[0][-jh->clrs]; 0408 else 0409 pred = (jh->vpred[c] += diff) - diff; 0410 if (jrow && col) 0411 switch (jh->psv) 0412 { 0413 case 1: 0414 break; 0415 case 2: 0416 pred = row[1][0]; 0417 break; 0418 case 3: 0419 pred = row[1][-jh->clrs]; 0420 break; 0421 case 4: 0422 pred = pred + row[1][0] - row[1][-jh->clrs]; 0423 break; 0424 case 5: 0425 pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); 0426 break; 0427 case 6: 0428 pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); 0429 break; 0430 case 7: 0431 pred = (pred + row[1][0]) >> 1; 0432 break; 0433 default: 0434 pred = 0; 0435 } 0436 if ((**row = pred + diff) >> jh->bits) 0437 if(!(load_flags & 512)) 0438 derror(); 0439 if (c <= jh->sraw) 0440 spred = **row; 0441 row[0]++; 0442 row[1]++; 0443 } 0444 return row[2]; 0445 } 0446 0447 ushort *LibRaw::ljpeg_row_unrolled(int jrow, struct jhead *jh) 0448 { 0449 int col, c, diff, pred; 0450 ushort mark = 0, *row[3]; 0451 0452 if (jh->restart != 0 && jrow * jh->wide % jh->restart == 0) 0453 { 0454 FORC(6) jh->vpred[c] = 1 << (jh->bits - 1); 0455 if (jrow) 0456 { 0457 fseek(ifp, -2, SEEK_CUR); 0458 do 0459 mark = (mark << 8) + (c = fgetc(ifp)); 0460 while (c != EOF && mark >> 4 != 0xffd); 0461 } 0462 getbits(-1); 0463 } 0464 FORC3 row[c] = jh->row + jh->wide * jh->clrs * ((jrow + c) & 1); 0465 0466 // The first column uses one particular predictor. 0467 FORC(jh->clrs) 0468 { 0469 diff = ljpeg_diff(jh->huff[c]); 0470 pred = (jh->vpred[c] += diff) - diff; 0471 if ((**row = pred + diff) >> jh->bits) 0472 derror(); 0473 row[0]++; 0474 row[1]++; 0475 } 0476 0477 if (!jrow) 0478 { 0479 for (col = 1; col < jh->wide; col++) 0480 FORC(jh->clrs) 0481 { 0482 diff = ljpeg_diff(jh->huff[c]); 0483 pred = row[0][-jh->clrs]; 0484 if ((**row = pred + diff) >> jh->bits) 0485 derror(); 0486 row[0]++; 0487 row[1]++; 0488 } 0489 } 0490 else if (jh->psv == 1) 0491 { 0492 for (col = 1; col < jh->wide; col++) 0493 FORC(jh->clrs) 0494 { 0495 diff = ljpeg_diff(jh->huff[c]); 0496 pred = row[0][-jh->clrs]; 0497 if ((**row = pred + diff) >> jh->bits) 0498 derror(); 0499 row[0]++; 0500 } 0501 } 0502 else 0503 { 0504 for (col = 1; col < jh->wide; col++) 0505 FORC(jh->clrs) 0506 { 0507 diff = ljpeg_diff(jh->huff[c]); 0508 pred = row[0][-jh->clrs]; 0509 switch (jh->psv) 0510 { 0511 case 1: 0512 break; 0513 case 2: 0514 pred = row[1][0]; 0515 break; 0516 case 3: 0517 pred = row[1][-jh->clrs]; 0518 break; 0519 case 4: 0520 pred = pred + row[1][0] - row[1][-jh->clrs]; 0521 break; 0522 case 5: 0523 pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); 0524 break; 0525 case 6: 0526 pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); 0527 break; 0528 case 7: 0529 pred = (pred + row[1][0]) >> 1; 0530 break; 0531 default: 0532 pred = 0; 0533 } 0534 if ((**row = pred + diff) >> jh->bits) 0535 derror(); 0536 row[0]++; 0537 row[1]++; 0538 } 0539 } 0540 return row[2]; 0541 } 0542 0543 void LibRaw::lossless_jpeg_load_raw() 0544 { 0545 int jwide, jhigh, jrow, jcol, val, jidx, i, j, row = 0, col = 0; 0546 struct jhead jh; 0547 ushort *rp; 0548 0549 if (!ljpeg_start(&jh, 0)) 0550 return; 0551 0552 if (jh.wide < 1 || jh.high < 1 || jh.clrs < 1 || jh.bits < 1) 0553 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0554 0555 if(cr2_slice[0] && !cr2_slice[1]) 0556 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0557 0558 jwide = jh.wide * jh.clrs; 0559 jhigh = jh.high; 0560 if (jh.clrs == 4 && jwide >= raw_width * 2) 0561 jhigh *= 2; 0562 0563 try 0564 { 0565 for (jrow = 0; jrow < jh.high; jrow++) 0566 { 0567 checkCancel(); 0568 rp = ljpeg_row(jrow, &jh); 0569 if (load_flags & 1) 0570 row = jrow & 1 ? height - 1 - jrow / 2 : jrow / 2; 0571 for (jcol = 0; jcol < jwide; jcol++) 0572 { 0573 val = curve[*rp++]; 0574 if (cr2_slice[0]) 0575 { 0576 jidx = jrow * jwide + jcol; 0577 i = jidx / (cr2_slice[1] * raw_height); 0578 if ((j = i >= cr2_slice[0])) 0579 i = cr2_slice[0]; 0580 if(!cr2_slice[1+j]) 0581 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0582 0583 jidx -= i * (cr2_slice[1] * raw_height); 0584 row = jidx / cr2_slice[1 + j]; 0585 col = jidx % cr2_slice[1 + j] + i * cr2_slice[1]; 0586 } 0587 if (raw_width == 3984 && (col -= 2) < 0) 0588 col += (row--, raw_width); 0589 if (row > raw_height) 0590 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0591 if ((unsigned)row < raw_height) 0592 RAW(row, col) = val; 0593 if (++col >= raw_width) 0594 col = (row++, 0); 0595 } 0596 } 0597 } 0598 catch (...) 0599 { 0600 ljpeg_end(&jh); 0601 throw; 0602 } 0603 ljpeg_end(&jh); 0604 } 0605 0606 void LibRaw::canon_sraw_load_raw() 0607 { 0608 struct jhead jh; 0609 short *rp = 0, (*ip)[4]; 0610 int jwide, slice, scol, ecol, row, col, jrow = 0, jcol = 0, pix[3], c; 0611 int v[3] = {0, 0, 0}, ver, hue; 0612 int saved_w = width, saved_h = height; 0613 char *cp; 0614 0615 if(!image) 0616 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0617 0618 if (!ljpeg_start(&jh, 0) || jh.clrs < 4) 0619 return; 0620 jwide = (jh.wide >>= 1) * jh.clrs; 0621 0622 if (jwide < 32 || jwide > 65535) 0623 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0624 0625 if (load_flags & 256) 0626 { 0627 width = raw_width; 0628 height = raw_height; 0629 } 0630 0631 try 0632 { 0633 for (ecol = slice = 0; slice <= cr2_slice[0]; slice++) 0634 { 0635 scol = ecol; 0636 ecol += cr2_slice[1] * 2 / jh.clrs; 0637 if (!cr2_slice[0] || ecol > raw_width - 1) 0638 ecol = raw_width & -2; 0639 for (row = 0; row < height; row += (jh.clrs >> 1) - 1) 0640 { 0641 checkCancel(); 0642 ip = (short(*)[4])image + row * width; 0643 for (col = scol; col < ecol; col += 2, jcol += jh.clrs) 0644 { 0645 if ((jcol %= jwide) == 0) 0646 rp = (short *)ljpeg_row(jrow++, &jh); 0647 if (col >= width) 0648 continue; 0649 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE) 0650 { 0651 FORC(jh.clrs - 2) 0652 { 0653 ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c]; 0654 ip[col + (c >> 1) * width + (c & 1)][1] = 0655 ip[col + (c >> 1) * width + (c & 1)][2] = 8192; 0656 } 0657 ip[col][1] = rp[jcol + jh.clrs - 2] - 8192; 0658 ip[col][2] = rp[jcol + jh.clrs - 1] - 8192; 0659 } 0660 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_RGB) 0661 { 0662 FORC(jh.clrs - 2) 0663 ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c]; 0664 ip[col][1] = rp[jcol + jh.clrs - 2] - 8192; 0665 ip[col][2] = rp[jcol + jh.clrs - 1] - 8192; 0666 } 0667 else 0668 { 0669 FORC(jh.clrs - 2) 0670 ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c]; 0671 ip[col][1] = rp[jcol + jh.clrs - 2] - 16384; 0672 ip[col][2] = rp[jcol + jh.clrs - 1] - 16384; 0673 } 0674 } 0675 } 0676 } 0677 } 0678 catch (...) 0679 { 0680 ljpeg_end(&jh); 0681 throw; 0682 } 0683 0684 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE) 0685 { 0686 ljpeg_end(&jh); 0687 maximum = 0x3fff; 0688 height = saved_h; 0689 width = saved_w; 0690 return; 0691 } 0692 0693 try 0694 { 0695 for (cp = model2; *cp && !isdigit(*cp); cp++) 0696 ; 0697 sscanf(cp, "%d.%d.%d", v, v + 1, v + 2); 0698 ver = (v[0] * 1000 + v[1]) * 1000 + v[2]; 0699 hue = (jh.sraw + 1) << 2; 0700 if (unique_id >= 0x80000281ULL || 0701 (unique_id == 0x80000218ULL && ver > 1000006)) 0702 hue = jh.sraw << 1; 0703 ip = (short(*)[4])image; 0704 rp = ip[0]; 0705 for (row = 0; row < height; row++, ip += width) 0706 { 0707 checkCancel(); 0708 if (row & (jh.sraw >> 1)) 0709 { 0710 for (col = 0; col < width; col += 2) 0711 for (c = 1; c < 3; c++) 0712 if (row == height - 1) 0713 { 0714 ip[col][c] = ip[col - width][c]; 0715 } 0716 else 0717 { 0718 ip[col][c] = (ip[col - width][c] + ip[col + width][c] + 1) >> 1; 0719 } 0720 } 0721 for (col = 1; col < width; col += 2) 0722 for (c = 1; c < 3; c++) 0723 if (col == width - 1) 0724 ip[col][c] = ip[col - 1][c]; 0725 else 0726 ip[col][c] = (ip[col - 1][c] + ip[col + 1][c] + 1) >> 1; 0727 } 0728 if (!(imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_RGB)) 0729 for (; rp < ip[0]; rp += 4) 0730 { 0731 checkCancel(); 0732 if ((unique_id == CanonID_EOS_5D_Mark_II) || 0733 (unique_id == CanonID_EOS_7D) || 0734 (unique_id == CanonID_EOS_50D) || 0735 (unique_id == CanonID_EOS_1D_Mark_IV) || 0736 (unique_id == CanonID_EOS_60D)) 0737 { 0738 rp[1] = (rp[1] << 2) + hue; 0739 rp[2] = (rp[2] << 2) + hue; 0740 pix[0] = rp[0] + ((50 * rp[1] + 22929 * rp[2]) >> 14); 0741 pix[1] = rp[0] + ((-5640 * rp[1] - 11751 * rp[2]) >> 14); 0742 pix[2] = rp[0] + ((29040 * rp[1] - 101 * rp[2]) >> 14); 0743 } 0744 else 0745 { 0746 if (unique_id < CanonID_EOS_5D_Mark_II) 0747 rp[0] -= 512; 0748 pix[0] = rp[0] + rp[2]; 0749 pix[2] = rp[0] + rp[1]; 0750 pix[1] = rp[0] + ((-778 * rp[1] - (rp[2] << 11)) >> 12); 0751 } 0752 FORC3 rp[c] = CLIP15(pix[c] * sraw_mul[c] >> 10); 0753 } 0754 } 0755 catch (...) 0756 { 0757 ljpeg_end(&jh); 0758 throw; 0759 } 0760 height = saved_h; 0761 width = saved_w; 0762 ljpeg_end(&jh); 0763 maximum = 0x3fff; 0764 } 0765 0766 void LibRaw::ljpeg_idct(struct jhead *jh) 0767 { 0768 int c, i, j, len, skip, coef; 0769 float work[3][8][8]; 0770 static float cs[106] = {0}; 0771 static const uchar zigzag[80] = { 0772 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 0773 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 0774 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 0775 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, 0776 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63}; 0777 0778 if (!cs[0]) 0779 FORC(106) cs[c] = cos((c & 31) * M_PI / 16) / 2; 0780 memset(work, 0, sizeof work); 0781 work[0][0][0] = jh->vpred[0] += ljpeg_diff(jh->huff[0]) * jh->quant[0]; 0782 for (i = 1; i < 64; i++) 0783 { 0784 len = gethuff(jh->huff[16]); 0785 i += skip = len >> 4; 0786 if (!(len &= 15) && skip < 15) 0787 break; 0788 coef = getbits(len); 0789 if ((coef & (1 << (len - 1))) == 0) 0790 coef -= (1 << len) - 1; 0791 ((float *)work)[zigzag[i]] = coef * jh->quant[i]; 0792 } 0793 FORC(8) work[0][0][c] *= float(M_SQRT1_2); 0794 FORC(8) work[0][c][0] *= float(M_SQRT1_2); 0795 for (i = 0; i < 8; i++) 0796 for (j = 0; j < 8; j++) 0797 FORC(8) work[1][i][j] += work[0][i][c] * cs[(j * 2 + 1) * c]; 0798 for (i = 0; i < 8; i++) 0799 for (j = 0; j < 8; j++) 0800 FORC(8) work[2][i][j] += work[1][c][j] * cs[(i * 2 + 1) * c]; 0801 0802 FORC(64) jh->idct[c] = CLIP(((float *)work[2])[c] + 0.5); 0803 } 0804 0805 void LibRaw::pentax_load_raw() 0806 { 0807 ushort bit[2][15], huff[4097]; 0808 int dep, row, col, diff, c, i; 0809 ushort vpred[2][2] = {{0, 0}, {0, 0}}, hpred[2]; 0810 0811 fseek(ifp, meta_offset, SEEK_SET); 0812 dep = (get2() + 12) & 15; 0813 fseek(ifp, 12, SEEK_CUR); 0814 FORC(dep) bit[0][c] = get2(); 0815 FORC(dep) bit[1][c] = fgetc(ifp); 0816 FORC(dep) 0817 for (i = bit[0][c]; i <= ((bit[0][c] + (4096 >> bit[1][c]) - 1) & 4095);) 0818 huff[++i] = bit[1][c] << 8 | c; 0819 huff[0] = 12; 0820 fseek(ifp, data_offset, SEEK_SET); 0821 getbits(-1); 0822 for (row = 0; row < raw_height; row++) 0823 { 0824 checkCancel(); 0825 for (col = 0; col < raw_width; col++) 0826 { 0827 diff = ljpeg_diff(huff); 0828 if (col < 2) 0829 hpred[col] = vpred[row & 1][col] += diff; 0830 else 0831 hpred[col & 1] += diff; 0832 RAW(row, col) = hpred[col & 1]; 0833 if (hpred[col & 1] >> tiff_bps) 0834 derror(); 0835 } 0836 } 0837 } 0838 void LibRaw::nikon_read_curve() 0839 { 0840 ushort ver0, ver1, vpred[2][2], csize; 0841 int i, step, max; 0842 0843 fseek(ifp, meta_offset, SEEK_SET); 0844 ver0 = fgetc(ifp); 0845 ver1 = fgetc(ifp); 0846 if (ver0 == 0x49 || ver1 == 0x58) 0847 fseek(ifp, 2110, SEEK_CUR); 0848 read_shorts(vpred[0], 4); 0849 step = max = 1 << tiff_bps & 0x7fff; 0850 if ((csize = get2()) > 1) 0851 step = max / (csize - 1); 0852 if (ver0 == 0x44 && (ver1 == 0x20 || (ver1 == 0x40 && step > 3)) && step > 0) 0853 { 0854 if (ver1 == 0x40) 0855 { 0856 step /= 4; 0857 max /= 4; 0858 } 0859 for (i = 0; i < csize; i++) 0860 curve[i * step] = get2(); 0861 for (i = 0; i < max; i++) 0862 curve[i] = (curve[i - i % step] * (step - i % step) + 0863 curve[i - i % step + step] * (i % step)) / 0864 step; 0865 } 0866 else if (ver0 != 0x46 && csize <= 0x4001) 0867 read_shorts(curve, max = csize); 0868 } 0869 0870 void LibRaw::nikon_load_raw() 0871 { 0872 static const uchar nikon_tree[][32] = { 0873 {0, 1, 5, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, /* 12-bit lossy */ 0874 5, 4, 3, 6, 2, 7, 1, 0, 8, 9, 11, 10, 12}, 0875 {0, 1, 5, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0876 0, 0, /* 12-bit lossy after split */ 0877 0x39, 0x5a, 0x38, 0x27, 0x16, 5, 4, 3, 2, 1, 0, 11, 12, 12}, 0878 0879 {0, 1, 4, 2, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 12-bit lossless */ 0880 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, 11, 12}, 0881 {0, 1, 4, 3, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, /* 14-bit lossy */ 0882 5, 6, 4, 7, 8, 3, 9, 2, 1, 0, 10, 11, 12, 13, 14}, 0883 {0, 1, 5, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0884 0, /* 14-bit lossy after split */ 0885 8, 0x5c, 0x4b, 0x3a, 0x29, 7, 6, 5, 4, 3, 2, 1, 0, 13, 14}, 0886 {0, 1, 4, 2, 2, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, /* 14-bit lossless */ 0887 7, 6, 8, 5, 9, 4, 10, 3, 11, 12, 2, 0, 1, 13, 14}}; 0888 ushort *huff, ver0, ver1, vpred[2][2], hpred[2]; 0889 int i, min, max, tree = 0, split = 0, row, col, len, shl, diff; 0890 0891 fseek(ifp, meta_offset, SEEK_SET); 0892 ver0 = fgetc(ifp); 0893 ver1 = fgetc(ifp); 0894 if (ver0 == 0x49 || ver1 == 0x58) 0895 fseek(ifp, 2110, SEEK_CUR); 0896 if (ver0 == 0x46) 0897 tree = 2; 0898 if (tiff_bps == 14) 0899 tree += 3; 0900 read_shorts(vpred[0], 4); 0901 max = 1 << tiff_bps & 0x7fff; 0902 if (ver0 == 0x44 && (ver1 == 0x20 || ver1 == 0x40)) 0903 { 0904 if (ver1 == 0x40) 0905 max /= 4; 0906 fseek(ifp, meta_offset + 562, SEEK_SET); 0907 split = get2(); 0908 } 0909 0910 while (max > 2 && (curve[max - 2] == curve[max - 1])) 0911 max--; 0912 huff = make_decoder(nikon_tree[tree]); 0913 fseek(ifp, data_offset, SEEK_SET); 0914 getbits(-1); 0915 try 0916 { 0917 for (min = row = 0; row < height; row++) 0918 { 0919 checkCancel(); 0920 if (split && row == split) 0921 { 0922 free(huff); 0923 huff = make_decoder(nikon_tree[tree + 1]); 0924 max += (min = 16) << 1; 0925 } 0926 for (col = 0; col < raw_width; col++) 0927 { 0928 i = gethuff(huff); 0929 len = i & 15; 0930 shl = i >> 4; 0931 diff = ((getbits(len - shl) << 1) + 1) << shl >> 1; 0932 if (len > 0 && (diff & (1 << (len - 1))) == 0) 0933 diff -= (1 << len) - !shl; 0934 if (col < 2) 0935 hpred[col] = vpred[row & 1][col] += diff; 0936 else 0937 hpred[col & 1] += diff; 0938 if ((ushort)(hpred[col & 1] + min) >= max) 0939 derror(); 0940 RAW(row, col) = curve[LIM((short)hpred[col & 1], 0, 0x3fff)]; 0941 } 0942 } 0943 } 0944 catch (...) 0945 { 0946 free(huff); 0947 throw; 0948 } 0949 free(huff); 0950 } 0951 0952 void LibRaw::nikon_yuv_load_raw() 0953 { 0954 if (!image) 0955 throw LIBRAW_EXCEPTION_IO_CORRUPT; 0956 int row, col, yuv[4]={0,0,0,0}, rgb[3], b, c; 0957 UINT64 bitbuf = 0; 0958 float cmul[4]; 0959 FORC4 { cmul[c] = cam_mul[c] > 0.001f ? cam_mul[c] : 1.f; } 0960 for (row = 0; row < raw_height; row++) 0961 { 0962 checkCancel(); 0963 0964 for (col = 0; col < raw_width; col++) 0965 { 0966 if (!(b = col & 1)) 0967 { 0968 bitbuf = 0; 0969 FORC(6) bitbuf |= (UINT64)fgetc(ifp) << c * 8; 0970 FORC(4) yuv[c] = (bitbuf >> c * 12 & 0xfff) - (c >> 1 << 11); 0971 } 0972 rgb[0] = yuv[b] + 1.370705 * yuv[3]; 0973 rgb[1] = yuv[b] - 0.337633 * yuv[2] - 0.698001 * yuv[3]; 0974 rgb[2] = yuv[b] + 1.732446 * yuv[2]; 0975 FORC3 image[row * width + col][c] = 0976 curve[LIM(rgb[c], 0, 0xfff)] / cmul[c]; 0977 } 0978 } 0979 } 0980 0981 void LibRaw::rollei_load_raw() 0982 { 0983 uchar pixel[10]; 0984 unsigned iten = 0, isix, i, buffer = 0, todo[16]; 0985 if (raw_width > 32767 || raw_height > 32767) 0986 throw LIBRAW_EXCEPTION_IO_BADFILE; 0987 unsigned maxpixel = raw_width * (raw_height + 7); 0988 0989 isix = raw_width * raw_height * 5 / 8; 0990 while (fread(pixel, 1, 10, ifp) == 10) 0991 { 0992 checkCancel(); 0993 for (i = 0; i < 10; i += 2) 0994 { 0995 todo[i] = iten++; 0996 todo[i + 1] = pixel[i] << 8 | pixel[i + 1]; 0997 buffer = pixel[i] >> 2 | buffer << 6; 0998 } 0999 for (; i < 16; i += 2) 1000 { 1001 todo[i] = isix++; 1002 todo[i + 1] = buffer >> (14 - i) * 5; 1003 } 1004 for (i = 0; i < 16; i += 2) 1005 if (todo[i] < maxpixel) 1006 raw_image[todo[i]] = (todo[i + 1] & 0x3ff); 1007 else 1008 derror(); 1009 } 1010 maximum = 0x3ff; 1011 } 1012 1013 void LibRaw::nokia_load_raw() 1014 { 1015 uchar *dp; 1016 int rev, dwide, row, col, c; 1017 double sum[] = {0, 0}; 1018 1019 rev = 3 * (order == 0x4949); 1020 dwide = (raw_width * 5 + 1) / 4; 1021 #ifdef USE_6BY9RPI 1022 if (raw_stride) 1023 dwide = raw_stride; 1024 #endif 1025 std::vector<uchar> data(dwide * 2 + 4); 1026 for (row = 0; row < raw_height; row++) 1027 { 1028 checkCancel(); 1029 if (fread(data.data() + dwide, 1, dwide, ifp) < dwide) 1030 derror(); 1031 FORC(dwide) data[c] = data[dwide + (c ^ rev)]; 1032 for (dp = data.data(), col = 0; col < raw_width; dp += 5, col += 4) 1033 FORC4 RAW(row, col + c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3); 1034 } 1035 maximum = 0x3ff; 1036 #ifdef USE_6BY9RPI 1037 if (!strcmp(make, "OmniVision") || 1038 !strcmp(make, "Sony") || 1039 !strcmp(make, "RaspberryPi")) return; 1040 #else 1041 if (strncmp(make, "OmniVision", 10)) 1042 return; 1043 #endif 1044 row = raw_height / 2; 1045 FORC(width - 1) 1046 { 1047 sum[c & 1] += SQR(RAW(row, c) - RAW(row + 1, c + 1)); 1048 sum[~c & 1] += SQR(RAW(row + 1, c) - RAW(row, c + 1)); 1049 } 1050 if (sum[1] > sum[0]) 1051 filters = 0x4b4b4b4b; 1052 } 1053 1054 #ifdef LIBRAW_OLD_VIDEO_SUPPORT 1055 1056 void LibRaw::canon_rmf_load_raw() 1057 { 1058 int row, col, bits, orow, ocol, c; 1059 1060 int *words = (int *)malloc(sizeof(int) * (raw_width / 3 + 1)); 1061 for (row = 0; row < raw_height; row++) 1062 { 1063 checkCancel(); 1064 fread(words, sizeof(int), raw_width / 3, ifp); 1065 for (col = 0; col < raw_width - 2; col += 3) 1066 { 1067 bits = words[col / 3]; 1068 FORC3 1069 { 1070 orow = row; 1071 if ((ocol = col + c - 4) < 0) 1072 { 1073 ocol += raw_width; 1074 if ((orow -= 2) < 0) 1075 orow += raw_height; 1076 } 1077 RAW(orow, ocol) = curve[bits >> (10 * c + 2) & 0x3ff]; 1078 } 1079 } 1080 } 1081 free(words); 1082 maximum = curve[0x3ff]; 1083 } 1084 #endif 1085 1086 unsigned LibRaw::pana_data(int nb, unsigned *bytes) 1087 { 1088 #ifndef LIBRAW_NOTHREADS 1089 #define vpos tls->pana_data.vpos 1090 #define buf tls->pana_data.buf 1091 #else 1092 static uchar buf[0x4002]; 1093 static int vpos; 1094 #endif 1095 int byte; 1096 1097 if (!nb && !bytes) 1098 return vpos = 0; 1099 1100 if (!vpos) 1101 { 1102 fread(buf + load_flags, 1, 0x4000 - load_flags, ifp); 1103 fread(buf, 1, load_flags, ifp); 1104 } 1105 1106 if (pana_encoding == 5) 1107 { 1108 for (byte = 0; byte < 16; byte++) 1109 { 1110 bytes[byte] = buf[vpos++]; 1111 vpos &= 0x3FFF; 1112 } 1113 } 1114 else 1115 { 1116 vpos = (vpos - nb) & 0x1ffff; 1117 byte = vpos >> 3 ^ 0x3ff0; 1118 return (buf[byte] | buf[byte + 1] << 8) >> (vpos & 7) & ~((~0u) << nb); 1119 } 1120 return 0; 1121 #ifndef LIBRAW_NOTHREADS 1122 #undef vpos 1123 #undef buf 1124 #endif 1125 } 1126 1127 void LibRaw::panasonic_load_raw() 1128 { 1129 int row, col, i, j, sh = 0, pred[2], nonz[2]; 1130 unsigned bytes[16]; 1131 memset(bytes,0,sizeof(bytes)); // make gcc11 happy 1132 ushort *raw_block_data; 1133 1134 pana_data(0, 0); 1135 1136 int enc_blck_size = pana_bpp == 12 ? 10 : 9; 1137 if (pana_encoding == 5) 1138 { 1139 for (row = 0; row < raw_height; row++) 1140 { 1141 raw_block_data = raw_image + row * raw_width; 1142 checkCancel(); 1143 for (col = 0; col < raw_width; col += enc_blck_size) 1144 { 1145 pana_data(0, bytes); 1146 1147 if (pana_bpp == 12) 1148 { 1149 raw_block_data[col] = ((bytes[1] & 0xF) << 8) + bytes[0]; 1150 raw_block_data[col + 1] = 16 * bytes[2] + (bytes[1] >> 4); 1151 raw_block_data[col + 2] = ((bytes[4] & 0xF) << 8) + bytes[3]; 1152 raw_block_data[col + 3] = 16 * bytes[5] + (bytes[4] >> 4); 1153 raw_block_data[col + 4] = ((bytes[7] & 0xF) << 8) + bytes[6]; 1154 raw_block_data[col + 5] = 16 * bytes[8] + (bytes[7] >> 4); 1155 raw_block_data[col + 6] = ((bytes[10] & 0xF) << 8) + bytes[9]; 1156 raw_block_data[col + 7] = 16 * bytes[11] + (bytes[10] >> 4); 1157 raw_block_data[col + 8] = ((bytes[13] & 0xF) << 8) + bytes[12]; 1158 raw_block_data[col + 9] = 16 * bytes[14] + (bytes[13] >> 4); 1159 } 1160 else if (pana_bpp == 14) 1161 { 1162 raw_block_data[col] = bytes[0] + ((bytes[1] & 0x3F) << 8); 1163 raw_block_data[col + 1] = 1164 (bytes[1] >> 6) + 4 * (bytes[2]) + ((bytes[3] & 0xF) << 10); 1165 raw_block_data[col + 2] = 1166 (bytes[3] >> 4) + 16 * (bytes[4]) + ((bytes[5] & 3) << 12); 1167 raw_block_data[col + 3] = ((bytes[5] & 0xFC) >> 2) + (bytes[6] << 6); 1168 raw_block_data[col + 4] = bytes[7] + ((bytes[8] & 0x3F) << 8); 1169 raw_block_data[col + 5] = 1170 (bytes[8] >> 6) + 4 * bytes[9] + ((bytes[10] & 0xF) << 10); 1171 raw_block_data[col + 6] = 1172 (bytes[10] >> 4) + 16 * bytes[11] + ((bytes[12] & 3) << 12); 1173 raw_block_data[col + 7] = 1174 ((bytes[12] & 0xFC) >> 2) + (bytes[13] << 6); 1175 raw_block_data[col + 8] = bytes[14] + ((bytes[15] & 0x3F) << 8); 1176 } 1177 } 1178 } 1179 } 1180 else 1181 { 1182 if (load_flags >= 0x4000) 1183 throw LIBRAW_EXCEPTION_IO_CORRUPT; 1184 for (row = 0; row < raw_height; row++) 1185 { 1186 checkCancel(); 1187 for (col = 0; col < raw_width; col++) 1188 { 1189 if ((i = col % 14) == 0) 1190 pred[0] = pred[1] = nonz[0] = nonz[1] = 0; 1191 if (i % 3 == 2) 1192 sh = 4 >> (3 - pana_data(2, 0)); 1193 if (nonz[i & 1]) 1194 { 1195 if ((j = pana_data(8, 0))) 1196 { 1197 if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4) 1198 pred[i & 1] &= ~((~0u) << sh); 1199 pred[i & 1] += j << sh; 1200 } 1201 } 1202 else if ((nonz[i & 1] = pana_data(8, 0)) || i > 11) 1203 pred[i & 1] = nonz[i & 1] << 4 | pana_data(4, 0); 1204 if ((RAW(row, col) = pred[col & 1]) > 4098 && col < width && 1205 row < height) 1206 derror(); 1207 } 1208 } 1209 } 1210 } 1211 1212 void LibRaw::olympus_load_raw() 1213 { 1214 ushort huff[4096]; 1215 int row, col, nbits, sign, low, high, i, c, w, n, nw; 1216 int acarry[2][3], *carry, pred, diff; 1217 1218 huff[n = 0] = 0xc0c; 1219 for (i = 12; i--;) 1220 FORC(2048 >> i) huff[++n] = (i + 1) << 8 | i; 1221 fseek(ifp, 7, SEEK_CUR); 1222 getbits(-1); 1223 for (row = 0; row < height; row++) 1224 { 1225 checkCancel(); 1226 memset(acarry, 0, sizeof acarry); 1227 for (col = 0; col < raw_width; col++) 1228 { 1229 carry = acarry[col & 1]; 1230 i = 2 * (carry[2] < 3); 1231 for (nbits = 2 + i; (ushort)carry[0] >> (nbits + i); nbits++) 1232 ; 1233 low = (sign = getbits(3)) & 3; 1234 sign = sign << 29 >> 31; 1235 if ((high = getbithuff(12, huff)) == 12) 1236 high = getbits(16 - nbits) >> 1; 1237 carry[0] = (high << nbits) | getbits(nbits); 1238 diff = (carry[0] ^ sign) + carry[1]; 1239 carry[1] = (diff * 3 + carry[1]) >> 5; 1240 carry[2] = carry[0] > 16 ? 0 : carry[2] + 1; 1241 if (col >= width) 1242 continue; 1243 if (row < 2 && col < 2) 1244 pred = 0; 1245 else if (row < 2) 1246 pred = RAW(row, col - 2); 1247 else if (col < 2) 1248 pred = RAW(row - 2, col); 1249 else 1250 { 1251 w = RAW(row, col - 2); 1252 n = RAW(row - 2, col); 1253 nw = RAW(row - 2, col - 2); 1254 if ((w < nw && nw < n) || (n < nw && nw < w)) 1255 { 1256 if (ABS(w - nw) > 32 || ABS(n - nw) > 32) 1257 pred = w + n - nw; 1258 else 1259 pred = (w + n) >> 1; 1260 } 1261 else 1262 pred = ABS(w - nw) > ABS(n - nw) ? w : n; 1263 } 1264 if ((RAW(row, col) = pred + ((diff << 2) | low)) >> 12) 1265 derror(); 1266 } 1267 } 1268 } 1269 1270 void LibRaw::minolta_rd175_load_raw() 1271 { 1272 uchar pixel[768]; 1273 unsigned irow, box, row, col; 1274 1275 for (irow = 0; irow < 1481; irow++) 1276 { 1277 checkCancel(); 1278 if (fread(pixel, 1, 768, ifp) < 768) 1279 derror(); 1280 box = irow / 82; 1281 row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box - 12) * 2); 1282 switch (irow) 1283 { 1284 case 1477: 1285 case 1479: 1286 continue; 1287 case 1476: 1288 row = 984; 1289 break; 1290 case 1480: 1291 row = 985; 1292 break; 1293 case 1478: 1294 row = 985; 1295 box = 1; 1296 } 1297 if ((box < 12) && (box & 1)) 1298 { 1299 for (col = 0; col < 1533; col++, row ^= 1) 1300 if (col != 1) 1301 RAW(row, col) = (col + 1) & 2 1302 ? pixel[col / 2 - 1] + pixel[col / 2 + 1] 1303 : pixel[col / 2] << 1; 1304 RAW(row, 1) = pixel[1] << 1; 1305 RAW(row, 1533) = pixel[765] << 1; 1306 } 1307 else 1308 for (col = row & 1; col < 1534; col += 2) 1309 RAW(row, col) = pixel[col / 2] << 1; 1310 } 1311 maximum = 0xff << 1; 1312 } 1313 1314 void LibRaw::quicktake_100_load_raw() 1315 { 1316 std::vector<uchar> pixel_buffer(484 * 644, 0x80); 1317 uchar* pixel = &pixel_buffer[0]; 1318 static const short gstep[16] = {-89, -60, -44, -32, -22, -15, -8, -2, 1319 2, 8, 15, 22, 32, 44, 60, 89}; 1320 static const short rstep[6][4] = {{-3, -1, 1, 3}, {-5, -1, 1, 5}, 1321 {-8, -2, 2, 8}, {-13, -3, 3, 13}, 1322 {-19, -4, 4, 19}, {-28, -6, 6, 28}}; 1323 static const short t_curve[256] = { 1324 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 1325 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 1326 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 1327 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 1328 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 1329 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 88, 90, 1330 92, 94, 97, 99, 101, 103, 105, 107, 110, 112, 114, 116, 118, 120, 1331 123, 125, 127, 129, 131, 134, 136, 138, 140, 142, 144, 147, 149, 151, 1332 153, 155, 158, 160, 162, 164, 166, 168, 171, 173, 175, 177, 179, 181, 1333 184, 186, 188, 190, 192, 195, 197, 199, 201, 203, 205, 208, 210, 212, 1334 214, 216, 218, 221, 223, 226, 230, 235, 239, 244, 248, 252, 257, 261, 1335 265, 270, 274, 278, 283, 287, 291, 296, 300, 305, 309, 313, 318, 322, 1336 326, 331, 335, 339, 344, 348, 352, 357, 361, 365, 370, 374, 379, 383, 1337 387, 392, 396, 400, 405, 409, 413, 418, 422, 426, 431, 435, 440, 444, 1338 448, 453, 457, 461, 466, 470, 474, 479, 483, 487, 492, 496, 500, 508, 1339 519, 531, 542, 553, 564, 575, 587, 598, 609, 620, 631, 643, 654, 665, 1340 676, 687, 698, 710, 721, 732, 743, 754, 766, 777, 788, 799, 810, 822, 1341 833, 844, 855, 866, 878, 889, 900, 911, 922, 933, 945, 956, 967, 978, 1342 989, 1001, 1012, 1023}; 1343 int rb, row, col, sharp, val = 0; 1344 1345 if (width > 640 || height > 480) 1346 throw LIBRAW_EXCEPTION_IO_CORRUPT; 1347 1348 getbits(-1); 1349 for (row = 2; row < height + 2; row++) 1350 { 1351 checkCancel(); 1352 for (col = 2 + (row & 1); col < width + 2; col += 2) 1353 { 1354 val = ((pixel[(row - 1) * 644 + col - 1] + 2 * pixel[(row - 1) * 644 + col + 1] + pixel[row * 644 + col - 2]) >> 2) + gstep[getbits(4)]; 1355 pixel[row * 644 + col] = val = LIM(val, 0, 255); 1356 if (col < 4) 1357 pixel[row * 644 + col - 2] = pixel[(row + 1) * 644 + (~row & 1)] = val; 1358 if (row == 2) 1359 pixel[(row - 1) * 644 + col + 1] = pixel[(row - 1) * 644 + col + 3] = val; 1360 } 1361 pixel[row * 644 + col] = val; 1362 } 1363 for (rb = 0; rb < 2; rb++) 1364 for (row = 2 + rb; row < height + 2; row += 2) 1365 { 1366 checkCancel(); 1367 for (col = 3 - (row & 1); col < width + 2; col += 2) 1368 { 1369 if (row < 4 || col < 4) 1370 sharp = 2; 1371 else 1372 { 1373 val = ABS(pixel[(row - 2) * 644 + col] - pixel[row * 644 + col - 2]) + ABS(pixel[(row - 2) * 644 + col] - pixel[(row - 2) * 644 + col - 2]) + 1374 ABS(pixel[row * 644 + col - 2] - pixel[(row - 2) * 644 + col - 2]); 1375 sharp = val < 4 1376 ? 0 1377 : val < 8 1378 ? 1 1379 : val < 16 ? 2 : val < 32 ? 3 : val < 48 ? 4 : 5; 1380 } 1381 val = ((pixel[(row - 2) * 644 + col] + pixel[row * 644 + col - 2]) >> 1) + rstep[sharp][getbits(2)]; 1382 pixel[row * 644 + col] = val = LIM(val, 0, 255); 1383 if (row < 4) 1384 pixel[(row - 2) * 644 + col + 2] = val; 1385 if (col < 4) 1386 pixel[(row + 2) * 644 + col - 2] = val; 1387 } 1388 } 1389 for (row = 2; row < height + 2; row++) 1390 { 1391 checkCancel(); 1392 for (col = 3 - (row & 1); col < width + 2; col += 2) 1393 { 1394 val = ((pixel[row * 644 + col - 1] + (pixel[row * 644 + col] << 2) + pixel[row * 644 + col + 1]) >> 1) - 0x100; 1395 pixel[row * 644 + col] = LIM(val, 0, 255); 1396 } 1397 } 1398 for (row = 0; row < height; row++) 1399 { 1400 checkCancel(); 1401 for (col = 0; col < width; col++) 1402 RAW(row, col) = t_curve[pixel[(row + 2) * 644 + col + 2]]; 1403 } 1404 maximum = 0x3ff; 1405 } 1406 1407 void LibRaw::sony_load_raw() 1408 { 1409 uchar head[40]; 1410 ushort *pixel; 1411 unsigned i, key, row, col; 1412 1413 fseek(ifp, 200896, SEEK_SET); 1414 fseek(ifp, (unsigned)fgetc(ifp) * 4 - 1, SEEK_CUR); 1415 order = 0x4d4d; 1416 key = get4(); 1417 1418 fseek(ifp, 164600, SEEK_SET); 1419 fread(head, 1, 40, ifp); 1420 sony_decrypt((unsigned *)head, 10, 1, key); 1421 for (i = 26; i-- > 22;) 1422 key = key << 8 | head[i]; 1423 1424 fseek(ifp, data_offset, SEEK_SET); 1425 for (row = 0; row < raw_height; row++) 1426 { 1427 checkCancel(); 1428 pixel = raw_image + row * raw_width; 1429 if (fread(pixel, 2, raw_width, ifp) < raw_width) 1430 derror(); 1431 sony_decrypt((unsigned *)pixel, raw_width / 2, !row, key); 1432 for (col = 0; col < raw_width; col++) 1433 if ((pixel[col] = ntohs(pixel[col])) >> 14) 1434 derror(); 1435 } 1436 maximum = 0x3ff0; 1437 } 1438 1439 void LibRaw::sony_arw_load_raw() 1440 { 1441 std::vector<ushort> huff_buffer(32770); 1442 ushort* huff = &huff_buffer[0]; 1443 static const ushort tab[18] = {0xf11, 0xf10, 0xe0f, 0xd0e, 0xc0d, 0xb0c, 1444 0xa0b, 0x90a, 0x809, 0x708, 0x607, 0x506, 1445 0x405, 0x304, 0x303, 0x300, 0x202, 0x201}; 1446 int i, c, n, col, row, sum = 0; 1447 1448 huff[0] = 15; 1449 for (n = i = 0; i < 18; i++) 1450 FORC(32768 >> (tab[i] >> 8)) huff[++n] = tab[i]; 1451 getbits(-1); 1452 for (col = raw_width; col--;) 1453 { 1454 checkCancel(); 1455 for (row = 0; row < raw_height + 1; row += 2) 1456 { 1457 if (row == raw_height) 1458 row = 1; 1459 if ((sum += ljpeg_diff(huff)) >> 12) 1460 derror(); 1461 if (row < height) 1462 RAW(row, col) = sum; 1463 } 1464 } 1465 } 1466 1467 void LibRaw::sony_arw2_load_raw() 1468 { 1469 uchar *data, *dp; 1470 ushort pix[16]; 1471 int row, col, val, max, min, imax, imin, sh, bit, i; 1472 1473 data = (uchar *)malloc(raw_width + 1); 1474 try 1475 { 1476 for (row = 0; row < height; row++) 1477 { 1478 checkCancel(); 1479 fread(data, 1, raw_width, ifp); 1480 for (dp = data, col = 0; col < raw_width - 30; dp += 16) 1481 { 1482 max = 0x7ff & (val = sget4(dp)); 1483 min = 0x7ff & val >> 11; 1484 imax = 0x0f & val >> 22; 1485 imin = 0x0f & val >> 26; 1486 for (sh = 0; sh < 4 && 0x80 << sh <= max - min; sh++) 1487 ; 1488 /* flag checks if outside of loop */ 1489 if (!(imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_ALLFLAGS) // no flag set 1490 || (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTATOVALUE)) 1491 { 1492 for (bit = 30, i = 0; i < 16; i++) 1493 if (i == imax) 1494 pix[i] = max; 1495 else if (i == imin) 1496 pix[i] = min; 1497 else 1498 { 1499 pix[i] = 1500 ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min; 1501 if (pix[i] > 0x7ff) 1502 pix[i] = 0x7ff; 1503 bit += 7; 1504 } 1505 } 1506 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_BASEONLY) 1507 { 1508 for (bit = 30, i = 0; i < 16; i++) 1509 if (i == imax) 1510 pix[i] = max; 1511 else if (i == imin) 1512 pix[i] = min; 1513 else 1514 pix[i] = 0; 1515 } 1516 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTAONLY) 1517 { 1518 for (bit = 30, i = 0; i < 16; i++) 1519 if (i == imax) 1520 pix[i] = 0; 1521 else if (i == imin) 1522 pix[i] = 0; 1523 else 1524 { 1525 pix[i] = 1526 ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min; 1527 if (pix[i] > 0x7ff) 1528 pix[i] = 0x7ff; 1529 bit += 7; 1530 } 1531 } 1532 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTAZEROBASE) 1533 { 1534 for (bit = 30, i = 0; i < 16; i++) 1535 if (i == imax) 1536 pix[i] = 0; 1537 else if (i == imin) 1538 pix[i] = 0; 1539 else 1540 { 1541 pix[i] = ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh); 1542 if (pix[i] > 0x7ff) 1543 pix[i] = 0x7ff; 1544 bit += 7; 1545 } 1546 } 1547 1548 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTATOVALUE) 1549 { 1550 for (i = 0; i < 16; i++, col += 2) 1551 { 1552 unsigned slope = 1553 pix[i] < 1001 ? 2 1554 : curve[pix[i] << 1] - curve[(pix[i] << 1) - 2]; 1555 unsigned step = 1 << sh; 1556 RAW(row, col) = 1557 curve[pix[i] << 1] > 1558 black + imgdata.rawparams.sony_arw2_posterization_thr 1559 ? LIM(((slope * step * 1000) / 1560 (curve[pix[i] << 1] - black)), 1561 0, 10000) 1562 : 0; 1563 } 1564 } 1565 else 1566 for (i = 0; i < 16; i++, col += 2) 1567 RAW(row, col) = curve[pix[i] << 1]; 1568 col -= col & 1 ? 1 : 31; 1569 } 1570 } 1571 } 1572 catch (...) 1573 { 1574 free(data); 1575 throw; 1576 } 1577 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTATOVALUE) 1578 maximum = 10000; 1579 free(data); 1580 } 1581 1582 void LibRaw::samsung_load_raw() 1583 { 1584 int row, col, c, i, dir, op[4], len[4]; 1585 if (raw_width > 32768 || 1586 raw_height > 32768) // definitely too much for old samsung 1587 throw LIBRAW_EXCEPTION_IO_BADFILE; 1588 unsigned maxpixels = raw_width * (raw_height + 7); 1589 1590 order = 0x4949; 1591 for (row = 0; row < raw_height; row++) 1592 { 1593 checkCancel(); 1594 fseek(ifp, strip_offset + row * 4, SEEK_SET); 1595 fseek(ifp, data_offset + get4(), SEEK_SET); 1596 ph1_bits(-1); 1597 FORC4 len[c] = row < 2 ? 7 : 4; 1598 for (col = 0; col < raw_width; col += 16) 1599 { 1600 dir = ph1_bits(1); 1601 FORC4 op[c] = ph1_bits(2); 1602 FORC4 switch (op[c]) 1603 { 1604 case 3: 1605 len[c] = ph1_bits(4); 1606 break; 1607 case 2: 1608 len[c]--; 1609 break; 1610 case 1: 1611 len[c]++; 1612 } 1613 for (c = 0; c < 16; c += 2) 1614 { 1615 i = len[((c & 1) << 1) | (c >> 3)]; 1616 unsigned idest = RAWINDEX(row, col + c); 1617 unsigned isrc = (dir ? RAWINDEX(row + (~c | -2), col + c) 1618 : col ? RAWINDEX(row, col + (c | -2)) : 0); 1619 if (idest < maxpixels && 1620 isrc < 1621 maxpixels) // less than zero is handled by unsigned conversion 1622 RAW(row, col + c) = (i > 0 ? ((signed)ph1_bits(i) << (32 - i) >> (32 - i)) : 0) + 1623 (dir ? RAW(row + (~c | -2), col + c) : col ? RAW(row, col + (c | -2)) : 128); 1624 else 1625 derror(); 1626 if (c == 14) 1627 c = -1; 1628 } 1629 } 1630 } 1631 for (row = 0; row < raw_height - 1; row += 2) 1632 for (col = 0; col < raw_width - 1; col += 2) 1633 SWAP(RAW(row, col + 1), RAW(row + 1, col)); 1634 } 1635 1636 void LibRaw::samsung2_load_raw() 1637 { 1638 static const ushort tab[14] = {0x304, 0x307, 0x206, 0x205, 0x403, 1639 0x600, 0x709, 0x80a, 0x90b, 0xa0c, 1640 0xa0d, 0x501, 0x408, 0x402}; 1641 ushort huff[1026], vpred[2][2] = {{0, 0}, {0, 0}}, hpred[2]; 1642 int i, c, n, row, col, diff; 1643 1644 huff[0] = 10; 1645 for (n = i = 0; i < 14; i++) 1646 FORC(1024 >> (tab[i] >> 8)) huff[++n] = tab[i]; 1647 getbits(-1); 1648 for (row = 0; row < raw_height; row++) 1649 { 1650 checkCancel(); 1651 for (col = 0; col < raw_width; col++) 1652 { 1653 diff = ljpeg_diff(huff); 1654 if (col < 2) 1655 hpred[col] = vpred[row & 1][col] += diff; 1656 else 1657 hpred[col & 1] += diff; 1658 RAW(row, col) = hpred[col & 1]; 1659 if (hpred[col & 1] >> tiff_bps) 1660 derror(); 1661 } 1662 } 1663 } 1664 1665 void LibRaw::samsung3_load_raw() 1666 { 1667 int opt, init, mag, pmode, row, tab, col, pred, diff, i, c; 1668 ushort lent[3][2], len[4], *prow[2]; 1669 order = 0x4949; 1670 fseek(ifp, 9, SEEK_CUR); 1671 opt = fgetc(ifp); 1672 init = (get2(), get2()); 1673 for (row = 0; row < raw_height; row++) 1674 { 1675 checkCancel(); 1676 fseek(ifp, (data_offset - ftell(ifp)) & 15, SEEK_CUR); 1677 ph1_bits(-1); 1678 mag = 0; 1679 pmode = 7; 1680 FORC(6)((ushort *)lent)[c] = row < 2 ? 7 : 4; 1681 prow[row & 1] = &RAW(row - 1, 1 - ((row & 1) << 1)); // green 1682 prow[~row & 1] = &RAW(row - 2, 0); // red and blue 1683 for (tab = 0; tab + 15 < raw_width; tab += 16) 1684 { 1685 if (~opt & 4 && !(tab & 63)) 1686 { 1687 i = ph1_bits(2); 1688 mag = i < 3 ? mag - '2' + "204"[i] : ph1_bits(12); 1689 } 1690 if (opt & 2) 1691 pmode = 7 - 4 * ph1_bits(1); 1692 else if (!ph1_bits(1)) 1693 pmode = ph1_bits(3); 1694 if (opt & 1 || !ph1_bits(1)) 1695 { 1696 FORC4 len[c] = ph1_bits(2); 1697 FORC4 1698 { 1699 i = ((row & 1) << 1 | (c & 1)) % 3; 1700 if (i < 0) 1701 throw LIBRAW_EXCEPTION_IO_CORRUPT; 1702 len[c] = len[c] < 3 ? lent[i][0] - '1' + "120"[len[c]] : ph1_bits(4); 1703 lent[i][0] = lent[i][1]; 1704 lent[i][1] = len[c]; 1705 } 1706 } 1707 FORC(16) 1708 { 1709 col = tab + (((c & 7) << 1) ^ (c >> 3) ^ (row & 1)); 1710 if (col < 0) 1711 throw LIBRAW_EXCEPTION_IO_CORRUPT; 1712 if (pmode < 0) 1713 throw LIBRAW_EXCEPTION_IO_CORRUPT; 1714 if (pmode != 7 && row >= 2 && (col - '4' + "0224468"[pmode]) < 0) 1715 throw LIBRAW_EXCEPTION_IO_CORRUPT; 1716 pred = (pmode == 7 || row < 2) 1717 ? (tab ? RAW(row, tab - 2 + (col & 1)) : init) 1718 : (prow[col & 1][col - '4' + "0224468"[pmode]] + 1719 prow[col & 1][col - '4' + "0244668"[pmode]] + 1) >> 1720 1; 1721 diff = ph1_bits(i = len[c >> 2]); 1722 if (i > 0 && diff >> (i - 1)) 1723 diff -= 1 << i; 1724 diff = diff * (mag * 2 + 1) + mag; 1725 RAW(row, col) = pred + diff; 1726 } 1727 } 1728 } 1729 } 1730 1731 #ifdef LIBRAW_OLD_VIDEO_SUPPORT 1732 void LibRaw::redcine_load_raw() 1733 { 1734 #ifndef NO_JASPER 1735 int c, row, col; 1736 jas_stream_t *in; 1737 jas_image_t *jimg; 1738 jas_matrix_t *jmat; 1739 jas_seqent_t *data; 1740 ushort *img, *pix; 1741 1742 jas_init(); 1743 in = (jas_stream_t *)ifp->make_jas_stream(); 1744 if (!in) 1745 throw LIBRAW_EXCEPTION_DECODE_JPEG2000; 1746 jas_stream_seek(in, data_offset + 20, SEEK_SET); 1747 jimg = jas_image_decode(in, -1, 0); 1748 if (!jimg) 1749 { 1750 jas_stream_close(in); 1751 throw LIBRAW_EXCEPTION_DECODE_JPEG2000; 1752 } 1753 jmat = jas_matrix_create(height / 2, width / 2); 1754 img = (ushort *)calloc((height + 2), (width + 2) * 2); 1755 bool fastexitflag = false; 1756 try 1757 { 1758 FORC4 1759 { 1760 checkCancel(); 1761 jas_image_readcmpt(jimg, c, 0, 0, width / 2, height / 2, jmat); 1762 data = jas_matrix_getref(jmat, 0, 0); 1763 for (row = c >> 1; row < height; row += 2) 1764 for (col = c & 1; col < width; col += 2) 1765 img[(row + 1) * (width + 2) + col + 1] = 1766 data[(row / 2) * (width / 2) + col / 2]; 1767 } 1768 for (col = 1; col <= width; col++) 1769 { 1770 img[col] = img[2 * (width + 2) + col]; 1771 img[(height + 1) * (width + 2) + col] = 1772 img[(height - 1) * (width + 2) + col]; 1773 } 1774 for (row = 0; row < height + 2; row++) 1775 { 1776 img[row * (width + 2)] = img[row * (width + 2) + 2]; 1777 img[(row + 1) * (width + 2) - 1] = img[(row + 1) * (width + 2) - 3]; 1778 } 1779 for (row = 1; row <= height; row++) 1780 { 1781 checkCancel(); 1782 pix = img + row * (width + 2) + (col = 1 + (FC(row, 1) & 1)); 1783 for (; col <= width; col += 2, pix += 2) 1784 { 1785 c = (((pix[0] - 0x800) << 3) + pix[-(width + 2)] + pix[width + 2] + 1786 pix[-1] + pix[1]) >> 1787 2; 1788 pix[0] = LIM(c, 0, 4095); 1789 } 1790 } 1791 for (row = 0; row < height; row++) 1792 { 1793 checkCancel(); 1794 for (col = 0; col < width; col++) 1795 RAW(row, col) = curve[img[(row + 1) * (width + 2) + col + 1]]; 1796 } 1797 } 1798 catch (...) 1799 { 1800 fastexitflag = true; 1801 } 1802 free(img); 1803 jas_matrix_destroy(jmat); 1804 jas_image_destroy(jimg); 1805 jas_stream_close(in); 1806 if (fastexitflag) 1807 throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK; 1808 #endif 1809 } 1810 #endif