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