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 #include "../../internal/libraw_cameraids.h"
21
getbithuff(int nbits,ushort * huff)22 unsigned LibRaw::getbithuff(int nbits, ushort *huff)
23 {
24 #ifdef LIBRAW_NOTHREADS
25 static unsigned bitbuf = 0;
26 static int vbits = 0, reset = 0;
27 #else
28 #define bitbuf tls->getbits.bitbuf
29 #define vbits tls->getbits.vbits
30 #define reset tls->getbits.reset
31 #endif
32 unsigned c;
33
34 if (nbits > 25)
35 return 0;
36 if (nbits < 0)
37 return bitbuf = vbits = reset = 0;
38 if (nbits == 0 || vbits < 0)
39 return 0;
40 while (!reset && vbits < nbits && (c = fgetc(ifp)) != (unsigned)EOF &&
41 !(reset = zero_after_ff && c == 0xff && fgetc(ifp)))
42 {
43 bitbuf = (bitbuf << 8) + (uchar)c;
44 vbits += 8;
45 }
46 c = vbits == 0 ? 0 : bitbuf << (32 - vbits) >> (32 - nbits);
47 if (huff)
48 {
49 vbits -= huff[c] >> 8;
50 c = (uchar)huff[c];
51 }
52 else
53 vbits -= nbits;
54 if (vbits < 0)
55 derror();
56 return c;
57 #ifndef LIBRAW_NOTHREADS
58 #undef bitbuf
59 #undef vbits
60 #undef reset
61 #endif
62 }
63
64 /*
65 Construct a decode tree according the specification in *source.
66 The first 16 bytes specify how many codes should be 1-bit, 2-bit
67 3-bit, etc. Bytes after that are the leaf values.
68
69 For example, if the source is
70
71 { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
72 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
73
74 then the code is
75
76 00 0x04
77 010 0x03
78 011 0x05
79 100 0x06
80 101 0x02
81 1100 0x07
82 1101 0x01
83 11100 0x08
84 11101 0x09
85 11110 0x00
86 111110 0x0a
87 1111110 0x0b
88 1111111 0xff
89 */
make_decoder_ref(const uchar ** source)90 ushort *LibRaw::make_decoder_ref(const uchar **source)
91 {
92 int max, len, h, i, j;
93 const uchar *count;
94 ushort *huff;
95
96 count = (*source += 16) - 17;
97 for (max = 16; max && !count[max]; max--)
98 ;
99 huff = (ushort *)calloc(1 + (1 << max), sizeof *huff);
100 merror(huff, "make_decoder()");
101 huff[0] = max;
102 for (h = len = 1; len <= max; len++)
103 for (i = 0; i < count[len]; i++, ++*source)
104 for (j = 0; j < 1 << (max - len); j++)
105 if (h <= 1 << max)
106 huff[h++] = len << 8 | **source;
107 return huff;
108 }
109
make_decoder(const uchar * source)110 ushort *LibRaw::make_decoder(const uchar *source)
111 {
112 return make_decoder_ref(&source);
113 }
114
crw_init_tables(unsigned table,ushort * huff[2])115 void LibRaw::crw_init_tables(unsigned table, ushort *huff[2])
116 {
117 static const uchar first_tree[3][29] = {
118 {0, 1, 4, 2, 3, 1, 2, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0x04, 0x03, 0x05, 0x06,
120 0x02, 0x07, 0x01, 0x08, 0x09, 0x00, 0x0a, 0x0b, 0xff},
121 {0, 2, 2, 3, 1, 1, 1, 1, 2, 0,
122 0, 0, 0, 0, 0, 0, 0x03, 0x02, 0x04, 0x01,
123 0x05, 0x00, 0x06, 0x07, 0x09, 0x08, 0x0a, 0x0b, 0xff},
124 {0, 0, 6, 3, 1, 1, 2, 0, 0, 0,
125 0, 0, 0, 0, 0, 0, 0x06, 0x05, 0x07, 0x04,
126 0x08, 0x03, 0x09, 0x02, 0x00, 0x0a, 0x01, 0x0b, 0xff},
127 };
128 static const uchar second_tree[3][180] = {
129 {0, 2, 2, 2, 1, 4, 2, 1, 2, 5, 1, 1,
130 0, 0, 0, 139, 0x03, 0x04, 0x02, 0x05, 0x01, 0x06, 0x07, 0x08,
131 0x12, 0x13, 0x11, 0x14, 0x09, 0x15, 0x22, 0x00, 0x21, 0x16, 0x0a, 0xf0,
132 0x23, 0x17, 0x24, 0x31, 0x32, 0x18, 0x19, 0x33, 0x25, 0x41, 0x34, 0x42,
133 0x35, 0x51, 0x36, 0x37, 0x38, 0x29, 0x79, 0x26, 0x1a, 0x39, 0x56, 0x57,
134 0x28, 0x27, 0x52, 0x55, 0x58, 0x43, 0x76, 0x59, 0x77, 0x54, 0x61, 0xf9,
135 0x71, 0x78, 0x75, 0x96, 0x97, 0x49, 0xb7, 0x53, 0xd7, 0x74, 0xb6, 0x98,
136 0x47, 0x48, 0x95, 0x69, 0x99, 0x91, 0xfa, 0xb8, 0x68, 0xb5, 0xb9, 0xd6,
137 0xf7, 0xd8, 0x67, 0x46, 0x45, 0x94, 0x89, 0xf8, 0x81, 0xd5, 0xf6, 0xb4,
138 0x88, 0xb1, 0x2a, 0x44, 0x72, 0xd9, 0x87, 0x66, 0xd4, 0xf5, 0x3a, 0xa7,
139 0x73, 0xa9, 0xa8, 0x86, 0x62, 0xc7, 0x65, 0xc8, 0xc9, 0xa1, 0xf4, 0xd1,
140 0xe9, 0x5a, 0x92, 0x85, 0xa6, 0xe7, 0x93, 0xe8, 0xc1, 0xc6, 0x7a, 0x64,
141 0xe1, 0x4a, 0x6a, 0xe6, 0xb3, 0xf1, 0xd3, 0xa5, 0x8a, 0xb2, 0x9a, 0xba,
142 0x84, 0xa4, 0x63, 0xe5, 0xc5, 0xf3, 0xd2, 0xc4, 0x82, 0xaa, 0xda, 0xe4,
143 0xf2, 0xca, 0x83, 0xa3, 0xa2, 0xc3, 0xea, 0xc2, 0xe2, 0xe3, 0xff, 0xff},
144 {0, 2, 2, 1, 4, 1, 4, 1, 3, 3, 1, 0,
145 0, 0, 0, 140, 0x02, 0x03, 0x01, 0x04, 0x05, 0x12, 0x11, 0x06,
146 0x13, 0x07, 0x08, 0x14, 0x22, 0x09, 0x21, 0x00, 0x23, 0x15, 0x31, 0x32,
147 0x0a, 0x16, 0xf0, 0x24, 0x33, 0x41, 0x42, 0x19, 0x17, 0x25, 0x18, 0x51,
148 0x34, 0x43, 0x52, 0x29, 0x35, 0x61, 0x39, 0x71, 0x62, 0x36, 0x53, 0x26,
149 0x38, 0x1a, 0x37, 0x81, 0x27, 0x91, 0x79, 0x55, 0x45, 0x28, 0x72, 0x59,
150 0xa1, 0xb1, 0x44, 0x69, 0x54, 0x58, 0xd1, 0xfa, 0x57, 0xe1, 0xf1, 0xb9,
151 0x49, 0x47, 0x63, 0x6a, 0xf9, 0x56, 0x46, 0xa8, 0x2a, 0x4a, 0x78, 0x99,
152 0x3a, 0x75, 0x74, 0x86, 0x65, 0xc1, 0x76, 0xb6, 0x96, 0xd6, 0x89, 0x85,
153 0xc9, 0xf5, 0x95, 0xb4, 0xc7, 0xf7, 0x8a, 0x97, 0xb8, 0x73, 0xb7, 0xd8,
154 0xd9, 0x87, 0xa7, 0x7a, 0x48, 0x82, 0x84, 0xea, 0xf4, 0xa6, 0xc5, 0x5a,
155 0x94, 0xa4, 0xc6, 0x92, 0xc3, 0x68, 0xb5, 0xc8, 0xe4, 0xe5, 0xe6, 0xe9,
156 0xa2, 0xa3, 0xe3, 0xc2, 0x66, 0x67, 0x93, 0xaa, 0xd4, 0xd5, 0xe7, 0xf8,
157 0x88, 0x9a, 0xd7, 0x77, 0xc4, 0x64, 0xe2, 0x98, 0xa5, 0xca, 0xda, 0xe8,
158 0xf3, 0xf6, 0xa9, 0xb2, 0xb3, 0xf2, 0xd2, 0x83, 0xba, 0xd3, 0xff, 0xff},
159 {0, 0, 6, 2, 1, 3, 3, 2, 5, 1, 2, 2,
160 8, 10, 0, 117, 0x04, 0x05, 0x03, 0x06, 0x02, 0x07, 0x01, 0x08,
161 0x09, 0x12, 0x13, 0x14, 0x11, 0x15, 0x0a, 0x16, 0x17, 0xf0, 0x00, 0x22,
162 0x21, 0x18, 0x23, 0x19, 0x24, 0x32, 0x31, 0x25, 0x33, 0x38, 0x37, 0x34,
163 0x35, 0x36, 0x39, 0x79, 0x57, 0x58, 0x59, 0x28, 0x56, 0x78, 0x27, 0x41,
164 0x29, 0x77, 0x26, 0x42, 0x76, 0x99, 0x1a, 0x55, 0x98, 0x97, 0xf9, 0x48,
165 0x54, 0x96, 0x89, 0x47, 0xb7, 0x49, 0xfa, 0x75, 0x68, 0xb6, 0x67, 0x69,
166 0xb9, 0xb8, 0xd8, 0x52, 0xd7, 0x88, 0xb5, 0x74, 0x51, 0x46, 0xd9, 0xf8,
167 0x3a, 0xd6, 0x87, 0x45, 0x7a, 0x95, 0xd5, 0xf6, 0x86, 0xb4, 0xa9, 0x94,
168 0x53, 0x2a, 0xa8, 0x43, 0xf5, 0xf7, 0xd4, 0x66, 0xa7, 0x5a, 0x44, 0x8a,
169 0xc9, 0xe8, 0xc8, 0xe7, 0x9a, 0x6a, 0x73, 0x4a, 0x61, 0xc7, 0xf4, 0xc6,
170 0x65, 0xe9, 0x72, 0xe6, 0x71, 0x91, 0x93, 0xa6, 0xda, 0x92, 0x85, 0x62,
171 0xf3, 0xc5, 0xb2, 0xa4, 0x84, 0xba, 0x64, 0xa5, 0xb3, 0xd2, 0x81, 0xe5,
172 0xd3, 0xaa, 0xc4, 0xca, 0xf2, 0xb1, 0xe4, 0xd1, 0x83, 0x63, 0xea, 0xc3,
173 0xe2, 0x82, 0xf1, 0xa3, 0xc2, 0xa1, 0xc1, 0xe3, 0xa2, 0xe1, 0xff, 0xff}};
174 if (table > 2)
175 table = 2;
176 huff[0] = make_decoder(first_tree[table]);
177 huff[1] = make_decoder(second_tree[table]);
178 }
179
180 /*
181 Return 0 if the image starts with compressed data,
182 1 if it starts with uncompressed low-order bits.
183
184 In Canon compressed data, 0xff is always followed by 0x00.
185 */
canon_has_lowbits()186 int LibRaw::canon_has_lowbits()
187 {
188 uchar test[0x4000];
189 int ret = 1, i;
190
191 fseek(ifp, 0, SEEK_SET);
192 fread(test, 1, sizeof test, ifp);
193 for (i = 540; i < int(sizeof test - 1); i++)
194 if (test[i] == 0xff)
195 {
196 if (test[i + 1])
197 return 1;
198 ret = 0;
199 }
200 return ret;
201 }
202
canon_load_raw()203 void LibRaw::canon_load_raw()
204 {
205 ushort *pixel, *prow, *huff[2];
206 int nblocks, lowbits, i, c, row, r, val;
207 INT64 save;
208 int block, diffbuf[64], leaf, len, diff, carry = 0, pnum = 0, base[2];
209
210 crw_init_tables(tiff_compress, huff);
211 lowbits = canon_has_lowbits();
212 if (!lowbits)
213 maximum = 0x3ff;
214 fseek(ifp, 540 + lowbits * raw_height * raw_width / 4, SEEK_SET);
215 zero_after_ff = 1;
216 getbits(-1);
217 try
218 {
219 for (row = 0; row < raw_height; row += 8)
220 {
221 checkCancel();
222 pixel = raw_image + row * raw_width;
223 nblocks = MIN(8, raw_height - row) * raw_width >> 6;
224 for (block = 0; block < nblocks; block++)
225 {
226 memset(diffbuf, 0, sizeof diffbuf);
227 for (i = 0; i < 64; i++)
228 {
229 leaf = gethuff(huff[i > 0]);
230 if (leaf == 0 && i)
231 break;
232 if (leaf == 0xff)
233 continue;
234 i += leaf >> 4;
235 len = leaf & 15;
236 if (len == 0)
237 continue;
238 diff = getbits(len);
239 if ((diff & (1 << (len - 1))) == 0)
240 diff -= (1 << len) - 1;
241 if (i < 64)
242 diffbuf[i] = diff;
243 }
244 diffbuf[0] += carry;
245 carry = diffbuf[0];
246 for (i = 0; i < 64; i++)
247 {
248 if (pnum++ % raw_width == 0)
249 base[0] = base[1] = 512;
250 if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
251 derror();
252 }
253 }
254 if (lowbits)
255 {
256 save = ftell(ifp);
257 fseek(ifp, 26 + row * raw_width / 4, SEEK_SET);
258 for (prow = pixel, i = 0; i < raw_width * 2; i++)
259 {
260 c = fgetc(ifp);
261 for (r = 0; r < 8; r += 2, prow++)
262 {
263 val = (*prow << 2) + ((c >> r) & 3);
264 if (raw_width == 2672 && val < 512)
265 val += 2;
266 *prow = val;
267 }
268 }
269 fseek(ifp, save, SEEK_SET);
270 }
271 }
272 }
273 catch (...)
274 {
275 FORC(2) free(huff[c]);
276 throw;
277 }
278 FORC(2) free(huff[c]);
279 }
280
ljpeg_start(struct jhead * jh,int info_only)281 int LibRaw::ljpeg_start(struct jhead *jh, int info_only)
282 {
283 ushort c, tag, len;
284 int cnt = 0;
285 std::vector<uchar> data_buffer(0x10000);
286 uchar* data = &data_buffer[0];
287 const uchar *dp;
288
289 memset(jh, 0, sizeof *jh);
290 jh->restart = INT_MAX;
291 if (fread(data, 2, 1, ifp) != 1 || data[1] != 0xd8)
292 return 0;
293 do
294 {
295 if (feof(ifp))
296 return 0;
297 if (cnt++ > 1024)
298 return 0; // 1024 tags limit
299 if (fread(data, 2, 2, ifp) != 2)
300 return 0;
301 tag = data[0] << 8 | data[1];
302 len = (data[2] << 8 | data[3]) - 2;
303 if (tag <= 0xff00)
304 return 0;
305 if (fread(data, 1, len, ifp) != len)
306 return 0;
307 switch (tag)
308 {
309 case 0xffc3: // start of frame; lossless, Huffman
310 jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
311 case 0xffc1:
312 case 0xffc0:
313 jh->algo = tag & 0xff;
314 jh->bits = data[0];
315 jh->high = data[1] << 8 | data[2];
316 jh->wide = data[3] << 8 | data[4];
317 jh->clrs = data[5] + jh->sraw;
318 if (len == 9 && !dng_version)
319 getc(ifp);
320 break;
321 case 0xffc4: // define Huffman tables
322 if (info_only)
323 break;
324 for (dp = data; dp < data + len && !((c = *dp++) & -20);)
325 jh->free[c] = jh->huff[c] = make_decoder_ref(&dp);
326 break;
327 case 0xffda: // start of scan
328 jh->psv = data[1 + data[0] * 2];
329 jh->bits -= data[3 + data[0] * 2] & 15;
330 break;
331 case 0xffdb:
332 FORC(64) jh->quant[c] = data[c * 2 + 1] << 8 | data[c * 2 + 2];
333 break;
334 case 0xffdd:
335 jh->restart = data[0] << 8 | data[1];
336 }
337 } while (tag != 0xffda);
338 if (jh->bits > 16 || jh->clrs > 6 || !jh->bits || !jh->high || !jh->wide ||
339 !jh->clrs)
340 return 0;
341 if (info_only)
342 return 1;
343 if (!jh->huff[0])
344 return 0;
345 FORC(19) if (!jh->huff[c + 1]) jh->huff[c + 1] = jh->huff[c];
346 if (jh->sraw)
347 {
348 FORC(4) jh->huff[2 + c] = jh->huff[1];
349 FORC(jh->sraw) jh->huff[1 + c] = jh->huff[0];
350 }
351 jh->row = (ushort *)calloc(jh->wide * jh->clrs, 16);
352 merror(jh->row, "ljpeg_start()");
353 return zero_after_ff = 1;
354 }
355
ljpeg_end(struct jhead * jh)356 void LibRaw::ljpeg_end(struct jhead *jh)
357 {
358 int c;
359 FORC4 if (jh->free[c]) free(jh->free[c]);
360 free(jh->row);
361 }
362
ljpeg_diff(ushort * huff)363 int LibRaw::ljpeg_diff(ushort *huff)
364 {
365 int len, diff;
366 if (!huff)
367 throw LIBRAW_EXCEPTION_IO_CORRUPT;
368
369 len = gethuff(huff);
370 if (len == 16 && (!dng_version || dng_version >= 0x1010000))
371 return -32768;
372 diff = getbits(len);
373
374
375 if ((diff & (1 << (len - 1))) == 0)
376 diff -= (1 << len) - 1;
377 return diff;
378 }
379
ljpeg_row(int jrow,struct jhead * jh)380 ushort *LibRaw::ljpeg_row(int jrow, struct jhead *jh)
381 {
382 int col, c, diff, pred, spred = 0;
383 ushort mark = 0, *row[3];
384
385 // Use the optimized, unrolled version if possible.
386 if (!jh->sraw)
387 return ljpeg_row_unrolled(jrow, jh);
388
389 if (jh->restart != 0 && jrow * jh->wide % jh->restart == 0)
390 {
391 FORC(6) jh->vpred[c] = 1 << (jh->bits - 1);
392 if (jrow)
393 {
394 fseek(ifp, -2, SEEK_CUR);
395 do
396 mark = (mark << 8) + (c = fgetc(ifp));
397 while (c != EOF && mark >> 4 != 0xffd);
398 }
399 getbits(-1);
400 }
401 FORC3 row[c] = jh->row + jh->wide * jh->clrs * ((jrow + c) & 1);
402 for (col = 0; col < jh->wide; col++)
403 FORC(jh->clrs)
404 {
405 diff = ljpeg_diff(jh->huff[c]);
406 if (jh->sraw && c <= jh->sraw && (col | c))
407 pred = spred;
408 else if (col)
409 pred = row[0][-jh->clrs];
410 else
411 pred = (jh->vpred[c] += diff) - diff;
412 if (jrow && col)
413 switch (jh->psv)
414 {
415 case 1:
416 break;
417 case 2:
418 pred = row[1][0];
419 break;
420 case 3:
421 pred = row[1][-jh->clrs];
422 break;
423 case 4:
424 pred = pred + row[1][0] - row[1][-jh->clrs];
425 break;
426 case 5:
427 pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1);
428 break;
429 case 6:
430 pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1);
431 break;
432 case 7:
433 pred = (pred + row[1][0]) >> 1;
434 break;
435 default:
436 pred = 0;
437 }
438 if ((**row = pred + diff) >> jh->bits)
439 if(!(load_flags & 512))
440 derror();
441 if (c <= jh->sraw)
442 spred = **row;
443 row[0]++;
444 row[1]++;
445 }
446 return row[2];
447 }
448
ljpeg_row_unrolled(int jrow,struct jhead * jh)449 ushort *LibRaw::ljpeg_row_unrolled(int jrow, struct jhead *jh)
450 {
451 int col, c, diff, pred;
452 ushort mark = 0, *row[3];
453
454 if (jh->restart != 0 && jrow * jh->wide % jh->restart == 0)
455 {
456 FORC(6) jh->vpred[c] = 1 << (jh->bits - 1);
457 if (jrow)
458 {
459 fseek(ifp, -2, SEEK_CUR);
460 do
461 mark = (mark << 8) + (c = fgetc(ifp));
462 while (c != EOF && mark >> 4 != 0xffd);
463 }
464 getbits(-1);
465 }
466 FORC3 row[c] = jh->row + jh->wide * jh->clrs * ((jrow + c) & 1);
467
468 // The first column uses one particular predictor.
469 FORC(jh->clrs)
470 {
471 diff = ljpeg_diff(jh->huff[c]);
472 pred = (jh->vpred[c] += diff) - diff;
473 if ((**row = pred + diff) >> jh->bits)
474 derror();
475 row[0]++;
476 row[1]++;
477 }
478
479 if (!jrow)
480 {
481 for (col = 1; col < jh->wide; col++)
482 FORC(jh->clrs)
483 {
484 diff = ljpeg_diff(jh->huff[c]);
485 pred = row[0][-jh->clrs];
486 if ((**row = pred + diff) >> jh->bits)
487 derror();
488 row[0]++;
489 row[1]++;
490 }
491 }
492 else if (jh->psv == 1)
493 {
494 for (col = 1; col < jh->wide; col++)
495 FORC(jh->clrs)
496 {
497 diff = ljpeg_diff(jh->huff[c]);
498 pred = row[0][-jh->clrs];
499 if ((**row = pred + diff) >> jh->bits)
500 derror();
501 row[0]++;
502 }
503 }
504 else
505 {
506 for (col = 1; col < jh->wide; col++)
507 FORC(jh->clrs)
508 {
509 diff = ljpeg_diff(jh->huff[c]);
510 pred = row[0][-jh->clrs];
511 switch (jh->psv)
512 {
513 case 1:
514 break;
515 case 2:
516 pred = row[1][0];
517 break;
518 case 3:
519 pred = row[1][-jh->clrs];
520 break;
521 case 4:
522 pred = pred + row[1][0] - row[1][-jh->clrs];
523 break;
524 case 5:
525 pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1);
526 break;
527 case 6:
528 pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1);
529 break;
530 case 7:
531 pred = (pred + row[1][0]) >> 1;
532 break;
533 default:
534 pred = 0;
535 }
536 if ((**row = pred + diff) >> jh->bits)
537 derror();
538 row[0]++;
539 row[1]++;
540 }
541 }
542 return row[2];
543 }
544
lossless_jpeg_load_raw()545 void LibRaw::lossless_jpeg_load_raw()
546 {
547 int jwide, jhigh, jrow, jcol, val, jidx, i, j, row = 0, col = 0;
548 struct jhead jh;
549 ushort *rp;
550
551 if (!ljpeg_start(&jh, 0))
552 return;
553
554 if (jh.wide < 1 || jh.high < 1 || jh.clrs < 1 || jh.bits < 1)
555 throw LIBRAW_EXCEPTION_IO_CORRUPT;
556 jwide = jh.wide * jh.clrs;
557 jhigh = jh.high;
558 if (jh.clrs == 4 && jwide >= raw_width * 2)
559 jhigh *= 2;
560
561 try
562 {
563 for (jrow = 0; jrow < jh.high; jrow++)
564 {
565 checkCancel();
566 rp = ljpeg_row(jrow, &jh);
567 if (load_flags & 1)
568 row = jrow & 1 ? height - 1 - jrow / 2 : jrow / 2;
569 for (jcol = 0; jcol < jwide; jcol++)
570 {
571 val = curve[*rp++];
572 if (cr2_slice[0])
573 {
574 jidx = jrow * jwide + jcol;
575 i = jidx / (cr2_slice[1] * raw_height);
576 if ((j = i >= cr2_slice[0]))
577 i = cr2_slice[0];
578 jidx -= i * (cr2_slice[1] * raw_height);
579 row = jidx / cr2_slice[1 + j];
580 col = jidx % cr2_slice[1 + j] + i * cr2_slice[1];
581 }
582 if (raw_width == 3984 && (col -= 2) < 0)
583 col += (row--, raw_width);
584 if (row > raw_height)
585 throw LIBRAW_EXCEPTION_IO_CORRUPT;
586 if ((unsigned)row < raw_height)
587 RAW(row, col) = val;
588 if (++col >= raw_width)
589 col = (row++, 0);
590 }
591 }
592 }
593 catch (...)
594 {
595 ljpeg_end(&jh);
596 throw;
597 }
598 ljpeg_end(&jh);
599 }
600
canon_sraw_load_raw()601 void LibRaw::canon_sraw_load_raw()
602 {
603 struct jhead jh;
604 short *rp = 0, (*ip)[4];
605 int jwide, slice, scol, ecol, row, col, jrow = 0, jcol = 0, pix[3], c;
606 int v[3] = {0, 0, 0}, ver, hue;
607 int saved_w = width, saved_h = height;
608 char *cp;
609
610 if (!ljpeg_start(&jh, 0) || jh.clrs < 4)
611 return;
612 jwide = (jh.wide >>= 1) * jh.clrs;
613
614 if (load_flags & 256)
615 {
616 width = raw_width;
617 height = raw_height;
618 }
619
620 try
621 {
622 for (ecol = slice = 0; slice <= cr2_slice[0]; slice++)
623 {
624 scol = ecol;
625 ecol += cr2_slice[1] * 2 / jh.clrs;
626 if (!cr2_slice[0] || ecol > raw_width - 1)
627 ecol = raw_width & -2;
628 for (row = 0; row < height; row += (jh.clrs >> 1) - 1)
629 {
630 checkCancel();
631 ip = (short(*)[4])image + row * width;
632 for (col = scol; col < ecol; col += 2, jcol += jh.clrs)
633 {
634 if ((jcol %= jwide) == 0)
635 rp = (short *)ljpeg_row(jrow++, &jh);
636 if (col >= width)
637 continue;
638 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE)
639 {
640 FORC(jh.clrs - 2)
641 {
642 ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c];
643 ip[col + (c >> 1) * width + (c & 1)][1] =
644 ip[col + (c >> 1) * width + (c & 1)][2] = 8192;
645 }
646 ip[col][1] = rp[jcol + jh.clrs - 2] - 8192;
647 ip[col][2] = rp[jcol + jh.clrs - 1] - 8192;
648 }
649 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_RGB)
650 {
651 FORC(jh.clrs - 2)
652 ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c];
653 ip[col][1] = rp[jcol + jh.clrs - 2] - 8192;
654 ip[col][2] = rp[jcol + jh.clrs - 1] - 8192;
655 }
656 else
657 {
658 FORC(jh.clrs - 2)
659 ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c];
660 ip[col][1] = rp[jcol + jh.clrs - 2] - 16384;
661 ip[col][2] = rp[jcol + jh.clrs - 1] - 16384;
662 }
663 }
664 }
665 }
666 }
667 catch (...)
668 {
669 ljpeg_end(&jh);
670 throw;
671 }
672
673 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE)
674 {
675 ljpeg_end(&jh);
676 maximum = 0x3fff;
677 height = saved_h;
678 width = saved_w;
679 return;
680 }
681
682 try
683 {
684 for (cp = model2; *cp && !isdigit(*cp); cp++)
685 ;
686 sscanf(cp, "%d.%d.%d", v, v + 1, v + 2);
687 ver = (v[0] * 1000 + v[1]) * 1000 + v[2];
688 hue = (jh.sraw + 1) << 2;
689 if (unique_id >= 0x80000281ULL ||
690 (unique_id == 0x80000218ULL && ver > 1000006))
691 hue = jh.sraw << 1;
692 ip = (short(*)[4])image;
693 rp = ip[0];
694 for (row = 0; row < height; row++, ip += width)
695 {
696 checkCancel();
697 if (row & (jh.sraw >> 1))
698 {
699 for (col = 0; col < width; col += 2)
700 for (c = 1; c < 3; c++)
701 if (row == height - 1)
702 {
703 ip[col][c] = ip[col - width][c];
704 }
705 else
706 {
707 ip[col][c] = (ip[col - width][c] + ip[col + width][c] + 1) >> 1;
708 }
709 }
710 for (col = 1; col < width; col += 2)
711 for (c = 1; c < 3; c++)
712 if (col == width - 1)
713 ip[col][c] = ip[col - 1][c];
714 else
715 ip[col][c] = (ip[col - 1][c] + ip[col + 1][c] + 1) >> 1;
716 }
717 if (!(imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_RGB))
718 for (; rp < ip[0]; rp += 4)
719 {
720 checkCancel();
721 if ((unique_id == CanonID_EOS_5D_Mark_II) ||
722 (unique_id == CanonID_EOS_7D) ||
723 (unique_id == CanonID_EOS_50D) ||
724 (unique_id == CanonID_EOS_1D_Mark_IV) ||
725 (unique_id == CanonID_EOS_60D))
726 {
727 rp[1] = (rp[1] << 2) + hue;
728 rp[2] = (rp[2] << 2) + hue;
729 pix[0] = rp[0] + ((50 * rp[1] + 22929 * rp[2]) >> 14);
730 pix[1] = rp[0] + ((-5640 * rp[1] - 11751 * rp[2]) >> 14);
731 pix[2] = rp[0] + ((29040 * rp[1] - 101 * rp[2]) >> 14);
732 }
733 else
734 {
735 if (unique_id < CanonID_EOS_5D_Mark_II)
736 rp[0] -= 512;
737 pix[0] = rp[0] + rp[2];
738 pix[2] = rp[0] + rp[1];
739 pix[1] = rp[0] + ((-778 * rp[1] - (rp[2] << 11)) >> 12);
740 }
741 FORC3 rp[c] = CLIP15(pix[c] * sraw_mul[c] >> 10);
742 }
743 }
744 catch (...)
745 {
746 ljpeg_end(&jh);
747 throw;
748 }
749 height = saved_h;
750 width = saved_w;
751 ljpeg_end(&jh);
752 maximum = 0x3fff;
753 }
754
ljpeg_idct(struct jhead * jh)755 void LibRaw::ljpeg_idct(struct jhead *jh)
756 {
757 int c, i, j, len, skip, coef;
758 float work[3][8][8];
759 static float cs[106] = {0};
760 static const uchar zigzag[80] = {
761 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
762 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
763 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
764 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
765 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63};
766
767 if (!cs[0])
768 FORC(106) cs[c] = cos((c & 31) * M_PI / 16) / 2;
769 memset(work, 0, sizeof work);
770 work[0][0][0] = jh->vpred[0] += ljpeg_diff(jh->huff[0]) * jh->quant[0];
771 for (i = 1; i < 64; i++)
772 {
773 len = gethuff(jh->huff[16]);
774 i += skip = len >> 4;
775 if (!(len &= 15) && skip < 15)
776 break;
777 coef = getbits(len);
778 if ((coef & (1 << (len - 1))) == 0)
779 coef -= (1 << len) - 1;
780 ((float *)work)[zigzag[i]] = coef * jh->quant[i];
781 }
782 FORC(8) work[0][0][c] *= M_SQRT1_2;
783 FORC(8) work[0][c][0] *= M_SQRT1_2;
784 for (i = 0; i < 8; i++)
785 for (j = 0; j < 8; j++)
786 FORC(8) work[1][i][j] += work[0][i][c] * cs[(j * 2 + 1) * c];
787 for (i = 0; i < 8; i++)
788 for (j = 0; j < 8; j++)
789 FORC(8) work[2][i][j] += work[1][c][j] * cs[(i * 2 + 1) * c];
790
791 FORC(64) jh->idct[c] = CLIP(((float *)work[2])[c] + 0.5);
792 }
793
pentax_load_raw()794 void LibRaw::pentax_load_raw()
795 {
796 ushort bit[2][15], huff[4097];
797 int dep, row, col, diff, c, i;
798 ushort vpred[2][2] = {{0, 0}, {0, 0}}, hpred[2];
799
800 fseek(ifp, meta_offset, SEEK_SET);
801 dep = (get2() + 12) & 15;
802 fseek(ifp, 12, SEEK_CUR);
803 FORC(dep) bit[0][c] = get2();
804 FORC(dep) bit[1][c] = fgetc(ifp);
805 FORC(dep)
806 for (i = bit[0][c]; i <= ((bit[0][c] + (4096 >> bit[1][c]) - 1) & 4095);)
807 huff[++i] = bit[1][c] << 8 | c;
808 huff[0] = 12;
809 fseek(ifp, data_offset, SEEK_SET);
810 getbits(-1);
811 for (row = 0; row < raw_height; row++)
812 {
813 checkCancel();
814 for (col = 0; col < raw_width; col++)
815 {
816 diff = ljpeg_diff(huff);
817 if (col < 2)
818 hpred[col] = vpred[row & 1][col] += diff;
819 else
820 hpred[col & 1] += diff;
821 RAW(row, col) = hpred[col & 1];
822 if (hpred[col & 1] >> tiff_bps)
823 derror();
824 }
825 }
826 }
nikon_read_curve()827 void LibRaw::nikon_read_curve()
828 {
829 ushort ver0, ver1, vpred[2][2], csize;
830 int i, step, max;
831
832 fseek(ifp, meta_offset, SEEK_SET);
833 ver0 = fgetc(ifp);
834 ver1 = fgetc(ifp);
835 if (ver0 == 0x49 || ver1 == 0x58)
836 fseek(ifp, 2110, SEEK_CUR);
837 read_shorts(vpred[0], 4);
838 step = max = 1 << tiff_bps & 0x7fff;
839 if ((csize = get2()) > 1)
840 step = max / (csize - 1);
841 if (ver0 == 0x44 && (ver1 == 0x20 || (ver1 == 0x40 && step > 3)) && step > 0)
842 {
843 if (ver1 == 0x40)
844 {
845 step /= 4;
846 max /= 4;
847 }
848 for (i = 0; i < csize; i++)
849 curve[i * step] = get2();
850 for (i = 0; i < max; i++)
851 curve[i] = (curve[i - i % step] * (step - i % step) +
852 curve[i - i % step + step] * (i % step)) /
853 step;
854 }
855 else if (ver0 != 0x46 && csize <= 0x4001)
856 read_shorts(curve, max = csize);
857 }
858
nikon_load_raw()859 void LibRaw::nikon_load_raw()
860 {
861 static const uchar nikon_tree[][32] = {
862 {0, 1, 5, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, /* 12-bit lossy */
863 5, 4, 3, 6, 2, 7, 1, 0, 8, 9, 11, 10, 12},
864 {0, 1, 5, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0,
865 0, 0, /* 12-bit lossy after split */
866 0x39, 0x5a, 0x38, 0x27, 0x16, 5, 4, 3, 2, 1, 0, 11, 12, 12},
867
868 {0, 1, 4, 2, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 12-bit lossless */
869 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, 11, 12},
870 {0, 1, 4, 3, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, /* 14-bit lossy */
871 5, 6, 4, 7, 8, 3, 9, 2, 1, 0, 10, 11, 12, 13, 14},
872 {0, 1, 5, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0,
873 0, /* 14-bit lossy after split */
874 8, 0x5c, 0x4b, 0x3a, 0x29, 7, 6, 5, 4, 3, 2, 1, 0, 13, 14},
875 {0, 1, 4, 2, 2, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, /* 14-bit lossless */
876 7, 6, 8, 5, 9, 4, 10, 3, 11, 12, 2, 0, 1, 13, 14}};
877 ushort *huff, ver0, ver1, vpred[2][2], hpred[2];
878 int i, min, max, tree = 0, split = 0, row, col, len, shl, diff;
879
880 fseek(ifp, meta_offset, SEEK_SET);
881 ver0 = fgetc(ifp);
882 ver1 = fgetc(ifp);
883 if (ver0 == 0x49 || ver1 == 0x58)
884 fseek(ifp, 2110, SEEK_CUR);
885 if (ver0 == 0x46)
886 tree = 2;
887 if (tiff_bps == 14)
888 tree += 3;
889 read_shorts(vpred[0], 4);
890 max = 1 << tiff_bps & 0x7fff;
891 if (ver0 == 0x44 && (ver1 == 0x20 || ver1 == 0x40))
892 {
893 if (ver1 == 0x40)
894 max /= 4;
895 fseek(ifp, meta_offset + 562, SEEK_SET);
896 split = get2();
897 }
898
899 while (max > 2 && (curve[max - 2] == curve[max - 1]))
900 max--;
901 huff = make_decoder(nikon_tree[tree]);
902 fseek(ifp, data_offset, SEEK_SET);
903 getbits(-1);
904 try
905 {
906 for (min = row = 0; row < height; row++)
907 {
908 checkCancel();
909 if (split && row == split)
910 {
911 free(huff);
912 huff = make_decoder(nikon_tree[tree + 1]);
913 max += (min = 16) << 1;
914 }
915 for (col = 0; col < raw_width; col++)
916 {
917 i = gethuff(huff);
918 len = i & 15;
919 shl = i >> 4;
920 diff = ((getbits(len - shl) << 1) + 1) << shl >> 1;
921 if (len > 0 && (diff & (1 << (len - 1))) == 0)
922 diff -= (1 << len) - !shl;
923 if (col < 2)
924 hpred[col] = vpred[row & 1][col] += diff;
925 else
926 hpred[col & 1] += diff;
927 if ((ushort)(hpred[col & 1] + min) >= max)
928 derror();
929 RAW(row, col) = curve[LIM((short)hpred[col & 1], 0, 0x3fff)];
930 }
931 }
932 }
933 catch (...)
934 {
935 free(huff);
936 throw;
937 }
938 free(huff);
939 }
940
nikon_yuv_load_raw()941 void LibRaw::nikon_yuv_load_raw()
942 {
943 if (!image)
944 throw LIBRAW_EXCEPTION_IO_CORRUPT;
945 int row, col, yuv[4]={0,0,0,0}, rgb[3], b, c;
946 UINT64 bitbuf = 0;
947 float cmul[4];
948 FORC4 { cmul[c] = cam_mul[c] > 0.001f ? cam_mul[c] : 1.f; }
949 for (row = 0; row < raw_height; row++)
950 {
951 checkCancel();
952
953 for (col = 0; col < raw_width; col++)
954 {
955 if (!(b = col & 1))
956 {
957 bitbuf = 0;
958 FORC(6) bitbuf |= (UINT64)fgetc(ifp) << c * 8;
959 FORC(4) yuv[c] = (bitbuf >> c * 12 & 0xfff) - (c >> 1 << 11);
960 }
961 rgb[0] = yuv[b] + 1.370705 * yuv[3];
962 rgb[1] = yuv[b] - 0.337633 * yuv[2] - 0.698001 * yuv[3];
963 rgb[2] = yuv[b] + 1.732446 * yuv[2];
964 FORC3 image[row * width + col][c] =
965 curve[LIM(rgb[c], 0, 0xfff)] / cmul[c];
966 }
967 }
968 }
969
rollei_load_raw()970 void LibRaw::rollei_load_raw()
971 {
972 uchar pixel[10];
973 unsigned iten = 0, isix, i, buffer = 0, todo[16];
974 if (raw_width > 32767 || raw_height > 32767)
975 throw LIBRAW_EXCEPTION_IO_BADFILE;
976 unsigned maxpixel = raw_width * (raw_height + 7);
977
978 isix = raw_width * raw_height * 5 / 8;
979 while (fread(pixel, 1, 10, ifp) == 10)
980 {
981 checkCancel();
982 for (i = 0; i < 10; i += 2)
983 {
984 todo[i] = iten++;
985 todo[i + 1] = pixel[i] << 8 | pixel[i + 1];
986 buffer = pixel[i] >> 2 | buffer << 6;
987 }
988 for (; i < 16; i += 2)
989 {
990 todo[i] = isix++;
991 todo[i + 1] = buffer >> (14 - i) * 5;
992 }
993 for (i = 0; i < 16; i += 2)
994 if (todo[i] < maxpixel)
995 raw_image[todo[i]] = (todo[i + 1] & 0x3ff);
996 else
997 derror();
998 }
999 maximum = 0x3ff;
1000 }
1001
nokia_load_raw()1002 void LibRaw::nokia_load_raw()
1003 {
1004 uchar *dp;
1005 int rev, dwide, row, col, c;
1006 double sum[] = {0, 0};
1007
1008 rev = 3 * (order == 0x4949);
1009 dwide = (raw_width * 5 + 1) / 4;
1010 #ifdef USE_6BY9RPI
1011 if (raw_stride)
1012 dwide = raw_stride;
1013 #endif
1014 std::vector<uchar> data(dwide * 2);
1015 for (row = 0; row < raw_height; row++)
1016 {
1017 checkCancel();
1018 if (fread(data.data() + dwide, 1, dwide, ifp) < dwide)
1019 derror();
1020 FORC(dwide) data[c] = data[dwide + (c ^ rev)];
1021 for (dp = data.data(), col = 0; col < raw_width; dp += 5, col += 4)
1022 FORC4 RAW(row, col + c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
1023 }
1024 maximum = 0x3ff;
1025 #ifdef USE_6BY9RPI
1026 if (!strcmp(make, "OmniVision") ||
1027 !strcmp(make, "Sony") ||
1028 !strcmp(make, "RaspberryPi")) return;
1029 #else
1030 if (strncmp(make, "OmniVision", 10))
1031 return;
1032 #endif
1033 row = raw_height / 2;
1034 FORC(width - 1)
1035 {
1036 sum[c & 1] += SQR(RAW(row, c) - RAW(row + 1, c + 1));
1037 sum[~c & 1] += SQR(RAW(row + 1, c) - RAW(row, c + 1));
1038 }
1039 if (sum[1] > sum[0])
1040 filters = 0x4b4b4b4b;
1041 }
1042
1043 #ifdef LIBRAW_OLD_VIDEO_SUPPORT
1044
canon_rmf_load_raw()1045 void LibRaw::canon_rmf_load_raw()
1046 {
1047 int row, col, bits, orow, ocol, c;
1048
1049 int *words = (int *)malloc(sizeof(int) * (raw_width / 3 + 1));
1050 merror(words, "canon_rmf_load_raw");
1051 for (row = 0; row < raw_height; row++)
1052 {
1053 checkCancel();
1054 fread(words, sizeof(int), raw_width / 3, ifp);
1055 for (col = 0; col < raw_width - 2; col += 3)
1056 {
1057 bits = words[col / 3];
1058 FORC3
1059 {
1060 orow = row;
1061 if ((ocol = col + c - 4) < 0)
1062 {
1063 ocol += raw_width;
1064 if ((orow -= 2) < 0)
1065 orow += raw_height;
1066 }
1067 RAW(orow, ocol) = curve[bits >> (10 * c + 2) & 0x3ff];
1068 }
1069 }
1070 }
1071 free(words);
1072 maximum = curve[0x3ff];
1073 }
1074 #endif
1075
pana_data(int nb,unsigned * bytes)1076 unsigned LibRaw::pana_data(int nb, unsigned *bytes)
1077 {
1078 #ifndef LIBRAW_NOTHREADS
1079 #define vpos tls->pana_data.vpos
1080 #define buf tls->pana_data.buf
1081 #else
1082 static uchar buf[0x4002];
1083 static int vpos;
1084 #endif
1085 int byte;
1086
1087 if (!nb && !bytes)
1088 return vpos = 0;
1089
1090 if (!vpos)
1091 {
1092 fread(buf + load_flags, 1, 0x4000 - load_flags, ifp);
1093 fread(buf, 1, load_flags, ifp);
1094 }
1095
1096 if (pana_encoding == 5)
1097 {
1098 for (byte = 0; byte < 16; byte++)
1099 {
1100 bytes[byte] = buf[vpos++];
1101 vpos &= 0x3FFF;
1102 }
1103 }
1104 else
1105 {
1106 vpos = (vpos - nb) & 0x1ffff;
1107 byte = vpos >> 3 ^ 0x3ff0;
1108 return (buf[byte] | buf[byte + 1] << 8) >> (vpos & 7) & ~((~0u) << nb);
1109 }
1110 return 0;
1111 #ifndef LIBRAW_NOTHREADS
1112 #undef vpos
1113 #undef buf
1114 #endif
1115 }
1116
panasonic_load_raw()1117 void LibRaw::panasonic_load_raw()
1118 {
1119 int row, col, i, j, sh = 0, pred[2], nonz[2];
1120 unsigned bytes[16];
1121 memset(bytes,0,sizeof(bytes)); // make gcc11 happy
1122 ushort *raw_block_data;
1123
1124 pana_data(0, 0);
1125
1126 int enc_blck_size = pana_bpp == 12 ? 10 : 9;
1127 if (pana_encoding == 5)
1128 {
1129 for (row = 0; row < raw_height; row++)
1130 {
1131 raw_block_data = raw_image + row * raw_width;
1132 checkCancel();
1133 for (col = 0; col < raw_width; col += enc_blck_size)
1134 {
1135 pana_data(0, bytes);
1136
1137 if (pana_bpp == 12)
1138 {
1139 raw_block_data[col] = ((bytes[1] & 0xF) << 8) + bytes[0];
1140 raw_block_data[col + 1] = 16 * bytes[2] + (bytes[1] >> 4);
1141 raw_block_data[col + 2] = ((bytes[4] & 0xF) << 8) + bytes[3];
1142 raw_block_data[col + 3] = 16 * bytes[5] + (bytes[4] >> 4);
1143 raw_block_data[col + 4] = ((bytes[7] & 0xF) << 8) + bytes[6];
1144 raw_block_data[col + 5] = 16 * bytes[8] + (bytes[7] >> 4);
1145 raw_block_data[col + 6] = ((bytes[10] & 0xF) << 8) + bytes[9];
1146 raw_block_data[col + 7] = 16 * bytes[11] + (bytes[10] >> 4);
1147 raw_block_data[col + 8] = ((bytes[13] & 0xF) << 8) + bytes[12];
1148 raw_block_data[col + 9] = 16 * bytes[14] + (bytes[13] >> 4);
1149 }
1150 else if (pana_bpp == 14)
1151 {
1152 raw_block_data[col] = bytes[0] + ((bytes[1] & 0x3F) << 8);
1153 raw_block_data[col + 1] =
1154 (bytes[1] >> 6) + 4 * (bytes[2]) + ((bytes[3] & 0xF) << 10);
1155 raw_block_data[col + 2] =
1156 (bytes[3] >> 4) + 16 * (bytes[4]) + ((bytes[5] & 3) << 12);
1157 raw_block_data[col + 3] = ((bytes[5] & 0xFC) >> 2) + (bytes[6] << 6);
1158 raw_block_data[col + 4] = bytes[7] + ((bytes[8] & 0x3F) << 8);
1159 raw_block_data[col + 5] =
1160 (bytes[8] >> 6) + 4 * bytes[9] + ((bytes[10] & 0xF) << 10);
1161 raw_block_data[col + 6] =
1162 (bytes[10] >> 4) + 16 * bytes[11] + ((bytes[12] & 3) << 12);
1163 raw_block_data[col + 7] =
1164 ((bytes[12] & 0xFC) >> 2) + (bytes[13] << 6);
1165 raw_block_data[col + 8] = bytes[14] + ((bytes[15] & 0x3F) << 8);
1166 }
1167 }
1168 }
1169 }
1170 else
1171 {
1172 for (row = 0; row < raw_height; row++)
1173 {
1174 checkCancel();
1175 for (col = 0; col < raw_width; col++)
1176 {
1177 if ((i = col % 14) == 0)
1178 pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
1179 if (i % 3 == 2)
1180 sh = 4 >> (3 - pana_data(2, 0));
1181 if (nonz[i & 1])
1182 {
1183 if ((j = pana_data(8, 0)))
1184 {
1185 if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
1186 pred[i & 1] &= ~((~0u) << sh);
1187 pred[i & 1] += j << sh;
1188 }
1189 }
1190 else if ((nonz[i & 1] = pana_data(8, 0)) || i > 11)
1191 pred[i & 1] = nonz[i & 1] << 4 | pana_data(4, 0);
1192 if ((RAW(row, col) = pred[col & 1]) > 4098 && col < width &&
1193 row < height)
1194 derror();
1195 }
1196 }
1197 }
1198 }
1199
olympus_load_raw()1200 void LibRaw::olympus_load_raw()
1201 {
1202 ushort huff[4096];
1203 int row, col, nbits, sign, low, high, i, c, w, n, nw;
1204 int acarry[2][3], *carry, pred, diff;
1205
1206 huff[n = 0] = 0xc0c;
1207 for (i = 12; i--;)
1208 FORC(2048 >> i) huff[++n] = (i + 1) << 8 | i;
1209 fseek(ifp, 7, SEEK_CUR);
1210 getbits(-1);
1211 for (row = 0; row < height; row++)
1212 {
1213 checkCancel();
1214 memset(acarry, 0, sizeof acarry);
1215 for (col = 0; col < raw_width; col++)
1216 {
1217 carry = acarry[col & 1];
1218 i = 2 * (carry[2] < 3);
1219 for (nbits = 2 + i; (ushort)carry[0] >> (nbits + i); nbits++)
1220 ;
1221 low = (sign = getbits(3)) & 3;
1222 sign = sign << 29 >> 31;
1223 if ((high = getbithuff(12, huff)) == 12)
1224 high = getbits(16 - nbits) >> 1;
1225 carry[0] = (high << nbits) | getbits(nbits);
1226 diff = (carry[0] ^ sign) + carry[1];
1227 carry[1] = (diff * 3 + carry[1]) >> 5;
1228 carry[2] = carry[0] > 16 ? 0 : carry[2] + 1;
1229 if (col >= width)
1230 continue;
1231 if (row < 2 && col < 2)
1232 pred = 0;
1233 else if (row < 2)
1234 pred = RAW(row, col - 2);
1235 else if (col < 2)
1236 pred = RAW(row - 2, col);
1237 else
1238 {
1239 w = RAW(row, col - 2);
1240 n = RAW(row - 2, col);
1241 nw = RAW(row - 2, col - 2);
1242 if ((w < nw && nw < n) || (n < nw && nw < w))
1243 {
1244 if (ABS(w - nw) > 32 || ABS(n - nw) > 32)
1245 pred = w + n - nw;
1246 else
1247 pred = (w + n) >> 1;
1248 }
1249 else
1250 pred = ABS(w - nw) > ABS(n - nw) ? w : n;
1251 }
1252 if ((RAW(row, col) = pred + ((diff << 2) | low)) >> 12)
1253 derror();
1254 }
1255 }
1256 }
1257
minolta_rd175_load_raw()1258 void LibRaw::minolta_rd175_load_raw()
1259 {
1260 uchar pixel[768];
1261 unsigned irow, box, row, col;
1262
1263 for (irow = 0; irow < 1481; irow++)
1264 {
1265 checkCancel();
1266 if (fread(pixel, 1, 768, ifp) < 768)
1267 derror();
1268 box = irow / 82;
1269 row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box - 12) * 2);
1270 switch (irow)
1271 {
1272 case 1477:
1273 case 1479:
1274 continue;
1275 case 1476:
1276 row = 984;
1277 break;
1278 case 1480:
1279 row = 985;
1280 break;
1281 case 1478:
1282 row = 985;
1283 box = 1;
1284 }
1285 if ((box < 12) && (box & 1))
1286 {
1287 for (col = 0; col < 1533; col++, row ^= 1)
1288 if (col != 1)
1289 RAW(row, col) = (col + 1) & 2
1290 ? pixel[col / 2 - 1] + pixel[col / 2 + 1]
1291 : pixel[col / 2] << 1;
1292 RAW(row, 1) = pixel[1] << 1;
1293 RAW(row, 1533) = pixel[765] << 1;
1294 }
1295 else
1296 for (col = row & 1; col < 1534; col += 2)
1297 RAW(row, col) = pixel[col / 2] << 1;
1298 }
1299 maximum = 0xff << 1;
1300 }
1301
quicktake_100_load_raw()1302 void LibRaw::quicktake_100_load_raw()
1303 {
1304 std::vector<uchar> pixel_buffer(484 * 644, 0x80);
1305 uchar* pixel = &pixel_buffer[0];
1306 static const short gstep[16] = {-89, -60, -44, -32, -22, -15, -8, -2,
1307 2, 8, 15, 22, 32, 44, 60, 89};
1308 static const short rstep[6][4] = {{-3, -1, 1, 3}, {-5, -1, 1, 5},
1309 {-8, -2, 2, 8}, {-13, -3, 3, 13},
1310 {-19, -4, 4, 19}, {-28, -6, 6, 28}};
1311 static const short t_curve[256] = {
1312 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14,
1313 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
1314 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
1315 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58,
1316 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
1317 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 88, 90,
1318 92, 94, 97, 99, 101, 103, 105, 107, 110, 112, 114, 116, 118, 120,
1319 123, 125, 127, 129, 131, 134, 136, 138, 140, 142, 144, 147, 149, 151,
1320 153, 155, 158, 160, 162, 164, 166, 168, 171, 173, 175, 177, 179, 181,
1321 184, 186, 188, 190, 192, 195, 197, 199, 201, 203, 205, 208, 210, 212,
1322 214, 216, 218, 221, 223, 226, 230, 235, 239, 244, 248, 252, 257, 261,
1323 265, 270, 274, 278, 283, 287, 291, 296, 300, 305, 309, 313, 318, 322,
1324 326, 331, 335, 339, 344, 348, 352, 357, 361, 365, 370, 374, 379, 383,
1325 387, 392, 396, 400, 405, 409, 413, 418, 422, 426, 431, 435, 440, 444,
1326 448, 453, 457, 461, 466, 470, 474, 479, 483, 487, 492, 496, 500, 508,
1327 519, 531, 542, 553, 564, 575, 587, 598, 609, 620, 631, 643, 654, 665,
1328 676, 687, 698, 710, 721, 732, 743, 754, 766, 777, 788, 799, 810, 822,
1329 833, 844, 855, 866, 878, 889, 900, 911, 922, 933, 945, 956, 967, 978,
1330 989, 1001, 1012, 1023};
1331 int rb, row, col, sharp, val = 0;
1332
1333 if (width > 640 || height > 480)
1334 throw LIBRAW_EXCEPTION_IO_CORRUPT;
1335
1336 getbits(-1);
1337 for (row = 2; row < height + 2; row++)
1338 {
1339 checkCancel();
1340 for (col = 2 + (row & 1); col < width + 2; col += 2)
1341 {
1342 val = ((pixel[(row - 1) * 644 + col - 1] + 2 * pixel[(row - 1) * 644 + col + 1] + pixel[row * 644 + col - 2]) >> 2) + gstep[getbits(4)];
1343 pixel[row * 644 + col] = val = LIM(val, 0, 255);
1344 if (col < 4)
1345 pixel[row * 644 + col - 2] = pixel[(row + 1) * 644 + (~row & 1)] = val;
1346 if (row == 2)
1347 pixel[(row - 1) * 644 + col + 1] = pixel[(row - 1) * 644 + col + 3] = val;
1348 }
1349 pixel[row * 644 + col] = val;
1350 }
1351 for (rb = 0; rb < 2; rb++)
1352 for (row = 2 + rb; row < height + 2; row += 2)
1353 {
1354 checkCancel();
1355 for (col = 3 - (row & 1); col < width + 2; col += 2)
1356 {
1357 if (row < 4 || col < 4)
1358 sharp = 2;
1359 else
1360 {
1361 val = ABS(pixel[(row - 2) * 644 + col] - pixel[row * 644 + col - 2]) + ABS(pixel[(row - 2) * 644 + col] - pixel[(row - 2) * 644 + col - 2]) +
1362 ABS(pixel[row * 644 + col - 2] - pixel[(row - 2) * 644 + col - 2]);
1363 sharp = val < 4
1364 ? 0
1365 : val < 8
1366 ? 1
1367 : val < 16 ? 2 : val < 32 ? 3 : val < 48 ? 4 : 5;
1368 }
1369 val = ((pixel[(row - 2) * 644 + col] + pixel[row * 644 + col - 2]) >> 1) + rstep[sharp][getbits(2)];
1370 pixel[row * 644 + col] = val = LIM(val, 0, 255);
1371 if (row < 4)
1372 pixel[(row - 2) * 644 + col + 2] = val;
1373 if (col < 4)
1374 pixel[(row + 2) * 644 + col - 2] = val;
1375 }
1376 }
1377 for (row = 2; row < height + 2; row++)
1378 {
1379 checkCancel();
1380 for (col = 3 - (row & 1); col < width + 2; col += 2)
1381 {
1382 val = ((pixel[row * 644 + col - 1] + (pixel[row * 644 + col] << 2) + pixel[row * 644 + col + 1]) >> 1) - 0x100;
1383 pixel[row * 644 + col] = LIM(val, 0, 255);
1384 }
1385 }
1386 for (row = 0; row < height; row++)
1387 {
1388 checkCancel();
1389 for (col = 0; col < width; col++)
1390 RAW(row, col) = t_curve[pixel[(row + 2) * 644 + col + 2]];
1391 }
1392 maximum = 0x3ff;
1393 }
1394
sony_load_raw()1395 void LibRaw::sony_load_raw()
1396 {
1397 uchar head[40];
1398 ushort *pixel;
1399 unsigned i, key, row, col;
1400
1401 fseek(ifp, 200896, SEEK_SET);
1402 fseek(ifp, (unsigned)fgetc(ifp) * 4 - 1, SEEK_CUR);
1403 order = 0x4d4d;
1404 key = get4();
1405
1406 fseek(ifp, 164600, SEEK_SET);
1407 fread(head, 1, 40, ifp);
1408 sony_decrypt((unsigned *)head, 10, 1, key);
1409 for (i = 26; i-- > 22;)
1410 key = key << 8 | head[i];
1411
1412 fseek(ifp, data_offset, SEEK_SET);
1413 for (row = 0; row < raw_height; row++)
1414 {
1415 checkCancel();
1416 pixel = raw_image + row * raw_width;
1417 if (fread(pixel, 2, raw_width, ifp) < raw_width)
1418 derror();
1419 sony_decrypt((unsigned *)pixel, raw_width / 2, !row, key);
1420 for (col = 0; col < raw_width; col++)
1421 if ((pixel[col] = ntohs(pixel[col])) >> 14)
1422 derror();
1423 }
1424 maximum = 0x3ff0;
1425 }
1426
sony_arw_load_raw()1427 void LibRaw::sony_arw_load_raw()
1428 {
1429 std::vector<ushort> huff_buffer(32770);
1430 ushort* huff = &huff_buffer[0];
1431 static const ushort tab[18] = {0xf11, 0xf10, 0xe0f, 0xd0e, 0xc0d, 0xb0c,
1432 0xa0b, 0x90a, 0x809, 0x708, 0x607, 0x506,
1433 0x405, 0x304, 0x303, 0x300, 0x202, 0x201};
1434 int i, c, n, col, row, sum = 0;
1435
1436 huff[0] = 15;
1437 for (n = i = 0; i < 18; i++)
1438 FORC(32768 >> (tab[i] >> 8)) huff[++n] = tab[i];
1439 getbits(-1);
1440 for (col = raw_width; col--;)
1441 {
1442 checkCancel();
1443 for (row = 0; row < raw_height + 1; row += 2)
1444 {
1445 if (row == raw_height)
1446 row = 1;
1447 if ((sum += ljpeg_diff(huff)) >> 12)
1448 derror();
1449 if (row < height)
1450 RAW(row, col) = sum;
1451 }
1452 }
1453 }
1454
sony_arw2_load_raw()1455 void LibRaw::sony_arw2_load_raw()
1456 {
1457 uchar *data, *dp;
1458 ushort pix[16];
1459 int row, col, val, max, min, imax, imin, sh, bit, i;
1460
1461 data = (uchar *)malloc(raw_width + 1);
1462 merror(data, "sony_arw2_load_raw()");
1463 try
1464 {
1465 for (row = 0; row < height; row++)
1466 {
1467 checkCancel();
1468 fread(data, 1, raw_width, ifp);
1469 for (dp = data, col = 0; col < raw_width - 30; dp += 16)
1470 {
1471 max = 0x7ff & (val = sget4(dp));
1472 min = 0x7ff & val >> 11;
1473 imax = 0x0f & val >> 22;
1474 imin = 0x0f & val >> 26;
1475 for (sh = 0; sh < 4 && 0x80 << sh <= max - min; sh++)
1476 ;
1477 /* flag checks if outside of loop */
1478 if (!(imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_ALLFLAGS) // no flag set
1479 || (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTATOVALUE))
1480 {
1481 for (bit = 30, i = 0; i < 16; i++)
1482 if (i == imax)
1483 pix[i] = max;
1484 else if (i == imin)
1485 pix[i] = min;
1486 else
1487 {
1488 pix[i] =
1489 ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
1490 if (pix[i] > 0x7ff)
1491 pix[i] = 0x7ff;
1492 bit += 7;
1493 }
1494 }
1495 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_BASEONLY)
1496 {
1497 for (bit = 30, i = 0; i < 16; i++)
1498 if (i == imax)
1499 pix[i] = max;
1500 else if (i == imin)
1501 pix[i] = min;
1502 else
1503 pix[i] = 0;
1504 }
1505 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTAONLY)
1506 {
1507 for (bit = 30, i = 0; i < 16; i++)
1508 if (i == imax)
1509 pix[i] = 0;
1510 else if (i == imin)
1511 pix[i] = 0;
1512 else
1513 {
1514 pix[i] =
1515 ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
1516 if (pix[i] > 0x7ff)
1517 pix[i] = 0x7ff;
1518 bit += 7;
1519 }
1520 }
1521 else if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTAZEROBASE)
1522 {
1523 for (bit = 30, i = 0; i < 16; i++)
1524 if (i == imax)
1525 pix[i] = 0;
1526 else if (i == imin)
1527 pix[i] = 0;
1528 else
1529 {
1530 pix[i] = ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh);
1531 if (pix[i] > 0x7ff)
1532 pix[i] = 0x7ff;
1533 bit += 7;
1534 }
1535 }
1536
1537 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTATOVALUE)
1538 {
1539 for (i = 0; i < 16; i++, col += 2)
1540 {
1541 unsigned slope =
1542 pix[i] < 1001 ? 2
1543 : curve[pix[i] << 1] - curve[(pix[i] << 1) - 2];
1544 unsigned step = 1 << sh;
1545 RAW(row, col) =
1546 curve[pix[i] << 1] >
1547 black + imgdata.rawparams.sony_arw2_posterization_thr
1548 ? LIM(((slope * step * 1000) /
1549 (curve[pix[i] << 1] - black)),
1550 0, 10000)
1551 : 0;
1552 }
1553 }
1554 else
1555 for (i = 0; i < 16; i++, col += 2)
1556 RAW(row, col) = curve[pix[i] << 1];
1557 col -= col & 1 ? 1 : 31;
1558 }
1559 }
1560 }
1561 catch (...)
1562 {
1563 free(data);
1564 throw;
1565 }
1566 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SONYARW2_DELTATOVALUE)
1567 maximum = 10000;
1568 free(data);
1569 }
1570
samsung_load_raw()1571 void LibRaw::samsung_load_raw()
1572 {
1573 int row, col, c, i, dir, op[4], len[4];
1574 if (raw_width > 32768 ||
1575 raw_height > 32768) // definitely too much for old samsung
1576 throw LIBRAW_EXCEPTION_IO_BADFILE;
1577 unsigned maxpixels = raw_width * (raw_height + 7);
1578
1579 order = 0x4949;
1580 for (row = 0; row < raw_height; row++)
1581 {
1582 checkCancel();
1583 fseek(ifp, strip_offset + row * 4, SEEK_SET);
1584 fseek(ifp, data_offset + get4(), SEEK_SET);
1585 ph1_bits(-1);
1586 FORC4 len[c] = row < 2 ? 7 : 4;
1587 for (col = 0; col < raw_width; col += 16)
1588 {
1589 dir = ph1_bits(1);
1590 FORC4 op[c] = ph1_bits(2);
1591 FORC4 switch (op[c])
1592 {
1593 case 3:
1594 len[c] = ph1_bits(4);
1595 break;
1596 case 2:
1597 len[c]--;
1598 break;
1599 case 1:
1600 len[c]++;
1601 }
1602 for (c = 0; c < 16; c += 2)
1603 {
1604 i = len[((c & 1) << 1) | (c >> 3)];
1605 unsigned idest = RAWINDEX(row, col + c);
1606 unsigned isrc = (dir ? RAWINDEX(row + (~c | -2), col + c)
1607 : col ? RAWINDEX(row, col + (c | -2)) : 0);
1608 if (idest < maxpixels &&
1609 isrc <
1610 maxpixels) // less than zero is handled by unsigned conversion
1611 RAW(row, col + c) = (i > 0 ? ((signed)ph1_bits(i) << (32 - i) >> (32 - i)) : 0) +
1612 (dir ? RAW(row + (~c | -2), col + c) : col ? RAW(row, col + (c | -2)) : 128);
1613 else
1614 derror();
1615 if (c == 14)
1616 c = -1;
1617 }
1618 }
1619 }
1620 for (row = 0; row < raw_height - 1; row += 2)
1621 for (col = 0; col < raw_width - 1; col += 2)
1622 SWAP(RAW(row, col + 1), RAW(row + 1, col));
1623 }
1624
samsung2_load_raw()1625 void LibRaw::samsung2_load_raw()
1626 {
1627 static const ushort tab[14] = {0x304, 0x307, 0x206, 0x205, 0x403,
1628 0x600, 0x709, 0x80a, 0x90b, 0xa0c,
1629 0xa0d, 0x501, 0x408, 0x402};
1630 ushort huff[1026], vpred[2][2] = {{0, 0}, {0, 0}}, hpred[2];
1631 int i, c, n, row, col, diff;
1632
1633 huff[0] = 10;
1634 for (n = i = 0; i < 14; i++)
1635 FORC(1024 >> (tab[i] >> 8)) huff[++n] = tab[i];
1636 getbits(-1);
1637 for (row = 0; row < raw_height; row++)
1638 {
1639 checkCancel();
1640 for (col = 0; col < raw_width; col++)
1641 {
1642 diff = ljpeg_diff(huff);
1643 if (col < 2)
1644 hpred[col] = vpred[row & 1][col] += diff;
1645 else
1646 hpred[col & 1] += diff;
1647 RAW(row, col) = hpred[col & 1];
1648 if (hpred[col & 1] >> tiff_bps)
1649 derror();
1650 }
1651 }
1652 }
1653
samsung3_load_raw()1654 void LibRaw::samsung3_load_raw()
1655 {
1656 int opt, init, mag, pmode, row, tab, col, pred, diff, i, c;
1657 ushort lent[3][2], len[4], *prow[2];
1658 order = 0x4949;
1659 fseek(ifp, 9, SEEK_CUR);
1660 opt = fgetc(ifp);
1661 init = (get2(), get2());
1662 for (row = 0; row < raw_height; row++)
1663 {
1664 checkCancel();
1665 fseek(ifp, (data_offset - ftell(ifp)) & 15, SEEK_CUR);
1666 ph1_bits(-1);
1667 mag = 0;
1668 pmode = 7;
1669 FORC(6)((ushort *)lent)[c] = row < 2 ? 7 : 4;
1670 prow[row & 1] = &RAW(row - 1, 1 - ((row & 1) << 1)); // green
1671 prow[~row & 1] = &RAW(row - 2, 0); // red and blue
1672 for (tab = 0; tab + 15 < raw_width; tab += 16)
1673 {
1674 if (~opt & 4 && !(tab & 63))
1675 {
1676 i = ph1_bits(2);
1677 mag = i < 3 ? mag - '2' + "204"[i] : ph1_bits(12);
1678 }
1679 if (opt & 2)
1680 pmode = 7 - 4 * ph1_bits(1);
1681 else if (!ph1_bits(1))
1682 pmode = ph1_bits(3);
1683 if (opt & 1 || !ph1_bits(1))
1684 {
1685 FORC4 len[c] = ph1_bits(2);
1686 FORC4
1687 {
1688 i = ((row & 1) << 1 | (c & 1)) % 3;
1689 if (i < 0)
1690 throw LIBRAW_EXCEPTION_IO_CORRUPT;
1691 len[c] = len[c] < 3 ? lent[i][0] - '1' + "120"[len[c]] : ph1_bits(4);
1692 lent[i][0] = lent[i][1];
1693 lent[i][1] = len[c];
1694 }
1695 }
1696 FORC(16)
1697 {
1698 col = tab + (((c & 7) << 1) ^ (c >> 3) ^ (row & 1));
1699 if (col < 0)
1700 throw LIBRAW_EXCEPTION_IO_CORRUPT;
1701 if (pmode < 0)
1702 throw LIBRAW_EXCEPTION_IO_CORRUPT;
1703 if (pmode != 7 && row >= 2 && (col - '4' + "0224468"[pmode]) < 0)
1704 throw LIBRAW_EXCEPTION_IO_CORRUPT;
1705 pred = (pmode == 7 || row < 2)
1706 ? (tab ? RAW(row, tab - 2 + (col & 1)) : init)
1707 : (prow[col & 1][col - '4' + "0224468"[pmode]] +
1708 prow[col & 1][col - '4' + "0244668"[pmode]] + 1) >>
1709 1;
1710 diff = ph1_bits(i = len[c >> 2]);
1711 if (i > 0 && diff >> (i - 1))
1712 diff -= 1 << i;
1713 diff = diff * (mag * 2 + 1) + mag;
1714 RAW(row, col) = pred + diff;
1715 }
1716 }
1717 }
1718 }
1719
1720 #ifdef LIBRAW_OLD_VIDEO_SUPPORT
redcine_load_raw()1721 void LibRaw::redcine_load_raw()
1722 {
1723 #ifndef NO_JASPER
1724 int c, row, col;
1725 jas_stream_t *in;
1726 jas_image_t *jimg;
1727 jas_matrix_t *jmat;
1728 jas_seqent_t *data;
1729 ushort *img, *pix;
1730
1731 jas_init();
1732 in = (jas_stream_t *)ifp->make_jas_stream();
1733 if (!in)
1734 throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
1735 jas_stream_seek(in, data_offset + 20, SEEK_SET);
1736 jimg = jas_image_decode(in, -1, 0);
1737 if (!jimg)
1738 {
1739 jas_stream_close(in);
1740 throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
1741 }
1742 jmat = jas_matrix_create(height / 2, width / 2);
1743 merror(jmat, "redcine_load_raw()");
1744 img = (ushort *)calloc((height + 2), (width + 2) * 2);
1745 merror(img, "redcine_load_raw()");
1746 bool fastexitflag = false;
1747 try
1748 {
1749 FORC4
1750 {
1751 checkCancel();
1752 jas_image_readcmpt(jimg, c, 0, 0, width / 2, height / 2, jmat);
1753 data = jas_matrix_getref(jmat, 0, 0);
1754 for (row = c >> 1; row < height; row += 2)
1755 for (col = c & 1; col < width; col += 2)
1756 img[(row + 1) * (width + 2) + col + 1] =
1757 data[(row / 2) * (width / 2) + col / 2];
1758 }
1759 for (col = 1; col <= width; col++)
1760 {
1761 img[col] = img[2 * (width + 2) + col];
1762 img[(height + 1) * (width + 2) + col] =
1763 img[(height - 1) * (width + 2) + col];
1764 }
1765 for (row = 0; row < height + 2; row++)
1766 {
1767 img[row * (width + 2)] = img[row * (width + 2) + 2];
1768 img[(row + 1) * (width + 2) - 1] = img[(row + 1) * (width + 2) - 3];
1769 }
1770 for (row = 1; row <= height; row++)
1771 {
1772 checkCancel();
1773 pix = img + row * (width + 2) + (col = 1 + (FC(row, 1) & 1));
1774 for (; col <= width; col += 2, pix += 2)
1775 {
1776 c = (((pix[0] - 0x800) << 3) + pix[-(width + 2)] + pix[width + 2] +
1777 pix[-1] + pix[1]) >>
1778 2;
1779 pix[0] = LIM(c, 0, 4095);
1780 }
1781 }
1782 for (row = 0; row < height; row++)
1783 {
1784 checkCancel();
1785 for (col = 0; col < width; col++)
1786 RAW(row, col) = curve[img[(row + 1) * (width + 2) + col + 1]];
1787 }
1788 }
1789 catch (...)
1790 {
1791 fastexitflag = true;
1792 }
1793 free(img);
1794 jas_matrix_destroy(jmat);
1795 jas_image_destroy(jimg);
1796 jas_stream_close(in);
1797 if (fastexitflag)
1798 throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;
1799 #endif
1800 }
1801 #endif
1802