1 /*
2   Copyright 2008-2018 LibRaw LLC (info@libraw.org)
3 
4 LibRaw is free software; you can redistribute it and/or modify
5 it under the terms of the one of two licenses as you choose:
6 
7 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
8    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
9 
10 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
11    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
12 
13    This file is generated from Dave Coffin's dcraw.c
14    dcraw.c -- Dave Coffin's raw photo decoder
15    Copyright 1997-2010 by Dave Coffin, dcoffin a cybercom o net
16 
17    Look into dcraw homepage (probably http://cybercom.net/~dcoffin/dcraw/)
18    for more information
19 */
20 
21 
22 #include <math.h>
23 #define CLASS LibRaw::
24 #include "libraw/libraw_types.h"
25 #define LIBRAW_LIBRARY_BUILD
26 #define LIBRAW_IO_REDEFINED
27 #include "libraw/libraw.h"
28 #include "internal/defines.h"
29 #include "internal/var_defines.h"
fcol(int row,int col)30 int CLASS fcol(int row, int col)
31 {
32   static const char filter[16][16] = {
33       {2, 1, 1, 3, 2, 3, 2, 0, 3, 2, 3, 0, 1, 2, 1, 0}, {0, 3, 0, 2, 0, 1, 3, 1, 0, 1, 1, 2, 0, 3, 3, 2},
34       {2, 3, 3, 2, 3, 1, 1, 3, 3, 1, 2, 1, 2, 0, 0, 3}, {0, 1, 0, 1, 0, 2, 0, 2, 2, 0, 3, 0, 1, 3, 2, 1},
35       {3, 1, 1, 2, 0, 1, 0, 2, 1, 3, 1, 3, 0, 1, 3, 0}, {2, 0, 0, 3, 3, 2, 3, 1, 2, 0, 2, 0, 3, 2, 2, 1},
36       {2, 3, 3, 1, 2, 1, 2, 1, 2, 1, 1, 2, 3, 0, 0, 1}, {1, 0, 0, 2, 3, 0, 0, 3, 0, 3, 0, 3, 2, 1, 2, 3},
37       {2, 3, 3, 1, 1, 2, 1, 0, 3, 2, 3, 0, 2, 3, 1, 3}, {1, 0, 2, 0, 3, 0, 3, 2, 0, 1, 1, 2, 0, 1, 0, 2},
38       {0, 1, 1, 3, 3, 2, 2, 1, 1, 3, 3, 0, 2, 1, 3, 2}, {2, 3, 2, 0, 0, 1, 3, 0, 2, 0, 1, 2, 3, 0, 1, 0},
39       {1, 3, 1, 2, 3, 2, 3, 2, 0, 2, 0, 1, 1, 0, 3, 0}, {0, 2, 0, 3, 1, 0, 0, 1, 1, 3, 3, 2, 3, 2, 2, 1},
40       {2, 1, 3, 2, 3, 1, 2, 1, 0, 3, 0, 2, 0, 2, 0, 2}, {0, 3, 1, 0, 0, 2, 0, 3, 2, 1, 3, 1, 1, 3, 1, 3}};
41 
42   if (filters == 1)
43     return filter[(row + top_margin) & 15][(col + left_margin) & 15];
44   if (filters == 9)
45     return xtrans[(row + 6) % 6][(col + 6) % 6];
46   return FC(row, col);
47 }
48 
49 #if !defined(__FreeBSD__)
local_strnlen(const char * s,size_t n)50 static size_t local_strnlen(const char *s, size_t n)
51 {
52   const char *p = (const char *)memchr(s, 0, n);
53   return (p ? p - s : n);
54 }
55 /* add OS X version check here ?? */
56 #define strnlen(a, b) local_strnlen(a, b)
57 #endif
58 
59 #ifdef LIBRAW_LIBRARY_BUILD
60 static int Fuji_wb_list1[] = {LIBRAW_WBI_FineWeather, LIBRAW_WBI_Shade, LIBRAW_WBI_FL_D,
61                               LIBRAW_WBI_FL_L,        LIBRAW_WBI_FL_W,  LIBRAW_WBI_Tungsten};
62 static int nFuji_wb_list1 = sizeof(Fuji_wb_list1) / sizeof(int);
63 static int FujiCCT_K[31] = {2500, 2550, 2650, 2700, 2800, 2850, 2950, 3000, 3100, 3200, 3300,
64                             3400, 3600, 3700, 3800, 4000, 4200, 4300, 4500, 4800, 5000, 5300,
65                             5600, 5900, 6300, 6700, 7100, 7700, 8300, 9100, 10000};
66 static int Fuji_wb_list2[] = {LIBRAW_WBI_Auto,  0,  LIBRAW_WBI_Custom,   6,  LIBRAW_WBI_FineWeather, 1,
67                               LIBRAW_WBI_Shade, 8,  LIBRAW_WBI_FL_D,     10, LIBRAW_WBI_FL_L,        11,
68                               LIBRAW_WBI_FL_W,  12, LIBRAW_WBI_Tungsten, 2,  LIBRAW_WBI_Underwater,  35,
69                               LIBRAW_WBI_Ill_A, 82, LIBRAW_WBI_D65,      83};
70 static int nFuji_wb_list2 = sizeof(Fuji_wb_list2) / sizeof(int);
71 
72 static int Oly_wb_list1[] = {LIBRAW_WBI_Shade,    LIBRAW_WBI_Cloudy, LIBRAW_WBI_FineWeather,
73                              LIBRAW_WBI_Tungsten, LIBRAW_WBI_Sunset, LIBRAW_WBI_FL_D,
74                              LIBRAW_WBI_FL_N,     LIBRAW_WBI_FL_W,   LIBRAW_WBI_FL_WW};
75 
76 static int Oly_wb_list2[] = {LIBRAW_WBI_Auto,
77                              0,
78                              LIBRAW_WBI_Tungsten,
79                              3000,
80                              0x100,
81                              3300,
82                              0x100,
83                              3600,
84                              0x100,
85                              3900,
86                              LIBRAW_WBI_FL_W,
87                              4000,
88                              0x100,
89                              4300,
90                              LIBRAW_WBI_FL_D,
91                              4500,
92                              0x100,
93                              4800,
94                              LIBRAW_WBI_FineWeather,
95                              5300,
96                              LIBRAW_WBI_Cloudy,
97                              6000,
98                              LIBRAW_WBI_FL_N,
99                              6600,
100                              LIBRAW_WBI_Shade,
101                              7500,
102                              LIBRAW_WBI_Custom1,
103                              0,
104                              LIBRAW_WBI_Custom2,
105                              0,
106                              LIBRAW_WBI_Custom3,
107                              0,
108                              LIBRAW_WBI_Custom4,
109                              0};
110 
111 static int Pentax_wb_list1[] = {LIBRAW_WBI_Daylight, LIBRAW_WBI_Shade, LIBRAW_WBI_Cloudy, LIBRAW_WBI_Tungsten,
112                                 LIBRAW_WBI_FL_D,     LIBRAW_WBI_FL_N,  LIBRAW_WBI_FL_W,   LIBRAW_WBI_Flash};
113 
114 static int Pentax_wb_list2[] = {LIBRAW_WBI_Daylight, LIBRAW_WBI_Shade, LIBRAW_WBI_Cloudy,
115                                 LIBRAW_WBI_Tungsten, LIBRAW_WBI_FL_D,  LIBRAW_WBI_FL_N,
116                                 LIBRAW_WBI_FL_W,     LIBRAW_WBI_Flash, LIBRAW_WBI_FL_L};
117 static int nPentax_wb_list2 = sizeof(Pentax_wb_list2) / sizeof(int);
118 
stread(char * buf,size_t len,LibRaw_abstract_datastream * fp)119 static int stread(char *buf, size_t len, LibRaw_abstract_datastream *fp)
120 {
121   if(len>0)
122   {
123     int r = fp->read(buf, len, 1);
124     buf[len - 1] = 0;
125     return r;
126   }
127   else
128     return 0;
129 }
130 #define stmread(buf, maxlen, fp) stread(buf, MIN(maxlen, sizeof(buf)), fp)
131 #endif
132 
133 #if !defined(__GLIBC__) && !defined(__FreeBSD__)
my_memmem(char * haystack,size_t haystacklen,char * needle,size_t needlelen)134 char *my_memmem(char *haystack, size_t haystacklen, char *needle, size_t needlelen)
135 {
136   char *c;
137   for (c = haystack; c <= haystack + haystacklen - needlelen; c++)
138     if (!memcmp(c, needle, needlelen))
139       return c;
140   return 0;
141 }
142 #define memmem my_memmem
my_strcasestr(char * haystack,const char * needle)143 char *my_strcasestr(char *haystack, const char *needle)
144 {
145   char *c;
146   for (c = haystack; *c; c++)
147     if (!strncasecmp(c, needle, strlen(needle)))
148       return c;
149   return 0;
150 }
151 #define strcasestr my_strcasestr
152 #endif
153 
154 #define strbuflen(buf) strnlen(buf, sizeof(buf) - 1)
155 
sget2(uchar * s)156 ushort CLASS sget2(uchar *s)
157 {
158   if (order == 0x4949) /* "II" means little-endian */
159     return s[0] | s[1] << 8;
160   else /* "MM" means big-endian */
161     return s[0] << 8 | s[1];
162 }
163 
164 // DNG was written by:
165 #define nonDNG 0
166 #define CameraDNG 1
167 #define AdobeDNG 2
168 
169 #ifdef LIBRAW_LIBRARY_BUILD
getwords(char * line,char * words[],int maxwords,int maxlen)170 static int getwords(char *line, char *words[], int maxwords, int maxlen)
171 {
172   line[maxlen - 1] = 0;
173   char *p = line;
174   int nwords = 0;
175 
176   while (1)
177   {
178     while (isspace(*p))
179       p++;
180     if (*p == '\0')
181       return nwords;
182     words[nwords++] = p;
183     while (!isspace(*p) && *p != '\0')
184       p++;
185     if (*p == '\0')
186       return nwords;
187     *p++ = '\0';
188     if (nwords >= maxwords)
189       return nwords;
190   }
191 }
192 
saneSonyCameraInfo(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f)193 static ushort saneSonyCameraInfo(uchar a, uchar b, uchar c, uchar d, uchar e, uchar f)
194 {
195   if ((a >> 4) > 9)
196     return 0;
197   else if ((a & 0x0f) > 9)
198     return 0;
199   else if ((b >> 4) > 9)
200     return 0;
201   else if ((b & 0x0f) > 9)
202     return 0;
203   else if ((c >> 4) > 9)
204     return 0;
205   else if ((c & 0x0f) > 9)
206     return 0;
207   else if ((d >> 4) > 9)
208     return 0;
209   else if ((d & 0x0f) > 9)
210     return 0;
211   else if ((e >> 4) > 9)
212     return 0;
213   else if ((e & 0x0f) > 9)
214     return 0;
215   else if ((f >> 4) > 9)
216     return 0;
217   else if ((f & 0x0f) > 9)
218     return 0;
219   return 1;
220 }
221 
bcd2dec(uchar data)222 static ushort bcd2dec(uchar data)
223 {
224   if ((data >> 4) > 9)
225     return 0;
226   else if ((data & 0x0f) > 9)
227     return 0;
228   else
229     return (data >> 4) * 10 + (data & 0x0f);
230 }
231 
232 static uchar SonySubstitution[257] =
233     "\x00\x01\x32\xb1\x0a\x0e\x87\x28\x02\xcc\xca\xad\x1b\xdc\x08\xed\x64\x86\xf0\x4f\x8c\x6c\xb8\xcb\x69\xc4\x2c\x03"
234     "\x97\xb6\x93\x7c\x14\xf3\xe2\x3e\x30\x8e\xd7\x60\x1c\xa1\xab\x37\xec\x75\xbe\x23\x15\x6a\x59\x3f\xd0\xb9\x96\xb5"
235     "\x50\x27\x88\xe3\x81\x94\xe0\xc0\x04\x5c\xc6\xe8\x5f\x4b\x70\x38\x9f\x82\x80\x51\x2b\xc5\x45\x49\x9b\x21\x52\x53"
236     "\x54\x85\x0b\x5d\x61\xda\x7b\x55\x26\x24\x07\x6e\x36\x5b\x47\xb7\xd9\x4a\xa2\xdf\xbf\x12\x25\xbc\x1e\x7f\x56\xea"
237     "\x10\xe6\xcf\x67\x4d\x3c\x91\x83\xe1\x31\xb3\x6f\xf4\x05\x8a\x46\xc8\x18\x76\x68\xbd\xac\x92\x2a\x13\xe9\x0f\xa3"
238     "\x7a\xdb\x3d\xd4\xe7\x3a\x1a\x57\xaf\x20\x42\xb2\x9e\xc3\x8b\xf2\xd5\xd3\xa4\x7e\x1f\x98\x9c\xee\x74\xa5\xa6\xa7"
239     "\xd8\x5e\xb0\xb4\x34\xce\xa8\x79\x77\x5a\xc1\x89\xae\x9a\x11\x33\x9d\xf5\x39\x19\x65\x78\x16\x71\xd2\xa9\x44\x63"
240     "\x40\x29\xba\xa0\x8f\xe4\xd6\x3b\x84\x0d\xc2\x4e\x58\xdd\x99\x22\x6b\xc9\xbb\x17\x06\xe5\x7d\x66\x43\x62\xf6\xcd"
241     "\x35\x90\x2e\x41\x8d\x6d\xaa\x09\x73\x95\x0c\xf1\x1d\xde\x4c\x2f\x2d\xf7\xd1\x72\xeb\xef\x48\xc7\xf8\xf9\xfa\xfb"
242     "\xfc\xfd\xfe\xff";
243 
sget2Rev(uchar * s)244 ushort CLASS sget2Rev(uchar *s) // specific to some Canon Makernotes fields, where they have endian in reverse
245 {
246   if (order == 0x4d4d) /* "II" means little-endian, and we reverse to "MM" - big endian */
247     return s[0] | s[1] << 8;
248   else /* "MM" means big-endian... */
249     return s[0] << 8 | s[1];
250 }
251 #endif
252 
get2()253 ushort CLASS get2()
254 {
255   uchar str[2] = {0xff, 0xff};
256   fread(str, 1, 2, ifp);
257   return sget2(str);
258 }
259 
sget4(uchar * s)260 unsigned CLASS sget4(uchar *s)
261 {
262   if (order == 0x4949)
263     return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
264   else
265     return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
266 }
267 #define sget4(s) sget4((uchar *)s)
268 
get4()269 unsigned CLASS get4()
270 {
271   uchar str[4] = {0xff, 0xff, 0xff, 0xff};
272   fread(str, 1, 4, ifp);
273   return sget4(str);
274 }
275 
getint(int type)276 unsigned CLASS getint(int type) { return type == 3 ? get2() : get4(); }
277 
int_to_float(int i)278 float CLASS int_to_float(int i)
279 {
280   union {
281     int i;
282     float f;
283   } u;
284   u.i = i;
285   return u.f;
286 }
287 
getreal(int type)288 double CLASS getreal(int type)
289 {
290   union {
291     char c[8];
292     double d;
293   } u, v;
294   int i, rev;
295 
296   switch (type)
297   {
298   case 3:
299     return (unsigned short)get2();
300   case 4:
301     return (unsigned int)get4();
302   case 5:
303     u.d = (unsigned int)get4();
304     v.d = (unsigned int)get4();
305     return u.d / (v.d ? v.d : 1);
306   case 8:
307     return (signed short)get2();
308   case 9:
309     return (signed int)get4();
310   case 10:
311     u.d = (signed int)get4();
312     v.d = (signed int)get4();
313     return u.d / (v.d ? v.d : 1);
314   case 11:
315     return int_to_float(get4());
316   case 12:
317     rev = 7 * ((order == 0x4949) == (ntohs(0x1234) == 0x1234));
318     for (i = 0; i < 8; i++)
319       u.c[i ^ rev] = fgetc(ifp);
320     return u.d;
321   default:
322     return fgetc(ifp);
323   }
324 }
325 
read_shorts(ushort * pixel,unsigned count)326 void CLASS read_shorts(ushort *pixel, unsigned count)
327 {
328   if (fread(pixel, 2, count, ifp) < count)
329     derror();
330   if ((order == 0x4949) == (ntohs(0x1234) == 0x1234))
331     swab((char *)pixel, (char *)pixel, count * 2);
332 }
333 
cubic_spline(const int * x_,const int * y_,const int len)334 void CLASS cubic_spline(const int *x_, const int *y_, const int len)
335 {
336   float **A, *b, *c, *d, *x, *y;
337   int i, j;
338 
339   A = (float **)calloc(((2 * len + 4) * sizeof **A + sizeof *A), 2 * len);
340   if (!A)
341     return;
342   A[0] = (float *)(A + 2 * len);
343   for (i = 1; i < 2 * len; i++)
344     A[i] = A[0] + 2 * len * i;
345   y = len + (x = i + (d = i + (c = i + (b = A[0] + i * i))));
346   for (i = 0; i < len; i++)
347   {
348     x[i] = x_[i] / 65535.0;
349     y[i] = y_[i] / 65535.0;
350   }
351   for (i = len - 1; i > 0; i--)
352   {
353     b[i] = (y[i] - y[i - 1]) / (x[i] - x[i - 1]);
354     d[i - 1] = x[i] - x[i - 1];
355   }
356   for (i = 1; i < len - 1; i++)
357   {
358     A[i][i] = 2 * (d[i - 1] + d[i]);
359     if (i > 1)
360     {
361       A[i][i - 1] = d[i - 1];
362       A[i - 1][i] = d[i - 1];
363     }
364     A[i][len - 1] = 6 * (b[i + 1] - b[i]);
365   }
366   for (i = 1; i < len - 2; i++)
367   {
368     float v = A[i + 1][i] / A[i][i];
369     for (j = 1; j <= len - 1; j++)
370       A[i + 1][j] -= v * A[i][j];
371   }
372   for (i = len - 2; i > 0; i--)
373   {
374     float acc = 0;
375     for (j = i; j <= len - 2; j++)
376       acc += A[i][j] * c[j];
377     c[i] = (A[i][len - 1] - acc) / A[i][i];
378   }
379   for (i = 0; i < 0x10000; i++)
380   {
381     float x_out = (float)(i / 65535.0);
382     float y_out = 0;
383     for (j = 0; j < len - 1; j++)
384     {
385       if (x[j] <= x_out && x_out <= x[j + 1])
386       {
387         float v = x_out - x[j];
388         y_out = y[j] + ((y[j + 1] - y[j]) / d[j] - (2 * d[j] * c[j] + c[j + 1] * d[j]) / 6) * v + (c[j] * 0.5) * v * v +
389                 ((c[j + 1] - c[j]) / (6 * d[j])) * v * v * v;
390       }
391     }
392     curve[i] = y_out < 0.0 ? 0 : (y_out >= 1.0 ? 65535 : (ushort)(y_out * 65535.0 + 0.5));
393   }
394   free(A);
395 }
396 
canon_600_fixed_wb(int temp)397 void CLASS canon_600_fixed_wb(int temp)
398 {
399   static const short mul[4][5] = {
400       {667, 358, 397, 565, 452}, {731, 390, 367, 499, 517}, {1119, 396, 348, 448, 537}, {1399, 485, 431, 508, 688}};
401   int lo, hi, i;
402   float frac = 0;
403 
404   for (lo = 4; --lo;)
405     if (*mul[lo] <= temp)
406       break;
407   for (hi = 0; hi < 3; hi++)
408     if (*mul[hi] >= temp)
409       break;
410   if (lo != hi)
411     frac = (float)(temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
412   for (i = 1; i < 5; i++)
413     pre_mul[i - 1] = 1 / (frac * mul[hi][i] + (1 - frac) * mul[lo][i]);
414 }
415 
416 /* Return values:  0 = white  1 = near white  2 = not white */
canon_600_color(int ratio[2],int mar)417 int CLASS canon_600_color(int ratio[2], int mar)
418 {
419   int clipped = 0, target, miss;
420 
421   if (flash_used)
422   {
423     if (ratio[1] < -104)
424     {
425       ratio[1] = -104;
426       clipped = 1;
427     }
428     if (ratio[1] > 12)
429     {
430       ratio[1] = 12;
431       clipped = 1;
432     }
433   }
434   else
435   {
436     if (ratio[1] < -264 || ratio[1] > 461)
437       return 2;
438     if (ratio[1] < -50)
439     {
440       ratio[1] = -50;
441       clipped = 1;
442     }
443     if (ratio[1] > 307)
444     {
445       ratio[1] = 307;
446       clipped = 1;
447     }
448   }
449   target = flash_used || ratio[1] < 197 ? -38 - (398 * ratio[1] >> 10) : -123 + (48 * ratio[1] >> 10);
450   if (target - mar <= ratio[0] && target + 20 >= ratio[0] && !clipped)
451     return 0;
452   miss = target - ratio[0];
453   if (abs(miss) >= mar * 4)
454     return 2;
455   if (miss < -20)
456     miss = -20;
457   if (miss > mar)
458     miss = mar;
459   ratio[0] = target - miss;
460   return 1;
461 }
462 
canon_600_auto_wb()463 void CLASS canon_600_auto_wb()
464 {
465   int mar, row, col, i, j, st, count[] = {0, 0};
466   int test[8], total[2][8], ratio[2][2], stat[2];
467 
468   memset(&total, 0, sizeof total);
469   i = canon_ev + 0.5;
470   if (i < 10)
471     mar = 150;
472   else if (i > 12)
473     mar = 20;
474   else
475     mar = 280 - 20 * i;
476   if (flash_used)
477     mar = 80;
478   for (row = 14; row < height - 14; row += 4)
479     for (col = 10; col < width; col += 2)
480     {
481       for (i = 0; i < 8; i++)
482         test[(i & 4) + FC(row + (i >> 1), col + (i & 1))] = BAYER(row + (i >> 1), col + (i & 1));
483       for (i = 0; i < 8; i++)
484         if (test[i] < 150 || test[i] > 1500)
485           goto next;
486       for (i = 0; i < 4; i++)
487         if (abs(test[i] - test[i + 4]) > 50)
488           goto next;
489       for (i = 0; i < 2; i++)
490       {
491         for (j = 0; j < 4; j += 2)
492           ratio[i][j >> 1] = ((test[i * 4 + j + 1] - test[i * 4 + j]) << 10) / test[i * 4 + j];
493         stat[i] = canon_600_color(ratio[i], mar);
494       }
495       if ((st = stat[0] | stat[1]) > 1)
496         goto next;
497       for (i = 0; i < 2; i++)
498         if (stat[i])
499           for (j = 0; j < 2; j++)
500             test[i * 4 + j * 2 + 1] = test[i * 4 + j * 2] * (0x400 + ratio[i][j]) >> 10;
501       for (i = 0; i < 8; i++)
502         total[st][i] += test[i];
503       count[st]++;
504     next:;
505     }
506   if (count[0] | count[1])
507   {
508     st = count[0] * 200 < count[1];
509     for (i = 0; i < 4; i++)
510       pre_mul[i] = 1.0 / (total[st][i] + total[st][i + 4]);
511   }
512 }
513 
canon_600_coeff()514 void CLASS canon_600_coeff()
515 {
516   static const short table[6][12] = {{-190, 702, -1878, 2390, 1861, -1349, 905, -393, -432, 944, 2617, -2105},
517                                      {-1203, 1715, -1136, 1648, 1388, -876, 267, 245, -1641, 2153, 3921, -3409},
518                                      {-615, 1127, -1563, 2075, 1437, -925, 509, 3, -756, 1268, 2519, -2007},
519                                      {-190, 702, -1886, 2398, 2153, -1641, 763, -251, -452, 964, 3040, -2528},
520                                      {-190, 702, -1878, 2390, 1861, -1349, 905, -393, -432, 944, 2617, -2105},
521                                      {-807, 1319, -1785, 2297, 1388, -876, 769, -257, -230, 742, 2067, -1555}};
522   int t = 0, i, c;
523   float mc, yc;
524 
525   mc = pre_mul[1] / pre_mul[2];
526   yc = pre_mul[3] / pre_mul[2];
527   if (mc > 1 && mc <= 1.28 && yc < 0.8789)
528     t = 1;
529   if (mc > 1.28 && mc <= 2)
530   {
531     if (yc < 0.8789)
532       t = 3;
533     else if (yc <= 2)
534       t = 4;
535   }
536   if (flash_used)
537     t = 5;
538   for (raw_color = i = 0; i < 3; i++)
539     FORCC rgb_cam[i][c] = table[t][i * 4 + c] / 1024.0;
540 }
541 
canon_600_load_raw()542 void CLASS canon_600_load_raw()
543 {
544   uchar data[1120], *dp;
545   ushort *pix;
546   int irow, row;
547 
548   for (irow = row = 0; irow < height; irow++)
549   {
550 #ifdef LIBRAW_LIBRARY_BUILD
551     checkCancel();
552 #endif
553     if (fread(data, 1, 1120, ifp) < 1120)
554       derror();
555     pix = raw_image + row * raw_width;
556     for (dp = data; dp < data + 1120; dp += 10, pix += 8)
557     {
558       pix[0] = (dp[0] << 2) + (dp[1] >> 6);
559       pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
560       pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
561       pix[3] = (dp[4] << 2) + (dp[1] & 3);
562       pix[4] = (dp[5] << 2) + (dp[9] & 3);
563       pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
564       pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
565       pix[7] = (dp[8] << 2) + (dp[9] >> 6);
566     }
567     if ((row += 2) > height)
568       row = 1;
569   }
570 }
571 
canon_600_correct()572 void CLASS canon_600_correct()
573 {
574   int row, col, val;
575   static const short mul[4][2] = {{1141, 1145}, {1128, 1109}, {1178, 1149}, {1128, 1109}};
576 
577   for (row = 0; row < height; row++)
578   {
579 #ifdef LIBRAW_LIBRARY_BUILD
580     checkCancel();
581 #endif
582     for (col = 0; col < width; col++)
583     {
584       if ((val = BAYER(row, col) - black) < 0)
585         val = 0;
586       val = val * mul[row & 3][col & 1] >> 9;
587       BAYER(row, col) = val;
588     }
589   }
590   canon_600_fixed_wb(1311);
591   canon_600_auto_wb();
592   canon_600_coeff();
593   maximum = (0x3ff - black) * 1109 >> 9;
594   black = 0;
595 }
596 
canon_s2is()597 int CLASS canon_s2is()
598 {
599   unsigned row;
600 
601   for (row = 0; row < 100; row++)
602   {
603     fseek(ifp, row * 3340 + 3284, SEEK_SET);
604     if (getc(ifp) > 15)
605       return 1;
606   }
607   return 0;
608 }
609 
getbithuff(int nbits,ushort * huff)610 unsigned CLASS getbithuff(int nbits, ushort *huff)
611 {
612 #ifdef LIBRAW_NOTHREADS
613   static unsigned bitbuf = 0;
614   static int vbits = 0, reset = 0;
615 #else
616 #define bitbuf tls->getbits.bitbuf
617 #define vbits tls->getbits.vbits
618 #define reset tls->getbits.reset
619 #endif
620   unsigned c;
621 
622   if (nbits > 25)
623     return 0;
624   if (nbits < 0)
625     return bitbuf = vbits = reset = 0;
626   if (nbits == 0 || vbits < 0)
627     return 0;
628   while (!reset && vbits < nbits && (c = fgetc(ifp)) != EOF && !(reset = zero_after_ff && c == 0xff && fgetc(ifp)))
629   {
630     bitbuf = (bitbuf << 8) + (uchar)c;
631     vbits += 8;
632   }
633   c = bitbuf << (32 - vbits) >> (32 - nbits);
634   if (huff)
635   {
636     vbits -= huff[c] >> 8;
637     c = (uchar)huff[c];
638   }
639   else
640     vbits -= nbits;
641   if (vbits < 0)
642     derror();
643   return c;
644 #ifndef LIBRAW_NOTHREADS
645 #undef bitbuf
646 #undef vbits
647 #undef reset
648 #endif
649 }
650 
651 #define getbits(n) getbithuff(n, 0)
652 #define gethuff(h) getbithuff(*h, h + 1)
653 
654 /*
655    Construct a decode tree according the specification in *source.
656    The first 16 bytes specify how many codes should be 1-bit, 2-bit
657    3-bit, etc.  Bytes after that are the leaf values.
658 
659    For example, if the source is
660 
661     { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
662       0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff  },
663 
664    then the code is
665 
666         00		0x04
667         010		0x03
668         011		0x05
669         100		0x06
670         101		0x02
671         1100		0x07
672         1101		0x01
673         11100		0x08
674         11101		0x09
675         11110		0x00
676         111110		0x0a
677         1111110		0x0b
678         1111111		0xff
679  */
make_decoder_ref(const uchar ** source)680 ushort *CLASS make_decoder_ref(const uchar **source)
681 {
682   int max, len, h, i, j;
683   const uchar *count;
684   ushort *huff;
685 
686   count = (*source += 16) - 17;
687   for (max = 16; max && !count[max]; max--)
688     ;
689   huff = (ushort *)calloc(1 + (1 << max), sizeof *huff);
690   merror(huff, "make_decoder()");
691   huff[0] = max;
692   for (h = len = 1; len <= max; len++)
693     for (i = 0; i < count[len]; i++, ++*source)
694       for (j = 0; j < 1 << (max - len); j++)
695         if (h <= 1 << max)
696           huff[h++] = len << 8 | **source;
697   return huff;
698 }
699 
make_decoder(const uchar * source)700 ushort *CLASS make_decoder(const uchar *source) { return make_decoder_ref(&source); }
701 
crw_init_tables(unsigned table,ushort * huff[2])702 void CLASS crw_init_tables(unsigned table, ushort *huff[2])
703 {
704   static const uchar first_tree[3][29] = {
705       {0, 1,    4,    2,    3,    1,    2,    0,    0,    0,    0,    0,    0,    0,   0,
706        0, 0x04, 0x03, 0x05, 0x06, 0x02, 0x07, 0x01, 0x08, 0x09, 0x00, 0x0a, 0x0b, 0xff},
707       {0, 2,    2,    3,    1,    1,    1,    1,    2,    0,    0,    0,    0,    0,   0,
708        0, 0x03, 0x02, 0x04, 0x01, 0x05, 0x00, 0x06, 0x07, 0x09, 0x08, 0x0a, 0x0b, 0xff},
709       {0, 0,    6,    3,    1,    1,    2,    0,    0,    0,    0,    0,    0,    0,   0,
710        0, 0x06, 0x05, 0x07, 0x04, 0x08, 0x03, 0x09, 0x02, 0x00, 0x0a, 0x01, 0x0b, 0xff},
711   };
712   static const uchar second_tree[3][180] = {
713       {0,    2,    2,    2,    1,    4,    2,    1,    2,    5,    1,    1,    0,    0,    0,    139,  0x03, 0x04,
714        0x02, 0x05, 0x01, 0x06, 0x07, 0x08, 0x12, 0x13, 0x11, 0x14, 0x09, 0x15, 0x22, 0x00, 0x21, 0x16, 0x0a, 0xf0,
715        0x23, 0x17, 0x24, 0x31, 0x32, 0x18, 0x19, 0x33, 0x25, 0x41, 0x34, 0x42, 0x35, 0x51, 0x36, 0x37, 0x38, 0x29,
716        0x79, 0x26, 0x1a, 0x39, 0x56, 0x57, 0x28, 0x27, 0x52, 0x55, 0x58, 0x43, 0x76, 0x59, 0x77, 0x54, 0x61, 0xf9,
717        0x71, 0x78, 0x75, 0x96, 0x97, 0x49, 0xb7, 0x53, 0xd7, 0x74, 0xb6, 0x98, 0x47, 0x48, 0x95, 0x69, 0x99, 0x91,
718        0xfa, 0xb8, 0x68, 0xb5, 0xb9, 0xd6, 0xf7, 0xd8, 0x67, 0x46, 0x45, 0x94, 0x89, 0xf8, 0x81, 0xd5, 0xf6, 0xb4,
719        0x88, 0xb1, 0x2a, 0x44, 0x72, 0xd9, 0x87, 0x66, 0xd4, 0xf5, 0x3a, 0xa7, 0x73, 0xa9, 0xa8, 0x86, 0x62, 0xc7,
720        0x65, 0xc8, 0xc9, 0xa1, 0xf4, 0xd1, 0xe9, 0x5a, 0x92, 0x85, 0xa6, 0xe7, 0x93, 0xe8, 0xc1, 0xc6, 0x7a, 0x64,
721        0xe1, 0x4a, 0x6a, 0xe6, 0xb3, 0xf1, 0xd3, 0xa5, 0x8a, 0xb2, 0x9a, 0xba, 0x84, 0xa4, 0x63, 0xe5, 0xc5, 0xf3,
722        0xd2, 0xc4, 0x82, 0xaa, 0xda, 0xe4, 0xf2, 0xca, 0x83, 0xa3, 0xa2, 0xc3, 0xea, 0xc2, 0xe2, 0xe3, 0xff, 0xff},
723       {0,    2,    2,    1,    4,    1,    4,    1,    3,    3,    1,    0,    0,    0,    0,    140,  0x02, 0x03,
724        0x01, 0x04, 0x05, 0x12, 0x11, 0x06, 0x13, 0x07, 0x08, 0x14, 0x22, 0x09, 0x21, 0x00, 0x23, 0x15, 0x31, 0x32,
725        0x0a, 0x16, 0xf0, 0x24, 0x33, 0x41, 0x42, 0x19, 0x17, 0x25, 0x18, 0x51, 0x34, 0x43, 0x52, 0x29, 0x35, 0x61,
726        0x39, 0x71, 0x62, 0x36, 0x53, 0x26, 0x38, 0x1a, 0x37, 0x81, 0x27, 0x91, 0x79, 0x55, 0x45, 0x28, 0x72, 0x59,
727        0xa1, 0xb1, 0x44, 0x69, 0x54, 0x58, 0xd1, 0xfa, 0x57, 0xe1, 0xf1, 0xb9, 0x49, 0x47, 0x63, 0x6a, 0xf9, 0x56,
728        0x46, 0xa8, 0x2a, 0x4a, 0x78, 0x99, 0x3a, 0x75, 0x74, 0x86, 0x65, 0xc1, 0x76, 0xb6, 0x96, 0xd6, 0x89, 0x85,
729        0xc9, 0xf5, 0x95, 0xb4, 0xc7, 0xf7, 0x8a, 0x97, 0xb8, 0x73, 0xb7, 0xd8, 0xd9, 0x87, 0xa7, 0x7a, 0x48, 0x82,
730        0x84, 0xea, 0xf4, 0xa6, 0xc5, 0x5a, 0x94, 0xa4, 0xc6, 0x92, 0xc3, 0x68, 0xb5, 0xc8, 0xe4, 0xe5, 0xe6, 0xe9,
731        0xa2, 0xa3, 0xe3, 0xc2, 0x66, 0x67, 0x93, 0xaa, 0xd4, 0xd5, 0xe7, 0xf8, 0x88, 0x9a, 0xd7, 0x77, 0xc4, 0x64,
732        0xe2, 0x98, 0xa5, 0xca, 0xda, 0xe8, 0xf3, 0xf6, 0xa9, 0xb2, 0xb3, 0xf2, 0xd2, 0x83, 0xba, 0xd3, 0xff, 0xff},
733       {0,    0,    6,    2,    1,    3,    3,    2,    5,    1,    2,    2,    8,    10,   0,    117,  0x04, 0x05,
734        0x03, 0x06, 0x02, 0x07, 0x01, 0x08, 0x09, 0x12, 0x13, 0x14, 0x11, 0x15, 0x0a, 0x16, 0x17, 0xf0, 0x00, 0x22,
735        0x21, 0x18, 0x23, 0x19, 0x24, 0x32, 0x31, 0x25, 0x33, 0x38, 0x37, 0x34, 0x35, 0x36, 0x39, 0x79, 0x57, 0x58,
736        0x59, 0x28, 0x56, 0x78, 0x27, 0x41, 0x29, 0x77, 0x26, 0x42, 0x76, 0x99, 0x1a, 0x55, 0x98, 0x97, 0xf9, 0x48,
737        0x54, 0x96, 0x89, 0x47, 0xb7, 0x49, 0xfa, 0x75, 0x68, 0xb6, 0x67, 0x69, 0xb9, 0xb8, 0xd8, 0x52, 0xd7, 0x88,
738        0xb5, 0x74, 0x51, 0x46, 0xd9, 0xf8, 0x3a, 0xd6, 0x87, 0x45, 0x7a, 0x95, 0xd5, 0xf6, 0x86, 0xb4, 0xa9, 0x94,
739        0x53, 0x2a, 0xa8, 0x43, 0xf5, 0xf7, 0xd4, 0x66, 0xa7, 0x5a, 0x44, 0x8a, 0xc9, 0xe8, 0xc8, 0xe7, 0x9a, 0x6a,
740        0x73, 0x4a, 0x61, 0xc7, 0xf4, 0xc6, 0x65, 0xe9, 0x72, 0xe6, 0x71, 0x91, 0x93, 0xa6, 0xda, 0x92, 0x85, 0x62,
741        0xf3, 0xc5, 0xb2, 0xa4, 0x84, 0xba, 0x64, 0xa5, 0xb3, 0xd2, 0x81, 0xe5, 0xd3, 0xaa, 0xc4, 0xca, 0xf2, 0xb1,
742        0xe4, 0xd1, 0x83, 0x63, 0xea, 0xc3, 0xe2, 0x82, 0xf1, 0xa3, 0xc2, 0xa1, 0xc1, 0xe3, 0xa2, 0xe1, 0xff, 0xff}};
743   if (table > 2)
744     table = 2;
745   huff[0] = make_decoder(first_tree[table]);
746   huff[1] = make_decoder(second_tree[table]);
747 }
748 
749 /*
750    Return 0 if the image starts with compressed data,
751    1 if it starts with uncompressed low-order bits.
752 
753    In Canon compressed data, 0xff is always followed by 0x00.
754  */
canon_has_lowbits()755 int CLASS canon_has_lowbits()
756 {
757   uchar test[0x4000];
758   int ret = 1, i;
759 
760   fseek(ifp, 0, SEEK_SET);
761   fread(test, 1, sizeof test, ifp);
762   for (i = 540; i < sizeof test - 1; i++)
763     if (test[i] == 0xff)
764     {
765       if (test[i + 1])
766         return 1;
767       ret = 0;
768     }
769   return ret;
770 }
771 
canon_load_raw()772 void CLASS canon_load_raw()
773 {
774   ushort *pixel, *prow, *huff[2];
775   int nblocks, lowbits, i, c, row, r, save, val;
776   int block, diffbuf[64], leaf, len, diff, carry = 0, pnum = 0, base[2];
777 
778   crw_init_tables(tiff_compress, huff);
779   lowbits = canon_has_lowbits();
780   if (!lowbits)
781     maximum = 0x3ff;
782   fseek(ifp, 540 + lowbits * raw_height * raw_width / 4, SEEK_SET);
783   zero_after_ff = 1;
784   getbits(-1);
785 #ifdef LIBRAW_LIBRARY_BUILD
786   try
787   {
788 #endif
789     for (row = 0; row < raw_height; row += 8)
790     {
791 #ifdef LIBRAW_LIBRARY_BUILD
792       checkCancel();
793 #endif
794       pixel = raw_image + row * raw_width;
795       nblocks = MIN(8, raw_height - row) * raw_width >> 6;
796       for (block = 0; block < nblocks; block++)
797       {
798         memset(diffbuf, 0, sizeof diffbuf);
799         for (i = 0; i < 64; i++)
800         {
801           leaf = gethuff(huff[i > 0]);
802           if (leaf == 0 && i)
803             break;
804           if (leaf == 0xff)
805             continue;
806           i += leaf >> 4;
807           len = leaf & 15;
808           if (len == 0)
809             continue;
810           diff = getbits(len);
811           if ((diff & (1 << (len - 1))) == 0)
812             diff -= (1 << len) - 1;
813           if (i < 64)
814             diffbuf[i] = diff;
815         }
816         diffbuf[0] += carry;
817         carry = diffbuf[0];
818         for (i = 0; i < 64; i++)
819         {
820           if (pnum++ % raw_width == 0)
821             base[0] = base[1] = 512;
822           if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
823             derror();
824         }
825       }
826       if (lowbits)
827       {
828         save = ftell(ifp);
829         fseek(ifp, 26 + row * raw_width / 4, SEEK_SET);
830         for (prow = pixel, i = 0; i < raw_width * 2; i++)
831         {
832           c = fgetc(ifp);
833           for (r = 0; r < 8; r += 2, prow++)
834           {
835             val = (*prow << 2) + ((c >> r) & 3);
836             if (raw_width == 2672 && val < 512)
837               val += 2;
838             *prow = val;
839           }
840         }
841         fseek(ifp, save, SEEK_SET);
842       }
843     }
844 #ifdef LIBRAW_LIBRARY_BUILD
845   }
846   catch (...)
847   {
848     FORC(2) free(huff[c]);
849     throw;
850   }
851 #endif
852   FORC(2) free(huff[c]);
853 }
854 
ljpeg_start(struct jhead * jh,int info_only)855 int CLASS ljpeg_start(struct jhead *jh, int info_only)
856 {
857   ushort c, tag, len;
858   int cnt = 0;
859   uchar data[0x10000];
860   const uchar *dp;
861 
862   memset(jh, 0, sizeof *jh);
863   jh->restart = INT_MAX;
864   if ((fgetc(ifp), fgetc(ifp)) != 0xd8)
865     return 0;
866   do
867   {
868     if (feof(ifp))
869       return 0;
870     if (cnt++ > 1024)
871       return 0; // 1024 tags limit
872     if (!fread(data, 2, 2, ifp))
873       return 0;
874     tag = data[0] << 8 | data[1];
875     len = (data[2] << 8 | data[3]) - 2;
876     if (tag <= 0xff00)
877       return 0;
878     fread(data, 1, len, ifp);
879     switch (tag)
880     {
881     case 0xffc3: // start of frame; lossless, Huffman
882       jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
883     case 0xffc1:
884     case 0xffc0:
885       jh->algo = tag & 0xff;
886       jh->bits = data[0];
887       jh->high = data[1] << 8 | data[2];
888       jh->wide = data[3] << 8 | data[4];
889       jh->clrs = data[5] + jh->sraw;
890       if (len == 9 && !dng_version)
891         getc(ifp);
892       break;
893     case 0xffc4: // define Huffman tables
894       if (info_only)
895         break;
896       for (dp = data; dp < data + len && !((c = *dp++) & -20);)
897         jh->free[c] = jh->huff[c] = make_decoder_ref(&dp);
898       break;
899     case 0xffda: // start of scan
900       jh->psv = data[1 + data[0] * 2];
901       jh->bits -= data[3 + data[0] * 2] & 15;
902       break;
903     case 0xffdb:
904       FORC(64) jh->quant[c] = data[c * 2 + 1] << 8 | data[c * 2 + 2];
905       break;
906     case 0xffdd:
907       jh->restart = data[0] << 8 | data[1];
908     }
909   } while (tag != 0xffda);
910   if (jh->bits > 16 || jh->clrs > 6 || !jh->bits || !jh->high || !jh->wide || !jh->clrs)
911     return 0;
912   if (info_only)
913     return 1;
914   if (!jh->huff[0])
915     return 0;
916   FORC(19) if (!jh->huff[c + 1]) jh->huff[c + 1] = jh->huff[c];
917   if (jh->sraw)
918   {
919     FORC(4) jh->huff[2 + c] = jh->huff[1];
920     FORC(jh->sraw) jh->huff[1 + c] = jh->huff[0];
921   }
922   jh->row = (ushort *)calloc(jh->wide * jh->clrs, 4);
923   merror(jh->row, "ljpeg_start()");
924   return zero_after_ff = 1;
925 }
926 
ljpeg_end(struct jhead * jh)927 void CLASS ljpeg_end(struct jhead *jh)
928 {
929   int c;
930   FORC4 if (jh->free[c]) free(jh->free[c]);
931   free(jh->row);
932 }
933 
ljpeg_diff(ushort * huff)934 int CLASS ljpeg_diff(ushort *huff)
935 {
936   int len, diff;
937   if (!huff)
938 #ifdef LIBRAW_LIBRARY_BUILD
939     throw LIBRAW_EXCEPTION_IO_CORRUPT;
940 #else
941     longjmp(failure, 2);
942 #endif
943 
944   len = gethuff(huff);
945   if (len == 16 && (!dng_version || dng_version >= 0x1010000))
946     return -32768;
947   diff = getbits(len);
948   if ((diff & (1 << (len - 1))) == 0)
949     diff -= (1 << len) - 1;
950   return diff;
951 }
952 
ljpeg_row(int jrow,struct jhead * jh)953 ushort *CLASS ljpeg_row(int jrow, struct jhead *jh)
954 {
955   int col, c, diff, pred, spred = 0;
956   ushort mark = 0, *row[3];
957 
958   if (jrow * jh->wide % jh->restart == 0)
959   {
960     FORC(6) jh->vpred[c] = 1 << (jh->bits - 1);
961     if (jrow)
962     {
963       fseek(ifp, -2, SEEK_CUR);
964       do
965         mark = (mark << 8) + (c = fgetc(ifp));
966       while (c != EOF && mark >> 4 != 0xffd);
967     }
968     getbits(-1);
969   }
970   FORC3 row[c] = jh->row + jh->wide * jh->clrs * ((jrow + c) & 1);
971   for (col = 0; col < jh->wide; col++)
972     FORC(jh->clrs)
973     {
974       diff = ljpeg_diff(jh->huff[c]);
975       if (jh->sraw && c <= jh->sraw && (col | c))
976         pred = spred;
977       else if (col)
978         pred = row[0][-jh->clrs];
979       else
980         pred = (jh->vpred[c] += diff) - diff;
981       if (jrow && col)
982         switch (jh->psv)
983         {
984         case 1:
985           break;
986         case 2:
987           pred = row[1][0];
988           break;
989         case 3:
990           pred = row[1][-jh->clrs];
991           break;
992         case 4:
993           pred = pred + row[1][0] - row[1][-jh->clrs];
994           break;
995         case 5:
996           pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1);
997           break;
998         case 6:
999           pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1);
1000           break;
1001         case 7:
1002           pred = (pred + row[1][0]) >> 1;
1003           break;
1004         default:
1005           pred = 0;
1006         }
1007       if ((**row = pred + diff) >> jh->bits)
1008         derror();
1009       if (c <= jh->sraw)
1010         spred = **row;
1011       row[0]++;
1012       row[1]++;
1013     }
1014   return row[2];
1015 }
1016 
lossless_jpeg_load_raw()1017 void CLASS lossless_jpeg_load_raw()
1018 {
1019   int jwide, jhigh, jrow, jcol, val, jidx, i, j, row = 0, col = 0;
1020   struct jhead jh;
1021   ushort *rp;
1022 
1023   if (!ljpeg_start(&jh, 0))
1024     return;
1025 
1026   if (jh.wide < 1 || jh.high < 1 || jh.clrs < 1 || jh.bits < 1)
1027 #ifdef LIBRAW_LIBRARY_BUILD
1028     throw LIBRAW_EXCEPTION_IO_CORRUPT;
1029 #else
1030     longjmp(failure, 2);
1031 #endif
1032   jwide = jh.wide * jh.clrs;
1033   jhigh = jh.high;
1034   if (jh.clrs == 4 && jwide >= raw_width * 2)
1035     jhigh *= 2;
1036 
1037 #ifdef LIBRAW_LIBRARY_BUILD
1038   try
1039   {
1040 #endif
1041     for (jrow = 0; jrow < jh.high; jrow++)
1042     {
1043 #ifdef LIBRAW_LIBRARY_BUILD
1044       checkCancel();
1045 #endif
1046       rp = ljpeg_row(jrow, &jh);
1047       if (load_flags & 1)
1048         row = jrow & 1 ? height - 1 - jrow / 2 : jrow / 2;
1049       for (jcol = 0; jcol < jwide; jcol++)
1050       {
1051         val = curve[*rp++];
1052         if (cr2_slice[0])
1053         {
1054           jidx = jrow * jwide + jcol;
1055           i = jidx / (cr2_slice[1] * raw_height);
1056           if ((j = i >= cr2_slice[0]))
1057             i = cr2_slice[0];
1058           jidx -= i * (cr2_slice[1] * raw_height);
1059           row = jidx / cr2_slice[1 + j];
1060           col = jidx % cr2_slice[1 + j] + i * cr2_slice[1];
1061         }
1062         if (raw_width == 3984 && (col -= 2) < 0)
1063           col += (row--, raw_width);
1064         if (row > raw_height)
1065 #ifdef LIBRAW_LIBRARY_BUILD
1066           throw LIBRAW_EXCEPTION_IO_CORRUPT;
1067 #else
1068         longjmp(failure, 3);
1069 #endif
1070         if ((unsigned)row < raw_height)
1071           RAW(row, col) = val;
1072         if (++col >= raw_width)
1073           col = (row++, 0);
1074       }
1075     }
1076 #ifdef LIBRAW_LIBRARY_BUILD
1077   }
1078   catch (...)
1079   {
1080     ljpeg_end(&jh);
1081     throw;
1082   }
1083 #endif
1084   ljpeg_end(&jh);
1085 }
1086 
canon_sraw_load_raw()1087 void CLASS canon_sraw_load_raw()
1088 {
1089   struct jhead jh;
1090   short *rp = 0, (*ip)[4];
1091   int jwide, slice, scol, ecol, row, col, jrow = 0, jcol = 0, pix[3], c;
1092   int v[3] = {0, 0, 0}, ver, hue;
1093 #ifdef LIBRAW_LIBRARY_BUILD
1094   int saved_w = width, saved_h = height;
1095 #endif
1096   char *cp;
1097 
1098   if (!ljpeg_start(&jh, 0) || jh.clrs < 4)
1099     return;
1100   jwide = (jh.wide >>= 1) * jh.clrs;
1101 
1102 #ifdef LIBRAW_LIBRARY_BUILD
1103   if (load_flags & 256)
1104   {
1105     width = raw_width;
1106     height = raw_height;
1107   }
1108 
1109   try
1110   {
1111 #endif
1112     for (ecol = slice = 0; slice <= cr2_slice[0]; slice++)
1113     {
1114       scol = ecol;
1115       ecol += cr2_slice[1] * 2 / jh.clrs;
1116       if (!cr2_slice[0] || ecol > raw_width - 1)
1117         ecol = raw_width & -2;
1118       for (row = 0; row < height; row += (jh.clrs >> 1) - 1)
1119       {
1120 #ifdef LIBRAW_LIBRARY_BUILD
1121         checkCancel();
1122 #endif
1123         ip = (short(*)[4])image + row * width;
1124         for (col = scol; col < ecol; col += 2, jcol += jh.clrs)
1125         {
1126           if ((jcol %= jwide) == 0)
1127             rp = (short *)ljpeg_row(jrow++, &jh);
1128           if (col >= width)
1129             continue;
1130 #ifdef LIBRAW_LIBRARY_BUILD
1131           if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SRAW_NO_INTERPOLATE)
1132           {
1133             FORC(jh.clrs - 2)
1134             {
1135               ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c];
1136               ip[col + (c >> 1) * width + (c & 1)][1] = ip[col + (c >> 1) * width + (c & 1)][2] = 8192;
1137             }
1138             ip[col][1] = rp[jcol + jh.clrs - 2] - 8192;
1139             ip[col][2] = rp[jcol + jh.clrs - 1] - 8192;
1140           }
1141           else if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SRAW_NO_RGB)
1142           {
1143             FORC(jh.clrs - 2)
1144             ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c];
1145             ip[col][1] = rp[jcol + jh.clrs - 2] - 8192;
1146             ip[col][2] = rp[jcol + jh.clrs - 1] - 8192;
1147           }
1148           else
1149 #endif
1150           {
1151             FORC(jh.clrs - 2)
1152             ip[col + (c >> 1) * width + (c & 1)][0] = rp[jcol + c];
1153             ip[col][1] = rp[jcol + jh.clrs - 2] - 16384;
1154             ip[col][2] = rp[jcol + jh.clrs - 1] - 16384;
1155           }
1156         }
1157       }
1158     }
1159 #ifdef LIBRAW_LIBRARY_BUILD
1160   }
1161   catch (...)
1162   {
1163     ljpeg_end(&jh);
1164     throw;
1165   }
1166 #endif
1167 
1168 #ifdef LIBRAW_LIBRARY_BUILD
1169   if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SRAW_NO_INTERPOLATE)
1170   {
1171     ljpeg_end(&jh);
1172     maximum = 0x3fff;
1173     height = saved_h;
1174     width = saved_w;
1175     return;
1176   }
1177 #endif
1178 
1179 #ifdef LIBRAW_LIBRARY_BUILD
1180   try
1181   {
1182 #endif
1183     for (cp = model2; *cp && !isdigit(*cp); cp++)
1184       ;
1185     sscanf(cp, "%d.%d.%d", v, v + 1, v + 2);
1186     ver = (v[0] * 1000 + v[1]) * 1000 + v[2];
1187     hue = (jh.sraw + 1) << 2;
1188     if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
1189       hue = jh.sraw << 1;
1190     ip = (short(*)[4])image;
1191     rp = ip[0];
1192     for (row = 0; row < height; row++, ip += width)
1193     {
1194 #ifdef LIBRAW_LIBRARY_BUILD
1195       checkCancel();
1196 #endif
1197       if (row & (jh.sraw >> 1))
1198       {
1199         for (col = 0; col < width; col += 2)
1200           for (c = 1; c < 3; c++)
1201             if (row == height - 1)
1202             {
1203               ip[col][c] = ip[col - width][c];
1204             }
1205             else
1206             {
1207               ip[col][c] = (ip[col - width][c] + ip[col + width][c] + 1) >> 1;
1208             }
1209       }
1210       for (col = 1; col < width; col += 2)
1211         for (c = 1; c < 3; c++)
1212           if (col == width - 1)
1213             ip[col][c] = ip[col - 1][c];
1214           else
1215             ip[col][c] = (ip[col - 1][c] + ip[col + 1][c] + 1) >> 1;
1216     }
1217 #ifdef LIBRAW_LIBRARY_BUILD
1218     if (!(imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SRAW_NO_RGB))
1219 #endif
1220       for (; rp < ip[0]; rp += 4)
1221       {
1222 #ifdef LIBRAW_LIBRARY_BUILD
1223         checkCancel();
1224 #endif
1225         if (unique_id == 0x80000218 || unique_id == 0x80000250 || unique_id == 0x80000261 || unique_id == 0x80000281 ||
1226             unique_id == 0x80000287)
1227         {
1228           rp[1] = (rp[1] << 2) + hue;
1229           rp[2] = (rp[2] << 2) + hue;
1230           pix[0] = rp[0] + ((50 * rp[1] + 22929 * rp[2]) >> 14);
1231           pix[1] = rp[0] + ((-5640 * rp[1] - 11751 * rp[2]) >> 14);
1232           pix[2] = rp[0] + ((29040 * rp[1] - 101 * rp[2]) >> 14);
1233         }
1234         else
1235         {
1236           if (unique_id < 0x80000218)
1237             rp[0] -= 512;
1238           pix[0] = rp[0] + rp[2];
1239           pix[2] = rp[0] + rp[1];
1240           pix[1] = rp[0] + ((-778 * rp[1] - (rp[2] << 11)) >> 12);
1241         }
1242         FORC3 rp[c] = CLIP15(pix[c] * sraw_mul[c] >> 10);
1243       }
1244 #ifdef LIBRAW_LIBRARY_BUILD
1245   }
1246   catch (...)
1247   {
1248     ljpeg_end(&jh);
1249     throw;
1250   }
1251   height = saved_h;
1252   width = saved_w;
1253 #endif
1254   ljpeg_end(&jh);
1255   maximum = 0x3fff;
1256 }
1257 
adobe_copy_pixel(unsigned row,unsigned col,ushort ** rp)1258 void CLASS adobe_copy_pixel(unsigned row, unsigned col, ushort **rp)
1259 {
1260   int c;
1261 
1262   if (tiff_samples == 2 && shot_select)
1263     (*rp)++;
1264   if (raw_image)
1265   {
1266     if (row < raw_height && col < raw_width)
1267       RAW(row, col) = curve[**rp];
1268     *rp += tiff_samples;
1269   }
1270   else
1271   {
1272 #ifdef LIBRAW_LIBRARY_BUILD
1273     if (row < raw_height && col < raw_width)
1274       FORC(tiff_samples)
1275     image[row * raw_width + col][c] = curve[(*rp)[c]];
1276     *rp += tiff_samples;
1277 #else
1278     if (row < height && col < width)
1279       FORC(tiff_samples)
1280     image[row * width + col][c] = curve[(*rp)[c]];
1281     *rp += tiff_samples;
1282 #endif
1283   }
1284   if (tiff_samples == 2 && shot_select)
1285     (*rp)--;
1286 }
1287 
ljpeg_idct(struct jhead * jh)1288 void CLASS ljpeg_idct(struct jhead *jh)
1289 {
1290   int c, i, j, len, skip, coef;
1291   float work[3][8][8];
1292   static float cs[106] = {0};
1293   static const uchar zigzag[80] = {0,  1,  8,  16, 9,  2,  3,  10, 17, 24, 32, 25, 18, 11, 4,  5,  12, 19, 26, 33,
1294                                    40, 48, 41, 34, 27, 20, 13, 6,  7,  14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36,
1295                                    29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54,
1296                                    47, 55, 62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63};
1297 
1298   if (!cs[0])
1299     FORC(106) cs[c] = cos((c & 31) * M_PI / 16) / 2;
1300   memset(work, 0, sizeof work);
1301   work[0][0][0] = jh->vpred[0] += ljpeg_diff(jh->huff[0]) * jh->quant[0];
1302   for (i = 1; i < 64; i++)
1303   {
1304     len = gethuff(jh->huff[16]);
1305     i += skip = len >> 4;
1306     if (!(len &= 15) && skip < 15)
1307       break;
1308     coef = getbits(len);
1309     if ((coef & (1 << (len - 1))) == 0)
1310       coef -= (1 << len) - 1;
1311     ((float *)work)[zigzag[i]] = coef * jh->quant[i];
1312   }
1313   FORC(8) work[0][0][c] *= M_SQRT1_2;
1314   FORC(8) work[0][c][0] *= M_SQRT1_2;
1315   for (i = 0; i < 8; i++)
1316     for (j = 0; j < 8; j++)
1317       FORC(8) work[1][i][j] += work[0][i][c] * cs[(j * 2 + 1) * c];
1318   for (i = 0; i < 8; i++)
1319     for (j = 0; j < 8; j++)
1320       FORC(8) work[2][i][j] += work[1][c][j] * cs[(i * 2 + 1) * c];
1321 
1322   FORC(64) jh->idct[c] = CLIP(((float *)work[2])[c] + 0.5);
1323 }
1324 
lossless_dng_load_raw()1325 void CLASS lossless_dng_load_raw()
1326 {
1327   unsigned save, trow = 0, tcol = 0, jwide, jrow, jcol, row, col, i, j;
1328   struct jhead jh;
1329   ushort *rp;
1330 
1331   while (trow < raw_height)
1332   {
1333 #ifdef LIBRAW_LIBRARY_BUILD
1334     checkCancel();
1335 #endif
1336     save = ftell(ifp);
1337     if (tile_length < INT_MAX)
1338       fseek(ifp, get4(), SEEK_SET);
1339     if (!ljpeg_start(&jh, 0))
1340       break;
1341     jwide = jh.wide;
1342     if (filters)
1343       jwide *= jh.clrs;
1344     jwide /= MIN(is_raw, tiff_samples);
1345 #ifdef LIBRAW_LIBRARY_BUILD
1346     try
1347     {
1348 #endif
1349       switch (jh.algo)
1350       {
1351       case 0xc1:
1352         jh.vpred[0] = 16384;
1353         getbits(-1);
1354         for (jrow = 0; jrow + 7 < jh.high; jrow += 8)
1355         {
1356 #ifdef LIBRAW_LIBRARY_BUILD
1357           checkCancel();
1358 #endif
1359           for (jcol = 0; jcol + 7 < jh.wide; jcol += 8)
1360           {
1361             ljpeg_idct(&jh);
1362             rp = jh.idct;
1363             row = trow + jcol / tile_width + jrow * 2;
1364             col = tcol + jcol % tile_width;
1365             for (i = 0; i < 16; i += 2)
1366               for (j = 0; j < 8; j++)
1367                 adobe_copy_pixel(row + i, col + j, &rp);
1368           }
1369         }
1370         break;
1371       case 0xc3:
1372         for (row = col = jrow = 0; jrow < jh.high; jrow++)
1373         {
1374 #ifdef LIBRAW_LIBRARY_BUILD
1375           checkCancel();
1376 #endif
1377           rp = ljpeg_row(jrow, &jh);
1378 	  if(tiff_samples == 1 && jh.clrs > 1 && jh.clrs*jwide == raw_width)
1379           for (jcol = 0; jcol < jwide*jh.clrs; jcol++)
1380           {
1381             adobe_copy_pixel(trow + row, tcol + col, &rp);
1382             if (++col >= tile_width || col >= raw_width)
1383               row += 1 + (col = 0);
1384 	  }
1385 	  else
1386           for (jcol = 0; jcol < jwide; jcol++)
1387           {
1388             adobe_copy_pixel(trow + row, tcol + col, &rp);
1389             if (++col >= tile_width || col >= raw_width)
1390               row += 1 + (col = 0);
1391           }
1392         }
1393       }
1394 #ifdef LIBRAW_LIBRARY_BUILD
1395     }
1396     catch (...)
1397     {
1398       ljpeg_end(&jh);
1399       throw;
1400     }
1401 #endif
1402     fseek(ifp, save + 4, SEEK_SET);
1403     if ((tcol += tile_width) >= raw_width)
1404       trow += tile_length + (tcol = 0);
1405     ljpeg_end(&jh);
1406   }
1407 }
1408 
packed_dng_load_raw()1409 void CLASS packed_dng_load_raw()
1410 {
1411   ushort *pixel, *rp;
1412   int row, col;
1413 
1414   pixel = (ushort *)calloc(raw_width, tiff_samples * sizeof *pixel);
1415   merror(pixel, "packed_dng_load_raw()");
1416 #ifdef LIBRAW_LIBRARY_BUILD
1417   try
1418   {
1419 #endif
1420     for (row = 0; row < raw_height; row++)
1421     {
1422 #ifdef LIBRAW_LIBRARY_BUILD
1423       checkCancel();
1424 #endif
1425       if (tiff_bps == 16)
1426         read_shorts(pixel, raw_width * tiff_samples);
1427       else
1428       {
1429         getbits(-1);
1430         for (col = 0; col < raw_width * tiff_samples; col++)
1431           pixel[col] = getbits(tiff_bps);
1432       }
1433       for (rp = pixel, col = 0; col < raw_width; col++)
1434         adobe_copy_pixel(row, col, &rp);
1435     }
1436 #ifdef LIBRAW_LIBRARY_BUILD
1437   }
1438   catch (...)
1439   {
1440     free(pixel);
1441     throw;
1442   }
1443 #endif
1444   free(pixel);
1445 }
1446 
pentax_load_raw()1447 void CLASS pentax_load_raw()
1448 {
1449   ushort bit[2][15], huff[4097];
1450   int dep, row, col, diff, c, i;
1451   ushort vpred[2][2] = {{0, 0}, {0, 0}}, hpred[2];
1452 
1453   fseek(ifp, meta_offset, SEEK_SET);
1454   dep = (get2() + 12) & 15;
1455   fseek(ifp, 12, SEEK_CUR);
1456   FORC(dep) bit[0][c] = get2();
1457   FORC(dep) bit[1][c] = fgetc(ifp);
1458   FORC(dep)
1459   for (i = bit[0][c]; i <= ((bit[0][c] + (4096 >> bit[1][c]) - 1) & 4095);)
1460     huff[++i] = bit[1][c] << 8 | c;
1461   huff[0] = 12;
1462   fseek(ifp, data_offset, SEEK_SET);
1463   getbits(-1);
1464   for (row = 0; row < raw_height; row++)
1465   {
1466 #ifdef LIBRAW_LIBRARY_BUILD
1467     checkCancel();
1468 #endif
1469     for (col = 0; col < raw_width; col++)
1470     {
1471       diff = ljpeg_diff(huff);
1472       if (col < 2)
1473         hpred[col] = vpred[row & 1][col] += diff;
1474       else
1475         hpred[col & 1] += diff;
1476       RAW(row, col) = hpred[col & 1];
1477       if (hpred[col & 1] >> tiff_bps)
1478         derror();
1479     }
1480   }
1481 }
1482 
1483 #ifdef LIBRAW_LIBRARY_BUILD
1484 
nikon_coolscan_load_raw()1485 void CLASS nikon_coolscan_load_raw()
1486 {
1487   if(!image)
1488     throw LIBRAW_EXCEPTION_IO_CORRUPT;
1489 
1490   int bypp = tiff_bps <= 8 ? 1 : 2;
1491   int bufsize = width * 3 * bypp;
1492 
1493   if (tiff_bps <= 8)
1494     gamma_curve(1.0 / imgdata.params.coolscan_nef_gamma, 0., 1, 255);
1495   else
1496     gamma_curve(1.0 / imgdata.params.coolscan_nef_gamma, 0., 1, 65535);
1497   fseek(ifp, data_offset, SEEK_SET);
1498   unsigned char *buf = (unsigned char *)malloc(bufsize);
1499   unsigned short *ubuf = (unsigned short *)buf;
1500   for (int row = 0; row < raw_height; row++)
1501   {
1502     int red = fread(buf, 1, bufsize, ifp);
1503     unsigned short(*ip)[4] = (unsigned short(*)[4])image + row * width;
1504     if (tiff_bps <= 8)
1505       for (int col = 0; col < width; col++)
1506       {
1507         ip[col][0] = curve[buf[col * 3]];
1508         ip[col][1] = curve[buf[col * 3 + 1]];
1509         ip[col][2] = curve[buf[col * 3 + 2]];
1510         ip[col][3] = 0;
1511       }
1512     else
1513       for (int col = 0; col < width; col++)
1514       {
1515         ip[col][0] = curve[ubuf[col * 3]];
1516         ip[col][1] = curve[ubuf[col * 3 + 1]];
1517         ip[col][2] = curve[ubuf[col * 3 + 2]];
1518         ip[col][3] = 0;
1519       }
1520   }
1521   free(buf);
1522 }
1523 #endif
1524 
nikon_load_raw()1525 void CLASS nikon_load_raw()
1526 {
1527   static const uchar nikon_tree[][32] = {
1528       {0, 1, 5, 1, 1, 1, 1, 1, 1, 2, 0,  0,  0, 0, 0, 0, /* 12-bit lossy */
1529        5, 4, 3, 6, 2, 7, 1, 0, 8, 9, 11, 10, 12},
1530       {0,    1,    5,    1,    1,    1, 1, 1, 1, 2, 0, 0,  0,  0, 0, 0, /* 12-bit lossy after split */
1531        0x39, 0x5a, 0x38, 0x27, 0x16, 5, 4, 3, 2, 1, 0, 11, 12, 12},
1532       {0, 1, 4, 2, 3, 1, 2, 0, 0, 0, 0,  0,  0, 0, 0, 0, /* 12-bit lossless */
1533        5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, 11, 12},
1534       {0, 1, 4, 3, 1, 1, 1, 1, 1, 2, 0,  0,  0,  0,  0, 0, /* 14-bit lossy */
1535        5, 6, 4, 7, 8, 3, 9, 2, 1, 0, 10, 11, 12, 13, 14},
1536       {0, 1,    5,    1,    1,    1, 1, 1, 1, 1, 2, 0, 0, 0,  0, 0, /* 14-bit lossy after split */
1537        8, 0x5c, 0x4b, 0x3a, 0x29, 7, 6, 5, 4, 3, 2, 1, 0, 13, 14},
1538       {0, 1, 4, 2, 2, 3, 1,  2, 0,  0,  0, 0, 0, 0,  0, 0, /* 14-bit lossless */
1539        7, 6, 8, 5, 9, 4, 10, 3, 11, 12, 2, 0, 1, 13, 14}};
1540   ushort *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
1541   int i, min, max, step = 0, tree = 0, split = 0, row, col, len, shl, diff;
1542 
1543   fseek(ifp, meta_offset, SEEK_SET);
1544   ver0 = fgetc(ifp);
1545   ver1 = fgetc(ifp);
1546   if (ver0 == 0x49 || ver1 == 0x58)
1547     fseek(ifp, 2110, SEEK_CUR);
1548   if (ver0 == 0x46)
1549     tree = 2;
1550   if (tiff_bps == 14)
1551     tree += 3;
1552   read_shorts(vpred[0], 4);
1553   max = 1 << tiff_bps & 0x7fff;
1554   if ((csize = get2()) > 1)
1555     step = max / (csize - 1);
1556   if (ver0 == 0x44 && ver1 == 0x20 && step > 0)
1557   {
1558     for (i = 0; i < csize; i++)
1559       curve[i * step] = get2();
1560     for (i = 0; i < max; i++)
1561       curve[i] = (curve[i - i % step] * (step - i % step) + curve[i - i % step + step] * (i % step)) / step;
1562     fseek(ifp, meta_offset + 562, SEEK_SET);
1563     split = get2();
1564   }
1565   else if (ver0 != 0x46 && csize <= 0x4001)
1566     read_shorts(curve, max = csize);
1567   while (curve[max - 2] == curve[max - 1])
1568     max--;
1569   huff = make_decoder(nikon_tree[tree]);
1570   fseek(ifp, data_offset, SEEK_SET);
1571   getbits(-1);
1572 #ifdef LIBRAW_LIBRARY_BUILD
1573   try
1574   {
1575 #endif
1576     for (min = row = 0; row < height; row++)
1577     {
1578 #ifdef LIBRAW_LIBRARY_BUILD
1579       checkCancel();
1580 #endif
1581       if (split && row == split)
1582       {
1583         free(huff);
1584         huff = make_decoder(nikon_tree[tree + 1]);
1585         max += (min = 16) << 1;
1586       }
1587       for (col = 0; col < raw_width; col++)
1588       {
1589         i = gethuff(huff);
1590         len = i & 15;
1591         shl = i >> 4;
1592         diff = ((getbits(len - shl) << 1) + 1) << shl >> 1;
1593         if ((diff & (1 << (len - 1))) == 0)
1594           diff -= (1 << len) - !shl;
1595         if (col < 2)
1596           hpred[col] = vpred[row & 1][col] += diff;
1597         else
1598           hpred[col & 1] += diff;
1599         if ((ushort)(hpred[col & 1] + min) >= max)
1600           derror();
1601         RAW(row, col) = curve[LIM((short)hpred[col & 1], 0, 0x3fff)];
1602       }
1603     }
1604 #ifdef LIBRAW_LIBRARY_BUILD
1605   }
1606   catch (...)
1607   {
1608     free(huff);
1609     throw;
1610   }
1611 #endif
1612   free(huff);
1613 }
1614 
nikon_yuv_load_raw()1615 void CLASS nikon_yuv_load_raw()
1616 {
1617 #ifdef LIBRAW_LIBRARY_BUILD
1618   if (!image)
1619     throw LIBRAW_EXCEPTION_IO_CORRUPT;
1620 #endif
1621   int row, col, yuv[4], rgb[3], b, c;
1622   UINT64 bitbuf = 0;
1623   float cmul[4];
1624   FORC4 { cmul[c] = cam_mul[c] > 0.001f ? cam_mul[c] : 1.f; }
1625   for (row = 0; row < raw_height; row++)
1626   {
1627 #ifdef LIBRAW_LIBRARY_BUILD
1628     checkCancel();
1629 #endif
1630 
1631     for (col = 0; col < raw_width; col++)
1632     {
1633       if (!(b = col & 1))
1634       {
1635         bitbuf = 0;
1636         FORC(6) bitbuf |= (UINT64)fgetc(ifp) << c * 8;
1637         FORC(4) yuv[c] = (bitbuf >> c * 12 & 0xfff) - (c >> 1 << 11);
1638       }
1639       rgb[0] = yuv[b] + 1.370705 * yuv[3];
1640       rgb[1] = yuv[b] - 0.337633 * yuv[2] - 0.698001 * yuv[3];
1641       rgb[2] = yuv[b] + 1.732446 * yuv[2];
1642       FORC3 image[row * width + col][c] = curve[LIM(rgb[c], 0, 0xfff)] / cmul[c];
1643     }
1644   }
1645 }
1646 
1647 /*
1648    Returns 1 for a Coolpix 995, 0 for anything else.
1649  */
nikon_e995()1650 int CLASS nikon_e995()
1651 {
1652   int i, histo[256];
1653   const uchar often[] = {0x00, 0x55, 0xaa, 0xff};
1654 
1655   memset(histo, 0, sizeof histo);
1656   fseek(ifp, -2000, SEEK_END);
1657   for (i = 0; i < 2000; i++)
1658     histo[fgetc(ifp)]++;
1659   for (i = 0; i < 4; i++)
1660     if (histo[often[i]] < 200)
1661       return 0;
1662   return 1;
1663 }
1664 
1665 /*
1666    Returns 1 for a Coolpix 2100, 0 for anything else.
1667  */
nikon_e2100()1668 int CLASS nikon_e2100()
1669 {
1670   uchar t[12];
1671   int i;
1672 
1673   fseek(ifp, 0, SEEK_SET);
1674   for (i = 0; i < 1024; i++)
1675   {
1676     fread(t, 1, 12, ifp);
1677     if (((t[2] & t[4] & t[7] & t[9]) >> 4 & t[1] & t[6] & t[8] & t[11] & 3) != 3)
1678       return 0;
1679   }
1680   return 1;
1681 }
1682 
nikon_3700()1683 void CLASS nikon_3700()
1684 {
1685   int bits, i;
1686   uchar dp[24];
1687   static const struct
1688   {
1689     int bits;
1690     char t_make[12], t_model[15];
1691   } table[] = {
1692       {0x00, "Pentax", "Optio 33WR"}, {0x03, "Nikon", "E3200"}, {0x32, "Nikon", "E3700"}, {0x33, "Olympus", "C740UZ"}};
1693 
1694   fseek(ifp, 3072, SEEK_SET);
1695   fread(dp, 1, 24, ifp);
1696   bits = (dp[8] & 3) << 4 | (dp[20] & 3);
1697   for (i = 0; i < sizeof table / sizeof *table; i++)
1698     if (bits == table[i].bits)
1699     {
1700       strcpy(make, table[i].t_make);
1701       strcpy(model, table[i].t_model);
1702     }
1703 }
1704 
1705 /*
1706    Separates a Minolta DiMAGE Z2 from a Nikon E4300.
1707  */
minolta_z2()1708 int CLASS minolta_z2()
1709 {
1710   int i, nz;
1711   char tail[424];
1712 
1713   fseek(ifp, -sizeof tail, SEEK_END);
1714   fread(tail, 1, sizeof tail, ifp);
1715   for (nz = i = 0; i < sizeof tail; i++)
1716     if (tail[i])
1717       nz++;
1718   return nz > 20;
1719 }
ppm_thumb()1720 void CLASS ppm_thumb()
1721 {
1722   char *thumb;
1723   thumb_length = thumb_width * thumb_height * 3;
1724   thumb = (char *)malloc(thumb_length);
1725   merror(thumb, "ppm_thumb()");
1726   fprintf(ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1727   fread(thumb, 1, thumb_length, ifp);
1728   fwrite(thumb, 1, thumb_length, ofp);
1729   free(thumb);
1730 }
1731 
ppm16_thumb()1732 void CLASS ppm16_thumb()
1733 {
1734   int i;
1735   char *thumb;
1736   thumb_length = thumb_width * thumb_height * 3;
1737   thumb = (char *)calloc(thumb_length, 2);
1738   merror(thumb, "ppm16_thumb()");
1739   read_shorts((ushort *)thumb, thumb_length);
1740   for (i = 0; i < thumb_length; i++)
1741     thumb[i] = ((ushort *)thumb)[i] >> 8;
1742   fprintf(ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1743   fwrite(thumb, 1, thumb_length, ofp);
1744   free(thumb);
1745 }
1746 
layer_thumb()1747 void CLASS layer_thumb()
1748 {
1749   int i, c;
1750   char *thumb, map[][4] = {"012", "102"};
1751 
1752   colors = thumb_misc >> 5 & 7;
1753   thumb_length = thumb_width * thumb_height;
1754   thumb = (char *)calloc(colors, thumb_length);
1755   merror(thumb, "layer_thumb()");
1756   fprintf(ofp, "P%d\n%d %d\n255\n", 5 + (colors >> 1), thumb_width, thumb_height);
1757   fread(thumb, thumb_length, colors, ifp);
1758   for (i = 0; i < thumb_length; i++)
1759     FORCC putc(thumb[i + thumb_length * (map[thumb_misc >> 8][c] - '0')], ofp);
1760   free(thumb);
1761 }
1762 
rollei_thumb()1763 void CLASS rollei_thumb()
1764 {
1765   unsigned i;
1766   ushort *thumb;
1767 
1768   thumb_length = thumb_width * thumb_height;
1769   thumb = (ushort *)calloc(thumb_length, 2);
1770   merror(thumb, "rollei_thumb()");
1771   fprintf(ofp, "P6\n%d %d\n255\n", thumb_width, thumb_height);
1772   read_shorts(thumb, thumb_length);
1773   for (i = 0; i < thumb_length; i++)
1774   {
1775     putc(thumb[i] << 3, ofp);
1776     putc(thumb[i] >> 5 << 2, ofp);
1777     putc(thumb[i] >> 11 << 3, ofp);
1778   }
1779   free(thumb);
1780 }
1781 
rollei_load_raw()1782 void CLASS rollei_load_raw()
1783 {
1784   uchar pixel[10];
1785   unsigned iten = 0, isix, i, buffer = 0, todo[16];
1786 #ifdef LIBRAW_LIBRARY_BUILD
1787   if(raw_width > 32767 || raw_height > 32767)
1788     throw LIBRAW_EXCEPTION_IO_BADFILE;
1789 #endif
1790   unsigned maxpixel = raw_width*(raw_height+7);
1791 
1792   isix = raw_width * raw_height * 5 / 8;
1793   while (fread(pixel, 1, 10, ifp) == 10)
1794   {
1795 #ifdef LIBRAW_LIBRARY_BUILD
1796     checkCancel();
1797 #endif
1798     for (i = 0; i < 10; i += 2)
1799     {
1800       todo[i] = iten++;
1801       todo[i + 1] = pixel[i] << 8 | pixel[i + 1];
1802       buffer = pixel[i] >> 2 | buffer << 6;
1803     }
1804     for (; i < 16; i += 2)
1805     {
1806       todo[i] = isix++;
1807       todo[i + 1] = buffer >> (14 - i) * 5;
1808     }
1809     for (i = 0; i < 16; i += 2)
1810       if(todo[i] < maxpixel)
1811         raw_image[todo[i]] = (todo[i + 1] & 0x3ff);
1812       else
1813         derror();
1814   }
1815   maximum = 0x3ff;
1816 }
1817 
raw(unsigned row,unsigned col)1818 int CLASS raw(unsigned row, unsigned col) { return (row < raw_height && col < raw_width) ? RAW(row, col) : 0; }
1819 
phase_one_flat_field(int is_float,int nc)1820 void CLASS phase_one_flat_field(int is_float, int nc)
1821 {
1822   ushort head[8];
1823   unsigned wide, high, y, x, c, rend, cend, row, col;
1824   float *mrow, num, mult[4];
1825 
1826   read_shorts(head, 8);
1827   if (head[2] * head[3] * head[4] * head[5] == 0)
1828     return;
1829   wide = head[2] / head[4] + (head[2] % head[4] != 0);
1830   high = head[3] / head[5] + (head[3] % head[5] != 0);
1831   mrow = (float *)calloc(nc * wide, sizeof *mrow);
1832   merror(mrow, "phase_one_flat_field()");
1833   for (y = 0; y < high; y++)
1834   {
1835 #ifdef LIBRAW_LIBRARY_BUILD
1836     checkCancel();
1837 #endif
1838     for (x = 0; x < wide; x++)
1839       for (c = 0; c < nc; c += 2)
1840       {
1841         num = is_float ? getreal(11) : get2() / 32768.0;
1842         if (y == 0)
1843           mrow[c * wide + x] = num;
1844         else
1845           mrow[(c + 1) * wide + x] = (num - mrow[c * wide + x]) / head[5];
1846       }
1847     if (y == 0)
1848       continue;
1849     rend = head[1] + y * head[5];
1850     for (row = rend - head[5]; row < raw_height && row < rend && row < head[1] + head[3] - head[5]; row++)
1851     {
1852       for (x = 1; x < wide; x++)
1853       {
1854         for (c = 0; c < nc; c += 2)
1855         {
1856           mult[c] = mrow[c * wide + x - 1];
1857           mult[c + 1] = (mrow[c * wide + x] - mult[c]) / head[4];
1858         }
1859         cend = head[0] + x * head[4];
1860         for (col = cend - head[4]; col < raw_width && col < cend && col < head[0] + head[2] - head[4]; col++)
1861         {
1862           c = nc > 2 ? FC(row - top_margin, col - left_margin) : 0;
1863           if (!(c & 1))
1864           {
1865             c = RAW(row, col) * mult[c];
1866             RAW(row, col) = LIM(c, 0, 65535);
1867           }
1868           for (c = 0; c < nc; c += 2)
1869             mult[c] += mult[c + 1];
1870         }
1871       }
1872       for (x = 0; x < wide; x++)
1873         for (c = 0; c < nc; c += 2)
1874           mrow[c * wide + x] += mrow[(c + 1) * wide + x];
1875     }
1876   }
1877   free(mrow);
1878 }
1879 
phase_one_correct()1880 int CLASS phase_one_correct()
1881 {
1882   unsigned entries, tag, data, save, col, row, type;
1883   int len, i, j, k, cip, val[4], dev[4], sum, max;
1884   int head[9], diff, mindiff = INT_MAX, off_412 = 0;
1885   /* static */ const signed char dir[12][2] = {{-1, -1}, {-1, 1}, {1, -1},  {1, 1},  {-2, 0}, {0, -2},
1886                                                {0, 2},   {2, 0},  {-2, -2}, {-2, 2}, {2, -2}, {2, 2}};
1887   float poly[8], num, cfrac, frac, mult[2], *yval[2] = {NULL, NULL};
1888   ushort *xval[2];
1889   int qmult_applied = 0, qlin_applied = 0;
1890 
1891 #ifdef LIBRAW_LIBRARY_BUILD
1892   if (!meta_length)
1893 #else
1894   if (half_size || !meta_length)
1895 #endif
1896     return 0;
1897 #ifdef DCRAW_VERBOSE
1898   if (verbose)
1899     fprintf(stderr, _("Phase One correction...\n"));
1900 #endif
1901   fseek(ifp, meta_offset, SEEK_SET);
1902   order = get2();
1903   fseek(ifp, 6, SEEK_CUR);
1904   fseek(ifp, meta_offset + get4(), SEEK_SET);
1905   entries = get4();
1906   get4();
1907 
1908 #ifdef LIBRAW_LIBRARY_BUILD
1909   try
1910   {
1911 #endif
1912     while (entries--)
1913     {
1914 #ifdef LIBRAW_LIBRARY_BUILD
1915       checkCancel();
1916 #endif
1917       tag = get4();
1918       len = get4();
1919       data = get4();
1920       save = ftell(ifp);
1921       fseek(ifp, meta_offset + data, SEEK_SET);
1922       if (tag == 0x419)
1923       { /* Polynomial curve */
1924         for (get4(), i = 0; i < 8; i++)
1925           poly[i] = getreal(11);
1926         poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
1927         for (i = 0; i < 0x10000; i++)
1928         {
1929           num = (poly[5] * i + poly[3]) * i + poly[1];
1930           curve[i] = LIM(num, 0, 65535);
1931         }
1932         goto apply; /* apply to right half */
1933       }
1934       else if (tag == 0x41a)
1935       { /* Polynomial curve */
1936         for (i = 0; i < 4; i++)
1937           poly[i] = getreal(11);
1938         for (i = 0; i < 0x10000; i++)
1939         {
1940           for (num = 0, j = 4; j--;)
1941             num = num * i + poly[j];
1942           curve[i] = LIM(num + i, 0, 65535);
1943         }
1944       apply: /* apply to whole image */
1945         for (row = 0; row < raw_height; row++)
1946         {
1947 #ifdef LIBRAW_LIBRARY_BUILD
1948           checkCancel();
1949 #endif
1950           for (col = (tag & 1) * ph1.split_col; col < raw_width; col++)
1951             RAW(row, col) = curve[RAW(row, col)];
1952         }
1953       }
1954       else if (tag == 0x400)
1955       { /* Sensor defects */
1956         while ((len -= 8) >= 0)
1957         {
1958           col = get2();
1959           row = get2();
1960           type = get2();
1961           get2();
1962           if (col >= raw_width)
1963             continue;
1964           if (type == 131 || type == 137) /* Bad column */
1965             for (row = 0; row < raw_height; row++)
1966               if (FC(row - top_margin, col - left_margin) == 1)
1967               {
1968                 for (sum = i = 0; i < 4; i++)
1969                   sum += val[i] = raw(row + dir[i][0], col + dir[i][1]);
1970                 for (max = i = 0; i < 4; i++)
1971                 {
1972                   dev[i] = abs((val[i] << 2) - sum);
1973                   if (dev[max] < dev[i])
1974                     max = i;
1975                 }
1976                 RAW(row, col) = (sum - val[max]) / 3.0 + 0.5;
1977               }
1978               else
1979               {
1980                 for (sum = 0, i = 8; i < 12; i++)
1981                   sum += raw(row + dir[i][0], col + dir[i][1]);
1982                 RAW(row, col) = 0.5 + sum * 0.0732233 + (raw(row, col - 2) + raw(row, col + 2)) * 0.3535534;
1983               }
1984           else if (type == 129)
1985           { /* Bad pixel */
1986             if (row >= raw_height)
1987               continue;
1988             j = (FC(row - top_margin, col - left_margin) != 1) * 4;
1989             for (sum = 0, i = j; i < j + 8; i++)
1990               sum += raw(row + dir[i][0], col + dir[i][1]);
1991             RAW(row, col) = (sum + 4) >> 3;
1992           }
1993         }
1994       }
1995       else if (tag == 0x401)
1996       { /* All-color flat fields */
1997         phase_one_flat_field(1, 2);
1998       }
1999       else if (tag == 0x416 || tag == 0x410)
2000       {
2001         phase_one_flat_field(0, 2);
2002       }
2003       else if (tag == 0x40b)
2004       { /* Red+blue flat field */
2005         phase_one_flat_field(0, 4);
2006       }
2007       else if (tag == 0x412)
2008       {
2009         fseek(ifp, 36, SEEK_CUR);
2010         diff = abs(get2() - ph1.tag_21a);
2011         if (mindiff > diff)
2012         {
2013           mindiff = diff;
2014           off_412 = ftell(ifp) - 38;
2015         }
2016       }
2017       else if (tag == 0x41f && !qlin_applied)
2018       { /* Quadrant linearization */
2019         ushort lc[2][2][16], ref[16];
2020         int qr, qc;
2021         for (qr = 0; qr < 2; qr++)
2022           for (qc = 0; qc < 2; qc++)
2023             for (i = 0; i < 16; i++)
2024               lc[qr][qc][i] = get4();
2025         for (i = 0; i < 16; i++)
2026         {
2027           int v = 0;
2028           for (qr = 0; qr < 2; qr++)
2029             for (qc = 0; qc < 2; qc++)
2030               v += lc[qr][qc][i];
2031           ref[i] = (v + 2) >> 2;
2032         }
2033         for (qr = 0; qr < 2; qr++)
2034         {
2035           for (qc = 0; qc < 2; qc++)
2036           {
2037             int cx[19], cf[19];
2038             for (i = 0; i < 16; i++)
2039             {
2040               cx[1 + i] = lc[qr][qc][i];
2041               cf[1 + i] = ref[i];
2042             }
2043             cx[0] = cf[0] = 0;
2044             cx[17] = cf[17] = ((unsigned int)ref[15] * 65535) / lc[qr][qc][15];
2045             cf[18] = cx[18] = 65535;
2046             cubic_spline(cx, cf, 19);
2047 
2048             for (row = (qr ? ph1.split_row : 0); row < (qr ? raw_height : ph1.split_row); row++)
2049             {
2050 #ifdef LIBRAW_LIBRARY_BUILD
2051               checkCancel();
2052 #endif
2053               for (col = (qc ? ph1.split_col : 0); col < (qc ? raw_width : ph1.split_col); col++)
2054                 RAW(row, col) = curve[RAW(row, col)];
2055             }
2056           }
2057         }
2058         qlin_applied = 1;
2059       }
2060       else if (tag == 0x41e && !qmult_applied)
2061       { /* Quadrant multipliers */
2062         float qmult[2][2] = {{1, 1}, {1, 1}};
2063         get4();
2064         get4();
2065         get4();
2066         get4();
2067         qmult[0][0] = 1.0 + getreal(11);
2068         get4();
2069         get4();
2070         get4();
2071         get4();
2072         get4();
2073         qmult[0][1] = 1.0 + getreal(11);
2074         get4();
2075         get4();
2076         get4();
2077         qmult[1][0] = 1.0 + getreal(11);
2078         get4();
2079         get4();
2080         get4();
2081         qmult[1][1] = 1.0 + getreal(11);
2082         for (row = 0; row < raw_height; row++)
2083         {
2084 #ifdef LIBRAW_LIBRARY_BUILD
2085           checkCancel();
2086 #endif
2087           for (col = 0; col < raw_width; col++)
2088           {
2089             i = qmult[row >= ph1.split_row][col >= ph1.split_col] * RAW(row, col);
2090             RAW(row, col) = LIM(i, 0, 65535);
2091           }
2092         }
2093         qmult_applied = 1;
2094       }
2095       else if (tag == 0x431 && !qmult_applied)
2096       { /* Quadrant combined */
2097         ushort lc[2][2][7], ref[7];
2098         int qr, qc;
2099         for (i = 0; i < 7; i++)
2100           ref[i] = get4();
2101         for (qr = 0; qr < 2; qr++)
2102           for (qc = 0; qc < 2; qc++)
2103             for (i = 0; i < 7; i++)
2104               lc[qr][qc][i] = get4();
2105         for (qr = 0; qr < 2; qr++)
2106         {
2107           for (qc = 0; qc < 2; qc++)
2108           {
2109             int cx[9], cf[9];
2110             for (i = 0; i < 7; i++)
2111             {
2112               cx[1 + i] = ref[i];
2113               cf[1 + i] = ((unsigned)ref[i] * lc[qr][qc][i]) / 10000;
2114             }
2115             cx[0] = cf[0] = 0;
2116             cx[8] = cf[8] = 65535;
2117             cubic_spline(cx, cf, 9);
2118             for (row = (qr ? ph1.split_row : 0); row < (qr ? raw_height : ph1.split_row); row++)
2119             {
2120 #ifdef LIBRAW_LIBRARY_BUILD
2121               checkCancel();
2122 #endif
2123               for (col = (qc ? ph1.split_col : 0); col < (qc ? raw_width : ph1.split_col); col++)
2124                 RAW(row, col) = curve[RAW(row, col)];
2125             }
2126           }
2127         }
2128         qmult_applied = 1;
2129         qlin_applied = 1;
2130       }
2131       fseek(ifp, save, SEEK_SET);
2132     }
2133     if (off_412)
2134     {
2135       fseek(ifp, off_412, SEEK_SET);
2136       for (i = 0; i < 9; i++)
2137         head[i] = get4() & 0x7fff;
2138       yval[0] = (float *)calloc(head[1] * head[3] + head[2] * head[4], 6);
2139       merror(yval[0], "phase_one_correct()");
2140       yval[1] = (float *)(yval[0] + head[1] * head[3]);
2141       xval[0] = (ushort *)(yval[1] + head[2] * head[4]);
2142       xval[1] = (ushort *)(xval[0] + head[1] * head[3]);
2143       get2();
2144       for (i = 0; i < 2; i++)
2145         for (j = 0; j < head[i + 1] * head[i + 3]; j++)
2146           yval[i][j] = getreal(11);
2147       for (i = 0; i < 2; i++)
2148         for (j = 0; j < head[i + 1] * head[i + 3]; j++)
2149           xval[i][j] = get2();
2150       for (row = 0; row < raw_height; row++)
2151       {
2152 #ifdef LIBRAW_LIBRARY_BUILD
2153         checkCancel();
2154 #endif
2155         for (col = 0; col < raw_width; col++)
2156         {
2157           cfrac = (float)col * head[3] / raw_width;
2158           cfrac -= cip = cfrac;
2159           num = RAW(row, col) * 0.5;
2160           for (i = cip; i < cip + 2; i++)
2161           {
2162             for (k = j = 0; j < head[1]; j++)
2163               if (num < xval[0][k = head[1] * i + j])
2164                 break;
2165             frac = (j == 0 || j == head[1]) ? 0 : (xval[0][k] - num) / (xval[0][k] - xval[0][k - 1]);
2166             mult[i - cip] = yval[0][k - 1] * frac + yval[0][k] * (1 - frac);
2167           }
2168           i = ((mult[0] * (1 - cfrac) + mult[1] * cfrac) * row + num) * 2;
2169           RAW(row, col) = LIM(i, 0, 65535);
2170         }
2171       }
2172       free(yval[0]);
2173     }
2174 #ifdef LIBRAW_LIBRARY_BUILD
2175   }
2176   catch (...)
2177   {
2178     if (yval[0])
2179       free(yval[0]);
2180     return LIBRAW_CANCELLED_BY_CALLBACK;
2181   }
2182 #endif
2183   return 0;
2184 }
2185 
phase_one_load_raw()2186 void CLASS phase_one_load_raw()
2187 {
2188   int a, b, i;
2189   ushort akey, bkey, t_mask;
2190 
2191   fseek(ifp, ph1.key_off, SEEK_SET);
2192   akey = get2();
2193   bkey = get2();
2194   t_mask = ph1.format == 1 ? 0x5555 : 0x1354;
2195 #ifdef LIBRAW_LIBRARY_BUILD
2196   if (ph1.black_col || ph1.black_row)
2197   {
2198     imgdata.rawdata.ph1_cblack = (short(*)[2])calloc(raw_height * 2, sizeof(ushort));
2199     merror(imgdata.rawdata.ph1_cblack, "phase_one_load_raw()");
2200     imgdata.rawdata.ph1_rblack = (short(*)[2])calloc(raw_width * 2, sizeof(ushort));
2201     merror(imgdata.rawdata.ph1_rblack, "phase_one_load_raw()");
2202     if (ph1.black_col)
2203     {
2204       fseek(ifp, ph1.black_col, SEEK_SET);
2205       read_shorts((ushort *)imgdata.rawdata.ph1_cblack[0], raw_height * 2);
2206     }
2207     if (ph1.black_row)
2208     {
2209       fseek(ifp, ph1.black_row, SEEK_SET);
2210       read_shorts((ushort *)imgdata.rawdata.ph1_rblack[0], raw_width * 2);
2211     }
2212   }
2213 #endif
2214   fseek(ifp, data_offset, SEEK_SET);
2215   read_shorts(raw_image, raw_width * raw_height);
2216   if (ph1.format)
2217     for (i = 0; i < raw_width * raw_height; i += 2)
2218     {
2219       a = raw_image[i + 0] ^ akey;
2220       b = raw_image[i + 1] ^ bkey;
2221       raw_image[i + 0] = (a & t_mask) | (b & ~t_mask);
2222       raw_image[i + 1] = (b & t_mask) | (a & ~t_mask);
2223     }
2224 }
2225 
ph1_bithuff(int nbits,ushort * huff)2226 unsigned CLASS ph1_bithuff(int nbits, ushort *huff)
2227 {
2228 #ifndef LIBRAW_NOTHREADS
2229 #define bitbuf tls->ph1_bits.bitbuf
2230 #define vbits tls->ph1_bits.vbits
2231 #else
2232   static UINT64 bitbuf = 0;
2233   static int vbits = 0;
2234 #endif
2235   unsigned c;
2236 
2237   if (nbits == -1)
2238     return bitbuf = vbits = 0;
2239   if (nbits == 0)
2240     return 0;
2241   if (vbits < nbits)
2242   {
2243     bitbuf = bitbuf << 32 | get4();
2244     vbits += 32;
2245   }
2246   c = bitbuf << (64 - vbits) >> (64 - nbits);
2247   if (huff)
2248   {
2249     vbits -= huff[c] >> 8;
2250     return (uchar)huff[c];
2251   }
2252   vbits -= nbits;
2253   return c;
2254 #ifndef LIBRAW_NOTHREADS
2255 #undef bitbuf
2256 #undef vbits
2257 #endif
2258 }
2259 #define ph1_bits(n) ph1_bithuff(n, 0)
2260 #define ph1_huff(h) ph1_bithuff(*h, h + 1)
2261 
phase_one_load_raw_c()2262 void CLASS phase_one_load_raw_c()
2263 {
2264   static const int length[] = {8, 7, 6, 9, 11, 10, 5, 12, 14, 13};
2265   int *offset, len[2], pred[2], row, col, i, j;
2266   ushort *pixel;
2267   short(*c_black)[2], (*r_black)[2];
2268 #ifdef LIBRAW_LIBRARY_BUILD
2269   if (ph1.format == 6)
2270     throw LIBRAW_EXCEPTION_IO_CORRUPT;
2271 #endif
2272 
2273   pixel = (ushort *)calloc(raw_width * 3 + raw_height * 4, 2);
2274   merror(pixel, "phase_one_load_raw_c()");
2275   offset = (int *)(pixel + raw_width);
2276   fseek(ifp, strip_offset, SEEK_SET);
2277   for (row = 0; row < raw_height; row++)
2278     offset[row] = get4();
2279   c_black = (short(*)[2])(offset + raw_height);
2280   fseek(ifp, ph1.black_col, SEEK_SET);
2281   if (ph1.black_col)
2282     read_shorts((ushort *)c_black[0], raw_height * 2);
2283   r_black = c_black + raw_height;
2284   fseek(ifp, ph1.black_row, SEEK_SET);
2285   if (ph1.black_row)
2286     read_shorts((ushort *)r_black[0], raw_width * 2);
2287 
2288 #ifdef LIBRAW_LIBRARY_BUILD
2289   // Copy data to internal copy (ever if not read)
2290   if (ph1.black_col || ph1.black_row)
2291   {
2292     imgdata.rawdata.ph1_cblack = (short(*)[2])calloc(raw_height * 2, sizeof(ushort));
2293     merror(imgdata.rawdata.ph1_cblack, "phase_one_load_raw_c()");
2294     memmove(imgdata.rawdata.ph1_cblack, (ushort *)c_black[0], raw_height * 2 * sizeof(ushort));
2295     imgdata.rawdata.ph1_rblack = (short(*)[2])calloc(raw_width * 2, sizeof(ushort));
2296     merror(imgdata.rawdata.ph1_rblack, "phase_one_load_raw_c()");
2297     memmove(imgdata.rawdata.ph1_rblack, (ushort *)r_black[0], raw_width * 2 * sizeof(ushort));
2298   }
2299 #endif
2300 
2301   for (i = 0; i < 256; i++)
2302     curve[i] = i * i / 3.969 + 0.5;
2303 #ifdef LIBRAW_LIBRARY_BUILD
2304   try
2305   {
2306 #endif
2307     for (row = 0; row < raw_height; row++)
2308     {
2309 #ifdef LIBRAW_LIBRARY_BUILD
2310       checkCancel();
2311 #endif
2312       fseek(ifp, data_offset + offset[row], SEEK_SET);
2313       ph1_bits(-1);
2314       pred[0] = pred[1] = 0;
2315       for (col = 0; col < raw_width; col++)
2316       {
2317         if (col >= (raw_width & -8))
2318           len[0] = len[1] = 14;
2319         else if ((col & 7) == 0)
2320           for (i = 0; i < 2; i++)
2321           {
2322             for (j = 0; j < 5 && !ph1_bits(1); j++)
2323               ;
2324             if (j--)
2325               len[i] = length[j * 2 + ph1_bits(1)];
2326           }
2327         if ((i = len[col & 1]) == 14)
2328           pixel[col] = pred[col & 1] = ph1_bits(16);
2329         else
2330           pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
2331         if (pred[col & 1] >> 16)
2332           derror();
2333         if (ph1.format == 5 && pixel[col] < 256)
2334           pixel[col] = curve[pixel[col]];
2335       }
2336 #ifndef LIBRAW_LIBRARY_BUILD
2337       for (col = 0; col < raw_width; col++)
2338       {
2339         int shift = ph1.format == 8 ? 0 : 2;
2340         i = (pixel[col] << shift) - ph1.t_black + c_black[row][col >= ph1.split_col] +
2341             r_black[col][row >= ph1.split_row];
2342         if (i > 0)
2343           RAW(row, col) = i;
2344       }
2345 #else
2346     if (ph1.format == 8)
2347       memmove(&RAW(row, 0), &pixel[0], raw_width * 2);
2348     else
2349       for (col = 0; col < raw_width; col++)
2350         RAW(row, col) = pixel[col] << 2;
2351 #endif
2352     }
2353 #ifdef LIBRAW_LIBRARY_BUILD
2354   }
2355   catch (...)
2356   {
2357     free(pixel);
2358     throw;
2359   }
2360 #endif
2361   free(pixel);
2362   maximum = 0xfffc - ph1.t_black;
2363 }
2364 
hasselblad_load_raw()2365 void CLASS hasselblad_load_raw()
2366 {
2367   struct jhead jh;
2368   int shot, row, col, *back[5], len[2], diff[12], pred, sh, f, s, c;
2369   unsigned upix, urow, ucol;
2370   ushort *ip;
2371 
2372   if (!ljpeg_start(&jh, 0))
2373     return;
2374   order = 0x4949;
2375   ph1_bits(-1);
2376 #ifdef LIBRAW_LIBRARY_BUILD
2377   try
2378   {
2379 #endif
2380     back[4] = (int *)calloc(raw_width, 3 * sizeof **back);
2381     merror(back[4], "hasselblad_load_raw()");
2382     FORC3 back[c] = back[4] + c * raw_width;
2383     cblack[6] >>= sh = tiff_samples > 1;
2384     shot = LIM(shot_select, 1, tiff_samples) - 1;
2385     for (row = 0; row < raw_height; row++)
2386     {
2387 #ifdef LIBRAW_LIBRARY_BUILD
2388       checkCancel();
2389 #endif
2390       FORC4 back[(c + 3) & 3] = back[c];
2391       for (col = 0; col < raw_width; col += 2)
2392       {
2393         for (s = 0; s < tiff_samples * 2; s += 2)
2394         {
2395           FORC(2) len[c] = ph1_huff(jh.huff[0]);
2396           FORC(2)
2397           {
2398             diff[s + c] = ph1_bits(len[c]);
2399             if ((diff[s + c] & (1 << (len[c] - 1))) == 0)
2400               diff[s + c] -= (1 << len[c]) - 1;
2401             if (diff[s + c] == 65535)
2402               diff[s + c] = -32768;
2403           }
2404         }
2405         for (s = col; s < col + 2; s++)
2406         {
2407           pred = 0x8000 + load_flags;
2408           if (col)
2409             pred = back[2][s - 2];
2410           if (col && row > 1)
2411             switch (jh.psv)
2412             {
2413             case 11:
2414               pred += back[0][s] / 2 - back[0][s - 2] / 2;
2415               break;
2416             }
2417           f = (row & 1) * 3 ^ ((col + s) & 1);
2418           FORC(tiff_samples)
2419           {
2420             pred += diff[(s & 1) * tiff_samples + c];
2421             upix = pred >> sh & 0xffff;
2422             if (raw_image && c == shot)
2423               RAW(row, s) = upix;
2424             if (image)
2425             {
2426               urow = row - top_margin + (c & 1);
2427               ucol = col - left_margin - ((c >> 1) & 1);
2428               ip = &image[urow * width + ucol][f];
2429               if (urow < height && ucol < width)
2430                 *ip = c < 4 ? upix : (*ip + upix) >> 1;
2431             }
2432           }
2433           back[2][s] = pred;
2434         }
2435       }
2436     }
2437 #ifdef LIBRAW_LIBRARY_BUILD
2438   }
2439   catch (...)
2440   {
2441     free(back[4]);
2442     ljpeg_end(&jh);
2443     throw;
2444   }
2445 #endif
2446   free(back[4]);
2447   ljpeg_end(&jh);
2448   if (image)
2449     mix_green = 1;
2450 }
2451 
leaf_hdr_load_raw()2452 void CLASS leaf_hdr_load_raw()
2453 {
2454   ushort *pixel = 0;
2455   unsigned tile = 0, r, c, row, col;
2456 
2457   if (!filters || !raw_image)
2458   {
2459 #ifdef LIBRAW_LIBRARY_BUILD
2460     if(!image)
2461       throw LIBRAW_EXCEPTION_IO_CORRUPT;
2462 #endif
2463     pixel = (ushort *)calloc(raw_width, sizeof *pixel);
2464     merror(pixel, "leaf_hdr_load_raw()");
2465   }
2466 #ifdef LIBRAW_LIBRARY_BUILD
2467   try
2468   {
2469 #endif
2470     FORC(tiff_samples)
2471     for (r = 0; r < raw_height; r++)
2472     {
2473 #ifdef LIBRAW_LIBRARY_BUILD
2474       checkCancel();
2475 #endif
2476       if (r % tile_length == 0)
2477       {
2478         fseek(ifp, data_offset + 4 * tile++, SEEK_SET);
2479         fseek(ifp, get4(), SEEK_SET);
2480       }
2481       if (filters && c != shot_select)
2482         continue;
2483       if (filters && raw_image)
2484         pixel = raw_image + r * raw_width;
2485       read_shorts(pixel, raw_width);
2486       if (!filters && image && (row = r - top_margin) < height)
2487         for (col = 0; col < width; col++)
2488           image[row * width + col][c] = pixel[col + left_margin];
2489     }
2490 #ifdef LIBRAW_LIBRARY_BUILD
2491   }
2492   catch (...)
2493   {
2494     if (!filters)
2495       free(pixel);
2496     throw;
2497   }
2498 #endif
2499   if (!filters)
2500   {
2501     maximum = 0xffff;
2502     raw_color = 1;
2503     free(pixel);
2504   }
2505 }
2506 
unpacked_load_raw()2507 void CLASS unpacked_load_raw()
2508 {
2509   int row, col, bits = 0;
2510   while (1 << ++bits < maximum)
2511     ;
2512   read_shorts(raw_image, raw_width * raw_height);
2513   fseek(ifp,-2,SEEK_CUR); // avoid EOF error
2514   if (maximum < 0xffff || load_flags)
2515     for (row = 0; row < raw_height; row++)
2516     {
2517 #ifdef LIBRAW_LIBRARY_BUILD
2518       checkCancel();
2519 #endif
2520       for (col = 0; col < raw_width; col++)
2521         if ((RAW(row, col) >>= load_flags) >> bits && (unsigned)(row - top_margin) < height &&
2522             (unsigned)(col - left_margin) < width)
2523           derror();
2524     }
2525 }
2526 
unpacked_load_raw_reversed()2527 void CLASS unpacked_load_raw_reversed()
2528 {
2529   int row, col, bits = 0;
2530   while (1 << ++bits < maximum)
2531     ;
2532   for (row = raw_height - 1; row >= 0; row--)
2533   {
2534 #ifdef LIBRAW_LIBRARY_BUILD
2535     checkCancel();
2536 #endif
2537     read_shorts(&raw_image[row * raw_width], raw_width);
2538     for (col = 0; col < raw_width; col++)
2539       if ((RAW(row, col) >>= load_flags) >> bits && (unsigned)(row - top_margin) < height &&
2540           (unsigned)(col - left_margin) < width)
2541         derror();
2542   }
2543 }
2544 
sinar_4shot_load_raw()2545 void CLASS sinar_4shot_load_raw()
2546 {
2547   ushort *pixel;
2548   unsigned shot, row, col, r, c;
2549 
2550   if (raw_image)
2551   {
2552     shot = LIM(shot_select, 1, 4) - 1;
2553     fseek(ifp, data_offset + shot * 4, SEEK_SET);
2554     fseek(ifp, get4(), SEEK_SET);
2555     unpacked_load_raw();
2556     return;
2557   }
2558 #ifdef LIBRAW_LIBRARY_BUILD
2559   if (!image)
2560     throw LIBRAW_EXCEPTION_IO_CORRUPT;
2561 #endif
2562   pixel = (ushort *)calloc(raw_width, sizeof *pixel);
2563   merror(pixel, "sinar_4shot_load_raw()");
2564 #ifdef LIBRAW_LIBRARY_BUILD
2565   try
2566   {
2567 #endif
2568     for (shot = 0; shot < 4; shot++)
2569     {
2570 #ifdef LIBRAW_LIBRARY_BUILD
2571       checkCancel();
2572 #endif
2573       fseek(ifp, data_offset + shot * 4, SEEK_SET);
2574       fseek(ifp, get4(), SEEK_SET);
2575       for (row = 0; row < raw_height; row++)
2576       {
2577         read_shorts(pixel, raw_width);
2578         if ((r = row - top_margin - (shot >> 1 & 1)) >= height)
2579           continue;
2580         for (col = 0; col < raw_width; col++)
2581         {
2582           if ((c = col - left_margin - (shot & 1)) >= width)
2583             continue;
2584           image[r * width + c][(row & 1) * 3 ^ (~col & 1)] = pixel[col];
2585         }
2586       }
2587     }
2588 #ifdef LIBRAW_LIBRARY_BUILD
2589   }
2590   catch (...)
2591   {
2592     free(pixel);
2593     throw;
2594   }
2595 #endif
2596   free(pixel);
2597   mix_green = 1;
2598 }
2599 
imacon_full_load_raw()2600 void CLASS imacon_full_load_raw()
2601 {
2602 #ifdef LIBRAW_LIBRARY_BUILD
2603   if (!image)
2604     throw LIBRAW_EXCEPTION_IO_CORRUPT;
2605 #endif
2606   int row, col;
2607 
2608 #ifdef LIBRAW_LIBRARY_BUILD
2609   unsigned short *buf = (unsigned short *)malloc(width * 3 * sizeof(unsigned short));
2610   merror(buf, "imacon_full_load_raw");
2611 #endif
2612 
2613   for (row = 0; row < height; row++)
2614   {
2615 #ifdef LIBRAW_LIBRARY_BUILD
2616     checkCancel();
2617     read_shorts(buf, width * 3);
2618     unsigned short(*rowp)[4] = &image[row * width];
2619     for (col = 0; col < width; col++)
2620     {
2621       rowp[col][0] = buf[col * 3];
2622       rowp[col][1] = buf[col * 3 + 1];
2623       rowp[col][2] = buf[col * 3 + 2];
2624       rowp[col][3] = 0;
2625     }
2626 #else
2627     for (col = 0; col < width; col++)
2628       read_shorts(image[row * width + col], 3);
2629 #endif
2630   }
2631 #ifdef LIBRAW_LIBRARY_BUILD
2632   free(buf);
2633 #endif
2634 }
2635 
packed_load_raw()2636 void CLASS packed_load_raw()
2637 {
2638   int vbits = 0, bwide, rbits, bite, half, irow, row, col, val, i;
2639   UINT64 bitbuf = 0;
2640 
2641   bwide = raw_width * tiff_bps / 8;
2642   bwide += bwide & load_flags >> 7;
2643   rbits = bwide * 8 - raw_width * tiff_bps;
2644   if (load_flags & 1)
2645     bwide = bwide * 16 / 15;
2646   bite = 8 + (load_flags & 24);
2647   half = (raw_height + 1) >> 1;
2648   for (irow = 0; irow < raw_height; irow++)
2649   {
2650 #ifdef LIBRAW_LIBRARY_BUILD
2651     checkCancel();
2652 #endif
2653     row = irow;
2654     if (load_flags & 2 && (row = irow % half * 2 + irow / half) == 1 && load_flags & 4)
2655     {
2656       if (vbits = 0, tiff_compress)
2657         fseek(ifp, data_offset - (-half * bwide & -2048), SEEK_SET);
2658       else
2659       {
2660         fseek(ifp, 0, SEEK_END);
2661         fseek(ifp, ftell(ifp) >> 3 << 2, SEEK_SET);
2662       }
2663     }
2664     if(feof(ifp)) throw LIBRAW_EXCEPTION_IO_EOF;
2665     for (col = 0; col < raw_width; col++)
2666     {
2667       for (vbits -= tiff_bps; vbits < 0; vbits += bite)
2668       {
2669         bitbuf <<= bite;
2670         for (i = 0; i < bite; i += 8)
2671           bitbuf |= (unsigned)(fgetc(ifp) << i);
2672       }
2673       val = bitbuf << (64 - tiff_bps - vbits) >> (64 - tiff_bps);
2674       RAW(row, col ^ (load_flags >> 6 & 1)) = val;
2675       if (load_flags & 1 && (col % 10) == 9 && fgetc(ifp) && row < height + top_margin && col < width + left_margin)
2676         derror();
2677     }
2678     vbits -= rbits;
2679   }
2680 }
2681 
2682 #ifdef LIBRAW_LIBRARY_BUILD
2683 
2684 ushort raw_stride;
2685 
parse_broadcom()2686 void CLASS parse_broadcom()
2687 {
2688 
2689   /* This structure is at offset 0xb0 from the 'BRCM' ident. */
2690   struct
2691   {
2692     uint8_t umode[32];
2693     uint16_t uwidth;
2694     uint16_t uheight;
2695     uint16_t padding_right;
2696     uint16_t padding_down;
2697     uint32_t unknown_block[6];
2698     uint16_t transform;
2699     uint16_t format;
2700     uint8_t bayer_order;
2701     uint8_t bayer_format;
2702   } header;
2703 
2704   header.bayer_order = 0;
2705   fseek(ifp, 0xb0 - 0x20, SEEK_CUR);
2706   fread(&header, 1, sizeof(header), ifp);
2707   raw_stride = ((((((header.uwidth + header.padding_right) * 5) + 3) >> 2) + 0x1f) & (~0x1f));
2708   raw_width = width = header.uwidth;
2709   raw_height = height = header.uheight;
2710   filters = 0x16161616; /* default Bayer order is 2, BGGR */
2711 
2712   switch (header.bayer_order)
2713   {
2714   case 0: /* RGGB */
2715     filters = 0x94949494;
2716     break;
2717   case 1: /* GBRG */
2718     filters = 0x49494949;
2719     break;
2720   case 3: /* GRBG */
2721     filters = 0x61616161;
2722     break;
2723   }
2724 }
2725 
broadcom_load_raw()2726 void CLASS broadcom_load_raw()
2727 {
2728 
2729   uchar *data, *dp;
2730   int rev, row, col, c;
2731 
2732   rev = 3 * (order == 0x4949);
2733   data = (uchar *)malloc(raw_stride * 2);
2734   merror(data, "broadcom_load_raw()");
2735 
2736   for (row = 0; row < raw_height; row++)
2737   {
2738     if (fread(data + raw_stride, 1, raw_stride, ifp) < raw_stride)
2739       derror();
2740     FORC(raw_stride) data[c] = data[raw_stride + (c ^ rev)];
2741     for (dp = data, col = 0; col < raw_width; dp += 5, col += 4)
2742       FORC4 RAW(row, col + c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
2743   }
2744   free(data);
2745 }
2746 #endif
2747 
nokia_load_raw()2748 void CLASS nokia_load_raw()
2749 {
2750   uchar *data, *dp;
2751   int rev, dwide, row, col, c;
2752   double sum[] = {0, 0};
2753 
2754   rev = 3 * (order == 0x4949);
2755   dwide = (raw_width * 5 + 1) / 4;
2756   data = (uchar *)malloc(dwide * 2);
2757   merror(data, "nokia_load_raw()");
2758 #ifdef LIBRAW_LIBRARY_BUILD
2759   try
2760   {
2761 #endif
2762     for (row = 0; row < raw_height; row++)
2763     {
2764 #ifdef LIBRAW_LIBRARY_BUILD
2765       checkCancel();
2766 #endif
2767       if (fread(data + dwide, 1, dwide, ifp) < dwide)
2768         derror();
2769       FORC(dwide) data[c] = data[dwide + (c ^ rev)];
2770       for (dp = data, col = 0; col < raw_width; dp += 5, col += 4)
2771         FORC4 RAW(row, col + c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
2772     }
2773 #ifdef LIBRAW_LIBRARY_BUILD
2774   }
2775   catch (...)
2776   {
2777     free(data);
2778     throw;
2779   }
2780 #endif
2781   free(data);
2782   maximum = 0x3ff;
2783   if (strncmp(make, "OmniVision", 10))
2784     return;
2785   row = raw_height / 2;
2786   FORC(width - 1)
2787   {
2788     sum[c & 1] += SQR(RAW(row, c) - RAW(row + 1, c + 1));
2789     sum[~c & 1] += SQR(RAW(row + 1, c) - RAW(row, c + 1));
2790   }
2791   if (sum[1] > sum[0])
2792     filters = 0x4b4b4b4b;
2793 }
2794 
android_tight_load_raw()2795 void CLASS android_tight_load_raw()
2796 {
2797   uchar *data, *dp;
2798   int bwide, row, col, c;
2799 
2800   bwide = -(-5 * raw_width >> 5) << 3;
2801   data = (uchar *)malloc(bwide);
2802   merror(data, "android_tight_load_raw()");
2803   for (row = 0; row < raw_height; row++)
2804   {
2805     if (fread(data, 1, bwide, ifp) < bwide)
2806       derror();
2807     for (dp = data, col = 0; col < raw_width; dp += 5, col += 4)
2808       FORC4 RAW(row, col + c) = (dp[c] << 2) | (dp[4] >> (c << 1) & 3);
2809   }
2810   free(data);
2811 }
2812 
android_loose_load_raw()2813 void CLASS android_loose_load_raw()
2814 {
2815   uchar *data, *dp;
2816   int bwide, row, col, c;
2817   UINT64 bitbuf = 0;
2818 
2819   bwide = (raw_width + 5) / 6 << 3;
2820   data = (uchar *)malloc(bwide);
2821   merror(data, "android_loose_load_raw()");
2822   for (row = 0; row < raw_height; row++)
2823   {
2824     if (fread(data, 1, bwide, ifp) < bwide)
2825       derror();
2826     for (dp = data, col = 0; col < raw_width; dp += 8, col += 6)
2827     {
2828       FORC(8) bitbuf = (bitbuf << 8) | dp[c ^ 7];
2829       FORC(6) RAW(row, col + c) = (bitbuf >> c * 10) & 0x3ff;
2830     }
2831   }
2832   free(data);
2833 }
2834 
canon_rmf_load_raw()2835 void CLASS canon_rmf_load_raw()
2836 {
2837   int row, col, bits, orow, ocol, c;
2838 
2839 #ifdef LIBRAW_LIBRARY_BUILD
2840   int *words = (int *)malloc(sizeof(int) * (raw_width / 3 + 1));
2841   merror(words, "canon_rmf_load_raw");
2842 #endif
2843   for (row = 0; row < raw_height; row++)
2844   {
2845 #ifdef LIBRAW_LIBRARY_BUILD
2846     checkCancel();
2847     fread(words, sizeof(int), raw_width / 3, ifp);
2848     for (col = 0; col < raw_width - 2; col += 3)
2849     {
2850       bits = words[col / 3];
2851       FORC3
2852       {
2853         orow = row;
2854         if ((ocol = col + c - 4) < 0)
2855         {
2856           ocol += raw_width;
2857           if ((orow -= 2) < 0)
2858             orow += raw_height;
2859         }
2860         RAW(orow, ocol) = curve[bits >> (10 * c + 2) & 0x3ff];
2861       }
2862     }
2863 #else
2864     for (col = 0; col < raw_width - 2; col += 3)
2865     {
2866       bits = get4();
2867       FORC3
2868       {
2869         orow = row;
2870         if ((ocol = col + c - 4) < 0)
2871         {
2872           ocol += raw_width;
2873           if ((orow -= 2) < 0)
2874             orow += raw_height;
2875         }
2876         RAW(orow, ocol) = curve[bits >> (10 * c + 2) & 0x3ff];
2877       }
2878     }
2879 #endif
2880   }
2881 #ifdef LIBRAW_LIBRARY_BUILD
2882   free(words);
2883 #endif
2884   maximum = curve[0x3ff];
2885 }
2886 
pana_data(int nb,unsigned * bytes)2887 unsigned CLASS pana_data(int nb, unsigned *bytes)
2888 {
2889 #ifndef LIBRAW_NOTHREADS
2890 #define vpos tls->pana_data.vpos
2891 #define buf tls->pana_data.buf
2892 #else
2893   static uchar buf[0x4002];
2894   static int vpos;
2895 #endif
2896   int byte;
2897 
2898   if (!nb && !bytes)
2899     return vpos = 0;
2900 
2901   if (!vpos)
2902   {
2903     fread(buf + load_flags, 1, 0x4000 - load_flags, ifp);
2904     fread(buf, 1, load_flags, ifp);
2905   }
2906 
2907   if (pana_encoding == 5)
2908   {
2909     for (byte = 0; byte < 16; byte++)
2910     {
2911       bytes[byte] = buf[vpos++];
2912       vpos &= 0x3FFF;
2913     }
2914   }
2915   else
2916   {
2917     vpos = (vpos - nb) & 0x1ffff;
2918     byte = vpos >> 3 ^ 0x3ff0;
2919     return (buf[byte] | buf[byte + 1] << 8) >> (vpos & 7) & ~((~0u) << nb);
2920   }
2921   return 0;
2922 #ifndef LIBRAW_NOTHREADS
2923 #undef vpos
2924 #undef buf
2925 #endif
2926 }
2927 
panasonic_load_raw()2928 void CLASS panasonic_load_raw()
2929 {
2930   int row, col, i, j, sh = 0, pred[2], nonz[2];
2931   unsigned bytes[16];
2932   ushort *raw_block_data;
2933   int enc_blck_size = pana_bpp == 12 ? 10 : 9;
2934 
2935   pana_data(0, 0);
2936   if (pana_encoding == 5)
2937   {
2938     for (row = 0; row < raw_height; row++)
2939     {
2940       raw_block_data = raw_image + row * raw_width;
2941 
2942 #ifdef LIBRAW_LIBRARY_BUILD
2943       checkCancel();
2944 #endif
2945       for (col = 0; col < raw_width; col += enc_blck_size)
2946       {
2947         pana_data(0, bytes);
2948 
2949         if (pana_bpp == 12)
2950         {
2951           raw_block_data[col] = ((bytes[1] & 0xF) << 8) + bytes[0];
2952           raw_block_data[col + 1] = 16 * bytes[2] + (bytes[1] >> 4);
2953           raw_block_data[col + 2] = ((bytes[4] & 0xF) << 8) + bytes[3];
2954           raw_block_data[col + 3] = 16 * bytes[5] + (bytes[4] >> 4);
2955           raw_block_data[col + 4] = ((bytes[7] & 0xF) << 8) + bytes[6];
2956           raw_block_data[col + 5] = 16 * bytes[8] + (bytes[7] >> 4);
2957           raw_block_data[col + 6] = ((bytes[10] & 0xF) << 8) + bytes[9];
2958           raw_block_data[col + 7] = 16 * bytes[11] + (bytes[10] >> 4);
2959           raw_block_data[col + 8] = ((bytes[13] & 0xF) << 8) + bytes[12];
2960           raw_block_data[col + 9] = 16 * bytes[14] + (bytes[13] >> 4);
2961         }
2962         else if (pana_bpp == 14)
2963         {
2964           raw_block_data[col] = bytes[0] + ((bytes[1] & 0x3F) << 8);
2965           raw_block_data[col + 1] = (bytes[1] >> 6) + 4 * (bytes[2]) +
2966                                     ((bytes[3] & 0xF) << 10);
2967           raw_block_data[col + 2] = (bytes[3] >> 4) + 16 * (bytes[4]) +
2968                                     ((bytes[5] & 3) << 12);
2969           raw_block_data[col + 3] = ((bytes[5] & 0xFC) >> 2) + (bytes[6] << 6);
2970           raw_block_data[col + 4] = bytes[7] + ((bytes[8] & 0x3F) << 8);
2971           raw_block_data[col + 5] = (bytes[8] >> 6) + 4 * bytes[9] + ((bytes[10] & 0xF) << 10);
2972           raw_block_data[col + 6] = (bytes[10] >> 4) + 16 * bytes[11] + ((bytes[12] & 3) << 12);
2973           raw_block_data[col + 7] = ((bytes[12] & 0xFC) >> 2) + (bytes[13] << 6);
2974           raw_block_data[col + 8] = bytes[14] + ((bytes[15] & 0x3F) << 8);
2975         }
2976       }
2977     }
2978   }
2979   else
2980   {
2981     for (row = 0; row < raw_height; row++)
2982     {
2983 #ifdef LIBRAW_LIBRARY_BUILD
2984       checkCancel();
2985 #endif
2986       for (col = 0; col < raw_width; col++)
2987       {
2988         if ((i = col % 14) == 0)
2989           pred[0] = pred[1] = nonz[0] = nonz[1] = 0;
2990         if (i % 3 == 2)
2991           sh = 4 >> (3 - pana_data(2, 0));
2992         if (nonz[i & 1])
2993         {
2994           if ((j = pana_data(8, 0)))
2995           {
2996             if ((pred[i & 1] -= 0x80 << sh) < 0 || sh == 4)
2997               pred[i & 1] &= ~((~0u) << sh);
2998             pred[i & 1] += j << sh;
2999           }
3000         }
3001         else if ((nonz[i & 1] = pana_data(8, 0)) || i > 11)
3002           pred[i & 1] = nonz[i & 1] << 4 | pana_data(4, 0);
3003         if ((RAW(row, col) = pred[col & 1]) > 4098 && col < width && row < height)
3004           derror();
3005       }
3006     }
3007   }
3008 }
3009 
3010 
olympus_load_raw()3011 void CLASS olympus_load_raw()
3012 {
3013   ushort huff[4096];
3014   int row, col, nbits, sign, low, high, i, c, w, n, nw;
3015   int acarry[2][3], *carry, pred, diff;
3016 
3017   huff[n = 0] = 0xc0c;
3018   for (i = 12; i--;)
3019     FORC(2048 >> i) huff[++n] = (i + 1) << 8 | i;
3020   fseek(ifp, 7, SEEK_CUR);
3021   getbits(-1);
3022   for (row = 0; row < height; row++)
3023   {
3024 #ifdef LIBRAW_LIBRARY_BUILD
3025     checkCancel();
3026 #endif
3027     memset(acarry, 0, sizeof acarry);
3028     for (col = 0; col < raw_width; col++)
3029     {
3030       carry = acarry[col & 1];
3031       i = 2 * (carry[2] < 3);
3032       for (nbits = 2 + i; (ushort)carry[0] >> (nbits + i); nbits++)
3033         ;
3034       low = (sign = getbits(3)) & 3;
3035       sign = sign << 29 >> 31;
3036       if ((high = getbithuff(12, huff)) == 12)
3037         high = getbits(16 - nbits) >> 1;
3038       carry[0] = (high << nbits) | getbits(nbits);
3039       diff = (carry[0] ^ sign) + carry[1];
3040       carry[1] = (diff * 3 + carry[1]) >> 5;
3041       carry[2] = carry[0] > 16 ? 0 : carry[2] + 1;
3042       if (col >= width)
3043         continue;
3044       if (row < 2 && col < 2)
3045         pred = 0;
3046       else if (row < 2)
3047         pred = RAW(row, col - 2);
3048       else if (col < 2)
3049         pred = RAW(row - 2, col);
3050       else
3051       {
3052         w = RAW(row, col - 2);
3053         n = RAW(row - 2, col);
3054         nw = RAW(row - 2, col - 2);
3055         if ((w < nw && nw < n) || (n < nw && nw < w))
3056         {
3057           if (ABS(w - nw) > 32 || ABS(n - nw) > 32)
3058             pred = w + n - nw;
3059           else
3060             pred = (w + n) >> 1;
3061         }
3062         else
3063           pred = ABS(w - nw) > ABS(n - nw) ? w : n;
3064       }
3065       if ((RAW(row, col) = pred + ((diff << 2) | low)) >> 12)
3066         derror();
3067     }
3068   }
3069 }
3070 
minolta_rd175_load_raw()3071 void CLASS minolta_rd175_load_raw()
3072 {
3073   uchar pixel[768];
3074   unsigned irow, box, row, col;
3075 
3076   for (irow = 0; irow < 1481; irow++)
3077   {
3078 #ifdef LIBRAW_LIBRARY_BUILD
3079     checkCancel();
3080 #endif
3081     if (fread(pixel, 1, 768, ifp) < 768)
3082       derror();
3083     box = irow / 82;
3084     row = irow % 82 * 12 + ((box < 12) ? box | 1 : (box - 12) * 2);
3085     switch (irow)
3086     {
3087     case 1477:
3088     case 1479:
3089       continue;
3090     case 1476:
3091       row = 984;
3092       break;
3093     case 1480:
3094       row = 985;
3095       break;
3096     case 1478:
3097       row = 985;
3098       box = 1;
3099     }
3100     if ((box < 12) && (box & 1))
3101     {
3102       for (col = 0; col < 1533; col++, row ^= 1)
3103         if (col != 1)
3104           RAW(row, col) = (col + 1) & 2 ? pixel[col / 2 - 1] + pixel[col / 2 + 1] : pixel[col / 2] << 1;
3105       RAW(row, 1) = pixel[1] << 1;
3106       RAW(row, 1533) = pixel[765] << 1;
3107     }
3108     else
3109       for (col = row & 1; col < 1534; col += 2)
3110         RAW(row, col) = pixel[col / 2] << 1;
3111   }
3112   maximum = 0xff << 1;
3113 }
3114 
quicktake_100_load_raw()3115 void CLASS quicktake_100_load_raw()
3116 {
3117   uchar pixel[484][644];
3118   static const short gstep[16] = {-89, -60, -44, -32, -22, -15, -8, -2, 2, 8, 15, 22, 32, 44, 60, 89};
3119   static const short rstep[6][4] = {{-3, -1, 1, 3},   {-5, -1, 1, 5},   {-8, -2, 2, 8},
3120                                     {-13, -3, 3, 13}, {-19, -4, 4, 19}, {-28, -6, 6, 28}};
3121   static const short t_curve[256] = {
3122       0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   11,  12,   13,   14,  15,  16,  17,  18,  19,  20,  21,  22,
3123       23,  24,  25,  26,  27,  28,  29,  30,  32,  33,  34,  35,   36,   37,  38,  39,  40,  41,  42,  43,  44,  45,
3124       46,  47,  48,  49,  50,  51,  53,  54,  55,  56,  57,  58,   59,   60,  61,  62,  63,  64,  65,  66,  67,  68,
3125       69,  70,  71,  72,  74,  75,  76,  77,  78,  79,  80,  81,   82,   83,  84,  86,  88,  90,  92,  94,  97,  99,
3126       101, 103, 105, 107, 110, 112, 114, 116, 118, 120, 123, 125,  127,  129, 131, 134, 136, 138, 140, 142, 144, 147,
3127       149, 151, 153, 155, 158, 160, 162, 164, 166, 168, 171, 173,  175,  177, 179, 181, 184, 186, 188, 190, 192, 195,
3128       197, 199, 201, 203, 205, 208, 210, 212, 214, 216, 218, 221,  223,  226, 230, 235, 239, 244, 248, 252, 257, 261,
3129       265, 270, 274, 278, 283, 287, 291, 296, 300, 305, 309, 313,  318,  322, 326, 331, 335, 339, 344, 348, 352, 357,
3130       361, 365, 370, 374, 379, 383, 387, 392, 396, 400, 405, 409,  413,  418, 422, 426, 431, 435, 440, 444, 448, 453,
3131       457, 461, 466, 470, 474, 479, 483, 487, 492, 496, 500, 508,  519,  531, 542, 553, 564, 575, 587, 598, 609, 620,
3132       631, 643, 654, 665, 676, 687, 698, 710, 721, 732, 743, 754,  766,  777, 788, 799, 810, 822, 833, 844, 855, 866,
3133       878, 889, 900, 911, 922, 933, 945, 956, 967, 978, 989, 1001, 1012, 1023};
3134   int rb, row, col, sharp, val = 0;
3135 #ifdef LIBRAW_LIBRARY_BUILD
3136   if(width>640 || height > 480)
3137     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3138 #endif
3139 
3140   getbits(-1);
3141   memset(pixel, 0x80, sizeof pixel);
3142   for (row = 2; row < height + 2; row++)
3143   {
3144 #ifdef LIBRAW_LIBRARY_BUILD
3145     checkCancel();
3146 #endif
3147     for (col = 2 + (row & 1); col < width + 2; col += 2)
3148     {
3149       val = ((pixel[row - 1][col - 1] + 2 * pixel[row - 1][col + 1] + pixel[row][col - 2]) >> 2) + gstep[getbits(4)];
3150       pixel[row][col] = val = LIM(val, 0, 255);
3151       if (col < 4)
3152         pixel[row][col - 2] = pixel[row + 1][~row & 1] = val;
3153       if (row == 2)
3154         pixel[row - 1][col + 1] = pixel[row - 1][col + 3] = val;
3155     }
3156     pixel[row][col] = val;
3157   }
3158   for (rb = 0; rb < 2; rb++)
3159     for (row = 2 + rb; row < height + 2; row += 2)
3160     {
3161 #ifdef LIBRAW_LIBRARY_BUILD
3162       checkCancel();
3163 #endif
3164       for (col = 3 - (row & 1); col < width + 2; col += 2)
3165       {
3166         if (row < 4 || col < 4)
3167           sharp = 2;
3168         else
3169         {
3170           val = ABS(pixel[row - 2][col] - pixel[row][col - 2]) + ABS(pixel[row - 2][col] - pixel[row - 2][col - 2]) +
3171                 ABS(pixel[row][col - 2] - pixel[row - 2][col - 2]);
3172           sharp = val < 4 ? 0 : val < 8 ? 1 : val < 16 ? 2 : val < 32 ? 3 : val < 48 ? 4 : 5;
3173         }
3174         val = ((pixel[row - 2][col] + pixel[row][col - 2]) >> 1) + rstep[sharp][getbits(2)];
3175         pixel[row][col] = val = LIM(val, 0, 255);
3176         if (row < 4)
3177           pixel[row - 2][col + 2] = val;
3178         if (col < 4)
3179           pixel[row + 2][col - 2] = val;
3180       }
3181     }
3182   for (row = 2; row < height + 2; row++)
3183   {
3184 #ifdef LIBRAW_LIBRARY_BUILD
3185     checkCancel();
3186 #endif
3187     for (col = 3 - (row & 1); col < width + 2; col += 2)
3188     {
3189       val = ((pixel[row][col - 1] + (pixel[row][col] << 2) + pixel[row][col + 1]) >> 1) - 0x100;
3190       pixel[row][col] = LIM(val, 0, 255);
3191     }
3192   }
3193   for (row = 0; row < height; row++)
3194   {
3195 #ifdef LIBRAW_LIBRARY_BUILD
3196     checkCancel();
3197 #endif
3198     for (col = 0; col < width; col++)
3199       RAW(row, col) = t_curve[pixel[row + 2][col + 2]];
3200   }
3201   maximum = 0x3ff;
3202 }
3203 
3204 #define radc_token(tree) ((signed char)getbithuff(8, huff[tree]))
3205 
3206 #define FORYX                                                                                                          \
3207   for (y = 1; y < 3; y++)                                                                                              \
3208     for (x = col + 1; x >= col; x--)
3209 
3210 #define PREDICTOR                                                                                                      \
3211   (c ? (buf[c][y - 1][x] + buf[c][y][x + 1]) / 2 : (buf[c][y - 1][x + 1] + 2 * buf[c][y - 1][x] + buf[c][y][x + 1]) / 4)
3212 
3213 #ifdef __GNUC__
3214 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
3215 #pragma GCC optimize("no-aggressive-loop-optimizations")
3216 #endif
3217 #endif
3218 
kodak_radc_load_raw()3219 void CLASS kodak_radc_load_raw()
3220 {
3221 #ifdef LIBRAW_LIBRARY_BUILD
3222   // All kodak radc images are 768x512
3223   if (width > 768 || raw_width > 768 || height > 512 || raw_height > 512)
3224     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3225 #endif
3226   static const signed char src[] = {
3227       1, 1,   2, 3,   3, 4,   4, 2,   5, 7,   6, 5,   7, 6,  7, 8,   1, 0,   2, 1,  3, 3,  4, 4,  5, 2,   6, 7,   7, 6,
3228       8, 5,   8, 8,   2, 1,   2, 3,   3, 0,   3, 2,   3, 4,  4, 6,   5, 5,   6, 7,  6, 8,  2, 0,  2, 1,   2, 3,   3, 2,
3229       4, 4,   5, 6,   6, 7,   7, 5,   7, 8,   2, 1,   2, 4,  3, 0,   3, 2,   3, 3,  4, 7,  5, 5,  6, 6,   6, 8,   2, 3,
3230       3, 1,   3, 2,   3, 4,   3, 5,   3, 6,   4, 7,   5, 0,  5, 8,   2, 3,   2, 6,  3, 0,  3, 1,  4, 4,   4, 5,   4, 7,
3231       5, 2,   5, 8,   2, 4,   2, 7,   3, 3,   3, 6,   4, 1,  4, 2,   4, 5,   5, 0,  5, 8,  2, 6,  3, 1,   3, 3,   3, 5,
3232       3, 7,   3, 8,   4, 0,   5, 2,   5, 4,   2, 0,   2, 1,  3, 2,   3, 3,   4, 4,  4, 5,  5, 6,  5, 7,   4, 8,   1, 0,
3233       2, 2,   2, -2,  1, -3,  1, 3,   2, -17, 2, -5,  2, 5,  2, 17,  2, -7,  2, 2,  2, 9,  2, 18, 2, -18, 2, -9,  2, -2,
3234       2, 7,   2, -28, 2, 28,  3, -49, 3, -9,  3, 9,   4, 49, 5, -79, 5, 79,  2, -1, 2, 13, 2, 26, 3, 39,  4, -16, 5, 55,
3235       6, -37, 6, 76,  2, -26, 2, -13, 2, 1,   3, -39, 4, 16, 5, -55, 6, -76, 6, 37};
3236   ushort huff[19][256];
3237   int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
3238   short last[3] = {16, 16, 16}, mul[3], buf[3][3][386];
3239   static const ushort pt[] = {0, 0, 1280, 1344, 2320, 3616, 3328, 8000, 4095, 16383, 65535, 16383};
3240 
3241   for (i = 2; i < 12; i += 2)
3242     for (c = pt[i - 2]; c <= pt[i]; c++)
3243       curve[c] = (float)(c - pt[i - 2]) / (pt[i] - pt[i - 2]) * (pt[i + 1] - pt[i - 1]) + pt[i - 1] + 0.5;
3244   for (s = i = 0; i < sizeof src; i += 2)
3245     FORC(256 >> src[i])
3246   ((ushort *)huff)[s++] = src[i] << 8 | (uchar)src[i + 1];
3247   s = kodak_cbpp == 243 ? 2 : 3;
3248   FORC(256) huff[18][c] = (8 - s) << 8 | c >> s << s | 1 << (s - 1);
3249   getbits(-1);
3250   for (i = 0; i < sizeof(buf) / sizeof(short); i++)
3251     ((short *)buf)[i] = 2048;
3252   for (row = 0; row < height; row += 4)
3253   {
3254 #ifdef LIBRAW_LIBRARY_BUILD
3255     checkCancel();
3256 #endif
3257     FORC3 mul[c] = getbits(6);
3258 #ifdef LIBRAW_LIBRARY_BUILD
3259     if (!mul[0] || !mul[1] || !mul[2])
3260       throw LIBRAW_EXCEPTION_IO_CORRUPT;
3261 #endif
3262     FORC3
3263     {
3264       val = ((0x1000000 / last[c] + 0x7ff) >> 12) * mul[c];
3265       s = val > 65564 ? 10 : 12;
3266       x = ~((~0u) << (s - 1));
3267       val <<= 12 - s;
3268       for (i = 0; i < sizeof(buf[0]) / sizeof(short); i++)
3269         ((short *)buf[c])[i] = (((short *)buf[c])[i] * val + x) >> s;
3270       last[c] = mul[c];
3271       for (r = 0; r <= !c; r++)
3272       {
3273         buf[c][1][width / 2] = buf[c][2][width / 2] = mul[c] << 7;
3274         for (tree = 1, col = width / 2; col > 0;)
3275         {
3276           if ((tree = radc_token(tree)))
3277           {
3278             col -= 2;
3279 	    if(col>=0)
3280 	    {
3281             if (tree == 8)
3282               FORYX buf[c][y][x] = (uchar)radc_token(18) * mul[c];
3283             else
3284               FORYX buf[c][y][x] = radc_token(tree + 10) * 16 + PREDICTOR;
3285 	    }
3286           }
3287           else
3288             do
3289             {
3290               nreps = (col > 2) ? radc_token(9) + 1 : 1;
3291               for (rep = 0; rep < 8 && rep < nreps && col > 0; rep++)
3292               {
3293                 col -= 2;
3294 		if(col>=0)
3295                 FORYX buf[c][y][x] = PREDICTOR;
3296                 if (rep & 1)
3297                 {
3298                   step = radc_token(10) << 4;
3299                   FORYX buf[c][y][x] += step;
3300                 }
3301               }
3302             } while (nreps == 9);
3303         }
3304         for (y = 0; y < 2; y++)
3305           for (x = 0; x < width / 2; x++)
3306           {
3307             val = (buf[c][y + 1][x] << 4) / mul[c];
3308             if (val < 0)
3309               val = 0;
3310             if (c)
3311               RAW(row + y * 2 + c - 1, x * 2 + 2 - c) = val;
3312             else
3313               RAW(row + r * 2 + y, x * 2 + y) = val;
3314           }
3315         memcpy(buf[c][0] + !c, buf[c][2], sizeof buf[c][0] - 2 * !c);
3316       }
3317     }
3318     for (y = row; y < row + 4; y++)
3319       for (x = 0; x < width; x++)
3320         if ((x + y) & 1)
3321         {
3322           r = x ? x - 1 : x + 1;
3323           s = x + 1 < width ? x + 1 : x - 1;
3324           val = (RAW(y, x) - 2048) * 2 + (RAW(y, r) + RAW(y, s)) / 2;
3325           if (val < 0)
3326             val = 0;
3327           RAW(y, x) = val;
3328         }
3329   }
3330   for (i = 0; i < height * width; i++)
3331     raw_image[i] = curve[raw_image[i]];
3332   maximum = 0x3fff;
3333 }
3334 
3335 #undef FORYX
3336 #undef PREDICTOR
3337 
3338 #ifdef NO_JPEG
kodak_jpeg_load_raw()3339 void CLASS kodak_jpeg_load_raw() {}
lossy_dng_load_raw()3340 void CLASS lossy_dng_load_raw() {}
3341 #else
3342 
3343 #ifndef LIBRAW_LIBRARY_BUILD
3344 METHODDEF(boolean)
fill_input_buffer(j_decompress_ptr cinfo)3345 fill_input_buffer(j_decompress_ptr cinfo)
3346 {
3347   static uchar jpeg_buffer[4096];
3348   size_t nbytes;
3349 
3350   nbytes = fread(jpeg_buffer, 1, 4096, ifp);
3351   swab(jpeg_buffer, jpeg_buffer, nbytes);
3352   cinfo->src->next_input_byte = jpeg_buffer;
3353   cinfo->src->bytes_in_buffer = nbytes;
3354   return TRUE;
3355 }
kodak_jpeg_load_raw()3356 void CLASS kodak_jpeg_load_raw()
3357 {
3358   struct jpeg_decompress_struct cinfo;
3359   struct jpeg_error_mgr jerr;
3360   JSAMPARRAY buf;
3361   JSAMPLE(*pixel)[3];
3362   int row, col;
3363 
3364   cinfo.err = jpeg_std_error(&jerr);
3365   jpeg_create_decompress(&cinfo);
3366   jpeg_stdio_src(&cinfo, ifp);
3367   cinfo.src->fill_input_buffer = fill_input_buffer;
3368   jpeg_read_header(&cinfo, TRUE);
3369   jpeg_start_decompress(&cinfo);
3370   if ((cinfo.output_width != width) || (cinfo.output_height * 2 != height) || (cinfo.output_components != 3))
3371   {
3372     fprintf(stderr, _("%s: incorrect JPEG dimensions\n"), ifname);
3373     jpeg_destroy_decompress(&cinfo);
3374     longjmp(failure, 3);
3375   }
3376   buf = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, width * 3, 1);
3377 
3378   while (cinfo.output_scanline < cinfo.output_height)
3379   {
3380     row = cinfo.output_scanline * 2;
3381     jpeg_read_scanlines(&cinfo, buf, 1);
3382     pixel = (JSAMPLE(*)[3])buf[0];
3383     for (col = 0; col < width; col += 2)
3384     {
3385       RAW(row + 0, col + 0) = pixel[col + 0][1] << 1;
3386       RAW(row + 1, col + 1) = pixel[col + 1][1] << 1;
3387       RAW(row + 0, col + 1) = pixel[col][0] + pixel[col + 1][0];
3388       RAW(row + 1, col + 0) = pixel[col][2] + pixel[col + 1][2];
3389     }
3390   }
3391   jpeg_finish_decompress(&cinfo);
3392   jpeg_destroy_decompress(&cinfo);
3393   maximum = 0xff << 1;
3394 }
3395 #else
3396 
3397 struct jpegErrorManager
3398 {
3399   struct jpeg_error_mgr pub;
3400 };
3401 
jpegErrorExit(j_common_ptr cinfo)3402 static void jpegErrorExit(j_common_ptr cinfo)
3403 {
3404   jpegErrorManager *myerr = (jpegErrorManager *)cinfo->err;
3405   throw LIBRAW_EXCEPTION_DECODE_JPEG;
3406 }
3407 
3408 // LibRaw's Kodak_jpeg_load_raw
kodak_jpeg_load_raw()3409 void CLASS kodak_jpeg_load_raw()
3410 {
3411   if (data_size < 1)
3412     throw LIBRAW_EXCEPTION_DECODE_JPEG;
3413 
3414   int row, col;
3415   jpegErrorManager jerr;
3416   struct jpeg_decompress_struct cinfo;
3417 
3418   cinfo.err = jpeg_std_error(&jerr.pub);
3419   jerr.pub.error_exit = jpegErrorExit;
3420 
3421   unsigned char *jpg_buf = (unsigned char *)malloc(data_size);
3422   merror(jpg_buf, "kodak_jpeg_load_raw");
3423   unsigned char *pixel_buf = (unsigned char *)malloc(width * 3);
3424   jpeg_create_decompress(&cinfo);
3425   merror(pixel_buf, "kodak_jpeg_load_raw");
3426 
3427   fread(jpg_buf, data_size, 1, ifp);
3428   swab((char *)jpg_buf, (char *)jpg_buf, data_size);
3429   try
3430   {
3431     jpeg_mem_src(&cinfo, jpg_buf, data_size);
3432     int rc = jpeg_read_header(&cinfo, TRUE);
3433     if (rc != 1)
3434       throw LIBRAW_EXCEPTION_DECODE_JPEG;
3435 
3436     jpeg_start_decompress(&cinfo);
3437     if ((cinfo.output_width != width) || (cinfo.output_height * 2 != height) || (cinfo.output_components != 3))
3438     {
3439       throw LIBRAW_EXCEPTION_DECODE_JPEG;
3440     }
3441 
3442     unsigned char *buf[1];
3443     buf[0] = pixel_buf;
3444 
3445     while (cinfo.output_scanline < cinfo.output_height)
3446     {
3447       checkCancel();
3448       row = cinfo.output_scanline * 2;
3449       jpeg_read_scanlines(&cinfo, buf, 1);
3450       unsigned char(*pixel)[3] = (unsigned char(*)[3])buf[0];
3451       for (col = 0; col < width; col += 2)
3452       {
3453         RAW(row + 0, col + 0) = pixel[col + 0][1] << 1;
3454         RAW(row + 1, col + 1) = pixel[col + 1][1] << 1;
3455         RAW(row + 0, col + 1) = pixel[col][0] + pixel[col + 1][0];
3456         RAW(row + 1, col + 0) = pixel[col][2] + pixel[col + 1][2];
3457       }
3458     }
3459   }
3460   catch (...)
3461   {
3462     jpeg_finish_decompress(&cinfo);
3463     jpeg_destroy_decompress(&cinfo);
3464     free(jpg_buf);
3465     free(pixel_buf);
3466     throw;
3467   }
3468   jpeg_finish_decompress(&cinfo);
3469   jpeg_destroy_decompress(&cinfo);
3470   free(jpg_buf);
3471   free(pixel_buf);
3472   maximum = 0xff << 1;
3473 }
3474 #endif
3475 
3476 #ifndef LIBRAW_LIBRARY_BUILD
3477 void CLASS gamma_curve(double pwr, double ts, int mode, int imax);
3478 #endif
3479 
lossy_dng_load_raw()3480 void CLASS lossy_dng_load_raw()
3481 {
3482 #ifdef LIBRAW_LIBRARY_BUILD
3483   if (!image)
3484     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3485 #endif
3486   struct jpeg_decompress_struct cinfo;
3487   struct jpeg_error_mgr jerr;
3488   JSAMPARRAY buf;
3489   JSAMPLE(*pixel)[3];
3490   unsigned sorder = order, ntags, opcode, deg, i, j, c;
3491   unsigned save = data_offset - 4, trow = 0, tcol = 0, row, col;
3492   ushort cur[3][256];
3493   double coeff[9], tot;
3494 
3495   if (meta_offset)
3496   {
3497     fseek(ifp, meta_offset, SEEK_SET);
3498     order = 0x4d4d;
3499     ntags = get4();
3500     while (ntags--)
3501     {
3502       opcode = get4();
3503       get4();
3504       get4();
3505       if (opcode != 8)
3506       {
3507         fseek(ifp, get4(), SEEK_CUR);
3508         continue;
3509       }
3510       fseek(ifp, 20, SEEK_CUR);
3511       if ((c = get4()) > 2)
3512         break;
3513       fseek(ifp, 12, SEEK_CUR);
3514       if ((deg = get4()) > 8)
3515         break;
3516       for (i = 0; i <= deg && i < 9; i++)
3517         coeff[i] = getreal(12);
3518       for (i = 0; i < 256; i++)
3519       {
3520         for (tot = j = 0; j <= deg; j++)
3521           tot += coeff[j] * pow(i / 255.0, (int)j);
3522         cur[c][i] = tot * 0xffff;
3523       }
3524     }
3525     order = sorder;
3526   }
3527   else
3528   {
3529     gamma_curve(1 / 2.4, 12.92, 1, 255);
3530     FORC3 memcpy(cur[c], curve, sizeof cur[0]);
3531   }
3532   cinfo.err = jpeg_std_error(&jerr);
3533   jpeg_create_decompress(&cinfo);
3534   while (trow < raw_height)
3535   {
3536     fseek(ifp, save += 4, SEEK_SET);
3537     if (tile_length < INT_MAX)
3538       fseek(ifp, get4(), SEEK_SET);
3539 #ifdef LIBRAW_LIBRARY_BUILD
3540     if (libraw_internal_data.internal_data.input->jpeg_src(&cinfo) == -1)
3541     {
3542       jpeg_destroy_decompress(&cinfo);
3543       throw LIBRAW_EXCEPTION_DECODE_JPEG;
3544     }
3545 #else
3546     jpeg_stdio_src(&cinfo, ifp);
3547 #endif
3548     jpeg_read_header(&cinfo, TRUE);
3549     jpeg_start_decompress(&cinfo);
3550     buf = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, cinfo.output_width * 3, 1);
3551 #ifdef LIBRAW_LIBRARY_BUILD
3552     try
3553     {
3554 #endif
3555       while (cinfo.output_scanline < cinfo.output_height && (row = trow + cinfo.output_scanline) < height)
3556       {
3557 #ifdef LIBRAW_LIBRARY_BUILD
3558         checkCancel();
3559 #endif
3560         jpeg_read_scanlines(&cinfo, buf, 1);
3561         pixel = (JSAMPLE(*)[3])buf[0];
3562         for (col = 0; col < cinfo.output_width && tcol + col < width; col++)
3563         {
3564           FORC3 image[row * width + tcol + col][c] = cur[c][pixel[col][c]];
3565         }
3566       }
3567 #ifdef LIBRAW_LIBRARY_BUILD
3568     }
3569     catch (...)
3570     {
3571       jpeg_destroy_decompress(&cinfo);
3572       throw;
3573     }
3574 #endif
3575     jpeg_abort_decompress(&cinfo);
3576     if ((tcol += tile_width) >= raw_width)
3577       trow += tile_length + (tcol = 0);
3578   }
3579   jpeg_destroy_decompress(&cinfo);
3580   maximum = 0xffff;
3581 }
3582 #endif
3583 
kodak_dc120_load_raw()3584 void CLASS kodak_dc120_load_raw()
3585 {
3586   static const int mul[4] = {162, 192, 187, 92};
3587   static const int add[4] = {0, 636, 424, 212};
3588   uchar pixel[848];
3589   int row, shift, col;
3590 
3591   for (row = 0; row < height; row++)
3592   {
3593 #ifdef LIBRAW_LIBRARY_BUILD
3594     checkCancel();
3595 #endif
3596     if (fread(pixel, 1, 848, ifp) < 848)
3597       derror();
3598     shift = row * mul[row & 3] + add[row & 3];
3599     for (col = 0; col < width; col++)
3600       RAW(row, col) = (ushort)pixel[(col + shift) % 848];
3601   }
3602   maximum = 0xff;
3603 }
3604 
eight_bit_load_raw()3605 void CLASS eight_bit_load_raw()
3606 {
3607   uchar *pixel;
3608   unsigned row, col;
3609 
3610   pixel = (uchar *)calloc(raw_width, sizeof *pixel);
3611   merror(pixel, "eight_bit_load_raw()");
3612 #ifdef LIBRAW_LIBRARY_BUILD
3613   try
3614   {
3615 #endif
3616     for (row = 0; row < raw_height; row++)
3617     {
3618 #ifdef LIBRAW_LIBRARY_BUILD
3619       checkCancel();
3620 #endif
3621       if (fread(pixel, 1, raw_width, ifp) < raw_width)
3622         derror();
3623       for (col = 0; col < raw_width; col++)
3624         RAW(row, col) = curve[pixel[col]];
3625     }
3626 #ifdef LIBRAW_LIBRARY_BUILD
3627   }
3628   catch (...)
3629   {
3630     free(pixel);
3631     throw;
3632   }
3633 #endif
3634   free(pixel);
3635   maximum = curve[0xff];
3636 }
3637 
kodak_c330_load_raw()3638 void CLASS kodak_c330_load_raw()
3639 {
3640 #ifdef LIBRAW_LIBRARY_BUILD
3641   if (!image)
3642     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3643 #endif
3644   uchar *pixel;
3645   int row, col, y, cb, cr, rgb[3], c;
3646 
3647   pixel = (uchar *)calloc(raw_width, 2 * sizeof *pixel);
3648   merror(pixel, "kodak_c330_load_raw()");
3649 #ifdef LIBRAW_LIBRARY_BUILD
3650   try
3651   {
3652 #endif
3653     for (row = 0; row < height; row++)
3654     {
3655 #ifdef LIBRAW_LIBRARY_BUILD
3656       checkCancel();
3657 #endif
3658       if (fread(pixel, raw_width, 2, ifp) < 2)
3659         derror();
3660       if (load_flags && (row & 31) == 31)
3661         fseek(ifp, raw_width * 32, SEEK_CUR);
3662       for (col = 0; col < width; col++)
3663       {
3664         y = pixel[col * 2];
3665         cb = pixel[(col * 2 & -4) | 1] - 128;
3666         cr = pixel[(col * 2 & -4) | 3] - 128;
3667         rgb[1] = y - ((cb + cr + 2) >> 2);
3668         rgb[2] = rgb[1] + cb;
3669         rgb[0] = rgb[1] + cr;
3670         FORC3 image[row * width + col][c] = curve[LIM(rgb[c], 0, 255)];
3671       }
3672     }
3673 #ifdef LIBRAW_LIBRARY_BUILD
3674   }
3675   catch (...)
3676   {
3677     free(pixel);
3678     throw;
3679   }
3680 #endif
3681   free(pixel);
3682   maximum = curve[0xff];
3683 }
3684 
kodak_c603_load_raw()3685 void CLASS kodak_c603_load_raw()
3686 {
3687 #ifdef LIBRAW_LIBRARY_BUILD
3688   if (!image)
3689     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3690 #endif
3691   uchar *pixel;
3692   int row, col, y, cb, cr, rgb[3], c;
3693 
3694   pixel = (uchar *)calloc(raw_width, 3 * sizeof *pixel);
3695   merror(pixel, "kodak_c603_load_raw()");
3696 #ifdef LIBRAW_LIBRARY_BUILD
3697   try
3698   {
3699 #endif
3700     for (row = 0; row < height; row++)
3701     {
3702 #ifdef LIBRAW_LIBRARY_BUILD
3703       checkCancel();
3704 #endif
3705       if (~row & 1)
3706         if (fread(pixel, raw_width, 3, ifp) < 3)
3707           derror();
3708       for (col = 0; col < width; col++)
3709       {
3710         y = pixel[width * 2 * (row & 1) + col];
3711         cb = pixel[width + (col & -2)] - 128;
3712         cr = pixel[width + (col & -2) + 1] - 128;
3713         rgb[1] = y - ((cb + cr + 2) >> 2);
3714         rgb[2] = rgb[1] + cb;
3715         rgb[0] = rgb[1] + cr;
3716         FORC3 image[row * width + col][c] = curve[LIM(rgb[c], 0, 255)];
3717       }
3718     }
3719 #ifdef LIBRAW_LIBRARY_BUILD
3720   }
3721   catch (...)
3722   {
3723     free(pixel);
3724     throw;
3725   }
3726 #endif
3727   free(pixel);
3728   maximum = curve[0xff];
3729 }
3730 
kodak_262_load_raw()3731 void CLASS kodak_262_load_raw()
3732 {
3733   static const uchar kodak_tree[2][26] = {
3734       {0, 1, 5, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
3735       {0, 3, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
3736   ushort *huff[2];
3737   uchar *pixel;
3738   int *strip, ns, c, row, col, chess, pi = 0, pi1, pi2, pred, val;
3739 
3740   FORC(2) huff[c] = make_decoder(kodak_tree[c]);
3741   ns = (raw_height + 63) >> 5;
3742   pixel = (uchar *)malloc(raw_width * 32 + ns * 4);
3743   merror(pixel, "kodak_262_load_raw()");
3744   strip = (int *)(pixel + raw_width * 32);
3745   order = 0x4d4d;
3746   FORC(ns) strip[c] = get4();
3747 #ifdef LIBRAW_LIBRARY_BUILD
3748   try
3749   {
3750 #endif
3751     for (row = 0; row < raw_height; row++)
3752     {
3753 #ifdef LIBRAW_LIBRARY_BUILD
3754       checkCancel();
3755 #endif
3756       if ((row & 31) == 0)
3757       {
3758         fseek(ifp, strip[row >> 5], SEEK_SET);
3759         getbits(-1);
3760         pi = 0;
3761       }
3762       for (col = 0; col < raw_width; col++)
3763       {
3764         chess = (row + col) & 1;
3765         pi1 = chess ? pi - 2 : pi - raw_width - 1;
3766         pi2 = chess ? pi - 2 * raw_width : pi - raw_width + 1;
3767         if (col <= chess)
3768           pi1 = -1;
3769         if (pi1 < 0)
3770           pi1 = pi2;
3771         if (pi2 < 0)
3772           pi2 = pi1;
3773         if (pi1 < 0 && col > 1)
3774           pi1 = pi2 = pi - 2;
3775         pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
3776         pixel[pi] = val = pred + ljpeg_diff(huff[chess]);
3777         if (val >> 8)
3778           derror();
3779         val = curve[pixel[pi++]];
3780         RAW(row, col) = val;
3781       }
3782     }
3783 #ifdef LIBRAW_LIBRARY_BUILD
3784   }
3785   catch (...)
3786   {
3787     free(pixel);
3788     throw;
3789   }
3790 #endif
3791   free(pixel);
3792   FORC(2) free(huff[c]);
3793 }
3794 
kodak_65000_decode(short * out,int bsize)3795 int CLASS kodak_65000_decode(short *out, int bsize)
3796 {
3797   uchar c, blen[768];
3798   ushort raw[6];
3799   INT64 bitbuf = 0;
3800   int save, bits = 0, i, j, len, diff;
3801 
3802   save = ftell(ifp);
3803   bsize = (bsize + 3) & -4;
3804   for (i = 0; i < bsize; i += 2)
3805   {
3806     c = fgetc(ifp);
3807     if ((blen[i] = c & 15) > 12 || (blen[i + 1] = c >> 4) > 12)
3808     {
3809       fseek(ifp, save, SEEK_SET);
3810       for (i = 0; i < bsize; i += 8)
3811       {
3812         read_shorts(raw, 6);
3813         out[i] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
3814         out[i + 1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
3815         for (j = 0; j < 6; j++)
3816           out[i + 2 + j] = raw[j] & 0xfff;
3817       }
3818       return 1;
3819     }
3820   }
3821   if ((bsize & 7) == 4)
3822   {
3823     bitbuf = fgetc(ifp) << 8;
3824     bitbuf += fgetc(ifp);
3825     bits = 16;
3826   }
3827   for (i = 0; i < bsize; i++)
3828   {
3829     len = blen[i];
3830     if (bits < len)
3831     {
3832       for (j = 0; j < 32; j += 8)
3833         bitbuf += (INT64)fgetc(ifp) << (bits + (j ^ 8));
3834       bits += 32;
3835     }
3836     diff = bitbuf & (0xffff >> (16 - len));
3837     bitbuf >>= len;
3838     bits -= len;
3839     if ((diff & (1 << (len - 1))) == 0)
3840       diff -= (1 << len) - 1;
3841     out[i] = diff;
3842   }
3843   return 0;
3844 }
3845 
kodak_65000_load_raw()3846 void CLASS kodak_65000_load_raw()
3847 {
3848   short buf[272]; /* 264 looks enough */
3849   int row, col, len, pred[2], ret, i;
3850 
3851   for (row = 0; row < height; row++)
3852   {
3853 #ifdef LIBRAW_LIBRARY_BUILD
3854     checkCancel();
3855 #endif
3856     for (col = 0; col < width; col += 256)
3857     {
3858       pred[0] = pred[1] = 0;
3859       len = MIN(256, width - col);
3860       ret = kodak_65000_decode(buf, len);
3861       for (i = 0; i < len; i++)
3862       {
3863         int idx = ret ? buf[i] : (pred[i & 1] += buf[i]);
3864         if (idx >= 0 && idx < 0xffff)
3865         {
3866           if ((RAW(row, col + i) = curve[idx]) >> 12)
3867             derror();
3868         }
3869         else
3870           derror();
3871       }
3872     }
3873   }
3874 }
3875 
kodak_ycbcr_load_raw()3876 void CLASS kodak_ycbcr_load_raw()
3877 {
3878 #ifdef LIBRAW_LIBRARY_BUILD
3879   if (!image)
3880     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3881 #endif
3882   short buf[384], *bp;
3883   int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
3884   ushort *ip;
3885 
3886   unsigned int bits = (load_flags && load_flags > 9 && load_flags < 17) ? load_flags : 10;
3887   for (row = 0; row < height; row += 2)
3888   {
3889 #ifdef LIBRAW_LIBRARY_BUILD
3890     checkCancel();
3891 #endif
3892     for (col = 0; col < width; col += 128)
3893     {
3894       len = MIN(128, width - col);
3895       kodak_65000_decode(buf, len * 3);
3896       y[0][1] = y[1][1] = cb = cr = 0;
3897       for (bp = buf, i = 0; i < len; i += 2, bp += 2)
3898       {
3899         cb += bp[4];
3900         cr += bp[5];
3901         rgb[1] = -((cb + cr + 2) >> 2);
3902         rgb[2] = rgb[1] + cb;
3903         rgb[0] = rgb[1] + cr;
3904         for (j = 0; j < 2; j++)
3905           for (k = 0; k < 2; k++)
3906           {
3907             if ((y[j][k] = y[j][k ^ 1] + *bp++) >> bits)
3908               derror();
3909             ip = image[(row + j) * width + col + i + k];
3910             FORC3 ip[c] = curve[LIM(y[j][k] + rgb[c], 0, 0xfff)];
3911           }
3912       }
3913     }
3914   }
3915 }
3916 
kodak_rgb_load_raw()3917 void CLASS kodak_rgb_load_raw()
3918 {
3919 #ifdef LIBRAW_LIBRARY_BUILD
3920   if (!image)
3921     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3922 #endif
3923   short buf[768], *bp;
3924   int row, col, len, c, i, rgb[3], ret;
3925   ushort *ip = image[0];
3926 
3927   for (row = 0; row < height; row++)
3928   {
3929 #ifdef LIBRAW_LIBRARY_BUILD
3930     checkCancel();
3931 #endif
3932     for (col = 0; col < width; col += 256)
3933     {
3934       len = MIN(256, width - col);
3935       ret = kodak_65000_decode(buf, len * 3);
3936       memset(rgb, 0, sizeof rgb);
3937       for (bp = buf, i = 0; i < len; i++, ip += 4)
3938 #ifdef LIBRAW_LIBRARY_BUILD
3939         if (load_flags == 12)
3940         {
3941           FORC3 ip[c] = ret ? (*bp++) : (rgb[c] += *bp++);
3942         }
3943         else
3944 #endif
3945           FORC3 if ((ip[c] = ret ? (*bp++) : (rgb[c] += *bp++)) >> 12) derror();
3946     }
3947   }
3948 }
3949 
kodak_thumb_load_raw()3950 void CLASS kodak_thumb_load_raw()
3951 {
3952 #ifdef LIBRAW_LIBRARY_BUILD
3953   if (!image)
3954     throw LIBRAW_EXCEPTION_IO_CORRUPT;
3955 #endif
3956   int row, col;
3957   colors = thumb_misc >> 5;
3958   for (row = 0; row < height; row++)
3959     for (col = 0; col < width; col++)
3960       read_shorts(image[row * width + col], colors);
3961   maximum = (1 << (thumb_misc & 31)) - 1;
3962 }
3963 
sony_decrypt(unsigned * data,int len,int start,int key)3964 void CLASS sony_decrypt(unsigned *data, int len, int start, int key)
3965 {
3966 #ifndef LIBRAW_NOTHREADS
3967 #define pad tls->sony_decrypt.pad
3968 #define p tls->sony_decrypt.p
3969 #else
3970   static unsigned pad[128], p;
3971 #endif
3972   if (start)
3973   {
3974     for (p = 0; p < 4; p++)
3975       pad[p] = key = key * 48828125 + 1;
3976     pad[3] = pad[3] << 1 | (pad[0] ^ pad[2]) >> 31;
3977     for (p = 4; p < 127; p++)
3978       pad[p] = (pad[p - 4] ^ pad[p - 2]) << 1 | (pad[p - 3] ^ pad[p - 1]) >> 31;
3979     for (p = 0; p < 127; p++)
3980       pad[p] = htonl(pad[p]);
3981   }
3982   while (len--)
3983   {
3984     *data++ ^= pad[p & 127] = pad[(p + 1) & 127] ^ pad[(p + 65) & 127];
3985     p++;
3986   }
3987 #ifndef LIBRAW_NOTHREADS
3988 #undef pad
3989 #undef p
3990 #endif
3991 }
3992 
sony_load_raw()3993 void CLASS sony_load_raw()
3994 {
3995   uchar head[40];
3996   ushort *pixel;
3997   unsigned i, key, row, col;
3998 
3999   fseek(ifp, 200896, SEEK_SET);
4000   fseek(ifp, (unsigned)fgetc(ifp) * 4 - 1, SEEK_CUR);
4001   order = 0x4d4d;
4002   key = get4();
4003   fseek(ifp, 164600, SEEK_SET);
4004   fread(head, 1, 40, ifp);
4005   sony_decrypt((unsigned *)head, 10, 1, key);
4006   for (i = 26; i-- > 22;)
4007     key = key << 8 | head[i];
4008   fseek(ifp, data_offset, SEEK_SET);
4009   for (row = 0; row < raw_height; row++)
4010   {
4011 #ifdef LIBRAW_LIBRARY_BUILD
4012     checkCancel();
4013 #endif
4014     pixel = raw_image + row * raw_width;
4015     if (fread(pixel, 2, raw_width, ifp) < raw_width)
4016       derror();
4017     sony_decrypt((unsigned *)pixel, raw_width / 2, !row, key);
4018     for (col = 0; col < raw_width; col++)
4019       if ((pixel[col] = ntohs(pixel[col])) >> 14)
4020         derror();
4021   }
4022   maximum = 0x3ff0;
4023 }
4024 
sony_arw_load_raw()4025 void CLASS sony_arw_load_raw()
4026 {
4027   ushort huff[32770];
4028   static const ushort tab[18] = {0xf11, 0xf10, 0xe0f, 0xd0e, 0xc0d, 0xb0c, 0xa0b, 0x90a, 0x809,
4029                                  0x708, 0x607, 0x506, 0x405, 0x304, 0x303, 0x300, 0x202, 0x201};
4030   int i, c, n, col, row, sum = 0;
4031 
4032   huff[0] = 15;
4033   for (n = i = 0; i < 18; i++)
4034     FORC(32768 >> (tab[i] >> 8)) huff[++n] = tab[i];
4035   getbits(-1);
4036   for (col = raw_width; col--;)
4037   {
4038 #ifdef LIBRAW_LIBRARY_BUILD
4039     checkCancel();
4040 #endif
4041     for (row = 0; row < raw_height + 1; row += 2)
4042     {
4043       if (row == raw_height)
4044         row = 1;
4045       if ((sum += ljpeg_diff(huff)) >> 12)
4046         derror();
4047       if (row < height)
4048         RAW(row, col) = sum;
4049     }
4050   }
4051 }
4052 
sony_arw2_load_raw()4053 void CLASS sony_arw2_load_raw()
4054 {
4055   uchar *data, *dp;
4056   ushort pix[16];
4057   int row, col, val, max, min, imax, imin, sh, bit, i;
4058 
4059   data = (uchar *)malloc(raw_width + 1);
4060   merror(data, "sony_arw2_load_raw()");
4061 #ifdef LIBRAW_LIBRARY_BUILD
4062   try
4063   {
4064 #endif
4065     for (row = 0; row < height; row++)
4066     {
4067 #ifdef LIBRAW_LIBRARY_BUILD
4068       checkCancel();
4069 #endif
4070       fread(data, 1, raw_width, ifp);
4071       for (dp = data, col = 0; col < raw_width - 30; dp += 16)
4072       {
4073         max = 0x7ff & (val = sget4(dp));
4074         min = 0x7ff & val >> 11;
4075         imax = 0x0f & val >> 22;
4076         imin = 0x0f & val >> 26;
4077         for (sh = 0; sh < 4 && 0x80 << sh <= max - min; sh++)
4078           ;
4079 #ifdef LIBRAW_LIBRARY_BUILD
4080         /* flag checks if outside of loop */
4081         if (!(imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SONYARW2_ALLFLAGS) // no flag set
4082             || (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SONYARW2_DELTATOVALUE))
4083         {
4084           for (bit = 30, i = 0; i < 16; i++)
4085             if (i == imax)
4086               pix[i] = max;
4087             else if (i == imin)
4088               pix[i] = min;
4089             else
4090             {
4091               pix[i] = ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
4092               if (pix[i] > 0x7ff)
4093                 pix[i] = 0x7ff;
4094               bit += 7;
4095             }
4096         }
4097         else if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SONYARW2_BASEONLY)
4098         {
4099           for (bit = 30, i = 0; i < 16; i++)
4100             if (i == imax)
4101               pix[i] = max;
4102             else if (i == imin)
4103               pix[i] = min;
4104             else
4105               pix[i] = 0;
4106         }
4107         else if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SONYARW2_DELTAONLY)
4108         {
4109           for (bit = 30, i = 0; i < 16; i++)
4110             if (i == imax)
4111               pix[i] = 0;
4112             else if (i == imin)
4113               pix[i] = 0;
4114             else
4115             {
4116               pix[i] = ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
4117               if (pix[i] > 0x7ff)
4118                 pix[i] = 0x7ff;
4119               bit += 7;
4120             }
4121         }
4122         else if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SONYARW2_DELTAZEROBASE)
4123         {
4124           for (bit = 30, i = 0; i < 16; i++)
4125             if (i == imax)
4126               pix[i] = 0;
4127             else if (i == imin)
4128               pix[i] = 0;
4129             else
4130             {
4131               pix[i] = ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh);
4132               if (pix[i] > 0x7ff)
4133                 pix[i] = 0x7ff;
4134               bit += 7;
4135             }
4136         }
4137 #else
4138       /* unaltered dcraw processing */
4139       for (bit = 30, i = 0; i < 16; i++)
4140         if (i == imax)
4141           pix[i] = max;
4142         else if (i == imin)
4143           pix[i] = min;
4144         else
4145         {
4146           pix[i] = ((sget2(dp + (bit >> 3)) >> (bit & 7) & 0x7f) << sh) + min;
4147           if (pix[i] > 0x7ff)
4148             pix[i] = 0x7ff;
4149           bit += 7;
4150         }
4151 #endif
4152 
4153 #ifdef LIBRAW_LIBRARY_BUILD
4154         if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SONYARW2_DELTATOVALUE)
4155         {
4156           for (i = 0; i < 16; i++, col += 2)
4157           {
4158             unsigned slope = pix[i] < 1001 ? 2 : curve[pix[i] << 1] - curve[(pix[i] << 1) - 2];
4159             unsigned step = 1 << sh;
4160             RAW(row, col) = curve[pix[i] << 1] > black + imgdata.params.sony_arw2_posterization_thr
4161                                 ? LIM(((slope * step * 1000) / (curve[pix[i] << 1] - black)), 0, 10000)
4162                                 : 0;
4163           }
4164         }
4165         else
4166         {
4167           for (i = 0; i < 16; i++, col += 2)
4168             RAW(row, col) = curve[pix[i] << 1];
4169         }
4170 #else
4171       for (i = 0; i < 16; i++, col += 2)
4172         RAW(row, col) = curve[pix[i] << 1] >> 2;
4173 #endif
4174         col -= col & 1 ? 1 : 31;
4175       }
4176     }
4177 #ifdef LIBRAW_LIBRARY_BUILD
4178   }
4179   catch (...)
4180   {
4181     free(data);
4182     throw;
4183   }
4184   if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_SONYARW2_DELTATOVALUE)
4185     maximum = 10000;
4186 #endif
4187   free(data);
4188 }
4189 
samsung_load_raw()4190 void CLASS samsung_load_raw()
4191 {
4192   int row, col, c, i, dir, op[4], len[4];
4193 #ifdef LIBRAW_LIBRARY_BUILD
4194   if(raw_width> 32768 || raw_height > 32768)  // definitely too much for old samsung
4195     throw LIBRAW_EXCEPTION_IO_BADFILE;
4196 #endif
4197   unsigned maxpixels = raw_width*(raw_height+7);
4198 
4199   order = 0x4949;
4200   for (row = 0; row < raw_height; row++)
4201   {
4202 #ifdef LIBRAW_LIBRARY_BUILD
4203     checkCancel();
4204 #endif
4205     fseek(ifp, strip_offset + row * 4, SEEK_SET);
4206     fseek(ifp, data_offset + get4(), SEEK_SET);
4207     ph1_bits(-1);
4208     FORC4 len[c] = row < 2 ? 7 : 4;
4209     for (col = 0; col < raw_width; col += 16)
4210     {
4211       dir = ph1_bits(1);
4212       FORC4 op[c] = ph1_bits(2);
4213       FORC4 switch (op[c])
4214       {
4215       case 3:
4216         len[c] = ph1_bits(4);
4217         break;
4218       case 2:
4219         len[c]--;
4220         break;
4221       case 1:
4222         len[c]++;
4223       }
4224       for (c = 0; c < 16; c += 2)
4225       {
4226         i = len[((c & 1) << 1) | (c >> 3)];
4227 	unsigned idest = RAWINDEX(row, col + c);
4228 	unsigned isrc = (dir ? RAWINDEX(row + (~c | -2), col + c) : col ? RAWINDEX(row, col + (c | -2)) : 0);
4229 	if(idest < maxpixels && isrc < maxpixels) // less than zero is handled by unsigned conversion
4230   	RAW(row, col + c) = ((signed)ph1_bits(i) << (32 - i) >> (32 - i)) + 			                (dir ? RAW(row + (~c | -2), col + c) : col ? RAW(row, col + (c | -2)) : 128);
4231 	else
4232   	  derror();
4233         if (c == 14)
4234           c = -1;
4235       }
4236     }
4237   }
4238   for (row = 0; row < raw_height - 1; row += 2)
4239     for (col = 0; col < raw_width - 1; col += 2)
4240       SWAP(RAW(row, col + 1), RAW(row + 1, col));
4241 }
4242 
samsung2_load_raw()4243 void CLASS samsung2_load_raw()
4244 {
4245   static const ushort tab[14] = {0x304, 0x307, 0x206, 0x205, 0x403, 0x600, 0x709,
4246                                  0x80a, 0x90b, 0xa0c, 0xa0d, 0x501, 0x408, 0x402};
4247   ushort huff[1026], vpred[2][2] = {{0, 0}, {0, 0}}, hpred[2];
4248   int i, c, n, row, col, diff;
4249 
4250   huff[0] = 10;
4251   for (n = i = 0; i < 14; i++)
4252     FORC(1024 >> (tab[i] >> 8)) huff[++n] = tab[i];
4253   getbits(-1);
4254   for (row = 0; row < raw_height; row++)
4255   {
4256 #ifdef LIBRAW_LIBRARY_BUILD
4257     checkCancel();
4258 #endif
4259     for (col = 0; col < raw_width; col++)
4260     {
4261       diff = ljpeg_diff(huff);
4262       if (col < 2)
4263         hpred[col] = vpred[row & 1][col] += diff;
4264       else
4265         hpred[col & 1] += diff;
4266       RAW(row, col) = hpred[col & 1];
4267       if (hpred[col & 1] >> tiff_bps)
4268         derror();
4269     }
4270   }
4271 }
4272 
samsung3_load_raw()4273 void CLASS samsung3_load_raw()
4274 {
4275   int opt, init, mag, pmode, row, tab, col, pred, diff, i, c;
4276   ushort lent[3][2], len[4], *prow[2];
4277 
4278   order = 0x4949;
4279   fseek(ifp, 9, SEEK_CUR);
4280   opt = fgetc(ifp);
4281   init = (get2(), get2());
4282   for (row = 0; row < raw_height; row++)
4283   {
4284 #ifdef LIBRAW_LIBRARY_BUILD
4285     checkCancel();
4286 #endif
4287     fseek(ifp, (data_offset - ftell(ifp)) & 15, SEEK_CUR);
4288     ph1_bits(-1);
4289     mag = 0;
4290     pmode = 7;
4291     FORC(6)((ushort *)lent)[c] = row < 2 ? 7 : 4;
4292     prow[row & 1] = &RAW(row - 1, 1 - ((row & 1) << 1)); // green
4293     prow[~row & 1] = &RAW(row - 2, 0);                   // red and blue
4294     for (tab = 0; tab + 15 < raw_width; tab += 16)
4295     {
4296       if (~opt & 4 && !(tab & 63))
4297       {
4298         i = ph1_bits(2);
4299         mag = i < 3 ? mag - '2' + "204"[i] : ph1_bits(12);
4300       }
4301       if (opt & 2)
4302         pmode = 7 - 4 * ph1_bits(1);
4303       else if (!ph1_bits(1))
4304         pmode = ph1_bits(3);
4305       if (opt & 1 || !ph1_bits(1))
4306       {
4307         FORC4 len[c] = ph1_bits(2);
4308         FORC4
4309         {
4310           i = ((row & 1) << 1 | (c & 1)) % 3;
4311           len[c] = len[c] < 3 ? lent[i][0] - '1' + "120"[len[c]] : ph1_bits(4);
4312           lent[i][0] = lent[i][1];
4313           lent[i][1] = len[c];
4314         }
4315       }
4316       FORC(16)
4317       {
4318         col = tab + (((c & 7) << 1) ^ (c >> 3) ^ (row & 1));
4319         pred =
4320             (pmode == 7 || row < 2)
4321                 ? (tab ? RAW(row, tab - 2 + (col & 1)) : init)
4322                 : (prow[col & 1][col - '4' + "0224468"[pmode]] + prow[col & 1][col - '4' + "0244668"[pmode]] + 1) >> 1;
4323         diff = ph1_bits(i = len[c >> 2]);
4324         if (diff >> (i - 1))
4325           diff -= 1 << i;
4326         diff = diff * (mag * 2 + 1) + mag;
4327         RAW(row, col) = pred + diff;
4328       }
4329     }
4330   }
4331 }
4332 
4333 #define HOLE(row) ((holes >> (((row)-raw_height) & 7)) & 1)
4334 
4335 /* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
smal_decode_segment(unsigned seg[2][2],int holes)4336 void CLASS smal_decode_segment(unsigned seg[2][2], int holes)
4337 {
4338   uchar hist[3][13] = {{7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0},
4339                        {7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0},
4340                        {3, 3, 0, 0, 63, 47, 31, 15, 0}};
4341   int low, high = 0xff, carry = 0, nbits = 8;
4342   int pix, s, count, bin, next, i, sym[3];
4343   uchar diff, pred[] = {0, 0};
4344   ushort data = 0, range = 0;
4345 
4346   fseek(ifp, seg[0][1] + 1, SEEK_SET);
4347   getbits(-1);
4348   if (seg[1][0] > raw_width * raw_height)
4349     seg[1][0] = raw_width * raw_height;
4350   for (pix = seg[0][0]; pix < seg[1][0]; pix++)
4351   {
4352     for (s = 0; s < 3; s++)
4353     {
4354       data = data << nbits | getbits(nbits);
4355       if (carry < 0)
4356         carry = (nbits += carry + 1) < 1 ? nbits - 1 : 0;
4357       while (--nbits >= 0)
4358         if ((data >> nbits & 0xff) == 0xff)
4359           break;
4360       if (nbits > 0)
4361         data = ((data & ((1 << (nbits - 1)) - 1)) << 1) |
4362                ((data + (((data & (1 << (nbits - 1)))) << 1)) & ((~0u) << nbits));
4363       if (nbits >= 0)
4364       {
4365         data += getbits(1);
4366         carry = nbits - 8;
4367       }
4368       count = ((((data - range + 1) & 0xffff) << 2) - 1) / (high >> 4);
4369       for (bin = 0; hist[s][bin + 5] > count; bin++)
4370         ;
4371       low = hist[s][bin + 5] * (high >> 4) >> 2;
4372       if (bin)
4373         high = hist[s][bin + 4] * (high >> 4) >> 2;
4374       high -= low;
4375       for (nbits = 0; high << nbits < 128; nbits++)
4376         ;
4377       range = (range + low) << nbits;
4378       high <<= nbits;
4379       next = hist[s][1];
4380       if (++hist[s][2] > hist[s][3])
4381       {
4382         next = (next + 1) & hist[s][0];
4383         hist[s][3] = (hist[s][next + 4] - hist[s][next + 5]) >> 2;
4384         hist[s][2] = 1;
4385       }
4386       if (hist[s][hist[s][1] + 4] - hist[s][hist[s][1] + 5] > 1)
4387       {
4388         if (bin < hist[s][1])
4389           for (i = bin; i < hist[s][1]; i++)
4390             hist[s][i + 5]--;
4391         else if (next <= bin)
4392           for (i = hist[s][1]; i < bin; i++)
4393             hist[s][i + 5]++;
4394       }
4395       hist[s][1] = next;
4396       sym[s] = bin;
4397     }
4398     diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
4399     if (sym[0] & 4)
4400       diff = diff ? -diff : 0x80;
4401     if (ftell(ifp) + 12 >= seg[1][1])
4402       diff = 0;
4403 #ifdef LIBRAW_LIBRARY_BUILD
4404     if (pix >= raw_width * raw_height)
4405       throw LIBRAW_EXCEPTION_IO_CORRUPT;
4406 #endif
4407     raw_image[pix] = pred[pix & 1] += diff;
4408     if (!(pix & 1) && HOLE(pix / raw_width))
4409       pix += 2;
4410   }
4411   maximum = 0xff;
4412 }
4413 
smal_v6_load_raw()4414 void CLASS smal_v6_load_raw()
4415 {
4416   unsigned seg[2][2];
4417 
4418   fseek(ifp, 16, SEEK_SET);
4419   seg[0][0] = 0;
4420   seg[0][1] = get2();
4421   seg[1][0] = raw_width * raw_height;
4422   seg[1][1] = INT_MAX;
4423   smal_decode_segment(seg, 0);
4424 }
4425 
median4(int * p)4426 int CLASS median4(int *p)
4427 {
4428   int min, max, sum, i;
4429 
4430   min = max = sum = p[0];
4431   for (i = 1; i < 4; i++)
4432   {
4433     sum += p[i];
4434     if (min > p[i])
4435       min = p[i];
4436     if (max < p[i])
4437       max = p[i];
4438   }
4439   return (sum - min - max) >> 1;
4440 }
4441 
fill_holes(int holes)4442 void CLASS fill_holes(int holes)
4443 {
4444   int row, col, val[4];
4445 
4446   for (row = 2; row < height - 2; row++)
4447   {
4448     if (!HOLE(row))
4449       continue;
4450     for (col = 1; col < width - 1; col += 4)
4451     {
4452       val[0] = RAW(row - 1, col - 1);
4453       val[1] = RAW(row - 1, col + 1);
4454       val[2] = RAW(row + 1, col - 1);
4455       val[3] = RAW(row + 1, col + 1);
4456       RAW(row, col) = median4(val);
4457     }
4458     for (col = 2; col < width - 2; col += 4)
4459       if (HOLE(row - 2) || HOLE(row + 2))
4460         RAW(row, col) = (RAW(row, col - 2) + RAW(row, col + 2)) >> 1;
4461       else
4462       {
4463         val[0] = RAW(row, col - 2);
4464         val[1] = RAW(row, col + 2);
4465         val[2] = RAW(row - 2, col);
4466         val[3] = RAW(row + 2, col);
4467         RAW(row, col) = median4(val);
4468       }
4469   }
4470 }
4471 
smal_v9_load_raw()4472 void CLASS smal_v9_load_raw()
4473 {
4474   unsigned seg[256][2], offset, nseg, holes, i;
4475 
4476   fseek(ifp, 67, SEEK_SET);
4477   offset = get4();
4478   nseg = (uchar)fgetc(ifp);
4479   fseek(ifp, offset, SEEK_SET);
4480   for (i = 0; i < nseg * 2; i++)
4481     ((unsigned *)seg)[i] = get4() + data_offset * (i & 1);
4482   fseek(ifp, 78, SEEK_SET);
4483   holes = fgetc(ifp);
4484   fseek(ifp, 88, SEEK_SET);
4485   seg[nseg][0] = raw_height * raw_width;
4486   seg[nseg][1] = get4() + data_offset;
4487   for (i = 0; i < nseg; i++)
4488     smal_decode_segment(seg + i, holes);
4489   if (holes)
4490     fill_holes(holes);
4491 }
4492 
redcine_load_raw()4493 void CLASS redcine_load_raw()
4494 {
4495 #ifndef NO_JASPER
4496   int c, row, col;
4497   jas_stream_t *in;
4498   jas_image_t *jimg;
4499   jas_matrix_t *jmat;
4500   jas_seqent_t *data;
4501   ushort *img, *pix;
4502 
4503   jas_init();
4504 #ifndef LIBRAW_LIBRARY_BUILD
4505   in = jas_stream_fopen(ifname, "rb");
4506 #else
4507   in = (jas_stream_t *)ifp->make_jas_stream();
4508   if (!in)
4509     throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
4510 #endif
4511   jas_stream_seek(in, data_offset + 20, SEEK_SET);
4512   jimg = jas_image_decode(in, -1, 0);
4513 #ifndef LIBRAW_LIBRARY_BUILD
4514   if (!jimg)
4515     longjmp(failure, 3);
4516 #else
4517   if (!jimg)
4518   {
4519     jas_stream_close(in);
4520     throw LIBRAW_EXCEPTION_DECODE_JPEG2000;
4521   }
4522 #endif
4523   jmat = jas_matrix_create(height / 2, width / 2);
4524   merror(jmat, "redcine_load_raw()");
4525   img = (ushort *)calloc((height + 2), (width + 2) * 2);
4526   merror(img, "redcine_load_raw()");
4527 #ifdef LIBRAW_LIBRARY_BUILD
4528   bool fastexitflag = false;
4529   try
4530   {
4531 #endif
4532     FORC4
4533     {
4534 #ifdef LIBRAW_LIBRARY_BUILD
4535       checkCancel();
4536 #endif
4537       jas_image_readcmpt(jimg, c, 0, 0, width / 2, height / 2, jmat);
4538       data = jas_matrix_getref(jmat, 0, 0);
4539       for (row = c >> 1; row < height; row += 2)
4540         for (col = c & 1; col < width; col += 2)
4541           img[(row + 1) * (width + 2) + col + 1] = data[(row / 2) * (width / 2) + col / 2];
4542     }
4543     for (col = 1; col <= width; col++)
4544     {
4545       img[col] = img[2 * (width + 2) + col];
4546       img[(height + 1) * (width + 2) + col] = img[(height - 1) * (width + 2) + col];
4547     }
4548     for (row = 0; row < height + 2; row++)
4549     {
4550       img[row * (width + 2)] = img[row * (width + 2) + 2];
4551       img[(row + 1) * (width + 2) - 1] = img[(row + 1) * (width + 2) - 3];
4552     }
4553     for (row = 1; row <= height; row++)
4554     {
4555 #ifdef LIBRAW_LIBRARY_BUILD
4556       checkCancel();
4557 #endif
4558       pix = img + row * (width + 2) + (col = 1 + (FC(row, 1) & 1));
4559       for (; col <= width; col += 2, pix += 2)
4560       {
4561         c = (((pix[0] - 0x800) << 3) + pix[-(width + 2)] + pix[width + 2] + pix[-1] + pix[1]) >> 2;
4562         pix[0] = LIM(c, 0, 4095);
4563       }
4564     }
4565     for (row = 0; row < height; row++)
4566     {
4567 #ifdef LIBRAW_LIBRARY_BUILD
4568       checkCancel();
4569 #endif
4570       for (col = 0; col < width; col++)
4571         RAW(row, col) = curve[img[(row + 1) * (width + 2) + col + 1]];
4572     }
4573 #ifdef LIBRAW_LIBRARY_BUILD
4574   }
4575   catch (...)
4576   {
4577     fastexitflag = true;
4578   }
4579 #endif
4580   free(img);
4581   jas_matrix_destroy(jmat);
4582   jas_image_destroy(jimg);
4583   jas_stream_close(in);
4584 #ifdef LIBRAW_LIBRARY_BUILD
4585   if (fastexitflag)
4586     throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;
4587 #endif
4588 #endif
4589 }
crop_masked_pixels()4590 void CLASS crop_masked_pixels()
4591 {
4592   int row, col;
4593   unsigned
4594 #ifndef LIBRAW_LIBRARY_BUILD
4595       r,
4596       raw_pitch = raw_width * 2, c, m, mblack[8], zero, val;
4597 #else
4598       c,
4599       m, zero, val;
4600 #define mblack imgdata.color.black_stat
4601 #endif
4602 
4603 #ifndef LIBRAW_LIBRARY_BUILD
4604   if (load_raw == &CLASS phase_one_load_raw || load_raw == &CLASS phase_one_load_raw_c)
4605     phase_one_correct();
4606   if (fuji_width)
4607   {
4608     for (row = 0; row < raw_height - top_margin * 2; row++)
4609     {
4610       for (col = 0; col < fuji_width << !fuji_layout; col++)
4611       {
4612         if (fuji_layout)
4613         {
4614           r = fuji_width - 1 - col + (row >> 1);
4615           c = col + ((row + 1) >> 1);
4616         }
4617         else
4618         {
4619           r = fuji_width - 1 + row - (col >> 1);
4620           c = row + ((col + 1) >> 1);
4621         }
4622         if (r < height && c < width)
4623           BAYER(r, c) = RAW(row + top_margin, col + left_margin);
4624       }
4625     }
4626   }
4627   else
4628   {
4629     for (row = 0; row < height; row++)
4630       for (col = 0; col < width; col++)
4631         BAYER2(row, col) = RAW(row + top_margin, col + left_margin);
4632   }
4633 #endif
4634   if (mask[0][3] > 0)
4635     goto mask_set;
4636   if (load_raw == &CLASS canon_load_raw || load_raw == &CLASS lossless_jpeg_load_raw)
4637   {
4638     mask[0][1] = mask[1][1] += 2;
4639     mask[0][3] -= 2;
4640     goto sides;
4641   }
4642   if (load_raw == &CLASS canon_600_load_raw || load_raw == &CLASS sony_load_raw ||
4643       (load_raw == &CLASS eight_bit_load_raw && strncmp(model, "DC2", 3)) || load_raw == &CLASS kodak_262_load_raw ||
4644       (load_raw == &CLASS packed_load_raw && (load_flags & 32)))
4645   {
4646   sides:
4647     mask[0][0] = mask[1][0] = top_margin;
4648     mask[0][2] = mask[1][2] = top_margin + height;
4649     mask[0][3] += left_margin;
4650     mask[1][1] += left_margin + width;
4651     mask[1][3] += raw_width;
4652   }
4653   if (load_raw == &CLASS nokia_load_raw)
4654   {
4655     mask[0][2] = top_margin;
4656     mask[0][3] = width;
4657   }
4658 #ifdef LIBRAW_LIBRARY_BUILD
4659   if (load_raw == &CLASS broadcom_load_raw)
4660   {
4661     mask[0][2] = top_margin;
4662     mask[0][3] = width;
4663   }
4664 #endif
4665 mask_set:
4666   memset(mblack, 0, sizeof mblack);
4667   for (zero = m = 0; m < 8; m++)
4668     for (row = MAX(mask[m][0], 0); row < MIN(mask[m][2], raw_height); row++)
4669       for (col = MAX(mask[m][1], 0); col < MIN(mask[m][3], raw_width); col++)
4670       {
4671         c = FC(row - top_margin, col - left_margin);
4672         mblack[c] += val = raw_image[(row)*raw_pitch / 2 + (col)];
4673         mblack[4 + c]++;
4674         zero += !val;
4675       }
4676   if (load_raw == &CLASS canon_600_load_raw && width < raw_width)
4677   {
4678     black = (mblack[0] + mblack[1] + mblack[2] + mblack[3]) / (mblack[4] + mblack[5] + mblack[6] + mblack[7]) - 4;
4679 #ifndef LIBRAW_LIBRARY_BUILD
4680     canon_600_correct();
4681 #endif
4682   }
4683   else if (zero < mblack[4] && mblack[5] && mblack[6] && mblack[7])
4684   {
4685     FORC4 cblack[c] = mblack[c] / mblack[4 + c];
4686     black = cblack[4] = cblack[5] = cblack[6] = 0;
4687   }
4688 }
4689 #ifdef LIBRAW_LIBRARY_BUILD
4690 #undef mblack
4691 #endif
4692 
remove_zeroes()4693 void CLASS remove_zeroes()
4694 {
4695   unsigned row, col, tot, n, r, c;
4696 
4697 #ifdef LIBRAW_LIBRARY_BUILD
4698   RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES, 0, 2);
4699 #endif
4700 
4701   for (row = 0; row < height; row++)
4702     for (col = 0; col < width; col++)
4703       if (BAYER(row, col) == 0)
4704       {
4705         tot = n = 0;
4706         for (r = row - 2; r <= row + 2; r++)
4707           for (c = col - 2; c <= col + 2; c++)
4708             if (r < height && c < width && FC(r, c) == FC(row, col) && BAYER(r, c))
4709               tot += (n++, BAYER(r, c));
4710         if (n)
4711           BAYER(row, col) = tot / n;
4712       }
4713 #ifdef LIBRAW_LIBRARY_BUILD
4714   RUN_CALLBACK(LIBRAW_PROGRESS_REMOVE_ZEROES, 1, 2);
4715 #endif
4716 }
4717 
4718 static const uchar xlat[2][256] = {
4719     {0xc1, 0xbf, 0x6d, 0x0d, 0x59, 0xc5, 0x13, 0x9d, 0x83, 0x61, 0x6b, 0x4f, 0xc7, 0x7f, 0x3d, 0x3d, 0x53, 0x59, 0xe3,
4720      0xc7, 0xe9, 0x2f, 0x95, 0xa7, 0x95, 0x1f, 0xdf, 0x7f, 0x2b, 0x29, 0xc7, 0x0d, 0xdf, 0x07, 0xef, 0x71, 0x89, 0x3d,
4721      0x13, 0x3d, 0x3b, 0x13, 0xfb, 0x0d, 0x89, 0xc1, 0x65, 0x1f, 0xb3, 0x0d, 0x6b, 0x29, 0xe3, 0xfb, 0xef, 0xa3, 0x6b,
4722      0x47, 0x7f, 0x95, 0x35, 0xa7, 0x47, 0x4f, 0xc7, 0xf1, 0x59, 0x95, 0x35, 0x11, 0x29, 0x61, 0xf1, 0x3d, 0xb3, 0x2b,
4723      0x0d, 0x43, 0x89, 0xc1, 0x9d, 0x9d, 0x89, 0x65, 0xf1, 0xe9, 0xdf, 0xbf, 0x3d, 0x7f, 0x53, 0x97, 0xe5, 0xe9, 0x95,
4724      0x17, 0x1d, 0x3d, 0x8b, 0xfb, 0xc7, 0xe3, 0x67, 0xa7, 0x07, 0xf1, 0x71, 0xa7, 0x53, 0xb5, 0x29, 0x89, 0xe5, 0x2b,
4725      0xa7, 0x17, 0x29, 0xe9, 0x4f, 0xc5, 0x65, 0x6d, 0x6b, 0xef, 0x0d, 0x89, 0x49, 0x2f, 0xb3, 0x43, 0x53, 0x65, 0x1d,
4726      0x49, 0xa3, 0x13, 0x89, 0x59, 0xef, 0x6b, 0xef, 0x65, 0x1d, 0x0b, 0x59, 0x13, 0xe3, 0x4f, 0x9d, 0xb3, 0x29, 0x43,
4727      0x2b, 0x07, 0x1d, 0x95, 0x59, 0x59, 0x47, 0xfb, 0xe5, 0xe9, 0x61, 0x47, 0x2f, 0x35, 0x7f, 0x17, 0x7f, 0xef, 0x7f,
4728      0x95, 0x95, 0x71, 0xd3, 0xa3, 0x0b, 0x71, 0xa3, 0xad, 0x0b, 0x3b, 0xb5, 0xfb, 0xa3, 0xbf, 0x4f, 0x83, 0x1d, 0xad,
4729      0xe9, 0x2f, 0x71, 0x65, 0xa3, 0xe5, 0x07, 0x35, 0x3d, 0x0d, 0xb5, 0xe9, 0xe5, 0x47, 0x3b, 0x9d, 0xef, 0x35, 0xa3,
4730      0xbf, 0xb3, 0xdf, 0x53, 0xd3, 0x97, 0x53, 0x49, 0x71, 0x07, 0x35, 0x61, 0x71, 0x2f, 0x43, 0x2f, 0x11, 0xdf, 0x17,
4731      0x97, 0xfb, 0x95, 0x3b, 0x7f, 0x6b, 0xd3, 0x25, 0xbf, 0xad, 0xc7, 0xc5, 0xc5, 0xb5, 0x8b, 0xef, 0x2f, 0xd3, 0x07,
4732      0x6b, 0x25, 0x49, 0x95, 0x25, 0x49, 0x6d, 0x71, 0xc7},
4733     {0xa7, 0xbc, 0xc9, 0xad, 0x91, 0xdf, 0x85, 0xe5, 0xd4, 0x78, 0xd5, 0x17, 0x46, 0x7c, 0x29, 0x4c, 0x4d, 0x03, 0xe9,
4734      0x25, 0x68, 0x11, 0x86, 0xb3, 0xbd, 0xf7, 0x6f, 0x61, 0x22, 0xa2, 0x26, 0x34, 0x2a, 0xbe, 0x1e, 0x46, 0x14, 0x68,
4735      0x9d, 0x44, 0x18, 0xc2, 0x40, 0xf4, 0x7e, 0x5f, 0x1b, 0xad, 0x0b, 0x94, 0xb6, 0x67, 0xb4, 0x0b, 0xe1, 0xea, 0x95,
4736      0x9c, 0x66, 0xdc, 0xe7, 0x5d, 0x6c, 0x05, 0xda, 0xd5, 0xdf, 0x7a, 0xef, 0xf6, 0xdb, 0x1f, 0x82, 0x4c, 0xc0, 0x68,
4737      0x47, 0xa1, 0xbd, 0xee, 0x39, 0x50, 0x56, 0x4a, 0xdd, 0xdf, 0xa5, 0xf8, 0xc6, 0xda, 0xca, 0x90, 0xca, 0x01, 0x42,
4738      0x9d, 0x8b, 0x0c, 0x73, 0x43, 0x75, 0x05, 0x94, 0xde, 0x24, 0xb3, 0x80, 0x34, 0xe5, 0x2c, 0xdc, 0x9b, 0x3f, 0xca,
4739      0x33, 0x45, 0xd0, 0xdb, 0x5f, 0xf5, 0x52, 0xc3, 0x21, 0xda, 0xe2, 0x22, 0x72, 0x6b, 0x3e, 0xd0, 0x5b, 0xa8, 0x87,
4740      0x8c, 0x06, 0x5d, 0x0f, 0xdd, 0x09, 0x19, 0x93, 0xd0, 0xb9, 0xfc, 0x8b, 0x0f, 0x84, 0x60, 0x33, 0x1c, 0x9b, 0x45,
4741      0xf1, 0xf0, 0xa3, 0x94, 0x3a, 0x12, 0x77, 0x33, 0x4d, 0x44, 0x78, 0x28, 0x3c, 0x9e, 0xfd, 0x65, 0x57, 0x16, 0x94,
4742      0x6b, 0xfb, 0x59, 0xd0, 0xc8, 0x22, 0x36, 0xdb, 0xd2, 0x63, 0x98, 0x43, 0xa1, 0x04, 0x87, 0x86, 0xf7, 0xa6, 0x26,
4743      0xbb, 0xd6, 0x59, 0x4d, 0xbf, 0x6a, 0x2e, 0xaa, 0x2b, 0xef, 0xe6, 0x78, 0xb6, 0x4e, 0xe0, 0x2f, 0xdc, 0x7c, 0xbe,
4744      0x57, 0x19, 0x32, 0x7e, 0x2a, 0xd0, 0xb8, 0xba, 0x29, 0x00, 0x3c, 0x52, 0x7d, 0xa8, 0x49, 0x3b, 0x2d, 0xeb, 0x25,
4745      0x49, 0xfa, 0xa3, 0xaa, 0x39, 0xa7, 0xc5, 0xa7, 0x50, 0x11, 0x36, 0xfb, 0xc6, 0x67, 0x4a, 0xf5, 0xa5, 0x12, 0x65,
4746      0x7e, 0xb0, 0xdf, 0xaf, 0x4e, 0xb3, 0x61, 0x7f, 0x2f}};
4747 
gamma_curve(double pwr,double ts,int mode,int imax)4748 void CLASS gamma_curve(double pwr, double ts, int mode, int imax)
4749 {
4750   int i;
4751   double g[6], bnd[2] = {0, 0}, r;
4752 
4753   g[0] = pwr;
4754   g[1] = ts;
4755   g[2] = g[3] = g[4] = 0;
4756   bnd[g[1] >= 1] = 1;
4757   if (g[1] && (g[1] - 1) * (g[0] - 1) <= 0)
4758   {
4759     for (i = 0; i < 48; i++)
4760     {
4761       g[2] = (bnd[0] + bnd[1]) / 2;
4762       if (g[0])
4763         bnd[(pow(g[2] / g[1], -g[0]) - 1) / g[0] - 1 / g[2] > -1] = g[2];
4764       else
4765         bnd[g[2] / exp(1 - 1 / g[2]) < g[1]] = g[2];
4766     }
4767     g[3] = g[2] / g[1];
4768     if (g[0])
4769       g[4] = g[2] * (1 / g[0] - 1);
4770   }
4771   if (g[0])
4772     g[5] = 1 / (g[1] * SQR(g[3]) / 2 - g[4] * (1 - g[3]) + (1 - pow(g[3], 1 + g[0])) * (1 + g[4]) / (1 + g[0])) - 1;
4773   else
4774     g[5] = 1 / (g[1] * SQR(g[3]) / 2 + 1 - g[2] - g[3] - g[2] * g[3] * (log(g[3]) - 1)) - 1;
4775   if (!mode--)
4776   {
4777     memcpy(gamm, g, sizeof gamm);
4778     return;
4779   }
4780   for (i = 0; i < 0x10000; i++)
4781   {
4782     curve[i] = 0xffff;
4783     if ((r = (double)i / imax) < 1)
4784       curve[i] = 0x10000 *
4785                  (mode ? (r < g[3] ? r * g[1] : (g[0] ? pow(r, g[0]) * (1 + g[4]) - g[4] : log(r) * g[2] + 1))
4786                        : (r < g[2] ? r / g[1] : (g[0] ? pow((r + g[4]) / (1 + g[4]), 1 / g[0]) : exp((r - 1) / g[2]))));
4787   }
4788 }
4789 
pseudoinverse(double (* in)[3],double (* out)[3],int size)4790 void CLASS pseudoinverse(double (*in)[3], double (*out)[3], int size)
4791 {
4792   double work[3][6], num;
4793   int i, j, k;
4794 
4795   for (i = 0; i < 3; i++)
4796   {
4797     for (j = 0; j < 6; j++)
4798       work[i][j] = j == i + 3;
4799     for (j = 0; j < 3; j++)
4800       for (k = 0; k < size; k++)
4801         work[i][j] += in[k][i] * in[k][j];
4802   }
4803   for (i = 0; i < 3; i++)
4804   {
4805     num = work[i][i];
4806     for (j = 0; j < 6; j++)
4807       if(fabs(num)>0.00001f)
4808       	work[i][j] /= num;
4809     for (k = 0; k < 3; k++)
4810     {
4811       if (k == i)
4812         continue;
4813       num = work[k][i];
4814       for (j = 0; j < 6; j++)
4815         work[k][j] -= work[i][j] * num;
4816     }
4817   }
4818   for (i = 0; i < size; i++)
4819     for (j = 0; j < 3; j++)
4820       for (out[i][j] = k = 0; k < 3; k++)
4821         out[i][j] += work[j][k + 3] * in[i][k];
4822 }
4823 
cam_xyz_coeff(float _rgb_cam[3][4],double cam_xyz[4][3])4824 void CLASS cam_xyz_coeff(float _rgb_cam[3][4], double cam_xyz[4][3])
4825 {
4826   double cam_rgb[4][3], inverse[4][3], num;
4827   int i, j, k;
4828 
4829   for (i = 0; i < colors; i++) /* Multiply out XYZ colorspace */
4830     for (j = 0; j < 3; j++)
4831       for (cam_rgb[i][j] = k = 0; k < 3; k++)
4832         cam_rgb[i][j] += cam_xyz[i][k] * xyz_rgb[k][j];
4833 
4834   for (i = 0; i < colors; i++)
4835   {                               /* Normalize cam_rgb so that */
4836     for (num = j = 0; j < 3; j++) /* cam_rgb * (1,1,1) is (1,1,1,1) */
4837       num += cam_rgb[i][j];
4838     if (num > 0.00001)
4839     {
4840       for (j = 0; j < 3; j++)
4841         cam_rgb[i][j] /= num;
4842       pre_mul[i] = 1 / num;
4843     }
4844     else
4845     {
4846       for (j = 0; j < 3; j++)
4847         cam_rgb[i][j] = 0.0;
4848       pre_mul[i] = 1.0;
4849     }
4850   }
4851   pseudoinverse(cam_rgb, inverse, colors);
4852   for (i = 0; i < 3; i++)
4853     for (j = 0; j < colors; j++)
4854       _rgb_cam[i][j] = inverse[j][i];
4855 }
4856 
4857 #ifdef COLORCHECK
colorcheck()4858 void CLASS colorcheck()
4859 {
4860 #define NSQ 24
4861   // Coordinates of the GretagMacbeth ColorChecker squares
4862   // width, height, 1st_column, 1st_row
4863   int cut[NSQ][4];                                             // you must set these
4864                                                                // ColorChecker Chart under 6500-kelvin illumination
4865   static const double gmb_xyY[NSQ][3] = {{0.400, 0.350, 10.1}, // Dark Skin
4866                                          {0.377, 0.345, 35.8}, // Light Skin
4867                                          {0.247, 0.251, 19.3}, // Blue Sky
4868                                          {0.337, 0.422, 13.3}, // Foliage
4869                                          {0.265, 0.240, 24.3}, // Blue Flower
4870                                          {0.261, 0.343, 43.1}, // Bluish Green
4871                                          {0.506, 0.407, 30.1}, // Orange
4872                                          {0.211, 0.175, 12.0}, // Purplish Blue
4873                                          {0.453, 0.306, 19.8}, // Moderate Red
4874                                          {0.285, 0.202, 6.6},  // Purple
4875                                          {0.380, 0.489, 44.3}, // Yellow Green
4876                                          {0.473, 0.438, 43.1}, // Orange Yellow
4877                                          {0.187, 0.129, 6.1},  // Blue
4878                                          {0.305, 0.478, 23.4}, // Green
4879                                          {0.539, 0.313, 12.0}, // Red
4880                                          {0.448, 0.470, 59.1}, // Yellow
4881                                          {0.364, 0.233, 19.8}, // Magenta
4882                                          {0.196, 0.252, 19.8}, // Cyan
4883                                          {0.310, 0.316, 90.0}, // White
4884                                          {0.310, 0.316, 59.1}, // Neutral 8
4885                                          {0.310, 0.316, 36.2}, // Neutral 6.5
4886                                          {0.310, 0.316, 19.8}, // Neutral 5
4887                                          {0.310, 0.316, 9.0},  // Neutral 3.5
4888                                          {0.310, 0.316, 3.1}}; // Black
4889   double gmb_cam[NSQ][4], gmb_xyz[NSQ][3];
4890   double inverse[NSQ][3], cam_xyz[4][3], balance[4], num;
4891   int c, i, j, k, sq, row, col, pass, count[4];
4892 
4893   memset(gmb_cam, 0, sizeof gmb_cam);
4894   for (sq = 0; sq < NSQ; sq++)
4895   {
4896     FORCC count[c] = 0;
4897     for (row = cut[sq][3]; row < cut[sq][3] + cut[sq][1]; row++)
4898       for (col = cut[sq][2]; col < cut[sq][2] + cut[sq][0]; col++)
4899       {
4900         c = FC(row, col);
4901         if (c >= colors)
4902           c -= 2;
4903         gmb_cam[sq][c] += BAYER2(row, col);
4904         BAYER2(row, col) = black + (BAYER2(row, col) - black) / 2;
4905         count[c]++;
4906       }
4907     FORCC gmb_cam[sq][c] = gmb_cam[sq][c] / count[c] - black;
4908     gmb_xyz[sq][0] = gmb_xyY[sq][2] * gmb_xyY[sq][0] / gmb_xyY[sq][1];
4909     gmb_xyz[sq][1] = gmb_xyY[sq][2];
4910     gmb_xyz[sq][2] = gmb_xyY[sq][2] * (1 - gmb_xyY[sq][0] - gmb_xyY[sq][1]) / gmb_xyY[sq][1];
4911   }
4912   pseudoinverse(gmb_xyz, inverse, NSQ);
4913   for (pass = 0; pass < 2; pass++)
4914   {
4915     for (raw_color = i = 0; i < colors; i++)
4916       for (j = 0; j < 3; j++)
4917         for (cam_xyz[i][j] = k = 0; k < NSQ; k++)
4918           cam_xyz[i][j] += gmb_cam[k][i] * inverse[k][j];
4919     cam_xyz_coeff(rgb_cam, cam_xyz);
4920     FORCC balance[c] = pre_mul[c] * gmb_cam[20][c];
4921     for (sq = 0; sq < NSQ; sq++)
4922       FORCC gmb_cam[sq][c] *= balance[c];
4923   }
4924   if (verbose)
4925   {
4926     printf("    { \"%s %s\", %d,\n\t{", make, model, black);
4927     num = 10000 / (cam_xyz[1][0] + cam_xyz[1][1] + cam_xyz[1][2]);
4928     FORCC for (j = 0; j < 3; j++) printf("%c%d", (c | j) ? ',' : ' ', (int)(cam_xyz[c][j] * num + 0.5));
4929     puts(" } },");
4930   }
4931 #undef NSQ
4932 }
4933 #endif
4934 
hat_transform(float * temp,float * base,int st,int size,int sc)4935 void CLASS hat_transform(float *temp, float *base, int st, int size, int sc)
4936 {
4937   int i;
4938   for (i = 0; i < sc; i++)
4939     temp[i] = 2 * base[st * i] + base[st * (sc - i)] + base[st * (i + sc)];
4940   for (; i + sc < size; i++)
4941     temp[i] = 2 * base[st * i] + base[st * (i - sc)] + base[st * (i + sc)];
4942   for (; i < size; i++)
4943     temp[i] = 2 * base[st * i] + base[st * (i - sc)] + base[st * (2 * size - 2 - (i + sc))];
4944 }
4945 
4946 #if !defined(LIBRAW_USE_OPENMP)
wavelet_denoise()4947 void CLASS wavelet_denoise()
4948 {
4949   float *fimg = 0, *temp, thold, mul[2], avg, diff;
4950   int scale = 1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
4951   ushort *window[4];
4952   static const float noise[] = {0.8002, 0.2735, 0.1202, 0.0585, 0.0291, 0.0152, 0.0080, 0.0044};
4953 
4954 #ifdef DCRAW_VERBOSE
4955   if (verbose)
4956     fprintf(stderr, _("Wavelet denoising...\n"));
4957 #endif
4958 
4959   while (maximum << scale < 0x10000)
4960     scale++;
4961   maximum <<= --scale;
4962   black <<= scale;
4963   FORC4 cblack[c] <<= scale;
4964   if ((size = iheight * iwidth) < 0x15550000)
4965     fimg = (float *)malloc((size * 3 + iheight + iwidth) * sizeof *fimg);
4966   merror(fimg, "wavelet_denoise()");
4967   temp = fimg + size * 3;
4968   if ((nc = colors) == 3 && filters)
4969     nc++;
4970   FORC(nc)
4971   { /* denoise R,G1,B,G3 individually */
4972     for (i = 0; i < size; i++)
4973       fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
4974     for (hpass = lev = 0; lev < 5; lev++)
4975     {
4976       lpass = size * ((lev & 1) + 1);
4977       for (row = 0; row < iheight; row++)
4978       {
4979         hat_transform(temp, fimg + hpass + row * iwidth, 1, iwidth, 1 << lev);
4980         for (col = 0; col < iwidth; col++)
4981           fimg[lpass + row * iwidth + col] = temp[col] * 0.25;
4982       }
4983       for (col = 0; col < iwidth; col++)
4984       {
4985         hat_transform(temp, fimg + lpass + col, iwidth, iheight, 1 << lev);
4986         for (row = 0; row < iheight; row++)
4987           fimg[lpass + row * iwidth + col] = temp[row] * 0.25;
4988       }
4989       thold = threshold * noise[lev];
4990       for (i = 0; i < size; i++)
4991       {
4992         fimg[hpass + i] -= fimg[lpass + i];
4993         if (fimg[hpass + i] < -thold)
4994           fimg[hpass + i] += thold;
4995         else if (fimg[hpass + i] > thold)
4996           fimg[hpass + i] -= thold;
4997         else
4998           fimg[hpass + i] = 0;
4999         if (hpass)
5000           fimg[i] += fimg[hpass + i];
5001       }
5002       hpass = lpass;
5003     }
5004     for (i = 0; i < size; i++)
5005       image[i][c] = CLIP(SQR(fimg[i] + fimg[lpass + i]) / 0x10000);
5006   }
5007   if (filters && colors == 3)
5008   { /* pull G1 and G3 closer together */
5009     for (row = 0; row < 2; row++)
5010     {
5011       mul[row] = 0.125 * pre_mul[FC(row + 1, 0) | 1] / pre_mul[FC(row, 0) | 1];
5012       blk[row] = cblack[FC(row, 0) | 1];
5013     }
5014     for (i = 0; i < 4; i++)
5015       window[i] = (ushort *)fimg + width * i;
5016     for (wlast = -1, row = 1; row < height - 1; row++)
5017     {
5018       while (wlast < row + 1)
5019       {
5020         for (wlast++, i = 0; i < 4; i++)
5021           window[(i + 3) & 3] = window[i];
5022         for (col = FC(wlast, 1) & 1; col < width; col += 2)
5023           window[2][col] = BAYER(wlast, col);
5024       }
5025       thold = threshold / 512;
5026       for (col = (FC(row, 0) & 1) + 1; col < width - 1; col += 2)
5027       {
5028         avg = (window[0][col - 1] + window[0][col + 1] + window[2][col - 1] + window[2][col + 1] - blk[~row & 1] * 4) *
5029                   mul[row & 1] +
5030               (window[1][col] + blk[row & 1]) * 0.5;
5031         avg = avg < 0 ? 0 : sqrt(avg);
5032         diff = sqrt((double)BAYER(row, col)) - avg;
5033         if (diff < -thold)
5034           diff += thold;
5035         else if (diff > thold)
5036           diff -= thold;
5037         else
5038           diff = 0;
5039         BAYER(row, col) = CLIP(SQR(avg + diff) + 0.5);
5040       }
5041     }
5042   }
5043   free(fimg);
5044 }
5045 #else /* LIBRAW_USE_OPENMP */
wavelet_denoise()5046 void CLASS wavelet_denoise()
5047 {
5048   float *fimg = 0, *temp, thold, mul[2], avg, diff;
5049   int scale = 1, size, lev, hpass, lpass, row, col, nc, c, i, wlast, blk[2];
5050   ushort *window[4];
5051   static const float noise[] = {0.8002, 0.2735, 0.1202, 0.0585, 0.0291, 0.0152, 0.0080, 0.0044};
5052 
5053 #ifdef DCRAW_VERBOSE
5054   if (verbose)
5055     fprintf(stderr, _("Wavelet denoising...\n"));
5056 #endif
5057 
5058   while (maximum << scale < 0x10000)
5059     scale++;
5060   maximum <<= --scale;
5061   black <<= scale;
5062   FORC4 cblack[c] <<= scale;
5063   if ((size = iheight * iwidth) < 0x15550000)
5064     fimg = (float *)malloc((size * 3 + iheight + iwidth) * sizeof *fimg);
5065   merror(fimg, "wavelet_denoise()");
5066   temp = fimg + size * 3;
5067   if ((nc = colors) == 3 && filters)
5068     nc++;
5069 #ifdef LIBRAW_LIBRARY_BUILD
5070 #pragma omp parallel default(shared) private(i, col, row, thold, lev, lpass, hpass, temp, c) firstprivate(scale, size)
5071 #endif
5072   {
5073     temp = (float *)malloc((iheight + iwidth) * sizeof *fimg);
5074     FORC(nc)
5075     { /* denoise R,G1,B,G3 individually */
5076 #ifdef LIBRAW_LIBRARY_BUILD
5077 #pragma omp for
5078 #endif
5079       for (i = 0; i < size; i++)
5080         fimg[i] = 256 * sqrt((double)(image[i][c] << scale));
5081       for (hpass = lev = 0; lev < 5; lev++)
5082       {
5083         lpass = size * ((lev & 1) + 1);
5084 #ifdef LIBRAW_LIBRARY_BUILD
5085 #pragma omp for
5086 #endif
5087         for (row = 0; row < iheight; row++)
5088         {
5089           hat_transform(temp, fimg + hpass + row * iwidth, 1, iwidth, 1 << lev);
5090           for (col = 0; col < iwidth; col++)
5091             fimg[lpass + row * iwidth + col] = temp[col] * 0.25;
5092         }
5093 #ifdef LIBRAW_LIBRARY_BUILD
5094 #pragma omp for
5095 #endif
5096         for (col = 0; col < iwidth; col++)
5097         {
5098           hat_transform(temp, fimg + lpass + col, iwidth, iheight, 1 << lev);
5099           for (row = 0; row < iheight; row++)
5100             fimg[lpass + row * iwidth + col] = temp[row] * 0.25;
5101         }
5102         thold = threshold * noise[lev];
5103 #ifdef LIBRAW_LIBRARY_BUILD
5104 #pragma omp for
5105 #endif
5106         for (i = 0; i < size; i++)
5107         {
5108           fimg[hpass + i] -= fimg[lpass + i];
5109           if (fimg[hpass + i] < -thold)
5110             fimg[hpass + i] += thold;
5111           else if (fimg[hpass + i] > thold)
5112             fimg[hpass + i] -= thold;
5113           else
5114             fimg[hpass + i] = 0;
5115           if (hpass)
5116             fimg[i] += fimg[hpass + i];
5117         }
5118         hpass = lpass;
5119       }
5120 #ifdef LIBRAW_LIBRARY_BUILD
5121 #pragma omp for
5122 #endif
5123       for (i = 0; i < size; i++)
5124         image[i][c] = CLIP(SQR(fimg[i] + fimg[lpass + i]) / 0x10000);
5125     }
5126     free(temp);
5127   } /* end omp parallel */
5128   /* the following loops are hard to parallize, no idea yes,
5129    * problem is wlast which is carrying dependency
5130    * second part should be easyer, but did not yet get it right.
5131    */
5132   if (filters && colors == 3)
5133   { /* pull G1 and G3 closer together */
5134     for (row = 0; row < 2; row++)
5135     {
5136       mul[row] = 0.125 * pre_mul[FC(row + 1, 0) | 1] / pre_mul[FC(row, 0) | 1];
5137       blk[row] = cblack[FC(row, 0) | 1];
5138     }
5139     for (i = 0; i < 4; i++)
5140       window[i] = (ushort *)fimg + width * i;
5141     for (wlast = -1, row = 1; row < height - 1; row++)
5142     {
5143       while (wlast < row + 1)
5144       {
5145         for (wlast++, i = 0; i < 4; i++)
5146           window[(i + 3) & 3] = window[i];
5147         for (col = FC(wlast, 1) & 1; col < width; col += 2)
5148           window[2][col] = BAYER(wlast, col);
5149       }
5150       thold = threshold / 512;
5151       for (col = (FC(row, 0) & 1) + 1; col < width - 1; col += 2)
5152       {
5153         avg = (window[0][col - 1] + window[0][col + 1] + window[2][col - 1] + window[2][col + 1] - blk[~row & 1] * 4) *
5154                   mul[row & 1] +
5155               (window[1][col] + blk[row & 1]) * 0.5;
5156         avg = avg < 0 ? 0 : sqrt(avg);
5157         diff = sqrt((double)BAYER(row, col)) - avg;
5158         if (diff < -thold)
5159           diff += thold;
5160         else if (diff > thold)
5161           diff -= thold;
5162         else
5163           diff = 0;
5164         BAYER(row, col) = CLIP(SQR(avg + diff) + 0.5);
5165       }
5166     }
5167   }
5168   free(fimg);
5169 }
5170 
5171 #endif
5172 
5173 // green equilibration
green_matching()5174 void CLASS green_matching()
5175 {
5176   int i, j;
5177   double m1, m2, c1, c2;
5178   int o1_1, o1_2, o1_3, o1_4;
5179   int o2_1, o2_2, o2_3, o2_4;
5180   ushort(*img)[4];
5181   const int margin = 3;
5182   int oj = 2, oi = 2;
5183   float f;
5184   const float thr = 0.01f;
5185   if (half_size || shrink)
5186     return;
5187   if (FC(oj, oi) != 3)
5188     oj++;
5189   if (FC(oj, oi) != 3)
5190     oi++;
5191   if (FC(oj, oi) != 3)
5192     oj--;
5193 
5194   img = (ushort(*)[4])calloc(height * width, sizeof *image);
5195   merror(img, "green_matching()");
5196   memcpy(img, image, height * width * sizeof *image);
5197 
5198   for (j = oj; j < height - margin; j += 2)
5199     for (i = oi; i < width - margin; i += 2)
5200     {
5201       o1_1 = img[(j - 1) * width + i - 1][1];
5202       o1_2 = img[(j - 1) * width + i + 1][1];
5203       o1_3 = img[(j + 1) * width + i - 1][1];
5204       o1_4 = img[(j + 1) * width + i + 1][1];
5205       o2_1 = img[(j - 2) * width + i][3];
5206       o2_2 = img[(j + 2) * width + i][3];
5207       o2_3 = img[j * width + i - 2][3];
5208       o2_4 = img[j * width + i + 2][3];
5209 
5210       m1 = (o1_1 + o1_2 + o1_3 + o1_4) / 4.0;
5211       m2 = (o2_1 + o2_2 + o2_3 + o2_4) / 4.0;
5212 
5213       c1 = (abs(o1_1 - o1_2) + abs(o1_1 - o1_3) + abs(o1_1 - o1_4) + abs(o1_2 - o1_3) + abs(o1_3 - o1_4) +
5214             abs(o1_2 - o1_4)) /
5215            6.0;
5216       c2 = (abs(o2_1 - o2_2) + abs(o2_1 - o2_3) + abs(o2_1 - o2_4) + abs(o2_2 - o2_3) + abs(o2_3 - o2_4) +
5217             abs(o2_2 - o2_4)) /
5218            6.0;
5219       if ((img[j * width + i][3] < maximum * 0.95) && (c1 < maximum * thr) && (c2 < maximum * thr))
5220       {
5221         f = image[j * width + i][3] * m1 / m2;
5222         image[j * width + i][3] = f > 0xffff ? 0xffff : f;
5223       }
5224     }
5225   free(img);
5226 }
5227 
scale_colors()5228 void CLASS scale_colors()
5229 {
5230   unsigned bottom, right, size, row, col, ur, uc, i, x, y, c, sum[8];
5231   int val, dark, sat;
5232   double dsum[8], dmin, dmax;
5233   float scale_mul[4], fr, fc;
5234   ushort *img = 0, *pix;
5235 
5236 #ifdef LIBRAW_LIBRARY_BUILD
5237   RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS, 0, 2);
5238 #endif
5239 
5240   if (user_mul[0])
5241     memcpy(pre_mul, user_mul, sizeof pre_mul);
5242   if (use_auto_wb || (use_camera_wb && cam_mul[0] == -1))
5243   {
5244     memset(dsum, 0, sizeof dsum);
5245     bottom = MIN(greybox[1] + greybox[3], height);
5246     right = MIN(greybox[0] + greybox[2], width);
5247     for (row = greybox[1]; row < bottom; row += 8)
5248       for (col = greybox[0]; col < right; col += 8)
5249       {
5250         memset(sum, 0, sizeof sum);
5251         for (y = row; y < row + 8 && y < bottom; y++)
5252           for (x = col; x < col + 8 && x < right; x++)
5253             FORC4
5254             {
5255               if (filters)
5256               {
5257                 c = fcol(y, x);
5258                 val = BAYER2(y, x);
5259               }
5260               else
5261                 val = image[y * width + x][c];
5262               if (val > maximum - 25)
5263                 goto skip_block;
5264               if ((val -= cblack[c]) < 0)
5265                 val = 0;
5266               sum[c] += val;
5267               sum[c + 4]++;
5268               if (filters)
5269                 break;
5270             }
5271         FORC(8) dsum[c] += sum[c];
5272       skip_block:;
5273       }
5274     FORC4 if (dsum[c]) pre_mul[c] = dsum[c + 4] / dsum[c];
5275   }
5276   if (use_camera_wb && cam_mul[0] != -1)
5277   {
5278     memset(sum, 0, sizeof sum);
5279     for (row = 0; row < 8; row++)
5280       for (col = 0; col < 8; col++)
5281       {
5282         c = FC(row, col);
5283         if ((val = white[row][col] - cblack[c]) > 0)
5284           sum[c] += val;
5285         sum[c + 4]++;
5286       }
5287 #ifdef LIBRAW_LIBRARY_BUILD
5288     if (load_raw == &LibRaw::nikon_load_sraw)
5289     {
5290       // Nikon sRAW: camera WB already applied:
5291       pre_mul[0] = pre_mul[1] = pre_mul[2] = pre_mul[3] = 1.0;
5292     }
5293     else
5294 #endif
5295         if (sum[0] && sum[1] && sum[2] && sum[3])
5296       FORC4 pre_mul[c] = (float)sum[c + 4] / sum[c];
5297     else if (cam_mul[0] && cam_mul[2])
5298       memcpy(pre_mul, cam_mul, sizeof pre_mul);
5299     else
5300     {
5301 #ifdef LIBRAW_LIBRARY_BUILD
5302       imgdata.process_warnings |= LIBRAW_WARN_BAD_CAMERA_WB;
5303 #endif
5304 #ifdef DCRAW_VERBOSE
5305       fprintf(stderr, _("%s: Cannot use camera white balance.\n"), ifname);
5306 #endif
5307     }
5308   }
5309 #ifdef LIBRAW_LIBRARY_BUILD
5310   // Nikon sRAW, daylight
5311   if (load_raw == &LibRaw::nikon_load_sraw && !use_camera_wb && !use_auto_wb && cam_mul[0] > 0.001f &&
5312       cam_mul[1] > 0.001f && cam_mul[2] > 0.001f)
5313   {
5314     for (c = 0; c < 3; c++)
5315       pre_mul[c] /= cam_mul[c];
5316   }
5317 #endif
5318   if (pre_mul[1] == 0)
5319     pre_mul[1] = 1;
5320   if (pre_mul[3] == 0)
5321     pre_mul[3] = colors < 4 ? pre_mul[1] : 1;
5322   dark = black;
5323   sat = maximum;
5324   if (threshold)
5325     wavelet_denoise();
5326   maximum -= black;
5327   for (dmin = DBL_MAX, dmax = c = 0; c < 4; c++)
5328   {
5329     if (dmin > pre_mul[c])
5330       dmin = pre_mul[c];
5331     if (dmax < pre_mul[c])
5332       dmax = pre_mul[c];
5333   }
5334   if (!highlight)
5335     dmax = dmin;
5336   FORC4 scale_mul[c] = (pre_mul[c] /= dmax) * 65535.0 / maximum;
5337 #ifdef DCRAW_VERBOSE
5338   if (verbose)
5339   {
5340     fprintf(stderr, _("Scaling with darkness %d, saturation %d, and\nmultipliers"), dark, sat);
5341     FORC4 fprintf(stderr, " %f", pre_mul[c]);
5342     fputc('\n', stderr);
5343   }
5344 #endif
5345   if (filters > 1000 && (cblack[4] + 1) / 2 == 1 && (cblack[5] + 1) / 2 == 1)
5346   {
5347     FORC4 cblack[FC(c / 2, c % 2)] += cblack[6 + c / 2 % cblack[4] * cblack[5] + c % 2 % cblack[5]];
5348     cblack[4] = cblack[5] = 0;
5349   }
5350   size = iheight * iwidth;
5351 #ifdef LIBRAW_LIBRARY_BUILD
5352   scale_colors_loop(scale_mul);
5353 #else
5354   for (i = 0; i < size * 4; i++)
5355   {
5356     if (!(val = ((ushort *)image)[i]))
5357       continue;
5358     if (cblack[4] && cblack[5])
5359       val -= cblack[6 + i / 4 / iwidth % cblack[4] * cblack[5] + i / 4 % iwidth % cblack[5]];
5360     val -= cblack[i & 3];
5361     val *= scale_mul[i & 3];
5362     ((ushort *)image)[i] = CLIP(val);
5363   }
5364 #endif
5365   if ((aber[0] != 1 || aber[2] != 1) && colors == 3)
5366   {
5367 #ifdef DCRAW_VERBOSE
5368     if (verbose)
5369       fprintf(stderr, _("Correcting chromatic aberration...\n"));
5370 #endif
5371     for (c = 0; c < 4; c += 2)
5372     {
5373       if (aber[c] == 1)
5374         continue;
5375       img = (ushort *)malloc(size * sizeof *img);
5376       merror(img, "scale_colors()");
5377       for (i = 0; i < size; i++)
5378         img[i] = image[i][c];
5379       for (row = 0; row < iheight; row++)
5380       {
5381         ur = fr = (row - iheight * 0.5) * aber[c] + iheight * 0.5;
5382         if (ur > iheight - 2)
5383           continue;
5384         fr -= ur;
5385         for (col = 0; col < iwidth; col++)
5386         {
5387           uc = fc = (col - iwidth * 0.5) * aber[c] + iwidth * 0.5;
5388           if (uc > iwidth - 2)
5389             continue;
5390           fc -= uc;
5391           pix = img + ur * iwidth + uc;
5392           image[row * iwidth + col][c] =
5393               (pix[0] * (1 - fc) + pix[1] * fc) * (1 - fr) + (pix[iwidth] * (1 - fc) + pix[iwidth + 1] * fc) * fr;
5394         }
5395       }
5396       free(img);
5397     }
5398   }
5399 #ifdef LIBRAW_LIBRARY_BUILD
5400   RUN_CALLBACK(LIBRAW_PROGRESS_SCALE_COLORS, 1, 2);
5401 #endif
5402 }
5403 
pre_interpolate()5404 void CLASS pre_interpolate()
5405 {
5406   ushort(*img)[4];
5407   int row, col, c;
5408 #ifdef LIBRAW_LIBRARY_BUILD
5409   RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE, 0, 2);
5410 #endif
5411   if (shrink)
5412   {
5413     if (half_size)
5414     {
5415       height = iheight;
5416       width = iwidth;
5417       if (filters == 9)
5418       {
5419         for (row = 0; row < 3; row++)
5420           for (col = 1; col < 4; col++)
5421             if (!(image[row * width + col][0] | image[row * width + col][2]))
5422               goto break2;
5423       break2:
5424         for (; row < height; row += 3)
5425           for (col = (col - 1) % 3 + 1; col < width - 1; col += 3)
5426           {
5427             img = image + row * width + col;
5428             for (c = 0; c < 3; c += 2)
5429               img[0][c] = (img[-1][c] + img[1][c]) >> 1;
5430           }
5431       }
5432     }
5433     else
5434     {
5435       img = (ushort(*)[4])calloc(height, width * sizeof *img);
5436       merror(img, "pre_interpolate()");
5437       for (row = 0; row < height; row++)
5438         for (col = 0; col < width; col++)
5439         {
5440           c = fcol(row, col);
5441           img[row * width + col][c] = image[(row >> 1) * iwidth + (col >> 1)][c];
5442         }
5443       free(image);
5444       image = img;
5445       shrink = 0;
5446     }
5447   }
5448   if (filters > 1000 && colors == 3)
5449   {
5450     mix_green = four_color_rgb ^ half_size;
5451     if (four_color_rgb | half_size)
5452       colors++;
5453     else
5454     {
5455       for (row = FC(1, 0) >> 1; row < height; row += 2)
5456         for (col = FC(row, 1) & 1; col < width; col += 2)
5457           image[row * width + col][1] = image[row * width + col][3];
5458       filters &= ~((filters & 0x55555555U) << 1);
5459     }
5460   }
5461   if (half_size)
5462     filters = 0;
5463 #ifdef LIBRAW_LIBRARY_BUILD
5464   RUN_CALLBACK(LIBRAW_PROGRESS_PRE_INTERPOLATE, 1, 2);
5465 #endif
5466 }
5467 
border_interpolate(int border)5468 void CLASS border_interpolate(int border)
5469 {
5470   unsigned row, col, y, x, f, c, sum[8];
5471 
5472   for (row = 0; row < height; row++)
5473     for (col = 0; col < width; col++)
5474     {
5475       if (col == border && row >= border && row < height - border)
5476         col = width - border;
5477       memset(sum, 0, sizeof sum);
5478       for (y = row - 1; y != row + 2; y++)
5479         for (x = col - 1; x != col + 2; x++)
5480           if (y < height && x < width)
5481           {
5482             f = fcol(y, x);
5483             sum[f] += image[y * width + x][f];
5484             sum[f + 4]++;
5485           }
5486       f = fcol(row, col);
5487       FORCC if (c != f && sum[c + 4]) image[row * width + col][c] = sum[c] / sum[c + 4];
5488     }
5489 }
5490 
lin_interpolate_loop(int code[16][16][32],int size)5491 void CLASS lin_interpolate_loop(int code[16][16][32], int size)
5492 {
5493   int row;
5494   for (row = 1; row < height - 1; row++)
5495   {
5496     int col, *ip;
5497     ushort *pix;
5498     for (col = 1; col < width - 1; col++)
5499     {
5500       int i;
5501       int sum[4];
5502       pix = image[row * width + col];
5503       ip = code[row % size][col % size];
5504       memset(sum, 0, sizeof sum);
5505       for (i = *ip++; i--; ip += 3)
5506         sum[ip[2]] += pix[ip[0]] << ip[1];
5507       for (i = colors; --i; ip += 2)
5508         pix[ip[0]] = sum[ip[0]] * ip[1] >> 8;
5509     }
5510   }
5511 }
5512 
lin_interpolate()5513 void CLASS lin_interpolate()
5514 {
5515   int code[16][16][32], size = 16, *ip, sum[4];
5516   int f, c, x, y, row, col, shift, color;
5517 
5518 #ifdef DCRAW_VERBOSE
5519   if (verbose)
5520     fprintf(stderr, _("Bilinear interpolation...\n"));
5521 #endif
5522 #ifdef LIBRAW_LIBRARY_BUILD
5523   RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE, 0, 3);
5524 #endif
5525 
5526   if (filters == 9)
5527     size = 6;
5528   border_interpolate(1);
5529   for (row = 0; row < size; row++)
5530     for (col = 0; col < size; col++)
5531     {
5532       ip = code[row][col] + 1;
5533       f = fcol(row, col);
5534       memset(sum, 0, sizeof sum);
5535       for (y = -1; y <= 1; y++)
5536         for (x = -1; x <= 1; x++)
5537         {
5538           shift = (y == 0) + (x == 0);
5539           color = fcol(row + y, col + x);
5540           if (color == f)
5541             continue;
5542           *ip++ = (width * y + x) * 4 + color;
5543           *ip++ = shift;
5544           *ip++ = color;
5545           sum[color] += 1 << shift;
5546         }
5547       code[row][col][0] = (ip - code[row][col]) / 3;
5548       FORCC
5549       if (c != f)
5550       {
5551         *ip++ = c;
5552         *ip++ = sum[c] > 0 ? 256 / sum[c] : 0;
5553       }
5554     }
5555 #ifdef LIBRAW_LIBRARY_BUILD
5556   RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE, 1, 3);
5557 #endif
5558   lin_interpolate_loop(code, size);
5559 #ifdef LIBRAW_LIBRARY_BUILD
5560   RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE, 2, 3);
5561 #endif
5562 }
5563 
5564 /*
5565    This algorithm is officially called:
5566 
5567    "Interpolation using a Threshold-based variable number of gradients"
5568 
5569    described in http://scien.stanford.edu/pages/labsite/1999/psych221/projects/99/tingchen/algodep/vargra.html
5570 
5571    I've extended the basic idea to work with non-Bayer filter arrays.
5572    Gradients are numbered clockwise from NW=0 to W=7.
5573  */
vng_interpolate()5574 void CLASS vng_interpolate()
5575 {
5576   static const signed char *cp,
5577       terms[] = {-2, -2, +0, -1, 0, 0x01, -2, -2, +0, +0, 1, 0x01, -2, -1, -1, +0, 0, 0x01, -2, -1, +0, -1, 0, 0x02,
5578                  -2, -1, +0, +0, 0, 0x03, -2, -1, +0, +1, 1, 0x01, -2, +0, +0, -1, 0, 0x06, -2, +0, +0, +0, 1, 0x02,
5579                  -2, +0, +0, +1, 0, 0x03, -2, +1, -1, +0, 0, 0x04, -2, +1, +0, -1, 1, 0x04, -2, +1, +0, +0, 0, 0x06,
5580                  -2, +1, +0, +1, 0, 0x02, -2, +2, +0, +0, 1, 0x04, -2, +2, +0, +1, 0, 0x04, -1, -2, -1, +0, 0, -128,
5581                  -1, -2, +0, -1, 0, 0x01, -1, -2, +1, -1, 0, 0x01, -1, -2, +1, +0, 1, 0x01, -1, -1, -1, +1, 0, -120,
5582                  -1, -1, +1, -2, 0, 0x40, -1, -1, +1, -1, 0, 0x22, -1, -1, +1, +0, 0, 0x33, -1, -1, +1, +1, 1, 0x11,
5583                  -1, +0, -1, +2, 0, 0x08, -1, +0, +0, -1, 0, 0x44, -1, +0, +0, +1, 0, 0x11, -1, +0, +1, -2, 1, 0x40,
5584                  -1, +0, +1, -1, 0, 0x66, -1, +0, +1, +0, 1, 0x22, -1, +0, +1, +1, 0, 0x33, -1, +0, +1, +2, 1, 0x10,
5585                  -1, +1, +1, -1, 1, 0x44, -1, +1, +1, +0, 0, 0x66, -1, +1, +1, +1, 0, 0x22, -1, +1, +1, +2, 0, 0x10,
5586                  -1, +2, +0, +1, 0, 0x04, -1, +2, +1, +0, 1, 0x04, -1, +2, +1, +1, 0, 0x04, +0, -2, +0, +0, 1, -128,
5587                  +0, -1, +0, +1, 1, -120, +0, -1, +1, -2, 0, 0x40, +0, -1, +1, +0, 0, 0x11, +0, -1, +2, -2, 0, 0x40,
5588                  +0, -1, +2, -1, 0, 0x20, +0, -1, +2, +0, 0, 0x30, +0, -1, +2, +1, 1, 0x10, +0, +0, +0, +2, 1, 0x08,
5589                  +0, +0, +2, -2, 1, 0x40, +0, +0, +2, -1, 0, 0x60, +0, +0, +2, +0, 1, 0x20, +0, +0, +2, +1, 0, 0x30,
5590                  +0, +0, +2, +2, 1, 0x10, +0, +1, +1, +0, 0, 0x44, +0, +1, +1, +2, 0, 0x10, +0, +1, +2, -1, 1, 0x40,
5591                  +0, +1, +2, +0, 0, 0x60, +0, +1, +2, +1, 0, 0x20, +0, +1, +2, +2, 0, 0x10, +1, -2, +1, +0, 0, -128,
5592                  +1, -1, +1, +1, 0, -120, +1, +0, +1, +2, 0, 0x08, +1, +0, +2, -1, 0, 0x40, +1, +0, +2, +1, 0, 0x10},
5593       chood[] = {-1, -1, -1, 0, -1, +1, 0, +1, +1, +1, +1, 0, +1, -1, 0, -1};
5594   ushort(*brow[5])[4], *pix;
5595   int prow = 8, pcol = 2, *ip, *code[16][16], gval[8], gmin, gmax, sum[4];
5596   int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
5597   int g, diff, thold, num, c;
5598 
5599   lin_interpolate();
5600 #ifdef DCRAW_VERBOSE
5601   if (verbose)
5602     fprintf(stderr, _("VNG interpolation...\n"));
5603 #endif
5604 
5605   if (filters == 1)
5606     prow = pcol = 16;
5607   if (filters == 9)
5608     prow = pcol = 6;
5609   ip = (int *)calloc(prow * pcol, 1280);
5610   merror(ip, "vng_interpolate()");
5611   for (row = 0; row < prow; row++) /* Precalculate for VNG */
5612     for (col = 0; col < pcol; col++)
5613     {
5614       code[row][col] = ip;
5615       for (cp = terms, t = 0; t < 64; t++)
5616       {
5617         y1 = *cp++;
5618         x1 = *cp++;
5619         y2 = *cp++;
5620         x2 = *cp++;
5621         weight = *cp++;
5622         grads = *cp++;
5623         color = fcol(row + y1, col + x1);
5624         if (fcol(row + y2, col + x2) != color)
5625           continue;
5626         diag = (fcol(row, col + 1) == color && fcol(row + 1, col) == color) ? 2 : 1;
5627         if (abs(y1 - y2) == diag && abs(x1 - x2) == diag)
5628           continue;
5629         *ip++ = (y1 * width + x1) * 4 + color;
5630         *ip++ = (y2 * width + x2) * 4 + color;
5631         *ip++ = weight;
5632         for (g = 0; g < 8; g++)
5633           if (grads & 1 << g)
5634             *ip++ = g;
5635         *ip++ = -1;
5636       }
5637       *ip++ = INT_MAX;
5638       for (cp = chood, g = 0; g < 8; g++)
5639       {
5640         y = *cp++;
5641         x = *cp++;
5642         *ip++ = (y * width + x) * 4;
5643         color = fcol(row, col);
5644         if (fcol(row + y, col + x) != color && fcol(row + y * 2, col + x * 2) == color)
5645           *ip++ = (y * width + x) * 8 + color;
5646         else
5647           *ip++ = 0;
5648       }
5649     }
5650   brow[4] = (ushort(*)[4])calloc(width * 3, sizeof **brow);
5651   merror(brow[4], "vng_interpolate()");
5652   for (row = 0; row < 3; row++)
5653     brow[row] = brow[4] + row * width;
5654   for (row = 2; row < height - 2; row++)
5655   { /* Do VNG interpolation */
5656 #ifdef LIBRAW_LIBRARY_BUILD
5657     if (!((row - 2) % 256))
5658       RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE, (row - 2) / 256 + 1, ((height - 3) / 256) + 1);
5659 #endif
5660     for (col = 2; col < width - 2; col++)
5661     {
5662       pix = image[row * width + col];
5663       ip = code[row % prow][col % pcol];
5664       memset(gval, 0, sizeof gval);
5665       while ((g = ip[0]) != INT_MAX)
5666       { /* Calculate gradients */
5667         diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
5668         gval[ip[3]] += diff;
5669         ip += 5;
5670         if ((g = ip[-1]) == -1)
5671           continue;
5672         gval[g] += diff;
5673         while ((g = *ip++) != -1)
5674           gval[g] += diff;
5675       }
5676       ip++;
5677       gmin = gmax = gval[0]; /* Choose a threshold */
5678       for (g = 1; g < 8; g++)
5679       {
5680         if (gmin > gval[g])
5681           gmin = gval[g];
5682         if (gmax < gval[g])
5683           gmax = gval[g];
5684       }
5685       if (gmax == 0)
5686       {
5687         memcpy(brow[2][col], pix, sizeof *image);
5688         continue;
5689       }
5690       thold = gmin + (gmax >> 1);
5691       memset(sum, 0, sizeof sum);
5692       color = fcol(row, col);
5693       for (num = g = 0; g < 8; g++, ip += 2)
5694       { /* Average the neighbors */
5695         if (gval[g] <= thold)
5696         {
5697           FORCC
5698           if (c == color && ip[1])
5699             sum[c] += (pix[c] + pix[ip[1]]) >> 1;
5700           else
5701             sum[c] += pix[ip[0] + c];
5702           num++;
5703         }
5704       }
5705       FORCC
5706       { /* Save to buffer */
5707         t = pix[color];
5708         if (c != color)
5709           t += (sum[c] - sum[color]) / num;
5710         brow[2][col][c] = CLIP(t);
5711       }
5712     }
5713     if (row > 3) /* Write buffer to image */
5714       memcpy(image[(row - 2) * width + 2], brow[0] + 2, (width - 4) * sizeof *image);
5715     for (g = 0; g < 4; g++)
5716       brow[(g - 1) & 3] = brow[g];
5717   }
5718   memcpy(image[(row - 2) * width + 2], brow[0] + 2, (width - 4) * sizeof *image);
5719   memcpy(image[(row - 1) * width + 2], brow[1] + 2, (width - 4) * sizeof *image);
5720   free(brow[4]);
5721   free(code[0][0]);
5722 }
5723 
5724 /*
5725    Patterned Pixel Grouping Interpolation by Alain Desbiolles
5726 */
ppg_interpolate()5727 void CLASS ppg_interpolate()
5728 {
5729   int dir[5] = {1, width, -1, -width, 1};
5730   int row, col, diff[2], guess[2], c, d, i;
5731   ushort(*pix)[4];
5732 
5733   border_interpolate(3);
5734 #ifdef DCRAW_VERBOSE
5735   if (verbose)
5736     fprintf(stderr, _("PPG interpolation...\n"));
5737 #endif
5738 
5739 /*  Fill in the green layer with gradients and pattern recognition: */
5740 #ifdef LIBRAW_LIBRARY_BUILD
5741   RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE, 0, 3);
5742 #ifdef LIBRAW_USE_OPENMP
5743 #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
5744 #endif
5745 #endif
5746   for (row = 3; row < height - 3; row++)
5747     for (col = 3 + (FC(row, 3) & 1), c = FC(row, col); col < width - 3; col += 2)
5748     {
5749       pix = image + row * width + col;
5750       for (i = 0; (d = dir[i]) > 0; i++)
5751       {
5752         guess[i] = (pix[-d][1] + pix[0][c] + pix[d][1]) * 2 - pix[-2 * d][c] - pix[2 * d][c];
5753         diff[i] = (ABS(pix[-2 * d][c] - pix[0][c]) + ABS(pix[2 * d][c] - pix[0][c]) + ABS(pix[-d][1] - pix[d][1])) * 3 +
5754                   (ABS(pix[3 * d][1] - pix[d][1]) + ABS(pix[-3 * d][1] - pix[-d][1])) * 2;
5755       }
5756       d = dir[i = diff[0] > diff[1]];
5757       pix[0][1] = ULIM(guess[i] >> 2, pix[d][1], pix[-d][1]);
5758     }
5759 /*  Calculate red and blue for each green pixel:		*/
5760 #ifdef LIBRAW_LIBRARY_BUILD
5761   RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE, 1, 3);
5762 #ifdef LIBRAW_USE_OPENMP
5763 #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
5764 #endif
5765 #endif
5766   for (row = 1; row < height - 1; row++)
5767     for (col = 1 + (FC(row, 2) & 1), c = FC(row, col + 1); col < width - 1; col += 2)
5768     {
5769       pix = image + row * width + col;
5770       for (i = 0; (d = dir[i]) > 0; c = 2 - c, i++)
5771         pix[0][c] = CLIP((pix[-d][c] + pix[d][c] + 2 * pix[0][1] - pix[-d][1] - pix[d][1]) >> 1);
5772     }
5773 /*  Calculate blue for red pixels and vice versa:		*/
5774 #ifdef LIBRAW_LIBRARY_BUILD
5775   RUN_CALLBACK(LIBRAW_PROGRESS_INTERPOLATE, 2, 3);
5776 #ifdef LIBRAW_USE_OPENMP
5777 #pragma omp parallel for default(shared) private(guess, diff, row, col, d, c, i, pix) schedule(static)
5778 #endif
5779 #endif
5780   for (row = 1; row < height - 1; row++)
5781     for (col = 1 + (FC(row, 1) & 1), c = 2 - FC(row, col); col < width - 1; col += 2)
5782     {
5783       pix = image + row * width + col;
5784       for (i = 0; (d = dir[i] + dir[i + 1]) > 0; i++)
5785       {
5786         diff[i] = ABS(pix[-d][c] - pix[d][c]) + ABS(pix[-d][1] - pix[0][1]) + ABS(pix[d][1] - pix[0][1]);
5787         guess[i] = pix[-d][c] + pix[d][c] + 2 * pix[0][1] - pix[-d][1] - pix[d][1];
5788       }
5789       if (diff[0] != diff[1])
5790         pix[0][c] = CLIP(guess[diff[0] > diff[1]] >> 1);
5791       else
5792         pix[0][c] = CLIP((guess[0] + guess[1]) >> 2);
5793     }
5794 }
5795 
cielab(ushort rgb[3],short lab[3])5796 void CLASS cielab(ushort rgb[3], short lab[3])
5797 {
5798   int c, i, j, k;
5799   float r, xyz[3];
5800 #ifdef LIBRAW_NOTHREADS
5801   static float cbrt[0x10000], xyz_cam[3][4];
5802 #else
5803 #define cbrt tls->ahd_data.cbrt
5804 #define xyz_cam tls->ahd_data.xyz_cam
5805 #endif
5806 
5807   if (!rgb)
5808   {
5809 #ifndef LIBRAW_NOTHREADS
5810     if (cbrt[0] < -1.0f)
5811 #endif
5812       for (i = 0; i < 0x10000; i++)
5813       {
5814         r = i / 65535.0;
5815         cbrt[i] = r > 0.008856 ? pow(r, 1.f / 3.0f) : 7.787f * r + 16.f / 116.0f;
5816       }
5817     for (i = 0; i < 3; i++)
5818       for (j = 0; j < colors; j++)
5819         for (xyz_cam[i][j] = k = 0; k < 3; k++)
5820           xyz_cam[i][j] += xyz_rgb[i][k] * rgb_cam[k][j] / d65_white[i];
5821     return;
5822   }
5823   xyz[0] = xyz[1] = xyz[2] = 0.5;
5824   FORCC
5825   {
5826     xyz[0] += xyz_cam[0][c] * rgb[c];
5827     xyz[1] += xyz_cam[1][c] * rgb[c];
5828     xyz[2] += xyz_cam[2][c] * rgb[c];
5829   }
5830   xyz[0] = cbrt[CLIP((int)xyz[0])];
5831   xyz[1] = cbrt[CLIP((int)xyz[1])];
5832   xyz[2] = cbrt[CLIP((int)xyz[2])];
5833   lab[0] = 64 * (116 * xyz[1] - 16);
5834   lab[1] = 64 * 500 * (xyz[0] - xyz[1]);
5835   lab[2] = 64 * 200 * (xyz[1] - xyz[2]);
5836 #ifndef LIBRAW_NOTHREADS
5837 #undef cbrt
5838 #undef xyz_cam
5839 #endif
5840 }
5841 
5842 #define TS 512 /* Tile Size */
5843 #define fcol(row, col) xtrans[(row + 6) % 6][(col + 6) % 6]
5844 
5845 /*
5846    Frank Markesteijn's algorithm for Fuji X-Trans sensors
5847  */
xtrans_interpolate(int passes)5848 void CLASS xtrans_interpolate(int passes)
5849 {
5850   int c, d, f, g, h, i, v, ng, row, col, top, left, mrow, mcol;
5851 
5852 #ifdef LIBRAW_LIBRARY_BUILD
5853   int cstat[4] = {0, 0, 0, 0};
5854 #endif
5855 
5856   int val, ndir, pass, hm[8], avg[4], color[3][8];
5857   static const short orth[12] = {1, 0, 0, 1, -1, 0, 0, -1, 1, 0, 0, 1},
5858                      patt[2][16] = {{0, 1, 0, -1, 2, 0, -1, 0, 1, 1, 1, -1, 0, 0, 0, 0},
5859                                     {0, 1, 0, -2, 1, 0, -2, 0, 1, 1, -2, -2, 1, -1, -1, 1}},
5860                      dir[4] = {1, TS, TS + 1, TS - 1};
5861   short allhex[3][3][2][8], *hex;
5862   ushort min, max, sgrow, sgcol;
5863   ushort(*rgb)[TS][TS][3], (*rix)[3], (*pix)[4];
5864   short(*lab)[TS][3], (*lix)[3];
5865   float(*drv)[TS][TS], diff[6], tr;
5866   char(*homo)[TS][TS], *buffer;
5867 
5868 #ifdef DCRAW_VERBOSE
5869   if (verbose)
5870     fprintf(stderr, _("%d-pass X-Trans interpolation...\n"), passes);
5871 #endif
5872 
5873 #ifdef LIBRAW_LIBRARY_BUILD
5874   if (width < TS || height < TS)
5875     throw LIBRAW_EXCEPTION_IO_CORRUPT; // too small image
5876                                        /* Check against right pattern */
5877   for (row = 0; row < 6; row++)
5878     for (col = 0; col < 6; col++)
5879       cstat[fcol(row, col)]++;
5880 
5881   if (cstat[0] < 6 || cstat[0] > 10 || cstat[1] < 16 || cstat[1] > 24 || cstat[2] < 6 || cstat[2] > 10 || cstat[3])
5882     throw LIBRAW_EXCEPTION_IO_CORRUPT;
5883 
5884   // Init allhex table to unreasonable values
5885   for (int i = 0; i < 3; i++)
5886     for (int j = 0; j < 3; j++)
5887       for (int k = 0; k < 2; k++)
5888         for (int l = 0; l < 8; l++)
5889           allhex[i][j][k][l] = 32700;
5890 #endif
5891   cielab(0, 0);
5892   ndir = 4 << (passes > 1);
5893   buffer = (char *)malloc(TS * TS * (ndir * 11 + 6));
5894   merror(buffer, "xtrans_interpolate()");
5895   rgb = (ushort(*)[TS][TS][3])buffer;
5896   lab = (short(*)[TS][3])(buffer + TS * TS * (ndir * 6));
5897   drv = (float(*)[TS][TS])(buffer + TS * TS * (ndir * 6 + 6));
5898   homo = (char(*)[TS][TS])(buffer + TS * TS * (ndir * 10 + 6));
5899 
5900   int minv = 0, maxv = 0, minh = 0, maxh = 0;
5901   /* Map a green hexagon around each non-green pixel and vice versa:	*/
5902   for (row = 0; row < 3; row++)
5903     for (col = 0; col < 3; col++)
5904       for (ng = d = 0; d < 10; d += 2)
5905       {
5906         g = fcol(row, col) == 1;
5907         if (fcol(row + orth[d], col + orth[d + 2]) == 1)
5908           ng = 0;
5909         else
5910           ng++;
5911         if (ng == 4)
5912         {
5913           sgrow = row;
5914           sgcol = col;
5915         }
5916         if (ng == g + 1)
5917           FORC(8)
5918           {
5919             v = orth[d] * patt[g][c * 2] + orth[d + 1] * patt[g][c * 2 + 1];
5920             h = orth[d + 2] * patt[g][c * 2] + orth[d + 3] * patt[g][c * 2 + 1];
5921             minv = MIN(v, minv);
5922             maxv = MAX(v, maxv);
5923             minh = MIN(v, minh);
5924             maxh = MAX(v, maxh);
5925             allhex[row][col][0][c ^ (g * 2 & d)] = h + v * width;
5926             allhex[row][col][1][c ^ (g * 2 & d)] = h + v * TS;
5927           }
5928       }
5929 
5930 #ifdef LIBRAW_LIBRARY_BUILD
5931   // Check allhex table initialization
5932   for (int i = 0; i < 3; i++)
5933     for (int j = 0; j < 3; j++)
5934       for (int k = 0; k < 2; k++)
5935         for (int l = 0; l < 8; l++)
5936           if (allhex[i][j][k][l] > maxh + maxv * width + 1 || allhex[i][j][k][l] < minh + minv * width - 1)
5937             throw LIBRAW_EXCEPTION_IO_CORRUPT;
5938   int retrycount = 0;
5939 #endif
5940   /* Set green1 and green3 to the minimum and maximum allowed values:	*/
5941   for (row = 2; row < height - 2; row++)
5942     for (min = ~(max = 0), col = 2; col < width - 2; col++)
5943     {
5944       if (fcol(row, col) == 1 && (min = ~(max = 0)))
5945         continue;
5946       pix = image + row * width + col;
5947       hex = allhex[row % 3][col % 3][0];
5948       if (!max)
5949         FORC(6)
5950         {
5951           val = pix[hex[c]][1];
5952           if (min > val)
5953             min = val;
5954           if (max < val)
5955             max = val;
5956         }
5957       pix[0][1] = min;
5958       pix[0][3] = max;
5959       switch ((row - sgrow) % 3)
5960       {
5961       case 1:
5962         if (row < height - 3)
5963         {
5964           row++;
5965           col--;
5966         }
5967         break;
5968       case 2:
5969         if ((min = ~(max = 0)) && (col += 2) < width - 3 && row > 2)
5970         {
5971           row--;
5972 #ifdef LIBRAW_LIBRARY_BUILD
5973           if (retrycount++ > width * height)
5974             throw LIBRAW_EXCEPTION_IO_CORRUPT;
5975 #endif
5976         }
5977       }
5978     }
5979 
5980   for (top = 3; top < height - 19; top += TS - 16)
5981     for (left = 3; left < width - 19; left += TS - 16)
5982     {
5983       mrow = MIN(top + TS, height - 3);
5984       mcol = MIN(left + TS, width - 3);
5985       for (row = top; row < mrow; row++)
5986         for (col = left; col < mcol; col++)
5987           memcpy(rgb[0][row - top][col - left], image[row * width + col], 6);
5988       FORC3 memcpy(rgb[c + 1], rgb[0], sizeof *rgb);
5989 
5990       /* Interpolate green horizontally, vertically, and along both diagonals: */
5991       for (row = top; row < mrow; row++)
5992         for (col = left; col < mcol; col++)
5993         {
5994           if ((f = fcol(row, col)) == 1)
5995             continue;
5996           pix = image + row * width + col;
5997           hex = allhex[row % 3][col % 3][0];
5998           color[1][0] = 174 * (pix[hex[1]][1] + pix[hex[0]][1]) - 46 * (pix[2 * hex[1]][1] + pix[2 * hex[0]][1]);
5999           color[1][1] = 223 * pix[hex[3]][1] + pix[hex[2]][1] * 33 + 92 * (pix[0][f] - pix[-hex[2]][f]);
6000           FORC(2)
6001           color[1][2 + c] = 164 * pix[hex[4 + c]][1] + 92 * pix[-2 * hex[4 + c]][1] +
6002                             33 * (2 * pix[0][f] - pix[3 * hex[4 + c]][f] - pix[-3 * hex[4 + c]][f]);
6003           FORC4 rgb[c ^ !((row - sgrow) % 3)][row - top][col - left][1] = LIM(color[1][c] >> 8, pix[0][1], pix[0][3]);
6004         }
6005 
6006       for (pass = 0; pass < passes; pass++)
6007       {
6008         if (pass == 1)
6009           memcpy(rgb += 4, buffer, 4 * sizeof *rgb);
6010 
6011         /* Recalculate green from interpolated values of closer pixels:	*/
6012         if (pass)
6013         {
6014           for (row = top + 2; row < mrow - 2; row++)
6015             for (col = left + 2; col < mcol - 2; col++)
6016             {
6017               if ((f = fcol(row, col)) == 1)
6018                 continue;
6019               pix = image + row * width + col;
6020               hex = allhex[row % 3][col % 3][1];
6021               for (d = 3; d < 6; d++)
6022               {
6023                 rix = &rgb[(d - 2) ^ !((row - sgrow) % 3)][row - top][col - left];
6024                 val =
6025                     rix[-2 * hex[d]][1] + 2 * rix[hex[d]][1] - rix[-2 * hex[d]][f] - 2 * rix[hex[d]][f] + 3 * rix[0][f];
6026                 rix[0][1] = LIM(val / 3, pix[0][1], pix[0][3]);
6027               }
6028             }
6029         }
6030 
6031         /* Interpolate red and blue values for solitary green pixels:	*/
6032         for (row = (top - sgrow + 4) / 3 * 3 + sgrow; row < mrow - 2; row += 3)
6033           for (col = (left - sgcol + 4) / 3 * 3 + sgcol; col < mcol - 2; col += 3)
6034           {
6035             rix = &rgb[0][row - top][col - left];
6036             h = fcol(row, col + 1);
6037             memset(diff, 0, sizeof diff);
6038             for (i = 1, d = 0; d < 6; d++, i ^= TS ^ 1, h ^= 2)
6039             {
6040               for (c = 0; c < 2; c++, h ^= 2)
6041               {
6042                 g = 2 * rix[0][1] - rix[i << c][1] - rix[-i << c][1];
6043                 color[h][d] = g + rix[i << c][h] + rix[-i << c][h];
6044                 if (d > 1)
6045                   diff[d] += SQR(rix[i << c][1] - rix[-i << c][1] - rix[i << c][h] + rix[-i << c][h]) + SQR(g);
6046               }
6047               if (d > 1 && (d & 1))
6048                 if (diff[d - 1] < diff[d])
6049                   FORC(2) color[c * 2][d] = color[c * 2][d - 1];
6050               if (d < 2 || (d & 1))
6051               {
6052                 FORC(2) rix[0][c * 2] = CLIP(color[c * 2][d] / 2);
6053                 rix += TS * TS;
6054               }
6055             }
6056           }
6057 
6058         /* Interpolate red for blue pixels and vice versa:		*/
6059         for (row = top + 3; row < mrow - 3; row++)
6060           for (col = left + 3; col < mcol - 3; col++)
6061           {
6062             if ((f = 2 - fcol(row, col)) == 1)
6063               continue;
6064             rix = &rgb[0][row - top][col - left];
6065             c = (row - sgrow) % 3 ? TS : 1;
6066             h = 3 * (c ^ TS ^ 1);
6067             for (d = 0; d < 4; d++, rix += TS * TS)
6068             {
6069               i = d > 1 || ((d ^ c) & 1) ||
6070                           ((ABS(rix[0][1] - rix[c][1]) + ABS(rix[0][1] - rix[-c][1])) <
6071                            2 * (ABS(rix[0][1] - rix[h][1]) + ABS(rix[0][1] - rix[-h][1])))
6072                       ? c
6073                       : h;
6074               rix[0][f] = CLIP((rix[i][f] + rix[-i][f] + 2 * rix[0][1] - rix[i][1] - rix[-i][1]) / 2);
6075             }
6076           }
6077 
6078         /* Fill in red and blue for 2x2 blocks of green:		*/
6079         for (row = top + 2; row < mrow - 2; row++)
6080           if ((row - sgrow) % 3)
6081             for (col = left + 2; col < mcol - 2; col++)
6082               if ((col - sgcol) % 3)
6083               {
6084                 rix = &rgb[0][row - top][col - left];
6085                 hex = allhex[row % 3][col % 3][1];
6086                 for (d = 0; d < ndir; d += 2, rix += TS * TS)
6087                   if (hex[d] + hex[d + 1])
6088                   {
6089                     g = 3 * rix[0][1] - 2 * rix[hex[d]][1] - rix[hex[d + 1]][1];
6090                     for (c = 0; c < 4; c += 2)
6091                       rix[0][c] = CLIP((g + 2 * rix[hex[d]][c] + rix[hex[d + 1]][c]) / 3);
6092                   }
6093                   else
6094                   {
6095                     g = 2 * rix[0][1] - rix[hex[d]][1] - rix[hex[d + 1]][1];
6096                     for (c = 0; c < 4; c += 2)
6097                       rix[0][c] = CLIP((g + rix[hex[d]][c] + rix[hex[d + 1]][c]) / 2);
6098                   }
6099               }
6100       }
6101       rgb = (ushort(*)[TS][TS][3])buffer;
6102       mrow -= top;
6103       mcol -= left;
6104 
6105       /* Convert to CIELab and differentiate in all directions:	*/
6106       for (d = 0; d < ndir; d++)
6107       {
6108         for (row = 2; row < mrow - 2; row++)
6109           for (col = 2; col < mcol - 2; col++)
6110             cielab(rgb[d][row][col], lab[row][col]);
6111         for (f = dir[d & 3], row = 3; row < mrow - 3; row++)
6112           for (col = 3; col < mcol - 3; col++)
6113           {
6114             lix = &lab[row][col];
6115             g = 2 * lix[0][0] - lix[f][0] - lix[-f][0];
6116             drv[d][row][col] = SQR(g) + SQR((2 * lix[0][1] - lix[f][1] - lix[-f][1] + g * 500 / 232)) +
6117                                SQR((2 * lix[0][2] - lix[f][2] - lix[-f][2] - g * 500 / 580));
6118           }
6119       }
6120 
6121       /* Build homogeneity maps from the derivatives:			*/
6122       memset(homo, 0, ndir * TS * TS);
6123       for (row = 4; row < mrow - 4; row++)
6124         for (col = 4; col < mcol - 4; col++)
6125         {
6126           for (tr = FLT_MAX, d = 0; d < ndir; d++)
6127             if (tr > drv[d][row][col])
6128               tr = drv[d][row][col];
6129           tr *= 8;
6130           for (d = 0; d < ndir; d++)
6131             for (v = -1; v <= 1; v++)
6132               for (h = -1; h <= 1; h++)
6133                 if (drv[d][row + v][col + h] <= tr)
6134                   homo[d][row][col]++;
6135         }
6136 
6137       /* Average the most homogenous pixels for the final result:	*/
6138       if (height - top < TS + 4)
6139         mrow = height - top + 2;
6140       if (width - left < TS + 4)
6141         mcol = width - left + 2;
6142       for (row = MIN(top, 8); row < mrow - 8; row++)
6143         for (col = MIN(left, 8); col < mcol - 8; col++)
6144         {
6145           for (d = 0; d < ndir; d++)
6146             for (hm[d] = 0, v = -2; v <= 2; v++)
6147               for (h = -2; h <= 2; h++)
6148                 hm[d] += homo[d][row + v][col + h];
6149           for (d = 0; d < ndir - 4; d++)
6150             if (hm[d] < hm[d + 4])
6151               hm[d] = 0;
6152             else if (hm[d] > hm[d + 4])
6153               hm[d + 4] = 0;
6154           for (max = hm[0], d = 1; d < ndir; d++)
6155             if (max < hm[d])
6156               max = hm[d];
6157           max -= max >> 3;
6158           memset(avg, 0, sizeof avg);
6159           for (d = 0; d < ndir; d++)
6160             if (hm[d] >= max)
6161             {
6162               FORC3 avg[c] += rgb[d][row][col][c];
6163               avg[3]++;
6164             }
6165           FORC3 image[(row + top) * width + col + left][c] = avg[c] / avg[3];
6166         }
6167     }
6168   free(buffer);
6169   border_interpolate(8);
6170 }
6171 #undef fcol
6172 
6173 /*
6174    Adaptive Homogeneity-Directed interpolation is based on
6175    the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
6176  */
6177 #ifdef LIBRAW_LIBRARY_BUILD
6178 
ahd_interpolate_green_h_and_v(int top,int left,ushort (* out_rgb)[TS][TS][3])6179 void CLASS ahd_interpolate_green_h_and_v(int top, int left, ushort (*out_rgb)[TS][TS][3])
6180 {
6181   int row, col;
6182   int c, val;
6183   ushort(*pix)[4];
6184   const int rowlimit = MIN(top + TS, height - 2);
6185   const int collimit = MIN(left + TS, width - 2);
6186 
6187   for (row = top; row < rowlimit; row++)
6188   {
6189     col = left + (FC(row, left) & 1);
6190     for (c = FC(row, col); col < collimit; col += 2)
6191     {
6192       pix = image + row * width + col;
6193       val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2 - pix[-2][c] - pix[2][c]) >> 2;
6194       out_rgb[0][row - top][col - left][1] = ULIM(val, pix[-1][1], pix[1][1]);
6195       val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2 - pix[-2 * width][c] - pix[2 * width][c]) >> 2;
6196       out_rgb[1][row - top][col - left][1] = ULIM(val, pix[-width][1], pix[width][1]);
6197     }
6198   }
6199 }
ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(int top,int left,ushort (* inout_rgb)[TS][3],short (* out_lab)[TS][3])6200 void CLASS ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(int top, int left, ushort (*inout_rgb)[TS][3],
6201                                                                 short (*out_lab)[TS][3])
6202 {
6203   unsigned row, col;
6204   int c, val;
6205   ushort(*pix)[4];
6206   ushort(*rix)[3];
6207   short(*lix)[3];
6208   float xyz[3];
6209   const unsigned num_pix_per_row = 4 * width;
6210   const unsigned rowlimit = MIN(top + TS - 1, height - 3);
6211   const unsigned collimit = MIN(left + TS - 1, width - 3);
6212   ushort *pix_above;
6213   ushort *pix_below;
6214   int t1, t2;
6215 
6216   for (row = top + 1; row < rowlimit; row++)
6217   {
6218     pix = image + row * width + left;
6219     rix = &inout_rgb[row - top][0];
6220     lix = &out_lab[row - top][0];
6221 
6222     for (col = left + 1; col < collimit; col++)
6223     {
6224       pix++;
6225       pix_above = &pix[0][0] - num_pix_per_row;
6226       pix_below = &pix[0][0] + num_pix_per_row;
6227       rix++;
6228       lix++;
6229 
6230       c = 2 - FC(row, col);
6231 
6232       if (c == 1)
6233       {
6234         c = FC(row + 1, col);
6235         t1 = 2 - c;
6236         val = pix[0][1] + ((pix[-1][t1] + pix[1][t1] - rix[-1][1] - rix[1][1]) >> 1);
6237         rix[0][t1] = CLIP(val);
6238         val = pix[0][1] + ((pix_above[c] + pix_below[c] - rix[-TS][1] - rix[TS][1]) >> 1);
6239       }
6240       else
6241       {
6242         t1 = -4 + c; /* -4+c: pixel of color c to the left */
6243         t2 = 4 + c;  /* 4+c: pixel of color c to the right */
6244         val = rix[0][1] + ((pix_above[t1] + pix_above[t2] + pix_below[t1] + pix_below[t2] - rix[-TS - 1][1] -
6245                             rix[-TS + 1][1] - rix[+TS - 1][1] - rix[+TS + 1][1] + 1) >>
6246                            2);
6247       }
6248       rix[0][c] = CLIP(val);
6249       c = FC(row, col);
6250       rix[0][c] = pix[0][c];
6251       cielab(rix[0], lix[0]);
6252     }
6253   }
6254 }
ahd_interpolate_r_and_b_and_convert_to_cielab(int top,int left,ushort (* inout_rgb)[TS][TS][3],short (* out_lab)[TS][TS][3])6255 void CLASS ahd_interpolate_r_and_b_and_convert_to_cielab(int top, int left, ushort (*inout_rgb)[TS][TS][3],
6256                                                          short (*out_lab)[TS][TS][3])
6257 {
6258   int direction;
6259   for (direction = 0; direction < 2; direction++)
6260   {
6261     ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(top, left, inout_rgb[direction], out_lab[direction]);
6262   }
6263 }
6264 
ahd_interpolate_build_homogeneity_map(int top,int left,short (* lab)[TS][TS][3],char (* out_homogeneity_map)[TS][2])6265 void CLASS ahd_interpolate_build_homogeneity_map(int top, int left, short (*lab)[TS][TS][3],
6266                                                  char (*out_homogeneity_map)[TS][2])
6267 {
6268   int row, col;
6269   int tr, tc;
6270   int direction;
6271   int i;
6272   short(*lix)[3];
6273   short(*lixs[2])[3];
6274   short *adjacent_lix;
6275   unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
6276   static const int dir[4] = {-1, 1, -TS, TS};
6277   const int rowlimit = MIN(top + TS - 2, height - 4);
6278   const int collimit = MIN(left + TS - 2, width - 4);
6279   int homogeneity;
6280   char(*homogeneity_map_p)[2];
6281 
6282   memset(out_homogeneity_map, 0, 2 * TS * TS);
6283 
6284   for (row = top + 2; row < rowlimit; row++)
6285   {
6286     tr = row - top;
6287     homogeneity_map_p = &out_homogeneity_map[tr][1];
6288     for (direction = 0; direction < 2; direction++)
6289     {
6290       lixs[direction] = &lab[direction][tr][1];
6291     }
6292 
6293     for (col = left + 2; col < collimit; col++)
6294     {
6295       tc = col - left;
6296       homogeneity_map_p++;
6297 
6298       for (direction = 0; direction < 2; direction++)
6299       {
6300         lix = ++lixs[direction];
6301         for (i = 0; i < 4; i++)
6302         {
6303           adjacent_lix = lix[dir[i]];
6304           ldiff[direction][i] = ABS(lix[0][0] - adjacent_lix[0]);
6305           abdiff[direction][i] = SQR(lix[0][1] - adjacent_lix[1]) + SQR(lix[0][2] - adjacent_lix[2]);
6306         }
6307       }
6308       leps = MIN(MAX(ldiff[0][0], ldiff[0][1]), MAX(ldiff[1][2], ldiff[1][3]));
6309       abeps = MIN(MAX(abdiff[0][0], abdiff[0][1]), MAX(abdiff[1][2], abdiff[1][3]));
6310       for (direction = 0; direction < 2; direction++)
6311       {
6312         homogeneity = 0;
6313         for (i = 0; i < 4; i++)
6314         {
6315           if (ldiff[direction][i] <= leps && abdiff[direction][i] <= abeps)
6316           {
6317             homogeneity++;
6318           }
6319         }
6320         homogeneity_map_p[0][direction] = homogeneity;
6321       }
6322     }
6323   }
6324 }
ahd_interpolate_combine_homogeneous_pixels(int top,int left,ushort (* rgb)[TS][TS][3],char (* homogeneity_map)[TS][2])6325 void CLASS ahd_interpolate_combine_homogeneous_pixels(int top, int left, ushort (*rgb)[TS][TS][3],
6326                                                       char (*homogeneity_map)[TS][2])
6327 {
6328   int row, col;
6329   int tr, tc;
6330   int i, j;
6331   int direction;
6332   int hm[2];
6333   int c;
6334   const int rowlimit = MIN(top + TS - 3, height - 5);
6335   const int collimit = MIN(left + TS - 3, width - 5);
6336 
6337   ushort(*pix)[4];
6338   ushort(*rix[2])[3];
6339 
6340   for (row = top + 3; row < rowlimit; row++)
6341   {
6342     tr = row - top;
6343     pix = &image[row * width + left + 2];
6344     for (direction = 0; direction < 2; direction++)
6345     {
6346       rix[direction] = &rgb[direction][tr][2];
6347     }
6348 
6349     for (col = left + 3; col < collimit; col++)
6350     {
6351       tc = col - left;
6352       pix++;
6353       for (direction = 0; direction < 2; direction++)
6354       {
6355         rix[direction]++;
6356       }
6357 
6358       for (direction = 0; direction < 2; direction++)
6359       {
6360         hm[direction] = 0;
6361         for (i = tr - 1; i <= tr + 1; i++)
6362         {
6363           for (j = tc - 1; j <= tc + 1; j++)
6364           {
6365             hm[direction] += homogeneity_map[i][j][direction];
6366           }
6367         }
6368       }
6369       if (hm[0] != hm[1])
6370       {
6371         memcpy(pix[0], rix[hm[1] > hm[0]][0], 3 * sizeof(ushort));
6372       }
6373       else
6374       {
6375         FORC3 { pix[0][c] = (rix[0][0][c] + rix[1][0][c]) >> 1; }
6376       }
6377     }
6378   }
6379 }
ahd_interpolate()6380 void CLASS ahd_interpolate()
6381 {
6382   int i, j, k, top, left;
6383   float xyz_cam[3][4], r;
6384   char *buffer;
6385   ushort(*rgb)[TS][TS][3];
6386   short(*lab)[TS][TS][3];
6387   char(*homo)[TS][2];
6388   int terminate_flag = 0;
6389 
6390   cielab(0, 0);
6391   border_interpolate(5);
6392 
6393 #ifdef LIBRAW_LIBRARY_BUILD
6394 #ifdef LIBRAW_USE_OPENMP
6395 #pragma omp parallel private(buffer, rgb, lab, homo, top, left, i, j, k) shared(xyz_cam, terminate_flag)
6396 #endif
6397 #endif
6398   {
6399     buffer = (char *)malloc(26 * TS * TS); /* 1664 kB */
6400     merror(buffer, "ahd_interpolate()");
6401     rgb = (ushort(*)[TS][TS][3])buffer;
6402     lab = (short(*)[TS][TS][3])(buffer + 12 * TS * TS);
6403     homo = (char(*)[TS][2])(buffer + 24 * TS * TS);
6404 
6405 #ifdef LIBRAW_LIBRARY_BUILD
6406 #ifdef LIBRAW_USE_OPENMP
6407 #pragma omp for schedule(dynamic)
6408 #endif
6409 #endif
6410     for (top = 2; top < height - 5; top += TS - 6)
6411     {
6412 #ifdef LIBRAW_LIBRARY_BUILD
6413 #ifdef LIBRAW_USE_OPENMP
6414       if (0 == omp_get_thread_num())
6415 #endif
6416         if (callbacks.progress_cb)
6417         {
6418           int rr =
6419               (*callbacks.progress_cb)(callbacks.progresscb_data, LIBRAW_PROGRESS_INTERPOLATE, top - 2, height - 7);
6420           if (rr)
6421             terminate_flag = 1;
6422         }
6423 #endif
6424       for (left = 2; !terminate_flag && (left < width - 5); left += TS - 6)
6425       {
6426         ahd_interpolate_green_h_and_v(top, left, rgb);
6427         ahd_interpolate_r_and_b_and_convert_to_cielab(top, left, rgb, lab);
6428         ahd_interpolate_build_homogeneity_map(top, left, lab, homo);
6429         ahd_interpolate_combine_homogeneous_pixels(top, left, rgb, homo);
6430       }
6431     }
6432     free(buffer);
6433   }
6434 #ifdef LIBRAW_LIBRARY_BUILD
6435   if (terminate_flag)
6436     throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;
6437 #endif
6438 }
6439 
6440 #else
ahd_interpolate()6441 void CLASS ahd_interpolate()
6442 {
6443   int i, j, top, left, row, col, tr, tc, c, d, val, hm[2];
6444   static const int dir[4] = {-1, 1, -TS, TS};
6445   unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
6446   ushort(*rgb)[TS][TS][3], (*rix)[3], (*pix)[4];
6447   short(*lab)[TS][TS][3], (*lix)[3];
6448   char(*homo)[TS][TS], *buffer;
6449 
6450 #ifdef DCRAW_VERBOSE
6451   if (verbose)
6452     fprintf(stderr, _("AHD interpolation...\n"));
6453 #endif
6454 
6455   cielab(0, 0);
6456   border_interpolate(5);
6457   buffer = (char *)malloc(26 * TS * TS);
6458   merror(buffer, "ahd_interpolate()");
6459   rgb = (ushort(*)[TS][TS][3])buffer;
6460   lab = (short(*)[TS][TS][3])(buffer + 12 * TS * TS);
6461   homo = (char(*)[TS][TS])(buffer + 24 * TS * TS);
6462 
6463   for (top = 2; top < height - 5; top += TS - 6)
6464     for (left = 2; left < width - 5; left += TS - 6)
6465     {
6466 
6467       /*  Interpolate green horizontally and vertically:		*/
6468       for (row = top; row < top + TS && row < height - 2; row++)
6469       {
6470         col = left + (FC(row, left) & 1);
6471         for (c = FC(row, col); col < left + TS && col < width - 2; col += 2)
6472         {
6473           pix = image + row * width + col;
6474           val = ((pix[-1][1] + pix[0][c] + pix[1][1]) * 2 - pix[-2][c] - pix[2][c]) >> 2;
6475           rgb[0][row - top][col - left][1] = ULIM(val, pix[-1][1], pix[1][1]);
6476           val = ((pix[-width][1] + pix[0][c] + pix[width][1]) * 2 - pix[-2 * width][c] - pix[2 * width][c]) >> 2;
6477           rgb[1][row - top][col - left][1] = ULIM(val, pix[-width][1], pix[width][1]);
6478         }
6479       }
6480 
6481       /*  Interpolate red and blue, and convert to CIELab:		*/
6482       for (d = 0; d < 2; d++)
6483         for (row = top + 1; row < top + TS - 1 && row < height - 3; row++)
6484           for (col = left + 1; col < left + TS - 1 && col < width - 3; col++)
6485           {
6486             pix = image + row * width + col;
6487             rix = &rgb[d][row - top][col - left];
6488             lix = &lab[d][row - top][col - left];
6489             if ((c = 2 - FC(row, col)) == 1)
6490             {
6491               c = FC(row + 1, col);
6492               val = pix[0][1] + ((pix[-1][2 - c] + pix[1][2 - c] - rix[-1][1] - rix[1][1]) >> 1);
6493               rix[0][2 - c] = CLIP(val);
6494               val = pix[0][1] + ((pix[-width][c] + pix[width][c] - rix[-TS][1] - rix[TS][1]) >> 1);
6495             }
6496             else
6497               val = rix[0][1] + ((pix[-width - 1][c] + pix[-width + 1][c] + pix[+width - 1][c] + pix[+width + 1][c] -
6498                                   rix[-TS - 1][1] - rix[-TS + 1][1] - rix[+TS - 1][1] - rix[+TS + 1][1] + 1) >>
6499                                  2);
6500             rix[0][c] = CLIP(val);
6501             c = FC(row, col);
6502             rix[0][c] = pix[0][c];
6503             cielab(rix[0], lix[0]);
6504           }
6505       /*  Build homogeneity maps from the CIELab images:		*/
6506       memset(homo, 0, 2 * TS * TS);
6507       for (row = top + 2; row < top + TS - 2 && row < height - 4; row++)
6508       {
6509         tr = row - top;
6510         for (col = left + 2; col < left + TS - 2 && col < width - 4; col++)
6511         {
6512           tc = col - left;
6513           for (d = 0; d < 2; d++)
6514           {
6515             lix = &lab[d][tr][tc];
6516             for (i = 0; i < 4; i++)
6517             {
6518               ldiff[d][i] = ABS(lix[0][0] - lix[dir[i]][0]);
6519               abdiff[d][i] = SQR(lix[0][1] - lix[dir[i]][1]) + SQR(lix[0][2] - lix[dir[i]][2]);
6520             }
6521           }
6522           leps = MIN(MAX(ldiff[0][0], ldiff[0][1]), MAX(ldiff[1][2], ldiff[1][3]));
6523           abeps = MIN(MAX(abdiff[0][0], abdiff[0][1]), MAX(abdiff[1][2], abdiff[1][3]));
6524           for (d = 0; d < 2; d++)
6525             for (i = 0; i < 4; i++)
6526               if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
6527                 homo[d][tr][tc]++;
6528         }
6529       }
6530       /*  Combine the most homogenous pixels for the final result:	*/
6531       for (row = top + 3; row < top + TS - 3 && row < height - 5; row++)
6532       {
6533         tr = row - top;
6534         for (col = left + 3; col < left + TS - 3 && col < width - 5; col++)
6535         {
6536           tc = col - left;
6537           for (d = 0; d < 2; d++)
6538             for (hm[d] = 0, i = tr - 1; i <= tr + 1; i++)
6539               for (j = tc - 1; j <= tc + 1; j++)
6540                 hm[d] += homo[d][i][j];
6541           if (hm[0] != hm[1])
6542             FORC3 image[row * width + col][c] = rgb[hm[1] > hm[0]][tr][tc][c];
6543           else
6544             FORC3 image[row * width + col][c] = (rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1;
6545         }
6546       }
6547     }
6548   free(buffer);
6549 }
6550 #endif
6551 #undef TS
6552 
median_filter()6553 void CLASS median_filter()
6554 {
6555   ushort(*pix)[4];
6556   int pass, c, i, j, k, med[9];
6557   static const uchar opt[] = /* Optimal 9-element median search */
6558       {1, 2, 4, 5, 7, 8, 0, 1, 3, 4, 6, 7, 1, 2, 4, 5, 7, 8, 0,
6559        3, 5, 8, 4, 7, 3, 6, 1, 4, 2, 5, 4, 7, 4, 2, 6, 4, 4, 2};
6560 
6561   for (pass = 1; pass <= med_passes; pass++)
6562   {
6563 #ifdef LIBRAW_LIBRARY_BUILD
6564     RUN_CALLBACK(LIBRAW_PROGRESS_MEDIAN_FILTER, pass - 1, med_passes);
6565 #endif
6566 #ifdef DCRAW_VERBOSE
6567     if (verbose)
6568       fprintf(stderr, _("Median filter pass %d...\n"), pass);
6569 #endif
6570     for (c = 0; c < 3; c += 2)
6571     {
6572       for (pix = image; pix < image + width * height; pix++)
6573         pix[0][3] = pix[0][c];
6574       for (pix = image + width; pix < image + width * (height - 1); pix++)
6575       {
6576         if ((pix - image + 1) % width < 2)
6577           continue;
6578         for (k = 0, i = -width; i <= width; i += width)
6579           for (j = i - 1; j <= i + 1; j++)
6580             med[k++] = pix[j][3] - pix[j][1];
6581         for (i = 0; i < sizeof opt; i += 2)
6582           if (med[opt[i]] > med[opt[i + 1]])
6583             SWAP(med[opt[i]], med[opt[i + 1]]);
6584         pix[0][c] = CLIP(med[4] + pix[0][1]);
6585       }
6586     }
6587   }
6588 }
6589 
blend_highlights()6590 void CLASS blend_highlights()
6591 {
6592   int clip = INT_MAX, row, col, c, i, j;
6593   static const float trans[2][4][4] = {{{1, 1, 1}, {1.7320508, -1.7320508, 0}, {-1, -1, 2}},
6594                                        {{1, 1, 1, 1}, {1, -1, 1, -1}, {1, 1, -1, -1}, {1, -1, -1, 1}}};
6595   static const float itrans[2][4][4] = {{{1, 0.8660254, -0.5}, {1, -0.8660254, -0.5}, {1, 0, 1}},
6596                                         {{1, 1, 1, 1}, {1, -1, 1, -1}, {1, 1, -1, -1}, {1, -1, -1, 1}}};
6597   float cam[2][4], lab[2][4], sum[2], chratio;
6598 
6599   if ((unsigned)(colors - 3) > 1)
6600     return;
6601 #ifdef DCRAW_VERBOSE
6602   if (verbose)
6603     fprintf(stderr, _("Blending highlights...\n"));
6604 #endif
6605 #ifdef LIBRAW_LIBRARY_BUILD
6606   RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS, 0, 2);
6607 #endif
6608   FORCC if (clip > (i = 65535 * pre_mul[c])) clip = i;
6609   for (row = 0; row < height; row++)
6610     for (col = 0; col < width; col++)
6611     {
6612       FORCC if (image[row * width + col][c] > clip) break;
6613       if (c == colors)
6614         continue;
6615       FORCC
6616       {
6617         cam[0][c] = image[row * width + col][c];
6618         cam[1][c] = MIN(cam[0][c], clip);
6619       }
6620       for (i = 0; i < 2; i++)
6621       {
6622         FORCC for (lab[i][c] = j = 0; j < colors; j++) lab[i][c] += trans[colors - 3][c][j] * cam[i][j];
6623         for (sum[i] = 0, c = 1; c < colors; c++)
6624           sum[i] += SQR(lab[i][c]);
6625       }
6626       chratio = sqrt(sum[1] / sum[0]);
6627       for (c = 1; c < colors; c++)
6628         lab[0][c] *= chratio;
6629       FORCC for (cam[0][c] = j = 0; j < colors; j++) cam[0][c] += itrans[colors - 3][c][j] * lab[0][j];
6630       FORCC image[row * width + col][c] = cam[0][c] / colors;
6631     }
6632 #ifdef LIBRAW_LIBRARY_BUILD
6633   RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS, 1, 2);
6634 #endif
6635 }
6636 
6637 #define SCALE (4 >> shrink)
recover_highlights()6638 void CLASS recover_highlights()
6639 {
6640   float *map, sum, wgt, grow;
6641   int hsat[4], count, spread, change, val, i;
6642   unsigned high, wide, mrow, mcol, row, col, kc, c, d, y, x;
6643   ushort *pixel;
6644   static const signed char dir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
6645 
6646 #ifdef DCRAW_VERBOSE
6647   if (verbose)
6648     fprintf(stderr, _("Rebuilding highlights...\n"));
6649 #endif
6650 
6651   grow = pow(2.0, 4 - highlight);
6652   FORCC hsat[c] = 32000 * pre_mul[c];
6653   for (kc = 0, c = 1; c < colors; c++)
6654     if (pre_mul[kc] < pre_mul[c])
6655       kc = c;
6656   high = height / SCALE;
6657   wide = width / SCALE;
6658   map = (float *)calloc(high, wide * sizeof *map);
6659   merror(map, "recover_highlights()");
6660   FORCC if (c != kc)
6661   {
6662 #ifdef LIBRAW_LIBRARY_BUILD
6663     RUN_CALLBACK(LIBRAW_PROGRESS_HIGHLIGHTS, c - 1, colors - 1);
6664 #endif
6665     memset(map, 0, high * wide * sizeof *map);
6666     for (mrow = 0; mrow < high; mrow++)
6667       for (mcol = 0; mcol < wide; mcol++)
6668       {
6669         sum = wgt = count = 0;
6670         for (row = mrow * SCALE; row < (mrow + 1) * SCALE; row++)
6671           for (col = mcol * SCALE; col < (mcol + 1) * SCALE; col++)
6672           {
6673             pixel = image[row * width + col];
6674             if (pixel[c] / hsat[c] == 1 && pixel[kc] > 24000)
6675             {
6676               sum += pixel[c];
6677               wgt += pixel[kc];
6678               count++;
6679             }
6680           }
6681         if (count == SCALE * SCALE)
6682           map[mrow * wide + mcol] = sum / wgt;
6683       }
6684     for (spread = 32 / grow; spread--;)
6685     {
6686       for (mrow = 0; mrow < high; mrow++)
6687         for (mcol = 0; mcol < wide; mcol++)
6688         {
6689           if (map[mrow * wide + mcol])
6690             continue;
6691           sum = count = 0;
6692           for (d = 0; d < 8; d++)
6693           {
6694             y = mrow + dir[d][0];
6695             x = mcol + dir[d][1];
6696             if (y < high && x < wide && map[y * wide + x] > 0)
6697             {
6698               sum += (1 + (d & 1)) * map[y * wide + x];
6699               count += 1 + (d & 1);
6700             }
6701           }
6702           if (count > 3)
6703             map[mrow * wide + mcol] = -(sum + grow) / (count + grow);
6704         }
6705       for (change = i = 0; i < high * wide; i++)
6706         if (map[i] < 0)
6707         {
6708           map[i] = -map[i];
6709           change = 1;
6710         }
6711       if (!change)
6712         break;
6713     }
6714     for (i = 0; i < high * wide; i++)
6715       if (map[i] == 0)
6716         map[i] = 1;
6717     for (mrow = 0; mrow < high; mrow++)
6718       for (mcol = 0; mcol < wide; mcol++)
6719       {
6720         for (row = mrow * SCALE; row < (mrow + 1) * SCALE; row++)
6721           for (col = mcol * SCALE; col < (mcol + 1) * SCALE; col++)
6722           {
6723             pixel = image[row * width + col];
6724             if (pixel[c] / hsat[c] > 1)
6725             {
6726               val = pixel[kc] * map[mrow * wide + mcol];
6727               if (pixel[c] < val)
6728                 pixel[c] = CLIP(val);
6729             }
6730           }
6731       }
6732   }
6733   free(map);
6734 }
6735 #undef SCALE
6736 
tiff_get(unsigned base,unsigned * tag,unsigned * type,unsigned * len,unsigned * save)6737 void CLASS tiff_get(unsigned base, unsigned *tag, unsigned *type, unsigned *len, unsigned *save)
6738 {
6739 #ifdef LIBRAW_IOSPACE_CHECK
6740   INT64 pos = ftell(ifp);
6741   INT64 fsize = ifp->size();
6742   if(fsize < 12 || (fsize-pos) < 12)
6743      throw LIBRAW_EXCEPTION_IO_EOF;
6744 #endif
6745   *tag = get2();
6746   *type = get2();
6747   *len = get4();
6748   *save = ftell(ifp) + 4;
6749   if (*len * ("11124811248484"[*type < 14 ? *type : 0] - '0') > 4)
6750     fseek(ifp, get4() + base, SEEK_SET);
6751 }
6752 
parse_thumb_note(int base,unsigned toff,unsigned tlen)6753 void CLASS parse_thumb_note(int base, unsigned toff, unsigned tlen)
6754 {
6755   unsigned entries, tag, type, len, save;
6756 
6757   entries = get2();
6758   while (entries--)
6759   {
6760     tiff_get(base, &tag, &type, &len, &save);
6761     if (tag == toff)
6762       thumb_offset = get4() + base;
6763     if (tag == tlen)
6764       thumb_length = get4();
6765     fseek(ifp, save, SEEK_SET);
6766   }
6767 }
6768 
powf_lim(float a,float b,float limup)6769 static float powf_lim(float a, float b, float limup) { return (b > limup || b < -limup) ? 0.f : powf(a, b); }
libraw_powf64l(float a,float b)6770 static float libraw_powf64l(float a, float b) { return powf_lim(a, b, 64.f); }
6771 
6772 #ifdef LIBRAW_LIBRARY_BUILD
6773 
my_roundf(float x)6774 static float my_roundf(float x)
6775 {
6776   float t;
6777   if (x >= 0.0)
6778   {
6779     t = ceilf(x);
6780     if (t - x > 0.5)
6781       t -= 1.0;
6782     return t;
6783   }
6784   else
6785   {
6786     t = ceilf(-x);
6787     if (t + x > 0.5)
6788       t -= 1.0;
6789     return -t;
6790   }
6791 }
6792 
_CanonConvertAperture(ushort in)6793 static float _CanonConvertAperture(ushort in)
6794 {
6795   if ((in == (ushort)0xffe0) || (in == (ushort)0x7fff))
6796     return 0.0f;
6797   return libraw_powf64l(2.0, in / 64.0);
6798 }
6799 
_CanonConvertEV(short in)6800 static float _CanonConvertEV(short in)
6801 {
6802   short EV, Sign, Frac;
6803   float Frac_f;
6804   EV = in;
6805   if (EV < 0)
6806   {
6807     EV = -EV;
6808     Sign = -1;
6809   }
6810   else
6811   {
6812     Sign = 1;
6813   }
6814   Frac = EV & 0x1f;
6815   EV -= Frac; // remove fraction
6816 
6817   if (Frac == 0x0c)
6818   { // convert 1/3 and 2/3 codes
6819     Frac_f = 32.0f / 3.0f;
6820   }
6821   else if (Frac == 0x14)
6822   {
6823     Frac_f = 64.0f / 3.0f;
6824   }
6825   else
6826     Frac_f = (float)Frac;
6827 
6828   return ((float)Sign * ((float)EV + Frac_f)) / 32.0f;
6829 }
6830 
setCanonBodyFeatures(unsigned id)6831 unsigned CLASS setCanonBodyFeatures(unsigned id)
6832 {
6833   if (id == 0x03740000) // EOS M3
6834     id = 0x80000374;
6835   else if (id == 0x03840000) // EOS M10
6836     id = 0x80000384;
6837   else if (id == 0x03940000) // EOS M5
6838     id = 0x80000394;
6839   else if (id == 0x04070000) // EOS M6
6840     id = 0x80000407;
6841   else if (id == 0x03980000) // EOS M100
6842     id = 0x80000398;
6843 
6844   imgdata.lens.makernotes.CamID = id;
6845   if ((id == 0x80000001) || // 1D
6846       (id == 0x80000174) || // 1D2
6847       (id == 0x80000232) || // 1D2N
6848       (id == 0x80000169) || // 1D3
6849       (id == 0x80000281)    // 1D4
6850   )
6851   {
6852     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_APSH;
6853     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Canon_EF;
6854   }
6855   else if ((id == 0x80000167) || // 1Ds
6856            (id == 0x80000188) || // 1Ds2
6857            (id == 0x80000215) || // 1Ds3
6858            (id == 0x80000269) || // 1DX
6859            (id == 0x80000328) || // 1DX2
6860            (id == 0x80000324) || // 1DC
6861            (id == 0x80000213) || // 5D
6862            (id == 0x80000218) || // 5D2
6863            (id == 0x80000285) || // 5D3
6864            (id == 0x80000349) || // 5D4
6865            (id == 0x80000382) || // 5DS
6866            (id == 0x80000401) || // 5DS R
6867            (id == 0x80000302)    // 6D
6868   )
6869   {
6870     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_FF;
6871     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Canon_EF;
6872   }
6873   else if ((id == 0x80000331) || // M
6874            (id == 0x80000355) || // M2
6875            (id == 0x80000374) || // M3
6876            (id == 0x80000384) || // M10
6877            (id == 0x80000394) || // M5
6878            (id == 0x80000407) || // M6
6879            (id == 0x80000398)    // M100
6880   )
6881   {
6882     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_APSC;
6883     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Canon_EF_M;
6884   }
6885   else if ((id == 0x01140000) || // D30
6886            (id == 0x01668000) || // D60
6887            (id > 0x80000000))
6888   {
6889     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_APSC;
6890     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Canon_EF;
6891     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Unknown;
6892   }
6893   else
6894   {
6895     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
6896     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
6897   }
6898 
6899   return id;
6900 }
6901 
processCanonCameraInfo(unsigned id,uchar * CameraInfo,unsigned maxlen,unsigned type)6902 void CLASS processCanonCameraInfo(unsigned id, uchar *CameraInfo, unsigned maxlen, unsigned type)
6903 {
6904   ushort iCanonLensID = 0, iCanonMaxFocal = 0, iCanonMinFocal = 0, iCanonLens = 0, iCanonCurFocal = 0,
6905          iCanonFocalType = 0;
6906   if (maxlen < 16)
6907     return; // too short
6908   CameraInfo[0] = 0;
6909   CameraInfo[1] = 0;
6910   if (type == 4)
6911   {
6912     if ((maxlen == 94) || (maxlen == 138) || (maxlen == 148) || (maxlen == 156) || (maxlen == 162) || (maxlen == 167) ||
6913         (maxlen == 171) || (maxlen == 264) || (maxlen > 400))
6914       imgdata.other.CameraTemperature = sget4(CameraInfo + ((maxlen - 3) << 2));
6915     else if (maxlen == 72)
6916       imgdata.other.CameraTemperature = sget4(CameraInfo + ((maxlen - 1) << 2));
6917     else if ((maxlen == 85) || (maxlen == 93))
6918       imgdata.other.CameraTemperature = sget4(CameraInfo + ((maxlen - 2) << 2));
6919     else if ((maxlen == 96) || (maxlen == 104))
6920       imgdata.other.CameraTemperature = sget4(CameraInfo + ((maxlen - 4) << 2));
6921   }
6922 
6923   switch (id)
6924   {
6925   case 0x80000001: // 1D
6926   case 0x80000167: // 1DS
6927     iCanonCurFocal = 10;
6928     iCanonLensID = 13;
6929     iCanonMinFocal = 14;
6930     iCanonMaxFocal = 16;
6931     if (!imgdata.lens.makernotes.CurFocal)
6932       imgdata.lens.makernotes.CurFocal = sget2(CameraInfo + iCanonCurFocal);
6933     if (!imgdata.lens.makernotes.MinFocal)
6934       imgdata.lens.makernotes.MinFocal = sget2(CameraInfo + iCanonMinFocal);
6935     if (!imgdata.lens.makernotes.MaxFocal)
6936       imgdata.lens.makernotes.MaxFocal = sget2(CameraInfo + iCanonMaxFocal);
6937     imgdata.other.CameraTemperature = 0.0f;
6938     break;
6939   case 0x80000174: // 1DMkII
6940   case 0x80000188: // 1DsMkII
6941     iCanonCurFocal = 9;
6942     iCanonLensID = 12;
6943     iCanonMinFocal = 17;
6944     iCanonMaxFocal = 19;
6945     iCanonFocalType = 45;
6946     break;
6947   case 0x80000232: // 1DMkII N
6948     iCanonCurFocal = 9;
6949     iCanonLensID = 12;
6950     iCanonMinFocal = 17;
6951     iCanonMaxFocal = 19;
6952     break;
6953   case 0x80000169: // 1DMkIII
6954   case 0x80000215: // 1DsMkIII
6955     iCanonCurFocal = 29;
6956     iCanonLensID = 273;
6957     iCanonMinFocal = 275;
6958     iCanonMaxFocal = 277;
6959     break;
6960   case 0x80000281: // 1DMkIV
6961     iCanonCurFocal = 30;
6962     iCanonLensID = 335;
6963     iCanonMinFocal = 337;
6964     iCanonMaxFocal = 339;
6965     break;
6966   case 0x80000269: // 1D X
6967     iCanonCurFocal = 35;
6968     iCanonLensID = 423;
6969     iCanonMinFocal = 425;
6970     iCanonMaxFocal = 427;
6971     break;
6972   case 0x80000213: // 5D
6973     iCanonCurFocal = 40;
6974     if (!sget2Rev(CameraInfo + 12))
6975       iCanonLensID = 151;
6976     else
6977       iCanonLensID = 12;
6978     iCanonMinFocal = 147;
6979     iCanonMaxFocal = 149;
6980     break;
6981   case 0x80000218: // 5DMkII
6982     iCanonCurFocal = 30;
6983     iCanonLensID = 230;
6984     iCanonMinFocal = 232;
6985     iCanonMaxFocal = 234;
6986     break;
6987   case 0x80000285: // 5DMkIII
6988     iCanonCurFocal = 35;
6989     iCanonLensID = 339;
6990     iCanonMinFocal = 341;
6991     iCanonMaxFocal = 343;
6992     break;
6993   case 0x80000302: // 6D
6994     iCanonCurFocal = 35;
6995     iCanonLensID = 353;
6996     iCanonMinFocal = 355;
6997     iCanonMaxFocal = 357;
6998     break;
6999   case 0x80000250: // 7D
7000     iCanonCurFocal = 30;
7001     iCanonLensID = 274;
7002     iCanonMinFocal = 276;
7003     iCanonMaxFocal = 278;
7004     break;
7005   case 0x80000190: // 40D
7006     iCanonCurFocal = 29;
7007     iCanonLensID = 214;
7008     iCanonMinFocal = 216;
7009     iCanonMaxFocal = 218;
7010     iCanonLens = 2347;
7011     break;
7012   case 0x80000261: // 50D
7013     iCanonCurFocal = 30;
7014     iCanonLensID = 234;
7015     iCanonMinFocal = 236;
7016     iCanonMaxFocal = 238;
7017     break;
7018   case 0x80000287: // 60D
7019     iCanonCurFocal = 30;
7020     iCanonLensID = 232;
7021     iCanonMinFocal = 234;
7022     iCanonMaxFocal = 236;
7023     break;
7024   case 0x80000325: // 70D
7025     iCanonCurFocal = 35;
7026     iCanonLensID = 358;
7027     iCanonMinFocal = 360;
7028     iCanonMaxFocal = 362;
7029     break;
7030   case 0x80000176: // 450D
7031     iCanonCurFocal = 29;
7032     iCanonLensID = 222;
7033     iCanonLens = 2355;
7034     break;
7035   case 0x80000252: // 500D
7036     iCanonCurFocal = 30;
7037     iCanonLensID = 246;
7038     iCanonMinFocal = 248;
7039     iCanonMaxFocal = 250;
7040     break;
7041   case 0x80000270: // 550D
7042     iCanonCurFocal = 30;
7043     iCanonLensID = 255;
7044     iCanonMinFocal = 257;
7045     iCanonMaxFocal = 259;
7046     break;
7047   case 0x80000286: // 600D
7048   case 0x80000288: // 1100D
7049     iCanonCurFocal = 30;
7050     iCanonLensID = 234;
7051     iCanonMinFocal = 236;
7052     iCanonMaxFocal = 238;
7053     break;
7054   case 0x80000301: // 650D
7055   case 0x80000326: // 700D
7056     iCanonCurFocal = 35;
7057     iCanonLensID = 295;
7058     iCanonMinFocal = 297;
7059     iCanonMaxFocal = 299;
7060     break;
7061   case 0x80000254: // 1000D
7062     iCanonCurFocal = 29;
7063     iCanonLensID = 226;
7064     iCanonMinFocal = 228;
7065     iCanonMaxFocal = 230;
7066     iCanonLens = 2359;
7067     break;
7068   }
7069   if (iCanonFocalType)
7070   {
7071     if (iCanonFocalType >= maxlen)
7072       return; // broken;
7073     imgdata.lens.makernotes.FocalType = CameraInfo[iCanonFocalType];
7074     if (!imgdata.lens.makernotes.FocalType) // zero means 'fixed' here, replacing with standard '1'
7075       imgdata.lens.makernotes.FocalType = 1;
7076   }
7077   if (!imgdata.lens.makernotes.CurFocal)
7078   {
7079     if (iCanonCurFocal >= maxlen)
7080       return; // broken;
7081     imgdata.lens.makernotes.CurFocal = sget2Rev(CameraInfo + iCanonCurFocal);
7082   }
7083   if (!imgdata.lens.makernotes.LensID)
7084   {
7085     if (iCanonLensID >= maxlen)
7086       return; // broken;
7087     imgdata.lens.makernotes.LensID = sget2Rev(CameraInfo + iCanonLensID);
7088   }
7089   if (!imgdata.lens.makernotes.MinFocal)
7090   {
7091     if (iCanonMinFocal >= maxlen)
7092       return; // broken;
7093     imgdata.lens.makernotes.MinFocal = sget2Rev(CameraInfo + iCanonMinFocal);
7094   }
7095   if (!imgdata.lens.makernotes.MaxFocal)
7096   {
7097     if (iCanonMaxFocal >= maxlen)
7098       return; // broken;
7099     imgdata.lens.makernotes.MaxFocal = sget2Rev(CameraInfo + iCanonMaxFocal);
7100   }
7101   if (!imgdata.lens.makernotes.Lens[0] && iCanonLens)
7102   {
7103     if (iCanonLens + 64 >= maxlen)
7104       return;                        // broken;
7105     if (CameraInfo[iCanonLens] < 65) // non-Canon lens
7106     {
7107       memcpy(imgdata.lens.makernotes.Lens, CameraInfo + iCanonLens, 64);
7108     }
7109     else if (!strncmp((char *)CameraInfo + iCanonLens, "EF-S", 4))
7110     {
7111       memcpy(imgdata.lens.makernotes.Lens, "EF-S ", 5);
7112       memcpy(imgdata.lens.makernotes.LensFeatures_pre, "EF-E", 4);
7113       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF_S;
7114       memcpy(imgdata.lens.makernotes.Lens + 5, CameraInfo + iCanonLens + 4, 60);
7115     }
7116     else if (!strncmp((char *)CameraInfo + iCanonLens, "TS-E", 4))
7117     {
7118       memcpy(imgdata.lens.makernotes.Lens, "TS-E ", 5);
7119       memcpy(imgdata.lens.makernotes.LensFeatures_pre, "TS-E", 4);
7120       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF;
7121       memcpy(imgdata.lens.makernotes.Lens + 5, CameraInfo + iCanonLens + 4, 60);
7122     }
7123     else if (!strncmp((char *)CameraInfo + iCanonLens, "MP-E", 4))
7124     {
7125       memcpy(imgdata.lens.makernotes.Lens, "MP-E ", 5);
7126       memcpy(imgdata.lens.makernotes.LensFeatures_pre, "MP-E", 4);
7127       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF;
7128       memcpy(imgdata.lens.makernotes.Lens + 5, CameraInfo + iCanonLens + 4, 60);
7129     }
7130     else if (!strncmp((char *)CameraInfo + iCanonLens, "EF-M", 4))
7131     {
7132       memcpy(imgdata.lens.makernotes.Lens, "EF-M ", 5);
7133       memcpy(imgdata.lens.makernotes.LensFeatures_pre, "EF-M", 4);
7134       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF_M;
7135       memcpy(imgdata.lens.makernotes.Lens + 5, CameraInfo + iCanonLens + 4, 60);
7136     }
7137     else
7138     {
7139       memcpy(imgdata.lens.makernotes.Lens, CameraInfo + iCanonLens, 2);
7140       memcpy(imgdata.lens.makernotes.LensFeatures_pre, "EF", 2);
7141       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF;
7142       imgdata.lens.makernotes.Lens[2] = 32;
7143       memcpy(imgdata.lens.makernotes.Lens + 3, CameraInfo + iCanonLens + 2, 62);
7144     }
7145   }
7146   return;
7147 }
7148 
Canon_CameraSettings()7149 void CLASS Canon_CameraSettings()
7150 {
7151   fseek(ifp, 10, SEEK_CUR);
7152   imgdata.shootinginfo.DriveMode = get2();
7153   get2();
7154   imgdata.shootinginfo.FocusMode = get2();
7155   fseek(ifp, 18, SEEK_CUR);
7156   imgdata.shootinginfo.MeteringMode = get2();
7157   get2();
7158   imgdata.shootinginfo.AFPoint = get2();
7159   imgdata.shootinginfo.ExposureMode = get2();
7160   get2();
7161   imgdata.lens.makernotes.LensID = get2();
7162   imgdata.lens.makernotes.MaxFocal = get2();
7163   imgdata.lens.makernotes.MinFocal = get2();
7164   imgdata.lens.makernotes.CanonFocalUnits = get2();
7165   if (imgdata.lens.makernotes.CanonFocalUnits > 1)
7166   {
7167     imgdata.lens.makernotes.MaxFocal /= (float)imgdata.lens.makernotes.CanonFocalUnits;
7168     imgdata.lens.makernotes.MinFocal /= (float)imgdata.lens.makernotes.CanonFocalUnits;
7169   }
7170   imgdata.lens.makernotes.MaxAp = _CanonConvertAperture(get2());
7171   imgdata.lens.makernotes.MinAp = _CanonConvertAperture(get2());
7172   fseek(ifp, 12, SEEK_CUR);
7173   imgdata.shootinginfo.ImageStabilization = get2();
7174 }
7175 
Canon_WBpresets(int skip1,int skip2)7176 void CLASS Canon_WBpresets(int skip1, int skip2)
7177 {
7178   int c;
7179   FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][c ^ (c >> 1)] = get2();
7180 
7181   if (skip1)
7182     fseek(ifp, skip1, SEEK_CUR);
7183   FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][c ^ (c >> 1)] = get2();
7184 
7185   if (skip1)
7186     fseek(ifp, skip1, SEEK_CUR);
7187   FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][c ^ (c >> 1)] = get2();
7188 
7189   if (skip1)
7190     fseek(ifp, skip1, SEEK_CUR);
7191   FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][c ^ (c >> 1)] = get2();
7192 
7193   if (skip1)
7194     fseek(ifp, skip1, SEEK_CUR);
7195   FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][c ^ (c >> 1)] = get2();
7196 
7197   if (skip2)
7198     fseek(ifp, skip2, SEEK_CUR);
7199   FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][c ^ (c >> 1)] = get2();
7200 
7201   return;
7202 }
7203 
Canon_WBCTpresets(short WBCTversion)7204 void CLASS Canon_WBCTpresets(short WBCTversion)
7205 {
7206   if (WBCTversion == 0)
7207     for (int i = 0; i < 15; i++) // tint, as shot R, as shot B, CСT
7208     {
7209       imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = 1.0f;
7210       fseek(ifp, 2, SEEK_CUR);
7211       imgdata.color.WBCT_Coeffs[i][1] = 1024.0f / fMAX(get2(), 1.f);
7212       imgdata.color.WBCT_Coeffs[i][3] = 1024.0f / fMAX(get2(), 1.f);
7213       imgdata.color.WBCT_Coeffs[i][0] = get2();
7214     }
7215   else if (WBCTversion == 1)
7216     for (int i = 0; i < 15; i++) // as shot R, as shot B, tint, CСT
7217     {
7218       imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = 1.0f;
7219       imgdata.color.WBCT_Coeffs[i][1] = 1024.0f / fMAX(get2(), 1.f);
7220       imgdata.color.WBCT_Coeffs[i][3] = 1024.0f / fMAX(get2(), 1.f);
7221       fseek(ifp, 2, SEEK_CUR);
7222       imgdata.color.WBCT_Coeffs[i][0] = get2();
7223     }
7224   else if ((WBCTversion == 2) && ((unique_id == 0x80000374) || // M3
7225                                   (unique_id == 0x80000384) || // M10
7226                                   (unique_id == 0x80000394) || // M5
7227                                   (unique_id == 0x80000407) || // M6
7228                                   (unique_id == 0x80000398) || // M100
7229                                   (unique_id == 0x03970000) || // G7 X Mark II
7230                                   (unique_id == 0x04100000) || // G9 X Mark II
7231                                   (unique_id == 0x04180000)))  // G1 X Mark III
7232     for (int i = 0; i < 15; i++)                               // tint, offset, as shot R, as shot B, CСT
7233     {
7234       fseek(ifp, 2, SEEK_CUR);
7235       fseek(ifp, 2, SEEK_CUR);
7236       imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = 1.0f;
7237       imgdata.color.WBCT_Coeffs[i][1] = 1024.0f / fMAX(1.f, get2());
7238       imgdata.color.WBCT_Coeffs[i][3] = 1024.0f / fMAX(1.f, get2());
7239       imgdata.color.WBCT_Coeffs[i][0] = get2();
7240     }
7241   else if ((WBCTversion == 2) && ((unique_id == 0x03950000) || (unique_id == 0x03930000))) // G5 X, G9 X
7242     for (int i = 0; i < 15; i++) // tint, offset, as shot R, as shot B, CСT
7243     {
7244       fseek(ifp, 2, SEEK_CUR);
7245       fseek(ifp, 2, SEEK_CUR);
7246       imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = 1.0f;
7247       imgdata.color.WBCT_Coeffs[i][1] = (float)get2() / 512.0f;
7248       imgdata.color.WBCT_Coeffs[i][3] = (float)get2() / 512.0f;
7249       imgdata.color.WBCT_Coeffs[i][0] = get2();
7250     }
7251   return;
7252 }
7253 
processNikonLensData(uchar * LensData,unsigned len)7254 void CLASS processNikonLensData(uchar *LensData, unsigned len)
7255 {
7256   ushort i;
7257   if (!(imgdata.lens.nikon.NikonLensType & 0x01))
7258   {
7259     imgdata.lens.makernotes.LensFeatures_pre[0] = 'A';
7260     imgdata.lens.makernotes.LensFeatures_pre[1] = 'F';
7261   }
7262   else
7263   {
7264     imgdata.lens.makernotes.LensFeatures_pre[0] = 'M';
7265     imgdata.lens.makernotes.LensFeatures_pre[1] = 'F';
7266   }
7267 
7268   if (imgdata.lens.nikon.NikonLensType & 0x02)
7269   {
7270     if (imgdata.lens.nikon.NikonLensType & 0x04)
7271       imgdata.lens.makernotes.LensFeatures_suf[0] = 'G';
7272     else
7273       imgdata.lens.makernotes.LensFeatures_suf[0] = 'D';
7274     imgdata.lens.makernotes.LensFeatures_suf[1] = ' ';
7275   }
7276 
7277   if (imgdata.lens.nikon.NikonLensType & 0x08)
7278   {
7279     imgdata.lens.makernotes.LensFeatures_suf[2] = 'V';
7280     imgdata.lens.makernotes.LensFeatures_suf[3] = 'R';
7281   }
7282 
7283   if (imgdata.lens.nikon.NikonLensType & 0x10)
7284   {
7285     imgdata.lens.makernotes.LensMount = imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Nikon_CX;
7286     imgdata.lens.makernotes.CameraFormat = imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_1INCH;
7287   }
7288   else
7289     imgdata.lens.makernotes.LensMount = imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Nikon_F;
7290 
7291   if (imgdata.lens.nikon.NikonLensType & 0x20)
7292   {
7293     strcpy(imgdata.lens.makernotes.Adapter, "FT-1");
7294     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Nikon_F;
7295     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Nikon_CX;
7296     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_1INCH;
7297   }
7298 
7299   imgdata.lens.nikon.NikonLensType = imgdata.lens.nikon.NikonLensType & 0xdf;
7300 
7301   if (len < 20)
7302   {
7303     switch (len)
7304     {
7305     case 9:
7306       i = 2;
7307       break;
7308     case 15:
7309       i = 7;
7310       break;
7311     case 16:
7312       i = 8;
7313       break;
7314     }
7315     imgdata.lens.nikon.NikonLensIDNumber = LensData[i];
7316     imgdata.lens.nikon.NikonLensFStops = LensData[i + 1];
7317     imgdata.lens.makernotes.LensFStops = (float)imgdata.lens.nikon.NikonLensFStops / 12.0f;
7318     if (fabsf(imgdata.lens.makernotes.MinFocal) < 1.1f)
7319     {
7320       if ((imgdata.lens.nikon.NikonLensType ^ (uchar)0x01) || LensData[i + 2])
7321         imgdata.lens.makernotes.MinFocal = 5.0f * libraw_powf64l(2.0f, (float)LensData[i + 2] / 24.0f);
7322       if ((imgdata.lens.nikon.NikonLensType ^ (uchar)0x01) || LensData[i + 3])
7323         imgdata.lens.makernotes.MaxFocal = 5.0f * libraw_powf64l(2.0f, (float)LensData[i + 3] / 24.0f);
7324       if ((imgdata.lens.nikon.NikonLensType ^ (uchar)0x01) || LensData[i + 4])
7325         imgdata.lens.makernotes.MaxAp4MinFocal = libraw_powf64l(2.0f, (float)LensData[i + 4] / 24.0f);
7326       if ((imgdata.lens.nikon.NikonLensType ^ (uchar)0x01) || LensData[i + 5])
7327         imgdata.lens.makernotes.MaxAp4MaxFocal = libraw_powf64l(2.0f, (float)LensData[i + 5] / 24.0f);
7328     }
7329     imgdata.lens.nikon.NikonMCUVersion = LensData[i + 6];
7330     if (i != 2)
7331     {
7332       if ((LensData[i - 1]) && (fabsf(imgdata.lens.makernotes.CurFocal) < 1.1f))
7333         imgdata.lens.makernotes.CurFocal = 5.0f * libraw_powf64l(2.0f, (float)LensData[i - 1] / 24.0f);
7334       if (LensData[i + 7])
7335         imgdata.lens.nikon.NikonEffectiveMaxAp = libraw_powf64l(2.0f, (float)LensData[i + 7] / 24.0f);
7336     }
7337     imgdata.lens.makernotes.LensID =
7338         (unsigned long long)LensData[i] << 56 | (unsigned long long)LensData[i + 1] << 48 |
7339         (unsigned long long)LensData[i + 2] << 40 | (unsigned long long)LensData[i + 3] << 32 |
7340         (unsigned long long)LensData[i + 4] << 24 | (unsigned long long)LensData[i + 5] << 16 |
7341         (unsigned long long)LensData[i + 6] << 8 | (unsigned long long)imgdata.lens.nikon.NikonLensType;
7342   }
7343   else if ((len == 459) || (len == 590))
7344   {
7345     memcpy(imgdata.lens.makernotes.Lens, LensData + 390, 64);
7346   }
7347   else if (len == 509)
7348   {
7349     memcpy(imgdata.lens.makernotes.Lens, LensData + 391, 64);
7350   }
7351   else if (len == 879)
7352   {
7353     memcpy(imgdata.lens.makernotes.Lens, LensData + 680, 64);
7354   }
7355   return;
7356 }
7357 
setOlympusBodyFeatures(unsigned long long id)7358 void CLASS setOlympusBodyFeatures(unsigned long long id)
7359 {
7360   imgdata.lens.makernotes.CamID = id;
7361   if (id == 0x5330303638ULL)
7362   {
7363     strcpy(model, "E-M10MarkIII");
7364   }
7365   if ((id == 0x4434303430ULL) || // E-1
7366       (id == 0x4434303431ULL) || // E-300
7367       ((id & 0x00ffff0000ULL) == 0x0030300000ULL))
7368   {
7369     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_FT;
7370     if ((id == 0x4434303430ULL) ||                              // E-1
7371         (id == 0x4434303431ULL) ||                              // E-330
7372         ((id >= 0x5330303033ULL) && (id <= 0x5330303138ULL)) || // E-330 to E-520
7373         (id == 0x5330303233ULL) ||                              // E-620
7374         (id == 0x5330303239ULL) ||                              // E-450
7375         (id == 0x5330303330ULL) ||                              // E-600
7376         (id == 0x5330303333ULL))                                // E-5
7377     {
7378       imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FT;
7379     }
7380     else
7381     {
7382       imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_mFT;
7383     }
7384   }
7385   else
7386   {
7387     imgdata.lens.makernotes.LensMount = imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
7388   }
7389   return;
7390 }
7391 
parseCanonMakernotes(unsigned tag,unsigned type,unsigned len)7392 void CLASS parseCanonMakernotes(unsigned tag, unsigned type, unsigned len)
7393 {
7394 
7395   if (tag == 0x0001)
7396     Canon_CameraSettings();
7397   else if (tag == 0x0002) // focal length
7398   {
7399     imgdata.lens.makernotes.FocalType = get2();
7400     imgdata.lens.makernotes.CurFocal = get2();
7401     if (imgdata.lens.makernotes.CanonFocalUnits > 1)
7402     {
7403       imgdata.lens.makernotes.CurFocal /= (float)imgdata.lens.makernotes.CanonFocalUnits;
7404     }
7405   }
7406 
7407   else if (tag == 0x0004) // shot info
7408   {
7409     short tempAp;
7410 
7411     fseek(ifp, 24, SEEK_CUR);
7412     tempAp = get2();
7413     if (tempAp != 0)
7414       imgdata.other.CameraTemperature = (float)(tempAp - 128);
7415     tempAp = get2();
7416     if (tempAp != -1)
7417       imgdata.other.FlashGN = ((float)tempAp) / 32;
7418     get2();
7419 
7420     // fseek(ifp, 30, SEEK_CUR);
7421     imgdata.other.FlashEC = _CanonConvertEV((signed short)get2());
7422     fseek(ifp, 8 - 32, SEEK_CUR);
7423     if ((tempAp = get2()) != 0x7fff)
7424       imgdata.lens.makernotes.CurAp = _CanonConvertAperture(tempAp);
7425     if (imgdata.lens.makernotes.CurAp < 0.7f)
7426     {
7427       fseek(ifp, 32, SEEK_CUR);
7428       imgdata.lens.makernotes.CurAp = _CanonConvertAperture(get2());
7429     }
7430     if (!aperture)
7431       aperture = imgdata.lens.makernotes.CurAp;
7432   }
7433 
7434   else if (tag == 0x000c)
7435   {
7436     unsigned tS = get4();
7437     sprintf (imgdata.shootinginfo.BodySerial, "%d", tS);
7438   }
7439 
7440   else if (tag == 0x0095 && // lens model tag
7441            !imgdata.lens.makernotes.Lens[0])
7442   {
7443     fread(imgdata.lens.makernotes.Lens, 2, 1, ifp);
7444     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF;
7445     if (imgdata.lens.makernotes.Lens[0] < 65) // non-Canon lens
7446       fread(imgdata.lens.makernotes.Lens + 2, 62, 1, ifp);
7447     else
7448     {
7449       char efs[2];
7450       imgdata.lens.makernotes.LensFeatures_pre[0] = imgdata.lens.makernotes.Lens[0];
7451       imgdata.lens.makernotes.LensFeatures_pre[1] = imgdata.lens.makernotes.Lens[1];
7452       fread(efs, 2, 1, ifp);
7453       if (efs[0] == 45 && (efs[1] == 83 || efs[1] == 69 || efs[1] == 77))
7454       { // "EF-S, TS-E, MP-E, EF-M" lenses
7455         imgdata.lens.makernotes.Lens[2] = imgdata.lens.makernotes.LensFeatures_pre[2] = efs[0];
7456         imgdata.lens.makernotes.Lens[3] = imgdata.lens.makernotes.LensFeatures_pre[3] = efs[1];
7457         imgdata.lens.makernotes.Lens[4] = 32;
7458         if (efs[1] == 83)
7459         {
7460           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF_S;
7461           imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_APSC;
7462         }
7463         else if (efs[1] == 77)
7464         {
7465           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF_M;
7466         }
7467       }
7468       else
7469       { // "EF" lenses
7470         imgdata.lens.makernotes.Lens[2] = 32;
7471         imgdata.lens.makernotes.Lens[3] = efs[0];
7472         imgdata.lens.makernotes.Lens[4] = efs[1];
7473       }
7474       fread(imgdata.lens.makernotes.Lens + 5, 58, 1, ifp);
7475     }
7476   }
7477 
7478   else if (tag == 0x009a)
7479   {
7480     get4();
7481     imgdata.sizes.raw_crop.cwidth = get4();
7482     imgdata.sizes.raw_crop.cheight = get4();
7483     imgdata.sizes.raw_crop.cleft = get4();
7484     imgdata.sizes.raw_crop.ctop = get4();
7485   }
7486 
7487   else if (tag == 0x00a9)
7488   {
7489     long int save1 = ftell(ifp);
7490     int c;
7491     fseek(ifp, (0x1 << 1), SEEK_CUR);
7492     FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7493     Canon_WBpresets(0, 0);
7494     fseek(ifp, save1, SEEK_SET);
7495   }
7496 
7497   else if (tag == 0x00e0) // sensor info
7498   {
7499     imgdata.makernotes.canon.SensorWidth = (get2(), get2());
7500     imgdata.makernotes.canon.SensorHeight = get2();
7501     imgdata.makernotes.canon.SensorLeftBorder = (get2(), get2(), get2());
7502     imgdata.makernotes.canon.SensorTopBorder = get2();
7503     imgdata.makernotes.canon.SensorRightBorder = get2();
7504     imgdata.makernotes.canon.SensorBottomBorder = get2();
7505     imgdata.makernotes.canon.BlackMaskLeftBorder = get2();
7506     imgdata.makernotes.canon.BlackMaskTopBorder = get2();
7507     imgdata.makernotes.canon.BlackMaskRightBorder = get2();
7508     imgdata.makernotes.canon.BlackMaskBottomBorder = get2();
7509   }
7510 
7511   else if (tag == 0x4013)
7512   {
7513     get4();
7514     imgdata.makernotes.canon.AFMicroAdjMode = get4();
7515     imgdata.makernotes.canon.AFMicroAdjValue = ((float)get4()) / ((float)get4());
7516   }
7517 
7518   else if (tag == 0x4001 && len > 500)
7519   {
7520     int c;
7521     long int save1 = ftell(ifp);
7522     switch (len)
7523     {
7524     case 582:
7525       imgdata.makernotes.canon.CanonColorDataVer = 1; // 20D / 350D
7526       {
7527         fseek(ifp, save1 + (0x1e << 1), SEEK_SET);
7528         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7529         fseek(ifp, save1 + (0x41 << 1), SEEK_SET);
7530         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1][c ^ (c >> 1)] = get2();
7531         fseek(ifp, save1 + (0x46 << 1), SEEK_SET);
7532         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom2][c ^ (c >> 1)] = get2();
7533 
7534         fseek(ifp, save1 + (0x23 << 1), SEEK_SET);
7535         Canon_WBpresets(2, 2);
7536         fseek(ifp, save1 + (0x4b << 1), SEEK_SET);
7537         Canon_WBCTpresets(1); // ABCT
7538       }
7539       break;
7540     case 653:
7541       imgdata.makernotes.canon.CanonColorDataVer = 2; // 1Dmk2 / 1DsMK2
7542       {
7543         fseek(ifp, save1 + (0x18 << 1), SEEK_SET);
7544         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7545         fseek(ifp, save1 + (0x90 << 1), SEEK_SET);
7546         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1][c ^ (c >> 1)] = get2();
7547         fseek(ifp, save1 + (0x95 << 1), SEEK_SET);
7548         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom2][c ^ (c >> 1)] = get2();
7549         fseek(ifp, save1 + (0x9a << 1), SEEK_SET);
7550         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom3][c ^ (c >> 1)] = get2();
7551 
7552         fseek(ifp, save1 + (0x27 << 1), SEEK_SET);
7553         Canon_WBpresets(2, 12);
7554         fseek(ifp, save1 + (0xa4 << 1), SEEK_SET);
7555         Canon_WBCTpresets(1); // ABCT
7556       }
7557       break;
7558     case 796:
7559       imgdata.makernotes.canon.CanonColorDataVer = 3; // 1DmkIIN / 5D / 30D / 400D
7560       imgdata.makernotes.canon.CanonColorDataSubVer = get2();
7561       {
7562         fseek(ifp, save1 + (0x44 << 1), SEEK_SET);
7563         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7564         fseek(ifp, save1 + (0x49 << 1), SEEK_SET);
7565         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Measured][c ^ (c >> 1)] = get2();
7566         fseek(ifp, save1 + (0x71 << 1), SEEK_SET);
7567         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1][c ^ (c >> 1)] = get2();
7568         fseek(ifp, save1 + (0x76 << 1), SEEK_SET);
7569         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom2][c ^ (c >> 1)] = get2();
7570         fseek(ifp, save1 + (0x7b << 1), SEEK_SET);
7571         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom3][c ^ (c >> 1)] = get2();
7572         fseek(ifp, save1 + (0x80 << 1), SEEK_SET);
7573         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom][c ^ (c >> 1)] = get2();
7574 
7575         fseek(ifp, save1 + (0x4e << 1), SEEK_SET);
7576         Canon_WBpresets(2, 12);
7577         fseek(ifp, save1 + (0x85 << 1), SEEK_SET);
7578         Canon_WBCTpresets(0);                       // BCAT
7579         fseek(ifp, save1 + (0x0c4 << 1), SEEK_SET); // offset 196 short
7580         int bls = 0;
7581         FORC4
7582         bls += (imgdata.makernotes.canon.ChannelBlackLevel[c] = get2());
7583         imgdata.makernotes.canon.AverageBlackLevel = bls / 4;
7584       }
7585       break;
7586     // 1DmkIII / 1DSmkIII / 1DmkIV / 5DmkII
7587     // 7D / 40D / 50D / 60D / 450D / 500D
7588     // 550D / 1000D / 1100D
7589     case 674:
7590     case 692:
7591     case 702:
7592     case 1227:
7593     case 1250:
7594     case 1251:
7595     case 1337:
7596     case 1338:
7597     case 1346:
7598       imgdata.makernotes.canon.CanonColorDataVer = 4;
7599       imgdata.makernotes.canon.CanonColorDataSubVer = get2();
7600       {
7601         fseek(ifp, save1 + (0x44 << 1), SEEK_SET);
7602         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7603         fseek(ifp, save1 + (0x49 << 1), SEEK_SET);
7604         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Measured][c ^ (c >> 1)] = get2();
7605 
7606         fseek(ifp, save1 + (0x53 << 1), SEEK_SET);
7607         Canon_WBpresets(2, 12);
7608         fseek(ifp, save1 + (0xa8 << 1), SEEK_SET);
7609         Canon_WBCTpresets(0);                       // BCAT
7610         fseek(ifp, save1 + (0x0e7 << 1), SEEK_SET); // offset 231 short
7611         int bls = 0;
7612         FORC4
7613         bls += (imgdata.makernotes.canon.ChannelBlackLevel[c] = get2());
7614         imgdata.makernotes.canon.AverageBlackLevel = bls / 4;
7615       }
7616       if ((imgdata.makernotes.canon.CanonColorDataSubVer == 4) || (imgdata.makernotes.canon.CanonColorDataSubVer == 5))
7617       {
7618         fseek(ifp, save1 + (0x2b8 << 1), SEEK_SET); // offset 696 shorts
7619         imgdata.makernotes.canon.NormalWhiteLevel = get2();
7620         imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7621         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7622       }
7623       else if ((imgdata.makernotes.canon.CanonColorDataSubVer == 6) ||
7624                (imgdata.makernotes.canon.CanonColorDataSubVer == 7))
7625       {
7626         fseek(ifp, save1 + (0x2cf << 1), SEEK_SET); // offset 719 shorts
7627         imgdata.makernotes.canon.NormalWhiteLevel = get2();
7628         imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7629         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7630       }
7631       else if (imgdata.makernotes.canon.CanonColorDataSubVer == 9)
7632       {
7633         fseek(ifp, save1 + (0x2d3 << 1), SEEK_SET); // offset 723 shorts
7634         imgdata.makernotes.canon.NormalWhiteLevel = get2();
7635         imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7636         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7637       }
7638       break;
7639 
7640     case 5120:
7641       imgdata.makernotes.canon.CanonColorDataVer = 5; // PowerSot G10, G12, G5 X, G7 X, G9 X, EOS M3, EOS M5, EOS M6
7642       {
7643         if ((unique_id == 0x03970000) || // G7 X Mark II
7644             (unique_id == 0x04100000) || // G9 X Mark II
7645             (unique_id == 0x04180000) || // G1 X Mark III
7646             (unique_id == 0x80000394) || // EOS M5
7647             (unique_id == 0x80000398) || // EOS M100
7648             (unique_id == 0x80000407))   // EOS M6
7649         {
7650           fseek(ifp, save1 + (0x4f << 1), SEEK_SET);
7651           FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7652           fseek(ifp, 8, SEEK_CUR);
7653           FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Measured][c ^ (c >> 1)] = get2();
7654           fseek(ifp, 8, SEEK_CUR);
7655           FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Other][c ^ (c >> 1)] = get2();
7656           fseek(ifp, 8, SEEK_CUR);
7657           Canon_WBpresets(8, 24);
7658           fseek(ifp, 168, SEEK_CUR);
7659           FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_WW][c ^ (c >> 1)] = get2();
7660           fseek(ifp, 24, SEEK_CUR);
7661           Canon_WBCTpresets(2); // BCADT
7662           fseek(ifp, 6, SEEK_CUR);
7663         }
7664         else
7665         {
7666           fseek(ifp, save1 + (0x4c << 1), SEEK_SET);
7667           FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7668           get2();
7669           FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Measured][c ^ (c >> 1)] = get2();
7670           get2();
7671           FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Other][c ^ (c >> 1)] = get2();
7672           get2();
7673           Canon_WBpresets(2, 12);
7674           fseek(ifp, save1 + (0xba << 1), SEEK_SET);
7675           Canon_WBCTpresets(2);                       // BCADT
7676           fseek(ifp, save1 + (0x108 << 1), SEEK_SET); // offset 264 short
7677         }
7678         int bls = 0;
7679         FORC4 bls += (imgdata.makernotes.canon.ChannelBlackLevel[c] = get2());
7680         imgdata.makernotes.canon.AverageBlackLevel = bls / 4;
7681       }
7682       break;
7683 
7684     case 1273:
7685     case 1275:
7686       imgdata.makernotes.canon.CanonColorDataVer = 6; // 600D / 1200D
7687       imgdata.makernotes.canon.CanonColorDataSubVer = get2();
7688       {
7689         fseek(ifp, save1 + (0x44 << 1), SEEK_SET);
7690         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7691         fseek(ifp, save1 + (0x49 << 1), SEEK_SET);
7692         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Measured][c ^ (c >> 1)] = get2();
7693 
7694         fseek(ifp, save1 + (0x67 << 1), SEEK_SET);
7695         Canon_WBpresets(2, 12);
7696         fseek(ifp, save1 + (0xbc << 1), SEEK_SET);
7697         Canon_WBCTpresets(0);                       // BCAT
7698         fseek(ifp, save1 + (0x0fb << 1), SEEK_SET); // offset 251 short
7699         int bls = 0;
7700         FORC4
7701         bls += (imgdata.makernotes.canon.ChannelBlackLevel[c] = get2());
7702         imgdata.makernotes.canon.AverageBlackLevel = bls / 4;
7703       }
7704       fseek(ifp, save1 + (0x1e3 << 1), SEEK_SET); // offset 483 shorts
7705       imgdata.makernotes.canon.NormalWhiteLevel = get2();
7706       imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7707       FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7708       break;
7709 
7710     // 1DX / 5DmkIII / 6D / 100D / 650D / 700D / EOS M / 7DmkII / 750D / 760D
7711     case 1312:
7712     case 1313:
7713     case 1316:
7714     case 1506:
7715       imgdata.makernotes.canon.CanonColorDataVer = 7;
7716       imgdata.makernotes.canon.CanonColorDataSubVer = get2();
7717       {
7718         fseek(ifp, save1 + (0x44 << 1), SEEK_SET);
7719         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7720         fseek(ifp, save1 + (0x49 << 1), SEEK_SET);
7721         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Measured][c ^ (c >> 1)] = get2();
7722 
7723         fseek(ifp, save1 + (0x80 << 1), SEEK_SET);
7724         Canon_WBpresets(2, 12);
7725         fseek(ifp, save1 + (0xd5 << 1), SEEK_SET);
7726         Canon_WBCTpresets(0);                       // BCAT
7727         fseek(ifp, save1 + (0x114 << 1), SEEK_SET); // offset 276 shorts
7728         int bls = 0;
7729         FORC4
7730         bls += (imgdata.makernotes.canon.ChannelBlackLevel[c] = get2());
7731         imgdata.makernotes.canon.AverageBlackLevel = bls / 4;
7732       }
7733       if (imgdata.makernotes.canon.CanonColorDataSubVer == 10)
7734       {
7735         fseek(ifp, save1 + (0x1fc << 1), SEEK_SET); // offset 508 shorts
7736         imgdata.makernotes.canon.NormalWhiteLevel = get2();
7737         imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7738         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7739       }
7740       else if (imgdata.makernotes.canon.CanonColorDataSubVer == 11)
7741       {
7742         fseek(ifp, save1 + (0x2dc << 1), SEEK_SET); // offset 732 shorts
7743         imgdata.makernotes.canon.NormalWhiteLevel = get2();
7744         imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7745         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7746       }
7747       break;
7748 
7749     // 5DS / 5DS R / 80D / 1300D / 5D4 / 800D / 77D / 6D II / 200D
7750     case 1560:
7751     case 1592:
7752     case 1353:
7753     case 1602:
7754       imgdata.makernotes.canon.CanonColorDataVer = 8;
7755       imgdata.makernotes.canon.CanonColorDataSubVer = get2();
7756       {
7757         fseek(ifp, save1 + (0x44 << 1), SEEK_SET);
7758         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
7759         fseek(ifp, save1 + (0x49 << 1), SEEK_SET);
7760         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Measured][c ^ (c >> 1)] = get2();
7761 
7762         fseek(ifp, save1 + (0x85 << 1), SEEK_SET);
7763         Canon_WBpresets(2, 12);
7764         fseek(ifp, save1 + (0x107 << 1), SEEK_SET);
7765         Canon_WBCTpresets(0);                       // BCAT
7766         fseek(ifp, save1 + (0x146 << 1), SEEK_SET); // offset 326 shorts
7767         int bls = 0;
7768         FORC4
7769         bls += (imgdata.makernotes.canon.ChannelBlackLevel[c] = get2());
7770         imgdata.makernotes.canon.AverageBlackLevel = bls / 4;
7771       }
7772       if (imgdata.makernotes.canon.CanonColorDataSubVer == 14) // 1300D
7773       {
7774         fseek(ifp, save1 + (0x230 << 1), SEEK_SET); // offset 560 shorts
7775         imgdata.makernotes.canon.NormalWhiteLevel = get2();
7776         imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7777         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7778       }
7779       else
7780       {
7781         fseek(ifp, save1 + (0x30e << 1), SEEK_SET); // offset 782 shorts
7782         imgdata.makernotes.canon.NormalWhiteLevel = get2();
7783         imgdata.makernotes.canon.SpecularWhiteLevel = get2();
7784         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.canon.SpecularWhiteLevel;
7785       }
7786       break;
7787     }
7788     fseek(ifp, save1, SEEK_SET);
7789   }
7790 }
7791 
setPentaxBodyFeatures(unsigned id)7792 void CLASS setPentaxBodyFeatures(unsigned id)
7793 {
7794   imgdata.lens.makernotes.CamID = id;
7795 
7796   switch (id)
7797   {
7798   case 0x12994:
7799   case 0x12aa2:
7800   case 0x12b1a:
7801   case 0x12b60:
7802   case 0x12b62:
7803   case 0x12b7e:
7804   case 0x12b80:
7805   case 0x12b9c:
7806   case 0x12b9d:
7807   case 0x12ba2:
7808   case 0x12c1e:
7809   case 0x12c20:
7810   case 0x12cd2:
7811   case 0x12cd4:
7812   case 0x12cfa:
7813   case 0x12d72:
7814   case 0x12d73:
7815   case 0x12db8:
7816   case 0x12dfe:
7817   case 0x12e6c:
7818   case 0x12e76:
7819   case 0x12ef8:
7820   case 0x12f52:
7821   case 0x12f70:
7822   case 0x12f71:
7823   case 0x12fb6:
7824   case 0x12fc0:
7825   case 0x12fca:
7826   case 0x1301a:
7827   case 0x13024:
7828   case 0x1309c:
7829   case 0x13222:
7830   case 0x1322c:
7831     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Pentax_K;
7832     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Pentax_K;
7833     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_APSC;
7834     break;
7835   case 0x13092:
7836     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Pentax_K;
7837     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Pentax_K;
7838     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_FF;
7839     break;
7840   case 0x12e08:
7841   case 0x13010:
7842     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Pentax_645;
7843     imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_MF;
7844     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Pentax_645;
7845     imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_MF;
7846     break;
7847   case 0x12ee4:
7848   case 0x12f66:
7849   case 0x12f7a:
7850   case 0x1302e:
7851     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Pentax_Q;
7852     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Pentax_Q;
7853     break;
7854   default:
7855     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
7856     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
7857   }
7858   return;
7859 }
7860 
PentaxISO(ushort c)7861 void CLASS PentaxISO(ushort c)
7862 {
7863   int code[] = {3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  15,   16,   17,  18,  19,  20,
7864                 21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33,   34,   35,  36,  37,  38,
7865                 39,  40,  41,  42,  43,  44,  45,  50,  100, 200, 400, 800, 1600, 3200, 258, 259, 260, 261,
7866                 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274,  275,  276, 277, 278};
7867   double value[] = {50,     64,     80,     100,    125,    160,    200,    250,   320,   400,    500,    640,
7868                     800,    1000,   1250,   1600,   2000,   2500,   3200,   4000,  5000,  6400,   8000,   10000,
7869                     12800,  16000,  20000,  25600,  32000,  40000,  51200,  64000, 80000, 102400, 128000, 160000,
7870                     204800, 258000, 325000, 409600, 516000, 650000, 819200, 50,    100,   200,    400,    800,
7871                     1600,   3200,   50,     70,     100,    140,    200,    280,   400,   560,    800,    1100,
7872                     1600,   2200,   3200,   4500,   6400,   9000,   12800,  18000, 25600, 36000,  51200};
7873 #define numel (sizeof(code) / sizeof(code[0]))
7874   int i;
7875   for (i = 0; i < numel; i++)
7876   {
7877     if (code[i] == c)
7878     {
7879       iso_speed = value[i];
7880       return;
7881     }
7882   }
7883   if (i == numel)
7884     iso_speed = 65535.0f;
7885 }
7886 #undef numel
7887 
PentaxLensInfo(unsigned id,unsigned len)7888 void CLASS PentaxLensInfo(unsigned id, unsigned len) // tag 0x0207
7889 {
7890   ushort iLensData = 0;
7891   uchar *table_buf;
7892   table_buf = (uchar *)malloc(MAX(len, 128));
7893   fread(table_buf, len, 1, ifp);
7894   if ((id < 0x12b9c) || (((id == 0x12b9c) ||  // K100D
7895                           (id == 0x12b9d) ||  // K110D
7896                           (id == 0x12ba2)) && // K100D Super
7897                          ((!table_buf[20] || (table_buf[20] == 0xff)))))
7898   {
7899     iLensData = 3;
7900     if (imgdata.lens.makernotes.LensID == -1)
7901       imgdata.lens.makernotes.LensID = (((unsigned)table_buf[0]) << 8) + table_buf[1];
7902   }
7903   else
7904     switch (len)
7905     {
7906     case 90: // LensInfo3
7907       iLensData = 13;
7908       if (imgdata.lens.makernotes.LensID == -1)
7909         imgdata.lens.makernotes.LensID = ((unsigned)((table_buf[1] & 0x0f) + table_buf[3]) << 8) + table_buf[4];
7910       break;
7911     case 91: // LensInfo4
7912       iLensData = 12;
7913       if (imgdata.lens.makernotes.LensID == -1)
7914         imgdata.lens.makernotes.LensID = ((unsigned)((table_buf[1] & 0x0f) + table_buf[3]) << 8) + table_buf[4];
7915       break;
7916     case 80: // LensInfo5
7917     case 128:
7918       iLensData = 15;
7919       if (imgdata.lens.makernotes.LensID == -1)
7920         imgdata.lens.makernotes.LensID = ((unsigned)((table_buf[1] & 0x0f) + table_buf[4]) << 8) + table_buf[5];
7921       break;
7922     default:
7923       if (id >= 0x12b9c) // LensInfo2
7924       {
7925         iLensData = 4;
7926         if (imgdata.lens.makernotes.LensID == -1)
7927           imgdata.lens.makernotes.LensID = ((unsigned)((table_buf[0] & 0x0f) + table_buf[2]) << 8) + table_buf[3];
7928       }
7929     }
7930   if (iLensData)
7931   {
7932     if (table_buf[iLensData + 9] && (fabs(imgdata.lens.makernotes.CurFocal) < 0.1f))
7933       imgdata.lens.makernotes.CurFocal =
7934           10 * (table_buf[iLensData + 9] >> 2) * libraw_powf64l(4, (table_buf[iLensData + 9] & 0x03) - 2);
7935     if (table_buf[iLensData + 10] & 0xf0)
7936       imgdata.lens.makernotes.MaxAp4CurFocal =
7937           libraw_powf64l(2.0f, (float)((table_buf[iLensData + 10] & 0xf0) >> 4) / 4.0f);
7938     if (table_buf[iLensData + 10] & 0x0f)
7939       imgdata.lens.makernotes.MinAp4CurFocal =
7940           libraw_powf64l(2.0f, (float)((table_buf[iLensData + 10] & 0x0f) + 10) / 4.0f);
7941 
7942     if (iLensData != 12)
7943     {
7944       switch (table_buf[iLensData] & 0x06)
7945       {
7946       case 0:
7947         imgdata.lens.makernotes.MinAp4MinFocal = 22.0f;
7948         break;
7949       case 2:
7950         imgdata.lens.makernotes.MinAp4MinFocal = 32.0f;
7951         break;
7952       case 4:
7953         imgdata.lens.makernotes.MinAp4MinFocal = 45.0f;
7954         break;
7955       case 6:
7956         imgdata.lens.makernotes.MinAp4MinFocal = 16.0f;
7957         break;
7958       }
7959       if (table_buf[iLensData] & 0x70)
7960         imgdata.lens.makernotes.LensFStops = ((float)(((table_buf[iLensData] & 0x70) >> 4) ^ 0x07)) / 2.0f + 5.0f;
7961 
7962       imgdata.lens.makernotes.MinFocusDistance = (float)(table_buf[iLensData + 3] & 0xf8);
7963       imgdata.lens.makernotes.FocusRangeIndex = (float)(table_buf[iLensData + 3] & 0x07);
7964 
7965       if ((table_buf[iLensData + 14] > 1) && (fabs(imgdata.lens.makernotes.MaxAp4CurFocal) < 0.7f))
7966         imgdata.lens.makernotes.MaxAp4CurFocal =
7967             libraw_powf64l(2.0f, (float)((table_buf[iLensData + 14] & 0x7f) - 1) / 32.0f);
7968     }
7969     else if ((id != 0x12e76) && // K-5
7970              (table_buf[iLensData + 15] > 1) && (fabs(imgdata.lens.makernotes.MaxAp4CurFocal) < 0.7f))
7971     {
7972       imgdata.lens.makernotes.MaxAp4CurFocal =
7973           libraw_powf64l(2.0f, (float)((table_buf[iLensData + 15] & 0x7f) - 1) / 32.0f);
7974     }
7975   }
7976   free(table_buf);
7977   return;
7978 }
7979 
setPhaseOneFeatures(unsigned id)7980 void CLASS setPhaseOneFeatures(unsigned id)
7981 {
7982 
7983   ushort i;
7984   static const struct
7985   {
7986     ushort id;
7987     char t_model[32];
7988   } p1_unique[] = {
7989       // Phase One section:
7990       {1, "Hasselblad V"},
7991       {10, "PhaseOne/Mamiya"},
7992       {12, "Contax 645"},
7993       {16, "Hasselblad V"},
7994       {17, "Hasselblad V"},
7995       {18, "Contax 645"},
7996       {19, "PhaseOne/Mamiya"},
7997       {20, "Hasselblad V"},
7998       {21, "Contax 645"},
7999       {22, "PhaseOne/Mamiya"},
8000       {23, "Hasselblad V"},
8001       {24, "Hasselblad H"},
8002       {25, "PhaseOne/Mamiya"},
8003       {32, "Contax 645"},
8004       {34, "Hasselblad V"},
8005       {35, "Hasselblad V"},
8006       {36, "Hasselblad H"},
8007       {37, "Contax 645"},
8008       {38, "PhaseOne/Mamiya"},
8009       {39, "Hasselblad V"},
8010       {40, "Hasselblad H"},
8011       {41, "Contax 645"},
8012       {42, "PhaseOne/Mamiya"},
8013       {44, "Hasselblad V"},
8014       {45, "Hasselblad H"},
8015       {46, "Contax 645"},
8016       {47, "PhaseOne/Mamiya"},
8017       {48, "Hasselblad V"},
8018       {49, "Hasselblad H"},
8019       {50, "Contax 645"},
8020       {51, "PhaseOne/Mamiya"},
8021       {52, "Hasselblad V"},
8022       {53, "Hasselblad H"},
8023       {54, "Contax 645"},
8024       {55, "PhaseOne/Mamiya"},
8025       {67, "Hasselblad V"},
8026       {68, "Hasselblad H"},
8027       {69, "Contax 645"},
8028       {70, "PhaseOne/Mamiya"},
8029       {71, "Hasselblad V"},
8030       {72, "Hasselblad H"},
8031       {73, "Contax 645"},
8032       {74, "PhaseOne/Mamiya"},
8033       {76, "Hasselblad V"},
8034       {77, "Hasselblad H"},
8035       {78, "Contax 645"},
8036       {79, "PhaseOne/Mamiya"},
8037       {80, "Hasselblad V"},
8038       {81, "Hasselblad H"},
8039       {82, "Contax 645"},
8040       {83, "PhaseOne/Mamiya"},
8041       {84, "Hasselblad V"},
8042       {85, "Hasselblad H"},
8043       {86, "Contax 645"},
8044       {87, "PhaseOne/Mamiya"},
8045       {99, "Hasselblad V"},
8046       {100, "Hasselblad H"},
8047       {101, "Contax 645"},
8048       {102, "PhaseOne/Mamiya"},
8049       {103, "Hasselblad V"},
8050       {104, "Hasselblad H"},
8051       {105, "PhaseOne/Mamiya"},
8052       {106, "Contax 645"},
8053       {112, "Hasselblad V"},
8054       {113, "Hasselblad H"},
8055       {114, "Contax 645"},
8056       {115, "PhaseOne/Mamiya"},
8057       {131, "Hasselblad V"},
8058       {132, "Hasselblad H"},
8059       {133, "Contax 645"},
8060       {134, "PhaseOne/Mamiya"},
8061       {135, "Hasselblad V"},
8062       {136, "Hasselblad H"},
8063       {137, "Contax 645"},
8064       {138, "PhaseOne/Mamiya"},
8065       {140, "Hasselblad V"},
8066       {141, "Hasselblad H"},
8067       {142, "Contax 645"},
8068       {143, "PhaseOne/Mamiya"},
8069       {148, "Hasselblad V"},
8070       {149, "Hasselblad H"},
8071       {150, "Contax 645"},
8072       {151, "PhaseOne/Mamiya"},
8073       {160, "A-250"},
8074       {161, "A-260"},
8075       {162, "A-280"},
8076       {167, "Hasselblad V"},
8077       {168, "Hasselblad H"},
8078       {169, "Contax 645"},
8079       {170, "PhaseOne/Mamiya"},
8080       {172, "Hasselblad V"},
8081       {173, "Hasselblad H"},
8082       {174, "Contax 645"},
8083       {175, "PhaseOne/Mamiya"},
8084       {176, "Hasselblad V"},
8085       {177, "Hasselblad H"},
8086       {178, "Contax 645"},
8087       {179, "PhaseOne/Mamiya"},
8088       {180, "Hasselblad V"},
8089       {181, "Hasselblad H"},
8090       {182, "Contax 645"},
8091       {183, "PhaseOne/Mamiya"},
8092       {208, "Hasselblad V"},
8093       {211, "PhaseOne/Mamiya"},
8094       {448, "Phase One 645AF"},
8095       {457, "Phase One 645DF"},
8096       {471, "Phase One 645DF+"},
8097       {704, "Phase One iXA"},
8098       {705, "Phase One iXA - R"},
8099       {706, "Phase One iXU 150"},
8100       {707, "Phase One iXU 150 - NIR"},
8101       {708, "Phase One iXU 180"},
8102       {721, "Phase One iXR"},
8103       // Leaf section:
8104       {333, "Mamiya"},
8105       {329, "Universal"},
8106       {330, "Hasselblad H1/H2"},
8107       {332, "Contax"},
8108       {336, "AFi"},
8109       {327, "Mamiya"},
8110       {324, "Universal"},
8111       {325, "Hasselblad H1/H2"},
8112       {326, "Contax"},
8113       {335, "AFi"},
8114       {340, "Mamiya"},
8115       {337, "Universal"},
8116       {338, "Hasselblad H1/H2"},
8117       {339, "Contax"},
8118       {323, "Mamiya"},
8119       {320, "Universal"},
8120       {322, "Hasselblad H1/H2"},
8121       {321, "Contax"},
8122       {334, "AFi"},
8123       {369, "Universal"},
8124       {370, "Mamiya"},
8125       {371, "Hasselblad H1/H2"},
8126       {372, "Contax"},
8127       {373, "Afi"},
8128   };
8129   imgdata.lens.makernotes.CamID = id;
8130   if (id && !imgdata.lens.makernotes.body[0])
8131   {
8132     for (i = 0; i < sizeof p1_unique / sizeof *p1_unique; i++)
8133       if (id == p1_unique[i].id)
8134       {
8135         strcpy(imgdata.lens.makernotes.body, p1_unique[i].t_model);
8136       }
8137   }
8138   return;
8139 }
8140 
parseFujiMakernotes(unsigned tag,unsigned type)8141 void CLASS parseFujiMakernotes(unsigned tag, unsigned type)
8142 {
8143   switch (tag)
8144   {
8145   case 0x1002:
8146     imgdata.makernotes.fuji.WB_Preset = get2();
8147     break;
8148   case 0x1011:
8149     imgdata.other.FlashEC = getreal(type);
8150     break;
8151   case 0x1020:
8152     imgdata.makernotes.fuji.Macro = get2();
8153     break;
8154   case 0x1021:
8155     imgdata.makernotes.fuji.FocusMode = get2();
8156     break;
8157   case 0x1022:
8158     imgdata.makernotes.fuji.AFMode = get2();
8159     break;
8160   case 0x1023:
8161     imgdata.makernotes.fuji.FocusPixel[0] = get2();
8162     imgdata.makernotes.fuji.FocusPixel[1] = get2();
8163     break;
8164   case 0x1034:
8165     imgdata.makernotes.fuji.ExrMode = get2();
8166     break;
8167   case 0x1050:
8168     imgdata.makernotes.fuji.ShutterType = get2();
8169     break;
8170   case 0x1400:
8171     imgdata.makernotes.fuji.FujiDynamicRange = get2();
8172     break;
8173   case 0x1401:
8174     imgdata.makernotes.fuji.FujiFilmMode = get2();
8175     break;
8176   case 0x1402:
8177     imgdata.makernotes.fuji.FujiDynamicRangeSetting = get2();
8178     break;
8179   case 0x1403:
8180     imgdata.makernotes.fuji.FujiDevelopmentDynamicRange = get2();
8181     break;
8182   case 0x140b:
8183     imgdata.makernotes.fuji.FujiAutoDynamicRange = get2();
8184     break;
8185   case 0x1404:
8186     imgdata.lens.makernotes.MinFocal = getreal(type);
8187     break;
8188   case 0x1405:
8189     imgdata.lens.makernotes.MaxFocal = getreal(type);
8190     break;
8191   case 0x1406:
8192     imgdata.lens.makernotes.MaxAp4MinFocal = getreal(type);
8193     break;
8194   case 0x1407:
8195     imgdata.lens.makernotes.MaxAp4MaxFocal = getreal(type);
8196     break;
8197   case 0x1422:
8198     imgdata.makernotes.fuji.ImageStabilization[0] = get2();
8199     imgdata.makernotes.fuji.ImageStabilization[1] = get2();
8200     imgdata.makernotes.fuji.ImageStabilization[2] = get2();
8201     imgdata.shootinginfo.ImageStabilization =
8202         (imgdata.makernotes.fuji.ImageStabilization[0] << 9) + imgdata.makernotes.fuji.ImageStabilization[1];
8203     break;
8204   case 0x1431:
8205     imgdata.makernotes.fuji.Rating = get4();
8206     break;
8207   case 0x3820:
8208     imgdata.makernotes.fuji.FrameRate = get2();
8209     break;
8210   case 0x3821:
8211     imgdata.makernotes.fuji.FrameWidth = get2();
8212     break;
8213   case 0x3822:
8214     imgdata.makernotes.fuji.FrameHeight = get2();
8215     break;
8216   }
8217   return;
8218 }
8219 
setSonyBodyFeatures(unsigned id)8220 void CLASS setSonyBodyFeatures(unsigned id)
8221 {
8222   ushort idx;
8223   static const struct
8224   {
8225     ushort scf[8];
8226     /*
8227     scf[0] camera id
8228     scf[1] camera format
8229     scf[2] camera mount: Minolta A, Sony E, fixed,
8230     scf[3] camera type: DSLR, NEX, SLT, ILCE, ILCA, DSC
8231     scf[4] lens mount
8232     scf[5] tag 0x2010 group (0 if not used)
8233     scf[6] offset of Sony ISO in 0x2010 table, 0xffff if not valid
8234     scf[7] offset of ImageCount3 in 0x9050 table, 0xffff if not valid
8235     */
8236   } SonyCamFeatures[] = {
8237       {256, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8238       {257, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8239       {258, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8240       {259, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8241       {260, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8242       {261, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8243       {262, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8244       {263, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8245       {264, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8246       {265, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8247       {266, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8248       {267, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8249       {268, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8250       {269, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8251       {270, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8252       {271, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8253       {272, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8254       {273, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8255       {274, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8256       {275, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8257       {276, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8258       {277, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8259       {278, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 0, 0xffff, 0xffff},
8260       {279, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 0, 0xffff, 0xffff},
8261       {280, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 0, 0xffff, 0xffff},
8262       {281, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 0, 0xffff, 0xffff},
8263       {282, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8264       {283, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_DSLR, 0, 0, 0xffff, 0xffff},
8265       {284, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 0, 0xffff, 0xffff},
8266       {285, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 0, 0xffff, 0xffff},
8267       {286, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 2, 0x1218, 0x01bd},
8268       {287, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 2, 0x1218, 0x01bd},
8269       {288, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 1, 0x113e, 0x01bd},
8270       {289, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 2, 0x1218, 0x01bd},
8271       {290, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 2, 0x1218, 0x01bd},
8272       {291, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 3, 0x11f4, 0x01bd},
8273       {292, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 3, 0x11f4, 0x01bd},
8274       {293, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 3, 0x11f4, 0x01bd},
8275       {294, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 5, 0x1254, 0x01aa},
8276       {295, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 5, 0x1254, 0x01aa},
8277       {296, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 5, 0x1254, 0x01aa},
8278       {297, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 5, 0x1254, 0xffff},
8279       {298, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 5, 0x1258, 0xffff},
8280       {299, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 5, 0x1254, 0x01aa},
8281       {300, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 5, 0x1254, 0x01aa},
8282       {301, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8283       {302, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 5, 0x1280, 0x01aa},
8284       {303, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_SLT, 0, 5, 0x1280, 0x01aa},
8285       {304, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8286       {305, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 5, 0x1280, 0x01aa},
8287       {306, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0xffff},
8288       {307, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_NEX, 0, 5, 0x1254, 0x01aa},
8289       {308, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 6, 0x113c, 0xffff},
8290       {309, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 7, 0x0344, 0xffff},
8291       {310, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 5, 0x1258, 0xffff},
8292       {311, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0xffff},
8293       {312, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0xffff},
8294       {313, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0x01aa},
8295       {314, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8296       {315, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8297       {316, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8298       {317, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 7, 0x0344, 0xffff},
8299       {318, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0xffff},
8300       {319, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_ILCA, 0, 7, 0x0344, 0x01a0},
8301       {320, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8302       {321, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8303       {322, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8304       {323, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8305       {324, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8306       {325, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8307       {326, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8308       {327, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8309       {328, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8310       {329, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8311       {330, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8312       {331, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8313       {332, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8314       {333, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8315       {334, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8316       {335, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8317       {336, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8318       {337, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8319       {338, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8320       {339, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0x01a0},
8321       {340, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0xffff},
8322       {341, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 8, 0x0346, 0xffff},
8323       {342, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 8, 0x0346, 0xffff},
8324       {343, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8325       {344, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 8, 0x0346, 0xffff},
8326       {345, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8327       {346, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 7, 0x0344, 0x01a0},
8328       {347, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 8, 0x0346, 0x01cb},
8329       {348, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8330       {349, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8331       {350, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 8, 0x0346, 0x01cb},
8332       {351, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8333       {352, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8334       {353, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_ILCA, 0, 7, 0x0344, 0x01a0},
8335       {354, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Minolta_A, LIBRAW_SONY_ILCA, 0, 8, 0x0346, 0x01cd},
8336       {355, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 8, 0x0346, 0xffff},
8337       {356, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 8, 0x0346, 0xffff},
8338       {357, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 8, 0x0346, 0x01cd},
8339       {358, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 9, 0x0320, 0x019f},
8340       {359, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8341       {360, LIBRAW_FORMAT_APSC, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 8, 0x0346, 0x01cd},
8342       {361, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8343       {362, LIBRAW_FORMAT_FF, LIBRAW_MOUNT_Sony_E, LIBRAW_SONY_ILCE, 0, 9, 0x0320, 0x019f},
8344       {363, 0, 0, 0, 0, 0, 0xffff, 0xffff},
8345       {364, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 8, 0x0346, 0xffff},
8346       {365, LIBRAW_FORMAT_1INCH, LIBRAW_MOUNT_FixedLens, LIBRAW_SONY_DSC, LIBRAW_MOUNT_FixedLens, 9, 0x0320, 0xffff},
8347   };
8348   imgdata.lens.makernotes.CamID = id;
8349 
8350   if (id == 2)
8351   {
8352     imgdata.lens.makernotes.CameraMount = imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
8353     imgdata.makernotes.sony.SonyCameraType = LIBRAW_SONY_DSC;
8354     imgdata.makernotes.sony.group2010 = 0;
8355     imgdata.makernotes.sony.real_iso_offset = 0xffff;
8356     imgdata.makernotes.sony.ImageCount3_offset = 0xffff;
8357     return;
8358   }
8359   else
8360     idx = id - 256;
8361 
8362   if ((idx >= 0) && (idx < sizeof SonyCamFeatures / sizeof *SonyCamFeatures))
8363   {
8364     if (!SonyCamFeatures[idx].scf[2])
8365       return;
8366     imgdata.lens.makernotes.CameraFormat = SonyCamFeatures[idx].scf[1];
8367     imgdata.lens.makernotes.CameraMount = SonyCamFeatures[idx].scf[2];
8368     imgdata.makernotes.sony.SonyCameraType = SonyCamFeatures[idx].scf[3];
8369     if (SonyCamFeatures[idx].scf[4])
8370       imgdata.lens.makernotes.LensMount = SonyCamFeatures[idx].scf[4];
8371     imgdata.makernotes.sony.group2010 = SonyCamFeatures[idx].scf[5];
8372     imgdata.makernotes.sony.real_iso_offset = SonyCamFeatures[idx].scf[6];
8373     imgdata.makernotes.sony.ImageCount3_offset = SonyCamFeatures[idx].scf[7];
8374   }
8375 
8376   char *sbstr = strstr(software, " v");
8377   if (sbstr != NULL)
8378   {
8379     sbstr += 2;
8380     imgdata.makernotes.sony.firmware = atof(sbstr);
8381 
8382     if ((id == 306) || (id == 311))
8383     {
8384       if (imgdata.makernotes.sony.firmware < 1.2f)
8385         imgdata.makernotes.sony.ImageCount3_offset = 0x01aa;
8386       else
8387         imgdata.makernotes.sony.ImageCount3_offset = 0x01c0;
8388     }
8389     else if (id == 312)
8390     {
8391       if (imgdata.makernotes.sony.firmware < 2.0f)
8392         imgdata.makernotes.sony.ImageCount3_offset = 0x01aa;
8393       else
8394         imgdata.makernotes.sony.ImageCount3_offset = 0x01c0;
8395     }
8396     else if ((id == 318) || (id == 340))
8397     {
8398       if (imgdata.makernotes.sony.firmware < 1.2f)
8399         imgdata.makernotes.sony.ImageCount3_offset = 0x01a0;
8400       else
8401         imgdata.makernotes.sony.ImageCount3_offset = 0x01b6;
8402     }
8403   }
8404 }
8405 
parseSonyLensType2(uchar a,uchar b)8406 void CLASS parseSonyLensType2(uchar a, uchar b)
8407 {
8408   ushort lid2;
8409   lid2 = (((ushort)a) << 8) | ((ushort)b);
8410   if (!lid2)
8411     return;
8412   if (lid2 < 0x100)
8413   {
8414     if ((imgdata.lens.makernotes.AdapterID != 0x4900) && (imgdata.lens.makernotes.AdapterID != 0xEF00))
8415     {
8416       imgdata.lens.makernotes.AdapterID = lid2;
8417       switch (lid2)
8418       {
8419       case 1:
8420       case 2:
8421       case 3:
8422       case 6:
8423         imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Minolta_A;
8424         break;
8425       case 44:
8426       case 78:
8427       case 239:
8428         imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF;
8429         break;
8430       }
8431     }
8432   }
8433   else
8434     imgdata.lens.makernotes.LensID = lid2;
8435   if ((lid2 >= 50481) && (lid2 < 50500))
8436   {
8437     strcpy(imgdata.lens.makernotes.Adapter, "MC-11");
8438     imgdata.lens.makernotes.AdapterID = 0x4900;
8439   }
8440   return;
8441 }
8442 
8443 #define strnXcat(buf, string) strncat(buf, string, LIM(sizeof(buf) - strbuflen(buf) - 1, 0, sizeof(buf)))
8444 
parseSonyLensFeatures(uchar a,uchar b)8445 void CLASS parseSonyLensFeatures(uchar a, uchar b)
8446 {
8447 
8448   ushort features;
8449   features = (((ushort)a) << 8) | ((ushort)b);
8450 
8451   if ((imgdata.lens.makernotes.LensMount == LIBRAW_MOUNT_Canon_EF) ||
8452       (imgdata.lens.makernotes.LensMount != LIBRAW_MOUNT_Sigma_X3F) || !features)
8453     return;
8454 
8455   imgdata.lens.makernotes.LensFeatures_pre[0] = 0;
8456   imgdata.lens.makernotes.LensFeatures_suf[0] = 0;
8457   if ((features & 0x0200) && (features & 0x0100))
8458     strcpy(imgdata.lens.makernotes.LensFeatures_pre, "E");
8459   else if (features & 0x0200)
8460     strcpy(imgdata.lens.makernotes.LensFeatures_pre, "FE");
8461   else if (features & 0x0100)
8462     strcpy(imgdata.lens.makernotes.LensFeatures_pre, "DT");
8463 
8464   if (!imgdata.lens.makernotes.LensFormat && !imgdata.lens.makernotes.LensMount)
8465   {
8466     imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_FF;
8467     imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Minolta_A;
8468 
8469     if ((features & 0x0200) && (features & 0x0100))
8470     {
8471       imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_APSC;
8472       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Sony_E;
8473     }
8474     else if (features & 0x0200)
8475     {
8476       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Sony_E;
8477     }
8478     else if (features & 0x0100)
8479     {
8480       imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_APSC;
8481     }
8482   }
8483 
8484   if (features & 0x4000)
8485     strnXcat(imgdata.lens.makernotes.LensFeatures_pre, " PZ");
8486 
8487   if (features & 0x0008)
8488     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " G");
8489   else if (features & 0x0004)
8490     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " ZA");
8491 
8492   if ((features & 0x0020) && (features & 0x0040))
8493     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " Macro");
8494   else if (features & 0x0020)
8495     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " STF");
8496   else if (features & 0x0040)
8497     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " Reflex");
8498   else if (features & 0x0080)
8499     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " Fisheye");
8500 
8501   if (features & 0x0001)
8502     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " SSM");
8503   else if (features & 0x0002)
8504     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " SAM");
8505 
8506   if (features & 0x8000)
8507     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " OSS");
8508 
8509   if (features & 0x2000)
8510     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " LE");
8511 
8512   if (features & 0x0800)
8513     strnXcat(imgdata.lens.makernotes.LensFeatures_suf, " II");
8514 
8515   if (imgdata.lens.makernotes.LensFeatures_suf[0] == ' ')
8516     memmove(imgdata.lens.makernotes.LensFeatures_suf, imgdata.lens.makernotes.LensFeatures_suf + 1,
8517             strbuflen(imgdata.lens.makernotes.LensFeatures_suf) - 1);
8518 
8519   return;
8520 }
8521 #undef strnXcat
8522 
process_Sony_0x0116(uchar * buf,ushort len,unsigned id)8523 void CLASS process_Sony_0x0116(uchar *buf, ushort len, unsigned id)
8524 {
8525   short bufx;
8526 
8527   if (((id == 257) || (id == 262) || (id == 269) || (id == 270)) && (len >= 2))
8528     bufx = buf[1];
8529   else if ((id >= 273) && (len >= 3))
8530     bufx = buf[2];
8531   else
8532     return;
8533 
8534   imgdata.other.BatteryTemperature = (float)(bufx - 32) / 1.8f;
8535 }
8536 
process_Sony_0x2010(uchar * buf,ushort len)8537 void CLASS process_Sony_0x2010(uchar *buf, ushort len)
8538 {
8539   if ((!imgdata.makernotes.sony.group2010) || (imgdata.makernotes.sony.real_iso_offset == 0xffff) ||
8540       (len < (imgdata.makernotes.sony.real_iso_offset + 2)))
8541     return;
8542 
8543   if (imgdata.other.real_ISO < 0.1f)
8544   {
8545     uchar s[2];
8546     s[0] = SonySubstitution[buf[imgdata.makernotes.sony.real_iso_offset]];
8547     s[1] = SonySubstitution[buf[imgdata.makernotes.sony.real_iso_offset + 1]];
8548     imgdata.other.real_ISO = 100.0f * libraw_powf64l(2.0f, (16 - ((float)sget2(s)) / 256.0f));
8549   }
8550 }
8551 
process_Sony_0x9050(uchar * buf,ushort len,unsigned id)8552 void CLASS process_Sony_0x9050(uchar *buf, ushort len, unsigned id)
8553 {
8554   ushort lid;
8555   uchar s[4];
8556   int c;
8557 
8558   if ((imgdata.lens.makernotes.CameraMount != LIBRAW_MOUNT_Sony_E) &&
8559       (imgdata.lens.makernotes.CameraMount != LIBRAW_MOUNT_FixedLens))
8560   {
8561     if (len < 2)
8562       return;
8563     if (buf[0])
8564       imgdata.lens.makernotes.MaxAp4CurFocal =
8565           my_roundf(libraw_powf64l(2.0f, ((float)SonySubstitution[buf[0]] / 8.0 - 1.06f) / 2.0f) * 10.0f) / 10.0f;
8566 
8567     if (buf[1])
8568       imgdata.lens.makernotes.MinAp4CurFocal =
8569           my_roundf(libraw_powf64l(2.0f, ((float)SonySubstitution[buf[1]] / 8.0 - 1.06f) / 2.0f) * 10.0f) / 10.0f;
8570   }
8571 
8572   if (imgdata.lens.makernotes.CameraMount != LIBRAW_MOUNT_FixedLens)
8573   {
8574     if (len <= 0x106)
8575       return;
8576     if (buf[0x3d] | buf[0x3c])
8577     {
8578       lid = SonySubstitution[buf[0x3d]] << 8 | SonySubstitution[buf[0x3c]];
8579       imgdata.lens.makernotes.CurAp = libraw_powf64l(2.0f, ((float)lid / 256.0f - 16.0f) / 2.0f);
8580     }
8581     if (buf[0x105] && (imgdata.lens.makernotes.LensMount != LIBRAW_MOUNT_Canon_EF) &&
8582         (imgdata.lens.makernotes.LensMount != LIBRAW_MOUNT_Sigma_X3F))
8583       imgdata.lens.makernotes.LensMount = SonySubstitution[buf[0x105]];
8584     if (buf[0x106])
8585       imgdata.lens.makernotes.LensFormat = SonySubstitution[buf[0x106]];
8586   }
8587 
8588   if (imgdata.lens.makernotes.CameraMount == LIBRAW_MOUNT_Sony_E)
8589   {
8590     if (len <= 0x108)
8591       return;
8592     parseSonyLensType2(SonySubstitution[buf[0x0108]], // LensType2 - Sony lens ids
8593                        SonySubstitution[buf[0x0107]]);
8594   }
8595 
8596   if (len <= 0x10a)
8597     return;
8598   if ((imgdata.lens.makernotes.LensID == -1) && (imgdata.lens.makernotes.CameraMount == LIBRAW_MOUNT_Minolta_A) &&
8599       (buf[0x010a] | buf[0x0109]))
8600   {
8601     imgdata.lens.makernotes.LensID = // LensType - Minolta/Sony lens ids
8602         SonySubstitution[buf[0x010a]] << 8 | SonySubstitution[buf[0x0109]];
8603 
8604     if ((imgdata.lens.makernotes.LensID > 0x4900) && (imgdata.lens.makernotes.LensID <= 0x5900))
8605     {
8606       imgdata.lens.makernotes.AdapterID = 0x4900;
8607       imgdata.lens.makernotes.LensID -= imgdata.lens.makernotes.AdapterID;
8608       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Sigma_X3F;
8609       strcpy(imgdata.lens.makernotes.Adapter, "MC-11");
8610     }
8611 
8612     else if ((imgdata.lens.makernotes.LensID > 0xEF00) && (imgdata.lens.makernotes.LensID < 0xFFFF) &&
8613              (imgdata.lens.makernotes.LensID != 0xFF00))
8614     {
8615       imgdata.lens.makernotes.AdapterID = 0xEF00;
8616       imgdata.lens.makernotes.LensID -= imgdata.lens.makernotes.AdapterID;
8617       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF;
8618     }
8619   }
8620 
8621   if ((id >= 286) && (id <= 293))
8622   {
8623     if (len <= 0x116)
8624       return;
8625     // "SLT-A65", "SLT-A77", "NEX-7", "NEX-VG20E",
8626     // "SLT-A37", "SLT-A57", "NEX-F3", "Lunar"
8627     parseSonyLensFeatures(SonySubstitution[buf[0x115]], SonySubstitution[buf[0x116]]);
8628   }
8629   else if (imgdata.lens.makernotes.CameraMount != LIBRAW_MOUNT_FixedLens)
8630   {
8631     if (len <= 0x117)
8632       return;
8633     parseSonyLensFeatures(SonySubstitution[buf[0x116]], SonySubstitution[buf[0x117]]);
8634   }
8635 
8636   if ((id == 347) || (id == 350) || (id == 354) || (id == 357) || (id == 358) || (id == 360) || (id == 362))
8637   {
8638     if (len <= 0x8d)
8639       return;
8640     unsigned long long b88 = SonySubstitution[buf[0x88]];
8641     unsigned long long b89 = SonySubstitution[buf[0x89]];
8642     unsigned long long b8a = SonySubstitution[buf[0x8a]];
8643     unsigned long long b8b = SonySubstitution[buf[0x8b]];
8644     unsigned long long b8c = SonySubstitution[buf[0x8c]];
8645     unsigned long long b8d = SonySubstitution[buf[0x8d]];
8646     sprintf(imgdata.shootinginfo.InternalBodySerial, "%06llx",
8647             (b88 << 40) + (b89 << 32) + (b8a << 24) + (b8b << 16) + (b8c << 8) + b8d);
8648   }
8649   else if (imgdata.lens.makernotes.CameraMount == LIBRAW_MOUNT_Minolta_A)
8650   {
8651     if (len <= 0xf4)
8652       return;
8653     unsigned long long bf0 = SonySubstitution[buf[0xf0]];
8654     unsigned long long bf1 = SonySubstitution[buf[0xf1]];
8655     unsigned long long bf2 = SonySubstitution[buf[0xf2]];
8656     unsigned long long bf3 = SonySubstitution[buf[0xf3]];
8657     unsigned long long bf4 = SonySubstitution[buf[0xf4]];
8658     sprintf(imgdata.shootinginfo.InternalBodySerial, "%05llx",
8659             (bf0 << 32) + (bf1 << 24) + (bf2 << 16) + (bf3 << 8) + bf4);
8660   }
8661   else if ((imgdata.lens.makernotes.CameraMount == LIBRAW_MOUNT_Sony_E) && (id != 288) && (id != 289) && (id != 290))
8662   {
8663     if (len <= 0x7f)
8664       return;
8665     unsigned b7c = SonySubstitution[buf[0x7c]];
8666     unsigned b7d = SonySubstitution[buf[0x7d]];
8667     unsigned b7e = SonySubstitution[buf[0x7e]];
8668     unsigned b7f = SonySubstitution[buf[0x7f]];
8669     sprintf(imgdata.shootinginfo.InternalBodySerial, "%04x", (b7c << 24) + (b7d << 16) + (b7e << 8) + b7f);
8670   }
8671 
8672   if ((imgdata.makernotes.sony.ImageCount3_offset != 0xffff) &&
8673       (len >= (imgdata.makernotes.sony.ImageCount3_offset + 4)))
8674   {
8675     FORC4 s[c] = SonySubstitution[buf[imgdata.makernotes.sony.ImageCount3_offset + c]];
8676     imgdata.makernotes.sony.ImageCount3 = sget4(s);
8677   }
8678 
8679   if (id == 362)
8680   {
8681     for (c = 0; c < 6; c++)
8682     {
8683       imgdata.makernotes.sony.TimeStamp[c] = SonySubstitution[buf[0x0066 + c]];
8684     }
8685   }
8686 
8687   return;
8688 }
8689 
process_Sony_0x9400(uchar * buf,ushort len,unsigned id)8690 void CLASS process_Sony_0x9400(uchar *buf, ushort len, unsigned id)
8691 {
8692 
8693   uchar s[4];
8694   int c;
8695   short bufx = buf[0];
8696 
8697   if (((bufx == 0x23) || (bufx == 0x24) || (bufx == 0x26)) && (len >= 0x1f))
8698   { // 0x9400 'c' version
8699 
8700     if ((id == 358) || (id == 362) || (id == 365))
8701     {
8702       imgdata.makernotes.sony.ShotNumberSincePowerUp = SonySubstitution[buf[0x0a]];
8703     }
8704     else
8705     {
8706       FORC4 s[c] = SonySubstitution[buf[0x0a + c]];
8707       imgdata.makernotes.sony.ShotNumberSincePowerUp = sget4(s);
8708     }
8709 
8710     imgdata.makernotes.sony.Sony0x9400_version = 0xc;
8711 
8712     imgdata.makernotes.sony.Sony0x9400_ReleaseMode2 = SonySubstitution[buf[0x09]];
8713 
8714     FORC4 s[c] = SonySubstitution[buf[0x12 + c]];
8715     imgdata.makernotes.sony.Sony0x9400_SequenceImageNumber = sget4(s);
8716 
8717     imgdata.makernotes.sony.Sony0x9400_SequenceLength1 = SonySubstitution[buf[0x16]]; // shots
8718 
8719     FORC4 s[c] = SonySubstitution[buf[0x1a + c]];
8720     imgdata.makernotes.sony.Sony0x9400_SequenceFileNumber = sget4(s);
8721 
8722     imgdata.makernotes.sony.Sony0x9400_SequenceLength2 = SonySubstitution[buf[0x1e]]; // files
8723   }
8724 
8725   else if ((bufx == 0x0c) && (len >= 0x1f))
8726   { // 0x9400 'b' version
8727     imgdata.makernotes.sony.Sony0x9400_version = 0xb;
8728 
8729     FORC4 s[c] = SonySubstitution[buf[0x08 + c]];
8730     imgdata.makernotes.sony.Sony0x9400_SequenceImageNumber = sget4(s);
8731 
8732     FORC4 s[c] = SonySubstitution[buf[0x0c + c]];
8733     imgdata.makernotes.sony.Sony0x9400_SequenceFileNumber = sget4(s);
8734 
8735     imgdata.makernotes.sony.Sony0x9400_ReleaseMode2 = SonySubstitution[buf[0x10]];
8736 
8737     imgdata.makernotes.sony.Sony0x9400_SequenceLength1 = SonySubstitution[buf[0x1e]];
8738   }
8739 
8740   else if ((bufx == 0x0a) && (len >= 0x23))
8741   { // 0x9400 'a' version
8742     imgdata.makernotes.sony.Sony0x9400_version = 0xa;
8743 
8744     FORC4 s[c] = SonySubstitution[buf[0x08 + c]];
8745     imgdata.makernotes.sony.Sony0x9400_SequenceImageNumber = sget4(s);
8746 
8747     FORC4 s[c] = SonySubstitution[buf[0x0c + c]];
8748     imgdata.makernotes.sony.Sony0x9400_SequenceFileNumber = sget4(s);
8749 
8750     imgdata.makernotes.sony.Sony0x9400_ReleaseMode2 = SonySubstitution[buf[0x10]];
8751 
8752     imgdata.makernotes.sony.Sony0x9400_SequenceLength1 = SonySubstitution[buf[0x22]];
8753   }
8754 
8755   else
8756     return;
8757 }
8758 
process_Sony_0x9402(uchar * buf,ushort len)8759 void CLASS process_Sony_0x9402(uchar *buf, ushort len)
8760 {
8761 
8762   if ((imgdata.makernotes.sony.SonyCameraType == LIBRAW_SONY_SLT) ||
8763       (imgdata.makernotes.sony.SonyCameraType == LIBRAW_SONY_ILCA))
8764     return;
8765 
8766   if (len < 5)
8767     return;
8768 
8769   short bufx = buf[0x00];
8770   if ((bufx == 0x05) || (bufx == 0xff) || (buf[0x02] != 0xff))
8771     return;
8772 
8773   imgdata.other.AmbientTemperature = (float)((short)SonySubstitution[buf[0x04]]);
8774 
8775   return;
8776 }
8777 
process_Sony_0x9403(uchar * buf,ushort len)8778 void CLASS process_Sony_0x9403(uchar *buf, ushort len)
8779 {
8780   if (len < 6)
8781     return;
8782   short bufx = SonySubstitution[buf[4]];
8783   if ((bufx == 0x00) || (bufx == 0x94))
8784     return;
8785 
8786   imgdata.other.SensorTemperature = (float)((short)SonySubstitution[buf[5]]);
8787 
8788   return;
8789 }
8790 
process_Sony_0x9406(uchar * buf,ushort len)8791 void CLASS process_Sony_0x9406(uchar *buf, ushort len)
8792 {
8793   if (len < 6)
8794     return;
8795   short bufx = buf[0];
8796   if ((bufx != 0x01) && (bufx != 0x08) && (bufx != 0x1b))
8797     return;
8798   bufx = buf[2];
8799   if ((bufx != 0x08) && (bufx != 0x1b))
8800     return;
8801 
8802   imgdata.other.BatteryTemperature = (float)(SonySubstitution[buf[5]] - 32) / 1.8f;
8803 
8804   return;
8805 }
8806 
process_Sony_0x940c(uchar * buf,ushort len)8807 void CLASS process_Sony_0x940c(uchar *buf, ushort len)
8808 {
8809   if ((imgdata.makernotes.sony.SonyCameraType != LIBRAW_SONY_ILCE) &&
8810       (imgdata.makernotes.sony.SonyCameraType != LIBRAW_SONY_NEX))
8811     return;
8812   if (len <= 0x000a)
8813     return;
8814 
8815   ushort lid2;
8816   if ((imgdata.lens.makernotes.LensMount != LIBRAW_MOUNT_Canon_EF) &&
8817       (imgdata.lens.makernotes.LensMount != LIBRAW_MOUNT_Sigma_X3F))
8818   {
8819     switch (SonySubstitution[buf[0x0008]])
8820     {
8821     case 1:
8822     case 5:
8823       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Minolta_A;
8824       break;
8825     case 4:
8826       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Sony_E;
8827       break;
8828     }
8829   }
8830   lid2 = (((ushort)SonySubstitution[buf[0x000a]]) << 8) | ((ushort)SonySubstitution[buf[0x0009]]);
8831   if ((lid2 > 0) && (lid2 < 32784))
8832     parseSonyLensType2(SonySubstitution[buf[0x000a]], // LensType2 - Sony lens ids
8833                        SonySubstitution[buf[0x0009]]);
8834   return;
8835 }
8836 
process_Sony_0x940e(uchar * buf,ushort len,unsigned id)8837 void CLASS process_Sony_0x940e(uchar *buf, ushort len, unsigned id)
8838 {
8839   if (((id == 286) || (id == 287) || (id == 294)) && (len >= 0x017e))
8840   {
8841     imgdata.makernotes.sony.AFMicroAdjValue = SonySubstitution[buf[0x017d]];
8842   }
8843   else if ((imgdata.makernotes.sony.SonyCameraType == LIBRAW_SONY_ILCA) && (len >= 0x0051))
8844   {
8845     imgdata.makernotes.sony.AFMicroAdjValue = SonySubstitution[buf[0x0050]];
8846   }
8847   else
8848     return;
8849 
8850   if (imgdata.makernotes.sony.AFMicroAdjValue != 0)
8851     imgdata.makernotes.sony.AFMicroAdjOn = 1;
8852 }
8853 
parseSonyMakernotes(unsigned tag,unsigned type,unsigned len,unsigned dng_writer,uchar * & table_buf_0x0116,ushort & table_buf_0x0116_len,uchar * & table_buf_0x2010,ushort & table_buf_0x2010_len,uchar * & table_buf_0x9050,ushort & table_buf_0x9050_len,uchar * & table_buf_0x9400,ushort & table_buf_0x9400_len,uchar * & table_buf_0x9402,ushort & table_buf_0x9402_len,uchar * & table_buf_0x9403,ushort & table_buf_0x9403_len,uchar * & table_buf_0x9406,ushort & table_buf_0x9406_len,uchar * & table_buf_0x940c,ushort & table_buf_0x940c_len,uchar * & table_buf_0x940e,ushort & table_buf_0x940e_len)8854 void CLASS parseSonyMakernotes(unsigned tag, unsigned type, unsigned len, unsigned dng_writer, uchar *&table_buf_0x0116,
8855                                ushort &table_buf_0x0116_len, uchar *&table_buf_0x2010, ushort &table_buf_0x2010_len,
8856                                uchar *&table_buf_0x9050, ushort &table_buf_0x9050_len, uchar *&table_buf_0x9400,
8857                                ushort &table_buf_0x9400_len, uchar *&table_buf_0x9402, ushort &table_buf_0x9402_len,
8858                                uchar *&table_buf_0x9403, ushort &table_buf_0x9403_len, uchar *&table_buf_0x9406,
8859                                ushort &table_buf_0x9406_len, uchar *&table_buf_0x940c, ushort &table_buf_0x940c_len,
8860                                uchar *&table_buf_0x940e, ushort &table_buf_0x940e_len)
8861 {
8862 
8863   ushort lid;
8864   uchar *table_buf;
8865 
8866   if (tag == 0xb001) // Sony ModelID
8867   {
8868     unique_id = get2();
8869     setSonyBodyFeatures(unique_id);
8870 
8871     if (table_buf_0x0116_len)
8872     {
8873       process_Sony_0x0116(table_buf_0x0116, table_buf_0x0116_len, unique_id);
8874       free(table_buf_0x0116);
8875       table_buf_0x0116_len = 0;
8876     }
8877 
8878     if (table_buf_0x2010_len)
8879     {
8880       process_Sony_0x2010(table_buf_0x2010, table_buf_0x2010_len);
8881       free(table_buf_0x2010);
8882       table_buf_0x2010_len = 0;
8883     }
8884 
8885     if (table_buf_0x9050_len)
8886     {
8887       process_Sony_0x9050(table_buf_0x9050, table_buf_0x9050_len, unique_id);
8888       free(table_buf_0x9050);
8889       table_buf_0x9050_len = 0;
8890     }
8891 
8892     if (table_buf_0x9400_len)
8893     {
8894       process_Sony_0x9400(table_buf_0x9400, table_buf_0x9400_len, unique_id);
8895       free(table_buf_0x9400);
8896       table_buf_0x9400_len = 0;
8897     }
8898 
8899     if (table_buf_0x9402_len)
8900     {
8901       process_Sony_0x9402(table_buf_0x9402, table_buf_0x9402_len);
8902       free(table_buf_0x9402);
8903       table_buf_0x9402_len = 0;
8904     }
8905 
8906     if (table_buf_0x9403_len)
8907     {
8908       process_Sony_0x9403(table_buf_0x9403, table_buf_0x9403_len);
8909       free(table_buf_0x9403);
8910       table_buf_0x9403_len = 0;
8911     }
8912 
8913     if (table_buf_0x9406_len)
8914     {
8915       process_Sony_0x9406(table_buf_0x9406, table_buf_0x9406_len);
8916       free(table_buf_0x9406);
8917       table_buf_0x9406_len = 0;
8918     }
8919 
8920     if (table_buf_0x940c_len)
8921     {
8922       process_Sony_0x940c(table_buf_0x940c, table_buf_0x940c_len);
8923       free(table_buf_0x940c);
8924       table_buf_0x940c_len = 0;
8925     }
8926 
8927     if (table_buf_0x940e_len)
8928     {
8929       process_Sony_0x940e(table_buf_0x940e, table_buf_0x940e_len, unique_id);
8930       free(table_buf_0x940e);
8931       table_buf_0x940e_len = 0;
8932     }
8933   }
8934 
8935   else if ((tag == 0x0010) && // CameraInfo
8936            strncasecmp(model, "DSLR-A100", 9) && strncasecmp(model, "NEX-5C", 6) && !strncasecmp(make, "SONY", 4) &&
8937            ((len == 368) ||  // a700
8938             (len == 5478) || // a850, a900
8939             (len == 5506) || // a200, a300, a350
8940             (len == 6118) || // a230, a290, a330, a380, a390
8941                              // a450, a500, a550, a560, a580
8942                              // a33, a35, a55
8943                              // NEX3, NEX5, NEX5C, NEXC3, VG10E
8944             (len == 15360)))
8945   {
8946     table_buf = (uchar *)malloc(len);
8947     fread(table_buf, len, 1, ifp);
8948     if (memcmp(table_buf, "\xff\xff\xff\xff\xff\xff\xff\xff", 8) &&
8949         memcmp(table_buf, "\x00\x00\x00\x00\x00\x00\x00\x00", 8))
8950     {
8951       switch (len)
8952       {
8953       case 368:
8954       case 5478:
8955         // a700, a850, a900: CameraInfo
8956         if ((!dng_writer) ||
8957             (saneSonyCameraInfo(table_buf[0], table_buf[3], table_buf[2], table_buf[5], table_buf[4], table_buf[7])))
8958         {
8959           if (table_buf[0] | table_buf[3])
8960             imgdata.lens.makernotes.MinFocal = bcd2dec(table_buf[0]) * 100 + bcd2dec(table_buf[3]);
8961           if (table_buf[2] | table_buf[5])
8962             imgdata.lens.makernotes.MaxFocal = bcd2dec(table_buf[2]) * 100 + bcd2dec(table_buf[5]);
8963           if (table_buf[4])
8964             imgdata.lens.makernotes.MaxAp4MinFocal = bcd2dec(table_buf[4]) / 10.0f;
8965           if (table_buf[4])
8966             imgdata.lens.makernotes.MaxAp4MaxFocal = bcd2dec(table_buf[7]) / 10.0f;
8967           parseSonyLensFeatures(table_buf[1], table_buf[6]);
8968 
8969           if (len == 5478)
8970           {
8971             imgdata.makernotes.sony.AFMicroAdjValue = table_buf[304] - 20;
8972             imgdata.makernotes.sony.AFMicroAdjOn = (((table_buf[305] & 0x80) == 0x80) ? 1 : 0);
8973             imgdata.makernotes.sony.AFMicroAdjRegisteredLenses = table_buf[305] & 0x7f;
8974           }
8975         }
8976         break;
8977       default:
8978         // CameraInfo2 & 3
8979         if ((!dng_writer) ||
8980             (saneSonyCameraInfo(table_buf[1], table_buf[2], table_buf[3], table_buf[4], table_buf[5], table_buf[6])))
8981         {
8982           if (table_buf[1] | table_buf[2])
8983             imgdata.lens.makernotes.MinFocal = bcd2dec(table_buf[1]) * 100 + bcd2dec(table_buf[2]);
8984           if (table_buf[3] | table_buf[4])
8985             imgdata.lens.makernotes.MaxFocal = bcd2dec(table_buf[3]) * 100 + bcd2dec(table_buf[4]);
8986           if (table_buf[5])
8987             imgdata.lens.makernotes.MaxAp4MinFocal = bcd2dec(table_buf[5]) / 10.0f;
8988           if (table_buf[6])
8989             imgdata.lens.makernotes.MaxAp4MaxFocal = bcd2dec(table_buf[6]) / 10.0f;
8990           parseSonyLensFeatures(table_buf[0], table_buf[7]);
8991         }
8992       }
8993     }
8994     free(table_buf);
8995   }
8996 
8997   else if ((!dng_writer) && (tag == 0x0020) && // WBInfoA100, needs 0xb028 processing
8998            !strncasecmp(model, "DSLR-A100", 9))
8999   {
9000     fseek(ifp, 0x49dc, SEEK_CUR);
9001     stmread(imgdata.shootinginfo.InternalBodySerial, 12, ifp);
9002   }
9003 
9004   else if (tag == 0x0104)
9005   {
9006     imgdata.other.FlashEC = getreal(type);
9007   }
9008 
9009   else if (tag == 0x0105) // Teleconverter
9010   {
9011     imgdata.lens.makernotes.TeleconverterID = get2();
9012   }
9013 
9014   else if (tag == 0x0114 && len < 256000) // CameraSettings
9015   {
9016     table_buf = (uchar *)malloc(len);
9017     fread(table_buf, len, 1, ifp);
9018     switch (len)
9019     {
9020     case 280:
9021     case 364:
9022     case 332:
9023       // CameraSettings and CameraSettings2 are big endian
9024       if (table_buf[2] | table_buf[3])
9025       {
9026         lid = (((ushort)table_buf[2]) << 8) | ((ushort)table_buf[3]);
9027         imgdata.lens.makernotes.CurAp = libraw_powf64l(2.0f, ((float)lid / 8.0f - 1.0f) / 2.0f);
9028       }
9029       break;
9030     case 1536:
9031     case 2048:
9032       // CameraSettings3 are little endian
9033       parseSonyLensType2(table_buf[1016], table_buf[1015]);
9034       if (imgdata.lens.makernotes.LensMount != LIBRAW_MOUNT_Canon_EF)
9035       {
9036         switch (table_buf[153])
9037         {
9038         case 16:
9039           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Minolta_A;
9040           break;
9041         case 17:
9042           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Sony_E;
9043           break;
9044         }
9045       }
9046       break;
9047     }
9048     free(table_buf);
9049   }
9050 
9051   else if ((tag == 0x3000) && (len < 256000))
9052   {
9053     uchar *table_buf_0x3000;
9054     table_buf_0x3000 = (uchar *)malloc(len);
9055     fread(table_buf_0x3000, len, 1, ifp);
9056     for (int i = 0; i < 20; i++)
9057       imgdata.makernotes.sony.SonyDateTime[i] = table_buf_0x3000[6 + i];
9058   }
9059 
9060   else if (tag == 0x0116 && len < 256000)
9061   {
9062     table_buf_0x0116 = (uchar *)malloc(len);
9063     table_buf_0x0116_len = len;
9064     fread(table_buf_0x0116, len, 1, ifp);
9065     if (imgdata.lens.makernotes.CamID)
9066     {
9067       process_Sony_0x0116(table_buf_0x0116, table_buf_0x0116_len, imgdata.lens.makernotes.CamID);
9068       free(table_buf_0x0116);
9069       table_buf_0x0116_len = 0;
9070     }
9071   }
9072 
9073   else if (tag == 0x2010 && len < 256000)
9074   {
9075     table_buf_0x2010 = (uchar *)malloc(len);
9076     table_buf_0x2010_len = len;
9077     fread(table_buf_0x2010, len, 1, ifp);
9078     if (imgdata.lens.makernotes.CamID)
9079     {
9080       process_Sony_0x2010(table_buf_0x2010, table_buf_0x2010_len);
9081       free(table_buf_0x2010);
9082       table_buf_0x2010_len = 0;
9083     }
9084   }
9085 
9086   else if (tag == 0x201a)
9087   {
9088     imgdata.makernotes.sony.ElectronicFrontCurtainShutter = get4();
9089   }
9090 
9091   else if (tag == 0x201b)
9092   {
9093     uchar uc;
9094     fread(&uc, 1, 1, ifp);
9095     imgdata.shootinginfo.FocusMode = (short)uc;
9096   }
9097 
9098   else if (tag == 0x202c)
9099   {
9100     imgdata.makernotes.sony.MeteringMode2 = get2();
9101   }
9102 
9103   else if (tag == 0x9050 && len < 256000) // little endian
9104   {
9105     table_buf_0x9050 = (uchar *)malloc(len);
9106     table_buf_0x9050_len = len;
9107     fread(table_buf_0x9050, len, 1, ifp);
9108 
9109     if (imgdata.lens.makernotes.CamID)
9110     {
9111       process_Sony_0x9050(table_buf_0x9050, table_buf_0x9050_len, imgdata.lens.makernotes.CamID);
9112       free(table_buf_0x9050);
9113       table_buf_0x9050_len = 0;
9114     }
9115   }
9116 
9117   else if (tag == 0x9400 && len < 256000)
9118   {
9119     table_buf_0x9400 = (uchar *)malloc(len);
9120     table_buf_0x9400_len = len;
9121     fread(table_buf_0x9400, len, 1, ifp);
9122     if (imgdata.lens.makernotes.CamID)
9123     {
9124       process_Sony_0x9400(table_buf_0x9400, table_buf_0x9400_len, unique_id);
9125       free(table_buf_0x9400);
9126       table_buf_0x9400_len = 0;
9127     }
9128   }
9129 
9130   else if (tag == 0x9402 && len < 256000)
9131   {
9132     table_buf_0x9402 = (uchar *)malloc(len);
9133     table_buf_0x9402_len = len;
9134     fread(table_buf_0x9402, len, 1, ifp);
9135     if (imgdata.lens.makernotes.CamID)
9136     {
9137       process_Sony_0x9402(table_buf_0x9402, table_buf_0x9402_len);
9138       free(table_buf_0x9402);
9139       table_buf_0x9402_len = 0;
9140     }
9141   }
9142 
9143   else if (tag == 0x9403 && len < 256000)
9144   {
9145     table_buf_0x9403 = (uchar *)malloc(len);
9146     table_buf_0x9403_len = len;
9147     fread(table_buf_0x9403, len, 1, ifp);
9148     if (imgdata.lens.makernotes.CamID)
9149     {
9150       process_Sony_0x9403(table_buf_0x9403, table_buf_0x9403_len);
9151       free(table_buf_0x9403);
9152       table_buf_0x9403_len = 0;
9153     }
9154   }
9155 
9156   else if ((tag == 0x9405) && (len < 256000) && (len > 0x64))
9157   {
9158     uchar *table_buf_0x9405;
9159     table_buf_0x9405 = (uchar *)malloc(len);
9160     fread(table_buf_0x9405, len, 1, ifp);
9161     uchar bufx = table_buf_0x9405[0x0];
9162     if (imgdata.other.real_ISO < 0.1f)
9163     {
9164       if ((bufx == 0x25) || (bufx == 0x3a) || (bufx == 0x76) || (bufx == 0x7e) || (bufx == 0x8b) || (bufx == 0x9a) ||
9165           (bufx == 0xb3) || (bufx == 0xe1))
9166       {
9167         uchar s[2];
9168         s[0] = SonySubstitution[table_buf_0x9405[0x04]];
9169         s[1] = SonySubstitution[table_buf_0x9405[0x05]];
9170         imgdata.other.real_ISO = 100.0f * libraw_powf64l(2.0f, (16 - ((float)sget2(s)) / 256.0f));
9171       }
9172     }
9173     free(table_buf_0x9405);
9174   }
9175 
9176   else if (tag == 0x9406 && len < 256000)
9177   {
9178     table_buf_0x9406 = (uchar *)malloc(len);
9179     table_buf_0x9406_len = len;
9180     fread(table_buf_0x9406, len, 1, ifp);
9181     if (imgdata.lens.makernotes.CamID)
9182     {
9183       process_Sony_0x9406(table_buf_0x9406, table_buf_0x9406_len);
9184       free(table_buf_0x9406);
9185       table_buf_0x9406_len = 0;
9186     }
9187   }
9188 
9189   else if (tag == 0x940c && len < 256000)
9190   {
9191     table_buf_0x940c = (uchar *)malloc(len);
9192     table_buf_0x940c_len = len;
9193     fread(table_buf_0x940c, len, 1, ifp);
9194     if (imgdata.lens.makernotes.CamID)
9195     {
9196       process_Sony_0x940c(table_buf_0x940c, table_buf_0x940c_len);
9197       free(table_buf_0x940c);
9198       table_buf_0x940c_len = 0;
9199     }
9200   }
9201 
9202   else if (tag == 0x940e && len < 256000)
9203   {
9204     table_buf_0x940e = (uchar *)malloc(len);
9205     table_buf_0x940e_len = len;
9206     fread(table_buf_0x940e, len, 1, ifp);
9207     if (imgdata.lens.makernotes.CamID)
9208     {
9209       process_Sony_0x940e(table_buf_0x940e, table_buf_0x940e_len, imgdata.lens.makernotes.CamID);
9210       free(table_buf_0x940e);
9211       table_buf_0x940e_len = 0;
9212     }
9213   }
9214 
9215   else if (((tag == 0xb027) || (tag == 0x010c)) && (imgdata.lens.makernotes.LensID == -1))
9216   {
9217     imgdata.lens.makernotes.LensID = get4();
9218     if ((imgdata.lens.makernotes.LensID > 0x4900) && (imgdata.lens.makernotes.LensID <= 0x5900))
9219     {
9220       imgdata.lens.makernotes.AdapterID = 0x4900;
9221       imgdata.lens.makernotes.LensID -= imgdata.lens.makernotes.AdapterID;
9222       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Sigma_X3F;
9223       strcpy(imgdata.lens.makernotes.Adapter, "MC-11");
9224     }
9225 
9226     else if ((imgdata.lens.makernotes.LensID > 0xEF00) && (imgdata.lens.makernotes.LensID < 0xFFFF) &&
9227              (imgdata.lens.makernotes.LensID != 0xFF00))
9228     {
9229       imgdata.lens.makernotes.AdapterID = 0xEF00;
9230       imgdata.lens.makernotes.LensID -= imgdata.lens.makernotes.AdapterID;
9231       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Canon_EF;
9232     }
9233     if (tag == 0x010c)
9234       imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Minolta_A;
9235   }
9236 
9237   else if (tag == 0xb02a && len < 256000) // Sony LensSpec
9238   {
9239     table_buf = (uchar *)malloc(len);
9240     fread(table_buf, len, 1, ifp);
9241     if ((!dng_writer) ||
9242         (saneSonyCameraInfo(table_buf[1], table_buf[2], table_buf[3], table_buf[4], table_buf[5], table_buf[6])))
9243     {
9244       if (table_buf[1] | table_buf[2])
9245         imgdata.lens.makernotes.MinFocal = bcd2dec(table_buf[1]) * 100 + bcd2dec(table_buf[2]);
9246       if (table_buf[3] | table_buf[4])
9247         imgdata.lens.makernotes.MaxFocal = bcd2dec(table_buf[3]) * 100 + bcd2dec(table_buf[4]);
9248       if (table_buf[5])
9249         imgdata.lens.makernotes.MaxAp4MinFocal = bcd2dec(table_buf[5]) / 10.0f;
9250       if (table_buf[6])
9251         imgdata.lens.makernotes.MaxAp4MaxFocal = bcd2dec(table_buf[6]) / 10.0f;
9252       parseSonyLensFeatures(table_buf[0], table_buf[7]);
9253     }
9254     free(table_buf);
9255   }
9256 
9257   else if ((tag == 0xb02b) && !imgdata.sizes.raw_crop.cwidth && (len == 2))
9258   {
9259     imgdata.sizes.raw_crop.cheight = get4();
9260     imgdata.sizes.raw_crop.cwidth = get4();
9261   }
9262 }
9263 
parse_makernote_0xc634(int base,int uptag,unsigned dng_writer)9264 void CLASS parse_makernote_0xc634(int base, int uptag, unsigned dng_writer)
9265 {
9266   unsigned ver97 = 0, offset = 0, entries, tag, type, len, save, c;
9267   unsigned i;
9268 
9269   uchar NikonKey, ci, cj, ck;
9270   unsigned serial = 0;
9271   unsigned custom_serial = 0;
9272   unsigned NikonLensDataVersion = 0;
9273   unsigned lenNikonLensData = 0;
9274   unsigned NikonFlashInfoVersion = 0;
9275 
9276   uchar *CanonCameraInfo;
9277   unsigned lenCanonCameraInfo = 0;
9278   unsigned typeCanonCameraInfo = 0;
9279 
9280   uchar *table_buf;
9281   uchar *table_buf_0x0116;
9282   ushort table_buf_0x0116_len = 0;
9283   uchar *table_buf_0x2010;
9284   ushort table_buf_0x2010_len = 0;
9285   uchar *table_buf_0x9050;
9286   ushort table_buf_0x9050_len = 0;
9287   uchar *table_buf_0x9400;
9288   ushort table_buf_0x9400_len = 0;
9289   uchar *table_buf_0x9402;
9290   ushort table_buf_0x9402_len = 0;
9291   uchar *table_buf_0x9403;
9292   ushort table_buf_0x9403_len = 0;
9293   uchar *table_buf_0x9406;
9294   ushort table_buf_0x9406_len = 0;
9295   uchar *table_buf_0x940c;
9296   ushort table_buf_0x940c_len = 0;
9297   uchar *table_buf_0x940e;
9298   ushort table_buf_0x940e_len = 0;
9299 
9300   short morder, sorder = order;
9301   char buf[10];
9302   INT64 fsize = ifp->size();
9303 
9304   fread(buf, 1, 10, ifp);
9305 
9306   /*
9307     printf("===>>buf: 0x");
9308     for (int i = 0; i < sizeof buf; i ++) {
9309           printf("%02x", buf[i]);
9310     }
9311     putchar('\n');
9312   */
9313 
9314   if (!strcmp(buf, "Nikon"))
9315   {
9316     base = ftell(ifp);
9317     order = get2();
9318     if (get2() != 42)
9319       goto quit;
9320     offset = get4();
9321     fseek(ifp, offset - 8, SEEK_CUR);
9322   }
9323   else if (!strcmp(buf, "OLYMPUS") || !strcmp(buf, "PENTAX ") ||
9324            (!strncmp(make, "SAMSUNG", 7) && (dng_writer == CameraDNG)))
9325   {
9326     base = ftell(ifp) - 10;
9327     fseek(ifp, -2, SEEK_CUR);
9328     order = get2();
9329     if (buf[0] == 'O')
9330       get2();
9331   }
9332   else if (!strncmp(buf, "SONY", 4) || !strcmp(buf, "Panasonic"))
9333   {
9334     goto nf;
9335   }
9336   else if (!strncmp(buf, "FUJIFILM", 8))
9337   {
9338     base = ftell(ifp) - 10;
9339   nf:
9340     order = 0x4949;
9341     fseek(ifp, 2, SEEK_CUR);
9342   }
9343   else if (!strcmp(buf, "OLYMP") || !strcmp(buf, "LEICA") || !strcmp(buf, "Ricoh") || !strcmp(buf, "EPSON"))
9344     fseek(ifp, -2, SEEK_CUR);
9345   else if (!strcmp(buf, "AOC") || !strcmp(buf, "QVC"))
9346     fseek(ifp, -4, SEEK_CUR);
9347   else
9348   {
9349     fseek(ifp, -10, SEEK_CUR);
9350     if ((!strncmp(make, "SAMSUNG", 7) && (dng_writer == AdobeDNG)))
9351       base = ftell(ifp);
9352   }
9353 
9354   entries = get2();
9355   if (entries > 1000)
9356     return;
9357   morder = order;
9358 
9359   while (entries--)
9360   {
9361     order = morder;
9362     tiff_get(base, &tag, &type, &len, &save);
9363     INT64 pos = ifp->tell();
9364     if (len > 8 && pos + len > 2 * fsize)
9365     {
9366       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
9367       continue;
9368     }
9369     tag |= uptag << 16;
9370     if (len > 100 * 1024 * 1024)
9371       goto next; // 100Mb tag? No!
9372 
9373     if (!strncmp(make, "Canon", 5))
9374     {
9375       if (tag == 0x000d && len < 256000) // camera info
9376       {
9377         if (type != 4)
9378         {
9379           CanonCameraInfo = (uchar *)malloc(MAX(16, len));
9380           fread(CanonCameraInfo, len, 1, ifp);
9381         }
9382         else
9383         {
9384           CanonCameraInfo = (uchar *)malloc(MAX(16, len * 4));
9385           fread(CanonCameraInfo, len, 4, ifp);
9386         }
9387         lenCanonCameraInfo = len;
9388         typeCanonCameraInfo = type;
9389       }
9390 
9391       else if (tag == 0x10) // Canon ModelID
9392       {
9393         unique_id = get4();
9394         unique_id = setCanonBodyFeatures(unique_id);
9395         if (lenCanonCameraInfo)
9396         {
9397           processCanonCameraInfo(unique_id, CanonCameraInfo, lenCanonCameraInfo, typeCanonCameraInfo);
9398           free(CanonCameraInfo);
9399           CanonCameraInfo = 0;
9400           lenCanonCameraInfo = 0;
9401         }
9402       }
9403 
9404       else
9405         parseCanonMakernotes(tag, type, len);
9406     }
9407 
9408     else if (!strncmp(make, "FUJI", 4))
9409       parseFujiMakernotes(tag, type);
9410 
9411     else if (!strncasecmp(make, "LEICA", 5))
9412     {
9413 
9414       if ((tag == 0x0320) && (type == 9) && (len == 1) && !strncasecmp(make, "Leica Camera AG", 15) &&
9415           !strncmp(buf, "LEICA", 5) && (buf[5] == 0) && (buf[6] == 0) && (buf[7] == 0))
9416         imgdata.other.CameraTemperature = getreal(type);
9417 
9418       if (tag == 0x34003402)
9419         imgdata.other.CameraTemperature = getreal(type);
9420 
9421       if (((tag == 0x035e) || (tag == 0x035f)) && (type == 10) && (len == 9))
9422       {
9423         int ind = tag == 0x035e ? 0 : 1;
9424         for (int j = 0; j < 3; j++)
9425           FORCC imgdata.color.dng_color[ind].forwardmatrix[j][c] = getreal(type);
9426         imgdata.color.dng_color[ind].parsedfields |= LIBRAW_DNGFM_FORWARDMATRIX;
9427       }
9428       if ((tag == 0x0303) && (type != 4))
9429       {
9430         stmread(imgdata.lens.makernotes.Lens, len, ifp);
9431       }
9432 
9433       if ((tag == 0x3405) || (tag == 0x0310) || (tag == 0x34003405))
9434       {
9435         imgdata.lens.makernotes.LensID = get4();
9436         imgdata.lens.makernotes.LensID =
9437             ((imgdata.lens.makernotes.LensID >> 2) << 8) | (imgdata.lens.makernotes.LensID & 0x3);
9438         if (imgdata.lens.makernotes.LensID != -1)
9439         {
9440           if ((model[0] == 'M') || !strncasecmp(model, "LEICA M", 7))
9441           {
9442             imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Leica_M;
9443             if (imgdata.lens.makernotes.LensID)
9444               imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Leica_M;
9445           }
9446           else if ((model[0] == 'S') || !strncasecmp(model, "LEICA S", 7))
9447           {
9448             imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Leica_S;
9449             if (imgdata.lens.makernotes.Lens[0])
9450               imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Leica_S;
9451           }
9452         }
9453       }
9454 
9455       else if (((tag == 0x0313) || (tag == 0x34003406)) && (fabs(imgdata.lens.makernotes.CurAp) < 0.17f) &&
9456                ((type == 10) || (type == 5)))
9457       {
9458         imgdata.lens.makernotes.CurAp = getreal(type);
9459         if (imgdata.lens.makernotes.CurAp > 126.3)
9460           imgdata.lens.makernotes.CurAp = 0.0f;
9461       }
9462 
9463       else if (tag == 0x3400)
9464       {
9465         parse_makernote(base, 0x3400);
9466       }
9467     }
9468 
9469     else if (!strncmp(make, "NIKON", 5))
9470     {
9471       if (tag == 0x1d) // serial number
9472         while ((c = fgetc(ifp)) && c != EOF)
9473         {
9474           if ((!custom_serial) && (!isdigit(c)))
9475           {
9476             if ((strbuflen(model) == 3) && (!strcmp(model, "D50")))
9477             {
9478               custom_serial = 34;
9479             }
9480             else
9481             {
9482               custom_serial = 96;
9483             }
9484           }
9485           serial = serial * 10 + (isdigit(c) ? c - '0' : c % 10);
9486         }
9487       else if (tag == 0x000a)
9488       {
9489         imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
9490         imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
9491       }
9492       else if (tag == 0x0082) // lens attachment
9493       {
9494         stmread(imgdata.lens.makernotes.Attachment, len, ifp);
9495       }
9496       else if (tag == 0x0083) // lens type
9497       {
9498         imgdata.lens.nikon.NikonLensType = fgetc(ifp);
9499       }
9500       else if (tag == 0x0084) // lens
9501       {
9502         imgdata.lens.makernotes.MinFocal = getreal(type);
9503         imgdata.lens.makernotes.MaxFocal = getreal(type);
9504         imgdata.lens.makernotes.MaxAp4MinFocal = getreal(type);
9505         imgdata.lens.makernotes.MaxAp4MaxFocal = getreal(type);
9506       }
9507       else if (tag == 0x008b) // lens f-stops
9508       {
9509         uchar a, b, c;
9510         a = fgetc(ifp);
9511         b = fgetc(ifp);
9512         c = fgetc(ifp);
9513         if (c)
9514         {
9515           imgdata.lens.nikon.NikonLensFStops = a * b * (12 / c);
9516           imgdata.lens.makernotes.LensFStops = (float)imgdata.lens.nikon.NikonLensFStops / 12.0f;
9517         }
9518       }
9519       else if (tag == 0x0093)
9520       {
9521         imgdata.makernotes.nikon.NEFCompression = i = get2();
9522         if ((i == 7) || (i == 9))
9523         {
9524           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
9525           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
9526         }
9527       }
9528       else if (tag == 0x0097)
9529       {
9530         for (i = 0; i < 4; i++)
9531           ver97 = ver97 * 10 + fgetc(ifp) - '0';
9532         if (ver97 == 601) // Coolpix A
9533         {
9534           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
9535           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
9536         }
9537       }
9538       else if (tag == 0x0098) // contains lens data
9539       {
9540         for (i = 0; i < 4; i++)
9541         {
9542           NikonLensDataVersion = NikonLensDataVersion * 10 + fgetc(ifp) - '0';
9543         }
9544         switch (NikonLensDataVersion)
9545         {
9546         case 100:
9547           lenNikonLensData = 9;
9548           break;
9549         case 101:
9550         case 201: // encrypted, starting from v.201
9551         case 202:
9552         case 203:
9553           lenNikonLensData = 15;
9554           break;
9555         case 204:
9556           lenNikonLensData = 16;
9557           break;
9558         case 400:
9559           lenNikonLensData = 459;
9560           break;
9561         case 401:
9562           lenNikonLensData = 590;
9563           break;
9564         case 402:
9565           lenNikonLensData = 509;
9566           break;
9567         case 403:
9568           lenNikonLensData = 879;
9569           break;
9570         }
9571         if (lenNikonLensData)
9572         {
9573           table_buf = (uchar *)malloc(lenNikonLensData);
9574           fread(table_buf, lenNikonLensData, 1, ifp);
9575           if ((NikonLensDataVersion < 201) && lenNikonLensData)
9576           {
9577             processNikonLensData(table_buf, lenNikonLensData);
9578             free(table_buf);
9579             lenNikonLensData = 0;
9580           }
9581         }
9582       }
9583 
9584       else if (tag == 0xa7) // shutter count
9585       {
9586         NikonKey = fgetc(ifp) ^ fgetc(ifp) ^ fgetc(ifp) ^ fgetc(ifp);
9587         if ((NikonLensDataVersion > 200) && lenNikonLensData)
9588         {
9589           if (custom_serial)
9590           {
9591             ci = xlat[0][custom_serial];
9592           }
9593           else
9594           {
9595             ci = xlat[0][serial & 0xff];
9596           }
9597           cj = xlat[1][NikonKey];
9598           ck = 0x60;
9599           for (i = 0; i < lenNikonLensData; i++)
9600             table_buf[i] ^= (cj += ci * ck++);
9601           processNikonLensData(table_buf, lenNikonLensData);
9602           lenNikonLensData = 0;
9603           free(table_buf);
9604         }
9605       }
9606       else if (tag == 0x00a8) // contains flash data
9607       {
9608         for (i = 0; i < 4; i++)
9609         {
9610           NikonFlashInfoVersion = NikonFlashInfoVersion * 10 + fgetc(ifp) - '0';
9611         }
9612       }
9613 
9614       else if (tag == 0x00b0)
9615       {
9616         get4(); // ME tag version, 4 symbols
9617         imgdata.makernotes.nikon.ExposureMode = get4();
9618         imgdata.makernotes.nikon.nMEshots = get4();
9619         imgdata.makernotes.nikon.MEgainOn = get4();
9620       }
9621 
9622       else if (tag == 0x00b9)
9623       {
9624         uchar uc;
9625         int8_t sc;
9626         fread(&uc, 1, 1, ifp);
9627         imgdata.makernotes.nikon.AFFineTune = uc;
9628         fread(&uc, 1, 1, ifp);
9629         imgdata.makernotes.nikon.AFFineTuneIndex = uc;
9630         fread(&sc, 1, 1, ifp);
9631         imgdata.makernotes.nikon.AFFineTuneAdj = sc;
9632       }
9633 
9634       else if (tag == 37 && (!iso_speed || iso_speed == 65535))
9635       {
9636         unsigned char cc;
9637         fread(&cc, 1, 1, ifp);
9638         iso_speed = (int)(100.0 * libraw_powf64l(2.0, (double)(cc) / 12.0 - 5.0));
9639         break;
9640       }
9641     }
9642 
9643     else if (!strncmp(make, "OLYMPUS", 7))
9644     {
9645       short nWB, tWB;
9646       int SubDirOffsetValid = strncmp(model, "E-300", 5) && strncmp(model, "E-330", 5) && strncmp(model, "E-400", 5) &&
9647                               strncmp(model, "E-500", 5) && strncmp(model, "E-1", 3);
9648 
9649       if ((tag == 0x2010) || (tag == 0x2020) || (tag == 0x2030) || (tag == 0x2031) || (tag == 0x2040) ||
9650           (tag == 0x2050) || (tag == 0x3000))
9651       {
9652         fseek(ifp, save - 4, SEEK_SET);
9653         fseek(ifp, base + get4(), SEEK_SET);
9654         parse_makernote_0xc634(base, tag, dng_writer);
9655       }
9656       if (!SubDirOffsetValid && ((len > 4) || (((type == 3) || (type == 8)) && (len > 2)) ||
9657                                  (((type == 4) || (type == 9)) && (len > 1)) || (type == 5) || (type > 9)))
9658         goto skip_Oly_broken_tags;
9659 
9660       if ((tag >= 0x20400101) && (tag <= 0x20400111))
9661       {
9662         if ((tag == 0x20400101) && (len == 2) && (!strncasecmp(model, "E-410", 5) || !strncasecmp(model, "E-510", 5)))
9663         {
9664           int i;
9665           for (i = 0; i < 64; i++)
9666           {
9667             imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = imgdata.color.WB_Coeffs[i][1] =
9668                 imgdata.color.WB_Coeffs[i][3] = 0x100;
9669           }
9670           for (i = 64; i < 256; i++)
9671           {
9672             imgdata.color.WB_Coeffs[i][1] = imgdata.color.WB_Coeffs[i][3] = 0x100;
9673           }
9674         }
9675         nWB = tag - 0x20400101;
9676         tWB = Oly_wb_list2[nWB << 1];
9677         ushort CT = Oly_wb_list2[(nWB << 1) | 1];
9678         int wb[4];
9679         wb[0] = get2();
9680         wb[2] = get2();
9681         if (tWB != 0x100)
9682         {
9683           imgdata.color.WB_Coeffs[tWB][0] = wb[0];
9684           imgdata.color.WB_Coeffs[tWB][2] = wb[2];
9685         }
9686         if (CT)
9687         {
9688           imgdata.color.WBCT_Coeffs[nWB - 1][0] = CT;
9689           imgdata.color.WBCT_Coeffs[nWB - 1][1] = wb[0];
9690           imgdata.color.WBCT_Coeffs[nWB - 1][3] = wb[2];
9691         }
9692         if (len == 4)
9693         {
9694           wb[1] = get2();
9695           wb[3] = get2();
9696           if (tWB != 0x100)
9697           {
9698             imgdata.color.WB_Coeffs[tWB][1] = wb[1];
9699             imgdata.color.WB_Coeffs[tWB][3] = wb[3];
9700           }
9701           if (CT)
9702           {
9703             imgdata.color.WBCT_Coeffs[nWB - 1][2] = wb[1];
9704             imgdata.color.WBCT_Coeffs[nWB - 1][4] = wb[3];
9705           }
9706         }
9707       }
9708       else if ((tag >= 0x20400112) && (tag <= 0x2040011e))
9709       {
9710         nWB = tag - 0x20400112;
9711         int wbG = get2();
9712         tWB = Oly_wb_list2[nWB << 1];
9713         if (nWB)
9714           imgdata.color.WBCT_Coeffs[nWB - 1][2] = imgdata.color.WBCT_Coeffs[nWB - 1][4] = wbG;
9715         if (tWB != 0x100)
9716           imgdata.color.WB_Coeffs[tWB][1] = imgdata.color.WB_Coeffs[tWB][3] = wbG;
9717       }
9718       else if (tag == 0x2040011f)
9719       {
9720         int wbG = get2();
9721         if (imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][0])
9722           imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][3] = wbG;
9723         FORC4 if (imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1 + c][0])
9724             imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1 + c][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1 + c][3] =
9725             wbG;
9726       }
9727       else if (tag == 0x20400121)
9728       {
9729         imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][0] = get2();
9730         imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][2] = get2();
9731         if (len == 4)
9732         {
9733           imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][1] = get2();
9734           imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][3] = get2();
9735         }
9736       }
9737       else if ((tag == 0x30000110) && strcmp(software, "v757-71"))
9738       {
9739         imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][0] = get2();
9740         imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][2] = get2();
9741         if (len == 2)
9742         {
9743           for (int i = 0; i < 256; i++)
9744             imgdata.color.WB_Coeffs[i][1] = imgdata.color.WB_Coeffs[i][3] = 0x100;
9745         }
9746       }
9747       else if ((((tag >= 0x30000120) && (tag <= 0x30000124)) || ((tag >= 0x30000130) && (tag <= 0x30000133))) &&
9748                strcmp(software, "v757-71"))
9749       {
9750         int wb_ind;
9751         if (tag <= 0x30000124)
9752           wb_ind = tag - 0x30000120;
9753         else
9754           wb_ind = tag - 0x30000130 + 5;
9755         imgdata.color.WB_Coeffs[Oly_wb_list1[wb_ind]][0] = get2();
9756         imgdata.color.WB_Coeffs[Oly_wb_list1[wb_ind]][2] = get2();
9757       }
9758       else
9759       {
9760         switch (tag)
9761         {
9762         case 0x0207:
9763         case 0x20100100:
9764         {
9765           uchar sOlyID[8];
9766           fread(sOlyID, MIN(len, 7), 1, ifp);
9767           sOlyID[7] = 0;
9768           OlyID = sOlyID[0];
9769           i = 1;
9770           while (i < 7 && sOlyID[i])
9771           {
9772             OlyID = OlyID << 8 | sOlyID[i];
9773             i++;
9774           }
9775           setOlympusBodyFeatures(OlyID);
9776         }
9777         break;
9778         case 0x1002:
9779           imgdata.lens.makernotes.CurAp = libraw_powf64l(2.0f, getreal(type) / 2);
9780           break;
9781         case 0x20100102:
9782           stmread(imgdata.shootinginfo.InternalBodySerial, len, ifp);
9783           break;
9784         case 0x20100201:
9785           imgdata.lens.makernotes.LensID = (unsigned long long)fgetc(ifp) << 16 |
9786                                            (unsigned long long)(fgetc(ifp), fgetc(ifp)) << 8 |
9787                                            (unsigned long long)fgetc(ifp);
9788           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FT;
9789           imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_FT;
9790           if (((imgdata.lens.makernotes.LensID < 0x20000) || (imgdata.lens.makernotes.LensID > 0x4ffff)) &&
9791               (imgdata.lens.makernotes.LensID & 0x10))
9792           {
9793             imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_mFT;
9794           }
9795           break;
9796         case 0x20100202:
9797           if ((!imgdata.lens.LensSerial[0]))
9798             stmread(imgdata.lens.LensSerial, len, ifp);
9799           break;
9800         case 0x20100203:
9801           stmread(imgdata.lens.makernotes.Lens, len, ifp);
9802           break;
9803         case 0x20100205:
9804           imgdata.lens.makernotes.MaxAp4MinFocal = libraw_powf64l(sqrt(2.0f), get2() / 256.0f);
9805           break;
9806         case 0x20100206:
9807           imgdata.lens.makernotes.MaxAp4MaxFocal = libraw_powf64l(sqrt(2.0f), get2() / 256.0f);
9808           break;
9809         case 0x20100207:
9810           imgdata.lens.makernotes.MinFocal = (float)get2();
9811           break;
9812         case 0x20100208:
9813           imgdata.lens.makernotes.MaxFocal = (float)get2();
9814           if (imgdata.lens.makernotes.MaxFocal > 1000.0f)
9815             imgdata.lens.makernotes.MaxFocal = imgdata.lens.makernotes.MinFocal;
9816           break;
9817         case 0x2010020a:
9818           imgdata.lens.makernotes.MaxAp4CurFocal = libraw_powf64l(sqrt(2.0f), get2() / 256.0f);
9819           break;
9820         case 0x20100301:
9821           imgdata.lens.makernotes.TeleconverterID = fgetc(ifp) << 8;
9822           fgetc(ifp);
9823           imgdata.lens.makernotes.TeleconverterID = imgdata.lens.makernotes.TeleconverterID | fgetc(ifp);
9824           break;
9825         case 0x20100303:
9826           stmread(imgdata.lens.makernotes.Teleconverter, len, ifp);
9827           break;
9828         case 0x20100403:
9829           stmread(imgdata.lens.makernotes.Attachment, len, ifp);
9830           break;
9831         case 0x20200306:
9832         {
9833           uchar uc;
9834           fread(&uc, 1, 1, ifp);
9835           imgdata.makernotes.olympus.AFFineTune = uc;
9836         }
9837         break;
9838         case 0x20200307:
9839           FORC3 imgdata.makernotes.olympus.AFFineTuneAdj[c] = get2();
9840           break;
9841         case 0x20200401:
9842           imgdata.other.FlashEC = getreal(type);
9843           break;
9844         case 0x1007:
9845           imgdata.other.SensorTemperature = (float)get2();
9846           break;
9847         case 0x1008:
9848           imgdata.other.LensTemperature = (float)get2();
9849           break;
9850         case 0x20401306:
9851         {
9852           int temp = get2();
9853           if ((temp != 0) && (temp != 100))
9854           {
9855             if (temp < 61)
9856               imgdata.other.CameraTemperature = (float)temp;
9857             else
9858               imgdata.other.CameraTemperature = (float)(temp - 32) / 1.8f;
9859             if ((OlyID == 0x4434353933ULL) && // TG-5
9860                 (imgdata.other.exifAmbientTemperature > -273.15f))
9861               imgdata.other.CameraTemperature += imgdata.other.exifAmbientTemperature;
9862           }
9863         }
9864         break;
9865         case 0x20501500:
9866           if (OlyID != 0x0ULL)
9867           {
9868             short temp = get2();
9869             if ((OlyID == 0x4434303430ULL) || // E-1
9870                 (OlyID == 0x5330303336ULL) || // E-M5
9871                 (len != 1))
9872               imgdata.other.SensorTemperature = (float)temp;
9873             else if ((temp != -32768) && (temp != 0))
9874             {
9875               if (temp > 199)
9876                 imgdata.other.SensorTemperature = 86.474958f - 0.120228f * (float)temp;
9877               else
9878                 imgdata.other.SensorTemperature = (float)temp;
9879             }
9880           }
9881           break;
9882         }
9883       }
9884     skip_Oly_broken_tags:;
9885     }
9886 
9887     else if (!strncmp(make, "PENTAX", 6) || !strncmp(model, "PENTAX", 6) ||
9888              (!strncmp(make, "SAMSUNG", 7) && (dng_writer == CameraDNG)))
9889     {
9890       if (tag == 0x0005)
9891       {
9892         unique_id = get4();
9893         setPentaxBodyFeatures(unique_id);
9894       }
9895       else if (tag == 0x000d)
9896       {
9897         imgdata.makernotes.pentax.FocusMode = get2();
9898       }
9899       else if (tag == 0x000e)
9900       {
9901         imgdata.makernotes.pentax.AFPointSelected = get2();
9902       }
9903       else if (tag == 0x000f)
9904       {
9905         imgdata.makernotes.pentax.AFPointsInFocus = getint(type);
9906       }
9907       else if (tag == 0x0010)
9908       {
9909         imgdata.makernotes.pentax.FocusPosition = get2();
9910       }
9911       else if (tag == 0x0013)
9912       {
9913         imgdata.lens.makernotes.CurAp = (float)get2() / 10.0f;
9914       }
9915       else if (tag == 0x0014)
9916       {
9917         PentaxISO(get2());
9918       }
9919       else if (tag == 0x001d)
9920       {
9921         imgdata.lens.makernotes.CurFocal = (float)get4() / 100.0f;
9922       }
9923       else if (tag == 0x0034)
9924       {
9925         uchar uc;
9926         FORC4
9927         {
9928           fread(&uc, 1, 1, ifp);
9929           imgdata.makernotes.pentax.DriveMode[c] = uc;
9930         }
9931       }
9932       else if (tag == 0x0038)
9933       {
9934         imgdata.sizes.raw_crop.cleft = get2();
9935         imgdata.sizes.raw_crop.ctop = get2();
9936       }
9937       else if (tag == 0x0039)
9938       {
9939         imgdata.sizes.raw_crop.cwidth = get2();
9940         imgdata.sizes.raw_crop.cheight = get2();
9941       }
9942       else if (tag == 0x003f)
9943       {
9944         imgdata.lens.makernotes.LensID = fgetc(ifp) << 8 | fgetc(ifp);
9945       }
9946       else if (tag == 0x0047)
9947       {
9948         imgdata.other.CameraTemperature = (float)fgetc(ifp);
9949       }
9950       else if (tag == 0x004d)
9951       {
9952         if (type == 9)
9953           imgdata.other.FlashEC = getreal(type) / 256.0f;
9954         else
9955           imgdata.other.FlashEC = (float)((signed short)fgetc(ifp)) / 6.0f;
9956       }
9957       else if (tag == 0x0072)
9958       {
9959         imgdata.makernotes.pentax.AFAdjustment = get2();
9960       }
9961       else if (tag == 0x007e)
9962       {
9963         imgdata.color.linear_max[0] = imgdata.color.linear_max[1] = imgdata.color.linear_max[2] =
9964             imgdata.color.linear_max[3] = (long)(-1) * get4();
9965       }
9966       else if (tag == 0x0207)
9967       {
9968         if (len < 65535) // Safety belt
9969           PentaxLensInfo(imgdata.lens.makernotes.CamID, len);
9970       }
9971       else if ((tag >= 0x020d) && (tag <= 0x0214))
9972       {
9973         FORC4 imgdata.color.WB_Coeffs[Pentax_wb_list1[tag - 0x020d]][c ^ (c >> 1)] = get2();
9974       }
9975       else if (tag == 0x0221)
9976       {
9977         int nWB = get2();
9978         if (nWB <= sizeof(imgdata.color.WBCT_Coeffs) / sizeof(imgdata.color.WBCT_Coeffs[0]))
9979           for (int i = 0; i < nWB; i++)
9980           {
9981             imgdata.color.WBCT_Coeffs[i][0] = (unsigned)0xcfc6 - get2();
9982             fseek(ifp, 2, SEEK_CUR);
9983             imgdata.color.WBCT_Coeffs[i][1] = get2();
9984             imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = 0x2000;
9985             imgdata.color.WBCT_Coeffs[i][3] = get2();
9986           }
9987       }
9988       else if (tag == 0x0215)
9989       {
9990         fseek(ifp, 16, SEEK_CUR);
9991         sprintf(imgdata.shootinginfo.InternalBodySerial, "%d", get4());
9992       }
9993       else if (tag == 0x0229)
9994       {
9995         stmread(imgdata.shootinginfo.BodySerial, len, ifp);
9996       }
9997       else if (tag == 0x022d)
9998       {
9999         int wb_ind;
10000         getc(ifp);
10001         for (int wb_cnt = 0; wb_cnt < nPentax_wb_list2; wb_cnt++)
10002         {
10003           wb_ind = getc(ifp);
10004           if (wb_ind < nPentax_wb_list2)
10005             FORC4 imgdata.color.WB_Coeffs[Pentax_wb_list2[wb_ind]][c ^ (c >> 1)] = get2();
10006         }
10007       }
10008       else if (tag == 0x0239) // Q-series lens info (LensInfoQ)
10009       {
10010         char LensInfo[20];
10011         fseek(ifp, 12, SEEK_CUR);
10012         stread(imgdata.lens.makernotes.Lens, 30, ifp);
10013         strcat(imgdata.lens.makernotes.Lens, " ");
10014         stread(LensInfo, 20, ifp);
10015         strcat(imgdata.lens.makernotes.Lens, LensInfo);
10016       }
10017     }
10018 
10019     else if (!strncmp(make, "SAMSUNG", 7) && (dng_writer == AdobeDNG))
10020     {
10021       if (tag == 0x0002)
10022       {
10023         if (get4() == 0x2000)
10024         {
10025           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Samsung_NX;
10026         }
10027         else if (!strncmp(model, "NX mini", 7))
10028         {
10029           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Samsung_NX_M;
10030         }
10031         else
10032         {
10033           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
10034           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
10035         }
10036       }
10037       else if (tag == 0x0003)
10038       {
10039         imgdata.lens.makernotes.CamID = unique_id = get4();
10040       }
10041       else if (tag == 0x0043)
10042       {
10043         int temp = get4();
10044         if (temp)
10045         {
10046           imgdata.other.CameraTemperature = (float)temp;
10047           if (get4() == 10)
10048             imgdata.other.CameraTemperature /= 10.0f;
10049         }
10050       }
10051       else if (tag == 0xa003)
10052       {
10053         imgdata.lens.makernotes.LensID = get2();
10054         if (imgdata.lens.makernotes.LensID)
10055           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Samsung_NX;
10056       }
10057       else if (tag == 0xa005)
10058       {
10059         stmread(imgdata.lens.InternalLensSerial, len, ifp);
10060       }
10061       else if (tag == 0xa019)
10062       {
10063         imgdata.lens.makernotes.CurAp = getreal(type);
10064       }
10065       else if (tag == 0xa01a)
10066       {
10067         imgdata.lens.makernotes.FocalLengthIn35mmFormat = get4() / 10.0f;
10068         if (imgdata.lens.makernotes.FocalLengthIn35mmFormat < 10.0f)
10069           imgdata.lens.makernotes.FocalLengthIn35mmFormat *= 10.0f;
10070       }
10071     }
10072 
10073     else if (!strncasecmp(make, "SONY", 4) || !strncasecmp(make, "Konica", 6) || !strncasecmp(make, "Minolta", 7) ||
10074              (!strncasecmp(make, "Hasselblad", 10) &&
10075               (!strncasecmp(model, "Stellar", 7) || !strncasecmp(model, "Lunar", 5) ||
10076                !strncasecmp(model, "Lusso", 5) || !strncasecmp(model, "HV", 2))))
10077     {
10078       parseSonyMakernotes(tag, type, len, AdobeDNG, table_buf_0x0116, table_buf_0x0116_len, table_buf_0x2010,
10079                           table_buf_0x2010_len, table_buf_0x9050, table_buf_0x9050_len, table_buf_0x9400,
10080                           table_buf_0x9400_len, table_buf_0x9402, table_buf_0x9402_len, table_buf_0x9403,
10081                           table_buf_0x9403_len, table_buf_0x9406, table_buf_0x9406_len, table_buf_0x940c,
10082                           table_buf_0x940c_len, table_buf_0x940e, table_buf_0x940e_len);
10083     }
10084   next:
10085     fseek(ifp, save, SEEK_SET);
10086   }
10087 quit:
10088   order = sorder;
10089 }
10090 
10091 #else
parse_makernote_0xc634(int base,int uptag,unsigned dng_writer)10092 void CLASS parse_makernote_0xc634(int base, int uptag, unsigned dng_writer)
10093 { /*placeholder */
10094 }
10095 #endif
10096 
parse_makernote(int base,int uptag)10097 void CLASS parse_makernote(int base, int uptag)
10098 {
10099   unsigned offset = 0, entries, tag, type, len, save, c;
10100   unsigned ver97 = 0, serial = 0, i, wbi = 0, wb[4] = {0, 0, 0, 0};
10101   uchar buf97[324], ci, cj, ck;
10102   short morder, sorder = order;
10103   char buf[10];
10104   unsigned SamsungKey[11];
10105   uchar NikonKey;
10106 
10107 #ifdef LIBRAW_LIBRARY_BUILD
10108   unsigned custom_serial = 0;
10109   unsigned NikonLensDataVersion = 0;
10110   unsigned lenNikonLensData = 0;
10111 
10112   unsigned NikonFlashInfoVersion = 0;
10113 
10114   uchar *CanonCameraInfo;
10115   unsigned lenCanonCameraInfo = 0;
10116   unsigned typeCanonCameraInfo = 0;
10117 
10118   uchar *table_buf;
10119   uchar *table_buf_0x0116;
10120   ushort table_buf_0x0116_len = 0;
10121   uchar *table_buf_0x2010;
10122   ushort table_buf_0x2010_len = 0;
10123   uchar *table_buf_0x9050;
10124   ushort table_buf_0x9050_len = 0;
10125   uchar *table_buf_0x9400;
10126   ushort table_buf_0x9400_len = 0;
10127   uchar *table_buf_0x9402;
10128   ushort table_buf_0x9402_len = 0;
10129   uchar *table_buf_0x9403;
10130   ushort table_buf_0x9403_len = 0;
10131   uchar *table_buf_0x9406;
10132   ushort table_buf_0x9406_len = 0;
10133   uchar *table_buf_0x940c;
10134   ushort table_buf_0x940c_len = 0;
10135   uchar *table_buf_0x940e;
10136   ushort table_buf_0x940e_len = 0;
10137 
10138   INT64 fsize = ifp->size();
10139 #endif
10140   /*
10141      The MakerNote might have its own TIFF header (possibly with
10142      its own byte-order!), or it might just be a table.
10143    */
10144   if (!strncmp(make, "Nokia", 5))
10145     return;
10146   fread(buf, 1, 10, ifp);
10147 
10148   /*
10149     printf("===>>buf: 0x");
10150     for (int i = 0; i < sizeof buf; i ++) {
10151           printf("%02x", buf[i]);
10152     }
10153     putchar('\n');
10154   */
10155 
10156   if (!strncmp(buf, "KDK", 3) || /* these aren't TIFF tables */
10157       !strncmp(buf, "VER", 3) || !strncmp(buf, "IIII", 4) || !strncmp(buf, "MMMM", 4))
10158     return;
10159   if (!strncmp(buf, "KC", 2) || /* Konica KD-400Z, KD-510Z */
10160       !strncmp(buf, "MLY", 3))
10161   { /* Minolta DiMAGE G series */
10162     order = 0x4d4d;
10163     while ((i = ftell(ifp)) < data_offset && i < 16384)
10164     {
10165       wb[0] = wb[2];
10166       wb[2] = wb[1];
10167       wb[1] = wb[3];
10168       wb[3] = get2();
10169       if (wb[1] == 256 && wb[3] == 256 && wb[0] > 256 && wb[0] < 640 && wb[2] > 256 && wb[2] < 640)
10170         FORC4 cam_mul[c] = wb[c];
10171     }
10172     goto quit;
10173   }
10174   if (!strcmp(buf, "Nikon"))
10175   {
10176     base = ftell(ifp);
10177     order = get2();
10178     if (get2() != 42)
10179       goto quit;
10180     offset = get4();
10181     fseek(ifp, offset - 8, SEEK_CUR);
10182   }
10183   else if (!strcmp(buf, "OLYMPUS") || !strcmp(buf, "PENTAX "))
10184   {
10185     base = ftell(ifp) - 10;
10186     fseek(ifp, -2, SEEK_CUR);
10187     order = get2();
10188     if (buf[0] == 'O')
10189       get2();
10190   }
10191   else if (!strncmp(buf, "SONY", 4) || !strcmp(buf, "Panasonic"))
10192   {
10193     goto nf;
10194   }
10195   else if (!strncmp(buf, "FUJIFILM", 8))
10196   {
10197     base = ftell(ifp) - 10;
10198   nf:
10199     order = 0x4949;
10200     fseek(ifp, 2, SEEK_CUR);
10201   }
10202   else if (!strcmp(buf, "OLYMP") || !strcmp(buf, "LEICA") || !strcmp(buf, "Ricoh") || !strcmp(buf, "EPSON"))
10203     fseek(ifp, -2, SEEK_CUR);
10204   else if (!strcmp(buf, "AOC") || !strcmp(buf, "QVC"))
10205     fseek(ifp, -4, SEEK_CUR);
10206   else
10207   {
10208     fseek(ifp, -10, SEEK_CUR);
10209     if (!strncmp(make, "SAMSUNG", 7))
10210       base = ftell(ifp);
10211   }
10212 
10213   // adjust pos & base for Leica M8/M9/M Mono tags and dir in tag 0x3400
10214   if (!strncasecmp(make, "LEICA", 5))
10215   {
10216     if (!strncmp(model, "M8", 2) || !strncasecmp(model, "Leica M8", 8) || !strncasecmp(model, "LEICA X", 7))
10217     {
10218       base = ftell(ifp) - 8;
10219     }
10220     else if (!strncasecmp(model, "LEICA M (Typ 240)", 17))
10221     {
10222       base = 0;
10223     }
10224     else if (!strncmp(model, "M9", 2) || !strncasecmp(model, "Leica M9", 8) || !strncasecmp(model, "M Monochrom", 11) ||
10225              !strncasecmp(model, "Leica M Monochrom", 11))
10226     {
10227       if (!uptag)
10228       {
10229         base = ftell(ifp) - 10;
10230         fseek(ifp, 8, SEEK_CUR);
10231       }
10232       else if (uptag == 0x3400)
10233       {
10234         fseek(ifp, 10, SEEK_CUR);
10235         base += 10;
10236       }
10237     }
10238     else if (!strncasecmp(model, "LEICA T", 7))
10239     {
10240       base = ftell(ifp) - 8;
10241 #ifdef LIBRAW_LIBRARY_BUILD
10242       imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Leica_T;
10243 #endif
10244     }
10245 #ifdef LIBRAW_LIBRARY_BUILD
10246     else if (!strncasecmp(model, "LEICA SL", 8))
10247     {
10248       imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Leica_SL;
10249       imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_FF;
10250     }
10251 #endif
10252   }
10253 
10254   entries = get2();
10255   if (entries > 1000)
10256     return;
10257   morder = order;
10258 
10259   while (entries--)
10260   {
10261     order = morder;
10262     tiff_get(base, &tag, &type, &len, &save);
10263     tag |= uptag << 16;
10264 
10265 #ifdef LIBRAW_LIBRARY_BUILD
10266     INT64 _pos = ftell(ifp);
10267     if (len > 8 && _pos + len > 2 * fsize)
10268     {
10269       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
10270       continue;
10271     }
10272     if (!strncasecmp(model, "KODAK P880", 10) || !strncasecmp(model, "KODAK P850", 10) ||
10273         !strncasecmp(model, "KODAK P712", 10))
10274     {
10275       if (tag == 0xf90b)
10276       {
10277         imgdata.makernotes.kodak.clipBlack = get2();
10278       }
10279       else if (tag == 0xf90c)
10280       {
10281         imgdata.makernotes.kodak.clipWhite = get2();
10282       }
10283     }
10284     if (!strncmp(make, "Canon", 5))
10285     {
10286       if (tag == 0x000d && len < 256000) // camera info
10287       {
10288         if (type != 4)
10289         {
10290           CanonCameraInfo = (uchar *)malloc(MAX(16, len));
10291           fread(CanonCameraInfo, len, 1, ifp);
10292         }
10293         else
10294         {
10295           CanonCameraInfo = (uchar *)malloc(MAX(16, len * 4));
10296           fread(CanonCameraInfo, len, 4, ifp);
10297         }
10298         lenCanonCameraInfo = len;
10299         typeCanonCameraInfo = type;
10300       }
10301 
10302       else if (tag == 0x10) // Canon ModelID
10303       {
10304         unique_id = get4();
10305         unique_id = setCanonBodyFeatures(unique_id);
10306         if (lenCanonCameraInfo)
10307         {
10308           processCanonCameraInfo(unique_id, CanonCameraInfo, lenCanonCameraInfo, typeCanonCameraInfo);
10309           free(CanonCameraInfo);
10310           CanonCameraInfo = 0;
10311           lenCanonCameraInfo = 0;
10312         }
10313       }
10314 
10315       else
10316         parseCanonMakernotes(tag, type, len);
10317     }
10318 
10319     else if (!strncmp(make, "FUJI", 4))
10320     {
10321       if (tag == 0x0010)
10322       {
10323         char FujiSerial[sizeof(imgdata.shootinginfo.InternalBodySerial)];
10324         char *words[4];
10325         char yy[2], mm[3], dd[3], ystr[16], ynum[16];
10326         int year, nwords, ynum_len;
10327         unsigned c;
10328         stmread(FujiSerial, len, ifp);
10329         nwords = getwords(FujiSerial, words, 4, sizeof(imgdata.shootinginfo.InternalBodySerial));
10330         for (int i = 0; i < nwords; i++)
10331         {
10332           mm[2] = dd[2] = 0;
10333           if (strnlen(words[i], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) < 18)
10334             if (i == 0)
10335               strncpy(imgdata.shootinginfo.InternalBodySerial, words[0],
10336                       sizeof(imgdata.shootinginfo.InternalBodySerial) - 1);
10337             else
10338             {
10339               char tbuf[sizeof(imgdata.shootinginfo.InternalBodySerial)];
10340               snprintf(tbuf, sizeof(tbuf), "%s %s", imgdata.shootinginfo.InternalBodySerial, words[i]);
10341               strncpy(imgdata.shootinginfo.InternalBodySerial, tbuf,
10342                       sizeof(imgdata.shootinginfo.InternalBodySerial) - 1);
10343             }
10344           else
10345           {
10346             strncpy(dd, words[i] + strnlen(words[i], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) - 14, 2);
10347             strncpy(mm, words[i] + strnlen(words[i], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) - 16, 2);
10348             strncpy(yy, words[i] + strnlen(words[i], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) - 18, 2);
10349             year = (yy[0] - '0') * 10 + (yy[1] - '0');
10350             if (year < 70)
10351               year += 2000;
10352             else
10353               year += 1900;
10354 
10355             ynum_len = (int)strnlen(words[i], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) - 18;
10356             strncpy(ynum, words[i], ynum_len);
10357             ynum[ynum_len] = 0;
10358             for (int j = 0; ynum[j] && ynum[j + 1] && sscanf(ynum + j, "%2x", &c); j += 2)
10359               ystr[j / 2] = c;
10360             ystr[ynum_len / 2 + 1] = 0;
10361             strcpy(model2, ystr);
10362 
10363             if (i == 0)
10364             {
10365               char tbuf[sizeof(imgdata.shootinginfo.InternalBodySerial)];
10366 
10367               if (nwords == 1)
10368                 snprintf(tbuf, sizeof(tbuf), "%s %s %d:%s:%s",
10369                          words[0] + strnlen(words[0], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) - 12, ystr,
10370                          year, mm, dd);
10371 
10372               else
10373                 snprintf(tbuf, sizeof(tbuf), "%s %d:%s:%s %s", ystr, year, mm, dd,
10374                          words[0] + strnlen(words[0], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) - 12);
10375 
10376               strncpy(imgdata.shootinginfo.InternalBodySerial, tbuf,
10377                       sizeof(imgdata.shootinginfo.InternalBodySerial) - 1);
10378             }
10379             else
10380             {
10381               char tbuf[sizeof(imgdata.shootinginfo.InternalBodySerial)];
10382               snprintf(tbuf, sizeof(tbuf), "%s %s %d:%s:%s %s", imgdata.shootinginfo.InternalBodySerial, ystr, year, mm,
10383                        dd, words[i] + strnlen(words[i], sizeof(imgdata.shootinginfo.InternalBodySerial) - 1) - 12);
10384               strncpy(imgdata.shootinginfo.InternalBodySerial, tbuf,
10385                       sizeof(imgdata.shootinginfo.InternalBodySerial) - 1);
10386             }
10387           }
10388         }
10389       }
10390       else
10391         parseFujiMakernotes(tag, type);
10392     }
10393 
10394     else if (!strncasecmp(model, "Hasselblad X1D", 14) || !strncasecmp(model, "Hasselblad H6D", 14) ||
10395              !strncasecmp(model, "Hasselblad A6D", 14))
10396     {
10397       if (tag == 0x0045)
10398       {
10399         imgdata.makernotes.hasselblad.BaseISO = get4();
10400       }
10401       else if (tag == 0x0046)
10402       {
10403         imgdata.makernotes.hasselblad.Gain = getreal(type);
10404       }
10405     }
10406 
10407     else if (!strncasecmp(make, "LEICA", 5))
10408     {
10409       if (((tag == 0x035e) || (tag == 0x035f)) && (type == 10) && (len == 9))
10410       {
10411         int ind = tag == 0x035e ? 0 : 1;
10412         for (int j = 0; j < 3; j++)
10413           FORCC imgdata.color.dng_color[ind].forwardmatrix[j][c] = getreal(type);
10414         imgdata.color.dng_color[ind].parsedfields |= LIBRAW_DNGFM_FORWARDMATRIX;
10415       }
10416 
10417       if (tag == 0x34003402)
10418         imgdata.other.CameraTemperature = getreal(type);
10419 
10420       if ((tag == 0x0320) && (type == 9) && (len == 1) && !strncasecmp(make, "Leica Camera AG", 15) &&
10421           !strncmp(buf, "LEICA", 5) && (buf[5] == 0) && (buf[6] == 0) && (buf[7] == 0))
10422         imgdata.other.CameraTemperature = getreal(type);
10423 
10424       if ((tag == 0x0303) && (type != 4))
10425       {
10426         stmread(imgdata.lens.makernotes.Lens, len, ifp);
10427       }
10428 
10429       if ((tag == 0x3405) || (tag == 0x0310) || (tag == 0x34003405))
10430       {
10431         imgdata.lens.makernotes.LensID = get4();
10432         imgdata.lens.makernotes.LensID =
10433             ((imgdata.lens.makernotes.LensID >> 2) << 8) | (imgdata.lens.makernotes.LensID & 0x3);
10434         if (imgdata.lens.makernotes.LensID != -1)
10435         {
10436           if ((model[0] == 'M') || !strncasecmp(model, "LEICA M", 7))
10437           {
10438             imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Leica_M;
10439             if (imgdata.lens.makernotes.LensID)
10440               imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Leica_M;
10441           }
10442           else if ((model[0] == 'S') || !strncasecmp(model, "LEICA S", 7))
10443           {
10444             imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Leica_S;
10445             if (imgdata.lens.makernotes.Lens[0])
10446               imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Leica_S;
10447           }
10448         }
10449       }
10450 
10451       else if (((tag == 0x0313) || (tag == 0x34003406)) && (fabs(imgdata.lens.makernotes.CurAp) < 0.17f) &&
10452                ((type == 10) || (type == 5)))
10453       {
10454         imgdata.lens.makernotes.CurAp = getreal(type);
10455         if (imgdata.lens.makernotes.CurAp > 126.3)
10456           imgdata.lens.makernotes.CurAp = 0.0f;
10457       }
10458 
10459       else if (tag == 0x3400)
10460       {
10461         parse_makernote(base, 0x3400);
10462       }
10463     }
10464 
10465     else if (!strncmp(make, "NIKON", 5))
10466     {
10467       if (tag == 0x000a)
10468       {
10469         imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
10470         imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
10471       }
10472       else if (tag == 0x0012)
10473       {
10474         char a, b, c;
10475         a = fgetc(ifp);
10476         b = fgetc(ifp);
10477         c = fgetc(ifp);
10478         if (c)
10479           imgdata.other.FlashEC = (float)(a * b) / (float)c;
10480       }
10481       else if (tag == 0x003b) // all 1s for regular exposures
10482       {
10483         imgdata.makernotes.nikon.ME_WB[0] = getreal(type);
10484         imgdata.makernotes.nikon.ME_WB[2] = getreal(type);
10485         imgdata.makernotes.nikon.ME_WB[1] = getreal(type);
10486         imgdata.makernotes.nikon.ME_WB[3] = getreal(type);
10487       }
10488       else if (tag == 0x0045)
10489       {
10490         imgdata.sizes.raw_crop.cleft = get2();
10491         imgdata.sizes.raw_crop.ctop = get2();
10492         imgdata.sizes.raw_crop.cwidth = get2();
10493         imgdata.sizes.raw_crop.cheight = get2();
10494       }
10495       else if (tag == 0x0082) // lens attachment
10496       {
10497         stmread(imgdata.lens.makernotes.Attachment, len, ifp);
10498       }
10499       else if (tag == 0x0083) // lens type
10500       {
10501         imgdata.lens.nikon.NikonLensType = fgetc(ifp);
10502       }
10503       else if (tag == 0x0084) // lens
10504       {
10505         imgdata.lens.makernotes.MinFocal = getreal(type);
10506         imgdata.lens.makernotes.MaxFocal = getreal(type);
10507         imgdata.lens.makernotes.MaxAp4MinFocal = getreal(type);
10508         imgdata.lens.makernotes.MaxAp4MaxFocal = getreal(type);
10509       }
10510       else if (tag == 0x008b) // lens f-stops
10511       {
10512         uchar a, b, c;
10513         a = fgetc(ifp);
10514         b = fgetc(ifp);
10515         c = fgetc(ifp);
10516         if (c)
10517         {
10518           imgdata.lens.nikon.NikonLensFStops = a * b * (12 / c);
10519           imgdata.lens.makernotes.LensFStops = (float)imgdata.lens.nikon.NikonLensFStops / 12.0f;
10520         }
10521       }
10522       else if (tag == 0x0093) // Nikon compression
10523       {
10524         imgdata.makernotes.nikon.NEFCompression = i = get2();
10525         if ((i == 7) || (i == 9))
10526         {
10527           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
10528           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
10529         }
10530       }
10531       else if (tag == 0x0098) // contains lens data
10532       {
10533         for (i = 0; i < 4; i++)
10534         {
10535           NikonLensDataVersion = NikonLensDataVersion * 10 + fgetc(ifp) - '0';
10536         }
10537         switch (NikonLensDataVersion)
10538         {
10539         case 100:
10540           lenNikonLensData = 9;
10541           break;
10542         case 101:
10543         case 201: // encrypted, starting from v.201
10544         case 202:
10545         case 203:
10546           lenNikonLensData = 15;
10547           break;
10548         case 204:
10549           lenNikonLensData = 16;
10550           break;
10551         case 400:
10552           lenNikonLensData = 459;
10553           break;
10554         case 401:
10555           lenNikonLensData = 590;
10556           break;
10557         case 402:
10558           lenNikonLensData = 509;
10559           break;
10560         case 403:
10561           lenNikonLensData = 879;
10562           break;
10563         }
10564         if (lenNikonLensData > 0)
10565         {
10566           table_buf = (uchar *)malloc(lenNikonLensData);
10567           fread(table_buf, lenNikonLensData, 1, ifp);
10568           if ((NikonLensDataVersion < 201) && lenNikonLensData)
10569           {
10570             processNikonLensData(table_buf, lenNikonLensData);
10571             free(table_buf);
10572             lenNikonLensData = 0;
10573           }
10574         }
10575       }
10576       else if (tag == 0x00a0)
10577       {
10578         stmread(imgdata.shootinginfo.BodySerial, len, ifp);
10579       }
10580       else if (tag == 0x00a8) // contains flash data
10581       {
10582         for (i = 0; i < 4; i++)
10583         {
10584           NikonFlashInfoVersion = NikonFlashInfoVersion * 10 + fgetc(ifp) - '0';
10585         }
10586       }
10587       else if (tag == 0x00b0)
10588       {
10589         get4(); // ME tag version, 4 symbols
10590         imgdata.makernotes.nikon.ExposureMode = get4();
10591         imgdata.makernotes.nikon.nMEshots = get4();
10592         imgdata.makernotes.nikon.MEgainOn = get4();
10593       }
10594       else if (tag == 0x00b9)
10595       {
10596         uchar uc;
10597         int8_t sc;
10598         fread(&uc, 1, 1, ifp);
10599         imgdata.makernotes.nikon.AFFineTune = uc;
10600         fread(&uc, 1, 1, ifp);
10601         imgdata.makernotes.nikon.AFFineTuneIndex = uc;
10602         fread(&sc, 1, 1, ifp);
10603         imgdata.makernotes.nikon.AFFineTuneAdj = sc;
10604       }
10605     }
10606 
10607     else if (!strncmp(make, "OLYMPUS", 7))
10608     {
10609       switch (tag)
10610       {
10611       case 0x0404:
10612       case 0x101a:
10613       case 0x20100101:
10614         if (!imgdata.shootinginfo.BodySerial[0])
10615           stmread(imgdata.shootinginfo.BodySerial, len, ifp);
10616         break;
10617       case 0x20100102:
10618         if (!imgdata.shootinginfo.InternalBodySerial[0])
10619           stmread(imgdata.shootinginfo.InternalBodySerial, len, ifp);
10620         break;
10621       case 0x0207:
10622       case 0x20100100:
10623       {
10624         uchar sOlyID[8];
10625         fread(sOlyID, MIN(len, 7), 1, ifp);
10626         sOlyID[7] = 0;
10627         OlyID = sOlyID[0];
10628         i = 1;
10629         while (i < 7 && sOlyID[i])
10630         {
10631           OlyID = OlyID << 8 | sOlyID[i];
10632           i++;
10633         }
10634         setOlympusBodyFeatures(OlyID);
10635       }
10636       break;
10637       case 0x1002:
10638         imgdata.lens.makernotes.CurAp = libraw_powf64l(2.0f, getreal(type) / 2);
10639         break;
10640       case 0x20400612:
10641       case 0x30000612:
10642         imgdata.sizes.raw_crop.cleft = get2();
10643         break;
10644       case 0x20400613:
10645       case 0x30000613:
10646         imgdata.sizes.raw_crop.ctop = get2();
10647         break;
10648       case 0x20400614:
10649       case 0x30000614:
10650         imgdata.sizes.raw_crop.cwidth = get2();
10651         break;
10652       case 0x20400615:
10653       case 0x30000615:
10654         imgdata.sizes.raw_crop.cheight = get2();
10655         break;
10656       case 0x20401112:
10657         imgdata.makernotes.olympus.OlympusCropID = get2();
10658         break;
10659       case 0x20401113:
10660         FORC4 imgdata.makernotes.olympus.OlympusFrame[c] = get2();
10661         break;
10662       case 0x20100201:
10663       {
10664         unsigned long long oly_lensid[3];
10665         oly_lensid[0] = fgetc(ifp);
10666         fgetc(ifp);
10667         oly_lensid[1] = fgetc(ifp);
10668         oly_lensid[2] = fgetc(ifp);
10669         imgdata.lens.makernotes.LensID = (oly_lensid[0] << 16) | (oly_lensid[1] << 8) | oly_lensid[2];
10670       }
10671         imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FT;
10672         imgdata.lens.makernotes.LensFormat = LIBRAW_FORMAT_FT;
10673         if (((imgdata.lens.makernotes.LensID < 0x20000) || (imgdata.lens.makernotes.LensID > 0x4ffff)) &&
10674             (imgdata.lens.makernotes.LensID & 0x10))
10675         {
10676           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_mFT;
10677         }
10678         break;
10679       case 0x20100202:
10680         stmread(imgdata.lens.LensSerial, len, ifp);
10681         break;
10682       case 0x20100203:
10683         stmread(imgdata.lens.makernotes.Lens, len, ifp);
10684         break;
10685       case 0x20100205:
10686         imgdata.lens.makernotes.MaxAp4MinFocal = libraw_powf64l(sqrt(2.0f), get2() / 256.0f);
10687         break;
10688       case 0x20100206:
10689         imgdata.lens.makernotes.MaxAp4MaxFocal = libraw_powf64l(sqrt(2.0f), get2() / 256.0f);
10690         break;
10691       case 0x20100207:
10692         imgdata.lens.makernotes.MinFocal = (float)get2();
10693         break;
10694       case 0x20100208:
10695         imgdata.lens.makernotes.MaxFocal = (float)get2();
10696         if (imgdata.lens.makernotes.MaxFocal > 1000.0f)
10697           imgdata.lens.makernotes.MaxFocal = imgdata.lens.makernotes.MinFocal;
10698         break;
10699       case 0x2010020a:
10700         imgdata.lens.makernotes.MaxAp4CurFocal = libraw_powf64l(sqrt(2.0f), get2() / 256.0f);
10701         break;
10702       case 0x20100301:
10703         imgdata.lens.makernotes.TeleconverterID = fgetc(ifp) << 8;
10704         fgetc(ifp);
10705         imgdata.lens.makernotes.TeleconverterID = imgdata.lens.makernotes.TeleconverterID | fgetc(ifp);
10706         break;
10707       case 0x20100303:
10708         stmread(imgdata.lens.makernotes.Teleconverter, len, ifp);
10709         break;
10710       case 0x20100403:
10711         stmread(imgdata.lens.makernotes.Attachment, len, ifp);
10712         break;
10713       case 0x1007:
10714         imgdata.other.SensorTemperature = (float)get2();
10715         break;
10716       case 0x1008:
10717         imgdata.other.LensTemperature = (float)get2();
10718         break;
10719       case 0x20401306:
10720       {
10721         int temp = get2();
10722         if ((temp != 0) && (temp != 100))
10723         {
10724           if (temp < 61)
10725             imgdata.other.CameraTemperature = (float)temp;
10726           else
10727             imgdata.other.CameraTemperature = (float)(temp - 32) / 1.8f;
10728           if ((OlyID == 0x4434353933ULL) && // TG-5
10729               (imgdata.other.exifAmbientTemperature > -273.15f))
10730             imgdata.other.CameraTemperature += imgdata.other.exifAmbientTemperature;
10731         }
10732       }
10733       break;
10734       case 0x20501500:
10735         if (OlyID != 0x0ULL)
10736         {
10737           short temp = get2();
10738           if ((OlyID == 0x4434303430ULL) || // E-1
10739               (OlyID == 0x5330303336ULL) || // E-M5
10740               (len != 1))
10741             imgdata.other.SensorTemperature = (float)temp;
10742           else if ((temp != -32768) && (temp != 0))
10743           {
10744             if (temp > 199)
10745               imgdata.other.SensorTemperature = 86.474958f - 0.120228f * (float)temp;
10746             else
10747               imgdata.other.SensorTemperature = (float)temp;
10748           }
10749         }
10750         break;
10751       }
10752     }
10753 
10754     else if ((!strncmp(make, "PENTAX", 6) || !strncmp(make, "RICOH", 5)) && !strncmp(model, "GR", 2))
10755     {
10756       if (tag == 0x0005)
10757       {
10758         char buffer[17];
10759         int count = 0;
10760         fread(buffer, 16, 1, ifp);
10761         buffer[16] = 0;
10762         for (int i = 0; i < 16; i++)
10763         {
10764           //    	        sprintf(imgdata.shootinginfo.InternalBodySerial+2*i, "%02x", buffer[i]);
10765           if ((isspace(buffer[i])) || (buffer[i] == 0x2D) || (isalnum(buffer[i])))
10766             count++;
10767         }
10768         if (count == 16)
10769         {
10770           sprintf(imgdata.shootinginfo.BodySerial, "%8s", buffer + 8);
10771           buffer[8] = 0;
10772           sprintf(imgdata.shootinginfo.InternalBodySerial, "%8s", buffer);
10773         }
10774         else
10775         {
10776           sprintf(imgdata.shootinginfo.BodySerial, "%02x%02x%02x%02x", buffer[4], buffer[5], buffer[6], buffer[7]);
10777           sprintf(imgdata.shootinginfo.InternalBodySerial, "%02x%02x%02x%02x", buffer[8], buffer[9], buffer[10],
10778                   buffer[11]);
10779         }
10780       }
10781       else if ((tag == 0x1001) && (type == 3))
10782       {
10783         imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
10784         imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
10785         imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_APSC;
10786         imgdata.lens.makernotes.LensID = -1;
10787         imgdata.lens.makernotes.FocalType = 1;
10788       }
10789 
10790       else if ((tag == 0x100b) && (type == 10))
10791       {
10792         imgdata.other.FlashEC = getreal(type);
10793       }
10794 
10795       else if ((tag == 0x1017) && (get2() == 2))
10796       {
10797         strcpy(imgdata.lens.makernotes.Attachment, "Wide-Angle Adapter");
10798       }
10799       else if (tag == 0x1500)
10800       {
10801         imgdata.lens.makernotes.CurFocal = getreal(type);
10802       }
10803     }
10804 
10805     else if (!strncmp(make, "RICOH", 5) && strncmp(model, "PENTAX", 6))
10806     {
10807       if ((tag == 0x0005) && !strncmp(model, "GXR", 3))
10808       {
10809         char buffer[9];
10810         buffer[8] = 0;
10811         fread(buffer, 8, 1, ifp);
10812         sprintf(imgdata.shootinginfo.InternalBodySerial, "%8s", buffer);
10813       }
10814 
10815       else if ((tag == 0x100b) && (type == 10))
10816       {
10817         imgdata.other.FlashEC = getreal(type);
10818       }
10819 
10820       else if ((tag == 0x1017) && (get2() == 2))
10821       {
10822         strcpy(imgdata.lens.makernotes.Attachment, "Wide-Angle Adapter");
10823       }
10824 
10825       else if (tag == 0x1500)
10826       {
10827         imgdata.lens.makernotes.CurFocal = getreal(type);
10828       }
10829 
10830       else if ((tag == 0x2001) && !strncmp(model, "GXR", 3))
10831       {
10832         short ntags, cur_tag;
10833         fseek(ifp, 20, SEEK_CUR);
10834         ntags = get2();
10835         cur_tag = get2();
10836         while (cur_tag != 0x002c)
10837         {
10838           fseek(ifp, 10, SEEK_CUR);
10839           cur_tag = get2();
10840         }
10841         fseek(ifp, 6, SEEK_CUR);
10842         fseek(ifp, get4() + 20, SEEK_SET);
10843         stread(imgdata.shootinginfo.BodySerial, 12, ifp);
10844         get2();
10845         imgdata.lens.makernotes.LensID = getc(ifp) - '0';
10846         switch (imgdata.lens.makernotes.LensID)
10847         {
10848         case 1:
10849         case 2:
10850         case 3:
10851         case 5:
10852         case 6:
10853           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
10854           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_RicohModule;
10855           break;
10856         case 8:
10857           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Leica_M;
10858           imgdata.lens.makernotes.CameraFormat = LIBRAW_FORMAT_APSC;
10859           imgdata.lens.makernotes.LensID = -1;
10860           break;
10861         default:
10862           imgdata.lens.makernotes.LensID = -1;
10863         }
10864         fseek(ifp, 17, SEEK_CUR);
10865         stread(imgdata.lens.LensSerial, 12, ifp);
10866       }
10867     }
10868 
10869     else if ((!strncmp(make, "PENTAX", 6) || !strncmp(model, "PENTAX", 6) ||
10870               (!strncmp(make, "SAMSUNG", 7) && dng_version)) &&
10871              strncmp(model, "GR", 2))
10872     {
10873       if (tag == 0x0005)
10874       {
10875         unique_id = get4();
10876         setPentaxBodyFeatures(unique_id);
10877       }
10878       else if (tag == 0x000d)
10879       {
10880         imgdata.makernotes.pentax.FocusMode = get2();
10881       }
10882       else if (tag == 0x000e)
10883       {
10884         imgdata.makernotes.pentax.AFPointSelected = get2();
10885       }
10886       else if (tag == 0x000f)
10887       {
10888         imgdata.makernotes.pentax.AFPointsInFocus = getint(type);
10889       }
10890       else if (tag == 0x0010)
10891       {
10892         imgdata.makernotes.pentax.FocusPosition = get2();
10893       }
10894       else if (tag == 0x0013)
10895       {
10896         imgdata.lens.makernotes.CurAp = (float)get2() / 10.0f;
10897       }
10898       else if (tag == 0x0014)
10899       {
10900         PentaxISO(get2());
10901       }
10902       else if (tag == 0x001d)
10903       {
10904         imgdata.lens.makernotes.CurFocal = (float)get4() / 100.0f;
10905       }
10906       else if (tag == 0x0034)
10907       {
10908         uchar uc;
10909         FORC4
10910         {
10911           fread(&uc, 1, 1, ifp);
10912           imgdata.makernotes.pentax.DriveMode[c] = uc;
10913         }
10914       }
10915       else if (tag == 0x0038)
10916       {
10917         imgdata.sizes.raw_crop.cleft = get2();
10918         imgdata.sizes.raw_crop.ctop = get2();
10919       }
10920       else if (tag == 0x0039)
10921       {
10922         imgdata.sizes.raw_crop.cwidth = get2();
10923         imgdata.sizes.raw_crop.cheight = get2();
10924       }
10925       else if (tag == 0x003f)
10926       {
10927         imgdata.lens.makernotes.LensID = fgetc(ifp) << 8 | fgetc(ifp);
10928       }
10929       else if (tag == 0x0047)
10930       {
10931         imgdata.other.CameraTemperature = (float)fgetc(ifp);
10932       }
10933       else if (tag == 0x004d)
10934       {
10935         if (type == 9)
10936           imgdata.other.FlashEC = getreal(type) / 256.0f;
10937         else
10938           imgdata.other.FlashEC = (float)((signed short)fgetc(ifp)) / 6.0f;
10939       }
10940       else if (tag == 0x0072)
10941       {
10942         imgdata.makernotes.pentax.AFAdjustment = get2();
10943       }
10944       else if (tag == 0x007e)
10945       {
10946         imgdata.color.linear_max[0] = imgdata.color.linear_max[1] = imgdata.color.linear_max[2] =
10947             imgdata.color.linear_max[3] = (long)(-1) * get4();
10948       }
10949       else if (tag == 0x0207)
10950       {
10951         if (len < 65535) // Safety belt
10952           PentaxLensInfo(imgdata.lens.makernotes.CamID, len);
10953       }
10954       else if ((tag >= 0x020d) && (tag <= 0x0214))
10955       {
10956         FORC4 imgdata.color.WB_Coeffs[Pentax_wb_list1[tag - 0x020d]][c ^ (c >> 1)] = get2();
10957       }
10958       else if (tag == 0x0221)
10959       {
10960         int nWB = get2();
10961         if (nWB <= sizeof(imgdata.color.WBCT_Coeffs) / sizeof(imgdata.color.WBCT_Coeffs[0]))
10962           for (int i = 0; i < nWB; i++)
10963           {
10964             imgdata.color.WBCT_Coeffs[i][0] = (unsigned)0xcfc6 - get2();
10965             fseek(ifp, 2, SEEK_CUR);
10966             imgdata.color.WBCT_Coeffs[i][1] = get2();
10967             imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = 0x2000;
10968             imgdata.color.WBCT_Coeffs[i][3] = get2();
10969           }
10970       }
10971       else if (tag == 0x0215)
10972       {
10973         fseek(ifp, 16, SEEK_CUR);
10974         sprintf(imgdata.shootinginfo.InternalBodySerial, "%d", get4());
10975       }
10976       else if (tag == 0x0229)
10977       {
10978         stmread(imgdata.shootinginfo.BodySerial, len, ifp);
10979       }
10980       else if (tag == 0x022d)
10981       {
10982         int wb_ind;
10983         getc(ifp);
10984         for (int wb_cnt = 0; wb_cnt < nPentax_wb_list2; wb_cnt++)
10985         {
10986           wb_ind = getc(ifp);
10987           if (wb_ind < nPentax_wb_list2)
10988             FORC4 imgdata.color.WB_Coeffs[Pentax_wb_list2[wb_ind]][c ^ (c >> 1)] = get2();
10989         }
10990       }
10991       else if (tag == 0x0239) // Q-series lens info (LensInfoQ)
10992       {
10993         char LensInfo[20];
10994         fseek(ifp, 2, SEEK_CUR);
10995         stread(imgdata.lens.makernotes.Lens, 30, ifp);
10996         strcat(imgdata.lens.makernotes.Lens, " ");
10997         stread(LensInfo, 20, ifp);
10998         strcat(imgdata.lens.makernotes.Lens, LensInfo);
10999       }
11000     }
11001 
11002     else if (!strncmp(make, "SAMSUNG", 7))
11003     {
11004       if (tag == 0x0002)
11005       {
11006         if (get4() == 0x2000)
11007         {
11008           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Samsung_NX;
11009         }
11010         else if (!strncmp(model, "NX mini", 7))
11011         {
11012           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Samsung_NX_M;
11013         }
11014         else
11015         {
11016           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
11017           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
11018         }
11019       }
11020       else if (tag == 0x0003)
11021       {
11022         unique_id = imgdata.lens.makernotes.CamID = get4();
11023       }
11024       else if (tag == 0x0043)
11025       {
11026         int temp = get4();
11027         if (temp)
11028         {
11029           imgdata.other.CameraTemperature = (float)temp;
11030           if (get4() == 10)
11031             imgdata.other.CameraTemperature /= 10.0f;
11032         }
11033       }
11034       else if (tag == 0xa002)
11035       {
11036         stmread(imgdata.shootinginfo.BodySerial, len, ifp);
11037       }
11038       else if (tag == 0xa003)
11039       {
11040         imgdata.lens.makernotes.LensID = get2();
11041         if (imgdata.lens.makernotes.LensID)
11042           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Samsung_NX;
11043       }
11044       else if (tag == 0xa005)
11045       {
11046         stmread(imgdata.lens.InternalLensSerial, len, ifp);
11047       }
11048       else if (tag == 0xa019)
11049       {
11050         imgdata.lens.makernotes.CurAp = getreal(type);
11051       }
11052       else if (tag == 0xa01a)
11053       {
11054         imgdata.lens.makernotes.FocalLengthIn35mmFormat = get4() / 10.0f;
11055         if (imgdata.lens.makernotes.FocalLengthIn35mmFormat < 10.0f)
11056           imgdata.lens.makernotes.FocalLengthIn35mmFormat *= 10.0f;
11057       }
11058     }
11059 
11060     else if (!strncasecmp(make, "SONY", 4) || !strncasecmp(make, "Konica", 6) || !strncasecmp(make, "Minolta", 7) ||
11061              (!strncasecmp(make, "Hasselblad", 10) &&
11062               (!strncasecmp(model, "Stellar", 7) || !strncasecmp(model, "Lunar", 5) ||
11063                !strncasecmp(model, "Lusso", 5) || !strncasecmp(model, "HV", 2))))
11064     {
11065       parseSonyMakernotes(tag, type, len, nonDNG, table_buf_0x0116, table_buf_0x0116_len, table_buf_0x2010,
11066                           table_buf_0x2010_len, table_buf_0x9050, table_buf_0x9050_len, table_buf_0x9400,
11067                           table_buf_0x9400_len, table_buf_0x9402, table_buf_0x9402_len, table_buf_0x9403,
11068                           table_buf_0x9403_len, table_buf_0x9406, table_buf_0x9406_len, table_buf_0x940c,
11069                           table_buf_0x940c_len, table_buf_0x940e, table_buf_0x940e_len);
11070     }
11071 
11072     fseek(ifp, _pos, SEEK_SET);
11073 #endif
11074 
11075     if (tag == 2 && strstr(make, "NIKON") && !iso_speed)
11076       iso_speed = (get2(), get2());
11077     if (tag == 37 && strstr(make, "NIKON") && (!iso_speed || iso_speed == 65535))
11078     {
11079       unsigned char cc;
11080       fread(&cc, 1, 1, ifp);
11081       iso_speed = int(100.0 * libraw_powf64l(2.0f, float(cc) / 12.0 - 5.0));
11082     }
11083     if (tag == 4 && len > 26 && len < 35)
11084     {
11085       if ((i = (get4(), get2())) != 0x7fff && (!iso_speed || iso_speed == 65535))
11086         iso_speed = 50 * libraw_powf64l(2.0, i / 32.0 - 4);
11087 #ifdef LIBRAW_LIBRARY_BUILD
11088       get4();
11089 #else
11090       if ((i = (get2(), get2())) != 0x7fff && !aperture)
11091         aperture = libraw_powf64l(2.0, i / 64.0);
11092 #endif
11093       if ((i = get2()) != 0xffff && !shutter)
11094         shutter = libraw_powf64l(2.0, (short)i / -32.0);
11095       wbi = (get2(), get2());
11096       shot_order = (get2(), get2());
11097     }
11098     if ((tag == 4 || tag == 0x114) && !strncmp(make, "KONICA", 6))
11099     {
11100       fseek(ifp, tag == 4 ? 140 : 160, SEEK_CUR);
11101       switch (get2())
11102       {
11103       case 72:
11104         flip = 0;
11105         break;
11106       case 76:
11107         flip = 6;
11108         break;
11109       case 82:
11110         flip = 5;
11111         break;
11112       }
11113     }
11114     if (tag == 7 && type == 2 && len > 20)
11115       fgets(model2, 64, ifp);
11116     if (tag == 8 && type == 4)
11117       shot_order = get4();
11118     if (tag == 9 && !strncmp(make, "Canon", 5))
11119       fread(artist, 64, 1, ifp);
11120     if (tag == 0xc && len == 4)
11121       FORC3 cam_mul[(c << 1 | c >> 1) & 3] = getreal(type);
11122     if (tag == 0xd && type == 7 && get2() == 0xaaaa)
11123     {
11124 #if 0 /* Canon rotation data is handled by EXIF.Orientation */
11125       for (c = i = 2; (ushort)c != 0xbbbb && i < len; i++)
11126         c = c << 8 | fgetc(ifp);
11127       while ((i += 4) < len - 5)
11128         if (get4() == 257 && (i = len) && (c = (get4(), fgetc(ifp))) < 3)
11129           flip = "065"[c] - '0';
11130 #endif
11131     }
11132 
11133 #ifndef LIBRAW_LIBRARY_BUILD
11134     if (tag == 0x10 && type == 4)
11135       unique_id = get4();
11136 #endif
11137 
11138 #ifdef LIBRAW_LIBRARY_BUILD
11139     INT64 _pos2 = ftell(ifp);
11140     if (!strncasecmp(make, "Olympus", 7))
11141     {
11142       short nWB, tWB;
11143       if ((tag == 0x20300108) || (tag == 0x20310109))
11144         imgdata.makernotes.olympus.ColorSpace = get2();
11145 
11146       if ((tag == 0x20400101) && (len == 2) && (!strncasecmp(model, "E-410", 5) || !strncasecmp(model, "E-510", 5)))
11147       {
11148         int i;
11149         for (i = 0; i < 64; i++)
11150           imgdata.color.WBCT_Coeffs[i][2] = imgdata.color.WBCT_Coeffs[i][4] = imgdata.color.WB_Coeffs[i][1] =
11151               imgdata.color.WB_Coeffs[i][3] = 0x100;
11152         for (i = 64; i < 256; i++)
11153           imgdata.color.WB_Coeffs[i][1] = imgdata.color.WB_Coeffs[i][3] = 0x100;
11154       }
11155       if ((tag >= 0x20400101) && (tag <= 0x20400111))
11156       {
11157         nWB = tag - 0x20400101;
11158         tWB = Oly_wb_list2[nWB << 1];
11159         ushort CT = Oly_wb_list2[(nWB << 1) | 1];
11160         int wb[4];
11161         wb[0] = get2();
11162         wb[2] = get2();
11163         if (tWB != 0x100)
11164         {
11165           imgdata.color.WB_Coeffs[tWB][0] = wb[0];
11166           imgdata.color.WB_Coeffs[tWB][2] = wb[2];
11167         }
11168         if (CT)
11169         {
11170           imgdata.color.WBCT_Coeffs[nWB - 1][0] = CT;
11171           imgdata.color.WBCT_Coeffs[nWB - 1][1] = wb[0];
11172           imgdata.color.WBCT_Coeffs[nWB - 1][3] = wb[2];
11173         }
11174         if (len == 4)
11175         {
11176           wb[1] = get2();
11177           wb[3] = get2();
11178           if (tWB != 0x100)
11179           {
11180             imgdata.color.WB_Coeffs[tWB][1] = wb[1];
11181             imgdata.color.WB_Coeffs[tWB][3] = wb[3];
11182           }
11183           if (CT)
11184           {
11185             imgdata.color.WBCT_Coeffs[nWB - 1][2] = wb[1];
11186             imgdata.color.WBCT_Coeffs[nWB - 1][4] = wb[3];
11187           }
11188         }
11189       }
11190       if ((tag >= 0x20400112) && (tag <= 0x2040011e))
11191       {
11192         nWB = tag - 0x20400112;
11193         int wbG = get2();
11194         tWB = Oly_wb_list2[nWB << 1];
11195         if (nWB)
11196           imgdata.color.WBCT_Coeffs[nWB - 1][2] = imgdata.color.WBCT_Coeffs[nWB - 1][4] = wbG;
11197         if (tWB != 0x100)
11198           imgdata.color.WB_Coeffs[tWB][1] = imgdata.color.WB_Coeffs[tWB][3] = wbG;
11199       }
11200 
11201       if (tag == 0x20400121)
11202       {
11203         imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][0] = get2();
11204         imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][2] = get2();
11205         if (len == 4)
11206         {
11207           imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][1] = get2();
11208           imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][3] = get2();
11209         }
11210       }
11211       if (tag == 0x2040011f)
11212       {
11213         int wbG = get2();
11214         if (imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][0])
11215           imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][3] = wbG;
11216         FORC4 if (imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1 + c][0])
11217             imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1 + c][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1 + c][3] =
11218             wbG;
11219       }
11220       if ((tag == 0x30000110) && strcmp(software, "v757-71"))
11221       {
11222         imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][0] = get2();
11223         imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][2] = get2();
11224         if (len == 2)
11225         {
11226           for (int i = 0; i < 256; i++)
11227             imgdata.color.WB_Coeffs[i][1] = imgdata.color.WB_Coeffs[i][3] = 0x100;
11228         }
11229       }
11230       if ((((tag >= 0x30000120) && (tag <= 0x30000124)) || ((tag >= 0x30000130) && (tag <= 0x30000133))) &&
11231           strcmp(software, "v757-71"))
11232       {
11233         int wb_ind;
11234         if (tag <= 0x30000124)
11235           wb_ind = tag - 0x30000120;
11236         else
11237           wb_ind = tag - 0x30000130 + 5;
11238         imgdata.color.WB_Coeffs[Oly_wb_list1[wb_ind]][0] = get2();
11239         imgdata.color.WB_Coeffs[Oly_wb_list1[wb_ind]][2] = get2();
11240       }
11241 
11242       if ((tag == 0x20400805) && (len == 2))
11243       {
11244         imgdata.makernotes.olympus.OlympusSensorCalibration[0] = getreal(type);
11245         imgdata.makernotes.olympus.OlympusSensorCalibration[1] = getreal(type);
11246         FORC4 imgdata.color.linear_max[c] = imgdata.makernotes.olympus.OlympusSensorCalibration[0];
11247       }
11248       if (tag == 0x20200306)
11249       {
11250         uchar uc;
11251         fread(&uc, 1, 1, ifp);
11252         imgdata.makernotes.olympus.AFFineTune = uc;
11253       }
11254       if (tag == 0x20200307)
11255       {
11256         FORC3 imgdata.makernotes.olympus.AFFineTuneAdj[c] = get2();
11257       }
11258       if (tag == 0x20200401)
11259       {
11260         imgdata.other.FlashEC = getreal(type);
11261       }
11262     }
11263     fseek(ifp, _pos2, SEEK_SET);
11264 
11265 #endif
11266     if (tag == 0x11 && is_raw && !strncmp(make, "NIKON", 5))
11267     {
11268       fseek(ifp, get4() + base, SEEK_SET);
11269       parse_tiff_ifd(base);
11270     }
11271     if (tag == 0x14 && type == 7)
11272     {
11273       if (len == 2560)
11274       {
11275         fseek(ifp, 1248, SEEK_CUR);
11276         goto get2_256;
11277       }
11278       fread(buf, 1, 10, ifp);
11279       if (!strncmp(buf, "NRW ", 4))
11280       {
11281         fseek(ifp, strcmp(buf + 4, "0100") ? 46 : 1546, SEEK_CUR);
11282         cam_mul[0] = get4() << 2;
11283         cam_mul[1] = get4() + get4();
11284         cam_mul[2] = get4() << 2;
11285       }
11286     }
11287     if (tag == 0x15 && type == 2 && is_raw)
11288       fread(model, 64, 1, ifp);
11289     if (strstr(make, "PENTAX"))
11290     {
11291       if (tag == 0x1b)
11292         tag = 0x1018;
11293       if (tag == 0x1c)
11294         tag = 0x1017;
11295     }
11296     if (tag == 0x1d)
11297     {
11298       while ((c = fgetc(ifp)) && c != EOF)
11299 #ifdef LIBRAW_LIBRARY_BUILD
11300       {
11301         if ((!custom_serial) && (!isdigit(c)))
11302         {
11303           if ((strbuflen(model) == 3) && (!strcmp(model, "D50")))
11304           {
11305             custom_serial = 34;
11306           }
11307           else
11308           {
11309             custom_serial = 96;
11310           }
11311         }
11312 #endif
11313         serial = serial * 10 + (isdigit(c) ? c - '0' : c % 10);
11314 #ifdef LIBRAW_LIBRARY_BUILD
11315       }
11316       if (!imgdata.shootinginfo.BodySerial[0])
11317         sprintf(imgdata.shootinginfo.BodySerial, "%d", serial);
11318 #endif
11319     }
11320     if (tag == 0x29 && type == 1)
11321     { // Canon PowerShot G9
11322       c = wbi < 18 ? "012347800000005896"[wbi] - '0' : 0;
11323       fseek(ifp, 8 + c * 32, SEEK_CUR);
11324       FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get4();
11325     }
11326 #ifndef LIBRAW_LIBRARY_BUILD
11327     if (tag == 0x3d && type == 3 && len == 4)
11328       FORC4 cblack[c ^ c >> 1] = get2() >> (14 - tiff_bps);
11329 #endif
11330     if (tag == 0x81 && type == 4)
11331     {
11332       data_offset = get4();
11333       fseek(ifp, data_offset + 41, SEEK_SET);
11334       raw_height = get2() * 2;
11335       raw_width = get2();
11336       filters = 0x61616161;
11337     }
11338     if ((tag == 0x81 && type == 7) || (tag == 0x100 && type == 7) || (tag == 0x280 && type == 1))
11339     {
11340       thumb_offset = ftell(ifp);
11341       thumb_length = len;
11342     }
11343     if (tag == 0x88 && type == 4 && (thumb_offset = get4()))
11344       thumb_offset += base;
11345     if (tag == 0x89 && type == 4)
11346       thumb_length = get4();
11347     if (tag == 0x8c || tag == 0x96)
11348       meta_offset = ftell(ifp);
11349     if (tag == 0x97)
11350     {
11351       for (i = 0; i < 4; i++)
11352         ver97 = ver97 * 10 + fgetc(ifp) - '0';
11353       switch (ver97)
11354       {
11355       case 100:
11356         fseek(ifp, 68, SEEK_CUR);
11357         FORC4 cam_mul[(c >> 1) | ((c & 1) << 1)] = get2();
11358         break;
11359       case 102:
11360         fseek(ifp, 6, SEEK_CUR);
11361         FORC4 cam_mul[c ^ (c >> 1)] = get2();
11362         break;
11363       case 103:
11364         fseek(ifp, 16, SEEK_CUR);
11365         FORC4 cam_mul[c] = get2();
11366       }
11367       if (ver97 >= 200)
11368       {
11369         if (ver97 != 205)
11370           fseek(ifp, 280, SEEK_CUR);
11371         fread(buf97, 324, 1, ifp);
11372       }
11373     }
11374     if ((tag == 0xa1) && (type == 7) && strncasecmp(make, "Samsung", 7))
11375     {
11376       order = 0x4949;
11377       fseek(ifp, 140, SEEK_CUR);
11378       FORC3 cam_mul[c] = get4();
11379     }
11380     if (tag == 0xa4 && type == 3)
11381     {
11382       fseek(ifp, wbi * 48, SEEK_CUR);
11383       FORC3 cam_mul[c] = get2();
11384     }
11385 
11386     if (tag == 0xa7)
11387     { // shutter count
11388       NikonKey = fgetc(ifp) ^ fgetc(ifp) ^ fgetc(ifp) ^ fgetc(ifp);
11389       if ((unsigned)(ver97 - 200) < 17)
11390       {
11391         ci = xlat[0][serial & 0xff];
11392         cj = xlat[1][NikonKey];
11393         ck = 0x60;
11394         for (i = 0; i < 324; i++)
11395           buf97[i] ^= (cj += ci * ck++);
11396         i = "66666>666;6A;:;55"[ver97 - 200] - '0';
11397         FORC4 cam_mul[c ^ (c >> 1) ^ (i & 1)] = sget2(buf97 + (i & -2) + c * 2);
11398       }
11399 #ifdef LIBRAW_LIBRARY_BUILD
11400       if ((NikonLensDataVersion > 200) && lenNikonLensData)
11401       {
11402         if (custom_serial)
11403         {
11404           ci = xlat[0][custom_serial];
11405         }
11406         else
11407         {
11408           ci = xlat[0][serial & 0xff];
11409         }
11410         cj = xlat[1][NikonKey];
11411         ck = 0x60;
11412         for (i = 0; i < lenNikonLensData; i++)
11413           table_buf[i] ^= (cj += ci * ck++);
11414         processNikonLensData(table_buf, lenNikonLensData);
11415         lenNikonLensData = 0;
11416         free(table_buf);
11417       }
11418       if (ver97 == 601) // Coolpix A
11419       {
11420         imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
11421         imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
11422       }
11423 #endif
11424     }
11425 
11426     if (tag == 0xb001 && type == 3) // Sony ModelID
11427     {
11428       unique_id = get2();
11429     }
11430     if (tag == 0x200 && len == 3)
11431       shot_order = (get4(), get4());
11432     if (tag == 0x200 && len == 4) // Pentax black level
11433       FORC4 cblack[c ^ c >> 1] = get2();
11434     if (tag == 0x201 && len == 4) // Pentax As Shot WB
11435       FORC4 cam_mul[c ^ (c >> 1)] = get2();
11436     if (tag == 0x220 && type == 7)
11437       meta_offset = ftell(ifp);
11438     if (tag == 0x401 && type == 4 && len == 4)
11439       FORC4 cblack[c ^ c >> 1] = get4();
11440 #ifdef LIBRAW_LIBRARY_BUILD
11441     // not corrected for file bitcount, to be patched in open_datastream
11442     if (tag == 0x03d && strstr(make, "NIKON") && len == 4)
11443     {
11444       FORC4 cblack[c ^ c >> 1] = get2();
11445       i = cblack[3];
11446       FORC3 if (i > cblack[c]) i = cblack[c];
11447       FORC4 cblack[c] -= i;
11448       black += i;
11449     }
11450 #endif
11451     if (tag == 0xe01)
11452     { /* Nikon Capture Note */
11453 #ifdef LIBRAW_LIBRARY_BUILD
11454       int loopc = 0;
11455 #endif
11456       order = 0x4949;
11457       fseek(ifp, 22, SEEK_CUR);
11458       for (offset = 22; offset + 22 < len; offset += 22 + i)
11459       {
11460 #ifdef LIBRAW_LIBRARY_BUILD
11461         if (loopc++ > 1024)
11462           throw LIBRAW_EXCEPTION_IO_CORRUPT;
11463 #endif
11464         tag = get4();
11465         fseek(ifp, 14, SEEK_CUR);
11466         i = get4() - 4;
11467         if (tag == 0x76a43207)
11468           flip = get2();
11469         else
11470           fseek(ifp, i, SEEK_CUR);
11471       }
11472     }
11473     if (tag == 0xe80 && len == 256 && type == 7)
11474     {
11475       fseek(ifp, 48, SEEK_CUR);
11476       cam_mul[0] = get2() * 508 * 1.078 / 0x10000;
11477       cam_mul[2] = get2() * 382 * 1.173 / 0x10000;
11478     }
11479     if (tag == 0xf00 && type == 7)
11480     {
11481       if (len == 614)
11482         fseek(ifp, 176, SEEK_CUR);
11483       else if (len == 734 || len == 1502)
11484         fseek(ifp, 148, SEEK_CUR);
11485       else
11486         goto next;
11487       goto get2_256;
11488     }
11489     if (((tag == 0x1011 && len == 9) || tag == 0x20400200) && strcmp(software, "v757-71"))
11490       for (i = 0; i < 3; i++)
11491       {
11492 #ifdef LIBRAW_LIBRARY_BUILD
11493         if (!imgdata.makernotes.olympus.ColorSpace)
11494         {
11495           FORC3 cmatrix[i][c] = ((short)get2()) / 256.0;
11496         }
11497         else
11498         {
11499           FORC3 imgdata.color.ccm[i][c] = ((short)get2()) / 256.0;
11500         }
11501 #else
11502         FORC3 cmatrix[i][c] = ((short)get2()) / 256.0;
11503 #endif
11504       }
11505     if ((tag == 0x1012 || tag == 0x20400600) && len == 4)
11506       FORC4 cblack[c ^ c >> 1] = get2();
11507     if (tag == 0x1017 || tag == 0x20400100)
11508       cam_mul[0] = get2() / 256.0;
11509     if (tag == 0x1018 || tag == 0x20400100)
11510       cam_mul[2] = get2() / 256.0;
11511     if (tag == 0x2011 && len == 2)
11512     {
11513     get2_256:
11514       order = 0x4d4d;
11515       cam_mul[0] = get2() / 256.0;
11516       cam_mul[2] = get2() / 256.0;
11517     }
11518     if ((tag | 0x70) == 0x2070 && (type == 4 || type == 13))
11519       fseek(ifp, get4() + base, SEEK_SET);
11520 #ifdef LIBRAW_LIBRARY_BUILD
11521     // IB start
11522     if (tag == 0x2010)
11523     {
11524       INT64 _pos3 = ftell(ifp);
11525       parse_makernote(base, 0x2010);
11526       fseek(ifp, _pos3, SEEK_SET);
11527     }
11528 
11529     if (((tag == 0x2020) || (tag == 0x3000) || (tag == 0x2030) || (tag == 0x2031) || (tag == 0x2050)) &&
11530         ((type == 7) || (type == 13)) && !strncasecmp(make, "Olympus", 7))
11531     {
11532       INT64 _pos3 = ftell(ifp);
11533       parse_makernote(base, tag);
11534       fseek(ifp, _pos3, SEEK_SET);
11535     }
11536 // IB end
11537 #endif
11538     if ((tag == 0x2020) && ((type == 7) || (type == 13)) && !strncmp(buf, "OLYMP", 5))
11539       parse_thumb_note(base, 257, 258);
11540     if (tag == 0x2040)
11541       parse_makernote(base, 0x2040);
11542     if (tag == 0xb028)
11543     {
11544       fseek(ifp, get4() + base, SEEK_SET);
11545       parse_thumb_note(base, 136, 137);
11546     }
11547     if (tag == 0x4001 && len > 500 && len < 100000)
11548     {
11549       i = len == 582 ? 50 : len == 653 ? 68 : len == 5120 ? 142 : 126;
11550       fseek(ifp, i, SEEK_CUR);
11551       FORC4 cam_mul[c ^ (c >> 1)] = get2();
11552       for (i += 18; i <= len; i += 10)
11553       {
11554         get2();
11555         FORC4 sraw_mul[c ^ (c >> 1)] = get2();
11556         if (sraw_mul[1] == 1170)
11557           break;
11558       }
11559     }
11560     if (!strncasecmp(make, "Samsung", 7))
11561     {
11562       if (tag == 0xa020) // get the full Samsung encryption key
11563         for (i = 0; i < 11; i++)
11564           SamsungKey[i] = get4();
11565       if (tag == 0xa021) // get and decode Samsung cam_mul array
11566         FORC4 cam_mul[c ^ (c >> 1)] = get4() - SamsungKey[c];
11567 #ifdef LIBRAW_LIBRARY_BUILD
11568       if (tag == 0xa022)
11569       {
11570         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get4() - SamsungKey[c + 4];
11571         if (imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][0] < (imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][1] >> 1))
11572         {
11573           imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][1] >> 4;
11574           imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][3] >> 4;
11575         }
11576       }
11577 
11578       if (tag == 0xa023)
11579       {
11580         imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][0] = get4() - SamsungKey[8];
11581         imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][1] = get4() - SamsungKey[9];
11582         imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][3] = get4() - SamsungKey[10];
11583         imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][2] = get4() - SamsungKey[0];
11584         if (imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][0] < (imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][1] >> 1))
11585         {
11586           imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][1] >> 4;
11587           imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Ill_A][3] >> 4;
11588         }
11589       }
11590       if (tag == 0xa024)
11591       {
11592         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_D65][c ^ (c >> 1)] = get4() - SamsungKey[c + 1];
11593         if (imgdata.color.WB_Coeffs[LIBRAW_WBI_D65][0] < (imgdata.color.WB_Coeffs[LIBRAW_WBI_D65][1] >> 1))
11594         {
11595           imgdata.color.WB_Coeffs[LIBRAW_WBI_D65][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_D65][1] >> 4;
11596           imgdata.color.WB_Coeffs[LIBRAW_WBI_D65][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_D65][3] >> 4;
11597         }
11598       }
11599       /*
11600             if (tag == 0xa025) {
11601               i = get4();
11602               imgdata.color.linear_max[0] = imgdata.color.linear_max[1] = imgdata.color.linear_max[2] =
11603                   imgdata.color.linear_max[3] = i - SamsungKey[0]; printf ("Samsung 0xa025 %d\n", i); }
11604       */
11605       if (tag == 0xa030 && len == 9)
11606         for (i = 0; i < 3; i++)
11607           FORC3 imgdata.color.ccm[i][c] = (float)((short)((get4() + SamsungKey[i * 3 + c]))) / 256.0;
11608 #endif
11609       if (tag == 0xa031 && len == 9) // get and decode Samsung color matrix
11610         for (i = 0; i < 3; i++)
11611           FORC3 cmatrix[i][c] = (float)((short)((get4() + SamsungKey[i * 3 + c]))) / 256.0;
11612 
11613       if (tag == 0xa028)
11614         FORC4 cblack[c ^ (c >> 1)] = get4() - SamsungKey[c];
11615     }
11616     else
11617     {
11618       // Somebody else use 0xa021 and 0xa028?
11619       if (tag == 0xa021)
11620         FORC4 cam_mul[c ^ (c >> 1)] = get4();
11621       if (tag == 0xa028)
11622         FORC4 cam_mul[c ^ (c >> 1)] -= get4();
11623     }
11624 #ifdef LIBRAW_LIBRARY_BUILD
11625     if (tag == 0x4021 && (imgdata.makernotes.canon.multishot[0] = get4()) &&
11626         (imgdata.makernotes.canon.multishot[1] = get4()))
11627     {
11628       if (len >= 4)
11629       {
11630         imgdata.makernotes.canon.multishot[2] = get4();
11631         imgdata.makernotes.canon.multishot[3] = get4();
11632       }
11633       FORC4 cam_mul[c] = 1024;
11634     }
11635 #else
11636     if (tag == 0x4021 && get4() && get4())
11637       FORC4 cam_mul[c] = 1024;
11638 #endif
11639   next:
11640     fseek(ifp, save, SEEK_SET);
11641   }
11642 quit:
11643   order = sorder;
11644 }
11645 
11646 /*
11647    Since the TIFF DateTime string has no timezone information,
11648    assume that the camera's clock was set to Universal Time.
11649  */
get_timestamp(int reversed)11650 void CLASS get_timestamp(int reversed)
11651 {
11652   struct tm t;
11653   char str[20];
11654   int i;
11655 
11656   str[19] = 0;
11657   if (reversed)
11658     for (i = 19; i--;)
11659       str[i] = fgetc(ifp);
11660   else
11661     fread(str, 19, 1, ifp);
11662   memset(&t, 0, sizeof t);
11663   if (sscanf(str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
11664     return;
11665   t.tm_year -= 1900;
11666   t.tm_mon -= 1;
11667   t.tm_isdst = -1;
11668   if (mktime(&t) > 0)
11669     timestamp = mktime(&t);
11670 }
11671 
parse_exif(int base)11672 void CLASS parse_exif(int base)
11673 {
11674   unsigned kodak, entries, tag, type, len, save, c;
11675   double expo, ape;
11676 
11677   kodak = !strncmp(make, "EASTMAN", 7) && tiff_nifds < 3;
11678   entries = get2();
11679   if (!strncmp(make, "Hasselblad", 10) && (tiff_nifds > 3) && (entries > 512))
11680     return;
11681 #ifdef LIBRAW_LIBRARY_BUILD
11682   INT64 fsize = ifp->size();
11683 #endif
11684   while (entries--)
11685   {
11686     tiff_get(base, &tag, &type, &len, &save);
11687 
11688 #ifdef LIBRAW_LIBRARY_BUILD
11689     INT64 savepos = ftell(ifp);
11690     if (len > 8 && savepos + len > fsize * 2)
11691     {
11692       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
11693       continue;
11694     }
11695     if (callbacks.exif_cb)
11696     {
11697       callbacks.exif_cb(callbacks.exifparser_data, tag, type, len, order, ifp);
11698       fseek(ifp, savepos, SEEK_SET);
11699     }
11700 #endif
11701     switch (tag)
11702     {
11703 #ifdef LIBRAW_LIBRARY_BUILD
11704 
11705     case 0x9400:
11706       imgdata.other.exifAmbientTemperature = getreal(type);
11707       if ((imgdata.other.CameraTemperature > -273.15f) && (OlyID == 0x4434353933ULL)) // TG-5
11708         imgdata.other.CameraTemperature += imgdata.other.exifAmbientTemperature;
11709       break;
11710     case 0x9401:
11711       imgdata.other.exifHumidity = getreal(type);
11712       break;
11713     case 0x9402:
11714       imgdata.other.exifPressure = getreal(type);
11715       break;
11716     case 0x9403:
11717       imgdata.other.exifWaterDepth = getreal(type);
11718       break;
11719     case 0x9404:
11720       imgdata.other.exifAcceleration = getreal(type);
11721       break;
11722     case 0x9405:
11723       imgdata.other.exifCameraElevationAngle = getreal(type);
11724       break;
11725 
11726     case 0xa405: // FocalLengthIn35mmFormat
11727       imgdata.lens.FocalLengthIn35mmFormat = get2();
11728       break;
11729     case 0xa431: // BodySerialNumber
11730       stmread(imgdata.shootinginfo.BodySerial, len, ifp);
11731       break;
11732     case 0xa432: // LensInfo, 42034dec, Lens Specification per EXIF standard
11733       imgdata.lens.MinFocal = getreal(type);
11734       imgdata.lens.MaxFocal = getreal(type);
11735       imgdata.lens.MaxAp4MinFocal = getreal(type);
11736       imgdata.lens.MaxAp4MaxFocal = getreal(type);
11737       break;
11738     case 0xa435: // LensSerialNumber
11739       stmread(imgdata.lens.LensSerial, len, ifp);
11740       break;
11741     case 0xc630: // DNG LensInfo, Lens Specification per EXIF standard
11742       imgdata.lens.dng.MinFocal = getreal(type);
11743       imgdata.lens.dng.MaxFocal = getreal(type);
11744       imgdata.lens.dng.MaxAp4MinFocal = getreal(type);
11745       imgdata.lens.dng.MaxAp4MaxFocal = getreal(type);
11746       break;
11747     case 0xa433: // LensMake
11748       stmread(imgdata.lens.LensMake, len, ifp);
11749       break;
11750     case 0xa434: // LensModel
11751       stmread(imgdata.lens.Lens, len, ifp);
11752       if (!strncmp(imgdata.lens.Lens, "----", 4))
11753         imgdata.lens.Lens[0] = 0;
11754       break;
11755     case 0x9205:
11756       imgdata.lens.EXIF_MaxAp = libraw_powf64l(2.0f, (getreal(type) / 2.0f));
11757       break;
11758 #endif
11759     case 33434:
11760       tiff_ifd[tiff_nifds - 1].t_shutter = shutter = getreal(type);
11761       break;
11762     case 33437:
11763       aperture = getreal(type);
11764       break; // 0x829d FNumber
11765     case 34855:
11766       iso_speed = get2();
11767       break;
11768     case 34865:
11769       if (iso_speed == 0xffff && !strncasecmp(make, "FUJI", 4))
11770         iso_speed = getreal(type);
11771       break;
11772     case 34866:
11773       if (iso_speed == 0xffff && (!strncasecmp(make, "SONY", 4) || !strncasecmp(make, "CANON", 5)))
11774         iso_speed = getreal(type);
11775       break;
11776     case 36867:
11777     case 36868:
11778       get_timestamp(0);
11779       break;
11780     case 37377:
11781       if ((expo = -getreal(type)) < 128 && shutter == 0.)
11782         tiff_ifd[tiff_nifds - 1].t_shutter = shutter = libraw_powf64l(2.0, expo);
11783       break;
11784     case 37378: // 0x9202 ApertureValue
11785       if ((fabs(ape = getreal(type)) < 256.0) && (!aperture))
11786         aperture = libraw_powf64l(2.0, ape / 2);
11787       break;
11788     case 37385:
11789       flash_used = getreal(type);
11790       break;
11791     case 37386:
11792       focal_len = getreal(type);
11793       break;
11794     case 37500: // tag 0x927c
11795 #ifdef LIBRAW_LIBRARY_BUILD
11796       if (((make[0] == '\0') && (!strncmp(model, "ov5647", 6))) ||
11797           ((!strncmp(make, "RaspberryPi", 11)) && (!strncmp(model, "RP_OV5647", 9))) ||
11798           ((!strncmp(make, "RaspberryPi", 11)) && (!strncmp(model, "RP_imx219", 9))))
11799       {
11800         char mn_text[512];
11801         char *pos;
11802         char ccms[512];
11803         ushort l;
11804         float num;
11805 
11806 	fgets(mn_text, MIN(len,511), ifp);
11807         mn_text[511] = 0;
11808 
11809         pos = strstr(mn_text, "gain_r=");
11810         if (pos)
11811           cam_mul[0] = atof(pos + 7);
11812         pos = strstr(mn_text, "gain_b=");
11813         if (pos)
11814           cam_mul[2] = atof(pos + 7);
11815         if ((cam_mul[0] > 0.001f) && (cam_mul[2] > 0.001f))
11816           cam_mul[1] = cam_mul[3] = 1.0f;
11817         else
11818           cam_mul[0] = cam_mul[2] = 0.0f;
11819 
11820         pos = strstr(mn_text, "ccm=");
11821         if(pos)
11822         {
11823          pos +=4;
11824          char *pos2 = strstr(pos, " ");
11825          if(pos2)
11826          {
11827            l = pos2 - pos;
11828            memcpy(ccms, pos, l);
11829            ccms[l] = '\0';
11830 #if defined WIN32 || defined(__MINGW32__)
11831            // Win32 strtok is already thread-safe
11832           pos = strtok(ccms, ",");
11833 #else
11834           char *last=0;
11835           pos = strtok_r(ccms, ",",&last);
11836 #endif
11837           if(pos)
11838           {
11839             for (l = 0; l < 4; l++)
11840             {
11841               num = 0.0;
11842               for (c = 0; c < 3; c++)
11843               {
11844                 imgdata.color.ccm[l][c] = (float)atoi(pos);
11845                 num += imgdata.color.ccm[l][c];
11846 #if defined WIN32 || defined(__MINGW32__)
11847                 pos = strtok(NULL, ",");
11848 #else
11849                 pos = strtok_r(NULL, ",",&last);
11850 #endif
11851                 if(!pos) goto end; // broken
11852               }
11853               if (num > 0.01)
11854                 FORC3 imgdata.color.ccm[l][c] = imgdata.color.ccm[l][c] / num;
11855             }
11856           }
11857         }
11858        }
11859       end:;
11860       }
11861       else
11862 
11863 #endif
11864         parse_makernote(base, 0);
11865       break;
11866     case 40962:
11867       if (kodak)
11868         raw_width = get4();
11869       break;
11870     case 40963:
11871       if (kodak)
11872         raw_height = get4();
11873       break;
11874     case 41730:
11875       if (get4() == 0x20002)
11876         for (exif_cfa = c = 0; c < 8; c += 2)
11877           exif_cfa |= fgetc(ifp) * 0x01010101U << c;
11878     }
11879     fseek(ifp, save, SEEK_SET);
11880   }
11881 }
11882 
11883 #ifdef LIBRAW_LIBRARY_BUILD
11884 
parse_gps_libraw(int base)11885 void CLASS parse_gps_libraw(int base)
11886 {
11887   unsigned entries, tag, type, len, save, c;
11888 
11889   entries = get2();
11890   if (entries > 200)
11891     return;
11892   if (entries > 0)
11893     imgdata.other.parsed_gps.gpsparsed = 1;
11894   while (entries--)
11895   {
11896     tiff_get(base, &tag, &type, &len, &save);
11897     if (len > 1024)
11898     {
11899       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
11900       continue; // no GPS tags are 1k or larger
11901     }
11902     switch (tag)
11903     {
11904     case 1:
11905       imgdata.other.parsed_gps.latref = getc(ifp);
11906       break;
11907     case 3:
11908       imgdata.other.parsed_gps.longref = getc(ifp);
11909       break;
11910     case 5:
11911       imgdata.other.parsed_gps.altref = getc(ifp);
11912       break;
11913     case 2:
11914       if (len == 3)
11915         FORC(3) imgdata.other.parsed_gps.latitude[c] = getreal(type);
11916       break;
11917     case 4:
11918       if (len == 3)
11919         FORC(3) imgdata.other.parsed_gps.longtitude[c] = getreal(type);
11920       break;
11921     case 7:
11922       if (len == 3)
11923         FORC(3) imgdata.other.parsed_gps.gpstimestamp[c] = getreal(type);
11924       break;
11925     case 6:
11926       imgdata.other.parsed_gps.altitude = getreal(type);
11927       break;
11928     case 9:
11929       imgdata.other.parsed_gps.gpsstatus = getc(ifp);
11930       break;
11931     }
11932     fseek(ifp, save, SEEK_SET);
11933   }
11934 }
11935 #endif
11936 
parse_gps(int base)11937 void CLASS parse_gps(int base)
11938 {
11939   unsigned entries, tag, type, len, save, c;
11940 
11941   entries = get2();
11942   while (entries--)
11943   {
11944     tiff_get(base, &tag, &type, &len, &save);
11945     if (len > 1024)
11946     {
11947       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
11948       continue; // no GPS tags are 1k or larger
11949     }
11950     switch (tag)
11951     {
11952     case 1:
11953     case 3:
11954     case 5:
11955       gpsdata[29 + tag / 2] = getc(ifp);
11956       break;
11957     case 2:
11958     case 4:
11959     case 7:
11960       FORC(6) gpsdata[tag / 3 * 6 + c] = get4();
11961       break;
11962     case 6:
11963       FORC(2) gpsdata[18 + c] = get4();
11964       break;
11965     case 18:
11966     case 29:
11967       fgets((char *)(gpsdata + 14 + tag / 3), MIN(len, 12), ifp);
11968     }
11969     fseek(ifp, save, SEEK_SET);
11970   }
11971 }
11972 
romm_coeff(float romm_cam[3][3])11973 void CLASS romm_coeff(float romm_cam[3][3])
11974 {
11975   static const float rgb_romm[3][3] = /* ROMM == Kodak ProPhoto */
11976       {{2.034193, -0.727420, -0.306766}, {-0.228811, 1.231729, -0.002922}, {-0.008565, -0.153273, 1.161839}};
11977   int i, j, k;
11978 
11979   for (i = 0; i < 3; i++)
11980     for (j = 0; j < 3; j++)
11981       for (cmatrix[i][j] = k = 0; k < 3; k++)
11982         cmatrix[i][j] += rgb_romm[i][k] * romm_cam[k][j];
11983 }
11984 
parse_mos(int offset)11985 void CLASS parse_mos(int offset)
11986 {
11987   char data[40];
11988   int skip, from, i, c, neut[4], planes = 0, frot = 0;
11989   static const char *mod[] = {"",
11990                               "DCB2",
11991                               "Volare",
11992                               "Cantare",
11993                               "CMost",
11994                               "Valeo 6",
11995                               "Valeo 11",
11996                               "Valeo 22",
11997                               "Valeo 11p",
11998                               "Valeo 17",
11999                               "",
12000                               "Aptus 17",
12001                               "Aptus 22",
12002                               "Aptus 75",
12003                               "Aptus 65",
12004                               "Aptus 54S",
12005                               "Aptus 65S",
12006                               "Aptus 75S",
12007                               "AFi 5",
12008                               "AFi 6",
12009                               "AFi 7",
12010                               "AFi-II 7",
12011                               "Aptus-II 7",
12012                               "",
12013                               "Aptus-II 6",
12014                               "",
12015                               "",
12016                               "Aptus-II 10",
12017                               "Aptus-II 5",
12018                               "",
12019                               "",
12020                               "",
12021                               "",
12022                               "Aptus-II 10R",
12023                               "Aptus-II 8",
12024                               "",
12025                               "Aptus-II 12",
12026                               "",
12027                               "AFi-II 12"};
12028   float romm_cam[3][3];
12029 
12030   fseek(ifp, offset, SEEK_SET);
12031   while (1)
12032   {
12033     if (get4() != 0x504b5453)
12034       break;
12035     get4();
12036     fread(data, 1, 40, ifp);
12037     skip = get4();
12038     from = ftell(ifp);
12039 
12040 // IB start
12041 #ifdef LIBRAW_LIBRARY_BUILD
12042     if (!strcmp(data, "CameraObj_camera_type"))
12043     {
12044       stmread(imgdata.lens.makernotes.body, skip, ifp);
12045     }
12046     if (!strcmp(data, "back_serial_number"))
12047     {
12048       char buffer[sizeof(imgdata.shootinginfo.BodySerial)];
12049       char *words[4];
12050       int nwords;
12051       stmread(buffer, skip, ifp);
12052       nwords = getwords(buffer, words, 4, sizeof(imgdata.shootinginfo.BodySerial));
12053       strcpy(imgdata.shootinginfo.BodySerial, words[0]);
12054     }
12055     if (!strcmp(data, "CaptProf_serial_number"))
12056     {
12057       char buffer[sizeof(imgdata.shootinginfo.InternalBodySerial)];
12058       char *words[4];
12059       int nwords;
12060       stmread(buffer, skip, ifp);
12061       nwords = getwords(buffer, words, 4, sizeof(imgdata.shootinginfo.InternalBodySerial));
12062       strcpy(imgdata.shootinginfo.InternalBodySerial, words[0]);
12063     }
12064 #endif
12065     // IB end
12066     if (!strcmp(data, "JPEG_preview_data"))
12067     {
12068       thumb_offset = from;
12069       thumb_length = skip;
12070     }
12071     if (!strcmp(data, "icc_camera_profile"))
12072     {
12073       profile_offset = from;
12074       profile_length = skip;
12075     }
12076     if (!strcmp(data, "ShootObj_back_type"))
12077     {
12078       fscanf(ifp, "%d", &i);
12079       if ((unsigned)i < sizeof mod / sizeof(*mod))
12080         strcpy(model, mod[i]);
12081     }
12082     if (!strcmp(data, "icc_camera_to_tone_matrix"))
12083     {
12084       for (i = 0; i < 9; i++)
12085         ((float *)romm_cam)[i] = int_to_float(get4());
12086       romm_coeff(romm_cam);
12087     }
12088     if (!strcmp(data, "CaptProf_color_matrix"))
12089     {
12090       for (i = 0; i < 9; i++)
12091         fscanf(ifp, "%f", (float *)romm_cam + i);
12092       romm_coeff(romm_cam);
12093     }
12094     if (!strcmp(data, "CaptProf_number_of_planes"))
12095       fscanf(ifp, "%d", &planes);
12096     if (!strcmp(data, "CaptProf_raw_data_rotation"))
12097       fscanf(ifp, "%d", &flip);
12098     if (!strcmp(data, "CaptProf_mosaic_pattern"))
12099       FORC4
12100       {
12101         fscanf(ifp, "%d", &i);
12102         if (i == 1)
12103           frot = c ^ (c >> 1);
12104       }
12105     if (!strcmp(data, "ImgProf_rotation_angle"))
12106     {
12107       fscanf(ifp, "%d", &i);
12108       flip = i - flip;
12109     }
12110     if (!strcmp(data, "NeutObj_neutrals") && !cam_mul[0])
12111     {
12112       FORC4 fscanf(ifp, "%d", neut + c);
12113       FORC3 cam_mul[c] = (float)neut[0] / neut[c + 1];
12114     }
12115     if (!strcmp(data, "Rows_data"))
12116       load_flags = get4();
12117     parse_mos(from);
12118     fseek(ifp, skip + from, SEEK_SET);
12119   }
12120   if (planes)
12121     filters = (planes == 1) * 0x01010101U * (uchar) "\x94\x61\x16\x49"[(flip / 90 + frot) & 3];
12122 }
12123 
linear_table(unsigned len)12124 void CLASS linear_table(unsigned len)
12125 {
12126   int i;
12127   if (len > 0x10000)
12128     len = 0x10000;
12129   else if(len < 1)
12130     return;
12131   read_shorts(curve, len);
12132   for (i = len; i < 0x10000; i++)
12133     curve[i] = curve[i - 1];
12134   maximum = curve[len < 0x1000 ? 0xfff : len - 1];
12135 }
12136 
12137 #ifdef LIBRAW_LIBRARY_BUILD
12138 
Kodak_WB_0x08tags(int wb,unsigned type)12139 void CLASS Kodak_WB_0x08tags(int wb, unsigned type)
12140 {
12141   float mul[3] = {1, 1, 1}, num, mul2;
12142   int c;
12143   FORC3 mul[c] = (num = getreal(type)) == 0 ? 1 : num;
12144   imgdata.color.WB_Coeffs[wb][1] = imgdata.color.WB_Coeffs[wb][3] = mul[1];
12145   mul2 = mul[1] * mul[1];
12146   imgdata.color.WB_Coeffs[wb][0] = mul2 / mul[0];
12147   imgdata.color.WB_Coeffs[wb][2] = mul2 / mul[2];
12148   return;
12149 }
12150 
12151 /* Thanks to Alexey Danilchenko for wb as-shot parsing code */
parse_kodak_ifd(int base)12152 void CLASS parse_kodak_ifd(int base)
12153 {
12154   unsigned entries, tag, type, len, save;
12155   int j, c, wbi = -2, romm_camTemp[9], romm_camScale[3];
12156   float mul[3] = {1, 1, 1}, num;
12157   static const int wbtag[] = {64037, 64040, 64039, 64041, -1, -1, 64042};
12158   //  int a_blck = 0;
12159 
12160   entries = get2();
12161   if (entries > 1024)
12162     return;
12163   INT64 fsize = ifp->size();
12164   while (entries--)
12165   {
12166     tiff_get(base, &tag, &type, &len, &save);
12167     INT64 savepos = ftell(ifp);
12168     if (len > 8 && len + savepos > 2 * fsize)
12169     {
12170       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
12171       continue;
12172     }
12173     if (callbacks.exif_cb)
12174     {
12175       callbacks.exif_cb(callbacks.exifparser_data, tag | 0x20000, type, len, order, ifp);
12176       fseek(ifp, savepos, SEEK_SET);
12177     }
12178     if (tag == 1003)
12179       imgdata.sizes.raw_crop.cleft = get2();
12180     if (tag == 1004)
12181       imgdata.sizes.raw_crop.ctop = get2();
12182     if (tag == 1005)
12183       imgdata.sizes.raw_crop.cwidth = get2();
12184     if (tag == 1006)
12185       imgdata.sizes.raw_crop.cheight = get2();
12186     if (tag == 1007)
12187       imgdata.makernotes.kodak.BlackLevelTop = get2();
12188     if (tag == 1008)
12189       imgdata.makernotes.kodak.BlackLevelBottom = get2();
12190     if (tag == 1011)
12191       imgdata.other.FlashEC = getreal(type);
12192     if (tag == 1020)
12193       wbi = getint(type);
12194     if (tag == 1021 && len == 72)
12195     { /* WB set in software */
12196       fseek(ifp, 40, SEEK_CUR);
12197       FORC3 cam_mul[c] = 2048.0 / fMAX(1.0f, get2());
12198       wbi = -2;
12199     }
12200 
12201     if ((tag == 1030) && (len == 1))
12202       imgdata.other.CameraTemperature = getreal(type);
12203     if ((tag == 1043) && (len == 1))
12204       imgdata.other.SensorTemperature = getreal(type);
12205 
12206     if ((tag == 0x03ef) && (!strcmp(model, "EOS D2000C")))
12207       black = get2();
12208     if ((tag == 0x03f0) && (!strcmp(model, "EOS D2000C")))
12209     {
12210       if (black) // already set by tag 0x03ef
12211         black = (black + get2()) / 2;
12212       else
12213         black = get2();
12214     }
12215     INT64 _pos2 = ftell(ifp);
12216     if (tag == 0x0848)
12217       Kodak_WB_0x08tags(LIBRAW_WBI_Daylight, type);
12218     if (tag == 0x0849)
12219       Kodak_WB_0x08tags(LIBRAW_WBI_Tungsten, type);
12220     if (tag == 0x084a)
12221       Kodak_WB_0x08tags(LIBRAW_WBI_Fluorescent, type);
12222     if (tag == 0x084b)
12223       Kodak_WB_0x08tags(LIBRAW_WBI_Flash, type);
12224     if (tag == 0x084c)
12225       Kodak_WB_0x08tags(LIBRAW_WBI_Custom, type);
12226     if (tag == 0x084d)
12227       Kodak_WB_0x08tags(LIBRAW_WBI_Auto, type);
12228     if (tag == 0x0e93)
12229       imgdata.color.linear_max[0] = imgdata.color.linear_max[1] = imgdata.color.linear_max[2] =
12230           imgdata.color.linear_max[3] = get2();
12231     if (tag == 0x09ce)
12232       stmread(imgdata.shootinginfo.InternalBodySerial, len, ifp);
12233     if (tag == 0xfa00)
12234       stmread(imgdata.shootinginfo.BodySerial, len, ifp);
12235     if (tag == 0xfa27)
12236     {
12237       FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][c] = get4();
12238       imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][1];
12239     }
12240     if (tag == 0xfa28)
12241     {
12242       FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][c] = get4();
12243       imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][1];
12244     }
12245     if (tag == 0xfa29)
12246     {
12247       FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][c] = get4();
12248       imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][1];
12249     }
12250     if (tag == 0xfa2a)
12251     {
12252       FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][c] = get4();
12253       imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][1];
12254     }
12255 
12256     fseek(ifp, _pos2, SEEK_SET);
12257 
12258     if (((tag == 0x07e4) || (tag == 0xfb01)) && (len == 9))
12259     {
12260       short validM = 0;
12261       if (type == 10)
12262       {
12263         for (j = 0; j < 9; j++)
12264         {
12265           ((float *)imgdata.makernotes.kodak.romm_camDaylight)[j] = getreal(type);
12266         }
12267         validM = 1;
12268       }
12269       else if (type == 9)
12270       {
12271         FORC3
12272         {
12273           romm_camScale[c] = 0;
12274           for (j = 0; j < 3; j++)
12275           {
12276             romm_camTemp[c * 3 + j] = get4();
12277             romm_camScale[c] += romm_camTemp[c * 3 + j];
12278           }
12279         }
12280         if ((romm_camScale[0] > 0x1fff) && (romm_camScale[1] > 0x1fff) && (romm_camScale[2] > 0x1fff))
12281         {
12282           FORC3 for (j = 0; j < 3; j++)
12283           {
12284             ((float *)imgdata.makernotes.kodak.romm_camDaylight)[c * 3 + j] =
12285                 ((float)romm_camTemp[c * 3 + j]) / ((float)romm_camScale[c]);
12286           }
12287           validM = 1;
12288         }
12289       }
12290       if (validM)
12291       {
12292         romm_coeff(imgdata.makernotes.kodak.romm_camDaylight);
12293       }
12294     }
12295     if (((tag == 0x07e5) || (tag == 0xfb02)) && (len == 9))
12296     {
12297       if (type == 10)
12298       {
12299         for (j = 0; j < 9; j++)
12300         {
12301           ((float *)imgdata.makernotes.kodak.romm_camTungsten)[j] = getreal(type);
12302         }
12303       }
12304       else if (type == 9)
12305       {
12306         FORC3
12307         {
12308           romm_camScale[c] = 0;
12309           for (j = 0; j < 3; j++)
12310           {
12311             romm_camTemp[c * 3 + j] = get4();
12312             romm_camScale[c] += romm_camTemp[c * 3 + j];
12313           }
12314         }
12315         if ((romm_camScale[0] > 0x1fff) && (romm_camScale[1] > 0x1fff) && (romm_camScale[2] > 0x1fff))
12316         {
12317           FORC3 for (j = 0; j < 3; j++)
12318           {
12319             ((float *)imgdata.makernotes.kodak.romm_camTungsten)[c * 3 + j] =
12320                 ((float)romm_camTemp[c * 3 + j]) / ((float)romm_camScale[c]);
12321           }
12322         }
12323       }
12324     }
12325     if (((tag == 0x07e6) || (tag == 0xfb03)) && (len == 9))
12326     {
12327       if (type == 10)
12328       {
12329         for (j = 0; j < 9; j++)
12330         {
12331           ((float *)imgdata.makernotes.kodak.romm_camFluorescent)[j] = getreal(type);
12332         }
12333       }
12334       else if (type == 9)
12335       {
12336         FORC3
12337         {
12338           romm_camScale[c] = 0;
12339           for (j = 0; j < 3; j++)
12340           {
12341             romm_camTemp[c * 3 + j] = get4();
12342             romm_camScale[c] += romm_camTemp[c * 3 + j];
12343           }
12344         }
12345         if ((romm_camScale[0] > 0x1fff) && (romm_camScale[1] > 0x1fff) && (romm_camScale[2] > 0x1fff))
12346         {
12347           FORC3 for (j = 0; j < 3; j++)
12348           {
12349             ((float *)imgdata.makernotes.kodak.romm_camFluorescent)[c * 3 + j] =
12350                 ((float)romm_camTemp[c * 3 + j]) / ((float)romm_camScale[c]);
12351           }
12352         }
12353       }
12354     }
12355     if (((tag == 0x07e7) || (tag == 0xfb04)) && (len == 9))
12356     {
12357       if (type == 10)
12358       {
12359         for (j = 0; j < 9; j++)
12360         {
12361           ((float *)imgdata.makernotes.kodak.romm_camFlash)[j] = getreal(type);
12362         }
12363       }
12364       else if (type == 9)
12365       {
12366         FORC3
12367         {
12368           romm_camScale[c] = 0;
12369           for (j = 0; j < 3; j++)
12370           {
12371             romm_camTemp[c * 3 + j] = get4();
12372             romm_camScale[c] += romm_camTemp[c * 3 + j];
12373           }
12374         }
12375         if ((romm_camScale[0] > 0x1fff) && (romm_camScale[1] > 0x1fff) && (romm_camScale[2] > 0x1fff))
12376         {
12377           FORC3 for (j = 0; j < 3; j++)
12378           {
12379             ((float *)imgdata.makernotes.kodak.romm_camFlash)[c * 3 + j] =
12380                 ((float)romm_camTemp[c * 3 + j]) / ((float)romm_camScale[c]);
12381           }
12382         }
12383       }
12384     }
12385     if (((tag == 0x07e8) || (tag == 0xfb05)) && (len == 9))
12386     {
12387       if (type == 10)
12388       {
12389         for (j = 0; j < 9; j++)
12390         {
12391           ((float *)imgdata.makernotes.kodak.romm_camCustom)[j] = getreal(type);
12392         }
12393       }
12394       else if (type == 9)
12395       {
12396         FORC3
12397         {
12398           romm_camScale[c] = 0;
12399           for (j = 0; j < 3; j++)
12400           {
12401             romm_camTemp[c * 3 + j] = get4();
12402             romm_camScale[c] += romm_camTemp[c * 3 + j];
12403           }
12404         }
12405         if ((romm_camScale[0] > 0x1fff) && (romm_camScale[1] > 0x1fff) && (romm_camScale[2] > 0x1fff))
12406         {
12407           FORC3 for (j = 0; j < 3; j++)
12408           {
12409             ((float *)imgdata.makernotes.kodak.romm_camCustom)[c * 3 + j] =
12410                 ((float)romm_camTemp[c * 3 + j]) / ((float)romm_camScale[c]);
12411           }
12412         }
12413       }
12414     }
12415     if (((tag == 0x07e9) || (tag == 0xfb06)) && (len == 9))
12416     {
12417       if (type == 10)
12418       {
12419         for (j = 0; j < 9; j++)
12420         {
12421           ((float *)imgdata.makernotes.kodak.romm_camAuto)[j] = getreal(type);
12422         }
12423       }
12424       else if (type == 9)
12425       {
12426         FORC3
12427         {
12428           romm_camScale[c] = 0;
12429           for (j = 0; j < 3; j++)
12430           {
12431             romm_camTemp[c * 3 + j] = get4();
12432             romm_camScale[c] += romm_camTemp[c * 3 + j];
12433           }
12434         }
12435         if ((romm_camScale[0] > 0x1fff) && (romm_camScale[1] > 0x1fff) && (romm_camScale[2] > 0x1fff))
12436         {
12437           FORC3 for (j = 0; j < 3; j++)
12438           {
12439             ((float *)imgdata.makernotes.kodak.romm_camAuto)[c * 3 + j] =
12440                 ((float)romm_camTemp[c * 3 + j]) / ((float)romm_camScale[c]);
12441           }
12442         }
12443       }
12444     }
12445 
12446     if (tag == 2120 + wbi || (wbi < 0 && tag == 2125)) /* use Auto WB if illuminant index is not set */
12447     {
12448       FORC3 mul[c] = (num = getreal(type)) == 0 ? 1 : num;
12449       FORC3 cam_mul[c] = mul[1] / mul[c]; /* normalise against green */
12450     }
12451     if (tag == 2317)
12452       linear_table(len);
12453     if (tag == 0x903)
12454       iso_speed = getreal(type);
12455     // if (tag == 6020) iso_speed = getint(type);
12456     if (tag == 64013)
12457       wbi = fgetc(ifp);
12458     if ((unsigned)wbi < 7 && tag == wbtag[wbi])
12459       FORC3 cam_mul[c] = get4();
12460     if (tag == 0xfa13)
12461       width = getint(type);
12462     if (tag == 0xfa14)
12463       height = (getint(type) + 1) & -2;
12464 
12465     /*
12466         height = getint(type);
12467 
12468         if (tag == 0xfa16)
12469           raw_width = get2();
12470         if (tag == 0xfa17)
12471           raw_height = get2();
12472     */
12473     if (tag == 0xfa18)
12474     {
12475       imgdata.makernotes.kodak.offset_left = getint(8);
12476       if (type != 8)
12477         imgdata.makernotes.kodak.offset_left += 1;
12478     }
12479     if (tag == 0xfa19)
12480     {
12481       imgdata.makernotes.kodak.offset_top = getint(8);
12482       if (type != 8)
12483         imgdata.makernotes.kodak.offset_top += 1;
12484     }
12485 
12486     if (tag == 0xfa31)
12487       imgdata.sizes.raw_crop.cwidth = get2();
12488     if (tag == 0xfa32)
12489       imgdata.sizes.raw_crop.cheight = get2();
12490     if (tag == 0xfa3e)
12491       imgdata.sizes.raw_crop.cleft = get2();
12492     if (tag == 0xfa3f)
12493       imgdata.sizes.raw_crop.ctop = get2();
12494 
12495     fseek(ifp, save, SEEK_SET);
12496   }
12497 }
12498 #else
parse_kodak_ifd(int base)12499 void CLASS parse_kodak_ifd(int base)
12500 {
12501   unsigned entries, tag, type, len, save;
12502   int i, c, wbi = -2, wbtemp = 6500;
12503   float mul[3] = {1, 1, 1}, num;
12504   static const int wbtag[] = {64037, 64040, 64039, 64041, -1, -1, 64042};
12505 
12506   entries = get2();
12507   if (entries > 1024)
12508     return;
12509   while (entries--)
12510   {
12511     tiff_get(base, &tag, &type, &len, &save);
12512     if (tag == 1020)
12513       wbi = getint(type);
12514     if (tag == 1021 && len == 72)
12515     { /* WB set in software */
12516       fseek(ifp, 40, SEEK_CUR);
12517       FORC3 cam_mul[c] = 2048.0 / fMAX(1.0, get2());
12518       wbi = -2;
12519     }
12520     if (tag == 2118)
12521       wbtemp = getint(type);
12522     if (tag == 2120 + wbi && wbi >= 0)
12523       FORC3 cam_mul[c] = 2048.0 / fMAX(1.0, getreal(type));
12524     if (tag == 2130 + wbi)
12525       FORC3 mul[c] = getreal(type);
12526     if (tag == 2140 + wbi && wbi >= 0)
12527       FORC3
12528       {
12529         for (num = i = 0; i < 4; i++)
12530           num += getreal(type) * pow(wbtemp / 100.0, i);
12531         cam_mul[c] = 2048 / fMAX(1.0, (num * mul[c]));
12532       }
12533     if (tag == 2317)
12534       linear_table(len);
12535     if (tag == 6020)
12536       iso_speed = getint(type);
12537     if (tag == 64013)
12538       wbi = fgetc(ifp);
12539     if ((unsigned)wbi < 7 && tag == wbtag[wbi])
12540       FORC3 cam_mul[c] = get4();
12541     if (tag == 64019)
12542       width = getint(type);
12543     if (tag == 64020)
12544       height = (getint(type) + 1) & -2;
12545     fseek(ifp, save, SEEK_SET);
12546   }
12547 }
12548 #endif
parse_tiff_ifd(int base)12549 int CLASS parse_tiff_ifd(int base)
12550 {
12551   unsigned entries, tag, type, len, plen = 16, save;
12552   int ifd, use_cm = 0, cfa, i, j, c, ima_len = 0;
12553   char *cbuf, *cp;
12554   uchar cfa_pat[16], cfa_pc[] = {0, 1, 2, 3}, tab[256];
12555   double fm[3][4], cc[4][4], cm[4][3], cam_xyz[4][3], num;
12556   double ab[] = {1, 1, 1, 1}, asn[] = {0, 0, 0, 0}, xyz[] = {1, 1, 1};
12557   unsigned sony_curve[] = {0, 0, 0, 0, 0, 4095};
12558   unsigned *buf, sony_offset = 0, sony_length = 0, sony_key = 0;
12559   struct jhead jh;
12560   int pana_raw = 0;
12561 #ifndef LIBRAW_LIBRARY_BUILD
12562   FILE *sfp;
12563 #endif
12564 
12565   if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
12566     return 1;
12567   ifd = tiff_nifds++;
12568   for (j = 0; j < 4; j++)
12569     for (i = 0; i < 4; i++)
12570       cc[j][i] = i == j;
12571   entries = get2();
12572   if (entries > 512)
12573     return 1;
12574 #ifdef LIBRAW_LIBRARY_BUILD
12575   INT64 fsize = ifp->size();
12576 #endif
12577   while (entries--)
12578   {
12579     tiff_get(base, &tag, &type, &len, &save);
12580 #ifdef LIBRAW_LIBRARY_BUILD
12581     INT64 savepos = ftell(ifp);
12582     if (len > 8 && savepos + len > 2 * fsize)
12583     {
12584       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
12585       continue;
12586     }
12587     if (callbacks.exif_cb)
12588     {
12589       callbacks.exif_cb(callbacks.exifparser_data, tag | (pana_raw ? 0x30000 : ((ifd + 1) << 20)), type, len, order,
12590                         ifp);
12591       fseek(ifp, savepos, SEEK_SET);
12592     }
12593 #endif
12594 
12595 #ifdef LIBRAW_LIBRARY_BUILD
12596     if (!strncasecmp(make, "SONY", 4) ||
12597         (!strncasecmp(make, "Hasselblad", 10) &&
12598          (!strncasecmp(model, "Stellar", 7) || !strncasecmp(model, "Lunar", 5) || !strncasecmp(model, "HV", 2))))
12599     {
12600       switch (tag)
12601       {
12602       case 0x7300: // SR2 black level
12603         for (int i = 0; i < 4 && i < len; i++)
12604           cblack[i] = get2();
12605         break;
12606       case 0x7302:
12607         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c < 2)] = get2();
12608         break;
12609       case 0x7312:
12610       {
12611         int i, lc[4];
12612         FORC4 lc[c] = get2();
12613         i = (lc[1] == 1024 && lc[2] == 1024) << 1;
12614         SWAP(lc[i], lc[i + 1]);
12615         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c] = lc[c];
12616       }
12617       break;
12618       case 0x7480:
12619       case 0x7820:
12620         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][c] = get2();
12621         imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][1];
12622         break;
12623       case 0x7481:
12624       case 0x7821:
12625         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][c] = get2();
12626         imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][1];
12627         break;
12628       case 0x7482:
12629       case 0x7822:
12630         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][c] = get2();
12631         imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][1];
12632         break;
12633       case 0x7483:
12634       case 0x7823:
12635         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][c] = get2();
12636         imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][1];
12637         break;
12638       case 0x7484:
12639       case 0x7824:
12640         imgdata.color.WBCT_Coeffs[0][0] = 4500;
12641         FORC3 imgdata.color.WBCT_Coeffs[0][c + 1] = get2();
12642         imgdata.color.WBCT_Coeffs[0][4] = imgdata.color.WBCT_Coeffs[0][2];
12643         break;
12644       case 0x7486:
12645         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][c] = get2();
12646         imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][1];
12647         break;
12648       case 0x7825:
12649         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][c] = get2();
12650         imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][1];
12651         break;
12652       case 0x7826:
12653         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][c] = get2();
12654         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][1];
12655         break;
12656       case 0x7827:
12657         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][c] = get2();
12658         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][1];
12659         break;
12660       case 0x7828:
12661         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][c] = get2();
12662         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][1];
12663         break;
12664       case 0x7829:
12665         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_L][c] = get2();
12666         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_L][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_L][1];
12667         break;
12668       case 0x782a:
12669         imgdata.color.WBCT_Coeffs[1][0] = 8500;
12670         FORC3 imgdata.color.WBCT_Coeffs[1][c + 1] = get2();
12671         imgdata.color.WBCT_Coeffs[1][4] = imgdata.color.WBCT_Coeffs[1][2];
12672         break;
12673       case 0x782b:
12674         imgdata.color.WBCT_Coeffs[2][0] = 6000;
12675         FORC3 imgdata.color.WBCT_Coeffs[2][c + 1] = get2();
12676         imgdata.color.WBCT_Coeffs[2][4] = imgdata.color.WBCT_Coeffs[2][2];
12677         break;
12678       case 0x782c:
12679         imgdata.color.WBCT_Coeffs[3][0] = 3200;
12680         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_StudioTungsten][c] = imgdata.color.WBCT_Coeffs[3][c + 1] = get2();
12681         imgdata.color.WB_Coeffs[LIBRAW_WBI_StudioTungsten][3] = imgdata.color.WBCT_Coeffs[3][4] =
12682             imgdata.color.WB_Coeffs[LIBRAW_WBI_StudioTungsten][1];
12683         break;
12684       case 0x782d:
12685         imgdata.color.WBCT_Coeffs[4][0] = 2500;
12686         FORC3 imgdata.color.WBCT_Coeffs[4][c + 1] = get2();
12687         imgdata.color.WBCT_Coeffs[4][4] = imgdata.color.WBCT_Coeffs[4][2];
12688         break;
12689       case 0x787f:
12690         if (len == 3)
12691         {
12692           FORC3 imgdata.color.linear_max[c] = get2();
12693           imgdata.color.linear_max[3] = imgdata.color.linear_max[1];
12694         }
12695         else if (len == 1)
12696         {
12697           imgdata.color.linear_max[0] = imgdata.color.linear_max[1] = imgdata.color.linear_max[2] =
12698               imgdata.color.linear_max[3] = getreal(type); // Is non-short possible here??
12699         }
12700         break;
12701       }
12702     }
12703 #endif
12704 
12705     switch (tag)
12706     {
12707     case 1:
12708       if (len == 4)
12709         pana_raw = get4();
12710       break;
12711     case 5:
12712       width = get2();
12713       break;
12714     case 6:
12715       height = get2();
12716       break;
12717     case 7:
12718       width += get2();
12719       break;
12720     case 9:
12721       if ((i = get2()))
12722         filters = i;
12723       break;
12724 #ifdef LIBRAW_LIBRARY_BUILD
12725     case 10:
12726       if (pana_raw && len == 1 && type == 3)
12727       {
12728         pana_bpp = get2();
12729       }
12730     break;
12731 #endif
12732     case 14:
12733     case 15:
12734     case 16:
12735 #ifdef LIBRAW_LIBRARY_BUILD
12736       if (pana_raw)
12737       {
12738         imgdata.color.linear_max[tag - 14] = get2();
12739         if (tag == 15)
12740           imgdata.color.linear_max[3] = imgdata.color.linear_max[1];
12741       }
12742 #endif
12743       break;
12744     case 17:
12745     case 18:
12746       if (type == 3 && len == 1)
12747         cam_mul[(tag - 17) * 2] = get2() / 256.0;
12748       break;
12749 #ifdef LIBRAW_LIBRARY_BUILD
12750     case 19:
12751       if (pana_raw)
12752       {
12753         ushort nWB, cnt, tWB;
12754         nWB = get2();
12755         if (nWB > 0x100)
12756           break;
12757         for (cnt = 0; cnt < nWB; cnt++)
12758         {
12759           tWB = get2();
12760           if (tWB < 0x100)
12761           {
12762             imgdata.color.WB_Coeffs[tWB][0] = get2();
12763             imgdata.color.WB_Coeffs[tWB][2] = get2();
12764             imgdata.color.WB_Coeffs[tWB][1] = imgdata.color.WB_Coeffs[tWB][3] = 0x100;
12765           }
12766           else
12767             get4();
12768         }
12769       }
12770       break;
12771 
12772     case 0x0120:
12773       if (pana_raw)
12774       {
12775         unsigned sorder = order;
12776         unsigned long sbase = base;
12777         base = ftell(ifp);
12778         order = get2();
12779         fseek(ifp, 2, SEEK_CUR);
12780         fseek(ifp, get4()-8, SEEK_CUR);
12781         parse_tiff_ifd (base);
12782         base = sbase;
12783         order = sorder;
12784       }
12785     break;
12786 
12787     case 0x2009:
12788       if ((pana_encoding == 4) || (pana_encoding == 5))
12789       {
12790         int n = MIN (8, len);
12791         int permut[8] = {3, 2, 1, 0, 3+4, 2+4, 1+4, 0+4};
12792 
12793         imgdata.makernotes.panasonic.BlackLevelDim = len;
12794 
12795         for (int i=0; i < n; i++)
12796         {
12797           imgdata.makernotes.panasonic.BlackLevel[permut[i]] =
12798             (float) (get2()) / (float) (powf(2.f, 14.f-pana_bpp));
12799         }
12800       }
12801       break;
12802 #endif
12803 
12804     case 23:
12805       if (type == 3)
12806         iso_speed = get2();
12807       break;
12808     case 28:
12809     case 29:
12810     case 30:
12811 #ifdef LIBRAW_LIBRARY_BUILD
12812       if (pana_raw && len == 1 && type == 3)
12813       {
12814         pana_black[tag - 28] = get2();
12815       }
12816       else
12817 #endif
12818       {
12819         cblack[tag - 28] = get2();
12820         cblack[3] = cblack[1];
12821       }
12822       break;
12823     case 36:
12824     case 37:
12825     case 38:
12826       cam_mul[tag - 36] = get2();
12827       break;
12828     case 39:
12829 #ifdef LIBRAW_LIBRARY_BUILD
12830       if (pana_raw)
12831       {
12832         ushort nWB, cnt, tWB;
12833         nWB = get2();
12834         if (nWB > 0x100)
12835           break;
12836         for (cnt = 0; cnt < nWB; cnt++)
12837         {
12838           tWB = get2();
12839           if (tWB < 0x100)
12840           {
12841             imgdata.color.WB_Coeffs[tWB][0] = get2();
12842             imgdata.color.WB_Coeffs[tWB][1] = imgdata.color.WB_Coeffs[tWB][3] = get2();
12843             imgdata.color.WB_Coeffs[tWB][2] = get2();
12844           }
12845           else
12846             fseek(ifp, 6, SEEK_CUR);
12847         }
12848       }
12849       break;
12850 #endif
12851       if (len < 50 || cam_mul[0])
12852         break;
12853       fseek(ifp, 12, SEEK_CUR);
12854       FORC3 cam_mul[c] = get2();
12855       break;
12856 #ifdef LIBRAW_LIBRARY_BUILD
12857     case 45:
12858       if (pana_raw && len == 1 && type == 3)
12859       {
12860         pana_encoding = get2();
12861       }
12862       break;
12863 #endif
12864     case 46:
12865       if (type != 7 || fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8)
12866         break;
12867       thumb_offset = ftell(ifp) - 2;
12868       thumb_length = len;
12869       break;
12870     case 61440: /* Fuji HS10 table */
12871       fseek(ifp, get4() + base, SEEK_SET);
12872       parse_tiff_ifd(base);
12873       break;
12874     case 2:
12875     case 256:
12876     case 61441: /* ImageWidth */
12877       tiff_ifd[ifd].t_width = getint(type);
12878       break;
12879     case 3:
12880     case 257:
12881     case 61442: /* ImageHeight */
12882       tiff_ifd[ifd].t_height = getint(type);
12883       break;
12884     case 258: /* BitsPerSample */
12885     case 61443:
12886       tiff_ifd[ifd].samples = len & 7;
12887       tiff_ifd[ifd].bps = getint(type);
12888       if (tiff_bps < tiff_ifd[ifd].bps)
12889         tiff_bps = tiff_ifd[ifd].bps;
12890       break;
12891     case 61446:
12892       raw_height = 0;
12893       if (tiff_ifd[ifd].bps > 12)
12894         break;
12895       load_raw = &CLASS packed_load_raw;
12896       load_flags = get4() ? 24 : 80;
12897       break;
12898     case 259: /* Compression */
12899       tiff_ifd[ifd].comp = getint(type);
12900       break;
12901     case 262: /* PhotometricInterpretation */
12902       tiff_ifd[ifd].phint = get2();
12903       break;
12904     case 270: /* ImageDescription */
12905       fread(desc, 512, 1, ifp);
12906       break;
12907     case 271: /* Make */
12908       fgets(make, 64, ifp);
12909       break;
12910     case 272: /* Model */
12911       fgets(model, 64, ifp);
12912       break;
12913 #ifdef LIBRAW_LIBRARY_BUILD
12914     case 278:
12915       tiff_ifd[ifd].rows_per_strip = getint(type);
12916       break;
12917 #endif
12918     case 280: /* Panasonic RW2 offset */
12919       if (type != 4)
12920         break;
12921       load_raw = &CLASS panasonic_load_raw;
12922       load_flags = 0x2008;
12923     case 273: /* StripOffset */
12924 #ifdef LIBRAW_LIBRARY_BUILD
12925       if (len > 1 && len < 16384)
12926       {
12927         off_t sav = ftell(ifp);
12928         tiff_ifd[ifd].strip_offsets = (int *)calloc(len, sizeof(int));
12929         tiff_ifd[ifd].strip_offsets_count = len;
12930         for (int i = 0; i < len; i++)
12931           tiff_ifd[ifd].strip_offsets[i] = get4() + base;
12932         fseek(ifp, sav, SEEK_SET); // restore position
12933       }
12934 /* fallback */
12935 #endif
12936     case 513: /* JpegIFOffset */
12937     case 61447:
12938       tiff_ifd[ifd].offset = get4() + base;
12939       if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0)
12940       {
12941         fseek(ifp, tiff_ifd[ifd].offset, SEEK_SET);
12942         if (ljpeg_start(&jh, 1))
12943         {
12944           tiff_ifd[ifd].comp = 6;
12945           tiff_ifd[ifd].t_width = jh.wide;
12946           tiff_ifd[ifd].t_height = jh.high;
12947           tiff_ifd[ifd].bps = jh.bits;
12948           tiff_ifd[ifd].samples = jh.clrs;
12949           if (!(jh.sraw || (jh.clrs & 1)))
12950             tiff_ifd[ifd].t_width *= jh.clrs;
12951           if ((tiff_ifd[ifd].t_width > 4 * tiff_ifd[ifd].t_height) & ~jh.clrs)
12952           {
12953             tiff_ifd[ifd].t_width /= 2;
12954             tiff_ifd[ifd].t_height *= 2;
12955           }
12956           i = order;
12957           parse_tiff(tiff_ifd[ifd].offset + 12);
12958           order = i;
12959         }
12960       }
12961       break;
12962     case 274: /* Orientation */
12963       tiff_ifd[ifd].t_flip = "50132467"[get2() & 7] - '0';
12964       break;
12965     case 277: /* SamplesPerPixel */
12966       tiff_ifd[ifd].samples = getint(type) & 7;
12967       break;
12968     case 279: /* StripByteCounts */
12969 #ifdef LIBRAW_LIBRARY_BUILD
12970       if (len > 1 && len < 16384)
12971       {
12972         off_t sav = ftell(ifp);
12973         tiff_ifd[ifd].strip_byte_counts = (int *)calloc(len, sizeof(int));
12974         tiff_ifd[ifd].strip_byte_counts_count = len;
12975         for (int i = 0; i < len; i++)
12976           tiff_ifd[ifd].strip_byte_counts[i] = get4();
12977         fseek(ifp, sav, SEEK_SET); // restore position
12978       }
12979 /* fallback */
12980 #endif
12981     case 514:
12982     case 61448:
12983       tiff_ifd[ifd].bytes = get4();
12984       break;
12985     case 61454: // FujiFilm "As Shot"
12986       FORC3 cam_mul[(4 - c) % 3] = getint(type);
12987       break;
12988     case 305:
12989     case 11: /* Software */
12990       if ((pana_raw) && (tag == 11) && (type == 3))
12991       {
12992 #ifdef LIBRAW_LIBRARY_BUILD
12993         imgdata.makernotes.panasonic.Compression = get2();
12994 #endif
12995         break;
12996       }
12997       fgets(software, 64, ifp);
12998       if (!strncmp(software, "Adobe", 5) || !strncmp(software, "dcraw", 5) || !strncmp(software, "UFRaw", 5) ||
12999           !strncmp(software, "Bibble", 6) || !strcmp(software, "Digital Photo Professional"))
13000         is_raw = 0;
13001       break;
13002     case 306: /* DateTime */
13003       get_timestamp(0);
13004       break;
13005     case 315: /* Artist */
13006       fread(artist, 64, 1, ifp);
13007       break;
13008     case 317:
13009       tiff_ifd[ifd].predictor = getint(type);
13010       break;
13011     case 322: /* TileWidth */
13012       tiff_ifd[ifd].t_tile_width = getint(type);
13013       break;
13014     case 323: /* TileLength */
13015       tiff_ifd[ifd].t_tile_length = getint(type);
13016       break;
13017     case 324: /* TileOffsets */
13018       tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
13019       if (len == 1)
13020         tiff_ifd[ifd].t_tile_width = tiff_ifd[ifd].t_tile_length = 0;
13021       if (len == 4)
13022       {
13023         load_raw = &CLASS sinar_4shot_load_raw;
13024         is_raw = 5;
13025       }
13026       break;
13027     case 325:
13028       tiff_ifd[ifd].bytes = len > 1 ? ftell(ifp) : get4();
13029       break;
13030     case 330: /* SubIFDs */
13031       if (!strcmp(model, "DSLR-A100") && tiff_ifd[ifd].t_width == 3872)
13032       {
13033         load_raw = &CLASS sony_arw_load_raw;
13034         data_offset = get4() + base;
13035         ifd++;
13036 #ifdef LIBRAW_LIBRARY_BUILD
13037         if (ifd >= sizeof tiff_ifd / sizeof tiff_ifd[0])
13038           throw LIBRAW_EXCEPTION_IO_CORRUPT;
13039 #endif
13040         break;
13041       }
13042 #ifdef LIBRAW_LIBRARY_BUILD
13043       if (!strncmp(make, "Hasselblad", 10) && libraw_internal_data.unpacker_data.hasselblad_parser_flag)
13044       {
13045         fseek(ifp, ftell(ifp) + 4, SEEK_SET);
13046         fseek(ifp, get4() + base, SEEK_SET);
13047         parse_tiff_ifd(base);
13048         break;
13049       }
13050 #endif
13051       if (len > 1000)
13052         len = 1000; /* 1000 SubIFDs is enough */
13053       while (len--)
13054       {
13055         i = ftell(ifp);
13056         fseek(ifp, get4() + base, SEEK_SET);
13057         if (parse_tiff_ifd(base))
13058           break;
13059         fseek(ifp, i + 4, SEEK_SET);
13060       }
13061       break;
13062     case 339:
13063       tiff_ifd[ifd].sample_format = getint(type);
13064       break;
13065     case 400:
13066       strcpy(make, "Sarnoff");
13067       maximum = 0xfff;
13068       break;
13069 #ifdef LIBRAW_LIBRARY_BUILD
13070     case 700:
13071       if ((type == 1 || type == 2 || type == 6 || type == 7) && len > 1 && len < 5100000)
13072       {
13073         xmpdata = (char *)malloc(xmplen = len + 1);
13074         fread(xmpdata, len, 1, ifp);
13075         xmpdata[len] = 0;
13076       }
13077       break;
13078 #endif
13079     case 28688:
13080       FORC4 sony_curve[c + 1] = get2() >> 2 & 0xfff;
13081       for (i = 0; i < 5; i++)
13082         for (j = sony_curve[i] + 1; j <= sony_curve[i + 1]; j++)
13083           curve[j] = curve[j - 1] + (1 << i);
13084       break;
13085     case 29184:
13086       sony_offset = get4();
13087       break;
13088     case 29185:
13089       sony_length = get4();
13090       break;
13091     case 29217:
13092       sony_key = get4();
13093       break;
13094     case 29264:
13095       parse_minolta(ftell(ifp));
13096       raw_width = 0;
13097       break;
13098     case 29443:
13099       FORC4 cam_mul[c ^ (c < 2)] = get2();
13100       break;
13101     case 29459:
13102       FORC4 cam_mul[c] = get2();
13103       i = (cam_mul[1] == 1024 && cam_mul[2] == 1024) << 1;
13104       SWAP(cam_mul[i], cam_mul[i + 1])
13105       break;
13106 #ifdef LIBRAW_LIBRARY_BUILD
13107     case 30720: // Sony matrix, Sony_SR2SubIFD_0x7800
13108       for (i = 0; i < 3; i++)
13109       {
13110         float num = 0.0;
13111         for (c = 0; c < 3; c++)
13112         {
13113           imgdata.color.ccm[i][c] = (float)((short)get2());
13114           num += imgdata.color.ccm[i][c];
13115         }
13116         if (num > 0.01)
13117           FORC3 imgdata.color.ccm[i][c] = imgdata.color.ccm[i][c] / num;
13118       }
13119       break;
13120 #endif
13121     case 29456: // Sony black level, Sony_SR2SubIFD_0x7310, no more needs to be divided by 4
13122       FORC4 cblack[c ^ c >> 1] = get2();
13123       i = cblack[3];
13124       FORC3 if (i > cblack[c]) i = cblack[c];
13125       FORC4 cblack[c] -= i;
13126       black = i;
13127 
13128 #ifdef DCRAW_VERBOSE
13129       if (verbose)
13130         fprintf(stderr, _("...Sony black: %u cblack: %u %u %u %u\n"), black, cblack[0], cblack[1], cblack[2],
13131                 cblack[3]);
13132 #endif
13133       break;
13134     case 33405: /* Model2 */
13135       fgets(model2, 64, ifp);
13136       break;
13137     case 33421: /* CFARepeatPatternDim */
13138       if (get2() == 6 && get2() == 6)
13139         filters = 9;
13140       break;
13141     case 33422: /* CFAPattern */
13142       if (filters == 9)
13143       {
13144         FORC(36)((char *)xtrans)[c] = fgetc(ifp) & 3;
13145         break;
13146       }
13147     case 64777: /* Kodak P-series */
13148       if (len == 36)
13149       {
13150         filters = 9;
13151         colors = 3;
13152         FORC(36) xtrans[0][c] = fgetc(ifp) & 3;
13153       }
13154       else if (len > 0)
13155       {
13156         if ((plen = len) > 16)
13157           plen = 16;
13158         fread(cfa_pat, 1, plen, ifp);
13159         for (colors = cfa = i = 0; i < plen && colors < 4; i++)
13160         {
13161 	  if(cfa_pat[i] > 31) continue; // Skip wrong data
13162           colors += !(cfa & (1 << cfa_pat[i]));
13163           cfa |= 1 << cfa_pat[i];
13164         }
13165         if (cfa == 070)
13166           memcpy(cfa_pc, "\003\004\005", 3); /* CMY */
13167         if (cfa == 072)
13168           memcpy(cfa_pc, "\005\003\004\001", 4); /* GMCY */
13169         goto guess_cfa_pc;
13170       }
13171       break;
13172     case 33424:
13173     case 65024:
13174       fseek(ifp, get4() + base, SEEK_SET);
13175       parse_kodak_ifd(base);
13176       break;
13177     case 33434: /* ExposureTime */
13178       tiff_ifd[ifd].t_shutter = shutter = getreal(type);
13179       break;
13180     case 33437: /* FNumber */
13181       aperture = getreal(type);
13182       break;
13183 #ifdef LIBRAW_LIBRARY_BUILD
13184     // IB start
13185     case 0x9400:
13186       imgdata.other.exifAmbientTemperature = getreal(type);
13187       if ((imgdata.other.CameraTemperature > -273.15f) && (OlyID == 0x4434353933ULL)) // TG-5
13188         imgdata.other.CameraTemperature += imgdata.other.exifAmbientTemperature;
13189       break;
13190     case 0x9401:
13191       imgdata.other.exifHumidity = getreal(type);
13192       break;
13193     case 0x9402:
13194       imgdata.other.exifPressure = getreal(type);
13195       break;
13196     case 0x9403:
13197       imgdata.other.exifWaterDepth = getreal(type);
13198       break;
13199     case 0x9404:
13200       imgdata.other.exifAcceleration = getreal(type);
13201       break;
13202     case 0x9405:
13203       imgdata.other.exifCameraElevationAngle = getreal(type);
13204       break;
13205     case 0xa405: // FocalLengthIn35mmFormat
13206       imgdata.lens.FocalLengthIn35mmFormat = get2();
13207       break;
13208     case 0xa431: // BodySerialNumber
13209     case 0xc62f:
13210       stmread(imgdata.shootinginfo.BodySerial, len, ifp);
13211       break;
13212     case 0xa432: // LensInfo, 42034dec, Lens Specification per EXIF standard
13213       imgdata.lens.MinFocal = getreal(type);
13214       imgdata.lens.MaxFocal = getreal(type);
13215       imgdata.lens.MaxAp4MinFocal = getreal(type);
13216       imgdata.lens.MaxAp4MaxFocal = getreal(type);
13217       break;
13218     case 0xa435: // LensSerialNumber
13219       stmread(imgdata.lens.LensSerial, len, ifp);
13220       break;
13221     case 0xc630: // DNG LensInfo, Lens Specification per EXIF standard
13222       imgdata.lens.MinFocal = getreal(type);
13223       imgdata.lens.MaxFocal = getreal(type);
13224       imgdata.lens.MaxAp4MinFocal = getreal(type);
13225       imgdata.lens.MaxAp4MaxFocal = getreal(type);
13226       break;
13227     case 0xa433: // LensMake
13228       stmread(imgdata.lens.LensMake, len, ifp);
13229       break;
13230     case 0xa434: // LensModel
13231       stmread(imgdata.lens.Lens, len, ifp);
13232       if (!strncmp(imgdata.lens.Lens, "----", 4))
13233         imgdata.lens.Lens[0] = 0;
13234       break;
13235     case 0x9205:
13236       imgdata.lens.EXIF_MaxAp = libraw_powf64l(2.0f, (getreal(type) / 2.0f));
13237       break;
13238 // IB end
13239 #endif
13240     case 34306: /* Leaf white balance */
13241       FORC4
13242       {
13243 	int q = get2();
13244 	if(q > 0) cam_mul[c ^ 1] = 4096.0 / q;
13245       }
13246       break;
13247     case 34307: /* Leaf CatchLight color matrix */
13248       fread(software, 1, 7, ifp);
13249       if (strncmp(software, "MATRIX", 6))
13250         break;
13251       colors = 4;
13252       for (raw_color = i = 0; i < 3; i++)
13253       {
13254         FORC4 fscanf(ifp, "%f", &rgb_cam[i][c ^ 1]);
13255         if (!use_camera_wb)
13256           continue;
13257         num = 0;
13258         FORC4 num += rgb_cam[i][c];
13259         FORC4 rgb_cam[i][c] /= MAX(1, num);
13260       }
13261       break;
13262     case 34310: /* Leaf metadata */
13263       parse_mos(ftell(ifp));
13264     case 34303:
13265       strcpy(make, "Leaf");
13266       break;
13267     case 34665: /* EXIF tag */
13268       fseek(ifp, get4() + base, SEEK_SET);
13269       parse_exif(base);
13270       break;
13271     case 34853: /* GPSInfo tag */
13272     {
13273       unsigned pos;
13274       fseek(ifp, pos = (get4() + base), SEEK_SET);
13275       parse_gps(base);
13276 #ifdef LIBRAW_LIBRARY_BUILD
13277       fseek(ifp, pos, SEEK_SET);
13278       parse_gps_libraw(base);
13279 #endif
13280     }
13281     break;
13282     case 34675: /* InterColorProfile */
13283     case 50831: /* AsShotICCProfile */
13284       profile_offset = ftell(ifp);
13285       profile_length = len;
13286       break;
13287     case 37122: /* CompressedBitsPerPixel */
13288       kodak_cbpp = get4();
13289       break;
13290     case 37386: /* FocalLength */
13291       focal_len = getreal(type);
13292       break;
13293     case 37393: /* ImageNumber */
13294       shot_order = getint(type);
13295       break;
13296     case 37400: /* old Kodak KDC tag */
13297       for (raw_color = i = 0; i < 3; i++)
13298       {
13299         getreal(type);
13300         FORC3 rgb_cam[i][c] = getreal(type);
13301       }
13302       break;
13303     case 40976:
13304       strip_offset = get4();
13305       switch (tiff_ifd[ifd].comp)
13306       {
13307       case 32770:
13308         load_raw = &CLASS samsung_load_raw;
13309         break;
13310       case 32772:
13311         load_raw = &CLASS samsung2_load_raw;
13312         break;
13313       case 32773:
13314         load_raw = &CLASS samsung3_load_raw;
13315         break;
13316       }
13317       break;
13318     case 46275: /* Imacon tags */
13319       strcpy(make, "Imacon");
13320       data_offset = ftell(ifp);
13321       ima_len = len;
13322       break;
13323     case 46279:
13324       if (!ima_len)
13325         break;
13326       fseek(ifp, 38, SEEK_CUR);
13327     case 46274:
13328       fseek(ifp, 40, SEEK_CUR);
13329       raw_width = get4();
13330       raw_height = get4();
13331       left_margin = get4() & 7;
13332       width = raw_width - left_margin - (get4() & 7);
13333       top_margin = get4() & 7;
13334       height = raw_height - top_margin - (get4() & 7);
13335       if (raw_width == 7262 && ima_len == 234317952)
13336       {
13337         height = 5412;
13338         width = 7216;
13339         left_margin = 7;
13340         filters = 0;
13341       }
13342       else if (raw_width == 7262)
13343       {
13344         height = 5444;
13345         width = 7244;
13346         left_margin = 7;
13347       }
13348       fseek(ifp, 52, SEEK_CUR);
13349       FORC3 cam_mul[c] = getreal(11);
13350       fseek(ifp, 114, SEEK_CUR);
13351       flip = (get2() >> 7) * 90;
13352       if (width * height * 6 == ima_len)
13353       {
13354         if (flip % 180 == 90)
13355           SWAP(width, height);
13356         raw_width = width;
13357         raw_height = height;
13358         left_margin = top_margin = filters = flip = 0;
13359       }
13360       sprintf(model, "Ixpress %d-Mp", height * width / 1000000);
13361       load_raw = &CLASS imacon_full_load_raw;
13362       if (filters)
13363       {
13364         if (left_margin & 1)
13365           filters = 0x61616161;
13366         load_raw = &CLASS unpacked_load_raw;
13367       }
13368       maximum = 0xffff;
13369       break;
13370     case 50454: /* Sinar tag */
13371     case 50455:
13372       if (len < 1 || len > 2560000 || !(cbuf = (char *)malloc(len)))
13373         break;
13374 #ifndef LIBRAW_LIBRARY_BUILD
13375       fread(cbuf, 1, len, ifp);
13376 #else
13377       if (fread(cbuf, 1, len, ifp) != len)
13378         throw LIBRAW_EXCEPTION_IO_CORRUPT; // cbuf to be free'ed in recycle
13379 #endif
13380       cbuf[len - 1] = 0;
13381       for (cp = cbuf - 1; cp && cp < cbuf + len; cp = strchr(cp, '\n'))
13382         if (!strncmp(++cp, "Neutral ", 8))
13383           sscanf(cp + 8, "%f %f %f", cam_mul, cam_mul + 1, cam_mul + 2);
13384       free(cbuf);
13385       break;
13386     case 50458:
13387       if (!make[0])
13388         strcpy(make, "Hasselblad");
13389       break;
13390     case 50459: /* Hasselblad tag */
13391 #ifdef LIBRAW_LIBRARY_BUILD
13392       libraw_internal_data.unpacker_data.hasselblad_parser_flag = 1;
13393 #endif
13394       i = order;
13395       j = ftell(ifp);
13396       c = tiff_nifds;
13397       order = get2();
13398       fseek(ifp, j + (get2(), get4()), SEEK_SET);
13399       parse_tiff_ifd(j);
13400       maximum = 0xffff;
13401       tiff_nifds = c;
13402       order = i;
13403       break;
13404     case 50706: /* DNGVersion */
13405       FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
13406       if (!make[0])
13407         strcpy(make, "DNG");
13408       is_raw = 1;
13409       break;
13410     case 50708: /* UniqueCameraModel */
13411 #ifdef LIBRAW_LIBRARY_BUILD
13412       stmread(imgdata.color.UniqueCameraModel, len, ifp);
13413       imgdata.color.UniqueCameraModel[sizeof(imgdata.color.UniqueCameraModel) - 1] = 0;
13414 #endif
13415       if (model[0])
13416         break;
13417 #ifndef LIBRAW_LIBRARY_BUILD
13418       fgets(make, 64, ifp);
13419 #else
13420       strncpy(make, imgdata.color.UniqueCameraModel, MIN(len, sizeof(imgdata.color.UniqueCameraModel)));
13421 #endif
13422       if ((cp = strchr(make, ' ')))
13423       {
13424         strcpy(model, cp + 1);
13425         *cp = 0;
13426       }
13427       break;
13428     case 50710: /* CFAPlaneColor */
13429       if (filters == 9)
13430         break;
13431       if (len > 4)
13432         len = 4;
13433       colors = len;
13434       fread(cfa_pc, 1, colors, ifp);
13435     guess_cfa_pc:
13436       FORCC tab[cfa_pc[c]] = c;
13437       cdesc[c] = 0;
13438       for (i = 16; i--;)
13439         filters = filters << 2 | tab[cfa_pat[i % plen]];
13440       filters -= !filters;
13441       break;
13442     case 50711: /* CFALayout */
13443       if (get2() == 2)
13444         fuji_width = 1;
13445       break;
13446     case 291:
13447     case 50712: /* LinearizationTable */
13448 #ifdef LIBRAW_LIBRARY_BUILD
13449       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_LINTABLE;
13450       tiff_ifd[ifd].lineartable_offset = ftell(ifp);
13451       tiff_ifd[ifd].lineartable_len = len;
13452 #endif
13453       linear_table(len);
13454       break;
13455     case 50713: /* BlackLevelRepeatDim */
13456 #ifdef LIBRAW_LIBRARY_BUILD
13457       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
13458       tiff_ifd[ifd].dng_levels.dng_cblack[4] =
13459 #endif
13460           cblack[4] = get2();
13461 #ifdef LIBRAW_LIBRARY_BUILD
13462       tiff_ifd[ifd].dng_levels.dng_cblack[5] =
13463 #endif
13464           cblack[5] = get2();
13465       if (cblack[4] * cblack[5] > (sizeof(cblack) / sizeof(cblack[0]) - 6))
13466 #ifdef LIBRAW_LIBRARY_BUILD
13467         tiff_ifd[ifd].dng_levels.dng_cblack[4] = tiff_ifd[ifd].dng_levels.dng_cblack[5] =
13468 #endif
13469             cblack[4] = cblack[5] = 1;
13470       break;
13471 
13472 #ifdef LIBRAW_LIBRARY_BUILD
13473     case 0xf00d:
13474       if (strcmp(model, "X-A3")  &&
13475           strcmp(model, "X-A10") &&
13476           strcmp(model, "X-A5")  &&
13477           strcmp(model, "X-A20"))
13478       {
13479         FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][(4 - c) % 3] = getint(type);
13480         imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][1];
13481       }
13482       break;
13483     case 0xf00c:
13484       if (strcmp(model, "X-A3")  &&
13485           strcmp(model, "X-A10") &&
13486           strcmp(model, "X-A5")  &&
13487           strcmp(model, "X-A20"))
13488       {
13489         unsigned fwb[4];
13490         FORC4 fwb[c] = get4();
13491         if (fwb[3] < 0x100)
13492         {
13493           imgdata.color.WB_Coeffs[fwb[3]][0] = fwb[1];
13494           imgdata.color.WB_Coeffs[fwb[3]][1] = imgdata.color.WB_Coeffs[fwb[3]][3] = fwb[0];
13495           imgdata.color.WB_Coeffs[fwb[3]][2] = fwb[2];
13496           if ((fwb[3] == 17) && (libraw_internal_data.unpacker_data.lenRAFData > 3) &&
13497               (libraw_internal_data.unpacker_data.lenRAFData < 10240000))
13498           {
13499             INT64 f_save = ftell(ifp);
13500             ushort *rafdata = (ushort *)malloc(sizeof(ushort) * libraw_internal_data.unpacker_data.lenRAFData);
13501             fseek(ifp, libraw_internal_data.unpacker_data.posRAFData, SEEK_SET);
13502             fread(rafdata, sizeof(ushort), libraw_internal_data.unpacker_data.lenRAFData, ifp);
13503             fseek(ifp, f_save, SEEK_SET);
13504             int fj, found = 0;
13505             for (int fi = 0; fi < (libraw_internal_data.unpacker_data.lenRAFData - 3); fi++)
13506             {
13507               if ((fwb[0] == rafdata[fi]) && (fwb[1] == rafdata[fi + 1]) && (fwb[2] == rafdata[fi + 2]))
13508               {
13509                 if (rafdata[fi - 15] != fwb[0])
13510                   continue;
13511 
13512                 for (int wb_ind = 0, ofst = fi - 15; wb_ind < nFuji_wb_list1; wb_ind++, ofst += 3)
13513                 {
13514                   imgdata.color.WB_Coeffs[Fuji_wb_list1[wb_ind]][1] =
13515                       imgdata.color.WB_Coeffs[Fuji_wb_list1[wb_ind]][3] = rafdata[ofst];
13516                   imgdata.color.WB_Coeffs[Fuji_wb_list1[wb_ind]][0] = rafdata[ofst + 1];
13517                   imgdata.color.WB_Coeffs[Fuji_wb_list1[wb_ind]][2] = rafdata[ofst + 2];
13518                 }
13519                 fi += 0x60;
13520                 for (fj = fi; fj < (fi + 15); fj += 3)
13521                   if (rafdata[fj] != rafdata[fi])
13522                   {
13523                     found = 1;
13524                     break;
13525                   }
13526                 if (found)
13527                 {
13528                   fj = fj - 93;
13529                   for (int iCCT = 0; iCCT < 31; iCCT++)
13530                   {
13531                     imgdata.color.WBCT_Coeffs[iCCT][0] = FujiCCT_K[iCCT];
13532                     imgdata.color.WBCT_Coeffs[iCCT][1] = rafdata[iCCT * 3 + 1 + fj];
13533                     imgdata.color.WBCT_Coeffs[iCCT][2] = imgdata.color.WBCT_Coeffs[iCCT][4] = rafdata[iCCT * 3 + fj];
13534                     imgdata.color.WBCT_Coeffs[iCCT][3] = rafdata[iCCT * 3 + 2 + fj];
13535                   }
13536                 }
13537                 free(rafdata);
13538                 break;
13539               }
13540             }
13541           }
13542         }
13543         FORC4 fwb[c] = get4();
13544         if (fwb[3] < 0x100)
13545         {
13546           imgdata.color.WB_Coeffs[fwb[3]][0] = fwb[1];
13547           imgdata.color.WB_Coeffs[fwb[3]][1] = imgdata.color.WB_Coeffs[fwb[3]][3] = fwb[0];
13548           imgdata.color.WB_Coeffs[fwb[3]][2] = fwb[2];
13549         }
13550       }
13551       break;
13552 #endif
13553 
13554 #ifdef LIBRAW_LIBRARY_BUILD
13555     case 50709:
13556       stmread(imgdata.color.LocalizedCameraModel, len, ifp);
13557       break;
13558 #endif
13559 
13560     case 61450:
13561       cblack[4] = cblack[5] = MIN(sqrt((double)len), 64);
13562     case 50714: /* BlackLevel */
13563 #ifdef LIBRAW_LIBRARY_BUILD
13564       if (tiff_ifd[ifd].samples > 1 && tiff_ifd[ifd].samples == len) // LinearDNG, per-channel black
13565       {
13566         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
13567         for (i = 0; i < colors && i < 4 && i < len; i++)
13568           tiff_ifd[ifd].dng_levels.dng_cblack[i] = cblack[i] = getreal(type) + 0.5;
13569 
13570         tiff_ifd[ifd].dng_levels.dng_black = black = 0;
13571       }
13572       else
13573 #endif
13574           if ((cblack[4] * cblack[5] < 2) && len == 1)
13575       {
13576 #ifdef LIBRAW_LIBRARY_BUILD
13577         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
13578         tiff_ifd[ifd].dng_levels.dng_black =
13579 #endif
13580             black = getreal(type);
13581       }
13582       else if (cblack[4] * cblack[5] <= len)
13583       {
13584         FORC(cblack[4] * cblack[5])
13585         cblack[6 + c] = getreal(type);
13586         black = 0;
13587         FORC4
13588         cblack[c] = 0;
13589 
13590 #ifdef LIBRAW_LIBRARY_BUILD
13591         if (tag == 50714)
13592         {
13593           tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
13594           FORC(cblack[4] * cblack[5])
13595           tiff_ifd[ifd].dng_levels.dng_cblack[6 + c] = cblack[6 + c];
13596           tiff_ifd[ifd].dng_levels.dng_black = 0;
13597           FORC4
13598           tiff_ifd[ifd].dng_levels.dng_cblack[c] = 0;
13599         }
13600 #endif
13601       }
13602       break;
13603     case 50715: /* BlackLevelDeltaH */
13604     case 50716: /* BlackLevelDeltaV */
13605       for (num = i = 0; i < len && i < 65536; i++)
13606         num += getreal(type);
13607       if(len>0)
13608        {
13609         black += num / len + 0.5;
13610 #ifdef LIBRAW_LIBRARY_BUILD
13611       tiff_ifd[ifd].dng_levels.dng_black += num / len + 0.5;
13612       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
13613 #endif
13614        }
13615       break;
13616     case 50717: /* WhiteLevel */
13617 #ifdef LIBRAW_LIBRARY_BUILD
13618       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_WHITE;
13619       tiff_ifd[ifd].dng_levels.dng_whitelevel[0] =
13620 #endif
13621           maximum = getint(type);
13622 #ifdef LIBRAW_LIBRARY_BUILD
13623       if (tiff_ifd[ifd].samples > 1) // Linear DNG case
13624         for (i = 1; i < colors && i < 4 && i < len; i++)
13625           tiff_ifd[ifd].dng_levels.dng_whitelevel[i] = getint(type);
13626 #endif
13627       break;
13628     case 50718: /* DefaultScale */
13629       {
13630 	float q1 = getreal(type);
13631 	float q2 = getreal(type);
13632 	if(q1 > 0.00001f && q2 > 0.00001f)
13633 	 {
13634       		pixel_aspect = q1/q2;
13635       		if (pixel_aspect > 0.995 && pixel_aspect < 1.005)
13636         		pixel_aspect = 1.0;
13637 	 }
13638       }
13639       break;
13640 #ifdef LIBRAW_LIBRARY_BUILD
13641     case 50719: /* DefaultCropOrigin */
13642       if (len == 2)
13643       {
13644         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_CROPORIGIN;
13645         tiff_ifd[ifd].dng_levels.default_crop[0] = getreal(type);
13646         tiff_ifd[ifd].dng_levels.default_crop[1] = getreal(type);
13647         if (!strncasecmp(make, "SONY", 4))
13648         {
13649           imgdata.sizes.raw_crop.cleft = tiff_ifd[ifd].dng_levels.default_crop[0];
13650           imgdata.sizes.raw_crop.ctop = tiff_ifd[ifd].dng_levels.default_crop[1];
13651         }
13652       }
13653       break;
13654     case 50720: /* DefaultCropSize */
13655       if (len == 2)
13656       {
13657         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_CROPSIZE;
13658         tiff_ifd[ifd].dng_levels.default_crop[2] = getreal(type);
13659         tiff_ifd[ifd].dng_levels.default_crop[3] = getreal(type);
13660         if (!strncasecmp(make, "SONY", 4))
13661         {
13662           imgdata.sizes.raw_crop.cwidth = tiff_ifd[ifd].dng_levels.default_crop[2];
13663           imgdata.sizes.raw_crop.cheight = tiff_ifd[ifd].dng_levels.default_crop[3];
13664         }
13665       }
13666       break;
13667     case 0x74c7:
13668       if ((len == 2) && !strncasecmp(make, "SONY", 4))
13669       {
13670         imgdata.makernotes.sony.raw_crop.cleft = get4();
13671         imgdata.makernotes.sony.raw_crop.ctop = get4();
13672       }
13673       break;
13674     case 0x74c8:
13675       if ((len == 2) && !strncasecmp(make, "SONY", 4))
13676       {
13677         imgdata.makernotes.sony.raw_crop.cwidth = get4();
13678         imgdata.makernotes.sony.raw_crop.cheight = get4();
13679       }
13680       break;
13681 
13682 #endif
13683 #ifdef LIBRAW_LIBRARY_BUILD
13684     case 50778:
13685       tiff_ifd[ifd].dng_color[0].illuminant = get2();
13686       tiff_ifd[ifd].dng_color[0].parsedfields |= LIBRAW_DNGFM_ILLUMINANT;
13687       break;
13688     case 50779:
13689       tiff_ifd[ifd].dng_color[1].illuminant = get2();
13690       tiff_ifd[ifd].dng_color[1].parsedfields |= LIBRAW_DNGFM_ILLUMINANT;
13691       break;
13692 
13693 #endif
13694     case 50721: /* ColorMatrix1 */
13695     case 50722: /* ColorMatrix2 */
13696 #ifdef LIBRAW_LIBRARY_BUILD
13697       i = tag == 50721 ? 0 : 1;
13698       tiff_ifd[ifd].dng_color[i].parsedfields |= LIBRAW_DNGFM_COLORMATRIX;
13699 #endif
13700       FORCC for (j = 0; j < 3; j++)
13701       {
13702 #ifdef LIBRAW_LIBRARY_BUILD
13703         tiff_ifd[ifd].dng_color[i].colormatrix[c][j] =
13704 #endif
13705             cm[c][j] = getreal(type);
13706       }
13707       use_cm = 1;
13708       break;
13709 
13710     case 0xc714: /* ForwardMatrix1 */
13711     case 0xc715: /* ForwardMatrix2 */
13712 #ifdef LIBRAW_LIBRARY_BUILD
13713       i = tag == 0xc714 ? 0 : 1;
13714       tiff_ifd[ifd].dng_color[i].parsedfields |= LIBRAW_DNGFM_FORWARDMATRIX;
13715 #endif
13716       for (j = 0; j < 3; j++)
13717         FORCC
13718         {
13719 #ifdef LIBRAW_LIBRARY_BUILD
13720           tiff_ifd[ifd].dng_color[i].forwardmatrix[j][c] =
13721 #endif
13722               fm[j][c] = getreal(type);
13723         }
13724       break;
13725 
13726     case 50723: /* CameraCalibration1 */
13727     case 50724: /* CameraCalibration2 */
13728 #ifdef LIBRAW_LIBRARY_BUILD
13729       j = tag == 50723 ? 0 : 1;
13730       tiff_ifd[ifd].dng_color[j].parsedfields |= LIBRAW_DNGFM_CALIBRATION;
13731 #endif
13732       for (i = 0; i < colors; i++)
13733         FORCC
13734         {
13735 #ifdef LIBRAW_LIBRARY_BUILD
13736           tiff_ifd[ifd].dng_color[j].calibration[i][c] =
13737 #endif
13738               cc[i][c] = getreal(type);
13739         }
13740       break;
13741     case 50727: /* AnalogBalance */
13742 #ifdef LIBRAW_LIBRARY_BUILD
13743       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_ANALOGBALANCE;
13744 #endif
13745       FORCC
13746       {
13747 #ifdef LIBRAW_LIBRARY_BUILD
13748         tiff_ifd[ifd].dng_levels.analogbalance[c] =
13749 #endif
13750             ab[c] = getreal(type);
13751       }
13752       break;
13753     case 50728: /* AsShotNeutral */
13754       FORCC asn[c] = getreal(type);
13755       break;
13756     case 50729: /* AsShotWhiteXY */
13757       xyz[0] = getreal(type);
13758       xyz[1] = getreal(type);
13759       xyz[2] = 1 - xyz[0] - xyz[1];
13760       FORC3 xyz[c] /= d65_white[c];
13761       break;
13762 #ifdef LIBRAW_LIBRARY_BUILD
13763     case 50730: /* DNG: Baseline Exposure */
13764       baseline_exposure = getreal(type);
13765       break;
13766 #endif
13767     // IB start
13768     case 50740: /* tag 0xc634 : DNG Adobe, DNG Pentax, Sony SR2, DNG Private */
13769 #ifdef LIBRAW_LIBRARY_BUILD
13770     {
13771       char mbuf[64];
13772       unsigned short makernote_found = 0;
13773       INT64 curr_pos, start_pos = ftell(ifp);
13774       unsigned MakN_order, m_sorder = order;
13775       unsigned MakN_length;
13776       unsigned pos_in_original_raw;
13777       fread(mbuf, 1, 6, ifp);
13778 
13779       if (!strcmp(mbuf, "Adobe"))
13780       {
13781         order = 0x4d4d; // Adobe header is always in "MM" / big endian
13782         curr_pos = start_pos + 6;
13783         while (curr_pos + 8 - start_pos <= len)
13784         {
13785           fread(mbuf, 1, 4, ifp);
13786           curr_pos += 8;
13787           if (!strncmp(mbuf, "MakN", 4))
13788           {
13789             makernote_found = 1;
13790             MakN_length = get4();
13791             MakN_order = get2();
13792             pos_in_original_raw = get4();
13793             order = MakN_order;
13794 
13795             INT64 save_pos = ifp->tell();
13796             parse_makernote_0xc634(curr_pos + 6 - pos_in_original_raw, 0, AdobeDNG);
13797 
13798             curr_pos = save_pos + MakN_length - 6;
13799             fseek(ifp, curr_pos, SEEK_SET);
13800 
13801             fread(mbuf, 1, 4, ifp);
13802             curr_pos += 8;
13803             if (!strncmp(mbuf, "SR2 ", 4))
13804             {
13805               order = 0x4d4d;
13806               MakN_length = get4();
13807               MakN_order = get2();
13808               pos_in_original_raw = get4();
13809               order = MakN_order;
13810 
13811               unsigned *buf_SR2;
13812               uchar *cbuf_SR2;
13813               unsigned icbuf_SR2;
13814               unsigned entries, tag, type, len, save;
13815               int ival;
13816               unsigned SR2SubIFDOffset = 0;
13817               unsigned SR2SubIFDLength = 0;
13818               unsigned SR2SubIFDKey = 0;
13819               int base = curr_pos + 6 - pos_in_original_raw;
13820               entries = get2();
13821               while (entries--)
13822               {
13823                 tiff_get(base, &tag, &type, &len, &save);
13824 
13825                 if (tag == 0x7200)
13826                 {
13827                   SR2SubIFDOffset = get4();
13828                 }
13829                 else if (tag == 0x7201)
13830                 {
13831                   SR2SubIFDLength = get4();
13832                 }
13833                 else if (tag == 0x7221)
13834                 {
13835                   SR2SubIFDKey = get4();
13836                 }
13837                 fseek(ifp, save, SEEK_SET);
13838               }
13839 
13840               if (SR2SubIFDLength && (SR2SubIFDLength < 10240000) && (buf_SR2 = (unsigned *)malloc(SR2SubIFDLength+1024))) // 1024b for safety
13841               {
13842                 fseek(ifp, SR2SubIFDOffset + base, SEEK_SET);
13843                 fread(buf_SR2, SR2SubIFDLength, 1, ifp);
13844                 sony_decrypt(buf_SR2, SR2SubIFDLength / 4, 1, SR2SubIFDKey);
13845                 cbuf_SR2 = (uchar *)buf_SR2;
13846                 entries = sget2(cbuf_SR2);
13847                 icbuf_SR2 = 2;
13848                 while (entries--)
13849                 {
13850                   tag = sget2(cbuf_SR2 + icbuf_SR2);
13851                   icbuf_SR2 += 2;
13852                   type = sget2(cbuf_SR2 + icbuf_SR2);
13853                   icbuf_SR2 += 2;
13854                   len = sget4(cbuf_SR2 + icbuf_SR2);
13855                   icbuf_SR2 += 4;
13856 
13857                   if (len * ("11124811248484"[type < 14 ? type : 0] - '0') > 4)
13858                   {
13859                     ival = sget4(cbuf_SR2 + icbuf_SR2) - SR2SubIFDOffset;
13860                   }
13861                   else
13862                   {
13863                     ival = icbuf_SR2;
13864                   }
13865 		  if(ival > SR2SubIFDLength) // points out of orig. buffer size
13866 		     break; // END processing. Generally we should check against SR2SubIFDLength minus 6 of 8, depending on tag, but we allocated extra 1024b for buffer, so this does not matter
13867 
13868                   icbuf_SR2 += 4;
13869 
13870                   switch (tag)
13871                   {
13872                   case 0x7302:
13873                     FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c < 2)] = sget2(cbuf_SR2 + ival + 2 * c);
13874                     break;
13875                   case 0x7312:
13876                   {
13877                     int i, lc[4];
13878                     FORC4 lc[c] = sget2(cbuf_SR2 + ival + 2 * c);
13879                     i = (lc[1] == 1024 && lc[2] == 1024) << 1;
13880                     SWAP(lc[i], lc[i + 1]);
13881                     FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c] = lc[c];
13882                   }
13883                   break;
13884                   case 0x7480:
13885                   case 0x7820:
13886                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][c] = sget2(cbuf_SR2 + ival + 2 * c);
13887                     imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][1];
13888                     break;
13889                   case 0x7481:
13890                   case 0x7821:
13891                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][c] = sget2(cbuf_SR2 + ival + 2 * c);
13892                     imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][1];
13893                     break;
13894                   case 0x7482:
13895                   case 0x7822:
13896                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][c] = sget2(cbuf_SR2 + ival + 2 * c);
13897                     imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][1];
13898                     break;
13899                   case 0x7483:
13900                   case 0x7823:
13901                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][c] = sget2(cbuf_SR2 + ival + 2 * c);
13902                     imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][1];
13903                     break;
13904                   case 0x7484:
13905                   case 0x7824:
13906                     imgdata.color.WBCT_Coeffs[0][0] = 4500;
13907                     FORC3 imgdata.color.WBCT_Coeffs[0][c + 1] = sget2(cbuf_SR2 + ival + 2 * c);
13908                     imgdata.color.WBCT_Coeffs[0][4] = imgdata.color.WBCT_Coeffs[0][2];
13909                     break;
13910                   case 0x7486:
13911                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][c] = sget2(cbuf_SR2 + ival + 2 * c);
13912                     imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][3] =
13913                         imgdata.color.WB_Coeffs[LIBRAW_WBI_Fluorescent][1];
13914                     break;
13915                   case 0x7825:
13916                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][c] = sget2(cbuf_SR2 + ival + 2 * c);
13917                     imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][1];
13918                     break;
13919                   case 0x7826:
13920                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][c] = sget2(cbuf_SR2 + ival + 2 * c);
13921                     imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][1];
13922                     break;
13923                   case 0x7827:
13924                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][c] = sget2(cbuf_SR2 + ival + 2 * c);
13925                     imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][1];
13926                     break;
13927                   case 0x7828:
13928                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][c] = sget2(cbuf_SR2 + ival + 2 * c);
13929                     imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][1];
13930                     break;
13931                   case 0x7829:
13932                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_L][c] = sget2(cbuf_SR2 + ival + 2 * c);
13933                     imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_L][3] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_L][1];
13934                     break;
13935                   case 0x782a:
13936                     imgdata.color.WBCT_Coeffs[1][0] = 8500;
13937                     FORC3 imgdata.color.WBCT_Coeffs[1][c + 1] = sget2(cbuf_SR2 + ival + 2 * c);
13938                     imgdata.color.WBCT_Coeffs[1][4] = imgdata.color.WBCT_Coeffs[1][2];
13939                     break;
13940                   case 0x782b:
13941                     imgdata.color.WBCT_Coeffs[2][0] = 6000;
13942                     FORC3 imgdata.color.WBCT_Coeffs[2][c + 1] = sget2(cbuf_SR2 + ival + 2 * c);
13943                     imgdata.color.WBCT_Coeffs[2][4] = imgdata.color.WBCT_Coeffs[2][2];
13944                     break;
13945                   case 0x782c:
13946                     imgdata.color.WBCT_Coeffs[3][0] = 3200;
13947                     FORC3 imgdata.color.WB_Coeffs[LIBRAW_WBI_StudioTungsten][c] = imgdata.color.WBCT_Coeffs[3][c + 1] =
13948                         sget2(cbuf_SR2 + ival + 2 * c);
13949                     imgdata.color.WB_Coeffs[LIBRAW_WBI_StudioTungsten][3] = imgdata.color.WBCT_Coeffs[3][4] =
13950                         imgdata.color.WB_Coeffs[LIBRAW_WBI_StudioTungsten][1];
13951                     break;
13952                   case 0x782d:
13953                     imgdata.color.WBCT_Coeffs[4][0] = 2500;
13954                     FORC3 imgdata.color.WBCT_Coeffs[4][c + 1] = sget2(cbuf_SR2 + ival + 2 * c);
13955                     imgdata.color.WBCT_Coeffs[4][4] = imgdata.color.WBCT_Coeffs[4][2];
13956                     break;
13957                   }
13958                 }
13959 
13960                 free(buf_SR2);
13961               }
13962 
13963             } /* SR2 processed */
13964             break;
13965           }
13966         }
13967       }
13968       else
13969       {
13970         fread(mbuf + 6, 1, 2, ifp);
13971         if (!strcmp(mbuf, "PENTAX ") || !strcmp(mbuf, "SAMSUNG"))
13972         {
13973           makernote_found = 1;
13974           fseek(ifp, start_pos, SEEK_SET);
13975           parse_makernote_0xc634(base, 0, CameraDNG);
13976         }
13977       }
13978 
13979       fseek(ifp, start_pos, SEEK_SET);
13980       order = m_sorder;
13981     }
13982 // IB end
13983 #endif
13984       if (dng_version)
13985         break;
13986       parse_minolta(j = get4() + base);
13987       fseek(ifp, j, SEEK_SET);
13988       parse_tiff_ifd(base);
13989       break;
13990     case 50752:
13991       read_shorts(cr2_slice, 3);
13992       break;
13993     case 50829: /* ActiveArea */
13994       top_margin = getint(type);
13995       left_margin = getint(type);
13996       height = getint(type) - top_margin;
13997       width = getint(type) - left_margin;
13998       break;
13999     case 50830: /* MaskedAreas */
14000       for (i = 0; i < len && i < 32; i++)
14001         ((int *)mask)[i] = getint(type);
14002       black = 0;
14003       break;
14004 #ifdef LIBRAW_LIBRARY_BUILD
14005     case 50970: /* PreviewColorSpace */
14006       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_PREVIEWCS;
14007       tiff_ifd[ifd].dng_levels.preview_colorspace = getint(type);
14008       break;
14009 #endif
14010     case 51009: /* OpcodeList2 */
14011 #ifdef LIBRAW_LIBRARY_BUILD
14012       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_OPCODE2;
14013       tiff_ifd[ifd].opcode2_offset =
14014 #endif
14015           meta_offset = ftell(ifp);
14016       break;
14017     case 64772: /* Kodak P-series */
14018       if (len < 13)
14019         break;
14020       fseek(ifp, 16, SEEK_CUR);
14021       data_offset = get4();
14022       fseek(ifp, 28, SEEK_CUR);
14023       data_offset += get4();
14024       load_raw = &CLASS packed_load_raw;
14025       break;
14026     case 65026:
14027       if (type == 2)
14028         fgets(model2, 64, ifp);
14029     }
14030     fseek(ifp, save, SEEK_SET);
14031   }
14032   if (sony_length && sony_length < 10240000 && (buf = (unsigned *)malloc(sony_length)))
14033   {
14034     fseek(ifp, sony_offset, SEEK_SET);
14035     fread(buf, sony_length, 1, ifp);
14036     sony_decrypt(buf, sony_length / 4, 1, sony_key);
14037 #ifndef LIBRAW_LIBRARY_BUILD
14038     sfp = ifp;
14039     if ((ifp = tmpfile()))
14040     {
14041       fwrite(buf, sony_length, 1, ifp);
14042       fseek(ifp, 0, SEEK_SET);
14043       parse_tiff_ifd(-sony_offset);
14044       fclose(ifp);
14045     }
14046     ifp = sfp;
14047 #else
14048     if (!ifp->tempbuffer_open(buf, sony_length))
14049     {
14050       parse_tiff_ifd(-sony_offset);
14051       ifp->tempbuffer_close();
14052     }
14053 #endif
14054     free(buf);
14055   }
14056   for (i = 0; i < colors; i++)
14057     FORCC cc[i][c] *= ab[i];
14058   if (use_cm)
14059   {
14060     FORCC for (i = 0; i < 3; i++) for (cam_xyz[c][i] = j = 0; j < colors; j++) cam_xyz[c][i] +=
14061         cc[c][j] * cm[j][i] * xyz[i];
14062     cam_xyz_coeff(cmatrix, cam_xyz);
14063   }
14064   if (asn[0])
14065   {
14066     cam_mul[3] = 0;
14067     FORCC
14068      if(fabs(asn[c])>0.0001)
14069      	cam_mul[c] = 1 / asn[c];
14070   }
14071   if (!use_cm)
14072     FORCC if(fabs(cc[c][c])>0.0001) pre_mul[c] /= cc[c][c];
14073   return 0;
14074 }
14075 
parse_tiff(int base)14076 int CLASS parse_tiff(int base)
14077 {
14078   int doff;
14079   fseek(ifp, base, SEEK_SET);
14080   order = get2();
14081   if (order != 0x4949 && order != 0x4d4d)
14082     return 0;
14083   get2();
14084   while ((doff = get4()))
14085   {
14086     fseek(ifp, doff + base, SEEK_SET);
14087     if (parse_tiff_ifd(base))
14088       break;
14089   }
14090   return 1;
14091 }
14092 
apply_tiff()14093 void CLASS apply_tiff()
14094 {
14095   int max_samp = 0, ties = 0, raw = -1, thm = -1, i;
14096   unsigned long long ns, os;
14097   struct jhead jh;
14098 
14099   thumb_misc = 16;
14100   if (thumb_offset)
14101   {
14102     fseek(ifp, thumb_offset, SEEK_SET);
14103     if (ljpeg_start(&jh, 1))
14104     {
14105       if ((unsigned)jh.bits < 17 && (unsigned)jh.wide < 0x10000 && (unsigned)jh.high < 0x10000)
14106       {
14107         thumb_misc = jh.bits;
14108         thumb_width = jh.wide;
14109         thumb_height = jh.high;
14110       }
14111     }
14112   }
14113   for (i = tiff_nifds; i--;)
14114   {
14115     if (tiff_ifd[i].t_shutter)
14116       shutter = tiff_ifd[i].t_shutter;
14117     tiff_ifd[i].t_shutter = shutter;
14118   }
14119   for (i = 0; i < tiff_nifds; i++)
14120   {
14121     if( tiff_ifd[i].t_width < 1 ||  tiff_ifd[i].t_width > 65535
14122        || tiff_ifd[i].t_height < 1 || tiff_ifd[i].t_height > 65535)
14123           continue; /* wrong image dimensions */
14124     if (max_samp < tiff_ifd[i].samples)
14125       max_samp = tiff_ifd[i].samples;
14126     if (max_samp > 3)
14127       max_samp = 3;
14128     os = raw_width * raw_height;
14129     ns = tiff_ifd[i].t_width * tiff_ifd[i].t_height;
14130     if (tiff_bps)
14131     {
14132       os *= tiff_bps;
14133       ns *= tiff_ifd[i].bps;
14134     }
14135     if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
14136         unsigned(tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 && (unsigned)tiff_ifd[i].bps < 33 &&
14137         (unsigned)tiff_ifd[i].samples < 13 && ns && ((ns > os && (ties = 1)) || (ns == os && shot_select == ties++)))
14138     {
14139       raw_width = tiff_ifd[i].t_width;
14140       raw_height = tiff_ifd[i].t_height;
14141       tiff_bps = tiff_ifd[i].bps;
14142       tiff_compress = tiff_ifd[i].comp;
14143       data_offset = tiff_ifd[i].offset;
14144 #ifdef LIBRAW_LIBRARY_BUILD
14145       data_size = tiff_ifd[i].bytes;
14146 #endif
14147       tiff_flip = tiff_ifd[i].t_flip;
14148       tiff_samples = tiff_ifd[i].samples;
14149       tile_width = tiff_ifd[i].t_tile_width;
14150       tile_length = tiff_ifd[i].t_tile_length;
14151       shutter = tiff_ifd[i].t_shutter;
14152       raw = i;
14153     }
14154   }
14155   if (is_raw == 1 && ties)
14156     is_raw = ties;
14157   if (!tile_width)
14158     tile_width = INT_MAX;
14159   if (!tile_length)
14160     tile_length = INT_MAX;
14161   for (i = tiff_nifds; i--;)
14162     if (tiff_ifd[i].t_flip)
14163       tiff_flip = tiff_ifd[i].t_flip;
14164   if (raw >= 0 && !load_raw)
14165     switch (tiff_compress)
14166     {
14167     case 32767:
14168 #ifdef LIBRAW_LIBRARY_BUILD
14169       if (!dng_version && INT64(tiff_ifd[raw].bytes) == INT64(raw_width) * INT64(raw_height))
14170 #else
14171       if (tiff_ifd[raw].bytes == raw_width * raw_height)
14172 #endif
14173       {
14174         tiff_bps = 14;
14175         load_raw = &CLASS sony_arw2_load_raw;
14176         break;
14177       }
14178 #ifdef LIBRAW_LIBRARY_BUILD
14179       if (!dng_version && !strncasecmp(make, "Sony", 4) && INT64(tiff_ifd[raw].bytes) == INT64(raw_width) * INT64(raw_height) * 2ULL)
14180 #else
14181       if (!strncasecmp(make, "Sony", 4) && tiff_ifd[raw].bytes == raw_width * raw_height * 2)
14182 #endif
14183       {
14184         tiff_bps = 14;
14185         load_raw = &CLASS unpacked_load_raw;
14186         break;
14187       }
14188 #ifdef LIBRAW_LIBRARY_BUILD
14189       if (INT64(tiff_ifd[raw].bytes) * 8ULL != INT64(raw_width) * INT64(raw_height) * INT64(tiff_bps))
14190 #else
14191       if (tiff_ifd[raw].bytes * 8 != raw_width * raw_height * tiff_bps)
14192 #endif
14193       {
14194         raw_height += 8;
14195         load_raw = &CLASS sony_arw_load_raw;
14196         break;
14197       }
14198       load_flags = 79;
14199     case 32769:
14200       load_flags++;
14201     case 32770:
14202     case 32773:
14203       goto slr;
14204     case 0:
14205     case 1:
14206 #ifdef LIBRAW_LIBRARY_BUILD
14207       // Sony 14-bit uncompressed
14208       if (!dng_version && !strncasecmp(make, "Sony", 4) && INT64(tiff_ifd[raw].bytes) == INT64(raw_width) * INT64(raw_height) * 2ULL)
14209       {
14210         tiff_bps = 14;
14211         load_raw = &CLASS unpacked_load_raw;
14212         break;
14213       }
14214       if (!dng_version && !strncasecmp(make, "Sony", 4) && tiff_ifd[raw].samples == 4 &&
14215           INT64(tiff_ifd[raw].bytes) == INT64(raw_width) * INT64(raw_height) * 8ULL) // Sony ARQ
14216       {
14217         tiff_bps = 14;
14218         tiff_samples = 4;
14219         load_raw = &CLASS sony_arq_load_raw;
14220         filters = 0;
14221         strcpy(cdesc, "RGBG");
14222         break;
14223       }
14224       if (!strncasecmp(make, "Nikon", 5) && !strncmp(software, "Nikon Scan", 10))
14225       {
14226         load_raw = &CLASS nikon_coolscan_load_raw;
14227         raw_color = 1;
14228         filters = 0;
14229         break;
14230       }
14231       if (!strncmp(make, "OLYMPUS", 7) && INT64(tiff_ifd[raw].bytes) * 2ULL == INT64(raw_width) * INT64(raw_height) * 3ULL)
14232 #else
14233       if (!strncmp(make, "OLYMPUS", 7) && tiff_ifd[raw].bytes * 2 == raw_width * raw_height * 3)
14234 #endif
14235         load_flags = 24;
14236 #ifdef LIBRAW_LIBRARY_BUILD
14237       if (!dng_version && INT64(tiff_ifd[raw].bytes) * 5ULL == INT64(raw_width) * INT64(raw_height) * 8ULL)
14238 #else
14239       if (tiff_ifd[raw].bytes * 5 == raw_width * raw_height * 8)
14240 #endif
14241       {
14242         load_flags = 81;
14243         tiff_bps = 12;
14244       }
14245     slr:
14246       switch (tiff_bps)
14247       {
14248       case 8:
14249         load_raw = &CLASS eight_bit_load_raw;
14250         break;
14251       case 12:
14252         if (tiff_ifd[raw].phint == 2)
14253           load_flags = 6;
14254         load_raw = &CLASS packed_load_raw;
14255         break;
14256       case 14:
14257         load_flags = 0;
14258       case 16:
14259         load_raw = &CLASS unpacked_load_raw;
14260 #ifdef LIBRAW_LIBRARY_BUILD
14261         if (!strncmp(make, "OLYMPUS", 7) && INT64(tiff_ifd[raw].bytes) * 7ULL > INT64(raw_width) * INT64(raw_height))
14262 #else
14263         if (!strncmp(make, "OLYMPUS", 7) && tiff_ifd[raw].bytes * 7 > raw_width * raw_height)
14264 #endif
14265           load_raw = &CLASS olympus_load_raw;
14266       }
14267       break;
14268     case 6:
14269     case 7:
14270     case 99:
14271       load_raw = &CLASS lossless_jpeg_load_raw;
14272       break;
14273     case 262:
14274       load_raw = &CLASS kodak_262_load_raw;
14275       break;
14276     case 34713:
14277 #ifdef LIBRAW_LIBRARY_BUILD
14278       if ((INT64(raw_width) + 9ULL) / 10ULL * 16ULL * INT64(raw_height) == INT64(tiff_ifd[raw].bytes))
14279 #else
14280       if ((raw_width + 9) / 10 * 16 * raw_height == tiff_ifd[raw].bytes)
14281 #endif
14282       {
14283         load_raw = &CLASS packed_load_raw;
14284         load_flags = 1;
14285       }
14286 #ifdef LIBRAW_LIBRARY_BUILD
14287       else if (INT64(raw_width) * INT64(raw_height) * 3ULL == INT64(tiff_ifd[raw].bytes) * 2ULL)
14288 #else
14289       else if (raw_width * raw_height * 3 == tiff_ifd[raw].bytes * 2)
14290 #endif
14291       {
14292         load_raw = &CLASS packed_load_raw;
14293         if (model[0] == 'N')
14294           load_flags = 80;
14295       }
14296 #ifdef LIBRAW_LIBRARY_BUILD
14297       else if (INT64(raw_width) * INT64(raw_height) * 3ULL == INT64(tiff_ifd[raw].bytes))
14298 #else
14299       else if (raw_width * raw_height * 3 == tiff_ifd[raw].bytes)
14300 #endif
14301       {
14302         load_raw = &CLASS nikon_yuv_load_raw;
14303         gamma_curve(1 / 2.4, 12.92, 1, 4095);
14304         memset(cblack, 0, sizeof cblack);
14305         filters = 0;
14306       }
14307 #ifdef LIBRAW_LIBRARY_BUILD
14308       else if (INT64(raw_width) * INT64(raw_height) * 2ULL == INT64(tiff_ifd[raw].bytes))
14309 #else
14310       else if (raw_width * raw_height * 2 == tiff_ifd[raw].bytes)
14311 #endif
14312       {
14313         load_raw = &CLASS unpacked_load_raw;
14314         load_flags = 4;
14315         order = 0x4d4d;
14316       }
14317       else
14318 #ifdef LIBRAW_LIBRARY_BUILD
14319           if (INT64(raw_width) * INT64(raw_height) * 3ULL == INT64(tiff_ifd[raw].bytes) * 2ULL)
14320       {
14321         load_raw = &CLASS packed_load_raw;
14322         load_flags = 80;
14323       }
14324       else if (tiff_ifd[raw].rows_per_strip && tiff_ifd[raw].strip_offsets_count &&
14325                tiff_ifd[raw].strip_offsets_count == tiff_ifd[raw].strip_byte_counts_count)
14326       {
14327         int fit = 1;
14328         for (int i = 0; i < tiff_ifd[raw].strip_byte_counts_count - 1; i++) // all but last
14329           if (INT64(tiff_ifd[raw].strip_byte_counts[i]) * 2ULL != INT64(tiff_ifd[raw].rows_per_strip) * INT64(raw_width) * 3ULL)
14330           {
14331             fit = 0;
14332             break;
14333           }
14334         if (fit)
14335           load_raw = &CLASS nikon_load_striped_packed_raw;
14336         else
14337           load_raw = &CLASS nikon_load_raw; // fallback
14338       }
14339       else
14340 #endif
14341         load_raw = &CLASS nikon_load_raw;
14342       break;
14343     case 65535:
14344       load_raw = &CLASS pentax_load_raw;
14345       break;
14346     case 65000:
14347       switch (tiff_ifd[raw].phint)
14348       {
14349       case 2:
14350         load_raw = &CLASS kodak_rgb_load_raw;
14351         filters = 0;
14352         break;
14353       case 6:
14354         load_raw = &CLASS kodak_ycbcr_load_raw;
14355         filters = 0;
14356         break;
14357       case 32803:
14358         load_raw = &CLASS kodak_65000_load_raw;
14359       }
14360     case 32867:
14361     case 34892:
14362       break;
14363 #ifdef LIBRAW_LIBRARY_BUILD
14364     case 8:
14365       break;
14366 #endif
14367     default:
14368       is_raw = 0;
14369     }
14370   if (!dng_version)
14371     if (((tiff_samples == 3 && tiff_ifd[raw].bytes && tiff_bps != 14 && (tiff_compress & -16) != 32768) ||
14372          (tiff_bps == 8 && strncmp(make, "Phase", 5) && strncmp(make, "Leaf", 4) && !strcasestr(make, "Kodak") &&
14373           !strstr(model2, "DEBUG RAW"))) &&
14374         strncmp(software, "Nikon Scan", 10))
14375       is_raw = 0;
14376   for (i = 0; i < tiff_nifds; i++)
14377     if (i != raw &&
14378         (tiff_ifd[i].samples == max_samp || (tiff_ifd[i].comp == 7 && tiff_ifd[i].samples == 1)) /* Allow 1-bps JPEGs */
14379         && tiff_ifd[i].bps > 0 && tiff_ifd[i].bps < 33 && tiff_ifd[i].phint != 32803 && tiff_ifd[i].phint != 34892 &&
14380         unsigned(tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 &&
14381         tiff_ifd[i].t_width * tiff_ifd[i].t_height / (SQR(tiff_ifd[i].bps) + 1) >
14382             thumb_width * thumb_height / (SQR(thumb_misc) + 1) &&
14383         tiff_ifd[i].comp != 34892)
14384     {
14385       thumb_width = tiff_ifd[i].t_width;
14386       thumb_height = tiff_ifd[i].t_height;
14387       thumb_offset = tiff_ifd[i].offset;
14388       thumb_length = tiff_ifd[i].bytes;
14389       thumb_misc = tiff_ifd[i].bps;
14390       thm = i;
14391     }
14392   if (thm >= 0)
14393   {
14394     thumb_misc |= tiff_ifd[thm].samples << 5;
14395     switch (tiff_ifd[thm].comp)
14396     {
14397     case 0:
14398       write_thumb = &CLASS layer_thumb;
14399       break;
14400     case 1:
14401       if (tiff_ifd[thm].bps <= 8)
14402         write_thumb = &CLASS ppm_thumb;
14403       else if (!strncmp(make, "Imacon", 6))
14404         write_thumb = &CLASS ppm16_thumb;
14405       else
14406         thumb_load_raw = &CLASS kodak_thumb_load_raw;
14407       break;
14408     case 65000:
14409       thumb_load_raw = tiff_ifd[thm].phint == 6 ? &CLASS kodak_ycbcr_load_raw : &CLASS kodak_rgb_load_raw;
14410     }
14411   }
14412 }
14413 
parse_minolta(int base)14414 void CLASS parse_minolta(int base)
14415 {
14416   int save, tag, len, offset, high = 0, wide = 0, i, c;
14417   short sorder = order;
14418 
14419   fseek(ifp, base, SEEK_SET);
14420   if (fgetc(ifp) || fgetc(ifp) - 'M' || fgetc(ifp) - 'R')
14421     return;
14422   order = fgetc(ifp) * 0x101;
14423   offset = base + get4() + 8;
14424 #ifdef LIBRAW_LIBRARY_BUILD
14425   if(offset>ifp->size()-8) // At least 8 bytes for tag/len
14426     offset = ifp->size()-8;
14427 #endif
14428 
14429   while ((save = ftell(ifp)) < offset)
14430   {
14431     for (tag = i = 0; i < 4; i++)
14432       tag = tag << 8 | fgetc(ifp);
14433     len = get4();
14434     if(len < 0)
14435       return; // just ignore wrong len?? or raise bad file exception?
14436     switch (tag)
14437     {
14438     case 0x505244: /* PRD */
14439       fseek(ifp, 8, SEEK_CUR);
14440       high = get2();
14441       wide = get2();
14442       break;
14443 #ifdef LIBRAW_LIBRARY_BUILD
14444     case 0x524946: /* RIF */
14445       if (!strncasecmp(model, "DSLR-A100", 9))
14446       {
14447         fseek(ifp, 8, SEEK_CUR);
14448         imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][0] = get2();
14449         imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][2] = get2();
14450         imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][0] = get2();
14451         imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][2] = get2();
14452         imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][0] = get2();
14453         imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][2] = get2();
14454         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][0] = get2();
14455         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][2] = get2();
14456         imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][0] = get2();
14457         imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][2] = get2();
14458         get4();
14459         imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][0] = get2();
14460         imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][2] = get2();
14461         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][0] = get2();
14462         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][2] = get2();
14463         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][0] = get2();
14464         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][2] = get2();
14465         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_WW][0] = get2();
14466         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_WW][2] = get2();
14467         imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Daylight][3] =
14468             imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][3] =
14469                 imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][3] =
14470                     imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Cloudy][3] =
14471                         imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][3] =
14472                             imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][1] = imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][3] =
14473                                 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][1] =
14474                                     imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][3] =
14475                                         imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][1] =
14476                                             imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_W][3] =
14477                                                 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_WW][1] =
14478                                                     imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_WW][3] = 0x100;
14479       }
14480       break;
14481 #endif
14482     case 0x574247: /* WBG */
14483       get4();
14484       i = strcmp(model, "DiMAGE A200") ? 0 : 3;
14485       FORC4 cam_mul[c ^ (c >> 1) ^ i] = get2();
14486       break;
14487     case 0x545457: /* TTW */
14488       parse_tiff(ftell(ifp));
14489       data_offset = offset;
14490     }
14491     fseek(ifp, save + len + 8, SEEK_SET);
14492   }
14493   raw_height = high;
14494   raw_width = wide;
14495   order = sorder;
14496 }
14497 
14498 /*
14499    Many cameras have a "debug mode" that writes JPEG and raw
14500    at the same time.  The raw file has no header, so try to
14501    to open the matching JPEG file and read its metadata.
14502  */
parse_external_jpeg()14503 void CLASS parse_external_jpeg()
14504 {
14505   const char *file, *ext;
14506   char *jname, *jfile, *jext;
14507 #ifndef LIBRAW_LIBRARY_BUILD
14508   FILE *save = ifp;
14509 #else
14510 #if defined(_WIN32) && !defined(__MINGW32__) && defined(_MSC_VER) && (_MSC_VER > 1310)
14511   if (ifp->wfname())
14512   {
14513     std::wstring rawfile(ifp->wfname());
14514     rawfile.replace(rawfile.length() - 3, 3, L"JPG");
14515     if (!ifp->subfile_open(rawfile.c_str()))
14516     {
14517       parse_tiff(12);
14518       thumb_offset = 0;
14519       is_raw = 1;
14520       ifp->subfile_close();
14521     }
14522     else
14523       imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA;
14524     return;
14525   }
14526 #endif
14527   if (!ifp->fname())
14528   {
14529     imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA;
14530     return;
14531   }
14532 #endif
14533 
14534   ext = strrchr(ifname, '.');
14535   file = strrchr(ifname, '/');
14536   if (!file)
14537     file = strrchr(ifname, '\\');
14538 #ifndef LIBRAW_LIBRARY_BUILD
14539   if (!file)
14540     file = ifname - 1;
14541 #else
14542   if (!file)
14543     file = (char *)ifname - 1;
14544 #endif
14545   file++;
14546   if (!ext || strlen(ext) != 4 || ext - file != 8)
14547     return;
14548   jname = (char *)malloc(strlen(ifname) + 1);
14549   merror(jname, "parse_external_jpeg()");
14550   strcpy(jname, ifname);
14551   jfile = file - ifname + jname;
14552   jext = ext - ifname + jname;
14553   if (strcasecmp(ext, ".jpg"))
14554   {
14555     strcpy(jext, isupper(ext[1]) ? ".JPG" : ".jpg");
14556     if (isdigit(*file))
14557     {
14558       memcpy(jfile, file + 4, 4);
14559       memcpy(jfile + 4, file, 4);
14560     }
14561   }
14562   else
14563     while (isdigit(*--jext))
14564     {
14565       if (*jext != '9')
14566       {
14567         (*jext)++;
14568         break;
14569       }
14570       *jext = '0';
14571     }
14572 #ifndef LIBRAW_LIBRARY_BUILD
14573   if (strcmp(jname, ifname))
14574   {
14575     if ((ifp = fopen(jname, "rb")))
14576     {
14577 #ifdef DCRAW_VERBOSE
14578       if (verbose)
14579         fprintf(stderr, _("Reading metadata from %s ...\n"), jname);
14580 #endif
14581       parse_tiff(12);
14582       thumb_offset = 0;
14583       is_raw = 1;
14584       fclose(ifp);
14585     }
14586   }
14587 #else
14588   if (strcmp(jname, ifname))
14589   {
14590     if (!ifp->subfile_open(jname))
14591     {
14592       parse_tiff(12);
14593       thumb_offset = 0;
14594       is_raw = 1;
14595       ifp->subfile_close();
14596     }
14597     else
14598       imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA;
14599   }
14600 #endif
14601   if (!timestamp)
14602   {
14603 #ifdef LIBRAW_LIBRARY_BUILD
14604     imgdata.process_warnings |= LIBRAW_WARN_NO_METADATA;
14605 #endif
14606 #ifdef DCRAW_VERBOSE
14607     fprintf(stderr, _("Failed to read metadata from %s\n"), jname);
14608 #endif
14609   }
14610   free(jname);
14611 #ifndef LIBRAW_LIBRARY_BUILD
14612   ifp = save;
14613 #endif
14614 }
14615 
14616 /*
14617    CIFF block 0x1030 contains an 8x8 white sample.
14618    Load this into white[][] for use in scale_colors().
14619  */
ciff_block_1030()14620 void CLASS ciff_block_1030()
14621 {
14622   static const ushort key[] = {0x410, 0x45f3};
14623   int i, bpp, row, col, vbits = 0;
14624   unsigned long bitbuf = 0;
14625 
14626   if ((get2(), get4()) != 0x80008 || !get4())
14627     return;
14628   bpp = get2();
14629   if (bpp != 10 && bpp != 12)
14630     return;
14631   for (i = row = 0; row < 8; row++)
14632     for (col = 0; col < 8; col++)
14633     {
14634       if (vbits < bpp)
14635       {
14636         bitbuf = bitbuf << 16 | (get2() ^ key[i++ & 1]);
14637         vbits += 16;
14638       }
14639       white[row][col] = bitbuf >> (vbits -= bpp) & ~(-1 << bpp);
14640     }
14641 }
14642 
14643 /*
14644    Parse a CIFF file, better known as Canon CRW format.
14645  */
14646 
parse_ciff(int offset,int length,int depth)14647 void CLASS parse_ciff(int offset, int length, int depth)
14648 {
14649   int tboff, nrecs, c, type, len, save, wbi = -1;
14650   ushort key[] = {0x410, 0x45f3};
14651 
14652   fseek(ifp, offset + length - 4, SEEK_SET);
14653   tboff = get4() + offset;
14654   fseek(ifp, tboff, SEEK_SET);
14655   nrecs = get2();
14656   if ((nrecs | depth) > 127)
14657     return;
14658   while (nrecs--)
14659   {
14660     type = get2();
14661     len = get4();
14662     save = ftell(ifp) + 4;
14663     fseek(ifp, offset + get4(), SEEK_SET);
14664     if ((((type >> 8) + 8) | 8) == 0x38)
14665     {
14666       parse_ciff(ftell(ifp), len, depth + 1); /* Parse a sub-table */
14667     }
14668 #ifdef LIBRAW_LIBRARY_BUILD
14669     if (type == 0x3004)
14670       parse_ciff(ftell(ifp), len, depth + 1);
14671 #endif
14672     if (type == 0x0810)
14673       fread(artist, 64, 1, ifp);
14674     if (type == 0x080a)
14675     {
14676       fread(make, 64, 1, ifp);
14677       fseek(ifp, strbuflen(make) - 63, SEEK_CUR);
14678       fread(model, 64, 1, ifp);
14679     }
14680     if (type == 0x1810)
14681     {
14682       width = get4();
14683       height = get4();
14684       pixel_aspect = int_to_float(get4());
14685       flip = get4();
14686     }
14687     if (type == 0x1835) /* Get the decoder table */
14688       tiff_compress = get4();
14689     if (type == 0x2007)
14690     {
14691       thumb_offset = ftell(ifp);
14692       thumb_length = len;
14693     }
14694     if (type == 0x1818)
14695     {
14696       shutter = libraw_powf64l(2.0f, -int_to_float((get4(), get4())));
14697       aperture = libraw_powf64l(2.0f, int_to_float(get4()) / 2);
14698 #ifdef LIBRAW_LIBRARY_BUILD
14699       imgdata.lens.makernotes.CurAp = aperture;
14700 #endif
14701     }
14702     if (type == 0x102a)
14703     {
14704       //      iso_speed = pow (2.0, (get4(),get2())/32.0 - 4) * 50;
14705       iso_speed = libraw_powf64l(2.0f, ((get2(), get2()) + get2()) / 32.0f - 5.0f) * 100.0f;
14706 #ifdef LIBRAW_LIBRARY_BUILD
14707       aperture = _CanonConvertAperture((get2(), get2()));
14708       imgdata.lens.makernotes.CurAp = aperture;
14709 #else
14710       aperture = libraw_powf64l(2.0, (get2(), (short)get2()) / 64.0);
14711 #endif
14712       shutter = libraw_powf64l(2.0, -((short)get2()) / 32.0);
14713       wbi = (get2(), get2());
14714       if (wbi > 17)
14715         wbi = 0;
14716       fseek(ifp, 32, SEEK_CUR);
14717       if (shutter > 1e6)
14718         shutter = get2() / 10.0;
14719     }
14720     if (type == 0x102c)
14721     {
14722       if (get2() > 512)
14723       { /* Pro90, G1 */
14724         fseek(ifp, 118, SEEK_CUR);
14725         FORC4 cam_mul[c ^ 2] = get2();
14726       }
14727       else
14728       { /* G2, S30, S40 */
14729         fseek(ifp, 98, SEEK_CUR);
14730         FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2();
14731       }
14732     }
14733 #ifdef LIBRAW_LIBRARY_BUILD
14734     if (type == 0x10a9)
14735     {
14736       INT64 o = ftell(ifp);
14737       fseek(ifp, (0x1 << 1), SEEK_CUR);
14738       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ (c >> 1)] = get2();
14739       Canon_WBpresets(0, 0);
14740       fseek(ifp, o, SEEK_SET);
14741     }
14742     if (type == 0x102d)
14743     {
14744       INT64 o = ftell(ifp);
14745       Canon_CameraSettings();
14746       fseek(ifp, o, SEEK_SET);
14747     }
14748     if (type == 0x580b)
14749     {
14750       if (strcmp(model, "Canon EOS D30"))
14751         sprintf(imgdata.shootinginfo.BodySerial, "%d", len);
14752       else
14753         sprintf(imgdata.shootinginfo.BodySerial, "%0x-%05d", len >> 16, len & 0xffff);
14754     }
14755 #endif
14756     if (type == 0x0032)
14757     {
14758       if (len == 768)
14759       { /* EOS D30 */
14760         fseek(ifp, 72, SEEK_CUR);
14761         FORC4
14762         {
14763           ushort q = get2();
14764           cam_mul[c ^ (c >> 1)] = 1024.0/ MAX(1,q);
14765         }
14766         if (!wbi)
14767           cam_mul[0] = -1; /* use my auto white balance */
14768       }
14769       else if (!cam_mul[0])
14770       {
14771         if (get2() == key[0]) /* Pro1, G6, S60, S70 */
14772           c = (strstr(model, "Pro1") ? "012346000000000000" : "01345:000000006008")[LIM(0, wbi, 17)] - '0' + 2;
14773         else
14774         { /* G3, G5, S45, S50 */
14775           c = "023457000000006000"[LIM(0, wbi, 17)] - '0';
14776           key[0] = key[1] = 0;
14777         }
14778         fseek(ifp, 78 + c * 8, SEEK_CUR);
14779         FORC4 cam_mul[c ^ (c >> 1) ^ 1] = get2() ^ key[c & 1];
14780         if (!wbi)
14781           cam_mul[0] = -1;
14782       }
14783     }
14784     if (type == 0x10a9)
14785     { /* D60, 10D, 300D, and clones */
14786       if (len > 66)
14787         wbi = "0134567028"[LIM(0, wbi, 9)] - '0';
14788       fseek(ifp, 2 + wbi * 8, SEEK_CUR);
14789       FORC4 cam_mul[c ^ (c >> 1)] = get2();
14790     }
14791     if (type == 0x1030 && wbi >= 0 && (0x18040 >> wbi & 1))
14792       ciff_block_1030(); /* all that don't have 0x10a9 */
14793     if (type == 0x1031)
14794     {
14795       raw_width = (get2(), get2());
14796       raw_height = get2();
14797     }
14798     if (type == 0x501c)
14799     {
14800       iso_speed = len & 0xffff;
14801     }
14802     if (type == 0x5029)
14803     {
14804 #ifdef LIBRAW_LIBRARY_BUILD
14805       imgdata.lens.makernotes.CurFocal = len >> 16;
14806       imgdata.lens.makernotes.FocalType = len & 0xffff;
14807       if (imgdata.lens.makernotes.FocalType == 2)
14808       {
14809         imgdata.lens.makernotes.CanonFocalUnits = 32;
14810         if (imgdata.lens.makernotes.CanonFocalUnits > 1)
14811           imgdata.lens.makernotes.CurFocal /= (float)imgdata.lens.makernotes.CanonFocalUnits;
14812       }
14813       focal_len = imgdata.lens.makernotes.CurFocal;
14814 #else
14815       focal_len = len >> 16;
14816       if ((len & 0xffff) == 2)
14817         focal_len /= 32;
14818 #endif
14819     }
14820     if (type == 0x5813)
14821       flash_used = int_to_float(len);
14822     if (type == 0x5814)
14823       canon_ev = int_to_float(len);
14824     if (type == 0x5817)
14825       shot_order = len;
14826     if (type == 0x5834)
14827     {
14828       unique_id = len;
14829 #ifdef LIBRAW_LIBRARY_BUILD
14830       unique_id = setCanonBodyFeatures(unique_id);
14831 #endif
14832     }
14833     if (type == 0x580e)
14834       timestamp = len;
14835     if (type == 0x180e)
14836       timestamp = get4();
14837 #ifdef LOCALTIME
14838     if ((type | 0x4000) == 0x580e)
14839       timestamp = mktime(gmtime(&timestamp));
14840 #endif
14841     fseek(ifp, save, SEEK_SET);
14842   }
14843 }
14844 
parse_rollei()14845 void CLASS parse_rollei()
14846 {
14847   char line[128], *val;
14848   struct tm t;
14849 
14850   fseek(ifp, 0, SEEK_SET);
14851   memset(&t, 0, sizeof t);
14852   do
14853   {
14854     fgets(line, 128, ifp);
14855     if ((val = strchr(line, '=')))
14856       *val++ = 0;
14857     else
14858       val = line + strbuflen(line);
14859     if (!strcmp(line, "DAT"))
14860       sscanf(val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
14861     if (!strcmp(line, "TIM"))
14862       sscanf(val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
14863     if (!strcmp(line, "HDR"))
14864       thumb_offset = atoi(val);
14865     if (!strcmp(line, "X  "))
14866       raw_width = atoi(val);
14867     if (!strcmp(line, "Y  "))
14868       raw_height = atoi(val);
14869     if (!strcmp(line, "TX "))
14870       thumb_width = atoi(val);
14871     if (!strcmp(line, "TY "))
14872       thumb_height = atoi(val);
14873   } while (strncmp(line, "EOHD", 4));
14874   data_offset = thumb_offset + thumb_width * thumb_height * 2;
14875   t.tm_year -= 1900;
14876   t.tm_mon -= 1;
14877   if (mktime(&t) > 0)
14878     timestamp = mktime(&t);
14879   strcpy(make, "Rollei");
14880   strcpy(model, "d530flex");
14881   write_thumb = &CLASS rollei_thumb;
14882 }
14883 
parse_sinar_ia()14884 void CLASS parse_sinar_ia()
14885 {
14886   int entries, off;
14887   char str[8], *cp;
14888 
14889   order = 0x4949;
14890   fseek(ifp, 4, SEEK_SET);
14891   entries = get4();
14892   fseek(ifp, get4(), SEEK_SET);
14893   while (entries--)
14894   {
14895     off = get4();
14896     get4();
14897     fread(str, 8, 1, ifp);
14898     if (!strcmp(str, "META"))
14899       meta_offset = off;
14900     if (!strcmp(str, "THUMB"))
14901       thumb_offset = off;
14902     if (!strcmp(str, "RAW0"))
14903       data_offset = off;
14904   }
14905   fseek(ifp, meta_offset + 20, SEEK_SET);
14906   fread(make, 64, 1, ifp);
14907   make[63] = 0;
14908   if ((cp = strchr(make, ' ')))
14909   {
14910     strcpy(model, cp + 1);
14911     *cp = 0;
14912   }
14913   raw_width = get2();
14914   raw_height = get2();
14915   load_raw = &CLASS unpacked_load_raw;
14916   thumb_width = (get4(), get2());
14917   thumb_height = get2();
14918   write_thumb = &CLASS ppm_thumb;
14919   maximum = 0x3fff;
14920 }
14921 
parse_phase_one(int base)14922 void CLASS parse_phase_one(int base)
14923 {
14924   unsigned entries, tag, type, len, data, save, i, c;
14925   float romm_cam[3][3];
14926   char *cp;
14927 
14928   memset(&ph1, 0, sizeof ph1);
14929   fseek(ifp, base, SEEK_SET);
14930   order = get4() & 0xffff;
14931   if (get4() >> 8 != 0x526177)
14932     return; /* "Raw" */
14933   fseek(ifp, get4() + base, SEEK_SET);
14934   entries = get4();
14935   get4();
14936   while (entries--)
14937   {
14938     tag = get4();
14939     type = get4();
14940     len = get4();
14941     data = get4();
14942     save = ftell(ifp);
14943     fseek(ifp, base + data, SEEK_SET);
14944     switch (tag)
14945     {
14946 
14947 #ifdef LIBRAW_LIBRARY_BUILD
14948     case 0x0102:
14949       stmread(imgdata.shootinginfo.BodySerial, len, ifp);
14950       if ((imgdata.shootinginfo.BodySerial[0] == 0x4c) && (imgdata.shootinginfo.BodySerial[1] == 0x49))
14951       {
14952         unique_id =
14953             (((imgdata.shootinginfo.BodySerial[0] & 0x3f) << 5) | (imgdata.shootinginfo.BodySerial[2] & 0x3f)) - 0x41;
14954       }
14955       else
14956       {
14957         unique_id =
14958             (((imgdata.shootinginfo.BodySerial[0] & 0x3f) << 5) | (imgdata.shootinginfo.BodySerial[1] & 0x3f)) - 0x41;
14959       }
14960       setPhaseOneFeatures(unique_id);
14961       break;
14962     case 0x0211:
14963       imgdata.other.SensorTemperature2 = int_to_float(data);
14964       break;
14965     case 0x0401:
14966       if (type == 4)
14967         imgdata.lens.makernotes.CurAp = libraw_powf64l(2.0f, (int_to_float(data) / 2.0f));
14968       else
14969         imgdata.lens.makernotes.CurAp = libraw_powf64l(2.0f, (getreal(type) / 2.0f));
14970       break;
14971     case 0x0403:
14972       if (type == 4)
14973         imgdata.lens.makernotes.CurFocal = int_to_float(data);
14974       else
14975         imgdata.lens.makernotes.CurFocal = getreal(type);
14976       break;
14977     case 0x0410:
14978       stmread(imgdata.lens.makernotes.body, len, ifp);
14979       break;
14980     case 0x0412:
14981       stmread(imgdata.lens.makernotes.Lens, len, ifp);
14982       break;
14983     case 0x0414:
14984       if (type == 4)
14985       {
14986         imgdata.lens.makernotes.MaxAp4CurFocal = libraw_powf64l(2.0f, (int_to_float(data) / 2.0f));
14987       }
14988       else
14989       {
14990         imgdata.lens.makernotes.MaxAp4CurFocal = libraw_powf64l(2.0f, (getreal(type) / 2.0f));
14991       }
14992       break;
14993     case 0x0415:
14994       if (type == 4)
14995       {
14996         imgdata.lens.makernotes.MinAp4CurFocal = libraw_powf64l(2.0f, (int_to_float(data) / 2.0f));
14997       }
14998       else
14999       {
15000         imgdata.lens.makernotes.MinAp4CurFocal = libraw_powf64l(2.0f, (getreal(type) / 2.0f));
15001       }
15002       break;
15003     case 0x0416:
15004       if (type == 4)
15005       {
15006         imgdata.lens.makernotes.MinFocal = int_to_float(data);
15007       }
15008       else
15009       {
15010         imgdata.lens.makernotes.MinFocal = getreal(type);
15011       }
15012       if (imgdata.lens.makernotes.MinFocal > 1000.0f)
15013       {
15014         imgdata.lens.makernotes.MinFocal = 0.0f;
15015       }
15016       break;
15017     case 0x0417:
15018       if (type == 4)
15019       {
15020         imgdata.lens.makernotes.MaxFocal = int_to_float(data);
15021       }
15022       else
15023       {
15024         imgdata.lens.makernotes.MaxFocal = getreal(type);
15025       }
15026       break;
15027 #endif
15028 
15029     case 0x100:
15030       flip = "0653"[data & 3] - '0';
15031       break;
15032     case 0x106:
15033       for (i = 0; i < 9; i++)
15034 #ifdef LIBRAW_LIBRARY_BUILD
15035         imgdata.color.P1_color[0].romm_cam[i] =
15036 #endif
15037             ((float *)romm_cam)[i] = getreal(11);
15038       romm_coeff(romm_cam);
15039       break;
15040     case 0x107:
15041       FORC3 cam_mul[c] = getreal(11);
15042       break;
15043     case 0x108:
15044       raw_width = data;
15045       break;
15046     case 0x109:
15047       raw_height = data;
15048       break;
15049     case 0x10a:
15050       left_margin = data;
15051       break;
15052     case 0x10b:
15053       top_margin = data;
15054       break;
15055     case 0x10c:
15056       width = data;
15057       break;
15058     case 0x10d:
15059       height = data;
15060       break;
15061     case 0x10e:
15062       ph1.format = data;
15063       break;
15064     case 0x10f:
15065       data_offset = data + base;
15066       break;
15067     case 0x110:
15068       meta_offset = data + base;
15069       meta_length = len;
15070       break;
15071     case 0x112:
15072       ph1.key_off = save - 4;
15073       break;
15074     case 0x210:
15075       ph1.tag_210 = int_to_float(data);
15076 #ifdef LIBRAW_LIBRARY_BUILD
15077       imgdata.other.SensorTemperature = ph1.tag_210;
15078 #endif
15079       break;
15080     case 0x21a:
15081       ph1.tag_21a = data;
15082       break;
15083     case 0x21c:
15084       strip_offset = data + base;
15085       break;
15086     case 0x21d:
15087       ph1.t_black = data;
15088       break;
15089     case 0x222:
15090       ph1.split_col = data;
15091       break;
15092     case 0x223:
15093       ph1.black_col = data + base;
15094       break;
15095     case 0x224:
15096       ph1.split_row = data;
15097       break;
15098     case 0x225:
15099       ph1.black_row = data + base;
15100       break;
15101 #ifdef LIBRAW_LIBRARY_BUILD
15102     case 0x226:
15103       for (i = 0; i < 9; i++)
15104         imgdata.color.P1_color[1].romm_cam[i] = getreal(11);
15105       break;
15106 #endif
15107     case 0x301:
15108       model[63] = 0;
15109       fread(model, 1, 63, ifp);
15110       if ((cp = strstr(model, " camera")))
15111         *cp = 0;
15112     }
15113     fseek(ifp, save, SEEK_SET);
15114   }
15115 
15116 #ifdef LIBRAW_LIBRARY_BUILD
15117   if (!imgdata.lens.makernotes.body[0] && !imgdata.shootinginfo.BodySerial[0])
15118   {
15119     fseek(ifp, meta_offset, SEEK_SET);
15120     order = get2();
15121     fseek(ifp, 6, SEEK_CUR);
15122     fseek(ifp, meta_offset + get4(), SEEK_SET);
15123     entries = get4();
15124     get4();
15125     while (entries--)
15126     {
15127       tag = get4();
15128       len = get4();
15129       data = get4();
15130       save = ftell(ifp);
15131       fseek(ifp, meta_offset + data, SEEK_SET);
15132       if (tag == 0x0407)
15133       {
15134         stmread(imgdata.shootinginfo.BodySerial, len, ifp);
15135         if ((imgdata.shootinginfo.BodySerial[0] == 0x4c) && (imgdata.shootinginfo.BodySerial[1] == 0x49))
15136         {
15137           unique_id =
15138               (((imgdata.shootinginfo.BodySerial[0] & 0x3f) << 5) | (imgdata.shootinginfo.BodySerial[2] & 0x3f)) - 0x41;
15139         }
15140         else
15141         {
15142           unique_id =
15143               (((imgdata.shootinginfo.BodySerial[0] & 0x3f) << 5) | (imgdata.shootinginfo.BodySerial[1] & 0x3f)) - 0x41;
15144         }
15145         setPhaseOneFeatures(unique_id);
15146       }
15147       fseek(ifp, save, SEEK_SET);
15148     }
15149   }
15150 #endif
15151 
15152   load_raw = ph1.format < 3 ? &CLASS phase_one_load_raw : &CLASS phase_one_load_raw_c;
15153   maximum = 0xffff;
15154   strcpy(make, "Phase One");
15155   if (model[0])
15156     return;
15157   switch (raw_height)
15158   {
15159   case 2060:
15160     strcpy(model, "LightPhase");
15161     break;
15162   case 2682:
15163     strcpy(model, "H 10");
15164     break;
15165   case 4128:
15166     strcpy(model, "H 20");
15167     break;
15168   case 5488:
15169     strcpy(model, "H 25");
15170     break;
15171   }
15172 }
15173 
parse_fuji(int offset)15174 void CLASS parse_fuji(int offset)
15175 {
15176   unsigned entries, tag, len, save, c;
15177 
15178   fseek(ifp, offset, SEEK_SET);
15179   entries = get4();
15180   if (entries > 255)
15181     return;
15182 #ifdef LIBRAW_LIBRARY_BUILD
15183   imgdata.process_warnings |= LIBRAW_WARN_PARSEFUJI_PROCESSED;
15184 #endif
15185   while (entries--)
15186   {
15187     tag = get2();
15188     len = get2();
15189     save = ftell(ifp);
15190 
15191     if (tag == 0x100)
15192     {
15193       raw_height = get2();
15194       raw_width = get2();
15195     }
15196     else if (tag == 0x121)
15197     {
15198       height = get2();
15199       if ((width = get2()) == 4284)
15200         width += 3;
15201     }
15202     else if (tag == 0x130)
15203     {
15204       fuji_layout = fgetc(ifp) >> 7;
15205       fuji_width = !(fgetc(ifp) & 8);
15206     }
15207     else if (tag == 0x131)
15208     {
15209       filters = 9;
15210       FORC(36)
15211       {
15212         int q = fgetc(ifp);
15213         xtrans_abs[0][35 - c] = MAX(0, MIN(q, 2)); /* & 3;*/
15214       }
15215     }
15216     else if (tag == 0x2ff0)
15217     {
15218       FORC4 cam_mul[c ^ 1] = get2();
15219 
15220 // IB start
15221 #ifdef LIBRAW_LIBRARY_BUILD
15222     }
15223 
15224     else if (tag == 0x110)
15225     {
15226       imgdata.sizes.raw_crop.ctop = get2();
15227       imgdata.sizes.raw_crop.cleft = get2();
15228     }
15229 
15230     else if (tag == 0x111)
15231     {
15232       imgdata.sizes.raw_crop.cheight = get2();
15233       imgdata.sizes.raw_crop.cwidth = get2();
15234     }
15235 
15236     else if ((tag == 0x122) && !strcmp(model, "DBP for GX680"))
15237     {
15238       int k = get2();
15239       int l = get2(); /* margins? */
15240       int m = get2(); /* margins? */
15241       int n = get2();
15242       //      printf ("==>>0x122: height= %d l= %d m= %d width= %d\n", k, l, m, n);
15243     }
15244 
15245     else if (tag == 0x9650)
15246     {
15247       short a = (short)get2();
15248       float b = fMAX(1.0f, get2());
15249       imgdata.makernotes.fuji.FujiExpoMidPointShift = a / b;
15250     }
15251 
15252     else if (tag == 0x2f00)
15253     {
15254       int nWBs = get4();
15255       nWBs = MIN(nWBs, 6);
15256       for (int wb_ind = 0; wb_ind < nWBs; wb_ind++)
15257       {
15258         FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Custom1 + wb_ind][c ^ 1] = get2();
15259         fseek(ifp, 8, SEEK_CUR);
15260       }
15261     }
15262 
15263     else if (tag == 0x2000)
15264     {
15265       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Auto][c ^ 1] = get2();
15266     }
15267     else if (tag == 0x2100)
15268     {
15269       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_FineWeather][c ^ 1] = get2();
15270     }
15271     else if (tag == 0x2200)
15272     {
15273       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Shade][c ^ 1] = get2();
15274     }
15275     else if (tag == 0x2300)
15276     {
15277       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_D][c ^ 1] = get2();
15278     }
15279     else if (tag == 0x2301)
15280     {
15281       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_N][c ^ 1] = get2();
15282     }
15283     else if (tag == 0x2302)
15284     {
15285       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_WW][c ^ 1] = get2();
15286     }
15287     else if (tag == 0x2310)
15288     {
15289       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_FL_L][c ^ 1] = get2();
15290     }
15291     else if (tag == 0x2400)
15292     {
15293       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Tungsten][c ^ 1] = get2();
15294     }
15295     else if (tag == 0x2410)
15296     {
15297       FORC4 imgdata.color.WB_Coeffs[LIBRAW_WBI_Flash][c ^ 1] = get2();
15298 #endif
15299       // IB end
15300     }
15301     else if (tag == 0xc000)
15302     /* 0xc000 tag versions, second ushort; valid if the first ushort is 0
15303     X100F	0x0259
15304     X100T	0x0153
15305     X-E2	0x014f	0x024f depends on firmware
15306     X-A1	0x014e
15307     XQ2		0x0150
15308     XQ1		0x0150
15309     X100S	0x0149	0x0249 depends on firmware
15310     X30		0x0152
15311     X20		0x0146
15312     X-T10	0x0154
15313     X-T2	0x0258
15314     X-M1	0x014d
15315     X-E2s	0x0355
15316     X-A2	0x014e
15317     X-T20	0x025b
15318     GFX 50S	0x025a
15319     X-T1	0x0151	0x0251 0x0351 depends on firmware
15320     X70		0x0155
15321     X-Pro2	0x0255
15322     */
15323     {
15324       c = order;
15325       order = 0x4949;
15326       if ((tag = get4()) > 10000)
15327         tag = get4();
15328       if (tag > 10000)
15329         tag = get4();
15330       width = tag;
15331       height = get4();
15332 #ifdef LIBRAW_LIBRARY_BUILD
15333       if (!strcmp(model, "X-A3")  ||
15334           !strcmp(model, "X-A10") ||
15335           !strcmp(model, "X-A5")  ||
15336           !strcmp(model, "X-A20"))
15337       {
15338         int wb[4];
15339         int nWB, tWB, pWB;
15340         int iCCT = 0;
15341         int cnt;
15342         fseek(ifp, save + 0x200, SEEK_SET);
15343         for (int wb_ind = 0; wb_ind < 42; wb_ind++)
15344         {
15345           nWB = get4();
15346           tWB = get4();
15347           wb[0] = get4() << 1;
15348           wb[1] = get4();
15349           wb[3] = get4();
15350           wb[2] = get4() << 1;
15351           if (tWB && (iCCT < 255))
15352           {
15353             imgdata.color.WBCT_Coeffs[iCCT][0] = tWB;
15354             for (cnt = 0; cnt < 4; cnt++)
15355               imgdata.color.WBCT_Coeffs[iCCT][cnt + 1] = wb[cnt];
15356             iCCT++;
15357           }
15358           if (nWB != 70)
15359           {
15360             for (pWB = 1; pWB < nFuji_wb_list2; pWB += 2)
15361             {
15362               if (Fuji_wb_list2[pWB] == nWB)
15363               {
15364                 for (cnt = 0; cnt < 4; cnt++)
15365                   imgdata.color.WB_Coeffs[Fuji_wb_list2[pWB - 1]][cnt] = wb[cnt];
15366                 break;
15367               }
15368             }
15369           }
15370         }
15371       }
15372       else
15373       {
15374         libraw_internal_data.unpacker_data.posRAFData = save;
15375         libraw_internal_data.unpacker_data.lenRAFData = (len >> 1);
15376       }
15377 #endif
15378       order = c;
15379     }
15380     fseek(ifp, save + len, SEEK_SET);
15381   }
15382   height <<= fuji_layout;
15383   width >>= fuji_layout;
15384 }
15385 
parse_jpeg(int offset)15386 int CLASS parse_jpeg(int offset)
15387 {
15388   int len, save, hlen, mark;
15389   fseek(ifp, offset, SEEK_SET);
15390   if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8)
15391     return 0;
15392 
15393   while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda)
15394   {
15395     order = 0x4d4d;
15396     len = get2() - 2;
15397     save = ftell(ifp);
15398     if (mark == 0xc0 || mark == 0xc3 || mark == 0xc9)
15399     {
15400       fgetc(ifp);
15401       raw_height = get2();
15402       raw_width = get2();
15403     }
15404     order = get2();
15405     hlen = get4();
15406     if (get4() == 0x48454150
15407 #ifdef LIBRAW_LIBRARY_BUILD
15408         && (save + hlen) >= 0 && (save + hlen) <= ifp->size()
15409 #endif
15410             ) /* "HEAP" */
15411     {
15412 #ifdef LIBRAW_LIBRARY_BUILD
15413       imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
15414       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
15415 #endif
15416       parse_ciff(save + hlen, len - hlen, 0);
15417     }
15418     if (parse_tiff(save + 6))
15419       apply_tiff();
15420     fseek(ifp, save + len, SEEK_SET);
15421   }
15422   return 1;
15423 }
15424 
parse_riff()15425 void CLASS parse_riff()
15426 {
15427   unsigned i, size, end;
15428   char tag[4], date[64], month[64];
15429   static const char mon[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
15430   struct tm t;
15431 
15432   order = 0x4949;
15433   fread(tag, 4, 1, ifp);
15434   size = get4();
15435   end = ftell(ifp) + size;
15436   if (!memcmp(tag, "RIFF", 4) || !memcmp(tag, "LIST", 4))
15437   {
15438     int maxloop = 1000;
15439     get4();
15440     while (ftell(ifp) + 7 < end && !feof(ifp) && maxloop--)
15441       parse_riff();
15442   }
15443   else if (!memcmp(tag, "nctg", 4))
15444   {
15445     while (ftell(ifp) + 7 < end)
15446     {
15447       i = get2();
15448       size = get2();
15449       if ((i + 1) >> 1 == 10 && size == 20)
15450         get_timestamp(0);
15451       else
15452         fseek(ifp, size, SEEK_CUR);
15453     }
15454   }
15455   else if (!memcmp(tag, "IDIT", 4) && size < 64)
15456   {
15457     fread(date, 64, 1, ifp);
15458     date[size] = 0;
15459     memset(&t, 0, sizeof t);
15460     if (sscanf(date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) == 6)
15461     {
15462       for (i = 0; i < 12 && strcasecmp(mon[i], month); i++)
15463         ;
15464       t.tm_mon = i;
15465       t.tm_year -= 1900;
15466       if (mktime(&t) > 0)
15467         timestamp = mktime(&t);
15468     }
15469   }
15470   else
15471     fseek(ifp, size, SEEK_CUR);
15472 }
15473 
parse_qt(int end)15474 void CLASS parse_qt(int end)
15475 {
15476   unsigned save, size;
15477   char tag[4];
15478 
15479   order = 0x4d4d;
15480   while (ftell(ifp) + 7 < end)
15481   {
15482     save = ftell(ifp);
15483     if ((size = get4()) < 8)
15484       return;
15485     if ((int)size < 0) return; // 2+GB is too much
15486     if (save + size < save) return; // 32bit overflow
15487     fread(tag, 4, 1, ifp);
15488     if (!memcmp(tag, "moov", 4) || !memcmp(tag, "udta", 4) || !memcmp(tag, "CNTH", 4))
15489       parse_qt(save + size);
15490     if (!memcmp(tag, "CNDA", 4))
15491       parse_jpeg(ftell(ifp));
15492     fseek(ifp, save + size, SEEK_SET);
15493   }
15494 }
15495 
parse_smal(int offset,int fsize)15496 void CLASS parse_smal(int offset, int fsize)
15497 {
15498   int ver;
15499 
15500   fseek(ifp, offset + 2, SEEK_SET);
15501   order = 0x4949;
15502   ver = fgetc(ifp);
15503   if (ver == 6)
15504     fseek(ifp, 5, SEEK_CUR);
15505   if (get4() != fsize)
15506     return;
15507   if (ver > 6)
15508     data_offset = get4();
15509   raw_height = height = get2();
15510   raw_width = width = get2();
15511   strcpy(make, "SMaL");
15512   sprintf(model, "v%d %dx%d", ver, width, height);
15513   if (ver == 6)
15514     load_raw = &CLASS smal_v6_load_raw;
15515   if (ver == 9)
15516     load_raw = &CLASS smal_v9_load_raw;
15517 }
15518 
parse_cine()15519 void CLASS parse_cine()
15520 {
15521   unsigned off_head, off_setup, off_image, i;
15522 
15523   order = 0x4949;
15524   fseek(ifp, 4, SEEK_SET);
15525   is_raw = get2() == 2;
15526   fseek(ifp, 14, SEEK_CUR);
15527   is_raw *= get4();
15528   off_head = get4();
15529   off_setup = get4();
15530   off_image = get4();
15531   timestamp = get4();
15532   if ((i = get4()))
15533     timestamp = i;
15534   fseek(ifp, off_head + 4, SEEK_SET);
15535   raw_width = get4();
15536   raw_height = get4();
15537   switch (get2(), get2())
15538   {
15539   case 8:
15540     load_raw = &CLASS eight_bit_load_raw;
15541     break;
15542   case 16:
15543     load_raw = &CLASS unpacked_load_raw;
15544   }
15545   fseek(ifp, off_setup + 792, SEEK_SET);
15546   strcpy(make, "CINE");
15547   sprintf(model, "%d", get4());
15548   fseek(ifp, 12, SEEK_CUR);
15549   switch ((i = get4()) & 0xffffff)
15550   {
15551   case 3:
15552     filters = 0x94949494;
15553     break;
15554   case 4:
15555     filters = 0x49494949;
15556     break;
15557   default:
15558     is_raw = 0;
15559   }
15560   fseek(ifp, 72, SEEK_CUR);
15561   switch ((get4() + 3600) % 360)
15562   {
15563   case 270:
15564     flip = 4;
15565     break;
15566   case 180:
15567     flip = 1;
15568     break;
15569   case 90:
15570     flip = 7;
15571     break;
15572   case 0:
15573     flip = 2;
15574   }
15575   cam_mul[0] = getreal(11);
15576   cam_mul[2] = getreal(11);
15577   maximum = ~((~0u) << get4());
15578   fseek(ifp, 668, SEEK_CUR);
15579   shutter = get4() / 1000000000.0;
15580   fseek(ifp, off_image, SEEK_SET);
15581   if (shot_select < is_raw)
15582     fseek(ifp, shot_select * 8, SEEK_CUR);
15583   data_offset = (INT64)get4() + 8;
15584   data_offset += (INT64)get4() << 32;
15585 }
15586 
parse_redcine()15587 void CLASS parse_redcine()
15588 {
15589   unsigned i, len, rdvo;
15590 
15591   order = 0x4d4d;
15592   is_raw = 0;
15593   fseek(ifp, 52, SEEK_SET);
15594   width = get4();
15595   height = get4();
15596   fseek(ifp, 0, SEEK_END);
15597   fseek(ifp, -(i = ftello(ifp) & 511), SEEK_CUR);
15598   if (get4() != i || get4() != 0x52454f42)
15599   {
15600 #ifdef DCRAW_VERBOSE
15601     fprintf(stderr, _("%s: Tail is missing, parsing from head...\n"), ifname);
15602 #endif
15603     fseek(ifp, 0, SEEK_SET);
15604     while ((len = get4()) != EOF)
15605     {
15606       if (get4() == 0x52454456)
15607         if (is_raw++ == shot_select)
15608           data_offset = ftello(ifp) - 8;
15609       fseek(ifp, len - 8, SEEK_CUR);
15610     }
15611   }
15612   else
15613   {
15614     rdvo = get4();
15615     fseek(ifp, 12, SEEK_CUR);
15616     is_raw = get4();
15617     fseeko(ifp, rdvo + 8 + shot_select * 4, SEEK_SET);
15618     data_offset = get4();
15619   }
15620 }
15621 
15622 /*
15623    All matrices are from Adobe DNG Converter unless otherwise noted.
15624  */
adobe_coeff(const char * t_make,const char * t_model,int internal_only)15625 void CLASS adobe_coeff(const char *t_make, const char *t_model
15626 #ifdef LIBRAW_LIBRARY_BUILD
15627                        ,
15628                        int internal_only
15629 #endif
15630 )
15631 {
15632   // clang-format off
15633   static const struct
15634   {
15635     const char *prefix;
15636     int t_black, t_maximum, trans[12];
15637   } table[] = {
15638     { "AgfaPhoto DC-833m", 0, 0, /* DJC */
15639       { 11438,-3762,-1115,-2409,9914,2497,-1227,2295,5300 } },
15640 
15641     { "Apple QuickTake", 0, 0, /* DJC */
15642       { 21392,-5653,-3353,2406,8010,-415,7166,1427,2078 } },
15643 
15644     {"Broadcom RPi IMX219", 66, 0x3ff,
15645       { 5302,1083,-728,-5320,14112,1699,-863,2371,5136 } }, /* LibRaw */
15646     { "Broadcom RPi OV5647", 16, 0x3ff,
15647       { 12782,-4059,-379,-478,9066,1413,1340,1513,5176 } }, /* DJC */
15648 
15649     { "Canon EOS D2000", 0, 0,
15650       { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
15651     { "Canon EOS D6000", 0, 0,
15652       { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
15653     { "Canon EOS D30", 0, 0, /* updated */
15654       { 9900,-2771,-1324,-7072,14229,3140,-2790,3344,8861 } },
15655     { "Canon EOS D60", 0, 0xfa0, /* updated */
15656       { 6211,-1358,-896,-8557,15766,3012,-3001,3507,8567 } },
15657     { "Canon EOS 5DS", 0, 0x3c96,
15658       { 6250,-711,-808,-5153,12794,2636,-1249,2198,5610 } },
15659     { "Canon EOS 5D Mark IV", 0, 0,
15660       { 6446,-366,-864,-4436,12204,2513,-952,2496,6348 } },
15661     { "Canon EOS 5D Mark III", 0, 0x3c80,
15662       { 6722,-635,-963,-4287,12460,2028,-908,2162,5668 } },
15663     { "Canon EOS 5D Mark II", 0, 0x3cf0,
15664       { 4716,603,-830,-7798,15474,2480,-1496,1937,6651 } },
15665     { "Canon EOS 5D", 0, 0xe6c,
15666       { 6347,-479,-972,-8297,15954,2480,-1968,2131,7649 } },
15667     { "Canon EOS 6D Mark II", 0, 0x38de,
15668       { 6875,-970,-932,-4691,12459,2501,-874,1953,5809 } },
15669     { "Canon EOS 6D", 0, 0x3c82,
15670       {7034, -804, -1014, -4420, 12564, 2058, -851, 1994, 5758 } },
15671     { "Canon EOS 77D", 0, 0,
15672       { 7377,-742,-998,-4235,11981,2549,-673,1918,5538 } },
15673     { "Canon EOS 7D Mark II", 0, 0x3510,
15674       { 7268,-1082,-969,-4186,11839,2663,-825,2029,5839 } },
15675     { "Canon EOS 7D", 0, 0x3510,
15676       { 6844,-996,-856,-3876,11761,2396,-593,1772,6198 } },
15677     { "Canon EOS 800D", 0, 0,
15678       { 6970,-512,-968,-4425,12161,2553,-739,1982,5601 } },
15679     { "Canon EOS 80D", 0, 0,
15680       { 7457,-671,-937,-4849,12495,2643,-1213,2354,5492 } },
15681     { "Canon EOS 10D", 0, 0xfa0, /* updated */
15682       { 8250,-2044,-1127,-8092,15606,2664,-2893,3453,8348 } },
15683     { "Canon EOS 200D", 0, 0,
15684       { 7377,-742,-998,-4235,11981,2549,-673,1918,5538 } },
15685     { "Canon EOS 20Da", 0, 0,
15686       { 14155,-5065,-1382,-6550,14633,2039,-1623,1824,6561 } },
15687     { "Canon EOS 20D", 0, 0xfff,
15688       { 6599,-537,-891,-8071,15783,2424,-1983,2234,7462 } },
15689     { "Canon EOS 30D", 0, 0,
15690       { 6257,-303,-1000,-7880,15621,2396,-1714,1904,7046 } },
15691     { "Canon EOS 40D", 0, 0x3f60,
15692       { 6071,-747,-856,-7653,15365,2441,-2025,2553,7315 } },
15693     { "Canon EOS 50D", 0, 0x3d93,
15694       { 4920,616,-593,-6493,13964,2784,-1774,3178,7005 } },
15695     { "Canon EOS 60Da", 0, 0x2ff7, /* added */
15696       { 17492,-7240,-2023,-1791,10323,1701,-186,1329,5406 } },
15697     { "Canon EOS 60D", 0, 0x2ff7,
15698       { 6719,-994,-925,-4408,12426,2211,-887,2129,6051 } },
15699     { "Canon EOS 70D", 0, 0x3bc7,
15700       { 7034,-804,-1014,-4420,12564,2058,-851,1994,5758 } },
15701     { "Canon EOS 100D", 0, 0x350f,
15702       { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
15703     { "Canon EOS 300D", 0, 0xfa0, /* updated */
15704       { 8250,-2044,-1127,-8092,15606,2664,-2893,3453,8348 } },
15705     { "Canon EOS 350D", 0, 0xfff,
15706       { 6018,-617,-965,-8645,15881,2975,-1530,1719,7642 } },
15707     { "Canon EOS 400D", 0, 0xe8e,
15708       { 7054,-1501,-990,-8156,15544,2812,-1278,1414,7796 } },
15709     { "Canon EOS 450D", 0, 0x390d,
15710       { 5784,-262,-821,-7539,15064,2672,-1982,2681,7427 } },
15711     { "Canon EOS 500D", 0, 0x3479,
15712       { 4763,712,-646,-6821,14399,2640,-1921,3276,6561 } },
15713     { "Canon EOS 550D", 0, 0x3dd7,
15714       { 6941,-1164,-857,-3825,11597,2534,-416,1540,6039 } },
15715     { "Canon EOS 600D", 0, 0x3510,
15716       { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
15717     { "Canon EOS 650D", 0, 0x354d,
15718       { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
15719     { "Canon EOS 750D", 0, 0x3c00,
15720       { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
15721     { "Canon EOS 760D", 0, 0x3c00,
15722       { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
15723     { "Canon EOS 700D", 0, 0x3c00,
15724       { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
15725     { "Canon EOS 1000D", 0, 0xe43,
15726       { 6771,-1139,-977,-7818,15123,2928,-1244,1437,7533 } },
15727     { "Canon EOS 1100D", 0, 0x3510,
15728       { 6444,-904,-893,-4563,12308,2535,-903,2016,6728 } },
15729     { "Canon EOS 1200D", 0, 0x37c2,
15730       { 6461,-907,-882,-4300,12184,2378,-819,1944,5931 } },
15731     { "Canon EOS 1300D", 0, 0x37c2,
15732       { 6939,-1016,-866,-4428,12473,2177,-1175,2178,6162 } },
15733     { "Canon EOS M6", 0, 0,
15734       { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
15735     { "Canon EOS M5", 0, 0,
15736       { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
15737     { "Canon EOS M3", 0, 0,
15738       { 6362,-823,-847,-4426,12109,2616,-743,1857,5635 } },
15739     { "Canon EOS M2", 0, 0, /* added */
15740       { 6400,-480,-888,-5294,13416,2047,-1296,2203,6137 } },
15741     { "Canon EOS M100", 0, 0,
15742       { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
15743     { "Canon EOS M10", 0, 0,
15744       { 6400,-480,-888,-5294,13416,2047,-1296,2203,6137 } },
15745     { "Canon EOS M", 0, 0,
15746       { 6602,-841,-939,-4472,12458,2247,-975,2039,6148 } },
15747     { "Canon EOS-1Ds Mark III", 0, 0x3bb0,
15748       { 5859,-211,-930,-8255,16017,2353,-1732,1887,7448 } },
15749     { "Canon EOS-1Ds Mark II", 0, 0xe80,
15750       { 6517,-602,-867,-8180,15926,2378,-1618,1771,7633 } },
15751     { "Canon EOS-1D Mark IV", 0, 0x3bb0,
15752       { 6014,-220,-795,-4109,12014,2361,-561,1824,5787 } },
15753     { "Canon EOS-1D Mark III", 0, 0x3bb0,
15754       { 6291,-540,-976,-8350,16145,2311,-1714,1858,7326 } },
15755     { "Canon EOS-1D Mark II N", 0, 0xe80,
15756       { 6240,-466,-822,-8180,15825,2500,-1801,1938,8042 } },
15757     { "Canon EOS-1D Mark II", 0, 0xe80,
15758       { 6264,-582,-724,-8312,15948,2504,-1744,1919,8664 } },
15759     { "Canon EOS-1DS", 0, 0xe20, /* updated */
15760       { 3925,4060,-1739,-8973,16552,2545,-3287,3945,8243 } },
15761     { "Canon EOS-1D C", 0, 0x3c4e,
15762       { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
15763     { "Canon EOS-1D X Mark II", 0, 0x3c4e, /* updated */
15764       { 7596,-978,-967,-4808,12571,2503,-1398,2567,5752 } },
15765     { "Canon EOS-1D X", 0, 0x3c4e,
15766       { 6847,-614,-1014,-4669,12737,2139,-1197,2488,6846 } },
15767     { "Canon EOS-1D", 0, 0xe20,
15768       { 6806,-179,-1020,-8097,16415,1687,-3267,4236,7690 } },
15769     { "Canon EOS C500", 853, 0, /* DJC */
15770       { 17851,-10604,922,-7425,16662,763,-3660,3636,22278 } },
15771     {"Canon PowerShot 600", 0, 0, /* added */
15772       { -3822,10019,1311,4085,-157,3386,-5341,10829,4812,-1969,10969,1126 } },
15773     { "Canon PowerShot A530", 0, 0,
15774       { 0 } }, /* don't want the A5 matrix */
15775     { "Canon PowerShot A50", 0, 0,
15776       { -5300,9846,1776,3436,684,3939,-5540,9879,6200,-1404,11175,217 } },
15777     { "Canon PowerShot A5", 0, 0,
15778       { -4801,9475,1952,2926,1611,4094,-5259,10164,5947,-1554,10883,547 } },
15779     { "Canon PowerShot G10", 0, 0,
15780       { 11093,-3906,-1028,-5047,12492,2879,-1003,1750,5561 } },
15781     { "Canon PowerShot G11", 0, 0,
15782       { 12177,-4817,-1069,-1612,9864,2049,-98,850,4471 } },
15783     { "Canon PowerShot G12", 0, 0,
15784       { 13244,-5501,-1248,-1508,9858,1935,-270,1083,4366 } },
15785     { "Canon PowerShot G15", 0, 0,
15786       { 7474,-2301,-567,-4056,11456,2975,-222,716,4181 } },
15787     { "Canon PowerShot G16", 0, 0, /* updated */
15788       { 8020,-2687,-682,-3704,11879,2052,-965,1921,5556 } },
15789     { "Canon PowerShot G1 X Mark III", 0, 0,
15790       { 8532,-701,-1167,-4095,11879,2508,-797,2424,7010 } },
15791     { "Canon PowerShot G1 X Mark II", 0, 0,
15792       { 7378,-1255,-1043,-4088,12251,2048,-876,1946,5805 } },
15793     { "Canon PowerShot G1 X", 0, 0,
15794       { 7378,-1255,-1043,-4088,12251,2048,-876,1946,5805 } },
15795     { "Canon PowerShot G1", 0, 0, /* updated */
15796       { -5686,10300,2223,4725,-1157,4383,-6128,10783,6163,-2688,12093,604 } },
15797     { "Canon PowerShot G2", 0, 0, /* updated */
15798       { 9194,-2787,-1059,-8098,15657,2608,-2610,3064,7867 } },
15799     { "Canon PowerShot G3 X", 0, 0,
15800       { 9701,-3857,-921,-3149,11537,1817,-786,1817,5147 } },
15801     { "Canon PowerShot G3", 0, 0, /* updated */
15802       { 9326,-2882,-1084,-7940,15447,2677,-2620,3090,7740 } },
15803     { "Canon PowerShot G5 X",0, 0,
15804       { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
15805     { "Canon PowerShot G5", 0, 0, /* updated */
15806       { 9869,-2972,-942,-7314,15098,2369,-1898,2536,7282 } },
15807     { "Canon PowerShot G6", 0, 0,
15808       { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
15809     { "Canon PowerShot G7 X Mark II", 0, 0,
15810       { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
15811     { "Canon PowerShot G7 X", 0, 0,
15812       { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
15813     { "Canon PowerShot G9 X Mark II", 0, 0,
15814       { 10056,-4131,-944,-2576,11143,1625,-238,1294,5179 } },
15815     { "Canon PowerShot G9 X",0, 0,
15816       { 9602,-3823,-937,-2984,11495,1675,-407,1415,5049 } },
15817     { "Canon PowerShot G9", 0, 0,
15818       { 7368,-2141,-598,-5621,13254,2625,-1418,1696,5743 } },
15819     { "Canon PowerShot Pro1", 0, 0,
15820       { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
15821     { "Canon PowerShot Pro70", 34, 0, /* updated */
15822       { -5106,10695,1576,3820,53,4566,-6497,10736,6701,-3336,11887,1394 } },
15823     { "Canon PowerShot Pro90", 0, 0, /* updated */
15824       { -5912,10768,2288,4612,-989,4333,-6153,10897,5944,-2907,12288,624 } },
15825     { "Canon PowerShot S30", 0, 0, /* updated */
15826       { 10744,-3813,-1142,-7962,15966,2075,-2492,2805,7744 } },
15827     { "Canon PowerShot S40", 0, 0, /* updated */
15828       { 8606,-2573,-949,-8237,15489,2974,-2649,3076,9100 } },
15829     { "Canon PowerShot S45", 0, 0, /* updated */
15830       { 8251,-2410,-964,-8047,15430,2823,-2380,2824,8119 } },
15831     { "Canon PowerShot S50", 0, 0, /* updated */
15832       { 8979,-2658,-871,-7721,15500,2357,-1773,2366,6634 } },
15833     { "Canon PowerShot S60", 0, 0,
15834       { 8795,-2482,-797,-7804,15403,2573,-1422,1996,7082 } },
15835     { "Canon PowerShot S70", 0, 0,
15836       { 9976,-3810,-832,-7115,14463,2906,-901,989,7889 } },
15837     { "Canon PowerShot S90", 0, 0,
15838       { 12374,-5016,-1049,-1677,9902,2078,-83,852,4683 } },
15839     { "Canon PowerShot S95", 0, 0,
15840       { 13440,-5896,-1279,-1236,9598,1931,-180,1001,4651 } },
15841     { "Canon PowerShot S120", 0, 0,
15842       { 6961,-1685,-695,-4625,12945,1836,-1114,2152,5518 } },
15843     { "Canon PowerShot S110", 0, 0,
15844       { 8039,-2643,-654,-3783,11230,2930,-206,690,4194 } },
15845     { "Canon PowerShot S100", 0, 0,
15846       { 7968,-2565,-636,-2873,10697,2513,180,667,4211 } },
15847     { "Canon PowerShot SX1 IS", 0, 0,
15848       { 6578,-259,-502,-5974,13030,3309,-308,1058,4970 } },
15849     { "Canon PowerShot SX50 HS", 0, 0,
15850       { 12432,-4753,-1247,-2110,10691,1629,-412,1623,4926 } },
15851     { "Canon PowerShot SX60 HS", 0, 0,
15852       { 13161,-5451,-1344,-1989,10654,1531,-47,1271,4955 } },
15853     { "Canon PowerShot A3300", 0, 0, /* DJC */
15854       { 10826,-3654,-1023,-3215,11310,1906,0,999,4960 } },
15855     { "Canon PowerShot A470", 0, 0, /* DJC */
15856       { 12513,-4407,-1242,-2680,10276,2405,-878,2215,4734 } },
15857     { "Canon PowerShot A610", 0, 0, /* DJC */
15858       { 15591,-6402,-1592,-5365,13198,2168,-1300,1824,5075 } },
15859     { "Canon PowerShot A620", 0, 0, /* DJC */
15860       { 15265,-6193,-1558,-4125,12116,2010,-888,1639,5220 } },
15861     { "Canon PowerShot A630", 0, 0, /* DJC */
15862       { 14201,-5308,-1757,-6087,14472,1617,-2191,3105,5348 } },
15863     { "Canon PowerShot A640", 0, 0, /* DJC */
15864       { 13124,-5329,-1390,-3602,11658,1944,-1612,2863,4885 } },
15865     { "Canon PowerShot A650", 0, 0, /* DJC */
15866       { 9427,-3036,-959,-2581,10671,1911,-1039,1982,4430 } },
15867     { "Canon PowerShot A720", 0, 0, /* DJC */
15868       { 14573,-5482,-1546,-1266,9799,1468,-1040,1912,3810 } },
15869     { "Canon PowerShot D10", 127, 0, /* DJC */
15870       { 14052,-5229,-1156,-1325,9420,2252,-498,1957,4116 } },
15871     { "Canon PowerShot S3 IS", 0, 0, /* DJC */
15872       { 14062,-5199,-1446,-4712,12470,2243,-1286,2028,4836 } },
15873     { "Canon PowerShot SX110 IS", 0, 0, /* DJC */
15874       { 14134,-5576,-1527,-1991,10719,1273,-1158,1929,3581 } },
15875     { "Canon PowerShot SX220", 0, 0, /* DJC */
15876       { 13898,-5076,-1447,-1405,10109,1297,-244,1860,3687 } },
15877     { "Canon IXUS 160", 0, 0, /* DJC */
15878       { 11657,-3781,-1136,-3544,11262,2283,-160,1219,4700 } },
15879 
15880     { "Casio EX-F1", 0, 0, /* added */
15881       { 9084,-2016,-848,-6711,14351,2570,-1059,1725,6135 } },
15882     { "Casio EX-FH100", 0, 0, /* added */
15883       { 12771,-4179,-1558,-2149,10938,1375,-453,1751,4494 } },
15884     { "Casio EX-S20", 0, 0, /* DJC */
15885       { 11634,-3924,-1128,-4968,12954,2015,-1588,2648,7206 } },
15886     { "Casio EX-Z750", 0, 0, /* DJC */
15887       { 10819,-3873,-1099,-4903,13730,1175,-1755,3751,4632 } },
15888     { "Casio EX-Z10", 128, 0xfff, /* DJC */
15889       { 9790,-3338,-603,-2321,10222,2099,-344,1273,4799 } },
15890 
15891     { "CINE 650", 0, 0,
15892       { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
15893     { "CINE 660", 0, 0,
15894       { 3390,480,-500,-800,3610,340,-550,2336,1192 } },
15895     { "CINE", 0, 0,
15896       { 20183,-4295,-423,-3940,15330,3985,-280,4870,9800 } },
15897 
15898     { "Contax N Digital", 0, 0xf1e,
15899       { 7777,1285,-1053,-9280,16543,2916,-3677,5679,7060 } },
15900 
15901     { "DXO ONE", 0, 0,
15902       { 6596,-2079,-562,-4782,13016,1933,-970,1581,5181 } },
15903 
15904     { "Epson R-D1", 0, 0,
15905       { 6827,-1878,-732,-8429,16012,2564,-704,592,7145 } },
15906 
15907     { "Fujifilm E550", 0, 0, /* updated */
15908       { 11044,-3888,-1120,-7248,15167,2208,-1531,2276,8069 } },
15909     { "Fujifilm E900", 0, 0,
15910       { 9183,-2526,-1078,-7461,15071,2574,-2022,2440,8639 } },
15911     { "Fujifilm F5", 0, 0,
15912       { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
15913     { "Fujifilm F6", 0, 0,
15914       { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
15915     { "Fujifilm F77", 0, 0xfe9,
15916       { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
15917     { "Fujifilm F7", 0, 0,
15918       { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
15919     { "Fujifilm F810", 0, 0, /* added */
15920       { 11044,-3888,-1120,-7248,15167,2208,-1531,2276,8069 } },
15921     { "Fujifilm F8", 0, 0,
15922       { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
15923     { "Fujifilm S100FS", 514, 0,
15924       { 11521,-4355,-1065,-6524,13767,3058,-1466,1984,6045 } },
15925     { "Fujifilm S1", 0, 0,
15926       { 12297,-4882,-1202,-2106,10691,1623,-88,1312,4790 } },
15927     { "Fujifilm S20Pro", 0, 0,
15928       { 10004,-3219,-1201,-7036,15047,2107,-1863,2565,7736 } },
15929     { "Fujifilm S20", 512, 0x3fff,
15930       { 11401,-4498,-1312,-5088,12751,2613,-838,1568,5941 } },
15931     { "Fujifilm S2Pro", 128, 0, /* updated */
15932       { 12741,-4916,-1420,-8510,16791,1715,-1767,2302,7771 } },
15933     { "Fujifilm S3Pro", 0, 0,
15934       { 11807,-4612,-1294,-8927,16968,1988,-2120,2741,8006 } },
15935     { "Fujifilm S5Pro", 0, 0,
15936       { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
15937     { "Fujifilm S5000", 0, 0,
15938       { 8754,-2732,-1019,-7204,15069,2276,-1702,2334,6982 } },
15939     { "Fujifilm S5100", 0, 0,
15940       { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
15941     { "Fujifilm S5500", 0, 0,
15942       { 11940,-4431,-1255,-6766,14428,2542,-993,1165,7421 } },
15943     { "Fujifilm S5200", 0, 0,
15944       { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
15945     { "Fujifilm S5600", 0, 0,
15946       { 9636,-2804,-988,-7442,15040,2589,-1803,2311,8621 } },
15947     { "Fujifilm S6", 0, 0,
15948       { 12628,-4887,-1401,-6861,14996,1962,-2198,2782,7091 } },
15949     { "Fujifilm S7000", 0, 0,
15950       { 10190,-3506,-1312,-7153,15051,2238,-2003,2399,7505 } },
15951     { "Fujifilm S9000", 0, 0,
15952       { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
15953     { "Fujifilm S9500", 0, 0,
15954       { 10491,-3423,-1145,-7385,15027,2538,-1809,2275,8692 } },
15955     { "Fujifilm S9100", 0, 0,
15956       { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
15957     { "Fujifilm S9600", 0, 0,
15958       { 12343,-4515,-1285,-7165,14899,2435,-1895,2496,8800 } },
15959     { "Fujifilm SL1000", 0, 0,
15960       { 11705,-4262,-1107,-2282,10791,1709,-555,1713,4945 } },
15961     { "Fujifilm IS-1", 0, 0,
15962       { 21461,-10807,-1441,-2332,10599,1999,289,875,7703 } },
15963     { "Fujifilm IS Pro", 0, 0,
15964       { 12300,-5110,-1304,-9117,17143,1998,-1947,2448,8100 } },
15965     { "Fujifilm HS10 HS11", 0, 0xf68,
15966       { 12440,-3954,-1183,-1123,9674,1708,-83,1614,4086 } },
15967     { "Fujifilm HS2", 0, 0,
15968       { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
15969     { "Fujifilm HS3", 0, 0,
15970       { 13690,-5358,-1474,-3369,11600,1998,-132,1554,4395 } },
15971     { "Fujifilm HS50EXR", 0, 0,
15972       { 12085,-4727,-953,-3257,11489,2002,-511,2046,4592 } },
15973     { "Fujifilm F900EXR", 0, 0,
15974       { 12085,-4727,-953,-3257,11489,2002,-511,2046,4592 } },
15975     { "Fujifilm X100S", 0, 0,
15976       { 10592,-4262,-1008,-3514,11355,2465,-870,2025,6386 } },
15977     { "Fujifilm X100F", 0, 0,
15978       { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
15979     { "Fujifilm X100T", 0, 0,
15980       { 10592,-4262,-1008,-3514,11355,2465,-870,2025,6386 } },
15981     { "Fujifilm X100", 0, 0,
15982       { 12161,-4457,-1069,-5034,12874,2400,-795,1724,6904 } },
15983     { "Fujifilm X10", 0, 0,
15984       { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
15985     { "Fujifilm X20", 0, 0,
15986       { 11768,-4971,-1133,-4904,12927,2183,-480,1723,4605 } },
15987     { "Fujifilm X30", 0, 0,
15988       { 12328,-5256,-1144,-4469,12927,1675,-87,1291,4351 } },
15989     { "Fujifilm X70", 0, 0,
15990       { 10450,-4329,-878,-3217,11105,2421,-752,1758,6519 } },
15991     { "Fujifilm X-Pro1", 0, 0,
15992       { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
15993     { "Fujifilm X-Pro2", 0, 0,
15994       { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
15995     { "Fujifilm X-A10", 0, 0,
15996       { 11540,-4999,-991,-2949,10963,2278,-382,1049,5605} },
15997 
15998     { "Fujifilm X-A20", 0, 0, /* temp */
15999       { 11540,-4999,-991,-2949,10963,2278,-382,1049,5605} },
16000 
16001     { "Fujifilm X-A1", 0, 0,
16002       { 11086,-4555,-839,-3512,11310,2517,-815,1341,5940 } },
16003     { "Fujifilm X-A2", 0, 0,
16004       { 10763,-4560,-917,-3346,11311,2322,-475,1135,5843 } },
16005     { "Fujifilm X-A3", 0, 0,
16006       { 12407,-5222,-1086,-2971,11116,2120,-294,1029,5284 } },
16007 
16008     { "Fujifilm X-A5", 0, 0, /* temp */
16009       { 12407,-5222,-1086,-2971,11116,2120,-294,1029,5284 } },
16010 
16011     { "Fujifilm X-E1", 0, 0,
16012       { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
16013     { "Fujifilm X-E2S", 0, 0,
16014       { 11562,-5118,-961,-3022,11007,2311,-525,1569,6097 } },
16015     { "Fujifilm X-E2", 0, 0,
16016       { 8458,-2451,-855,-4597,12447,2407,-1475,2482,6526 } },
16017     { "Fujifilm X-E3", 0, 0,
16018       { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
16019     { "Fujifilm XF1", 0, 0,
16020       { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
16021     { "Fujifilm X-M1", 0, 0,
16022       { 10413,-3996,-993,-3721,11640,2361,-733,1540,6011 } },
16023     { "Fujifilm X-S1", 0, 0,
16024       { 13509,-6199,-1254,-4430,12733,1865,-331,1441,5022 } },
16025     { "Fujifilm X-T20", 0, 0,
16026       { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
16027     { "Fujifilm X-T2", 0, 0,
16028       { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
16029     { "Fujifilm X-T10", 0, 0, /* updated */
16030       { 8458,-2451,-855,-4597,12447,2407,-1475,2482,6526 } },
16031     { "Fujifilm X-T1", 0, 0,
16032       { 8458,-2451,-855,-4597,12447,2407,-1475,2482,6526 } },
16033     { "Fujifilm X-H1", 0, 0,
16034       { 11434,-4948,-1210,-3746,12042,1903,-666,1479,5235 } },
16035     { "Fujifilm XQ1", 0, 0,
16036       { 9252,-2704,-1064,-5893,14265,1717,-1101,2341,4349 } },
16037     { "Fujifilm XQ2", 0, 0,
16038       { 9252,-2704,-1064,-5893,14265,1717,-1101,2341,4349 } },
16039     { "Fujifilm GFX 50S", 0, 0,
16040       { 11756,-4754,-874,-3056,11045,2305,-381,1457,6006 } },
16041 
16042     { "GITUP GIT2P", 4160, 0,
16043       { 8489, -2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
16044     { "GITUP GIT2", 3200, 0,
16045       { 8489, -2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
16046 
16047     { "Hasselblad HV", 0, 0, /* added */
16048       { 6344,-1612,-461,-4862,12476,2680,-864,1785,6898 } },
16049     { "Hasselblad Lunar", 0, 0,
16050       { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
16051     { "Hasselblad Lusso", 0, 0, /* added */
16052       { 4912,-540,-201,-6129,13513,2906,-1563,2151,7182 } },
16053     { "Hasselblad Stellar", -800, 0,
16054       { 8651,-2754,-1057,-3464,12207,1373,-568,1398,4434 } },
16055     { "Hasselblad 500 mech.", 0, 0, /* added */
16056       { 8519,-3260,-280,-5081,13459,1738,-1449,2960,7809 } },
16057     { "Hasselblad CFV", 0, 0,
16058       { 8519,-3260,-280,-5081,13459,1738,-1449,2960,7809 } },
16059     { "Hasselblad H-16MP", 0, 0, /* LibRaw */
16060       { 17765,-5322,-1734,-6168,13354,2135,-264,2524,7440 } },
16061     { "Hasselblad H-22MP", 0, 0, /* LibRaw */
16062       { 17765,-5322,-1734,-6168,13354,2135,-264,2524,7440 } },
16063     { "Hasselblad H-31MP",0, 0, /* LibRaw */
16064       { 14480,-5448,-1686,-3534,13123,2260,384,2952,7232 } },
16065     { "Hasselblad 39-Coated", 0, 0, /* added */
16066       { 3857,452,-46,-6008,14477,1596,-2627,4481,5718 } },
16067     { "Hasselblad H-39MP",0, 0,
16068       { 3857,452,-46,-6008,14477,1596,-2627,4481,5718 } },
16069     { "Hasselblad H2D-39", 0, 0, /* added */
16070       { 3894,-110,287,-4672,12610,2295,-2092,4100,6196 } },
16071     { "Hasselblad H3D-50", 0, 0,
16072       { 3857,452,-46,-6008,14477,1596,-2627,4481,5718 } },
16073     { "Hasselblad H3D", 0, 0, /* added */
16074       { 3857,452,-46,-6008,14477,1596,-2627,4481,5718 } },
16075     { "Hasselblad H4D-40",0, 0, /* LibRaw */
16076       { 6325,-860,-957,-6559,15945,266,167,770,5936 } },
16077     { "Hasselblad H4D-50",0, 0, /* LibRaw */
16078       { 15283,-6272,-465,-2030,16031,478,-2379,390,7965 } },
16079     { "Hasselblad H4D-60",0, 0,
16080       { 9662,-684,-279,-4903,12293,2950,-344,1669,6024 } },
16081     { "Hasselblad H5D-50c",0, 0,
16082       { 4932,-835,141,-4878,11868,3437,-1138,1961,7067 } },
16083     { "Hasselblad H5D-50",0, 0,
16084       { 5656,-659,-346,-3923,12306,1791,-1602,3509,5442 } },
16085     { "Hasselblad H6D-100c",0, 0,
16086       { 5110,-1357,-308,-5573,12835,3077,-1279,2025,7010 } },
16087     { "Hasselblad X1D",0, 0,
16088       { 4932,-835,141,-4878,11868,3437,-1138,1961,7067 } },
16089 
16090     { "HTC One A9", 64, 1023, /* this is CM1 transposed */
16091       { 101, -20, -2, -11, 145, 41, -24, 1, 56 } },
16092 
16093     { "Imacon Ixpress", 0, 0, /* DJC */
16094       { 7025,-1415,-704,-5188,13765,1424,-1248,2742,6038 } },
16095 
16096     { "Kodak NC2000", 0, 0,
16097       { 13891,-6055,-803,-465,9919,642,2121,82,1291 } },
16098     { "Kodak DCS315C", -8, 0,
16099       { 17523,-4827,-2510,756,8546,-137,6113,1649,2250 } },
16100     { "Kodak DCS330C", -8, 0,
16101       { 20620,-7572,-2801,-103,10073,-396,3551,-233,2220 } },
16102     { "Kodak DCS420", 0, 0,
16103       { 10868,-1852,-644,-1537,11083,484,2343,628,2216 } },
16104     { "Kodak DCS460", 0, 0,
16105       { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
16106     { "Kodak EOSDCS1", 0, 0,
16107       { 10592,-2206,-967,-1944,11685,230,2206,670,1273 } },
16108     { "Kodak EOSDCS3B", 0, 0,
16109       { 9898,-2700,-940,-2478,12219,206,1985,634,1031 } },
16110     { "Kodak DCS520C", -178, 0,
16111       { 24542,-10860,-3401,-1490,11370,-297,2858,-605,3225 } },
16112     { "Kodak DCS560C", -177, 0,
16113       { 20482,-7172,-3125,-1033,10410,-285,2542,226,3136 } },
16114     { "Kodak DCS620C", -177, 0,
16115       { 23617,-10175,-3149,-2054,11749,-272,2586,-489,3453 } },
16116     { "Kodak DCS620X", -176, 0,
16117       { 13095,-6231,154,12221,-21,-2137,895,4602,2258 } },
16118     { "Kodak DCS660C", -173, 0,
16119       { 18244,-6351,-2739,-791,11193,-521,3711,-129,2802 } },
16120     { "Kodak DCS720X", 0, 0,
16121       { 11775,-5884,950,9556,1846,-1286,-1019,6221,2728 } },
16122     { "Kodak DCS760C", 0, 0,
16123       { 16623,-6309,-1411,-4344,13923,323,2285,274,2926 } },
16124     { "Kodak DCS Pro SLR", 0, 0,
16125       { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
16126     { "Kodak DCS Pro 14nx", 0, 0,
16127       { 5494,2393,-232,-6427,13850,2846,-1876,3997,5445 } },
16128     { "Kodak DCS Pro 14", 0, 0,
16129       { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
16130     { "Photo Control Camerz ZDS 14", 0, 0,
16131       { 7791,3128,-776,-8588,16458,2039,-2455,4006,6198 } },
16132     { "Kodak ProBack645", 0, 0,
16133       { 16414,-6060,-1470,-3555,13037,473,2545,122,4948 } },
16134     { "Kodak ProBack", 0, 0,
16135       { 21179,-8316,-2918,-915,11019,-165,3477,-180,4210 } },
16136     { "Kodak P712", 0, 0,
16137       { 9658,-3314,-823,-5163,12695,2768,-1342,1843,6044 } },
16138     { "Kodak P850", 0, 0xf7c,
16139       { 10511,-3836,-1102,-6946,14587,2558,-1481,1792,6246 } },
16140     { "Kodak P880", 0, 0xfff,
16141       { 12805,-4662,-1376,-7480,15267,2360,-1626,2194,7904 } },
16142     { "Kodak EasyShare Z980", 0, 0,
16143       { 11313,-3559,-1101,-3893,11891,2257,-1214,2398,4908 } },
16144     { "Kodak EasyShare Z981", 0, 0,
16145       { 12729,-4717,-1188,-1367,9187,2582,274,860,4411 } },
16146     { "Kodak EasyShare Z990", 0, 0xfed,
16147       { 11749,-4048,-1309,-1867,10572,1489,-138,1449,4522 } },
16148     { "Kodak EASYSHARE Z1015", 0, 0xef1,
16149       { 11265,-4286,-992,-4694,12343,2647,-1090,1523,5447 } },
16150 
16151     { "Leaf C-Most", 0, 0, /* updated */
16152       { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
16153     { "Leaf Valeo 6", 0, 0,
16154       { 3952,2189,449,-6701,14585,2275,-4536,7349,6536 } },
16155     { "Leaf Aptus 54S", 0, 0,
16156       { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
16157     { "Leaf Aptus-II 8", 0, 0, /* added */
16158       { 7361,1257,-163,-6929,14061,3176,-1839,3454,5603 } },
16159     { "Leaf AFi-II 7", 0, 0, /* added */
16160       { 7691,-108,-339,-6185,13627,2833,-2046,3899,5952 } },
16161     { "Leaf Aptus-II 5", 0, 0, /* added */
16162       { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
16163     { "Leaf Aptus 65", 0, 0,
16164       { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
16165     { "Leaf AFi 65S", 0, 0, /* added */
16166       { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
16167     { "Leaf Aptus 75", 0, 0,
16168       { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
16169     { "Leaf AFi 75S", 0, 0, /* added */
16170       { 7914,1414,-1190,-8777,16582,2280,-2811,4605,5562 } },
16171     { "Leaf Credo 40", 0, 0,
16172       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16173     { "Leaf Credo 50", 0, 0,
16174       { 3984,0,0,0,10000,0,0,0,7666 } },
16175     { "Leaf Credo 60", 0, 0,
16176       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16177     { "Leaf Credo 80", 0, 0,
16178       { 6294,686,-712,-5435, 13417,2211,-1006,2435,5042 } },
16179     { "Leaf", 0, 0,
16180       { 8236,1746,-1314,-8251,15953,2428,-3673,5786,5771 } },
16181 
16182     { "Leica M10", 0, 0, /* added */
16183       { 9090,-3342,-740,-4006,13456,493,-569,2266,6871 } },
16184     { "Leica M9", 0, 0, /* added */
16185       { 6687,-1751,-291,-3556,11373,2492,-548,2204,7146 } },
16186     { "Leica M8", 0, 0, /* added */
16187       { 7675,-2196,-305,-5860,14119,1856,-2425,4006,6578 } },
16188     { "Leica M (Typ 240)", 0, 0, /* added */
16189       { 7199,-2140,-712,-4005,13327,649,-810,2521,6673 } },
16190     { "Leica M (Typ 262)", 0, 0,
16191       { 7199,-2140,-712,-4005,13327,649,-810,2521,6673 } },
16192     { "Leica SL (Typ 601)", 0, 0,
16193       { 11865,-4523,-1441,-5423,14458,935,-1587,2687,4830} },
16194     { "Leica S2", 0, 0, /* added */
16195       { 5627,-721,-447,-4423,12456,2192,-1048,2948,7379 } },
16196     {"Leica S-E (Typ 006)", 0, 0, /* added */
16197       { 5749,-1072,-382,-4274,12432,2048,-1166,3104,7105 } },
16198     {"Leica S (Typ 006)", 0, 0, /* added */
16199       { 5749,-1072,-382,-4274,12432,2048,-1166,3104,7105 } },
16200     { "Leica S (Typ 007)", 0, 0,
16201       { 6063,-2234,-231,-5210,13787,1500,-1043,2866,6997 } },
16202     { "Leica Q (Typ 116)", 0, 0, /* updated */
16203       { 10068,-4043,-1068,-5319,14268,1044,-765,1701,6522 } },
16204     { "Leica T (Typ 701)", 0, 0, /* added */
16205       { 6295 ,-1679 ,-475 ,-5586 ,13046 ,2837 ,-1410 ,1889 ,7075 } },
16206     { "Leica X2", 0, 0, /* added */
16207       { 8336,-2853,-699,-4425,11989,2760,-954,1625,6396 } },
16208     { "Leica X1", 0, 0, /* added */
16209       { 9055,-2611,-666,-4906,12652,2519,-555,1384,7417 } },
16210     { "Leica X", 0, 0, /* X(113), X-U(113), XV, X Vario(107) */ /* updated */
16211       { 9062,-3198,-828,-4065,11772,2603,-761,1468,6458 } },
16212 
16213     { "Mamiya M31", 0, 0, /* added */
16214       { 4516 ,-244 ,-36 ,-7020 ,14976 ,2174 ,-3206 ,4670 ,7087 } },
16215     { "Mamiya M22", 0, 0, /* added */
16216       { 2905 ,732 ,-237 ,-8135 ,16626 ,1476 ,-3038 ,4253 ,7517 } },
16217     { "Mamiya M18", 0, 0, /* added */
16218       { 6516 ,-2050 ,-507 ,-8217 ,16703 ,1479 ,-3492 ,4741 ,8489 } },
16219     { "Mamiya ZD", 0, 0,
16220       { 7645,2579,-1363,-8689,16717,2015,-3712,5941,5961 } },
16221 
16222     { "Micron 2010", 110, 0, /* DJC */
16223       { 16695,-3761,-2151,155,9682,163,3433,951,4904 } },
16224 
16225     { "Minolta DiMAGE 5", 0, 0xf7d, /* updated */
16226       { 9117,-3063,-973,-7949,15763,2306,-2752,3136,8093 } },
16227     { "Minolta DiMAGE 7Hi", 0, 0xf7d, /* updated */
16228       { 11555,-4064,-1256,-7903,15633,2409,-2811,3320,7358 } },
16229     { "Minolta DiMAGE 7i", 0, 0xf7d, /* added */
16230       { 11050,-3791,-1199,-7875,15585,2434,-2797,3359,7560 } },
16231     { "Minolta DiMAGE 7", 0, 0xf7d, /* updated */
16232       { 9258,-2879,-1008,-8076,15847,2351,-2806,3280,7821 } },
16233     { "Minolta DiMAGE A1", 0, 0xf8b, /* updated */
16234       { 9274,-2548,-1167,-8220,16324,1943,-2273,2721,8340 } },
16235     { "Minolta DiMAGE A200", 0, 0,
16236       { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
16237     { "Minolta DiMAGE A2", 0, 0xf8f,
16238       { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
16239     { "Minolta DiMAGE Z2", 0, 0, /* DJC */
16240       { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
16241     { "Minolta DYNAX 5", 0, 0xffb,
16242       { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
16243     { "Minolta Maxxum 5D", 0, 0xffb, /* added */
16244       { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
16245     { "Minolta ALPHA-5 DIGITAL", 0, 0xffb, /* added */
16246       { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
16247     { "Minolta ALPHA SWEET DIGITAL", 0, 0xffb, /* added */
16248       { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
16249     { "Minolta DYNAX 7", 0, 0xffb,
16250       { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
16251     { "Minolta Maxxum 7D", 0, 0xffb, /* added */
16252       { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
16253     { "Minolta ALPHA-7 DIGITAL", 0, 0xffb, /* added */
16254       { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
16255 
16256     { "Motorola PIXL", 0, 0, /* DJC */
16257       { 8898,-989,-1033,-3292,11619,1674,-661,3178,5216 } },
16258 
16259     { "Nikon D100", 0, 0,
16260       { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
16261     { "Nikon D1H", 0, 0, /* updated */
16262       { 7659,-2238,-935,-8942,16969,2004,-2701,3051,8690 } },
16263     { "Nikon D1X", 0, 0,
16264       { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
16265     { "Nikon D1", 0, 0, /* multiplied by 2.218750, 1.0, 1.148438 */
16266       { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
16267     { "Nikon D200", 0, 0xfbc,
16268       { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
16269     { "Nikon D2H", 0, 0,
16270       { 5733,-911,-629,-7967,15987,2055,-3050,4013,7048 } },
16271     { "Nikon D2X", 0, 0, /* updated */
16272       { 10231,-2768,-1254,-8302,15900,2551,-797,681,7148 } },
16273     { "Nikon D3000", 0, 0,
16274       { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
16275     { "Nikon D3100", 0, 0,
16276       { 7911,-2167,-813,-5327,13150,2408,-1288,2483,7968 } },
16277     { "Nikon D3200", 0, 0xfb9,
16278       { 7013,-1408,-635,-5268,12902,2640,-1470,2801,7379 } },
16279     { "Nikon D3300", 0, 0,
16280       { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
16281     { "Nikon D3400", 0, 0,
16282       { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
16283     { "Nikon D300", 0, 0,
16284       { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
16285     { "Nikon D3X", 0, 0,
16286       { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
16287     { "Nikon D3S", 0, 0,
16288       { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
16289     { "Nikon D3", 0, 0,
16290       { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
16291     { "Nikon D40X", 0, 0,
16292       { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
16293     { "Nikon D40", 0, 0,
16294       { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
16295     { "Nikon D4S", 0, 0,
16296       { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
16297     { "Nikon D4", 0, 0,
16298       { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
16299     { "Nikon Df", 0, 0,
16300       { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
16301     { "Nikon D5000", 0, 0xf00,
16302       { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
16303     { "Nikon D5100", 0, 0x3de6,
16304       { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
16305     { "Nikon D5200", 0, 0,
16306       { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
16307     { "Nikon D5300", 0, 0,
16308       { 6988,-1384,-714,-5631,13410,2447,-1485,2204,7318 } },
16309     { "Nikon D5500", 0, 0,
16310       { 8821,-2938,-785,-4178,12142,2287,-824,1651,6860 } },
16311     { "Nikon D5600", 0, 0,
16312       { 8821,-2938,-785,-4178,12142,2287,-824,1651,6860 } },
16313     { "Nikon D500", 0, 0,
16314         { 8813,-3210,-1036,-4703,12868,2021,-1054,1940,6129 } },
16315     { "Nikon D50", 0, 0,
16316       { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
16317     { "Nikon D5", 0, 0,
16318       { 9200,-3522,-992,-5755,13803,2117,-753,1486,6338 } },
16319     { "Nikon D600", 0, 0x3e07,
16320       { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
16321     { "Nikon D610",0, 0, /* updated */
16322       { 8178,-2245,-609,-4857,12394,2776,-1207,2086,7298 } },
16323     { "Nikon D60", 0, 0,
16324       { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
16325     { "Nikon D7000", 0, 0,
16326       { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
16327     { "Nikon D7100", 0, 0,
16328       { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
16329     { "Nikon D7200", 0, 0,
16330       { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
16331     { "Nikon D7500", 0, 0,
16332       { 8813,-3210,-1036,-4703,12868,2021,-1054,1940,6129 } },
16333     { "Nikon D750", -600, 0,
16334       { 9020,-2890,-715,-4535,12436,2348,-934,1919,7086 } },
16335     { "Nikon D700", 0, 0,
16336       { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
16337     { "Nikon D70", 0, 0,
16338       { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
16339     { "Nikon D850", 0, 0,
16340       { 10405,-3755,-1270,-5461,13787,1793,-1040,2015,6785 } },
16341     { "Nikon D810A", 0, 0,
16342       { 11973,-5685,-888,-1965,10326,1901,-115,1123,7169 } },
16343     { "Nikon D810", 0, 0,
16344       { 9369,-3195,-791,-4488,12430,2301,-893,1796,6872 } },
16345     { "Nikon D800", 0, 0,
16346       { 7866,-2108,-555,-4869,12483,2681,-1176,2069,7501 } },
16347     { "Nikon D80", 0, 0,
16348       { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
16349     { "Nikon D90", 0, 0xf00,
16350       { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
16351     { "Nikon E700", 0, 0x3dd, /* DJC */
16352       { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
16353     { "Nikon E800", 0, 0x3dd, /* DJC */
16354       { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
16355     { "Nikon E950", 0, 0x3dd, /* DJC */
16356       { -3746,10611,1665,9621,-1734,2114,-2389,7082,3064,3406,6116,-244 } },
16357     { "Nikon E995", 0, 0, /* copied from E5000 */
16358       { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
16359     { "Nikon E2100", 0, 0, /* copied from Z2, new white balance */
16360       { 13142,-4152,-1596,-4655,12374,2282,-1769,2696,6711 } },
16361     { "Nikon E2500", 0, 0,
16362       { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
16363     { "Nikon E3200", 0, 0, /* DJC */
16364       { 9846,-2085,-1019,-3278,11109,2170,-774,2134,5745 } },
16365     { "Nikon E4300", 0, 0, /* copied from Minolta DiMAGE Z2 */
16366       { 11280,-3564,-1370,-4655,12374,2282,-1423,2168,5396 } },
16367     { "Nikon E4500", 0, 0,
16368       { -5547,11762,2189,5814,-558,3342,-4924,9840,5949,688,9083,96 } },
16369     { "Nikon E5000", 0, 0, /* updated */
16370       { -6678,12805,2248,5725,-499,3375,-5903,10713,6034,-270,9976,134 } },
16371     { "Nikon E5400", 0, 0, /* updated */
16372       { 9349,-2988,-1001,-7918,15766,2266,-2097,2680,6839 } },
16373     { "Nikon E5700", 0, 0, /* updated */
16374       { -6475,12496,2428,5409,-16,3180,-5965,10912,5866,-177,9918,248 } },
16375     { "Nikon E8400", 0, 0,
16376       { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
16377     { "Nikon E8700", 0, 0,
16378       { 8489,-2583,-1036,-8051,15583,2643,-1307,1407,7354 } },
16379     { "Nikon E8800", 0, 0,
16380       { 7971,-2314,-913,-8451,15762,2894,-1442,1520,7610 } },
16381     { "Nikon COOLPIX A", 0, 0,
16382       { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
16383     { "Nikon COOLPIX B700", 0, 0,
16384       { 14387,-6014,-1299,-1357,9975,1616,467,1047,4744 } },
16385     { "Nikon COOLPIX P330", -200, 0,
16386       { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
16387     { "Nikon COOLPIX P340", -200, 0,
16388       { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
16389     { "Nikon COOLPIX Kalon", 0, 0, /* added */
16390       { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
16391     { "Nikon COOLPIX Deneb", 0, 0, /* added */
16392       { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
16393     { "Nikon COOLPIX P6000", 0, 0,
16394       { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
16395     { "Nikon COOLPIX P7000", 0, 0,
16396       { 11432,-3679,-1111,-3169,11239,2202,-791,1380,4455 } },
16397     { "Nikon COOLPIX P7100", 0, 0,
16398       { 11053,-4269,-1024,-1976,10182,2088,-526,1263,4469 } },
16399     { "Nikon COOLPIX P7700", -3200, 0,
16400       { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
16401     { "Nikon COOLPIX P7800", -3200, 0,
16402       { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
16403     { "Nikon 1 V3", -200, 0,
16404       { 5958,-1559,-571,-4021,11453,2939,-634,1548,5087 } },
16405     { "Nikon 1 J4", 0, 0,
16406       { 5958,-1559,-571,-4021,11453,2939,-634,1548,5087 } },
16407     { "Nikon 1 J5", 0, 0,
16408       { 7520,-2518,-645,-3844,12102,1945,-913,2249,6835 } },
16409     { "Nikon 1 S2", -200, 0,
16410       { 6612,-1342,-618,-3338,11055,2623,-174,1792,5075 } },
16411     { "Nikon 1 V2", 0, 0,
16412       { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
16413     { "Nikon 1 J3", 0, 0, /* updated */
16414       { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
16415     { "Nikon 1 AW1", 0, 0,
16416       { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
16417     { "Nikon 1 ", 0, 0, /* J1, J2, S1, V1 */
16418       { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
16419 
16420     { "Olympus AIR-A01", 0, 0xfe1,
16421       { 8992,-3093,-639,-2563,10721,2122,-437,1270,5473 } },
16422     { "Olympus C5050", 0, 0, /* updated */
16423       { 10633,-3234,-1285,-7460,15570,1967,-1917,2510,6299 } },
16424     { "Olympus C5060", 0, 0,
16425       { 10445,-3362,-1307,-7662,15690,2058,-1135,1176,7602 } },
16426     { "Olympus C7070", 0, 0,
16427       { 10252,-3531,-1095,-7114,14850,2436,-1451,1723,6365 } },
16428     { "Olympus C70", 0, 0,
16429       { 10793,-3791,-1146,-7498,15177,2488,-1390,1577,7321 } },
16430     { "Olympus C80", 0, 0,
16431       { 8606,-2509,-1014,-8238,15714,2703,-942,979,7760 } },
16432     { "Olympus E-10", 0, 0xffc, /* updated */
16433       { 12970,-4703,-1433,-7466,15843,1644,-2191,2451,6668 } },
16434     { "Olympus E-1", 0, 0,
16435       { 11846,-4767,-945,-7027,15878,1089,-2699,4122,8311 } },
16436     { "Olympus E-20", 0, 0xffc, /* updated */
16437       { 13414,-4950,-1517,-7166,15293,1960,-2325,2664,7212 } },
16438     { "Olympus E-300", 0, 0,
16439       { 7828,-1761,-348,-5788,14071,1830,-2853,4518,6557 } },
16440     { "Olympus E-330", 0, 0,
16441       { 8961,-2473,-1084,-7979,15990,2067,-2319,3035,8249 } },
16442     { "Olympus E-30", 0, 0xfbc,
16443       { 8144,-1861,-1111,-7763,15894,1929,-1865,2542,7607 } },
16444     { "Olympus E-3", 0, 0xf99,
16445       { 9487,-2875,-1115,-7533,15606,2010,-1618,2100,7389 } },
16446     { "Olympus E-400", 0, 0,
16447       { 6169,-1483,-21,-7107,14761,2536,-2904,3580,8568 } },
16448     { "Olympus E-410", 0, 0xf6a,
16449       { 8856,-2582,-1026,-7761,15766,2082,-2009,2575,7469 } },
16450     { "Olympus E-420", 0, 0xfd7,
16451       { 8746,-2425,-1095,-7594,15612,2073,-1780,2309,7416 } },
16452     { "Olympus E-450", 0, 0xfd2,
16453       { 8745,-2425,-1095,-7594,15613,2073,-1780,2309,7416 } },
16454     { "Olympus E-500", 0, 0,
16455       { 8136,-1968,-299,-5481,13742,1871,-2556,4205,6630 } },
16456     { "Olympus E-510", 0, 0xf6a,
16457       { 8785,-2529,-1033,-7639,15624,2112,-1783,2300,7817 } },
16458     { "Olympus E-520", 0, 0xfd2,
16459       { 8344,-2322,-1020,-7596,15635,2048,-1748,2269,7287 } },
16460     { "Olympus E-5", 0, 0xeec,
16461       { 11200,-3783,-1325,-4576,12593,2206,-695,1742,7504 } },
16462     { "Olympus E-600", 0, 0xfaf,
16463       { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
16464     { "Olympus E-620", 0, 0xfaf,
16465       { 8453,-2198,-1092,-7609,15681,2008,-1725,2337,7824 } },
16466     { "Olympus E-P1", 0, 0xffd,
16467       { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
16468     { "Olympus E-P2", 0, 0xffd,
16469       { 8343,-2050,-1021,-7715,15705,2103,-1831,2380,8235 } },
16470     { "Olympus E-P3", 0, 0,
16471       { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
16472     { "Olympus E-P5", 0, 0,
16473       { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
16474     { "Olympus E-PL1s", 0, 0,
16475       { 11409,-3872,-1393,-4572,12757,2003,-709,1810,7415 } },
16476     { "Olympus E-PL1", 0, 0,
16477       { 11408,-4289,-1215,-4286,12385,2118,-387,1467,7787 } },
16478     { "Olympus E-PL2", 0, 0xcf3,
16479       { 15030,-5552,-1806,-3987,12387,1767,-592,1670,7023 } },
16480     { "Olympus E-PL3", 0, 0,
16481       { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
16482     { "Olympus E-PL5", 0, 0xfcb,
16483       { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
16484     { "Olympus E-PL6", 0, 0,
16485       { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
16486     { "Olympus E-PL7", 0, 0,
16487       { 9197,-3190,-659,-2606,10830,2039,-458,1250,5458 } },
16488     { "Olympus E-PL8", 0, 0,
16489       { 9197,-3190,-659,-2606,10830,2039,-458,1250,5458 } },
16490     { "Olympus E-PL9", 0, 0,
16491       { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
16492     { "Olympus E-PM1", 0, 0,
16493       { 7575,-2159,-571,-3722,11341,2725,-1434,2819,6271 } },
16494     { "Olympus E-PM2", 0, 0,
16495       { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
16496     { "Olympus E-M10", 0, 0, /* Same for E-M10MarkII, E-M10MarkIII */
16497       { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
16498     { "Olympus E-M1MarkII", 0, 0,
16499       { 9383,-3170,-763,-2457,10702,2020,-384,1236,5552 } },
16500     { "Olympus E-M1", 0, 0,
16501       { 7687,-1984,-606,-4327,11928,2721,-1381,2339,6452 } },
16502     { "Olympus E-M5MarkII", 0, 0,
16503       { 9422,-3258,-711,-2655,10898,2015,-512,1354,5512 } },
16504     { "Olympus E-M5", 0, 0xfe1,
16505       { 8380,-2630,-639,-2887,10725,2496,-627,1427,5438 } },
16506     { "Olympus PEN-F",0, 0,
16507       { 9476,-3182,-765,-2613,10958,1893,-449,1315,5268 } },
16508     { "Olympus SP350", 0, 0,
16509       { 12078,-4836,-1069,-6671,14306,2578,-786,939,7418 } },
16510     { "Olympus SP3", 0, 0,
16511       { 11766,-4445,-1067,-6901,14421,2707,-1029,1217,7572 } },
16512     { "Olympus SP500UZ", 0, 0xfff,
16513       { 9493,-3415,-666,-5211,12334,3260,-1548,2262,6482 } },
16514     { "Olympus SP510UZ", 0, 0xffe,
16515       { 10593,-3607,-1010,-5881,13127,3084,-1200,1805,6721 } },
16516     { "Olympus SP550UZ", 0, 0xffe,
16517       { 11597,-4006,-1049,-5432,12799,2957,-1029,1750,6516 } },
16518     { "Olympus SP560UZ", 0, 0xff9,
16519       { 10915,-3677,-982,-5587,12986,2911,-1168,1968,6223 } },
16520     { "Olympus SP565UZ", 0, 0, /* added */
16521       { 11856,-4469,-1159,-4814,12368,2756,-993,1779,5589 } },
16522     { "Olympus SP570UZ", 0, 0,
16523       { 11522,-4044,-1146,-4736,12172,2904,-988,1829,6039 } },
16524     { "Olympus SH-2", 0, 0,
16525      { 10156,-3425,-1077,-2611,11177,1624,-385,1592,5080 } },
16526     { "Olympus SH-3", 0, 0, /* Alias of SH-2 */
16527      { 10156,-3425,-1077,-2611,11177,1624,-385,1592,5080 } },
16528     { "Olympus STYLUS1",0, 0, /* updated */
16529       { 8360,-2420,-880,-3928,12353,1739,-1381,2416,5173 } },
16530     { "Olympus TG-4", 0, 0,
16531      { 11426,-4159,-1126,-2066,10678,1593,-120,1327,4998 } },
16532     { "Olympus TG-5", 0, 0,
16533      { 10899,-3833,-1082,-2112,10736,1575,-267,1452,5269 } },
16534     { "Olympus XZ-10", 0, 0,
16535       { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
16536     { "Olympus XZ-1", 0, 0,
16537       { 10901,-4095,-1074,-1141,9208,2293,-62,1417,5158 } },
16538     { "Olympus XZ-2", 0, 0,
16539       { 9777,-3483,-925,-2886,11297,1800,-602,1663,5134 } },
16540 
16541     { "OmniVision", 16, 0x3ff,
16542       { 12782,-4059,-379,-478,9066,1413,1340,1513,5176 } }, /* DJC */
16543 
16544     { "Pentax *ist DL2", 0, 0,
16545       { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
16546     { "Pentax *ist DL", 0, 0,
16547       { 10829,-2838,-1115,-8339,15817,2696,-837,680,11939 } },
16548     { "Pentax *ist DS2", 0, 0,
16549       { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
16550     { "Pentax *ist DS", 0, 0,
16551       { 10371,-2333,-1206,-8688,16231,2602,-1230,1116,11282 } },
16552     { "Pentax *ist D", 0, 0,
16553       { 9651,-2059,-1189,-8881,16512,2487,-1460,1345,10687 } },
16554     { "Pentax GR", 0, 0, /* added */
16555       { 5329,-1459,-390,-5407,12930,2768,-1119,1772,6046 } },
16556     { "Pentax K-01", 0, 0, /* added */
16557       { 8134,-2728,-645,-4365,11987,2694,-838,1509,6498 } },
16558     { "Pentax K10D", 0, 0, /* updated */
16559       { 9679,-2965,-811,-8622,16514,2182,-975,883,9793 } },
16560     { "Pentax K1", 0, 0,
16561       { 11095,-3157,-1324,-8377,15834,2720,-1108,947,11688 } },
16562     { "Pentax K20D", 0, 0,
16563       { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
16564     { "Pentax K200D", 0, 0,
16565       { 9186,-2678,-907,-8693,16517,2260,-1129,1094,8524 } },
16566     { "Pentax K2000", 0, 0, /* updated */
16567       { 9730,-2989,-970,-8527,16258,2381,-1060,970,8362 } },
16568     { "Pentax K-m", 0, 0, /* updated */
16569       { 9730,-2989,-970,-8527,16258,2381,-1060,970,8362 } },
16570     { "Pentax KP", 0, 0,
16571       { 7825,-2160,-1403,-4841,13555,1349,-1559,2449,5814 } },
16572     { "Pentax K-x", 0, 0,
16573       { 8843,-2837,-625,-5025,12644,2668,-411,1234,7410 } },
16574     { "Pentax K-r", 0, 0,
16575       { 9895,-3077,-850,-5304,13035,2521,-883,1768,6936 } },
16576     { "Pentax K-1", 0, 0, /* updated */
16577       { 8596,-2981,-639,-4202,12046,2431,-685,1424,6122 } },
16578     { "Pentax K-30", 0, 0, /* updated */
16579       { 8134,-2728,-645,-4365,11987,2694,-838,1509,6498 } },
16580     { "Pentax K-3 II", 0, 0, /* updated */
16581       { 7415,-2052,-721,-5186,12788,2682,-1446,2157,6773 } },
16582     { "Pentax K-3", 0, 0,
16583       { 7415,-2052,-721,-5186,12788,2682,-1446,2157,6773 } },
16584     { "Pentax K-5 II", 0, 0,
16585       { 8170,-2725,-639,-4440,12017,2744,-771,1465,6599 } },
16586     { "Pentax K-500", 0, 0, /* added */
16587       { 8109,-2740,-608,-4593,12175,2731,-1006,1515,6545 } },
16588     { "Pentax K-50", 0, 0, /* added */
16589       { 8109,-2740,-608,-4593,12175,2731,-1006,1515,6545 } },
16590     { "Pentax K-5", 0, 0,
16591       { 8713,-2833,-743,-4342,11900,2772,-722,1543,6247 } },
16592     { "Pentax K-70", 0, 0,
16593       { 8766,-3149,-747,-3976,11943,2292,-517,1259,5552 } },
16594     { "Pentax K-7", 0, 0,
16595       { 9142,-2947,-678,-8648,16967,1663,-2224,2898,8615 } },
16596     { "Pentax KP", 0, 0,  /* temp */
16597       { 8626,-2607,-1155,-3995,12301,1881,-1039,1822,6925 } },
16598     { "Pentax K-S1", 0, 0,
16599       { 8512,-3211,-787,-4167,11966,2487,-638,1288,6054 } },
16600     { "Pentax K-S2", 0, 0,
16601       { 8662,-3280,-798,-3928,11771,2444,-586,1232,6054 } },
16602     { "Pentax Q-S1", 0, 0,
16603       { 12995,-5593,-1107,-1879,10139,2027,-64,1233,4919 } },
16604     { "Pentax Q7", 0, 0, /* added */
16605       { 10901,-3938,-1025,-2743,11210,1738,-823,1805,5344 } },
16606     { "Pentax Q10", 0, 0, /* updated */
16607       { 11562,-4183,-1172,-2357,10919,1641,-582,1726,5112 } },
16608     { "Pentax Q", 0, 0, /* added */
16609       { 11731,-4169,-1267,-2015,10727,1473,-217,1492,4870 } },
16610     { "Pentax MX-1", 0, 0, /* updated */
16611       { 9296,-3146,-888,-2860,11287,1783,-618,1698,5151 } },
16612     { "Pentax 645D", 0, 0x3e00,
16613       { 10646,-3593,-1158,-3329,11699,1831,-667,2874,6287 } },
16614     { "Pentax 645Z", 0, 0, /* updated */
16615       { 9519,-3591,-664,-4074,11725,2671,-624,1501,6653 } },
16616 
16617     { "Panasonic DMC-CM10", -15, 0,
16618       { 8770,-3194,-820,-2871,11281,1803,-513,1552,4434 } },
16619     { "Panasonic DMC-CM1", -15, 0,
16620       { 8770,-3194,-820,-2871,11281,1803,-513,1552,4434 } },
16621     { "Panasonic DC-FZ82", -15, 0, /* markets: FZ80 FZ82 */
16622       { 8550,-2908,-842,-3195,11529,1881,-338,1603,4631 } },
16623     { "Panasonic DC-FZ80", -15, 0, /* markets: FZ80 FZ82 */
16624       { 8550,-2908,-842,-3195,11529,1881,-338,1603,4631 } },
16625     { "Panasonic DMC-FZ8", 0, 0xf7f,
16626       { 8986,-2755,-802,-6341,13575,3077,-1476,2144,6379 } },
16627     { "Panasonic DMC-FZ18", 0, 0,
16628       { 9932,-3060,-935,-5809,13331,2753,-1267,2155,5575 } },
16629     { "Panasonic DMC-FZ28", -15, 0xf96,
16630       { 10109,-3488,-993,-5412,12812,2916,-1305,2140,5543 } },
16631     { "Panasonic DMC-FZ300", -15, 0xfff,
16632       { 8378,-2798,-769,-3068,11410,1877,-538,1792,4623 } },
16633     { "Panasonic DMC-FZ330", -15, 0xfff,
16634       { 8378,-2798,-769,-3068,11410,1877,-538,1792,4623 } },
16635     { "Panasonic DMC-FZ30", 0, 0xf94,
16636       { 10976,-4029,-1141,-7918,15491,2600,-1670,2071,8246 } },
16637     { "Panasonic DMC-FZ3", -15, 0,
16638       { 9938,-2780,-890,-4604,12393,2480,-1117,2304,4620 } },
16639     { "Panasonic DMC-FZ4", -15, 0, /* 40,42,45 */
16640       { 13639,-5535,-1371,-1698,9633,2430,316,1152,4108 } },
16641     { "Panasonic DMC-FZ50", 0, 0,
16642       { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
16643     { "Panasonic DMC-FZ7", -15, 0,
16644       { 11532,-4324,-1066,-2375,10847,1749,-564,1699,4351 } },
16645     { "Leica V-LUX1", 0, 0,
16646       { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
16647     { "Leica V-LUX 1", 0, 0,
16648       { 7906,-2709,-594,-6231,13351,3220,-1922,2631,6537 } },
16649     { "Panasonic DMC-L10", -15, 0xf96,
16650       { 8025,-1942,-1050,-7920,15904,2100,-2456,3005,7039 } },
16651     { "Panasonic DMC-L1", 0, 0xf7f,
16652       { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
16653     { "Leica DIGILUX3", 0, 0xf7f, /* added */
16654       { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
16655     { "Leica DIGILUX 3", 0, 0xf7f,
16656       { 8054,-1885,-1025,-8349,16367,2040,-2805,3542,7629 } },
16657     { "Panasonic DMC-LC1", 0, 0,
16658       { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
16659     { "Leica DIGILUX2", 0, 0, /* added */
16660       { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
16661     { "Leica DIGILUX 2", 0, 0,
16662       { 11340,-4069,-1275,-7555,15266,2448,-2960,3426,7685 } },
16663     { "Panasonic DMC-LX100", -15, 0,
16664       { 8844,-3538,-768,-3709,11762,2200,-698,1792,5220 } },
16665     { "Leica D-LUX (Typ 109)", -15, 0,
16666       { 8844,-3538,-768,-3709,11762,2200,-698,1792,5220 } },
16667     { "Panasonic DMC-LF1", -15, 0,
16668       { 9379,-3267,-816,-3227,11560,1881,-926,1928,5340 } },
16669     { "Leica C (Typ 112)", -15, 0,
16670       { 9379,-3267,-816,-3227,11560,1881,-926,1928,5340 } },
16671     { "Panasonic DMC-LX9", -15, 0, /* markets: LX9 LX10 LX15 */
16672       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16673     { "Panasonic DMC-LX10", -15, 0, /* markets: LX9 LX10 LX15 */
16674       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16675     { "Panasonic DMC-LX15", -15, 0, /* markets: LX9 LX10 LX15 */
16676       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16677     { "Panasonic DMC-LX1", 0, 0xf7f,
16678       { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
16679     { "Leica D-Lux (Typ 109)", 0, 0xf7f,
16680       { 8844,-3538,-768,-3709,11762,2200,-698,1792,5220 } },
16681     { "Leica D-LUX2", 0, 0xf7f,
16682       { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
16683     { "Leica D-LUX 2", 0, 0xf7f, /* added */
16684       { 10704,-4187,-1230,-8314,15952,2501,-920,945,8927 } },
16685     { "Panasonic DMC-LX2", 0, 0,
16686       { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
16687     { "Leica D-LUX3", 0, 0,
16688       { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
16689     { "Leica D-LUX 3", 0, 0, /* added */
16690       { 8048,-2810,-623,-6450,13519,3272,-1700,2146,7049 } },
16691     { "Panasonic DMC-LX3", -15, 0,
16692       { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
16693     { "Leica D-LUX 4", -15, 0,
16694       { 8128,-2668,-655,-6134,13307,3161,-1782,2568,6083 } },
16695     { "Panasonic DMC-LX5", -15, 0,
16696       { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
16697     { "Leica D-LUX 5", -15, 0,
16698       { 10909,-4295,-948,-1333,9306,2399,22,1738,4582 } },
16699     { "Panasonic DMC-LX7", -15, 0,
16700       { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
16701     { "Leica D-LUX 6", -15, 0,
16702       { 10148,-3743,-991,-2837,11366,1659,-701,1893,4899 } },
16703     { "Panasonic DMC-FZ1000", -15, 0,
16704       { 7830,-2696,-763,-3325,11667,1866,-641,1712,4824 } },
16705     { "Leica V-LUX (Typ 114)", 15, 0,
16706       { 7830,-2696,-763,-3325,11667,1866,-641,1712,4824 } },
16707     { "Panasonic DMC-FZ100", -15, 0xfff,
16708       { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
16709     { "Leica V-LUX 2", -15, 0xfff,
16710       { 16197,-6146,-1761,-2393,10765,1869,366,2238,5248 } },
16711     { "Panasonic DMC-FZ150", -15, 0xfff,
16712       { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
16713     { "Leica V-LUX 3", -15, 0xfff,
16714       { 11904,-4541,-1189,-2355,10899,1662,-296,1586,4289 } },
16715     { "Panasonic DMC-FZ2000", -15, 0, /* markets: DMC-FZ2000, DMC-FZ2500 ,FZH1 */
16716       { 7386,-2443,-743,-3437,11864,1757,-608,1660,4766 } },
16717     { "Panasonic DMC-FZ2500", -15, 0,
16718       { 7386,-2443,-743,-3437,11864,1757,-608,1660,4766 } },
16719     { "Panasonic DMC-FZH1", -15, 0,
16720       { 7386,-2443,-743,-3437,11864,1757,-608,1660,4766 } },
16721     { "Panasonic DMC-FZ200", -15, 0xfff,
16722       { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
16723     { "Leica V-LUX 4", -15, 0xfff,
16724       { 8112,-2563,-740,-3730,11784,2197,-941,2075,4933 } },
16725     { "Panasonic DMC-FX150", -15, 0xfff,
16726       { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
16727     { "Panasonic DMC-FX180", -15, 0xfff, /* added */
16728       { 9082,-2907,-925,-6119,13377,3058,-1797,2641,5609 } },
16729     { "Panasonic DMC-G10", 0, 0,
16730       { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
16731     { "Panasonic DMC-G1", -15, 0xf94,
16732       { 8199,-2065,-1056,-8124,16156,2033,-2458,3022,7220 } },
16733     { "Panasonic DMC-G2", -15, 0xf3c,
16734       { 10113,-3400,-1114,-4765,12683,2317,-377,1437,6710 } },
16735     { "Panasonic DMC-G3", -15, 0xfff,
16736       { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
16737     { "Panasonic DMC-G5", -15, 0xfff,
16738       { 7798,-2562,-740,-3879,11584,2613,-1055,2248,5434 } },
16739     { "Panasonic DMC-G6", -15, 0xfff,
16740       { 8294,-2891,-651,-3869,11590,2595,-1183,2267,5352 } },
16741     { "Panasonic DMC-G7", -15, 0xfff,
16742       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16743     { "Panasonic DMC-G8", -15, 0xfff, /* markets: DMC-G8, DMC-G80, DMC-G81, DMC-G85 */
16744       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16745     { "Panasonic DC-G9", -15, 0,
16746       { 7685,-2375,-634,-3687,11700,2249,-748,1546,5111 } },
16747     { "Panasonic DMC-GF1", -15, 0xf92,
16748       { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
16749     { "Panasonic DMC-GF2", -15, 0xfff,
16750       { 7888,-1902,-1011,-8106,16085,2099,-2353,2866,7330 } },
16751     { "Panasonic DMC-GF3", -15, 0xfff,
16752       { 9051,-2468,-1204,-5212,13276,2121,-1197,2510,6890 } },
16753     { "Panasonic DMC-GF5", -15, 0xfff,
16754       { 8228,-2945,-660,-3938,11792,2430,-1094,2278,5793 } },
16755     { "Panasonic DMC-GF6", -15, 0,
16756       { 8130,-2801,-946,-3520,11289,2552,-1314,2511,5791 } },
16757     { "Panasonic DMC-GF7", -15, 0,
16758       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16759     { "Panasonic DMC-GF8", -15, 0,
16760       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16761     { "Panasonic DMC-GH1", -15, 0xf92,
16762       { 6299,-1466,-532,-6535,13852,2969,-2331,3112,5984 } },
16763     { "Panasonic DMC-GH2", -15, 0xf95,
16764       { 7780,-2410,-806,-3913,11724,2484,-1018,2390,5298 } },
16765     { "Panasonic DMC-GH3", -15, 0,
16766       { 6559,-1752,-491,-3672,11407,2586,-962,1875,5130 } },
16767     { "Panasonic DMC-GH4", -15, 0,
16768       { 7122,-2108,-512,-3155,11201,2231,-541,1423,5045 } },
16769     { "Panasonic AG-GH4", -15, 0, /* added */
16770       { 7122,-2108,-512,-3155,11201,2231,-541,1423,5045 } },
16771     {"Panasonic DC-GH5s", -15, 0,
16772       { 6929,-2355,-708,-4192,12534,1828,-1097,1989,5195 } },
16773     { "Panasonic DC-GH5", -15, 0,
16774       { 7641,-2336,-605,-3218,11299,2187,-485,1338,5121 } },
16775     { "Yuneec CGO4", -15, 0,
16776       { 7122,-2108,-512,-3155,11201,2231,-541,1423,5045 } },
16777     { "Panasonic DMC-GM1", -15, 0,
16778       { 6770,-1895,-744,-5232,13145,2303,-1664,2691,5703 } },
16779     { "Panasonic DMC-GM5", -15, 0,
16780       { 8238,-3244,-679,-3921,11814,2384,-836,2022,5852 } },
16781     { "Panasonic DMC-GX1", -15, 0,
16782       { 6763,-1919,-863,-3868,11515,2684,-1216,2387,5879 } },
16783 
16784     { "Panasonic DC-GF10", -15, 0, /* temp, markets: GF10, GF90 */
16785       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16786     { "Panasonic DC-GF90", -15, 0, /* temp, markets: GF10, GF90 */
16787       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16788 
16789     { "Panasonic DC-GX850", -15, 0, /* markets: GX850 GX800 GF9 */
16790       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16791     { "Panasonic DC-GX800", -15, 0, /* markets: GX850 GX800 GF9 */
16792       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16793     { "Panasonic DC-GF9", -15, 0, /* markets: GX850 GX800 GF9 */
16794       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16795     { "Panasonic DMC-GX85", -15, 0, /* markets: GX85 GX80 GX7MK2 */
16796       { 7771,-3020,-629,-4029,11950,2345,-821,1977,6119 } },
16797     { "Panasonic DMC-GX80", -15, 0, /* markets: GX85 GX80 GX7MK2 */
16798       { 7771,-3020,-629,-4029,11950,2345,-821,1977,6119 } },
16799     { "Panasonic DMC-GX7MK2", -15, 0, /* markets: GX85 GX80 GX7MK2 */
16800       { 7771,-3020,-629,-4029,11950,2345,-821,1977,6119 } },
16801     { "Panasonic DMC-GX7", -15,0,
16802       { 7610,-2780,-576,-4614,12195,2733,-1375,2393,6490 } },
16803     { "Panasonic DMC-GX8", -15,0,
16804       { 7564,-2263,-606,-3148,11239,2177,-540,1435,4853 } },
16805 
16806     { "Panasonic DC-GX9", -15, 0, /* temp */
16807       { 7685,-2375,-634,-3687,11700,2249,-748,1546,5111 } },
16808 
16809     { "Panasonic DMC-TZ6", -15, 0,  /* markets: ZS40 TZ60 TZ61 */
16810       { 8607,-2822,-808,-3755,11930,2049,-820,2060,5224 } },
16811     { "Panasonic DMC-TZ8", -15, 0, /* markets: ZS60 TZ80 TZ81 TZ82 TZ85 */
16812       { 8550,-2908,-842,-3195,11529,1881,-338,1603,4631 } },
16813     { "Panasonic DC-TZ90", -15, 0, /* markets: ZS70  TZ90 TZ91 TZ92 T93 */
16814       { 9052,-3117,-883,-3045,11346,1927,-205,1520,4730 } },
16815     { "Panasonic DC-TZ91", -15, 0, /* markets: ZS70  TZ90 TZ91 TZ92 T93 */
16816       { 9052,-3117,-883,-3045,11346,1927,-205,1520,4730 } },
16817     { "Panasonic DC-TZ92", -15, 0, /* markets: ZS70  TZ90 TZ91 TZ92 T93 */
16818       { 9052,-3117,-883,-3045,11346,1927,-205,1520,4730 } },
16819     { "Panasonic DC-T93", -15, 0, /* markets: ZS70  TZ90 TZ91 TZ92 T93 */
16820       { 9052,-3117,-883,-3045,11346,1927,-205,1520,4730 } },
16821     { "Panasonic DMC-ZS4", -15, 0, /* markets: ZS40 TZ60 TZ61 */
16822       { 8607,-2822,-808,-3755,11930,2049,-820,2060,5224 } },
16823     { "Panasonic DMC-TZ7", -15, 0, /* markets: ZS50 TZ70 TZ71 */
16824       { 8802,-3135,-789,-3151,11468,1904,-550,1745,4810 } },
16825     { "Panasonic DMC-ZS5", -15, 0, /* markets: ZS50 TZ70 TZ71 */
16826       { 8802,-3135,-789,-3151,11468,1904,-550,1745,4810 } },
16827     { "Panasonic DMC-ZS6", -15, 0, /* markets: ZS60 TZ80 TZ81 TZ85 */
16828       { 8550,-2908,-842,-3195,11529,1881,-338,1603,4631 } },
16829     { "Panasonic DC-ZS70", -15, 0, /* markets: ZS70  TZ90 TZ91 TZ92 T93 */
16830       { 9052,-3117,-883,-3045,11346,1927,-205,1520,4730 } },
16831     { "Panasonic DMC-ZS100", -15, 0, /* markets: ZS100 ZS110 TZ100 TZ101 TZ110 TX1 */
16832       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16833     { "Panasonic DMC-ZS110", -15, 0, /* markets: ZS100 ZS110 TZ100 TZ101 TZ110 TX1 */
16834       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16835     { "Panasonic DMC-TZ100", -15, 0, /* markets: ZS100 ZS110 TZ100 TZ101 TZ110 TX1 */
16836       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16837     { "Panasonic DMC-TZ101", -15, 0, /* markets: ZS100 ZS110 TZ100 TZ101 TZ110 TX1 */
16838       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16839     { "Panasonic DMC-TZ110", -15, 0, /* markets: ZS100 ZS110 TZ100 TZ101 TZ110 TX1 */
16840       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16841     { "Panasonic DMC-TX1", -15, 0, /* markets: ZS100 ZS110 TZ100 TZ101 TZ110 TX1 */
16842       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16843 
16844     { "Panasonic DC-ZS200", -15, 0, /* temp, markets: ZS200 TZ200 */
16845       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16846     { "Panasonic DC-TZ200", -15, 0, /* temp, markets: ZS200 TZ200 */
16847       { 7790,-2736,-755,-3452,11870,1769,-628,1647,4898 } },
16848 
16849     { "Phase One H 20", 0, 0, /* DJC */
16850       { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
16851     { "Phase One H20", 0, 0, /* DJC */
16852       { 1313,1855,-109,-6715,15908,808,-327,1840,6020 } },
16853     { "Phase One H 25", 0, 0,
16854       { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
16855     { "Phase One H25", 0, 0, /* added */
16856       { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
16857     { "Phase One IQ280", 0, 0, /* added */
16858       { 6294,686,-712,-5435,13417,2211,-1006,2435,5042 } },
16859     { "Phase One IQ260", 0, 0, /* added */
16860       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16861     { "Phase One IQ250",0, 0,
16862 //    {3984,0,0,0,10000,0,0,0,7666}},
16863       {10325,845,-604,-4113,13385,481,-1791,4163,6924}}, /* emb */
16864     { "Phase One IQ180", 0, 0, /* added */
16865       { 6294,686,-712,-5435,13417,2211,-1006,2435,5042 } },
16866     { "Phase One IQ160", 0, 0, /* added */
16867       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16868     { "Phase One IQ150", 0, 0, /* added */
16869       {10325,845,-604,-4113,13385,481,-1791,4163,6924}}, /* temp */ /* emb */
16870 //      { 3984,0,0,0,10000,0,0,0,7666 } },
16871     { "Phase One IQ140", 0, 0, /* added */
16872       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16873     { "Phase One P65", 0, 0,
16874       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16875     { "Phase One P 65", 0, 0,
16876       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16877     { "Phase One P45", 0, 0, /* added */
16878       { 5053,-24,-117,-5685,14077,1703,-2619,4491,5850 } },
16879     { "Phase One P 45", 0, 0, /* added */
16880       { 5053,-24,-117,-5685,14077,1703,-2619,4491,5850 } },
16881     { "Phase One P40", 0, 0,
16882       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16883     { "Phase One P 40", 0, 0,
16884       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16885     { "Phase One P30", 0, 0, /* added */
16886       { 4516,-244,-36,-7020,14976,2174,-3206,4670,7087 } },
16887     { "Phase One P 30", 0, 0, /* added */
16888       { 4516,-244,-36,-7020,14976,2174,-3206,4670,7087 } },
16889     { "Phase One P25", 0, 0, /* added */
16890       { 2905,732,-237,-8135,16626,1476,-3038,4253,7517 } },
16891     { "Phase One P 25", 0, 0, /* added */
16892       { 2905,732,-237,-8135,16626,1476,-3038,4253,7517 } },
16893     { "Phase One P21", 0, 0, /* added */
16894       { 6516,-2050,-507,-8217,16703,1479,-3492,4741,8489 } },
16895     { "Phase One P 21", 0, 0, /* added */
16896       { 6516,-2050,-507,-8217,16703,1479,-3492,4741,8489 } },
16897     { "Phase One P20", 0, 0, /* added */
16898       { 2905,732,-237,-8135,16626,1476,-3038,4253,7517 } },
16899     { "Phase One P20", 0, 0, /* added */
16900       { 2905,732,-237,-8135,16626,1476,-3038,4253,7517 } },
16901     { "Phase One P 2", 0, 0,
16902       { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
16903     { "Phase One P2", 0, 0,
16904       { 2905,732,-237,-8134,16626,1476,-3038,4253,7517 } },
16905     { "Phase One IQ3 100MP", 0, 0, /* added */
16906 //    {2423,0,0,0,9901,0,0,0,7989}},
16907       { 10999,354,-742,-4590,13342,937,-1060,2166,8120} }, /* emb */
16908     { "Phase One IQ3 80MP", 0, 0, /* added */
16909       { 6294,686,-712,-5435,13417,2211,-1006,2435,5042 } },
16910     { "Phase One IQ3 60MP", 0, 0, /* added */
16911       { 8035,435,-962,-6001,13872,2320,-1159,3065,5434 } },
16912     { "Phase One IQ3 50MP", 0, 0, /* added */
16913 //      { 3984,0,0,0,10000,0,0,0,7666 } },
16914       {10058,1079,-587,-4135,12903,944,-916,2726,7480}}, /* emb */
16915     { "Photron BC2-HD", 0, 0, /* DJC */
16916       { 14603,-4122,-528,-1810,9794,2017,-297,2763,5936 } },
16917 
16918     { "Polaroid x530", 0, 0,
16919       { 13458,-2556,-510,-5444,15081,205,0,0,12120 } },
16920 
16921     { "Red One", 704, 0xffff, /* DJC */
16922       { 21014,-7891,-2613,-3056,12201,856,-2203,5125,8042 } },
16923 
16924     { "Ricoh S10 24-72mm F2.5-4.4 VC", 0, 0, /* added */
16925       { 10531,-4043,-878,-2038,10270,2052,-107,895,4577 } },
16926     { "Ricoh GR A12 50mm F2.5 MACRO", 0, 0, /* added */
16927       { 8849,-2560,-689,-5092,12831,2520,-507,1280,7104 } },
16928     { "Ricoh GR DIGITAL 3", 0, 0, /* added */
16929       { 8170,-2496,-655,-5147,13056,2312,-1367,1859,5265 } },
16930     { "Ricoh GR DIGITAL 4", 0, 0, /* added */
16931       { 8771,-3151,-837,-3097,11015,2389,-703,1343,4924 } },
16932     { "Ricoh GR II", 0, 0,
16933       { 4630,-834,-423,-4977,12805,2417,-638,1467,6115 } },
16934     { "Ricoh GR", 0, 0,
16935       { 3708,-543,-160,-5381,12254,3556,-1471,1929,8234 } },
16936     { "Ricoh GX200", 0, 0, /* added */
16937       { 8040,-2368,-626,-4659,12543,2363,-1125,1581,5660 } },
16938     { "Ricoh RICOH GX200", 0, 0, /* added */
16939       { 8040,-2368,-626,-4659,12543,2363,-1125,1581,5660 } },
16940     { "Ricoh GXR MOUNT A12", 0, 0, /* added */
16941       { 7834,-2182,-739,-5453,13409,2241,-952,2005,6620 } },
16942     { "Ricoh GXR A16", 0, 0, /* added */
16943       { 7837,-2538,-730,-4370,12184,2461,-868,1648,5830 } },
16944     { "Ricoh GXR A12", 0, 0, /* added */
16945       { 10228,-3159,-933,-5304,13158,2371,-943,1873,6685 } },
16946 
16947     { "Samsung EK-GN100", 0, 0, /* added */ /* Galaxy NX */
16948       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16949     { "Samsung EK-GN110", 0, 0, /* added */ /* Galaxy NX */
16950       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16951     { "Samsung EK-GN120", 0, 0, /* Galaxy NX */
16952       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16953     { "Samsung EK-KN120", 0, 0, /* added */ /* Galaxy NX */
16954       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16955     { "Samsung EX1", 0, 0x3e00,
16956       { 8898,-2498,-994,-3144,11328,2066,-760,1381,4576 } },
16957     { "Samsung EX2F", 0, 0x7ff,
16958       { 10648,-3897,-1055,-2022,10573,1668,-492,1611,4742 } },
16959     { "Samsung Galaxy S7 Edge", 0, 0, /* added */
16960       { 9927,-3704,-1024,-3935,12758,1257,-389,1512,4993 } },
16961     { "Samsung Galaxy S7", 0, 0, /* added */
16962       { 9927,-3704,-1024,-3935,12758,1257,-389,1512,4993 } },
16963     { "Samsung Galaxy NX", 0, 0, /* added */
16964       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16965     { "Samsung NX U", 0, 0, /* added */
16966       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16967     { "Samsung NX mini", 0, 0,
16968       { 5222,-1196,-550,-6540,14649,2009,-1666,2819,5657 } },
16969     { "Samsung NX3300", 0, 0,
16970       { 8060,-2933,-761,-4504,12890,1762,-630,1489,5227 } },
16971     { "Samsung NX3000", 0, 0,
16972       { 8060,-2933,-761,-4504,12890,1762,-630,1489,5227 } },
16973     { "Samsung NX30", 0, 0, /* used for NX30/NX300/NX300M */
16974       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16975     { "Samsung NX2000", 0, 0,
16976       { 7557,-2522,-739,-4679,12949,1894,-840,1777,5311 } },
16977     { "Samsung NX2", 0, 0xfff, /* used for NX20/NX200/NX210 */
16978       { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
16979     { "Samsung NX1000", 0, 0,
16980       { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
16981     { "Samsung NX1100", 0, 0,
16982       { 6933,-2268,-753,-4921,13387,1647,-803,1641,6096 } },
16983     { "Samsung NX11", 0, 0,
16984       { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
16985     { "Samsung NX10", 0, 0, /* used for NX10/NX100 */
16986       { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
16987     { "Samsung NX500", 0, 0,
16988       { 10686,-4042,-1052,-3595,13238,276,-464,1259,5931 } },
16989     { "Samsung NX5", 0, 0,
16990       { 10332,-3234,-1168,-6111,14639,1520,-1352,2647,8331 } },
16991     { "Samsung NX1", 0, 0,
16992       { 10686,-4042,-1052,-3595,13238,276,-464,1259,5931 } },
16993     { "Samsung NXF1", 0, 0, /* added */
16994       { 5222,-1196,-550,-6540,14649,2009,-1666,2819,5657 } },
16995     { "Samsung WB2000", 0, 0xfff,
16996       { 12093,-3557,-1155,-1000,9534,1733,-22,1787,4576 } },
16997     { "Samsung GX10", 0, 0, /* added */ /* Pentax K10D */
16998       { 9679,-2965,-811,-8622,16514,2182,-975,883,9793 } },
16999     { "Samsung GX-10", 0, 0, /* added */ /* Pentax K10D */
17000       { 9679,-2965,-811,-8622,16514,2182,-975,883,9793 } },
17001     { "Samsung GX-1", 0, 0, /* used for GX-1L/GX-1S */
17002       { 10504,-2438,-1189,-8603,16207,2531,-1022,863,12242 } },
17003     { "Samsung GX20", 0, 0, /* copied from Pentax K20D */
17004       { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
17005     { "Samsung GX-20", 0, 0, /* added */ /* copied from Pentax K20D */
17006       { 9427,-2714,-868,-7493,16092,1373,-2199,3264,7180 } },
17007     { "Samsung S85", 0, 0, /* DJC */
17008       { 11885,-3968,-1473,-4214,12299,1916,-835,1655,5549 } },
17009 
17010 // Foveon: LibRaw color data
17011     { "Sigma dp0 Quattro", 2047, 0,
17012       { 13801,-3390,-1016,5535,3802,877,1848,4245,3730 } },
17013     { "Sigma dp1 Quattro", 2047, 0,
17014       { 13801,-3390,-1016,5535,3802,877,1848,4245,3730 } },
17015     { "Sigma dp2 Quattro", 2047, 0,
17016       { 13801,-3390,-1016,5535,3802,877,1848,4245,3730 } },
17017     { "Sigma dp3 Quattro", 2047, 0,
17018       { 13801,-3390,-1016,5535,3802,877,1848,4245,3730 } },
17019     { "Sigma sd Quattro H", 256, 0,
17020       { 1295,108,-311, 256,828,-65,-28,750,254 } }, /* temp */
17021     { "Sigma sd Quattro", 2047, 0,
17022       { 1295,108,-311, 256,828,-65,-28,750,254 } }, /* temp */
17023     { "Sigma SD9", 15, 4095, /* updated */
17024       { 13564,-2537,-751,-5465,15154,194,-67,116,10425 } },
17025     { "Sigma SD10", 15, 16383, /* updated */
17026       { 6787,-1682,575,-3091,8357,160,217,-369,12314 } },
17027     { "Sigma SD14", 15, 16383, /* updated */
17028       { 13589,-2509,-739,-5440,15104,193,-61,105,10554 } },
17029     { "Sigma SD15", 15, 4095, /* updated */
17030       { 13556,-2537,-730,-5462,15144,195,-61,106,10577 } },
17031 // Merills + SD1
17032     { "Sigma SD1", 31, 4095, /* LibRaw */
17033       { 5133,-1895,-353,4978,744,144,3837,3069,2777 } },
17034     { "Sigma DP1 Merrill", 31, 4095, /* LibRaw */
17035       { 5133,-1895,-353,4978,744,144,3837,3069,2777 } },
17036     { "Sigma DP2 Merrill", 31, 4095, /* LibRaw */
17037       { 5133,-1895,-353,4978,744,144,3837,3069,2777 } },
17038     { "Sigma DP3 Merrill", 31, 4095, /* LibRaw */
17039       { 5133,-1895,-353,4978,744,144,3837,3069,2777 } },
17040 // Sigma DP (non-Merill Versions)
17041     { "Sigma DP1X", 0, 4095, /* updated */
17042       { 13704,-2452,-857,-5413,15073,186,-89,151,9820 } },
17043     { "Sigma DP1", 0, 4095, /* updated */
17044       { 12774,-2591,-394,-5333,14676,207,15,-21,12127 } },
17045     { "Sigma DP", 0, 4095, /* LibRaw */
17046       //  { 7401,-1169,-567,2059,3769,1510,664,3367,5328 } },
17047       { 13100,-3638,-847,6855,2369,580,2723,3218,3251 } },
17048 
17049     { "Sinar", 0, 0, /* DJC */
17050       { 16442,-2956,-2422,-2877,12128,750,-1136,6066,4559 } },
17051 
17052     { "Sony DSC-F828", 0, 0,
17053       { 7924,-1910,-777,-8226,15459,2998,-1517,2199,6818,-7242,11401,3481 } },
17054     { "Sony DSC-R1", 0, 0,
17055       { 8512,-2641,-694,-8042,15670,2526,-1821,2117,7414 } },
17056     { "Sony DSC-V3", 0, 0,
17057       { 7511,-2571,-692,-7894,15088,3060,-948,1111,8128 } },
17058     {"Sony DSC-RX100M5", -800, 0,
17059       { 6596,-2079,-562,-4782,13016,1933,-970,1581,5181 } },
17060     { "Sony DSC-RX100M", -800, 0, /* used for M2/M3/M4 */
17061       { 6596,-2079,-562,-4782,13016,1933,-970,1581,5181 } },
17062     { "Sony DSC-RX100", 0, 0,
17063       { 8651,-2754,-1057,-3464,12207,1373,-568,1398,4434 } },
17064     {"Sony DSC-RX10M4", -800, 0,
17065       { 7699,-2566,-629,-2967,11270,1928,-378,1286,4807 } },
17066     { "Sony DSC-RX10",0, 0, /* same for M2/M3 */
17067       { 6679,-1825,-745,-5047,13256,1953,-1580,2422,5183 } },
17068     { "Sony DSC-RX1RM2", 0, 0,
17069       { 6629,-1900,-483,-4618,12349,2550,-622,1381,6514 } },
17070     { "Sony DSC-RX1R", 0, 0, /* updated */
17071       { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
17072     { "Sony DSC-RX1", 0, 0,
17073       { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
17074 
17075     {"Sony DSC-RX0", -800, 0, /* temp */
17076       { 9396,-3507,-843,-2497,11111,1572,-343,1355,5089 } },
17077 
17078     { "Sony DSLR-A100", 0, 0xfeb,
17079       { 9437,-2811,-774,-8405,16215,2290,-710,596,7181 } },
17080     { "Sony DSLR-A290", 0, 0,
17081       { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
17082     { "Sony DSLR-A2", 0, 0,
17083       { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
17084     { "Sony DSLR-A300", 0, 0,
17085       { 9847,-3091,-928,-8485,16345,2225,-715,595,7103 } },
17086     { "Sony DSLR-A330", 0, 0,
17087       { 9847,-3091,-929,-8485,16346,2225,-714,595,7103 } },
17088     { "Sony DSLR-A350", 0, 0xffc,
17089       { 6038,-1484,-578,-9146,16746,2513,-875,746,7217 } },
17090     { "Sony DSLR-A380", 0, 0,
17091       { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
17092     { "Sony DSLR-A390", 0, 0,
17093       { 6038,-1484,-579,-9145,16746,2512,-875,746,7218 } },
17094     { "Sony DSLR-A450", 0, 0xfeb,
17095       { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
17096     { "Sony DSLR-A580", 0, 16596,
17097       { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
17098     { "Sony DSLR-A500", 0, 16596,
17099       { 6046,-1127,-278,-5574,13076,2786,-691,1419,7625 } },
17100     { "Sony DSLR-A550", 0, 16596,
17101       { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
17102     { "Sony DSLR-A5", 0, 0xfeb, /* Is there any cameras not covered above? */
17103       { 4950,-580,-103,-5228,12542,3029,-709,1435,7371 } },
17104     { "Sony DSLR-A700", 0, 0,
17105       { 5775,-805,-359,-8574,16295,2391,-1943,2341,7249 } },
17106     { "Sony DSLR-A850", 0, 0,
17107       { 5413,-1162,-365,-5665,13098,2866,-608,1179,8440 } },
17108     { "Sony DSLR-A900", 0, 0,
17109       { 5209,-1072,-397,-8845,16120,2919,-1618,1803,8654 } },
17110     { "Sony ILCA-68", 0, 0,
17111       { 6435,-1903,-536,-4722,12449,2550,-663,1363,6517 } },
17112     { "Sony ILCA-77M2", 0, 0,
17113       { 5991,-1732,-443,-4100,11989,2381,-704,1467,5992 } },
17114     { "Sony ILCA-99M2", 0, 0,
17115       { 6660,-1918,-471,-4613,12398,2485,-649,1433,6447 } },
17116     { "Sony ILCE-9", 0, 0,
17117       { 6389,-1703,-378,-4562,12265,2587,-670,1489,6550 } },
17118     { "Sony ILCE-7M2", 0, 0,
17119       { 5271,-712,-347,-6153,13653,2763,-1601,2366,7242 } },
17120     { "Sony ILCE-7SM2", 0, 0,
17121       { 5838,-1430,-246,-3497,11477,2297,-748,1885,5778 } },
17122     { "Sony ILCE-7S", 0, 0,
17123       { 5838,-1430,-246,-3497,11477,2297,-748,1885,5778 } },
17124     { "Sony ILCE-7RM3", 0, 0,
17125       { 6640,-1847,-503,-5238,13010,2474,-993,1673,6527 } },
17126     { "Sony ILCE-7RM2", 0, 0,
17127       { 6629,-1900,-483,-4618,12349,2550,-622,1381,6514 } },
17128     { "Sony ILCE-7R", 0, 0,
17129       { 4913,-541,-202,-6130,13513,2906,-1564,2151,7183 } },
17130     { "Sony ILCE-7", 0, 0,
17131       { 5271,-712,-347,-6153,13653,2763,-1601,2366,7242 } },
17132     { "Sony ILCE-6300", 0, 0,
17133       { 5973,-1695,-419,-3826,11797,2293,-639,1398,5789 } },
17134     { "Sony ILCE-6500", 0, 0,
17135       { 5973,-1695,-419,-3826,11797,2293,-639,1398,5789 } },
17136     { "Sony ILCE", 0, 0, /* 3000, 5000, 5100, 6000, and QX1 */
17137       { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
17138     { "Sony MODEL-NAME", 0, 0, /* added */
17139       { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
17140     { "Sony NEX-5N", 0, 0,
17141       { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
17142     { "Sony NEX-5R", 0, 0,
17143       { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
17144     { "Sony NEX-5T", 0, 0,
17145       { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
17146     { "Sony NEX-3N", 0, 0,
17147       { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
17148     { "Sony NEX-3", 0, 0,
17149       { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
17150     { "Sony NEX-5", 0, 0,
17151       { 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
17152     { "Sony NEX-6", 0, 0,
17153       { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
17154     { "Sony NEX-7", 0, 0,
17155       { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
17156     { "Sony NEX-VG30", 0, 0, /* added */
17157       { 6129,-1545,-418,-4930,12490,2743,-977,1693,6615 } },
17158     { "Sony NEX-VG900", 0, 0, /* added */
17159       { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
17160     { "Sony NEX", 0, 0, /* NEX-C3, NEX-F3, NEX-VG20 */
17161       { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
17162     { "Sony SLT-A33", 0, 0,
17163       { 6069,-1221,-366,-5221,12779,2734,-1024,2066,6834 } },
17164     { "Sony SLT-A35", 0, 0,
17165       { 5986,-1618,-415,-4557,11820,3120,-681,1404,6971 } },
17166     { "Sony SLT-A37", 0, 0,
17167       { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
17168     { "Sony SLT-A55", 0, 0,
17169       { 5932,-1492,-411,-4813,12285,2856,-741,1524,6739 } },
17170     { "Sony SLT-A57", 0, 0,
17171       { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
17172     { "Sony SLT-A58", 0, 0,
17173       { 5991,-1456,-455,-4764,12135,2980,-707,1425,6701 } },
17174     { "Sony SLT-A65", 0, 0,
17175       { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
17176     { "Sony SLT-A77", 0, 0,
17177       { 5491,-1192,-363,-4951,12342,2948,-911,1722,7192 } },
17178     { "Sony SLT-A99", 0, 0,
17179       { 6344,-1612,-462,-4863,12477,2681,-865,1786,6899 } },
17180   };
17181   // clang-format on
17182 
17183   double cam_xyz[4][3];
17184   char name[130];
17185   int i, j;
17186 
17187   if (colors > 4 || colors < 1)
17188     return;
17189 
17190   int bl4 = (cblack[0] + cblack[1] + cblack[2] + cblack[3]) / 4, bl64 = 0;
17191   if (cblack[4] * cblack[5] > 0)
17192   {
17193     for (unsigned c = 0; c < 4096 && c < cblack[4] * cblack[5]; c++)
17194       bl64 += cblack[c + 6];
17195     bl64 /= cblack[4] * cblack[5];
17196   }
17197   int rblack = black + bl4 + bl64;
17198 
17199   sprintf(name, "%s %s", t_make, t_model);
17200   for (i = 0; i < sizeof table / sizeof *table; i++)
17201     if (!strncasecmp(name, table[i].prefix, strlen(table[i].prefix)))
17202     {
17203       if (!dng_version)
17204       {
17205         if (table[i].t_black > 0)
17206         {
17207           black = (ushort)table[i].t_black;
17208           memset(cblack, 0, sizeof(cblack));
17209         }
17210         else if (table[i].t_black < 0 && rblack == 0)
17211         {
17212           black = (ushort)(-table[i].t_black);
17213           memset(cblack, 0, sizeof(cblack));
17214         }
17215         if (table[i].t_maximum)
17216           maximum = (ushort)table[i].t_maximum;
17217       }
17218       if (table[i].trans[0])
17219       {
17220         for (raw_color = j = 0; j < 12; j++)
17221 #ifdef LIBRAW_LIBRARY_BUILD
17222           if (internal_only)
17223             imgdata.color.cam_xyz[0][j] = table[i].trans[j] / 10000.0;
17224           else
17225             imgdata.color.cam_xyz[0][j] =
17226 #endif
17227                 ((double *)cam_xyz)[j] = table[i].trans[j] / 10000.0;
17228 #ifdef LIBRAW_LIBRARY_BUILD
17229         if (!internal_only)
17230 #endif
17231           cam_xyz_coeff(rgb_cam, cam_xyz);
17232       }
17233       break;
17234     }
17235 }
17236 
simple_coeff(int index)17237 void CLASS simple_coeff(int index)
17238 {
17239   static const float table[][12] = {/* index 0 -- all Foveon cameras */
17240                                     {1.4032, -0.2231, -0.1016, -0.5263, 1.4816, 0.017, -0.0112, 0.0183, 0.9113},
17241                                     /* index 1 -- Kodak DC20 and DC25 */
17242                                     {2.25, 0.75, -1.75, -0.25, -0.25, 0.75, 0.75, -0.25, -0.25, -1.75, 0.75, 2.25},
17243                                     /* index 2 -- Logitech Fotoman Pixtura */
17244                                     {1.893, -0.418, -0.476, -0.495, 1.773, -0.278, -1.017, -0.655, 2.672},
17245                                     /* index 3 -- Nikon E880, E900, and E990 */
17246                                     {-1.936280, 1.800443, -1.448486, 2.584324, 1.405365, -0.524955, -0.289090, 0.408680,
17247                                      -1.204965, 1.082304, 2.941367, -1.818705}};
17248   int i, c;
17249 
17250   for (raw_color = i = 0; i < 3; i++)
17251     FORCC rgb_cam[i][c] = table[index][i * colors + c];
17252 }
17253 
guess_byte_order(int words)17254 short CLASS guess_byte_order(int words)
17255 {
17256   uchar test[4][2];
17257   int t = 2, msb;
17258   double diff, sum[2] = {0, 0};
17259 
17260   fread(test[0], 2, 2, ifp);
17261   for (words -= 2; words--;)
17262   {
17263     fread(test[t], 2, 1, ifp);
17264     for (msb = 0; msb < 2; msb++)
17265     {
17266       diff = (test[t ^ 2][msb] << 8 | test[t ^ 2][!msb]) - (test[t][msb] << 8 | test[t][!msb]);
17267       sum[msb] += diff * diff;
17268     }
17269     t = (t + 1) & 3;
17270   }
17271   return sum[0] < sum[1] ? 0x4d4d : 0x4949;
17272 }
17273 
find_green(int bps,int bite,int off0,int off1)17274 float CLASS find_green(int bps, int bite, int off0, int off1)
17275 {
17276   UINT64 bitbuf = 0;
17277   int vbits, col, i, c;
17278   ushort img[2][2064];
17279   double sum[] = {0, 0};
17280   if(width > 2064) return 0.f; // too wide
17281 
17282   FORC(2)
17283   {
17284     fseek(ifp, c ? off1 : off0, SEEK_SET);
17285     for (vbits = col = 0; col < width; col++)
17286     {
17287       for (vbits -= bps; vbits < 0; vbits += bite)
17288       {
17289         bitbuf <<= bite;
17290         for (i = 0; i < bite; i += 8)
17291           bitbuf |= (unsigned)(fgetc(ifp) << i);
17292       }
17293       img[c][col] = bitbuf << (64 - bps - vbits) >> (64 - bps);
17294     }
17295   }
17296   FORC(width - 1)
17297   {
17298     sum[c & 1] += ABS(img[0][c] - img[1][c + 1]);
17299     sum[~c & 1] += ABS(img[1][c] - img[0][c + 1]);
17300   }
17301   return 100 * log(sum[0] / sum[1]);
17302 }
17303 
17304 #ifdef LIBRAW_LIBRARY_BUILD
remove_trailing_spaces(char * string,size_t len)17305 static void remove_trailing_spaces(char *string, size_t len)
17306 {
17307   if (len < 1)
17308     return; // not needed, b/c sizeof of make/model is 64
17309   string[len - 1] = 0;
17310   if (len < 3)
17311     return; // also not needed
17312   len = strnlen(string, len - 1);
17313   for (int i = len - 1; i >= 0; i--)
17314   {
17315     if (isspace((unsigned char)string[i]))
17316       string[i] = 0;
17317     else
17318       break;
17319   }
17320 }
17321 
initdata()17322 void CLASS initdata()
17323 {
17324   tiff_flip = flip = filters = UINT_MAX; /* unknown */
17325   raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
17326   maximum = height = width = top_margin = left_margin = 0;
17327   cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
17328   iso_speed = shutter = aperture = focal_len = unique_id = 0;
17329   tiff_nifds = 0;
17330   memset(tiff_ifd, 0, sizeof tiff_ifd);
17331   for (int i = 0; i < LIBRAW_IFD_MAXCOUNT; i++)
17332   {
17333     tiff_ifd[i].dng_color[0].illuminant = tiff_ifd[i].dng_color[1].illuminant = 0xffff;
17334     for (int c = 0; c < 4; c++)
17335       tiff_ifd[i].dng_levels.analogbalance[c] = 1.0f;
17336   }
17337   for (int i = 0; i < 0x10000; i++)
17338     curve[i] = i;
17339   memset(gpsdata, 0, sizeof gpsdata);
17340   memset(cblack, 0, sizeof cblack);
17341   memset(white, 0, sizeof white);
17342   memset(mask, 0, sizeof mask);
17343   thumb_offset = thumb_length = thumb_width = thumb_height = 0;
17344   load_raw = thumb_load_raw = 0;
17345   write_thumb = &CLASS jpeg_thumb;
17346   data_offset = meta_offset = meta_length = tiff_bps = tiff_compress = 0;
17347   kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
17348   timestamp = shot_order = tiff_samples = black = is_foveon = 0;
17349   mix_green = profile_length = data_error = zero_is_bad = 0;
17350   pixel_aspect = is_raw = raw_color = 1;
17351   tile_width = tile_length = 0;
17352 }
17353 
17354 #endif
17355 /*
17356    Identify which camera created this file, and set global variables
17357    accordingly.
17358  */
identify()17359 void CLASS identify()
17360 {
17361   static const short pana[][6] = {
17362       {3130, 1743, 4, 0, -6, 0},    {3130, 2055, 4, 0, -6, 0},      {3130, 2319, 4, 0, -6, 0},
17363       {3170, 2103, 18, 0, -42, 20}, {3170, 2367, 18, 13, -42, -21}, {3177, 2367, 0, 0, -1, 0},
17364       {3304, 2458, 0, 0, -1, 0},    {3330, 2463, 9, 0, -5, 0},      {3330, 2479, 9, 0, -17, 4},
17365       {3370, 1899, 15, 0, -44, 20}, {3370, 2235, 15, 0, -44, 20},   {3370, 2511, 15, 10, -44, -21},
17366       {3690, 2751, 3, 0, -8, -3},   {3710, 2751, 0, 0, -3, 0},      {3724, 2450, 0, 0, 0, -2},
17367       {3770, 2487, 17, 0, -44, 19}, {3770, 2799, 17, 15, -44, -19}, {3880, 2170, 6, 0, -6, 0},
17368       {4060, 3018, 0, 0, 0, -2},    {4290, 2391, 3, 0, -8, -1},     {4330, 2439, 17, 15, -44, -19},
17369       {4508, 2962, 0, 0, -3, -4},   {4508, 3330, 0, 0, -3, -6},
17370   };
17371   static const ushort canon[][11] = {
17372       {1944, 1416, 0, 0, 48, 0},
17373       {2144, 1560, 4, 8, 52, 2, 0, 0, 0, 25},
17374       {2224, 1456, 48, 6, 0, 2},
17375       {2376, 1728, 12, 6, 52, 2},
17376       {2672, 1968, 12, 6, 44, 2},
17377       {3152, 2068, 64, 12, 0, 0, 16},
17378       {3160, 2344, 44, 12, 4, 4},
17379       {3344, 2484, 4, 6, 52, 6},
17380       {3516, 2328, 42, 14, 0, 0},
17381       {3596, 2360, 74, 12, 0, 0},
17382       {3744, 2784, 52, 12, 8, 12},
17383       {3944, 2622, 30, 18, 6, 2},
17384       {3948, 2622, 42, 18, 0, 2},
17385       {3984, 2622, 76, 20, 0, 2, 14},
17386       {4104, 3048, 48, 12, 24, 12},
17387       {4116, 2178, 4, 2, 0, 0},
17388       {4152, 2772, 192, 12, 0, 0},
17389       {4160, 3124, 104, 11, 8, 65},
17390       {4176, 3062, 96, 17, 8, 0, 0, 16, 0, 7, 0x49},
17391       {4192, 3062, 96, 17, 24, 0, 0, 16, 0, 0, 0x49},
17392       {4312, 2876, 22, 18, 0, 2},
17393       {4352, 2874, 62, 18, 0, 0},
17394       {4476, 2954, 90, 34, 0, 0},
17395       {4480, 3348, 12, 10, 36, 12, 0, 0, 0, 18, 0x49},
17396       {4480, 3366, 80, 50, 0, 0},
17397       {4496, 3366, 80, 50, 12, 0},
17398       {4768, 3516, 96, 16, 0, 0, 0, 16},
17399       {4832, 3204, 62, 26, 0, 0},
17400       {4832, 3228, 62, 51, 0, 0},
17401       {5108, 3349, 98, 13, 0, 0},
17402       {5120, 3318, 142, 45, 62, 0},
17403       {5280, 3528, 72, 52, 0, 0}, /* EOS M */
17404       {5344, 3516, 142, 51, 0, 0},
17405       {5344, 3584, 126, 100, 0, 2},
17406       {5360, 3516, 158, 51, 0, 0},
17407       {5568, 3708, 72, 38, 0, 0},
17408       {5632, 3710, 96, 17, 0, 0, 0, 16, 0, 0, 0x49},
17409       {5712, 3774, 62, 20, 10, 2},
17410       {5792, 3804, 158, 51, 0, 0},
17411       {5920, 3950, 122, 80, 2, 0},
17412       {6096, 4056, 72, 34, 0, 0},  /* EOS M3 */
17413       {6288, 4056, 266, 36, 0, 0}, /* EOS 80D */
17414       {6384, 4224, 120, 44, 0, 0}, /* 6D II */
17415       {6880, 4544, 136, 42, 0, 0}, /* EOS 5D4 */
17416       {8896, 5920, 160, 64, 0, 0},
17417   };
17418   static const struct
17419   {
17420     ushort id;
17421     char t_model[20];
17422   } unique[] =
17423       {
17424           {0x001, "EOS-1D"},
17425           {0x167, "EOS-1DS"},
17426           {0x168, "EOS 10D"},
17427           {0x169, "EOS-1D Mark III"},
17428           {0x170, "EOS 300D"},
17429           {0x174, "EOS-1D Mark II"},
17430           {0x175, "EOS 20D"},
17431           {0x176, "EOS 450D"},
17432           {0x188, "EOS-1Ds Mark II"},
17433           {0x189, "EOS 350D"},
17434           {0x190, "EOS 40D"},
17435           {0x213, "EOS 5D"},
17436           {0x215, "EOS-1Ds Mark III"},
17437           {0x218, "EOS 5D Mark II"},
17438           {0x232, "EOS-1D Mark II N"},
17439           {0x234, "EOS 30D"},
17440           {0x236, "EOS 400D"},
17441           {0x250, "EOS 7D"},
17442           {0x252, "EOS 500D"},
17443           {0x254, "EOS 1000D"},
17444           {0x261, "EOS 50D"},
17445           {0x269, "EOS-1D X"},
17446           {0x270, "EOS 550D"},
17447           {0x281, "EOS-1D Mark IV"},
17448           {0x285, "EOS 5D Mark III"},
17449           {0x286, "EOS 600D"},
17450           {0x287, "EOS 60D"},
17451           {0x288, "EOS 1100D"},
17452           {0x289, "EOS 7D Mark II"},
17453           {0x301, "EOS 650D"},
17454           {0x302, "EOS 6D"},
17455           {0x324, "EOS-1D C"},
17456           {0x325, "EOS 70D"},
17457           {0x326, "EOS 700D"},
17458           {0x327, "EOS 1200D"},
17459           {0x328, "EOS-1D X Mark II"},
17460           {0x331, "EOS M"},
17461           {0x335, "EOS M2"},
17462           {0x374, "EOS M3"},   /* temp */
17463           {0x384, "EOS M10"},  /* temp */
17464           {0x394, "EOS M5"},   /* temp */
17465           {0x398, "EOS M100"}, /* temp */
17466           {0x346, "EOS 100D"},
17467           {0x347, "EOS 760D"},
17468           {0x349, "EOS 5D Mark IV"},
17469           {0x350, "EOS 80D"},
17470           {0x382, "EOS 5DS"},
17471           {0x393, "EOS 750D"},
17472           {0x401, "EOS 5DS R"},
17473           {0x404, "EOS 1300D"},
17474           {0x405, "EOS 800D"},
17475           {0x406, "EOS 6D Mark II"},
17476           {0x407, "EOS M6"},
17477           {0x408, "EOS 77D"},
17478           {0x417, "EOS 200D"},
17479       },
17480     sonique[] = {
17481         {0x002, "DSC-R1"},      {0x100, "DSLR-A100"},   {0x101, "DSLR-A900"},  {0x102, "DSLR-A700"},
17482         {0x103, "DSLR-A200"},   {0x104, "DSLR-A350"},   {0x105, "DSLR-A300"},  {0x106, "DSLR-A900"},
17483         {0x107, "DSLR-A380"},   {0x108, "DSLR-A330"},   {0x109, "DSLR-A230"},  {0x10a, "DSLR-A290"},
17484         {0x10d, "DSLR-A850"},   {0x10e, "DSLR-A850"},   {0x111, "DSLR-A550"},  {0x112, "DSLR-A500"},
17485         {0x113, "DSLR-A450"},   {0x116, "NEX-5"},       {0x117, "NEX-3"},      {0x118, "SLT-A33"},
17486         {0x119, "SLT-A55V"},    {0x11a, "DSLR-A560"},   {0x11b, "DSLR-A580"},  {0x11c, "NEX-C3"},
17487         {0x11d, "SLT-A35"},     {0x11e, "SLT-A65V"},    {0x11f, "SLT-A77V"},   {0x120, "NEX-5N"},
17488         {0x121, "NEX-7"},       {0x122, "NEX-VG20E"},   {0x123, "SLT-A37"},    {0x124, "SLT-A57"},
17489         {0x125, "NEX-F3"},      {0x126, "SLT-A99V"},    {0x127, "NEX-6"},      {0x128, "NEX-5R"},
17490         {0x129, "DSC-RX100"},   {0x12a, "DSC-RX1"},     {0x12b, "NEX-VG900"},  {0x12c, "NEX-VG30E"},
17491         {0x12e, "ILCE-3000"},   {0x12f, "SLT-A58"},     {0x131, "NEX-3N"},     {0x132, "ILCE-7"},
17492         {0x133, "NEX-5T"},      {0x134, "DSC-RX100M2"}, {0x135, "DSC-RX10"},   {0x136, "DSC-RX1R"},
17493         {0x137, "ILCE-7R"},     {0x138, "ILCE-6000"},   {0x139, "ILCE-5000"},  {0x13d, "DSC-RX100M3"},
17494         {0x13e, "ILCE-7S"},     {0x13f, "ILCA-77M2"},   {0x153, "ILCE-5100"},  {0x154, "ILCE-7M2"},
17495         {0x155, "DSC-RX100M4"}, {0x156, "DSC-RX10M2"},  {0x158, "DSC-RX1RM2"}, {0x15a, "ILCE-QX1"},
17496         {0x15b, "ILCE-7RM2"},   {0x15e, "ILCE-7SM2"},   {0x161, "ILCA-68"},    {0x162, "ILCA-99M2"},
17497         {0x163, "DSC-RX10M3"},  {0x164, "DSC-RX100M5"}, {0x165, "ILCE-6300"},  {0x166, "ILCE-9"},
17498         {0x168, "ILCE-6500"},   {0x16a, "ILCE-7RM3"},   {0x16c, "DSC-RX0"},    {0x16d, "DSC-RX10M4"},
17499     };
17500 
17501 #ifdef LIBRAW_LIBRARY_BUILD
17502   static const libraw_custom_camera_t const_table[]
17503 #else
17504   static const struct
17505   {
17506     unsigned fsize;
17507     ushort rw, rh;
17508     uchar lm, tm, rm, bm, lf, cf, max, flags;
17509     char t_make[10], t_model[20];
17510     ushort offset;
17511   } table[]
17512 #endif
17513       = {
17514           {786432, 1024, 768, 0, 0, 0, 0, 0, 0x94, 0, 0, "AVT", "F-080C"},
17515           {1447680, 1392, 1040, 0, 0, 0, 0, 0, 0x94, 0, 0, "AVT", "F-145C"},
17516           {1920000, 1600, 1200, 0, 0, 0, 0, 0, 0x94, 0, 0, "AVT", "F-201C"},
17517           {5067304, 2588, 1958, 0, 0, 0, 0, 0, 0x94, 0, 0, "AVT", "F-510C"},
17518           {5067316, 2588, 1958, 0, 0, 0, 0, 0, 0x94, 0, 0, "AVT", "F-510C", 12},
17519           {10134608, 2588, 1958, 0, 0, 0, 0, 9, 0x94, 0, 0, "AVT", "F-510C"},
17520           {10134620, 2588, 1958, 0, 0, 0, 0, 9, 0x94, 0, 0, "AVT", "F-510C", 12},
17521           {16157136, 3272, 2469, 0, 0, 0, 0, 9, 0x94, 0, 0, "AVT", "F-810C"},
17522           {15980544, 3264, 2448, 0, 0, 0, 0, 8, 0x61, 0, 1, "AgfaPhoto", "DC-833m"},
17523           {9631728, 2532, 1902, 0, 0, 0, 0, 96, 0x61, 0, 0, "Alcatel", "5035D"},
17524           {31850496, 4608, 3456, 0, 0, 0, 0, 0, 0x94, 0, 0, "GITUP", "GIT2 4:3"},
17525           {23887872, 4608, 2592, 0, 0, 0, 0, 0, 0x94, 0, 0, "GITUP", "GIT2 16:9"},
17526           {32257024, 4624, 3488, 8, 2, 16, 2, 0, 0x94, 0, 0, "GITUP", "GIT2P 4:3"},
17527           //   Android Raw dumps id start
17528           //   File Size in bytes Horizontal Res Vertical Flag then bayer order eg 0x16 bbgr 0x94 rggb
17529           {1540857, 2688, 1520, 0, 0, 0, 0, 1, 0x61, 0, 0, "Samsung", "S3"},
17530           {2658304, 1212, 1096, 0, 0, 0, 0, 1, 0x16, 0, 0, "LG", "G3FrontMipi"},
17531           {2842624, 1296, 1096, 0, 0, 0, 0, 1, 0x16, 0, 0, "LG", "G3FrontQCOM"},
17532           {2969600, 1976, 1200, 0, 0, 0, 0, 1, 0x16, 0, 0, "Xiaomi", "MI3wMipi"},
17533           {3170304, 1976, 1200, 0, 0, 0, 0, 1, 0x16, 0, 0, "Xiaomi", "MI3wQCOM"},
17534           {3763584, 1584, 1184, 0, 0, 0, 0, 96, 0x61, 0, 0, "I_Mobile", "I_StyleQ6"},
17535           {5107712, 2688, 1520, 0, 0, 0, 0, 1, 0x61, 0, 0, "OmniVisi", "UltraPixel1"},
17536           {5382640, 2688, 1520, 0, 0, 0, 0, 1, 0x61, 0, 0, "OmniVisi", "UltraPixel2"},
17537           {5664912, 2688, 1520, 0, 0, 0, 0, 1, 0x61, 0, 0, "OmniVisi", "4688"},
17538           {5664912, 2688, 1520, 0, 0, 0, 0, 1, 0x61, 0, 0, "OmniVisi", "4688"},
17539           {5364240, 2688, 1520, 0, 0, 0, 0, 1, 0x61, 0, 0, "OmniVisi", "4688"},
17540           {6299648, 2592, 1944, 0, 0, 0, 0, 1, 0x16, 0, 0, "OmniVisi", "OV5648"},
17541           {6721536, 2592, 1944, 0, 0, 0, 0, 0, 0x16, 0, 0, "OmniVisi", "OV56482"},
17542           {6746112, 2592, 1944, 0, 0, 0, 0, 0, 0x16, 0, 0, "HTC", "OneSV"},
17543           {9631728, 2532, 1902, 0, 0, 0, 0, 96, 0x61, 0, 0, "Sony", "5mp"},
17544           {9830400, 2560, 1920, 0, 0, 0, 0, 96, 0x61, 0, 0, "NGM", "ForwardArt"},
17545           {10186752, 3264, 2448, 0, 0, 0, 0, 1, 0x94, 0, 0, "Sony", "IMX219-mipi 8mp"},
17546           {10223360, 2608, 1944, 0, 0, 0, 0, 96, 0x16, 0, 0, "Sony", "IMX"},
17547           {10782464, 3282, 2448, 0, 0, 0, 0, 0, 0x16, 0, 0, "HTC", "MyTouch4GSlide"},
17548           {10788864, 3282, 2448, 0, 0, 0, 0, 0, 0x16, 0, 0, "Xperia", "L"},
17549           {15967488, 3264, 2446, 0, 0, 0, 0, 96, 0x16, 0, 0, "OmniVison", "OV8850"},
17550           {16224256, 4208, 3082, 0, 0, 0, 0, 1, 0x16, 0, 0, "LG", "G3MipiL"},
17551           {16424960, 4208, 3120, 0, 0, 0, 0, 1, 0x16, 0, 0, "IMX135", "MipiL"},
17552           {17326080, 4164, 3120, 0, 0, 0, 0, 1, 0x16, 0, 0, "LG", "G3LQCom"},
17553           {17522688, 4212, 3120, 0, 0, 0, 0, 0, 0x16, 0, 0, "Sony", "IMX135-QCOM"},
17554           {19906560, 4608, 3456, 0, 0, 0, 0, 1, 0x16, 0, 0, "Gione", "E7mipi"},
17555           {19976192, 5312, 2988, 0, 0, 0, 0, 1, 0x16, 0, 0, "LG", "G4"},
17556           {20389888, 4632, 3480, 0, 0, 0, 0, 1, 0x16, 0, 0, "Xiaomi", "RedmiNote3Pro"},
17557           {20500480, 4656, 3496, 0, 0, 0, 0, 1, 0x94, 0, 0, "Sony", "IMX298-mipi 16mp"},
17558           {21233664, 4608, 3456, 0, 0, 0, 0, 1, 0x16, 0, 0, "Gione", "E7qcom"},
17559           {26023936, 4192, 3104, 0, 0, 0, 0, 96, 0x94, 0, 0, "THL", "5000"},
17560           {26257920, 4208, 3120, 0, 0, 0, 0, 96, 0x94, 0, 0, "Sony", "IMX214"},
17561           {26357760, 4224, 3120, 0, 0, 0, 0, 96, 0x61, 0, 0, "OV", "13860"},
17562           {41312256, 5248, 3936, 0, 0, 0, 0, 96, 0x61, 0, 0, "Meizu", "MX4"},
17563           {42923008, 5344, 4016, 0, 0, 0, 0, 96, 0x61, 0, 0, "Sony", "IMX230"},
17564           //   Android Raw dumps id end
17565           {20137344, 3664, 2748, 0, 0, 0, 0, 0x40, 0x49, 0, 0, "Aptina", "MT9J003", 0xffff},
17566           {2868726, 1384, 1036, 0, 0, 0, 0, 64, 0x49, 0, 8, "Baumer", "TXG14", 1078},
17567           {5298000, 2400, 1766, 12, 12, 44, 2, 40, 0x94, 0, 2, "Canon", "PowerShot SD300"},
17568           {6553440, 2664, 1968, 4, 4, 44, 4, 40, 0x94, 0, 2, "Canon", "PowerShot A460"},
17569           {6573120, 2672, 1968, 12, 8, 44, 0, 40, 0x94, 0, 2, "Canon", "PowerShot A610"},
17570           {6653280, 2672, 1992, 10, 6, 42, 2, 40, 0x94, 0, 2, "Canon", "PowerShot A530"},
17571           {7710960, 2888, 2136, 44, 8, 4, 0, 40, 0x94, 0, 2, "Canon", "PowerShot S3 IS"},
17572           {9219600, 3152, 2340, 36, 12, 4, 0, 40, 0x94, 0, 2, "Canon", "PowerShot A620"},
17573           {9243240, 3152, 2346, 12, 7, 44, 13, 40, 0x49, 0, 2, "Canon", "PowerShot A470"},
17574           {10341600, 3336, 2480, 6, 5, 32, 3, 40, 0x94, 0, 2, "Canon", "PowerShot A720 IS"},
17575           {10383120, 3344, 2484, 12, 6, 44, 6, 40, 0x94, 0, 2, "Canon", "PowerShot A630"},
17576           {12945240, 3736, 2772, 12, 6, 52, 6, 40, 0x94, 0, 2, "Canon", "PowerShot A640"},
17577           {15636240, 4104, 3048, 48, 12, 24, 12, 40, 0x94, 0, 2, "Canon", "PowerShot A650"},
17578           {15467760, 3720, 2772, 6, 12, 30, 0, 40, 0x94, 0, 2, "Canon", "PowerShot SX110 IS"},
17579           {15534576, 3728, 2778, 12, 9, 44, 9, 40, 0x94, 0, 2, "Canon", "PowerShot SX120 IS"},
17580           {18653760, 4080, 3048, 24, 12, 24, 12, 40, 0x94, 0, 2, "Canon", "PowerShot SX20 IS"},
17581           {18763488, 4104, 3048, 10, 22, 82, 22, 8, 0x49, 0, 0, "Canon", "PowerShot D10"},
17582           {19131120, 4168, 3060, 92, 16, 4, 1, 40, 0x94, 0, 2, "Canon", "PowerShot SX220 HS"},
17583           {21936096, 4464, 3276, 25, 10, 73, 12, 40, 0x16, 0, 2, "Canon", "PowerShot SX30 IS"},
17584           {24724224, 4704, 3504, 8, 16, 56, 8, 40, 0x49, 0, 2, "Canon", "PowerShot A3300 IS"},
17585           {30858240, 5248, 3920, 8, 16, 56, 16, 40, 0x94, 0, 2, "Canon", "IXUS 160"},
17586           {1976352, 1632, 1211, 0, 2, 0, 1, 0, 0x94, 0, 1, "Casio", "QV-2000UX"},
17587           {3217760, 2080, 1547, 0, 0, 10, 1, 0, 0x94, 0, 1, "Casio", "QV-3*00EX"},
17588           {6218368, 2585, 1924, 0, 0, 9, 0, 0, 0x94, 0, 1, "Casio", "QV-5700"},
17589           {7816704, 2867, 2181, 0, 0, 34, 36, 0, 0x16, 0, 1, "Casio", "EX-Z60"},
17590           {2937856, 1621, 1208, 0, 0, 1, 0, 0, 0x94, 7, 13, "Casio", "EX-S20"},
17591           {4948608, 2090, 1578, 0, 0, 32, 34, 0, 0x94, 7, 1, "Casio", "EX-S100"},
17592           {6054400, 2346, 1720, 2, 0, 32, 0, 0, 0x94, 7, 1, "Casio", "QV-R41"},
17593           {7426656, 2568, 1928, 0, 0, 0, 0, 0, 0x94, 0, 1, "Casio", "EX-P505"},
17594           {7530816, 2602, 1929, 0, 0, 22, 0, 0, 0x94, 7, 1, "Casio", "QV-R51"},
17595           {7542528, 2602, 1932, 0, 0, 32, 0, 0, 0x94, 7, 1, "Casio", "EX-Z50"},
17596           {7562048, 2602, 1937, 0, 0, 25, 0, 0, 0x16, 7, 1, "Casio", "EX-Z500"},
17597           {7753344, 2602, 1986, 0, 0, 32, 26, 0, 0x94, 7, 1, "Casio", "EX-Z55"},
17598           {9313536, 2858, 2172, 0, 0, 14, 30, 0, 0x94, 7, 1, "Casio", "EX-P600"},
17599           {10834368, 3114, 2319, 0, 0, 27, 0, 0, 0x94, 0, 1, "Casio", "EX-Z750"},
17600           {10843712, 3114, 2321, 0, 0, 25, 0, 0, 0x94, 0, 1, "Casio", "EX-Z75"},
17601           {10979200, 3114, 2350, 0, 0, 32, 32, 0, 0x94, 7, 1, "Casio", "EX-P700"},
17602           {12310144, 3285, 2498, 0, 0, 6, 30, 0, 0x94, 0, 1, "Casio", "EX-Z850"},
17603           {12489984, 3328, 2502, 0, 0, 47, 35, 0, 0x94, 0, 1, "Casio", "EX-Z8"},
17604           {15499264, 3754, 2752, 0, 0, 82, 0, 0, 0x94, 0, 1, "Casio", "EX-Z1050"},
17605           {18702336, 4096, 3044, 0, 0, 24, 0, 80, 0x94, 7, 1, "Casio", "EX-ZR100"},
17606           {7684000, 2260, 1700, 0, 0, 0, 0, 13, 0x94, 0, 1, "Casio", "QV-4000"},
17607           {787456, 1024, 769, 0, 1, 0, 0, 0, 0x49, 0, 0, "Creative", "PC-CAM 600"},
17608           {28829184, 4384, 3288, 0, 0, 0, 0, 36, 0x61, 0, 0, "DJI"},
17609           {15151104, 4608, 3288, 0, 0, 0, 0, 0, 0x94, 0, 0, "Matrix"},
17610           {3840000, 1600, 1200, 0, 0, 0, 0, 65, 0x49, 0, 0, "Foculus", "531C"},
17611           {307200, 640, 480, 0, 0, 0, 0, 0, 0x94, 0, 0, "Generic"},
17612           {62464, 256, 244, 1, 1, 6, 1, 0, 0x8d, 0, 0, "Kodak", "DC20"},
17613           {124928, 512, 244, 1, 1, 10, 1, 0, 0x8d, 0, 0, "Kodak", "DC20"},
17614           {1652736, 1536, 1076, 0, 52, 0, 0, 0, 0x61, 0, 0, "Kodak", "DCS200"},
17615           {4159302, 2338, 1779, 1, 33, 1, 2, 0, 0x94, 0, 0, "Kodak", "C330"},
17616           {4162462, 2338, 1779, 1, 33, 1, 2, 0, 0x94, 0, 0, "Kodak", "C330", 3160},
17617           {2247168, 1232, 912, 0, 0, 16, 0, 0, 0x00, 0, 0, "Kodak", "C330"},
17618           {3370752, 1232, 912, 0, 0, 16, 0, 0, 0x00, 0, 0, "Kodak", "C330"},
17619           {6163328, 2864, 2152, 0, 0, 0, 0, 0, 0x94, 0, 0, "Kodak", "C603"},
17620           {6166488, 2864, 2152, 0, 0, 0, 0, 0, 0x94, 0, 0, "Kodak", "C603", 3160},
17621           {460800, 640, 480, 0, 0, 0, 0, 0, 0x00, 0, 0, "Kodak", "C603"},
17622           {9116448, 2848, 2134, 0, 0, 0, 0, 0, 0x00, 0, 0, "Kodak", "C603"},
17623           {12241200, 4040, 3030, 2, 0, 0, 13, 0, 0x49, 0, 0, "Kodak", "12MP"},
17624           {12272756, 4040, 3030, 2, 0, 0, 13, 0, 0x49, 0, 0, "Kodak", "12MP", 31556},
17625           {18000000, 4000, 3000, 0, 0, 0, 0, 0, 0x00, 0, 0, "Kodak", "12MP"},
17626           {614400, 640, 480, 0, 3, 0, 0, 64, 0x94, 0, 0, "Kodak", "KAI-0340"},
17627           {15360000, 3200, 2400, 0, 0, 0, 0, 96, 0x16, 0, 0, "Lenovo", "A820"},
17628           {3884928, 1608, 1207, 0, 0, 0, 0, 96, 0x16, 0, 0, "Micron", "2010", 3212},
17629           {1138688, 1534, 986, 0, 0, 0, 0, 0, 0x61, 0, 0, "Minolta", "RD175", 513},
17630           {1581060, 1305, 969, 0, 0, 18, 6, 6, 0x1e, 4, 1, "Nikon", "E900"},
17631           {2465792, 1638, 1204, 0, 0, 22, 1, 6, 0x4b, 5, 1, "Nikon", "E950"},
17632           {2940928, 1616, 1213, 0, 0, 0, 7, 30, 0x94, 0, 1, "Nikon", "E2100"},
17633           {4771840, 2064, 1541, 0, 0, 0, 1, 6, 0xe1, 0, 1, "Nikon", "E990"},
17634           {4775936, 2064, 1542, 0, 0, 0, 0, 30, 0x94, 0, 1, "Nikon", "E3700"},
17635           {5865472, 2288, 1709, 0, 0, 0, 1, 6, 0xb4, 0, 1, "Nikon", "E4500"},
17636           {5869568, 2288, 1710, 0, 0, 0, 0, 6, 0x16, 0, 1, "Nikon", "E4300"},
17637           {7438336, 2576, 1925, 0, 0, 0, 1, 6, 0xb4, 0, 1, "Nikon", "E5000"},
17638           {8998912, 2832, 2118, 0, 0, 0, 0, 30, 0x94, 7, 1, "Nikon", "COOLPIX S6"},
17639           {5939200, 2304, 1718, 0, 0, 0, 0, 30, 0x16, 0, 0, "Olympus", "C770UZ"},
17640           {3178560, 2064, 1540, 0, 0, 0, 0, 0, 0x94, 0, 1, "Pentax", "Optio S"},
17641           {4841984, 2090, 1544, 0, 0, 22, 0, 0, 0x94, 7, 1, "Pentax", "Optio S"},
17642           {6114240, 2346, 1737, 0, 0, 22, 0, 0, 0x94, 7, 1, "Pentax", "Optio S4"},
17643           {10702848, 3072, 2322, 0, 0, 0, 21, 30, 0x94, 0, 1, "Pentax", "Optio 750Z"},
17644           {4147200, 1920, 1080, 0, 0, 0, 0, 0, 0x49, 0, 0, "Photron", "BC2-HD"},
17645           {4151666, 1920, 1080, 0, 0, 0, 0, 0, 0x49, 0, 0, "Photron", "BC2-HD", 8},
17646           {13248000, 2208, 3000, 0, 0, 0, 0, 13, 0x61, 0, 0, "Pixelink", "A782"},
17647           {6291456, 2048, 1536, 0, 0, 0, 0, 96, 0x61, 0, 0, "RoverShot", "3320AF"},
17648           {311696, 644, 484, 0, 0, 0, 0, 0, 0x16, 0, 8, "ST Micro", "STV680 VGA"},
17649           {16098048, 3288, 2448, 0, 0, 24, 0, 9, 0x94, 0, 1, "Samsung", "S85"},
17650           {16215552, 3312, 2448, 0, 0, 48, 0, 9, 0x94, 0, 1, "Samsung", "S85"},
17651           {20487168, 3648, 2808, 0, 0, 0, 0, 13, 0x94, 5, 1, "Samsung", "WB550"},
17652           {24000000, 4000, 3000, 0, 0, 0, 0, 13, 0x94, 5, 1, "Samsung", "WB550"},
17653           {12582980, 3072, 2048, 0, 0, 0, 0, 33, 0x61, 0, 0, "Sinar", "", 68},
17654           {33292868, 4080, 4080, 0, 0, 0, 0, 33, 0x61, 0, 0, "Sinar", "", 68},
17655           {44390468, 4080, 5440, 0, 0, 0, 0, 33, 0x61, 0, 0, "Sinar", "", 68},
17656           {1409024, 1376, 1024, 0, 0, 1, 0, 0, 0x49, 0, 0, "Sony", "XCD-SX910CR"},
17657           {2818048, 1376, 1024, 0, 0, 1, 0, 97, 0x49, 0, 0, "Sony", "XCD-SX910CR"},
17658       };
17659 #ifdef LIBRAW_LIBRARY_BUILD
17660   libraw_custom_camera_t table[64 + sizeof(const_table) / sizeof(const_table[0])];
17661 #endif
17662 
17663   static const char *corp[] = {"AgfaPhoto", "Canon",     "Casio",  "Epson",   "Fujifilm", "Mamiya", "Minolta",
17664                                "Motorola",  "Kodak",     "Konica", "Leica",   "Nikon",    "Nokia",  "Olympus",
17665                                "Pentax",    "Phase One", "Ricoh",  "Samsung", "Sigma",    "Sinar",  "Sony"};
17666 #ifdef LIBRAW_LIBRARY_BUILD
17667   char head[64], *cp;
17668 #else
17669   char head[32], *cp;
17670 #endif
17671   int hlen, flen, fsize, zero_fsize = 1, i, c;
17672   struct jhead jh;
17673 
17674 #ifdef LIBRAW_LIBRARY_BUILD
17675   unsigned camera_count = parse_custom_cameras(64, table, imgdata.params.custom_camera_strings);
17676   for (int q = 0; q < sizeof(const_table) / sizeof(const_table[0]); q++)
17677     memmove(&table[q + camera_count], &const_table[q], sizeof(const_table[0]));
17678   camera_count += sizeof(const_table) / sizeof(const_table[0]);
17679 #endif
17680 
17681   tiff_flip = flip = filters = UINT_MAX; /* unknown */
17682   raw_height = raw_width = fuji_width = fuji_layout = cr2_slice[0] = 0;
17683   maximum = height = width = top_margin = left_margin = 0;
17684   cdesc[0] = desc[0] = artist[0] = make[0] = model[0] = model2[0] = 0;
17685   iso_speed = shutter = aperture = focal_len = unique_id = 0;
17686   tiff_nifds = 0;
17687   memset(tiff_ifd, 0, sizeof tiff_ifd);
17688 
17689 #ifdef LIBRAW_LIBRARY_BUILD
17690   imgdata.other.CameraTemperature = imgdata.other.SensorTemperature = imgdata.other.SensorTemperature2 =
17691       imgdata.other.LensTemperature = imgdata.other.AmbientTemperature = imgdata.other.BatteryTemperature =
17692           imgdata.other.exifAmbientTemperature = -1000.0f;
17693 
17694   for (i = 0; i < LIBRAW_IFD_MAXCOUNT; i++)
17695   {
17696     tiff_ifd[i].dng_color[0].illuminant = tiff_ifd[i].dng_color[1].illuminant = 0xffff;
17697     for (int c = 0; c < 4; c++)
17698       tiff_ifd[i].dng_levels.analogbalance[c] = 1.0f;
17699   }
17700 #endif
17701   memset(gpsdata, 0, sizeof gpsdata);
17702   memset(cblack, 0, sizeof cblack);
17703   memset(white, 0, sizeof white);
17704   memset(mask, 0, sizeof mask);
17705   thumb_offset = thumb_length = thumb_width = thumb_height = 0;
17706   load_raw = thumb_load_raw = 0;
17707   write_thumb = &CLASS jpeg_thumb;
17708   data_offset = meta_offset = meta_length = tiff_bps = tiff_compress = 0;
17709   kodak_cbpp = zero_after_ff = dng_version = load_flags = 0;
17710   timestamp = shot_order = tiff_samples = black = is_foveon = 0;
17711   mix_green = profile_length = data_error = zero_is_bad = 0;
17712   pixel_aspect = is_raw = raw_color = 1;
17713   tile_width = tile_length = 0;
17714 
17715   for (i = 0; i < 4; i++)
17716   {
17717     cam_mul[i] = i == 1;
17718     pre_mul[i] = i < 3;
17719     FORC3 cmatrix[c][i] = 0;
17720     FORC3 rgb_cam[c][i] = c == i;
17721   }
17722   colors = 3;
17723   for (i = 0; i < 0x10000; i++)
17724     curve[i] = i;
17725 
17726   order = get2();
17727   hlen = get4();
17728   fseek(ifp, 0, SEEK_SET);
17729 #ifdef LIBRAW_LIBRARY_BUILD
17730   if(fread(head, 1, 64, ifp) < 64) throw LIBRAW_EXCEPTION_IO_CORRUPT;
17731   libraw_internal_data.unpacker_data.lenRAFData = libraw_internal_data.unpacker_data.posRAFData = 0;
17732 #else
17733   fread(head, 1, 32, ifp);
17734 #endif
17735   fseek(ifp, 0, SEEK_END);
17736   flen = fsize = ftell(ifp);
17737   if ((cp = (char *)memmem(head, 32, (char *)"MMMM", 4)) || (cp = (char *)memmem(head, 32, (char *)"IIII", 4)))
17738   {
17739     parse_phase_one(cp - head);
17740     if (cp - head && parse_tiff(0))
17741       apply_tiff();
17742   }
17743   else if (order == 0x4949 || order == 0x4d4d)
17744   {
17745     if (!memcmp(head + 6, "HEAPCCDR", 8))
17746     {
17747       data_offset = hlen;
17748 #ifdef LIBRAW_LIBRARY_BUILD
17749       imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
17750       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
17751 #endif
17752       parse_ciff(hlen, flen - hlen, 0);
17753       load_raw = &CLASS canon_load_raw;
17754     }
17755     else if (parse_tiff(0))
17756       apply_tiff();
17757   }
17758   else if (!memcmp(head, "\xff\xd8\xff\xe1", 4) && !memcmp(head + 6, "Exif", 4))
17759   {
17760     fseek(ifp, 4, SEEK_SET);
17761     data_offset = 4 + get2();
17762     fseek(ifp, data_offset, SEEK_SET);
17763     if (fgetc(ifp) != 0xff)
17764       parse_tiff(12);
17765     thumb_offset = 0;
17766   }
17767   else if (!memcmp(head + 25, "ARECOYK", 7))
17768   {
17769     strcpy(make, "Contax");
17770     strcpy(model, "N Digital");
17771     fseek(ifp, 33, SEEK_SET);
17772     get_timestamp(1);
17773     fseek(ifp, 52, SEEK_SET);
17774     switch (get4())
17775     {
17776     case 7:
17777       iso_speed = 25;
17778       break;
17779     case 8:
17780       iso_speed = 32;
17781       break;
17782     case 9:
17783       iso_speed = 40;
17784       break;
17785     case 10:
17786       iso_speed = 50;
17787       break;
17788     case 11:
17789       iso_speed = 64;
17790       break;
17791     case 12:
17792       iso_speed = 80;
17793       break;
17794     case 13:
17795       iso_speed = 100;
17796       break;
17797     case 14:
17798       iso_speed = 125;
17799       break;
17800     case 15:
17801       iso_speed = 160;
17802       break;
17803     case 16:
17804       iso_speed = 200;
17805       break;
17806     case 17:
17807       iso_speed = 250;
17808       break;
17809     case 18:
17810       iso_speed = 320;
17811       break;
17812     case 19:
17813       iso_speed = 400;
17814       break;
17815     }
17816     shutter = libraw_powf64l(2.0f, (((float)get4()) / 8.0f)) / 16000.0f;
17817     FORC4 cam_mul[c ^ (c >> 1)] = get4();
17818     fseek(ifp, 88, SEEK_SET);
17819     aperture = libraw_powf64l(2.0f, ((float)get4()) / 16.0f);
17820     fseek(ifp, 112, SEEK_SET);
17821     focal_len = get4();
17822 #ifdef LIBRAW_LIBRARY_BUILD
17823     fseek(ifp, 104, SEEK_SET);
17824     imgdata.lens.makernotes.MaxAp4CurFocal = libraw_powf64l(2.0f, ((float)get4()) / 16.0f);
17825     fseek(ifp, 124, SEEK_SET);
17826     stmread(imgdata.lens.makernotes.Lens, 32, ifp);
17827     imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_Contax_N;
17828     if (imgdata.lens.makernotes.Lens[0])
17829       imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_Contax_N;
17830 #endif
17831   }
17832   else if (!strcmp(head, "PXN"))
17833   {
17834     strcpy(make, "Logitech");
17835     strcpy(model, "Fotoman Pixtura");
17836   }
17837   else if (!strcmp(head, "qktk"))
17838   {
17839     strcpy(make, "Apple");
17840     strcpy(model, "QuickTake 100");
17841     load_raw = &CLASS quicktake_100_load_raw;
17842   }
17843   else if (!strcmp(head, "qktn"))
17844   {
17845     strcpy(make, "Apple");
17846     strcpy(model, "QuickTake 150");
17847     load_raw = &CLASS kodak_radc_load_raw;
17848   }
17849   else if (!memcmp(head, "FUJIFILM", 8))
17850   {
17851 #ifdef LIBRAW_LIBRARY_BUILD
17852     strncpy(model, head + 0x1c,0x20);
17853     model[0x20]=0;
17854     memcpy(model2, head + 0x3c, 4);
17855     model2[4] = 0;
17856 #endif
17857     fseek(ifp, 84, SEEK_SET);
17858     thumb_offset = get4();
17859     thumb_length = get4();
17860     fseek(ifp, 92, SEEK_SET);
17861     parse_fuji(get4());
17862     if (thumb_offset > 120)
17863     {
17864       fseek(ifp, 120, SEEK_SET);
17865       is_raw += (i = get4()) ? 1 : 0;
17866       if (is_raw == 2 && shot_select)
17867         parse_fuji(i);
17868     }
17869     load_raw = &CLASS unpacked_load_raw;
17870     fseek(ifp, 100 + 28 * (shot_select > 0), SEEK_SET);
17871     parse_tiff(data_offset = get4());
17872     parse_tiff(thumb_offset + 12);
17873     apply_tiff();
17874   }
17875   else if (!memcmp(head, "RIFF", 4))
17876   {
17877     fseek(ifp, 0, SEEK_SET);
17878     parse_riff();
17879   }
17880   else if (!memcmp(head + 4, "ftypqt   ", 9))
17881   {
17882     fseek(ifp, 0, SEEK_SET);
17883     parse_qt(fsize);
17884     is_raw = 0;
17885   }
17886   else if (!memcmp(head, "\0\001\0\001\0@", 6))
17887   {
17888     fseek(ifp, 6, SEEK_SET);
17889     fread(make, 1, 8, ifp);
17890     fread(model, 1, 8, ifp);
17891     fread(model2, 1, 16, ifp);
17892     data_offset = get2();
17893     get2();
17894     raw_width = get2();
17895     raw_height = get2();
17896     load_raw = &CLASS nokia_load_raw;
17897     filters = 0x61616161;
17898   }
17899   else if (!memcmp(head, "NOKIARAW", 8))
17900   {
17901     strcpy(make, "NOKIA");
17902     order = 0x4949;
17903     fseek(ifp, 300, SEEK_SET);
17904     data_offset = get4();
17905     i = get4(); // bytes count
17906     width = get2();
17907     height = get2();
17908 #ifdef LIBRAW_LIBRARY_BUILD
17909     // Data integrity check
17910     if (width < 1 || width > 16000 || height < 1 || height > 16000 || i < (width * height) || i > (2 * width * height))
17911       throw LIBRAW_EXCEPTION_IO_CORRUPT;
17912 #endif
17913     switch (tiff_bps = i * 8 / (width * height))
17914     {
17915     case 8:
17916       load_raw = &CLASS eight_bit_load_raw;
17917       break;
17918     case 10:
17919       load_raw = &CLASS nokia_load_raw;
17920       break;
17921     case 0:
17922       throw LIBRAW_EXCEPTION_IO_CORRUPT;
17923       break;
17924     }
17925     raw_height = height + (top_margin = i / (width * tiff_bps / 8) - height);
17926     mask[0][3] = 1;
17927     filters = 0x61616161;
17928   }
17929   else if (!memcmp(head, "ARRI", 4))
17930   {
17931     order = 0x4949;
17932     fseek(ifp, 20, SEEK_SET);
17933     width = get4();
17934     height = get4();
17935     strcpy(make, "ARRI");
17936     fseek(ifp, 668, SEEK_SET);
17937     fread(model, 1, 64, ifp);
17938     data_offset = 4096;
17939     load_raw = &CLASS packed_load_raw;
17940     load_flags = 88;
17941     filters = 0x61616161;
17942   }
17943   else if (!memcmp(head, "XPDS", 4))
17944   {
17945     order = 0x4949;
17946     fseek(ifp, 0x800, SEEK_SET);
17947     fread(make, 1, 41, ifp);
17948     raw_height = get2();
17949     raw_width = get2();
17950     fseek(ifp, 56, SEEK_CUR);
17951     fread(model, 1, 30, ifp);
17952     data_offset = 0x10000;
17953     load_raw = &CLASS canon_rmf_load_raw;
17954     gamma_curve(0, 12.25, 1, 1023);
17955   }
17956   else if (!memcmp(head + 4, "RED1", 4))
17957   {
17958     strcpy(make, "Red");
17959     strcpy(model, "One");
17960     parse_redcine();
17961     load_raw = &CLASS redcine_load_raw;
17962     gamma_curve(1 / 2.4, 12.92, 1, 4095);
17963     filters = 0x49494949;
17964   }
17965   else if (!memcmp(head, "DSC-Image", 9))
17966     parse_rollei();
17967   else if (!memcmp(head, "PWAD", 4))
17968     parse_sinar_ia();
17969   else if (!memcmp(head, "\0MRM", 4))
17970     parse_minolta(0);
17971   else if (!memcmp(head, "FOVb", 4))
17972   {
17973 #ifdef LIBRAW_LIBRARY_BUILD
17974     /* no foveon support for dcraw build from libraw source */
17975     parse_x3f();
17976 #endif
17977   }
17978   else if (!memcmp(head, "CI", 2))
17979     parse_cine();
17980   if (make[0] == 0)
17981 #ifdef LIBRAW_LIBRARY_BUILD
17982     for (zero_fsize = i = 0; i < camera_count; i++)
17983 #else
17984     for (zero_fsize = i = 0; i < sizeof table / sizeof *table; i++)
17985 #endif
17986       if (fsize == table[i].fsize)
17987       {
17988         strcpy(make, table[i].t_make);
17989 #ifdef LIBRAW_LIBRARY_BUILD
17990         if (!strncmp(make, "Canon", 5))
17991         {
17992           imgdata.lens.makernotes.CameraMount = LIBRAW_MOUNT_FixedLens;
17993           imgdata.lens.makernotes.LensMount = LIBRAW_MOUNT_FixedLens;
17994         }
17995 #endif
17996         strcpy(model, table[i].t_model);
17997         flip = table[i].flags >> 2;
17998         zero_is_bad = table[i].flags & 2;
17999         if (table[i].flags & 1)
18000           parse_external_jpeg();
18001         data_offset = table[i].offset == 0xffff ? 0 : table[i].offset;
18002         raw_width = table[i].rw;
18003         raw_height = table[i].rh;
18004         left_margin = table[i].lm;
18005         top_margin = table[i].tm;
18006         width = raw_width - left_margin - table[i].rm;
18007         height = raw_height - top_margin - table[i].bm;
18008         filters = 0x1010101U * table[i].cf;
18009         colors = 4 - !((filters & filters >> 1) & 0x5555);
18010         load_flags = table[i].lf;
18011         switch (tiff_bps = (fsize - data_offset) * 8 / (raw_width * raw_height))
18012         {
18013         case 6:
18014           load_raw = &CLASS minolta_rd175_load_raw;
18015           break;
18016         case 8:
18017           load_raw = &CLASS eight_bit_load_raw;
18018           break;
18019         case 10:
18020           if ((fsize - data_offset) / raw_height * 3 >= raw_width * 4)
18021           {
18022             load_raw = &CLASS android_loose_load_raw;
18023             break;
18024           }
18025           else if (load_flags & 1)
18026           {
18027             load_raw = &CLASS android_tight_load_raw;
18028             break;
18029           }
18030         case 12:
18031           load_flags |= 128;
18032           load_raw = &CLASS packed_load_raw;
18033           break;
18034         case 16:
18035           order = 0x4949 | 0x404 * (load_flags & 1);
18036           tiff_bps -= load_flags >> 4;
18037           tiff_bps -= load_flags = load_flags >> 1 & 7;
18038           load_raw = table[i].offset == 0xffff ? &CLASS unpacked_load_raw_reversed : &CLASS unpacked_load_raw;
18039         }
18040         maximum = (1 << tiff_bps) - (1 << table[i].max);
18041         break;
18042       }
18043   if (zero_fsize)
18044     fsize = 0;
18045   if (make[0] == 0)
18046     parse_smal(0, flen);
18047   if (make[0] == 0)
18048   {
18049     parse_jpeg(0);
18050     fseek(ifp, 0, SEEK_END);
18051     int sz = ftell(ifp);
18052 #ifdef LIBRAW_LIBRARY_BUILD
18053     if (!strncmp(model, "RP_imx219", 9) && sz >= 0x9cb600 && !fseek(ifp, -0x9cb600, SEEK_END) &&
18054         fread(head, 1, 0x20, ifp) && !strncmp(head, "BRCM", 4))
18055     {
18056       strcpy(make, "Broadcom");
18057       strcpy(model, "RPi IMX219");
18058       if (raw_height > raw_width)
18059         flip = 5;
18060       data_offset = ftell(ifp) + 0x8000 - 0x20;
18061       parse_broadcom();
18062       black = 66;
18063       maximum = 0x3ff;
18064       load_raw = &CLASS broadcom_load_raw;
18065       thumb_offset = 0;
18066       thumb_length = sz - 0x9cb600 - 1;
18067     }
18068     else if (!(strncmp(model, "ov5647", 6) && strncmp(model, "RP_OV5647", 9)) && sz >= 0x61b800 &&
18069              !fseek(ifp, -0x61b800, SEEK_END) && fread(head, 1, 0x20, ifp) && !strncmp(head, "BRCM", 4))
18070     {
18071       strcpy(make, "Broadcom");
18072       if (!strncmp(model, "ov5647", 6))
18073         strcpy(model, "RPi OV5647 v.1");
18074       else
18075         strcpy(model, "RPi OV5647 v.2");
18076       if (raw_height > raw_width)
18077         flip = 5;
18078       data_offset = ftell(ifp) + 0x8000 - 0x20;
18079       parse_broadcom();
18080       black = 16;
18081       maximum = 0x3ff;
18082       load_raw = &CLASS broadcom_load_raw;
18083       thumb_offset = 0;
18084       thumb_length = sz - 0x61b800 - 1;
18085 #else
18086     if (!(strncmp(model, "ov", 2) && strncmp(model, "RP_OV", 5)) && sz >= 6404096 && !fseek(ifp, -6404096, SEEK_END) &&
18087         fread(head, 1, 32, ifp) && !strcmp(head, "BRCMn"))
18088     {
18089       strcpy(make, "OmniVision");
18090       data_offset = ftell(ifp) + 0x8000 - 32;
18091       width = raw_width;
18092       raw_width = 2611;
18093       load_raw = &CLASS nokia_load_raw;
18094       filters = 0x16161616;
18095 #endif
18096     }
18097     else
18098       is_raw = 0;
18099   }
18100 #ifdef LIBRAW_LIBRARY_BUILD
18101   // make sure strings are terminated
18102   desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
18103 #endif
18104   for (i = 0; i < sizeof corp / sizeof *corp; i++)
18105     if (strcasestr(make, corp[i])) /* Simplify company names */
18106       strcpy(make, corp[i]);
18107   if ((!strncmp(make, "Kodak", 5) || !strncmp(make, "Leica", 5)) &&
18108       ((cp = strcasestr(model, " DIGITAL CAMERA")) || (cp = strstr(model, "FILE VERSION"))))
18109     *cp = 0;
18110   if (!strncasecmp(model, "PENTAX", 6))
18111     strcpy(make, "Pentax");
18112 #ifdef LIBRAW_LIBRARY_BUILD
18113   remove_trailing_spaces(make, sizeof(make));
18114   remove_trailing_spaces(model, sizeof(model));
18115 #else
18116   cp = make + strlen(make); /* Remove trailing spaces */
18117   while (*--cp == ' ')
18118     *cp = 0;
18119   cp = model + strlen(model);
18120   while (*--cp == ' ')
18121     *cp = 0;
18122 #endif
18123   i = strbuflen(make); /* Remove make from model */
18124   if (!strncasecmp(model, make, i) && model[i++] == ' ')
18125     memmove(model, model + i, 64 - i);
18126   if (!strncmp(model, "FinePix ", 8))
18127     memmove(model, model + 8,strlen(model)-7);
18128   if (!strncmp(model, "Digital Camera ", 15))
18129    memmove(model, model + 15,strlen(model)-14);
18130   desc[511] = artist[63] = make[63] = model[63] = model2[63] = 0;
18131   if (!is_raw)
18132     goto notraw;
18133 
18134   if (!height)
18135     height = raw_height;
18136   if (!width)
18137     width = raw_width;
18138   if (height == 2624 && width == 3936) /* Pentax K10D and Samsung GX10 */
18139   {
18140     height = 2616;
18141     width = 3896;
18142   }
18143   if (height == 3136 && width == 4864) /* Pentax K20D and Samsung GX20 */
18144   {
18145     height = 3124;
18146     width = 4688;
18147     filters = 0x16161616;
18148   }
18149   if (width == 4352 && (!strcmp(model, "K-r") || !strcmp(model, "K-x")))
18150   {
18151     width = 4309;
18152     filters = 0x16161616;
18153   }
18154   if (width >= 4960 && !strncmp(model, "K-5", 3))
18155   {
18156     left_margin = 10;
18157     width = 4950;
18158     filters = 0x16161616;
18159   }
18160   if (width == 6080 && !strcmp(model, "K-70"))
18161   {
18162     height = 4016;
18163     top_margin = 32;
18164     width = 6020;
18165     left_margin = 60;
18166   }
18167   if (width == 4736 && !strcmp(model, "K-7"))
18168   {
18169     height = 3122;
18170     width = 4684;
18171     filters = 0x16161616;
18172     top_margin = 2;
18173   }
18174   if (width == 6080 && !strcmp(model, "K-3 II")) /* moved back */
18175   {
18176     left_margin = 4;
18177     width = 6040;
18178   }
18179   if (width == 6112 && !strcmp(model, "KP"))
18180   {
18181     /* From DNG, maybe too strict */
18182     left_margin = 54;
18183     top_margin = 28;
18184     width = 6028;
18185     height = raw_height - top_margin;
18186   }
18187   if (width == 6080 && !strcmp(model, "K-3"))
18188   {
18189     left_margin = 4;
18190     width = 6040;
18191   }
18192   if (width == 7424 && !strcmp(model, "645D"))
18193   {
18194     height = 5502;
18195     width = 7328;
18196     filters = 0x61616161;
18197     top_margin = 29;
18198     left_margin = 48;
18199   }
18200   if (height == 3014 && width == 4096) /* Ricoh GX200 */
18201     width = 4014;
18202   if (dng_version)
18203   {
18204     if (filters == UINT_MAX)
18205       filters = 0;
18206     if (filters)
18207       is_raw *= tiff_samples;
18208     else
18209       colors = tiff_samples;
18210     switch (tiff_compress)
18211     {
18212     case 0: /* Compression not set, assuming uncompressed */
18213     case 1:
18214       load_raw = &CLASS packed_dng_load_raw;
18215       break;
18216     case 7:
18217       load_raw = &CLASS lossless_dng_load_raw;
18218       break;
18219 #ifdef LIBRAW_LIBRARY_BUILD
18220     case 8:
18221       load_raw = &CLASS deflate_dng_load_raw;
18222       break;
18223 #endif
18224     case 34892:
18225       load_raw = &CLASS lossy_dng_load_raw;
18226       break;
18227     default:
18228       load_raw = 0;
18229     }
18230     if (!strncmp(make, "Canon", 5) && unique_id)
18231     {
18232       for (i = 0; i < sizeof unique / sizeof *unique; i++)
18233         if (unique_id == 0x80000000 + unique[i].id)
18234         {
18235           strcpy(model, unique[i].t_model);
18236           break;
18237         }
18238     }
18239     if (!strncasecmp(make, "Sony", 4) && unique_id)
18240     {
18241       for (i = 0; i < sizeof sonique / sizeof *sonique; i++)
18242         if (unique_id == sonique[i].id)
18243         {
18244           strcpy(model, sonique[i].t_model);
18245           break;
18246         }
18247     }
18248     goto dng_skip;
18249   }
18250   if (!strncmp(make, "Canon", 5) && !fsize && tiff_bps != 15)
18251   {
18252     if (!load_raw)
18253       load_raw = &CLASS lossless_jpeg_load_raw;
18254     for (i = 0; i < sizeof canon / sizeof *canon; i++)
18255       if (raw_width == canon[i][0] && raw_height == canon[i][1])
18256       {
18257         width = raw_width - (left_margin = canon[i][2]);
18258         height = raw_height - (top_margin = canon[i][3]);
18259         width -= canon[i][4];
18260         height -= canon[i][5];
18261         mask[0][1] = canon[i][6];
18262         mask[0][3] = -canon[i][7];
18263         mask[1][1] = canon[i][8];
18264         mask[1][3] = -canon[i][9];
18265         if (canon[i][10])
18266           filters = canon[i][10] * 0x01010101U;
18267       }
18268     if ((unique_id | 0x20000) == 0x2720000)
18269     {
18270       left_margin = 8;
18271       top_margin = 16;
18272     }
18273   }
18274   if (!strncmp(make, "Canon", 5) && unique_id)
18275   {
18276     for (i = 0; i < sizeof unique / sizeof *unique; i++)
18277       if (unique_id == 0x80000000 + unique[i].id)
18278       {
18279         adobe_coeff("Canon", unique[i].t_model);
18280         strcpy(model, unique[i].t_model);
18281       }
18282   }
18283 
18284   if (!strncasecmp(make, "Sony", 4) && unique_id)
18285   {
18286     for (i = 0; i < sizeof sonique / sizeof *sonique; i++)
18287       if (unique_id == sonique[i].id)
18288       {
18289         adobe_coeff("Sony", sonique[i].t_model);
18290         strcpy(model, sonique[i].t_model);
18291       }
18292   }
18293 
18294   if (!strncmp(make, "Nikon", 5))
18295   {
18296     if (!load_raw)
18297       load_raw = &CLASS packed_load_raw;
18298     if (model[0] == 'E')
18299       load_flags |= !data_offset << 2 | 2;
18300   }
18301 
18302   /* Set parameters based on camera name (for non-DNG files). */
18303 
18304   if (!strcmp(model, "KAI-0340") && find_green(16, 16, 3840, 5120) < 25)
18305   {
18306     height = 480;
18307     top_margin = filters = 0;
18308     strcpy(model, "C603");
18309   }
18310 #ifndef LIBRAW_LIBRARY_BUILD
18311   if (!strcmp(make, "Sony") && raw_width > 3888 && !black && !cblack[0])
18312     black = 128 << (tiff_bps - 12);
18313 #else
18314   /* Always 512 for arw2_load_raw */
18315   if (!strcmp(make, "Sony") && raw_width > 3888 && !black && !cblack[0])
18316     black = (load_raw == &LibRaw::sony_arw2_load_raw) ? 512 : (128 << (tiff_bps - 12));
18317 #endif
18318 
18319   if (is_foveon)
18320   {
18321     if (height * 2 < width)
18322       pixel_aspect = 0.5;
18323     if (height > width)
18324       pixel_aspect = 2;
18325     filters = 0;
18326   }
18327   else if (!strncmp(make, "Pentax", 6) && !strncmp(model, "K-1", 3))
18328   {
18329     top_margin = 18;
18330     height = raw_height - top_margin;
18331     if (raw_width == 7392)
18332     {
18333       left_margin = 6;
18334       width = 7376;
18335     }
18336   }
18337   else if (!strncmp(make, "Canon", 5) && tiff_bps == 15)
18338   {
18339     switch (width)
18340     {
18341     case 3344:
18342       width -= 66;
18343     case 3872:
18344       width -= 6;
18345     }
18346     if (height > width)
18347     {
18348       SWAP(height, width);
18349       SWAP(raw_height, raw_width);
18350     }
18351     if (width == 7200 && height == 3888)
18352     {
18353       raw_width = width = 6480;
18354       raw_height = height = 4320;
18355     }
18356     filters = 0;
18357     tiff_samples = colors = 3;
18358     load_raw = &CLASS canon_sraw_load_raw;
18359   }
18360   else if (!strcmp(model, "PowerShot 600"))
18361   {
18362     height = 613;
18363     width = 854;
18364     raw_width = 896;
18365     colors = 4;
18366     filters = 0xe1e4e1e4;
18367     load_raw = &CLASS canon_600_load_raw;
18368   }
18369   else if (!strcmp(model, "PowerShot A5") || !strcmp(model, "PowerShot A5 Zoom"))
18370   {
18371     height = 773;
18372     width = 960;
18373     raw_width = 992;
18374     pixel_aspect = 256 / 235.0;
18375     filters = 0x1e4e1e4e;
18376     goto canon_a5;
18377   }
18378   else if (!strcmp(model, "PowerShot A50"))
18379   {
18380     height = 968;
18381     width = 1290;
18382     raw_width = 1320;
18383     filters = 0x1b4e4b1e;
18384     goto canon_a5;
18385   }
18386   else if (!strcmp(model, "PowerShot Pro70"))
18387   {
18388     height = 1024;
18389     width = 1552;
18390     filters = 0x1e4b4e1b;
18391   canon_a5:
18392     colors = 4;
18393     tiff_bps = 10;
18394     load_raw = &CLASS packed_load_raw;
18395     load_flags = 40;
18396   }
18397   else if (!strcmp(model, "PowerShot Pro90 IS") || !strcmp(model, "PowerShot G1"))
18398   {
18399     colors = 4;
18400     filters = 0xb4b4b4b4;
18401   }
18402   else if (!strcmp(model, "PowerShot A610"))
18403   {
18404     if (canon_s2is())
18405       strcpy(model + 10, "S2 IS");
18406   }
18407   else if (!strcmp(model, "PowerShot SX220 HS"))
18408   {
18409     mask[1][3] = -4;
18410     top_margin = 16;
18411     left_margin = 92;
18412   }
18413   else if (!strcmp(model, "PowerShot S120"))
18414   {
18415     raw_width = 4192;
18416     raw_height = 3062;
18417     width = 4022;
18418     height = 3016;
18419     mask[0][0] = top_margin = 31;
18420     mask[0][2] = top_margin + height;
18421     left_margin = 120;
18422     mask[0][1] = 23;
18423     mask[0][3] = 72;
18424   }
18425   else if (!strcmp(model, "PowerShot G16"))
18426   {
18427     mask[0][0] = 0;
18428     mask[0][2] = 80;
18429     mask[0][1] = 0;
18430     mask[0][3] = 16;
18431     top_margin = 29;
18432     left_margin = 120;
18433     width = raw_width - left_margin - 48;
18434     height = raw_height - top_margin - 14;
18435   }
18436   else if (!strcmp(model, "PowerShot SX50 HS"))
18437   {
18438     top_margin = 17;
18439   }
18440   else if (!strcmp(model, "EOS D2000C"))
18441   {
18442     filters = 0x61616161;
18443     if (!black)
18444       black = curve[200];
18445   }
18446   else if (!strcmp(model, "D1"))
18447   {
18448     cam_mul[0] *= 256 / 527.0;
18449     cam_mul[2] *= 256 / 317.0;
18450   }
18451   else if (!strcmp(model, "D1X"))
18452   {
18453     width -= 4;
18454     pixel_aspect = 0.5;
18455   }
18456   else if (!strcmp(model, "D40X") || !strcmp(model, "D60") || !strcmp(model, "D80") || !strcmp(model, "D3000"))
18457   {
18458     height -= 3;
18459     width -= 4;
18460   }
18461   else if (!strcmp(model, "D3") || !strcmp(model, "D3S") || !strcmp(model, "D700"))
18462   {
18463     width -= 4;
18464     left_margin = 2;
18465   }
18466   else if (!strcmp(model, "D3100"))
18467   {
18468     width -= 28;
18469     left_margin = 6;
18470   }
18471   else if (!strcmp(model, "D5000") || !strcmp(model, "D90"))
18472   {
18473     width -= 42;
18474   }
18475   else if (!strcmp(model, "D5100") || !strcmp(model, "D7000") || !strcmp(model, "COOLPIX A"))
18476   {
18477     width -= 44;
18478   }
18479   else if (!strcmp(model, "D3200") || !strncmp(model, "D6", 2) || !strncmp(model, "D800", 4))
18480   {
18481     width -= 46;
18482   }
18483   else if (!strcmp(model, "D4") || !strcmp(model, "Df"))
18484   {
18485     width -= 52;
18486     left_margin = 2;
18487   }
18488   else if (!strcmp(model, "D500"))
18489   {
18490     // Empty - to avoid width-1 below
18491   }
18492   else if (!strncmp(model, "D40", 3) || !strncmp(model, "D50", 3) || !strncmp(model, "D70", 3))
18493   {
18494     width--;
18495   }
18496   else if (!strcmp(model, "D100"))
18497   {
18498     if (load_flags)
18499       raw_width = (width += 3) + 3;
18500   }
18501   else if (!strcmp(model, "D200"))
18502   {
18503     left_margin = 1;
18504     width -= 4;
18505     filters = 0x94949494;
18506   }
18507   else if (!strncmp(model, "D2H", 3))
18508   {
18509     left_margin = 6;
18510     width -= 14;
18511   }
18512   else if (!strncmp(model, "D2X", 3))
18513   {
18514     if (width == 3264)
18515       width -= 32;
18516     else
18517       width -= 8;
18518   }
18519   else if (!strncmp(model, "D300", 4))
18520   {
18521     width -= 32;
18522   }
18523   else if (!strncmp(make, "Nikon", 5) && raw_width == 4032)
18524   {
18525     if (!strcmp(model, "COOLPIX P7700"))
18526     {
18527       adobe_coeff("Nikon", "COOLPIX P7700");
18528       maximum = 65504;
18529       load_flags = 0;
18530     }
18531     else if (!strcmp(model, "COOLPIX P7800"))
18532     {
18533       adobe_coeff("Nikon", "COOLPIX P7800");
18534       maximum = 65504;
18535       load_flags = 0;
18536     }
18537     else if (!strcmp(model, "COOLPIX P340"))
18538       load_flags = 0;
18539   }
18540   else if (!strncmp(model, "COOLPIX P", 9) && raw_width != 4032)
18541   {
18542     load_flags = 24;
18543     filters = 0x94949494;
18544     if (model[9] == '7' && (iso_speed >= 400 || iso_speed == 0) && !strstr(software, "V1.2"))
18545       black = 255;
18546   }
18547   else if (!strncmp(model, "COOLPIX B700", 12))
18548   {
18549     load_flags = 24;
18550     black = 200;
18551   }
18552   else if (!strncmp(model, "1 ", 2))
18553   {
18554     height -= 2;
18555   }
18556   else if (fsize == 1581060)
18557   {
18558     simple_coeff(3);
18559     pre_mul[0] = 1.2085;
18560     pre_mul[1] = 1.0943;
18561     pre_mul[3] = 1.1103;
18562   }
18563   else if (fsize == 3178560)
18564   {
18565     cam_mul[0] *= 4;
18566     cam_mul[2] *= 4;
18567   }
18568   else if (fsize == 4771840)
18569   {
18570     if (!timestamp && nikon_e995())
18571       strcpy(model, "E995");
18572     if (strcmp(model, "E995"))
18573     {
18574       filters = 0xb4b4b4b4;
18575       simple_coeff(3);
18576       pre_mul[0] = 1.196;
18577       pre_mul[1] = 1.246;
18578       pre_mul[2] = 1.018;
18579     }
18580   }
18581   else if (fsize == 2940928)
18582   {
18583     if (!timestamp && !nikon_e2100())
18584       strcpy(model, "E2500");
18585     if (!strcmp(model, "E2500"))
18586     {
18587       height -= 2;
18588       load_flags = 6;
18589       colors = 4;
18590       filters = 0x4b4b4b4b;
18591     }
18592   }
18593   else if (fsize == 4775936)
18594   {
18595     if (!timestamp)
18596       nikon_3700();
18597     if (model[0] == 'E' && atoi(model + 1) < 3700)
18598       filters = 0x49494949;
18599     if (!strcmp(model, "Optio 33WR"))
18600     {
18601       flip = 1;
18602       filters = 0x16161616;
18603     }
18604     if (make[0] == 'O')
18605     {
18606       i = find_green(12, 32, 1188864, 3576832);
18607       c = find_green(12, 32, 2383920, 2387016);
18608       if (abs(i) < abs(c))
18609       {
18610         SWAP(i, c);
18611         load_flags = 24;
18612       }
18613       if (i < 0)
18614         filters = 0x61616161;
18615     }
18616   }
18617   else if (fsize == 5869568)
18618   {
18619     if (!timestamp && minolta_z2())
18620     {
18621       strcpy(make, "Minolta");
18622       strcpy(model, "DiMAGE Z2");
18623     }
18624     load_flags = 6 + 24 * (make[0] == 'M');
18625   }
18626   else if (fsize == 6291456)
18627   {
18628     fseek(ifp, 0x300000, SEEK_SET);
18629     if ((order = guess_byte_order(0x10000)) == 0x4d4d)
18630     {
18631       height -= (top_margin = 16);
18632       width -= (left_margin = 28);
18633       maximum = 0xf5c0;
18634       strcpy(make, "ISG");
18635       model[0] = 0;
18636     }
18637   }
18638   else if (!strncmp(make, "Fujifilm", 8))
18639   {
18640     if (!strcmp(model, "X-A3") || !strcmp(model, "X-A10")
18641     || !strcmp(model, "X-A5") || !strcmp(model, "X-A20"))
18642     {
18643       left_margin = 0;
18644       top_margin = 0;
18645       width = raw_width;
18646       height = raw_height;
18647     }
18648     if (!strcmp(model + 7, "S2Pro"))
18649     {
18650       strcpy(model, "S2Pro");
18651       height = 2144;
18652       width = 2880;
18653       flip = 6;
18654     }
18655     else if (load_raw != &CLASS packed_load_raw && strncmp(model, "X-", 2) && filters >=1000) // Bayer and not X-models
18656       maximum = (is_raw == 2 && shot_select) ? 0x2f00 : 0x3e00;
18657     top_margin = (raw_height - height) >> 2 << 1;
18658     left_margin = (raw_width - width) >> 2 << 1;
18659     if (width == 2848 || width == 3664)
18660       filters = 0x16161616;
18661     if (width == 4032 || width == 4952)
18662       left_margin = 0;
18663     if (width == 3328 && (width -= 66))
18664       left_margin = 34;
18665     if (width == 4936)
18666       left_margin = 4;
18667     if (width == 6032)
18668       left_margin = 0;
18669     if (!strcmp(model, "HS50EXR") || !strcmp(model, "F900EXR"))
18670     {
18671       width += 2;
18672       left_margin = 0;
18673       filters = 0x16161616;
18674     }
18675     if (!strcmp(model, "GFX 50S"))
18676     {
18677       left_margin = 0;
18678       top_margin = 0;
18679     }
18680     if (!strcmp(model, "S5500"))
18681     {
18682       height -= (top_margin = 6);
18683     }
18684     if (fuji_layout)
18685       raw_width *= is_raw;
18686     if (filters == 9)
18687       FORC(36)((char *)xtrans)[c] = xtrans_abs[(c / 6 + top_margin) % 6][(c + left_margin) % 6];
18688   }
18689   else if (!strcmp(model, "KD-400Z"))
18690   {
18691     height = 1712;
18692     width = 2312;
18693     raw_width = 2336;
18694     goto konica_400z;
18695   }
18696   else if (!strcmp(model, "KD-510Z"))
18697   {
18698     goto konica_510z;
18699   }
18700   else if (!strncasecmp(make, "Minolta", 7))
18701   {
18702     if (!load_raw && (maximum = 0xfff))
18703       load_raw = &CLASS unpacked_load_raw;
18704     if (!strncmp(model, "DiMAGE A", 8))
18705     {
18706       if (!strcmp(model, "DiMAGE A200"))
18707         filters = 0x49494949;
18708       tiff_bps = 12;
18709       load_raw = &CLASS packed_load_raw;
18710     }
18711     else if (!strncmp(model, "ALPHA", 5) || !strncmp(model, "DYNAX", 5) || !strncmp(model, "MAXXUM", 6))
18712     {
18713       sprintf(model + 20, "DYNAX %-10s", model + 6 + (model[0] == 'M'));
18714       adobe_coeff(make, model + 20);
18715       load_raw = &CLASS packed_load_raw;
18716     }
18717     else if (!strncmp(model, "DiMAGE G", 8))
18718     {
18719       if (model[8] == '4')
18720       {
18721         height = 1716;
18722         width = 2304;
18723       }
18724       else if (model[8] == '5')
18725       {
18726       konica_510z:
18727         height = 1956;
18728         width = 2607;
18729         raw_width = 2624;
18730       }
18731       else if (model[8] == '6')
18732       {
18733         height = 2136;
18734         width = 2848;
18735       }
18736       data_offset += 14;
18737       filters = 0x61616161;
18738     konica_400z:
18739       load_raw = &CLASS unpacked_load_raw;
18740       maximum = 0x3df;
18741       order = 0x4d4d;
18742     }
18743   }
18744   else if (!strcmp(model, "*ist D"))
18745   {
18746     load_raw = &CLASS unpacked_load_raw;
18747     data_error = -1;
18748   }
18749   else if (!strcmp(model, "*ist DS"))
18750   {
18751     height -= 2;
18752   }
18753   else if (!strncmp(make, "Samsung", 7) && raw_width == 4704)
18754   {
18755     height -= top_margin = 8;
18756     width -= 2 * (left_margin = 8);
18757     load_flags = 32;
18758   }
18759   else if (!strncmp(make, "Samsung", 7) && !strcmp(model, "NX3000"))
18760   {
18761     top_margin = 38;
18762     left_margin = 92;
18763     width = 5456;
18764     height = 3634;
18765     filters = 0x61616161;
18766     colors = 3;
18767   }
18768   else if (!strncmp(make, "Samsung", 7) && raw_height == 3714)
18769   {
18770     height -= top_margin = 18;
18771     left_margin = raw_width - (width = 5536);
18772     if (raw_width != 5600)
18773       left_margin = top_margin = 0;
18774     filters = 0x61616161;
18775     colors = 3;
18776   }
18777   else if (!strncmp(make, "Samsung", 7) && raw_width == 5632)
18778   {
18779     order = 0x4949;
18780     height = 3694;
18781     top_margin = 2;
18782     width = 5574 - (left_margin = 32 + tiff_bps);
18783     if (tiff_bps == 12)
18784       load_flags = 80;
18785   }
18786   else if (!strncmp(make, "Samsung", 7) && raw_width == 5664)
18787   {
18788     height -= top_margin = 17;
18789     left_margin = 96;
18790     width = 5544;
18791     filters = 0x49494949;
18792   }
18793   else if (!strncmp(make, "Samsung", 7) && raw_width == 6496)
18794   {
18795     filters = 0x61616161;
18796 #ifdef LIBRAW_LIBRARY_BUILD
18797     if (!black && !cblack[0] && !cblack[1] && !cblack[2] && !cblack[3])
18798 #endif
18799       black = 1 << (tiff_bps - 7);
18800   }
18801   else if (!strcmp(model, "EX1"))
18802   {
18803     order = 0x4949;
18804     height -= 20;
18805     top_margin = 2;
18806     if ((width -= 6) > 3682)
18807     {
18808       height -= 10;
18809       width -= 46;
18810       top_margin = 8;
18811     }
18812   }
18813   else if (!strcmp(model, "WB2000"))
18814   {
18815     order = 0x4949;
18816     height -= 3;
18817     top_margin = 2;
18818     if ((width -= 10) > 3718)
18819     {
18820       height -= 28;
18821       width -= 56;
18822       top_margin = 8;
18823     }
18824   }
18825   else if (strstr(model, "WB550"))
18826   {
18827     strcpy(model, "WB550");
18828   }
18829   else if (!strcmp(model, "EX2F"))
18830   {
18831     height = 3030;
18832     width = 4040;
18833     top_margin = 15;
18834     left_margin = 24;
18835     order = 0x4949;
18836     filters = 0x49494949;
18837     load_raw = &CLASS unpacked_load_raw;
18838   }
18839   else if (!strcmp(model, "STV680 VGA"))
18840   {
18841     black = 16;
18842   }
18843   else if (!strcmp(model, "N95"))
18844   {
18845     height = raw_height - (top_margin = 2);
18846   }
18847   else if (!strcmp(model, "640x480"))
18848   {
18849     gamma_curve(0.45, 4.5, 1, 255);
18850   }
18851   else if (!strncmp(make, "Hasselblad", 10))
18852   {
18853     if (load_raw == &CLASS lossless_jpeg_load_raw)
18854       load_raw = &CLASS hasselblad_load_raw;
18855     if (raw_width == 7262)
18856     {
18857       height = 5444;
18858       width = 7248;
18859       top_margin = 4;
18860       left_margin = 7;
18861       filters = 0x61616161;
18862       if (!strncasecmp(model, "H3D", 3))
18863       {
18864         adobe_coeff("Hasselblad", "H3DII-39");
18865         strcpy(model, "H3DII-39");
18866       }
18867     }
18868     else if (raw_width == 12000) // H6D 100c, A6D 100c
18869     {
18870       left_margin = 64;
18871       width = 11608;
18872       top_margin = 108;
18873       height = raw_height - top_margin;
18874       adobe_coeff("Hasselblad", "H6D-100c");
18875     }
18876     else if (raw_width == 7410 || raw_width == 8282)
18877     {
18878       height -= 84;
18879       width -= 82;
18880       top_margin = 4;
18881       left_margin = 41;
18882       filters = 0x61616161;
18883       adobe_coeff("Hasselblad", "H4D-40");
18884       strcpy(model, "H4D-40");
18885     }
18886     else if (raw_width == 8384) // X1D
18887     {
18888       top_margin = 96;
18889       height -= 96;
18890       left_margin = 48;
18891       width -= 106;
18892       adobe_coeff("Hasselblad", "X1D");
18893       maximum = 0xffff;
18894       tiff_bps = 16;
18895     }
18896     else if (raw_width == 9044)
18897     {
18898       if (black > 500)
18899       {
18900         top_margin = 12;
18901         left_margin = 44;
18902         width = 8956;
18903         height = 6708;
18904         memset(cblack, 0, sizeof(cblack));
18905         adobe_coeff("Hasselblad", "H4D-60");
18906         strcpy(model, "H4D-60");
18907         black = 512;
18908       }
18909       else
18910       {
18911         height = 6716;
18912         width = 8964;
18913         top_margin = 8;
18914         left_margin = 40;
18915         black += load_flags = 256;
18916         maximum = 0x8101;
18917         strcpy(model, "H3DII-60");
18918       }
18919     }
18920     else if (raw_width == 4090)
18921     {
18922       strcpy(model, "V96C");
18923       height -= (top_margin = 6);
18924       width -= (left_margin = 3) + 7;
18925       filters = 0x61616161;
18926     }
18927     else if (raw_width == 8282 && raw_height == 6240)
18928     {
18929       if (!strncasecmp(model, "H5D", 3))
18930       {
18931         /* H5D 50*/
18932         left_margin = 54;
18933         top_margin = 16;
18934         width = 8176;
18935         height = 6132;
18936         black = 256;
18937         strcpy(model, "H5D-50");
18938       }
18939       else if (!strncasecmp(model, "H3D", 3))
18940       {
18941         black = 0;
18942         left_margin = 54;
18943         top_margin = 16;
18944         width = 8176;
18945         height = 6132;
18946         memset(cblack, 0, sizeof(cblack));
18947         adobe_coeff("Hasselblad", "H3D-50");
18948         strcpy(model, "H3D-50");
18949       }
18950     }
18951     else if (raw_width == 8374 && raw_height == 6304)
18952     {
18953       /* H5D 50c*/
18954       left_margin = 52;
18955       top_margin = 100;
18956       width = 8272;
18957       height = 6200;
18958       black = 256;
18959       strcpy(model, "H5D-50c");
18960     }
18961     if (tiff_samples > 1)
18962     {
18963       is_raw = tiff_samples + 1;
18964       if (!shot_select && !half_size)
18965         filters = 0;
18966     }
18967   }
18968   else if (!strncmp(make, "Sinar", 5))
18969   {
18970     if (!load_raw)
18971       load_raw = &CLASS unpacked_load_raw;
18972     if (is_raw > 1 && !shot_select && !half_size)
18973       filters = 0;
18974     maximum = 0x3fff;
18975   }
18976   else if (!strncmp(make, "Leaf", 4))
18977   {
18978     maximum = 0x3fff;
18979     fseek(ifp, data_offset, SEEK_SET);
18980     if (ljpeg_start(&jh, 1) && jh.bits == 15)
18981       maximum = 0x1fff;
18982     if (tiff_samples > 1)
18983       filters = 0;
18984     if (tiff_samples > 1 || tile_length < raw_height)
18985     {
18986       load_raw = &CLASS leaf_hdr_load_raw;
18987       raw_width = tile_width;
18988     }
18989     if ((width | height) == 2048)
18990     {
18991       if (tiff_samples == 1)
18992       {
18993         filters = 1;
18994         strcpy(cdesc, "RBTG");
18995         strcpy(model, "CatchLight");
18996         top_margin = 8;
18997         left_margin = 18;
18998         height = 2032;
18999         width = 2016;
19000       }
19001       else
19002       {
19003         strcpy(model, "DCB2");
19004         top_margin = 10;
19005         left_margin = 16;
19006         height = 2028;
19007         width = 2022;
19008       }
19009     }
19010     else if (width + height == 3144 + 2060)
19011     {
19012       if (!model[0])
19013         strcpy(model, "Cantare");
19014       if (width > height)
19015       {
19016         top_margin = 6;
19017         left_margin = 32;
19018         height = 2048;
19019         width = 3072;
19020         filters = 0x61616161;
19021       }
19022       else
19023       {
19024         left_margin = 6;
19025         top_margin = 32;
19026         width = 2048;
19027         height = 3072;
19028         filters = 0x16161616;
19029       }
19030       if (!cam_mul[0] || model[0] == 'V')
19031         filters = 0;
19032       else
19033         is_raw = tiff_samples;
19034     }
19035     else if (width == 2116)
19036     {
19037       strcpy(model, "Valeo 6");
19038       height -= 2 * (top_margin = 30);
19039       width -= 2 * (left_margin = 55);
19040       filters = 0x49494949;
19041     }
19042     else if (width == 3171)
19043     {
19044       strcpy(model, "Valeo 6");
19045       height -= 2 * (top_margin = 24);
19046       width -= 2 * (left_margin = 24);
19047       filters = 0x16161616;
19048     }
19049   }
19050   else if (!strncmp(make, "Leica", 5) || !strncmp(make, "Panasonic", 9) || !strncasecmp(make, "YUNEEC", 6))
19051   {
19052 
19053     if (raw_width > 0 && ((flen - data_offset) / (raw_width * 8 / 7) == raw_height))
19054       load_raw = &CLASS panasonic_load_raw;
19055     if (!load_raw)
19056     {
19057       load_raw = &CLASS unpacked_load_raw;
19058       load_flags = 4;
19059     }
19060     zero_is_bad = 1;
19061     if ((height += 12) > raw_height)
19062       height = raw_height;
19063     for (i = 0; i < sizeof pana / sizeof *pana; i++)
19064       if (raw_width == pana[i][0] && raw_height == pana[i][1])
19065       {
19066         left_margin = pana[i][2];
19067         top_margin = pana[i][3];
19068         width += pana[i][4];
19069         height += pana[i][5];
19070       }
19071     filters = 0x01010101U * (uchar) "\x94\x61\x49\x16"[((filters - 1) ^ (left_margin & 1) ^ (top_margin << 1)) & 3];
19072   }
19073   else if (!strcmp(model, "C770UZ"))
19074   {
19075     height = 1718;
19076     width = 2304;
19077     filters = 0x16161616;
19078     load_raw = &CLASS packed_load_raw;
19079     load_flags = 30;
19080   }
19081   else if (!strncmp(make, "Olympus", 7))
19082   {
19083     height += height & 1;
19084     if (exif_cfa)
19085       filters = exif_cfa;
19086     if (width == 4100)
19087       width -= 4;
19088     if (width == 4080)
19089       width -= 24;
19090     if (width == 9280)
19091     {
19092       width -= 6;
19093       height -= 6;
19094     }
19095     if (load_raw == &CLASS unpacked_load_raw)
19096       load_flags = 4;
19097     tiff_bps = 12;
19098     if (!strcmp(model, "E-300") || !strcmp(model, "E-500"))
19099     {
19100       width -= 20;
19101       if (load_raw == &CLASS unpacked_load_raw)
19102       {
19103         maximum = 0xfc3;
19104         memset(cblack, 0, sizeof cblack);
19105       }
19106     }
19107     else if (!strcmp(model, "STYLUS1"))
19108     {
19109       width -= 14;
19110       maximum = 0xfff;
19111     }
19112     else if (!strcmp(model, "E-330"))
19113     {
19114       width -= 30;
19115       if (load_raw == &CLASS unpacked_load_raw)
19116         maximum = 0xf79;
19117     }
19118     else if (!strcmp(model, "SP550UZ"))
19119     {
19120       thumb_length = flen - (thumb_offset = 0xa39800);
19121       thumb_height = 480;
19122       thumb_width = 640;
19123     }
19124     else if (!strcmp(model, "TG-4"))
19125     {
19126       width -= 16;
19127     }
19128     else if (!strcmp(model, "TG-5"))
19129     {
19130       width -= 26;
19131     }
19132   }
19133   else if (!strcmp(model, "N Digital"))
19134   {
19135     height = 2047;
19136     width = 3072;
19137     filters = 0x61616161;
19138     data_offset = 0x1a00;
19139     load_raw = &CLASS packed_load_raw;
19140   }
19141   else if (!strcmp(model, "DSC-F828"))
19142   {
19143     width = 3288;
19144     left_margin = 5;
19145     mask[1][3] = -17;
19146     data_offset = 862144;
19147     load_raw = &CLASS sony_load_raw;
19148     filters = 0x9c9c9c9c;
19149     colors = 4;
19150     strcpy(cdesc, "RGBE");
19151   }
19152   else if (!strcmp(model, "DSC-V3"))
19153   {
19154     width = 3109;
19155     left_margin = 59;
19156     mask[0][1] = 9;
19157     data_offset = 787392;
19158     load_raw = &CLASS sony_load_raw;
19159   }
19160   else if (!strncmp(make, "Sony", 4) && raw_width == 3984)
19161   {
19162     width = 3925;
19163     order = 0x4d4d;
19164   }
19165   else if (!strncmp(make, "Sony", 4) && raw_width == 4288)
19166   {
19167     width -= 32;
19168   }
19169   else if (!strcmp(make, "Sony") && raw_width == 4600)
19170   {
19171     if (!strcmp(model, "DSLR-A350"))
19172       height -= 4;
19173     black = 0;
19174   }
19175   else if (!strncmp(make, "Sony", 4) && raw_width == 4928)
19176   {
19177     if (height < 3280)
19178       width -= 8;
19179   }
19180   else if (!strncmp(make, "Sony", 4) && raw_width == 5504)
19181   { // ILCE-3000//5000
19182     width -= height > 3664 ? 8 : 32;
19183   }
19184   else if (!strncmp(make, "Sony", 4) && raw_width == 6048)
19185   {
19186     width -= 24;
19187     if (strstr(model, "RX1") || strstr(model, "A99"))
19188       width -= 6;
19189   }
19190   else if (!strncmp(make, "Sony", 4) && raw_width == 7392)
19191   {
19192     width -= 30;
19193   }
19194   else if (!strncmp(make, "Sony", 4) && raw_width == 8000)
19195   {
19196     width -= 32;
19197   }
19198   else if (!strcmp(model, "DSLR-A100"))
19199   {
19200     if (width == 3880)
19201     {
19202       height--;
19203       width = ++raw_width;
19204     }
19205     else
19206     {
19207       height -= 4;
19208       width -= 4;
19209       order = 0x4d4d;
19210       load_flags = 2;
19211     }
19212     filters = 0x61616161;
19213   }
19214   else if (!strcmp(model, "PIXL"))
19215   {
19216     height -= top_margin = 4;
19217     width -= left_margin = 32;
19218     gamma_curve(0, 7, 1, 255);
19219   }
19220   else if (!strcmp(model, "C603") || !strcmp(model, "C330") || !strcmp(model, "12MP"))
19221   {
19222     order = 0x4949;
19223     if (filters && data_offset)
19224     {
19225       fseek(ifp, data_offset < 4096 ? 168 : 5252, SEEK_SET);
19226       read_shorts(curve, 256);
19227     }
19228     else
19229       gamma_curve(0, 3.875, 1, 255);
19230     load_raw = filters ? &CLASS eight_bit_load_raw
19231                        : strcmp(model, "C330") ? &CLASS kodak_c603_load_raw : &CLASS kodak_c330_load_raw;
19232     load_flags = tiff_bps > 16;
19233     tiff_bps = 8;
19234   }
19235   else if (!strncasecmp(model, "EasyShare", 9))
19236   {
19237     data_offset = data_offset < 0x15000 ? 0x15000 : 0x17000;
19238     load_raw = &CLASS packed_load_raw;
19239   }
19240   else if (!strncasecmp(make, "Kodak", 5))
19241   {
19242     if (filters == UINT_MAX)
19243       filters = 0x61616161;
19244     if (!strncmp(model, "NC2000", 6) || !strncmp(model, "EOSDCS", 6) || !strncmp(model, "DCS4", 4))
19245     {
19246       width -= 4;
19247       left_margin = 2;
19248       if (model[6] == ' ')
19249         model[6] = 0;
19250       if (!strcmp(model, "DCS460A"))
19251         goto bw;
19252     }
19253     else if (!strcmp(model, "DCS660M"))
19254     {
19255       black = 214;
19256       goto bw;
19257     }
19258     else if (!strcmp(model, "DCS760M"))
19259     {
19260     bw:
19261       colors = 1;
19262       filters = 0;
19263     }
19264     if (!strcmp(model + 4, "20X"))
19265       strcpy(cdesc, "MYCY");
19266     if (strstr(model, "DC25"))
19267     {
19268       strcpy(model, "DC25");
19269       data_offset = 15424;
19270     }
19271     if (!strncmp(model, "DC2", 3))
19272     {
19273       raw_height = 2 + (height = 242);
19274       if (!strncmp(model, "DC290", 5))
19275         iso_speed = 100;
19276       if (!strncmp(model, "DC280", 5))
19277         iso_speed = 70;
19278       if (flen < 100000)
19279       {
19280         raw_width = 256;
19281         width = 249;
19282         pixel_aspect = (4.0 * height) / (3.0 * width);
19283       }
19284       else
19285       {
19286         raw_width = 512;
19287         width = 501;
19288         pixel_aspect = (493.0 * height) / (373.0 * width);
19289       }
19290       top_margin = left_margin = 1;
19291       colors = 4;
19292       filters = 0x8d8d8d8d;
19293       simple_coeff(1);
19294       pre_mul[1] = 1.179;
19295       pre_mul[2] = 1.209;
19296       pre_mul[3] = 1.036;
19297       load_raw = &CLASS eight_bit_load_raw;
19298     }
19299     else if (!strcmp(model, "40"))
19300     {
19301       strcpy(model, "DC40");
19302       height = 512;
19303       width = 768;
19304       data_offset = 1152;
19305       load_raw = &CLASS kodak_radc_load_raw;
19306       tiff_bps = 12;
19307     }
19308     else if (strstr(model, "DC50"))
19309     {
19310       strcpy(model, "DC50");
19311       height = 512;
19312       width = 768;
19313       iso_speed = 84;
19314       data_offset = 19712;
19315       load_raw = &CLASS kodak_radc_load_raw;
19316     }
19317     else if (strstr(model, "DC120"))
19318     {
19319       strcpy(model, "DC120");
19320       raw_height = height = 976;
19321       raw_width = width = 848;
19322       iso_speed = 160;
19323       pixel_aspect = height / 0.75 / width;
19324       load_raw = tiff_compress == 7 ? &CLASS kodak_jpeg_load_raw : &CLASS kodak_dc120_load_raw;
19325     }
19326     else if (!strcmp(model, "DCS200"))
19327     {
19328       thumb_height = 128;
19329       thumb_width = 192;
19330       thumb_offset = 6144;
19331       thumb_misc = 360;
19332       iso_speed = 140;
19333       write_thumb = &CLASS layer_thumb;
19334       black = 17;
19335     }
19336   }
19337   else if (!strcmp(model, "Fotoman Pixtura"))
19338   {
19339     height = 512;
19340     width = 768;
19341     data_offset = 3632;
19342     load_raw = &CLASS kodak_radc_load_raw;
19343     filters = 0x61616161;
19344     simple_coeff(2);
19345   }
19346   else if (!strncmp(model, "QuickTake", 9))
19347   {
19348     if (head[5])
19349       strcpy(model + 10, "200");
19350     fseek(ifp, 544, SEEK_SET);
19351     height = get2();
19352     width = get2();
19353     data_offset = (get4(), get2()) == 30 ? 738 : 736;
19354     if (height > width)
19355     {
19356       SWAP(height, width);
19357       fseek(ifp, data_offset - 6, SEEK_SET);
19358       flip = ~get2() & 3 ? 5 : 6;
19359     }
19360     filters = 0x61616161;
19361   }
19362   else if (!strncmp(make, "Rollei", 6) && !load_raw)
19363   {
19364     switch (raw_width)
19365     {
19366     case 1316:
19367       height = 1030;
19368       width = 1300;
19369       top_margin = 1;
19370       left_margin = 6;
19371       break;
19372     case 2568:
19373       height = 1960;
19374       width = 2560;
19375       top_margin = 2;
19376       left_margin = 8;
19377     }
19378     filters = 0x16161616;
19379     load_raw = &CLASS rollei_load_raw;
19380   }
19381   else if (!strcmp(model, "GRAS-50S5C"))
19382   {
19383     height = 2048;
19384     width = 2440;
19385     load_raw = &CLASS unpacked_load_raw;
19386     data_offset = 0;
19387     filters = 0x49494949;
19388     order = 0x4949;
19389     maximum = 0xfffC;
19390   }
19391   else if (!strcmp(model, "BB-500CL"))
19392   {
19393     height = 2058;
19394     width = 2448;
19395     load_raw = &CLASS unpacked_load_raw;
19396     data_offset = 0;
19397     filters = 0x94949494;
19398     order = 0x4949;
19399     maximum = 0x3fff;
19400   }
19401   else if (!strcmp(model, "BB-500GE"))
19402   {
19403     height = 2058;
19404     width = 2456;
19405     load_raw = &CLASS unpacked_load_raw;
19406     data_offset = 0;
19407     filters = 0x94949494;
19408     order = 0x4949;
19409     maximum = 0x3fff;
19410   }
19411   else if (!strcmp(model, "SVS625CL"))
19412   {
19413     height = 2050;
19414     width = 2448;
19415     load_raw = &CLASS unpacked_load_raw;
19416     data_offset = 0;
19417     filters = 0x94949494;
19418     order = 0x4949;
19419     maximum = 0x0fff;
19420   }
19421   /* Early reject for damaged images */
19422   if (!load_raw || height < 22 || width < 22 ||
19423 #ifdef LIBRAW_LIBRARY_BUILD
19424       (tiff_bps > 16 && load_raw != &LibRaw::deflate_dng_load_raw)
19425 #else
19426       tiff_bps > 16
19427 #endif
19428       || tiff_samples > 4 || colors > 4 || colors < 1
19429       /* alloc in unpack() may be fooled by size adjust */
19430       || ((int)width + (int)left_margin > 65535) || ((int)height + (int)top_margin > 65535))
19431   {
19432     is_raw = 0;
19433 #ifdef LIBRAW_LIBRARY_BUILD
19434     RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY, 1, 2);
19435 #endif
19436     return;
19437   }
19438   if (!model[0])
19439     sprintf(model, "%dx%d", width, height);
19440   if (filters == UINT_MAX)
19441     filters = 0x94949494;
19442   if (thumb_offset && !thumb_height)
19443   {
19444     fseek(ifp, thumb_offset, SEEK_SET);
19445     if (ljpeg_start(&jh, 1))
19446     {
19447       thumb_width = jh.wide;
19448       thumb_height = jh.high;
19449     }
19450   }
19451 
19452 dng_skip:
19453 #ifdef LIBRAW_LIBRARY_BUILD
19454   if (dng_version) /* Override black level by DNG tags */
19455   {
19456     /* copy DNG data from per-IFD field to color.dng */
19457     int iifd = 0; // Active IFD we'll show to user.
19458     for (; iifd < tiff_nifds; iifd++)
19459       if (tiff_ifd[iifd].offset == data_offset) // found
19460         break;
19461     int pifd = -1;
19462     for (int ii = 0; ii < tiff_nifds; ii++)
19463       if (tiff_ifd[ii].offset == thumb_offset) // found
19464       {
19465         pifd = ii;
19466         break;
19467       }
19468 
19469 #define CFAROUND(value, filters) filters ? (filters >= 1000 ? ((value + 1) / 2) * 2 : ((value + 5) / 6) * 6) : value
19470 
19471 #define IFDCOLORINDEX(ifd, subset, bit)                                                                                \
19472   (tiff_ifd[ifd].dng_color[subset].parsedfields & bit) ? ifd                                                           \
19473                                                        : ((tiff_ifd[0].dng_color[subset].parsedfields & bit) ? 0 : -1)
19474 
19475 #define IFDLEVELINDEX(ifd, bit)                                                                                        \
19476   (tiff_ifd[ifd].dng_levels.parsedfields & bit) ? ifd : ((tiff_ifd[0].dng_levels.parsedfields & bit) ? 0 : -1)
19477 
19478 #define COPYARR(to, from) memmove(&to, &from, sizeof(from))
19479 
19480     if (iifd < tiff_nifds)
19481     {
19482       int sidx;
19483       // Per field, not per structure
19484 	  if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_CHECK_DNG_ILLUMINANT)
19485 	  {
19486 		  int illidx[2], cmidx[2],calidx[2], abidx;
19487 		  for(int i = 0; i < 2; i++)
19488 		  {
19489 			  illidx[i] = IFDCOLORINDEX(iifd, i, LIBRAW_DNGFM_ILLUMINANT);
19490 			  cmidx[i] = IFDCOLORINDEX(iifd, i, LIBRAW_DNGFM_COLORMATRIX);
19491 			  calidx[i] = IFDCOLORINDEX(iifd, i, LIBRAW_DNGFM_CALIBRATION);
19492 		  }
19493 		  abidx = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_ANALOGBALANCE);
19494 		  // Data found, all in same ifd, illuminants are inited
19495 		  if (illidx[0] >= 0 && illidx[0] < tiff_nifds && illidx[0] == illidx[1] && illidx[0] == cmidx[0] && illidx[0] == cmidx[1]
19496 			  && tiff_ifd[illidx[0]].dng_color[0].illuminant>0 && tiff_ifd[illidx[0]].dng_color[1].illuminant>0)
19497 		  {
19498 			  sidx = illidx[0]; // => selected IFD
19499 			  double cc[4][4], cm[4][3], cam_xyz[4][3];
19500 			  // CM -> Color Matrix
19501 			  // CC -> Camera calibration
19502 			  for (int j = 0; j < 4; j++)  for (int i = 0; i < 4; i++)  cc[j][i] = i == j;
19503 			  int colidx = -1;
19504 
19505 			  // IS D65 here?
19506 			  for(int i = 0; i < 2; i++)
19507 			  {
19508 				  int ill = tiff_ifd[sidx].dng_color[i].illuminant;
19509 				  if (tiff_ifd[sidx].dng_color[i].illuminant == LIBRAW_WBI_D65)
19510 				  {
19511 					  colidx = i; break;
19512 				  }
19513 			  }
19514 
19515 			  // Other daylight-type ill
19516 			  if(colidx<0)
19517 				  for(int i = 0; i < 2; i++)
19518 				  {
19519 					  int ill = tiff_ifd[sidx].dng_color[i].illuminant;
19520 					  if (ill == LIBRAW_WBI_Daylight || ill == LIBRAW_WBI_D55 || ill == LIBRAW_WBI_D75 || ill == LIBRAW_WBI_D50 || ill == LIBRAW_WBI_Flash)
19521 					  {
19522 						  colidx = i; break;
19523 					  }
19524 				  }
19525 			  if(colidx>=0) // Selected
19526 			  {
19527 				  // Init camera matrix from DNG
19528 				  FORCC for (int j = 0; j < 3; j++)
19529 					  cm[c][j] = tiff_ifd[sidx].dng_color[colidx].colormatrix[c][j];
19530 
19531 				  if(calidx[colidx] == sidx)
19532 				  {
19533 					  for (int i = 0; i < colors; i++)
19534 						  FORCC
19535 						  cc[i][c] = tiff_ifd[sidx].dng_color[colidx].calibration[i][c];
19536 				  }
19537 
19538 				  if(abidx == sidx)
19539 					for (int i = 0; i < colors; i++)
19540 						  FORCC cc[i][c] *= tiff_ifd[sidx].dng_levels.analogbalance[i];
19541 				  int j;
19542 				  FORCC for (int i = 0; i < 3; i++) for (cam_xyz[c][i] = j = 0; j < colors; j++) cam_xyz[c][i] +=
19543 					  cc[c][j] * cm[j][i];// add AsShotXY later * xyz[i];
19544 				  cam_xyz_coeff(cmatrix, cam_xyz);
19545 			  }
19546 		  }
19547 	  }
19548 
19549       if (imgdata.params.raw_processing_options & LIBRAW_PROCESSING_USE_DNG_DEFAULT_CROP)
19550       {
19551         sidx = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_CROPORIGIN);
19552         int sidx2 = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_CROPSIZE);
19553         if (sidx >= 0 && sidx == sidx2 && tiff_ifd[sidx].dng_levels.default_crop[2] > 0 &&
19554             tiff_ifd[sidx].dng_levels.default_crop[3] > 0)
19555         {
19556           int lm = tiff_ifd[sidx].dng_levels.default_crop[0];
19557           int lmm = CFAROUND(lm, filters);
19558           int tm = tiff_ifd[sidx].dng_levels.default_crop[1];
19559           int tmm = CFAROUND(tm, filters);
19560           int ww = tiff_ifd[sidx].dng_levels.default_crop[2];
19561           int hh = tiff_ifd[sidx].dng_levels.default_crop[3];
19562           if (lmm > lm)
19563             ww -= (lmm - lm);
19564           if (tmm > tm)
19565             hh -= (tmm - tm);
19566           if (left_margin + lm + ww <= raw_width && top_margin + tm + hh <= raw_height)
19567           {
19568             left_margin += lmm;
19569             top_margin += tmm;
19570             width = ww;
19571             height = hh;
19572           }
19573         }
19574       }
19575       if (!(imgdata.color.dng_color[0].parsedfields & LIBRAW_DNGFM_FORWARDMATRIX)) // Not set already (Leica makernotes)
19576       {
19577         sidx = IFDCOLORINDEX(iifd, 0, LIBRAW_DNGFM_FORWARDMATRIX);
19578         if (sidx >= 0)
19579           COPYARR(imgdata.color.dng_color[0].forwardmatrix, tiff_ifd[sidx].dng_color[0].forwardmatrix);
19580       }
19581       if (!(imgdata.color.dng_color[1].parsedfields & LIBRAW_DNGFM_FORWARDMATRIX)) // Not set already (Leica makernotes)
19582       {
19583         sidx = IFDCOLORINDEX(iifd, 1, LIBRAW_DNGFM_FORWARDMATRIX);
19584         if (sidx >= 0)
19585           COPYARR(imgdata.color.dng_color[1].forwardmatrix, tiff_ifd[sidx].dng_color[1].forwardmatrix);
19586       }
19587       for (int ss = 0; ss < 2; ss++)
19588       {
19589         sidx = IFDCOLORINDEX(iifd, ss, LIBRAW_DNGFM_COLORMATRIX);
19590         if (sidx >= 0)
19591           COPYARR(imgdata.color.dng_color[ss].colormatrix, tiff_ifd[sidx].dng_color[ss].colormatrix);
19592 
19593         sidx = IFDCOLORINDEX(iifd, ss, LIBRAW_DNGFM_CALIBRATION);
19594         if (sidx >= 0)
19595           COPYARR(imgdata.color.dng_color[ss].calibration, tiff_ifd[sidx].dng_color[ss].calibration);
19596 
19597         sidx = IFDCOLORINDEX(iifd, ss, LIBRAW_DNGFM_ILLUMINANT);
19598         if (sidx >= 0)
19599           imgdata.color.dng_color[ss].illuminant = tiff_ifd[sidx].dng_color[ss].illuminant;
19600       }
19601       // Levels
19602       sidx = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_ANALOGBALANCE);
19603       if (sidx >= 0)
19604         COPYARR(imgdata.color.dng_levels.analogbalance, tiff_ifd[sidx].dng_levels.analogbalance);
19605       sidx = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_WHITE);
19606       if (sidx >= 0)
19607         COPYARR(imgdata.color.dng_levels.dng_whitelevel, tiff_ifd[sidx].dng_levels.dng_whitelevel);
19608       sidx = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_BLACK);
19609       if (sidx >= 0)
19610       {
19611         imgdata.color.dng_levels.dng_black = tiff_ifd[sidx].dng_levels.dng_black;
19612         COPYARR(imgdata.color.dng_levels.dng_cblack, tiff_ifd[sidx].dng_levels.dng_cblack);
19613       }
19614       if (pifd >= 0)
19615       {
19616         sidx = IFDLEVELINDEX(pifd, LIBRAW_DNGFM_PREVIEWCS);
19617         if (sidx >= 0)
19618           imgdata.color.dng_levels.preview_colorspace = tiff_ifd[sidx].dng_levels.preview_colorspace;
19619       }
19620       sidx = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_OPCODE2);
19621       if (sidx >= 0)
19622         meta_offset = tiff_ifd[sidx].opcode2_offset;
19623 
19624       sidx = IFDLEVELINDEX(iifd, LIBRAW_DNGFM_LINTABLE);
19625       INT64 linoff = -1;
19626       int linlen = 0;
19627       if (sidx >= 0)
19628       {
19629         linoff = tiff_ifd[sidx].lineartable_offset;
19630         linlen = tiff_ifd[sidx].lineartable_len;
19631       }
19632 
19633       if (linoff >= 0 && linlen > 0)
19634       {
19635         INT64 pos = ftell(ifp);
19636         fseek(ifp, linoff, SEEK_SET);
19637         linear_table(linlen);
19638         fseek(ifp, pos, SEEK_SET);
19639       }
19640       // Need to add curve too
19641     }
19642     /* Copy DNG black level to LibRaw's */
19643     maximum = imgdata.color.dng_levels.dng_whitelevel[0];
19644     black = imgdata.color.dng_levels.dng_black;
19645     int ll = LIM(0, (sizeof(cblack) / sizeof(cblack[0])),
19646                  (sizeof(imgdata.color.dng_levels.dng_cblack) / sizeof(imgdata.color.dng_levels.dng_cblack[0])));
19647     for (int i = 0; i < ll; i++)
19648       cblack[i] = imgdata.color.dng_levels.dng_cblack[i];
19649   }
19650 #endif
19651   /* Early reject for damaged images */
19652   if (!load_raw || height < 22 || width < 22 ||
19653 #ifdef LIBRAW_LIBRARY_BUILD
19654       (tiff_bps > 16 && load_raw != &LibRaw::deflate_dng_load_raw)
19655 #else
19656       tiff_bps > 16
19657 #endif
19658       || tiff_samples > 4 || colors > 4 || colors < 1)
19659   {
19660     is_raw = 0;
19661 #ifdef LIBRAW_LIBRARY_BUILD
19662     RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY, 1, 2);
19663 #endif
19664     return;
19665   }
19666   {
19667    // Check cam_mul range
19668    int cmul_ok =1;
19669    FORCC if(cam_mul[c] <= 0.001f)  cmul_ok = 0;;
19670 
19671    if(cmul_ok)
19672    {
19673 	  double cmin = cam_mul[0],cmax;
19674 	  double cnorm[4];
19675 	  FORCC	  cmin = MIN(cmin,cam_mul[c]);
19676 	  FORCC	  cnorm[c] = cam_mul[c]/cmin;
19677 	  cmax = cmin = cnorm[0];
19678 	  FORCC
19679 	  {
19680 		  cmin = MIN(cmin,cnorm[c]);
19681 		  cmax = MIN(cmax,cnorm[c]);
19682 	  }
19683 	  if(cmin <= 0.01f || cmax > 100.f)
19684 		  cmul_ok = false;
19685    }
19686    if(!cmul_ok)
19687 	  cam_mul[0] = cam_mul[3] = 0;
19688 
19689   }
19690   if ((use_camera_matrix & ((use_camera_wb || dng_version) | 0x2)) && cmatrix[0][0] > 0.125)
19691   {
19692     memcpy(rgb_cam, cmatrix, sizeof cmatrix);
19693     raw_color = 0;
19694   }
19695 
19696   if (raw_color)
19697     adobe_coeff(make, model);
19698 #ifdef LIBRAW_LIBRARY_BUILD
19699   else if (imgdata.color.cam_xyz[0][0] < 0.01)
19700     adobe_coeff(make, model, 1);
19701 #endif
19702 
19703   if (load_raw == &CLASS kodak_radc_load_raw)
19704     if (raw_color)
19705       adobe_coeff("Apple", "Quicktake");
19706 
19707 #ifdef LIBRAW_LIBRARY_BUILD
19708   // Clear erorneus fuji_width if not set through parse_fuji or for DNG
19709   if (fuji_width && !dng_version && !(imgdata.process_warnings & LIBRAW_WARN_PARSEFUJI_PROCESSED))
19710     fuji_width = 0;
19711 #endif
19712   if (fuji_width)
19713   {
19714     fuji_width = width >> !fuji_layout;
19715     filters = fuji_width & 1 ? 0x94949494 : 0x49494949;
19716     width = (height >> fuji_layout) + fuji_width;
19717     height = width - 1;
19718     pixel_aspect = 1;
19719   }
19720   else
19721   {
19722     if (raw_height < height)
19723       raw_height = height;
19724     if (raw_width < width)
19725       raw_width = width;
19726   }
19727   if (!tiff_bps)
19728     tiff_bps = 12;
19729   if (!maximum)
19730   {
19731     maximum = (1 << tiff_bps) - 1;
19732     if (maximum < 0x10000 && curve[maximum] > 0 && load_raw == &CLASS sony_arw2_load_raw)
19733       maximum = curve[maximum];
19734   }
19735   if (!load_raw || height < 22 || width < 22 ||
19736 #ifdef LIBRAW_LIBRARY_BUILD
19737       (tiff_bps > 16 && load_raw != &LibRaw::deflate_dng_load_raw)
19738 #else
19739       tiff_bps > 16
19740 #endif
19741       || tiff_samples > 6 || colors > 4)
19742     is_raw = 0;
19743 
19744   if (raw_width < 22 || raw_width > 64000 || raw_height < 22 || raw_height > 64000)
19745     is_raw = 0;
19746 
19747 #ifdef NO_JASPER
19748   if (load_raw == &CLASS redcine_load_raw)
19749   {
19750 #ifdef DCRAW_VERBOSE
19751     fprintf(stderr, _("%s: You must link dcraw with %s!!\n"), ifname, "libjasper");
19752 #endif
19753     is_raw = 0;
19754 #ifdef LIBRAW_LIBRARY_BUILD
19755     imgdata.process_warnings |= LIBRAW_WARN_NO_JASPER;
19756 #endif
19757   }
19758 #endif
19759 #ifdef NO_JPEG
19760   if (load_raw == &CLASS kodak_jpeg_load_raw || load_raw == &CLASS lossy_dng_load_raw)
19761   {
19762 #ifdef DCRAW_VERBOSE
19763     fprintf(stderr, _("%s: You must link dcraw with %s!!\n"), ifname, "libjpeg");
19764 #endif
19765     is_raw = 0;
19766 #ifdef LIBRAW_LIBRARY_BUILD
19767     imgdata.process_warnings |= LIBRAW_WARN_NO_JPEGLIB;
19768 #endif
19769   }
19770 #endif
19771   if (!cdesc[0])
19772     strcpy(cdesc, colors == 3 ? "RGBG" : "GMCY");
19773   if (!raw_height)
19774     raw_height = height;
19775   if (!raw_width)
19776     raw_width = width;
19777   if (filters > 999 && colors == 3)
19778     filters |= ((filters >> 2 & 0x22222222) | (filters << 2 & 0x88888888)) & filters << 1;
19779 notraw:
19780   if (flip == UINT_MAX)
19781     flip = tiff_flip;
19782   if (flip == UINT_MAX)
19783     flip = 0;
19784 
19785   // Convert from degrees to bit-field if needed
19786   if (flip > 89 || flip < -89)
19787   {
19788     switch ((flip + 3600) % 360)
19789     {
19790     case 270:
19791       flip = 5;
19792       break;
19793     case 180:
19794       flip = 3;
19795       break;
19796     case 90:
19797       flip = 6;
19798       break;
19799     }
19800   }
19801 
19802 #ifdef LIBRAW_LIBRARY_BUILD
19803   RUN_CALLBACK(LIBRAW_PROGRESS_IDENTIFY, 1, 2);
19804 #endif
19805 }
19806 
19807 void CLASS convert_to_rgb()
19808 {
19809 #ifndef LIBRAW_LIBRARY_BUILD
19810   int row, col, c;
19811 #endif
19812   int i, j, k;
19813 #ifndef LIBRAW_LIBRARY_BUILD
19814   ushort *img;
19815   float out[3];
19816 #endif
19817   float out_cam[3][4];
19818   double num, inverse[3][3];
19819   static const double xyzd50_srgb[3][3] = {
19820       {0.436083, 0.385083, 0.143055}, {0.222507, 0.716888, 0.060608}, {0.013930, 0.097097, 0.714022}};
19821   static const double rgb_rgb[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
19822   static const double adobe_rgb[3][3] = {
19823       {0.715146, 0.284856, 0.000000}, {0.000000, 1.000000, 0.000000}, {0.000000, 0.041166, 0.958839}};
19824   static const double wide_rgb[3][3] = {
19825       {0.593087, 0.404710, 0.002206}, {0.095413, 0.843149, 0.061439}, {0.011621, 0.069091, 0.919288}};
19826   static const double prophoto_rgb[3][3] = {
19827       {0.529317, 0.330092, 0.140588}, {0.098368, 0.873465, 0.028169}, {0.016879, 0.117663, 0.865457}};
19828   static const double aces_rgb[3][3] = {
19829       {0.432996, 0.375380, 0.189317}, {0.089427, 0.816523, 0.102989}, {0.019165, 0.118150, 0.941914}};
19830   static const double(*out_rgb[])[3] = {rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb, aces_rgb};
19831   static const char *name[] = {"sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ", "ACES"};
19832   static const unsigned phead[] = {1024, 0, 0x2100000,  0x6d6e7472, 0x52474220, 0x58595a20, 0,
19833                                    0,    0, 0x61637370, 0,          0,          0x6e6f6e65, 0,
19834                                    0,    0, 0,          0xf6d6,     0x10000,    0xd32d};
19835   unsigned pbody[] = {10,         0x63707274, 0,  36, /* cprt */
19836                       0x64657363, 0,          40,     /* desc */
19837                       0x77747074, 0,          20,     /* wtpt */
19838                       0x626b7074, 0,          20,     /* bkpt */
19839                       0x72545243, 0,          14,     /* rTRC */
19840                       0x67545243, 0,          14,     /* gTRC */
19841                       0x62545243, 0,          14,     /* bTRC */
19842                       0x7258595a, 0,          20,     /* rXYZ */
19843                       0x6758595a, 0,          20,     /* gXYZ */
19844                       0x6258595a, 0,          20};    /* bXYZ */
19845   static const unsigned pwhite[] = {0xf351, 0x10000, 0x116cc};
19846   unsigned pcurve[] = {0x63757276, 0, 1, 0x1000000};
19847 
19848 #ifdef LIBRAW_LIBRARY_BUILD
19849   RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB, 0, 2);
19850 #endif
19851   gamma_curve(gamm[0], gamm[1], 0, 0);
19852   memcpy(out_cam, rgb_cam, sizeof out_cam);
19853 #ifndef LIBRAW_LIBRARY_BUILD
19854   raw_color |= colors == 1 || document_mode || output_color < 1 || output_color > 6;
19855 #else
19856   raw_color |= colors == 1 || output_color < 1 || output_color > 6;
19857 #endif
19858   if (!raw_color)
19859   {
19860     oprof = (unsigned *)calloc(phead[0], 1);
19861     merror(oprof, "convert_to_rgb()");
19862     memcpy(oprof, phead, sizeof phead);
19863     if (output_color == 5)
19864       oprof[4] = oprof[5];
19865     oprof[0] = 132 + 12 * pbody[0];
19866     for (i = 0; i < pbody[0]; i++)
19867     {
19868       oprof[oprof[0] / 4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874;
19869       pbody[i * 3 + 2] = oprof[0];
19870       oprof[0] += (pbody[i * 3 + 3] + 3) & -4;
19871     }
19872     memcpy(oprof + 32, pbody, sizeof pbody);
19873     oprof[pbody[5] / 4 + 2] = strlen(name[output_color - 1]) + 1;
19874     memcpy((char *)oprof + pbody[8] + 8, pwhite, sizeof pwhite);
19875     pcurve[3] = (short)(256 / gamm[5] + 0.5) << 16;
19876     for (i = 4; i < 7; i++)
19877       memcpy((char *)oprof + pbody[i * 3 + 2], pcurve, sizeof pcurve);
19878     pseudoinverse((double(*)[3])out_rgb[output_color - 1], inverse, 3);
19879     for (i = 0; i < 3; i++)
19880       for (j = 0; j < 3; j++)
19881       {
19882         for (num = k = 0; k < 3; k++)
19883           num += xyzd50_srgb[i][k] * inverse[j][k];
19884         oprof[pbody[j * 3 + 23] / 4 + i + 2] = num * 0x10000 + 0.5;
19885       }
19886     for (i = 0; i < phead[0] / 4; i++)
19887       oprof[i] = htonl(oprof[i]);
19888     strcpy((char *)oprof + pbody[2] + 8, "auto-generated by dcraw");
19889     strcpy((char *)oprof + pbody[5] + 12, name[output_color - 1]);
19890     for (i = 0; i < 3; i++)
19891       for (j = 0; j < colors; j++)
19892         for (out_cam[i][j] = k = 0; k < 3; k++)
19893           out_cam[i][j] += out_rgb[output_color - 1][i][k] * rgb_cam[k][j];
19894   }
19895 #ifdef DCRAW_VERBOSE
19896   if (verbose)
19897     fprintf(stderr, raw_color ? _("Building histograms...\n") : _("Converting to %s colorspace...\n"),
19898             name[output_color - 1]);
19899 #endif
19900 #ifdef LIBRAW_LIBRARY_BUILD
19901   convert_to_rgb_loop(out_cam);
19902 #else
19903   memset(histogram, 0, sizeof histogram);
19904   for (img = image[0], row = 0; row < height; row++)
19905     for (col = 0; col < width; col++, img += 4)
19906     {
19907       if (!raw_color)
19908       {
19909         out[0] = out[1] = out[2] = 0;
19910         FORCC
19911         {
19912           out[0] += out_cam[0][c] * img[c];
19913           out[1] += out_cam[1][c] * img[c];
19914           out[2] += out_cam[2][c] * img[c];
19915         }
19916         FORC3 img[c] = CLIP((int)out[c]);
19917       }
19918       else if (document_mode)
19919         img[0] = img[fcol(row, col)];
19920       FORCC histogram[c][img[c] >> 3]++;
19921     }
19922 #endif
19923   if (colors == 4 && output_color)
19924     colors = 3;
19925 #ifndef LIBRAW_LIBRARY_BUILD
19926   if (document_mode && filters)
19927     colors = 1;
19928 #endif
19929 #ifdef LIBRAW_LIBRARY_BUILD
19930   RUN_CALLBACK(LIBRAW_PROGRESS_CONVERT_RGB, 1, 2);
19931 #endif
19932 }
19933 
19934 void CLASS fuji_rotate()
19935 {
19936   int i, row, col;
19937   double step;
19938   float r, c, fr, fc;
19939   unsigned ur, uc;
19940   ushort wide, high, (*img)[4], (*pix)[4];
19941 
19942   if (!fuji_width)
19943     return;
19944 #ifdef DCRAW_VERBOSE
19945   if (verbose)
19946     fprintf(stderr, _("Rotating image 45 degrees...\n"));
19947 #endif
19948   fuji_width = (fuji_width - 1 + shrink) >> shrink;
19949   step = sqrt(0.5);
19950   wide = fuji_width / step;
19951   high = (height - fuji_width) / step;
19952   img = (ushort(*)[4])calloc(high, wide * sizeof *img);
19953   merror(img, "fuji_rotate()");
19954 
19955 #ifdef LIBRAW_LIBRARY_BUILD
19956   RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE, 0, 2);
19957 #endif
19958 
19959   for (row = 0; row < high; row++)
19960     for (col = 0; col < wide; col++)
19961     {
19962       ur = r = fuji_width + (row - col) * step;
19963       uc = c = (row + col) * step;
19964       if (ur > height - 2 || uc > width - 2)
19965         continue;
19966       fr = r - ur;
19967       fc = c - uc;
19968       pix = image + ur * width + uc;
19969       for (i = 0; i < colors; i++)
19970         img[row * wide + col][i] = (pix[0][i] * (1 - fc) + pix[1][i] * fc) * (1 - fr) +
19971                                    (pix[width][i] * (1 - fc) + pix[width + 1][i] * fc) * fr;
19972     }
19973 
19974   free(image);
19975   width = wide;
19976   height = high;
19977   image = img;
19978   fuji_width = 0;
19979 #ifdef LIBRAW_LIBRARY_BUILD
19980   RUN_CALLBACK(LIBRAW_PROGRESS_FUJI_ROTATE, 1, 2);
19981 #endif
19982 }
19983 
19984 void CLASS stretch()
19985 {
19986   ushort newdim, (*img)[4], *pix0, *pix1;
19987   int row, col, c;
19988   double rc, frac;
19989 
19990   if (pixel_aspect == 1)
19991     return;
19992 #ifdef LIBRAW_LIBRARY_BUILD
19993   RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH, 0, 2);
19994 #endif
19995 #ifdef DCRAW_VERBOSE
19996   if (verbose)
19997     fprintf(stderr, _("Stretching the image...\n"));
19998 #endif
19999   if (pixel_aspect < 1)
20000   {
20001     newdim = height / pixel_aspect + 0.5;
20002     img = (ushort(*)[4])calloc(width, newdim * sizeof *img);
20003     merror(img, "stretch()");
20004     for (rc = row = 0; row < newdim; row++, rc += pixel_aspect)
20005     {
20006       frac = rc - (c = rc);
20007       pix0 = pix1 = image[c * width];
20008       if (c + 1 < height)
20009         pix1 += width * 4;
20010       for (col = 0; col < width; col++, pix0 += 4, pix1 += 4)
20011         FORCC img[row * width + col][c] = pix0[c] * (1 - frac) + pix1[c] * frac + 0.5;
20012     }
20013     height = newdim;
20014   }
20015   else
20016   {
20017     newdim = width * pixel_aspect + 0.5;
20018     img = (ushort(*)[4])calloc(height, newdim * sizeof *img);
20019     merror(img, "stretch()");
20020     for (rc = col = 0; col < newdim; col++, rc += 1 / pixel_aspect)
20021     {
20022       frac = rc - (c = rc);
20023       pix0 = pix1 = image[c];
20024       if (c + 1 < width)
20025         pix1 += 4;
20026       for (row = 0; row < height; row++, pix0 += width * 4, pix1 += width * 4)
20027         FORCC img[row * newdim + col][c] = pix0[c] * (1 - frac) + pix1[c] * frac + 0.5;
20028     }
20029     width = newdim;
20030   }
20031   free(image);
20032   image = img;
20033 #ifdef LIBRAW_LIBRARY_BUILD
20034   RUN_CALLBACK(LIBRAW_PROGRESS_STRETCH, 1, 2);
20035 #endif
20036 }
20037 
20038 int CLASS flip_index(int row, int col)
20039 {
20040   if (flip & 4)
20041     SWAP(row, col);
20042   if (flip & 2)
20043     row = iheight - 1 - row;
20044   if (flip & 1)
20045     col = iwidth - 1 - col;
20046   return row * iwidth + col;
20047 }
20048 
20049 void CLASS tiff_set(struct tiff_hdr *th, ushort *ntag, ushort tag, ushort type, int count, int val)
20050 {
20051   struct libraw_tiff_tag *tt;
20052   int c;
20053 
20054   tt = (struct libraw_tiff_tag *)(ntag + 1) + (*ntag)++;
20055   tt->val.i = val;
20056   if (type == 1 && count <= 4)
20057     FORC(4) tt->val.c[c] = val >> (c << 3);
20058   else if (type == 2)
20059   {
20060     count = strnlen((char *)th + val, count - 1) + 1;
20061     if (count <= 4)
20062       FORC(4) tt->val.c[c] = ((char *)th)[val + c];
20063   }
20064   else if (type == 3 && count <= 2)
20065     FORC(2) tt->val.s[c] = val >> (c << 4);
20066   tt->count = count;
20067   tt->type = type;
20068   tt->tag = tag;
20069 }
20070 
20071 #define TOFF(ptr) ((char *)(&(ptr)) - (char *)th)
20072 
20073 void CLASS tiff_head(struct tiff_hdr *th, int full)
20074 {
20075   int c, psize = 0;
20076   struct tm *t;
20077 
20078   memset(th, 0, sizeof *th);
20079   th->t_order = htonl(0x4d4d4949) >> 16;
20080   th->magic = 42;
20081   th->ifd = 10;
20082   th->rat[0] = th->rat[2] = 300;
20083   th->rat[1] = th->rat[3] = 1;
20084   FORC(6) th->rat[4 + c] = 1000000;
20085   th->rat[4] *= shutter;
20086   th->rat[6] *= aperture;
20087   th->rat[8] *= focal_len;
20088   strncpy(th->t_desc, desc, 512);
20089   strncpy(th->t_make, make, 64);
20090   strncpy(th->t_model, model, 64);
20091   strcpy(th->soft, "dcraw v" DCRAW_VERSION);
20092   t = localtime(&timestamp);
20093   sprintf(th->date, "%04d:%02d:%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
20094           t->tm_min, t->tm_sec);
20095   strncpy(th->t_artist, artist, 64);
20096   if (full)
20097   {
20098     tiff_set(th, &th->ntag, 254, 4, 1, 0);
20099     tiff_set(th, &th->ntag, 256, 4, 1, width);
20100     tiff_set(th, &th->ntag, 257, 4, 1, height);
20101     tiff_set(th, &th->ntag, 258, 3, colors, output_bps);
20102     if (colors > 2)
20103       th->tag[th->ntag - 1].val.i = TOFF(th->bps);
20104     FORC4 th->bps[c] = output_bps;
20105     tiff_set(th, &th->ntag, 259, 3, 1, 1);
20106     tiff_set(th, &th->ntag, 262, 3, 1, 1 + (colors > 1));
20107   }
20108   tiff_set(th, &th->ntag, 270, 2, 512, TOFF(th->t_desc));
20109   tiff_set(th, &th->ntag, 271, 2, 64, TOFF(th->t_make));
20110   tiff_set(th, &th->ntag, 272, 2, 64, TOFF(th->t_model));
20111   if (full)
20112   {
20113     if (oprof)
20114       psize = ntohl(oprof[0]);
20115     tiff_set(th, &th->ntag, 273, 4, 1, sizeof *th + psize);
20116     tiff_set(th, &th->ntag, 277, 3, 1, colors);
20117     tiff_set(th, &th->ntag, 278, 4, 1, height);
20118     tiff_set(th, &th->ntag, 279, 4, 1, height * width * colors * output_bps / 8);
20119   }
20120   else
20121     tiff_set(th, &th->ntag, 274, 3, 1, "12435867"[flip] - '0');
20122   tiff_set(th, &th->ntag, 282, 5, 1, TOFF(th->rat[0]));
20123   tiff_set(th, &th->ntag, 283, 5, 1, TOFF(th->rat[2]));
20124   tiff_set(th, &th->ntag, 284, 3, 1, 1);
20125   tiff_set(th, &th->ntag, 296, 3, 1, 2);
20126   tiff_set(th, &th->ntag, 305, 2, 32, TOFF(th->soft));
20127   tiff_set(th, &th->ntag, 306, 2, 20, TOFF(th->date));
20128   tiff_set(th, &th->ntag, 315, 2, 64, TOFF(th->t_artist));
20129   tiff_set(th, &th->ntag, 34665, 4, 1, TOFF(th->nexif));
20130   if (psize)
20131     tiff_set(th, &th->ntag, 34675, 7, psize, sizeof *th);
20132   tiff_set(th, &th->nexif, 33434, 5, 1, TOFF(th->rat[4]));
20133   tiff_set(th, &th->nexif, 33437, 5, 1, TOFF(th->rat[6]));
20134   tiff_set(th, &th->nexif, 34855, 3, 1, iso_speed);
20135   tiff_set(th, &th->nexif, 37386, 5, 1, TOFF(th->rat[8]));
20136   if (gpsdata[1])
20137   {
20138     tiff_set(th, &th->ntag, 34853, 4, 1, TOFF(th->ngps));
20139     tiff_set(th, &th->ngps, 0, 1, 4, 0x202);
20140     tiff_set(th, &th->ngps, 1, 2, 2, gpsdata[29]);
20141     tiff_set(th, &th->ngps, 2, 5, 3, TOFF(th->gps[0]));
20142     tiff_set(th, &th->ngps, 3, 2, 2, gpsdata[30]);
20143     tiff_set(th, &th->ngps, 4, 5, 3, TOFF(th->gps[6]));
20144     tiff_set(th, &th->ngps, 5, 1, 1, gpsdata[31]);
20145     tiff_set(th, &th->ngps, 6, 5, 1, TOFF(th->gps[18]));
20146     tiff_set(th, &th->ngps, 7, 5, 3, TOFF(th->gps[12]));
20147     tiff_set(th, &th->ngps, 18, 2, 12, TOFF(th->gps[20]));
20148     tiff_set(th, &th->ngps, 29, 2, 12, TOFF(th->gps[23]));
20149     memcpy(th->gps, gpsdata, sizeof th->gps);
20150   }
20151 }
20152 
20153 #ifdef LIBRAW_LIBRARY_BUILD
20154 void CLASS jpeg_thumb_writer(FILE *tfp, char *t_humb, int t_humb_length)
20155 {
20156   ushort exif[5];
20157   struct tiff_hdr th;
20158   fputc(0xff, tfp);
20159   fputc(0xd8, tfp);
20160   if (strcmp(t_humb + 6, "Exif"))
20161   {
20162     memcpy(exif, "\xff\xe1  Exif\0\0", 10);
20163     exif[1] = htons(8 + sizeof th);
20164     fwrite(exif, 1, sizeof exif, tfp);
20165     tiff_head(&th, 0);
20166     fwrite(&th, 1, sizeof th, tfp);
20167   }
20168   fwrite(t_humb + 2, 1, t_humb_length - 2, tfp);
20169 }
20170 
20171 void CLASS jpeg_thumb()
20172 {
20173   char *thumb;
20174 
20175   thumb = (char *)malloc(thumb_length);
20176   merror(thumb, "jpeg_thumb()");
20177   fread(thumb, 1, thumb_length, ifp);
20178   jpeg_thumb_writer(ofp, thumb, thumb_length);
20179   free(thumb);
20180 }
20181 #else
20182 void CLASS jpeg_thumb()
20183 {
20184   char *thumb;
20185   ushort exif[5];
20186   struct tiff_hdr th;
20187 
20188   thumb = (char *)malloc(thumb_length);
20189   merror(thumb, "jpeg_thumb()");
20190   fread(thumb, 1, thumb_length, ifp);
20191   fputc(0xff, ofp);
20192   fputc(0xd8, ofp);
20193   if (strcmp(thumb + 6, "Exif"))
20194   {
20195     memcpy(exif, "\xff\xe1  Exif\0\0", 10);
20196     exif[1] = htons(8 + sizeof th);
20197     fwrite(exif, 1, sizeof exif, ofp);
20198     tiff_head(&th, 0);
20199     fwrite(&th, 1, sizeof th, ofp);
20200   }
20201   fwrite(thumb + 2, 1, thumb_length - 2, ofp);
20202   free(thumb);
20203 }
20204 #endif
20205 
20206 void CLASS write_ppm_tiff()
20207 {
20208   struct tiff_hdr th;
20209   uchar *ppm;
20210   ushort *ppm2;
20211   int c, row, col, soff, rstep, cstep;
20212   int perc, val, total, t_white = 0x2000;
20213 
20214 #ifdef LIBRAW_LIBRARY_BUILD
20215   perc = width * height * auto_bright_thr;
20216 #else
20217   perc = width * height * 0.01; /* 99th percentile white level */
20218 #endif
20219   if (fuji_width)
20220     perc /= 2;
20221   if (!((highlight & ~2) || no_auto_bright))
20222     for (t_white = c = 0; c < colors; c++)
20223     {
20224       for (val = 0x2000, total = 0; --val > 32;)
20225         if ((total += histogram[c][val]) > perc)
20226           break;
20227       if (t_white < val)
20228         t_white = val;
20229     }
20230   gamma_curve(gamm[0], gamm[1], 2, (t_white << 3) / bright);
20231   iheight = height;
20232   iwidth = width;
20233   if (flip & 4)
20234     SWAP(height, width);
20235   ppm = (uchar *)calloc(width, colors * output_bps / 8);
20236   ppm2 = (ushort *)ppm;
20237   merror(ppm, "write_ppm_tiff()");
20238   if (output_tiff)
20239   {
20240     tiff_head(&th, 1);
20241     fwrite(&th, sizeof th, 1, ofp);
20242     if (oprof)
20243       fwrite(oprof, ntohl(oprof[0]), 1, ofp);
20244   }
20245   else if (colors > 3)
20246     fprintf(ofp, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n", width, height, colors,
20247             (1 << output_bps) - 1, cdesc);
20248   else
20249     fprintf(ofp, "P%d\n%d %d\n%d\n", colors / 2 + 5, width, height, (1 << output_bps) - 1);
20250   soff = flip_index(0, 0);
20251   cstep = flip_index(0, 1) - soff;
20252   rstep = flip_index(1, 0) - flip_index(0, width);
20253   for (row = 0; row < height; row++, soff += rstep)
20254   {
20255     for (col = 0; col < width; col++, soff += cstep)
20256       if (output_bps == 8)
20257         FORCC ppm[col * colors + c] = curve[image[soff][c]] >> 8;
20258       else
20259         FORCC ppm2[col * colors + c] = curve[image[soff][c]];
20260     if (output_bps == 16 && !output_tiff && htons(0x55aa) != 0x55aa)
20261       swab((char *)ppm2, (char *)ppm2, width * colors * 2);
20262     fwrite(ppm, colors * output_bps / 8, width, ofp);
20263   }
20264   free(ppm);
20265 }
20266