File indexing completed on 2025-01-05 03:57:05

0001 /* -*- C++ -*-
0002  * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
0003  *
0004  LibRaw uses code from dcraw.c -- Dave Coffin's raw photo decoder,
0005  dcraw.c is copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net.
0006  LibRaw do not use RESTRICTED code from dcraw.c
0007 
0008  LibRaw is free software; you can redistribute it and/or modify
0009  it under the terms of the one of two licenses as you choose:
0010 
0011 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
0012    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
0013 
0014 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
0015    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
0016 
0017  */
0018 
0019 #include "../../internal/dcraw_defs.h"
0020 #include "../../internal/libraw_cameraids.h"
0021 
0022 int LibRaw::parse_tiff_ifd(int base)
0023 {
0024   unsigned entries, tag, type, len, plen = 16, save, utmp;
0025   int ifd, use_cm = 0, cfa, i, j, c, ima_len = 0;
0026   char *cbuf, *cp;
0027   uchar cfa_pat[16], cfa_pc[] = {0, 1, 2, 3}, tab[256];
0028   double fm[3][4], cc[4][4], cm[4][3], cam_xyz[4][3], num;
0029   double ab[] = {1, 1, 1, 1}, asn[] = {0, 0, 0, 0}, xyz[] = {1, 1, 1};
0030   unsigned sony_curve[] = {0, 0, 0, 0, 0, 4095};
0031   unsigned *buf, sony_offset = 0, sony_length = 0, sony_key = 0;
0032   struct jhead jh;
0033 
0034   ushort *rafdata;
0035 
0036   if (tiff_nifds >= sizeof tiff_ifd / sizeof tiff_ifd[0])
0037     return 1;
0038   ifd = tiff_nifds++;
0039   for (j = 0; j < 4; j++)
0040     for (i = 0; i < 4; i++)
0041       cc[j][i] = i == j;
0042 
0043   if (libraw_internal_data.unpacker_data.ifd0_offset == -1LL)
0044     libraw_internal_data.unpacker_data.ifd0_offset = base;
0045 
0046   entries = get2();
0047   if (entries > 512)
0048     return 1;
0049 
0050   INT64 fsize = ifp->size();
0051 
0052   while (entries--)
0053   {
0054     tiff_get(base, &tag, &type, &len, &save);
0055     INT64 savepos = ftell(ifp);
0056     if (len > 8 && savepos + len > 2 * fsize)
0057     {
0058       fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
0059       continue;
0060     }
0061     if (callbacks.exif_cb)
0062     {
0063       callbacks.exif_cb(callbacks.exifparser_data,
0064                         tag | (is_pana_raw ? 0x30000 : ((ifd + 1) << 20)), type,
0065                         len, order, ifp, base);
0066       fseek(ifp, savepos, SEEK_SET);
0067     }
0068 
0069     if (!is_pana_raw)
0070     { /* processing of EXIF tags that collide w/ PanasonicRaw tags */
0071       switch (tag)
0072       {
0073       case 0x0001:
0074         if (len == 4)
0075           is_pana_raw = get4();
0076         break;
0077       case 0x000b: /* 11, Std. EXIF Software tag */
0078         fgets(software, 64, ifp);
0079         if (!strncmp(software, "Adobe", 5) || !strncmp(software, "dcraw", 5) ||
0080             !strncmp(software, "UFRaw", 5) || !strncmp(software, "Bibble", 6) ||
0081             !strcmp(software, "Digital Photo Professional"))
0082           is_raw = 0;
0083         break;
0084       case 0x001c: /*  28, safeguard, probably not needed */
0085       case 0x001d: /*  29, safeguard, probably not needed */
0086       case 0x001e: /*  30, safeguard, probably not needed */
0087         cblack[tag - 0x001c] = get2();
0088         cblack[3] = cblack[1];
0089         break;
0090 
0091       case 0x0111: /* 273, StripOffset */
0092         if (len > 1 && len < 16384)
0093         {
0094           off_t sav = ftell(ifp);
0095           tiff_ifd[ifd].strip_offsets = (int *)calloc(len, sizeof(int));
0096           tiff_ifd[ifd].strip_offsets_count = len;
0097           for (int ii = 0; ii < (int)len; ii++)
0098             tiff_ifd[ifd].strip_offsets[ii] = get4() + base;
0099           fseek(ifp, sav, SEEK_SET); // restore position
0100         }
0101         /* fallback */
0102       case 0x0201: /* 513, JpegIFOffset */
0103       case 0xf007: // 61447
0104         tiff_ifd[ifd].offset = get4() + base;
0105         if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0)
0106         {
0107           fseek(ifp, tiff_ifd[ifd].offset, SEEK_SET);
0108           if (ljpeg_start(&jh, 1))
0109           {
0110             if (!dng_version && !strcasecmp(make, "SONY") && tiff_ifd[ifd].phint == 32803 &&
0111                 tiff_ifd[ifd].comp == 7) // Sony/lossless compressed IFD
0112             {
0113               tiff_ifd[ifd].comp = 6;
0114               tiff_ifd[ifd].bps = jh.bits;
0115               tiff_ifd[ifd].samples = 1;
0116             }
0117             else
0118             {
0119               tiff_ifd[ifd].comp = 6;
0120               tiff_ifd[ifd].bps = jh.bits;
0121               tiff_ifd[ifd].t_width = jh.wide;
0122               tiff_ifd[ifd].t_height = jh.high;
0123               tiff_ifd[ifd].samples = jh.clrs;
0124               if (!(jh.sraw || (jh.clrs & 1)))
0125                 tiff_ifd[ifd].t_width *= jh.clrs;
0126               if ((tiff_ifd[ifd].t_width > 4 * tiff_ifd[ifd].t_height) & ~jh.clrs)
0127               {
0128                 tiff_ifd[ifd].t_width /= 2;
0129                 tiff_ifd[ifd].t_height *= 2;
0130               }
0131               i = order;
0132               parse_tiff(tiff_ifd[ifd].offset + 12);
0133               order = i;
0134             }
0135           }
0136         }
0137         break;
0138       }
0139     }
0140     else
0141     { /* processing Panasonic-specific "PanasonicRaw" tags */
0142       switch (tag)
0143       {
0144       case 0x0004: /*   4, SensorTopBorder */
0145         imgdata.sizes.raw_inset_crops[0].ctop = get2();
0146         break;
0147       case 0x000a: /*  10, BitsPerSample */
0148         pana_bpp = get2();
0149         pana_bpp = LIM(pana_bpp, 8, 16);
0150         break;
0151       case 0x000b: /*  11, Compression */
0152         imPana.Compression = get2();
0153         break;
0154       case 0x000e: /*  14, LinearityLimitRed */
0155       case 0x000f: /*  15, LinearityLimitGreen */
0156       case 0x0010: /*  16, LinearityLimitBlue */
0157         imgdata.color.linear_max[tag - 14] = get2();
0158         if (imgdata.color.linear_max[tag - 14] == 16383)
0159             imgdata.color.linear_max[tag - 14] -= 64;
0160         if (imgdata.color.linear_max[tag - 14] == 4095)
0161           imgdata.color.linear_max[tag - 14] -= 16;
0162         if (tag == 0x000f) // 15, LinearityLimitGreen
0163           imgdata.color.linear_max[3] = imgdata.color.linear_max[1];
0164         break;
0165       case 0x0013: /*  19, WBInfo */
0166         if ((i = get2()) > 0x100)
0167           break;
0168         for (c = 0; c < i; c++)
0169         {
0170           if ((j = get2()) < 0x100)
0171           {
0172             icWBC[j][0] = get2();
0173             icWBC[j][2] = get2();
0174             icWBC[j][1] = icWBC[j][3] =
0175                 0x100;
0176           }
0177           else // light source out of EXIF numbers range
0178             get4();
0179         }
0180         break;
0181       case 0x0018: /* 24, HighISOMultiplierRed */
0182       case 0x0019: /* 25, HighISOMultiplierGreen */
0183       case 0x001a: /* 26, HighISOMultiplierBlue */
0184         imPana.HighISOMultiplier[tag - 0x0018] = get2();
0185         break;
0186       case 0x001c: /*  28, BlackLevelRed */
0187       case 0x001d: /*  29, BlackLevelGreen */
0188       case 0x001e: /*  30, BlackLevelBlue */
0189         pana_black[tag - 0x001c] = get2();
0190         break;
0191       case 0x002d: /*  45, RawFormat */
0192                    /* pana_encoding: tag 0x002d (45dec)
0193                         not used - DMC-LX1/FZ30/FZ50/L1/LX1/LX2
0194                         2 - RAW DMC-FZ8/FZ18
0195                         3 - RAW DMC-L10
0196                         4 - RW2 for most other models, including G9 in "pixel shift off"
0197                       mode and YUNEEC CGO4            (must add 15 to black levels for
0198                       RawFormat == 4)            5 - RW2 DC-GH5s; G9 in "pixel shift on"
0199                       mode            6 - RW2            DC-S1, DC-S1R in "pixel shift off"
0200                       mode            7 -            RW2 DC-S1R (probably            DC-S1 too) in
0201                       "pixel shift on" mode
0202                    */
0203         pana_encoding = get2();
0204         break;
0205       case 0x002f: /*  47, CropTop */
0206         imgdata.sizes.raw_inset_crops[0].ctop = get2();
0207         break;
0208       case 0x0030: /*  48, CropLeft */
0209         imgdata.sizes.raw_inset_crops[0].cleft = get2();
0210         break;
0211       case 0x0031: /*  49, CropBottom */
0212         imgdata.sizes.raw_inset_crops[0].cheight =
0213             get2() - imgdata.sizes.raw_inset_crops[0].ctop;
0214         break;
0215       case 0x0032: /*  50, CropRight */
0216         imgdata.sizes.raw_inset_crops[0].cwidth =
0217             get2() - imgdata.sizes.raw_inset_crops[0].cleft;
0218         break;
0219       case 0x0037: /*  55, ISO if  ISO in 0x8827 & ISO in 0x0017 == 65535 */
0220         if (iso_speed == 65535)
0221           iso_speed = get4();
0222         break;
0223       case 0x011c: /* 284, Gamma */
0224       {
0225         int n = get2();
0226         if (n >= 1024)
0227           imPana.gamma = (float)n / 1024.0f;
0228         else if (n >= 256)
0229           imPana.gamma = (float)n / 256.0f;
0230         else
0231           imPana.gamma = (float)n / 100.0f;
0232       }
0233       break;
0234       case 0x0120: /* 288, CameraIFD, contains tags 0x1xxx, 0x2xxx, 0x3xxx */
0235       {
0236         unsigned sorder = order;
0237         unsigned long sbase = base;
0238         base = ftell(ifp);
0239         order = get2();
0240         fseek(ifp, 2, SEEK_CUR);
0241         fseek(ifp, INT64(get4()) - 8LL, SEEK_CUR);
0242         parse_tiff_ifd(base);
0243         base = sbase;
0244         order = sorder;
0245       }
0246       break;
0247       case 0x0121: /* 289, Multishot, 0 is Off, 65536 is Pixel Shift */
0248         imPana.Multishot = get4();
0249         break;
0250       case 0x1001:
0251         if (imPana.Multishot == 0) {
0252           imPana.Multishot = get4();
0253           if (imPana.Multishot)
0254             imPana.Multishot += 65535;
0255         }
0256         break;
0257       case 0x1100:
0258         imPana.FocusStepNear = get2();
0259         break;
0260       case 0x1101:
0261         imPana.FocusStepCount = get2();
0262         break;
0263       case 0x1105:
0264         imPana.ZoomPosition = get4();
0265         break;
0266       case 0x1201:
0267         if (tagtypeIs(LIBRAW_EXIFTAG_TYPE_SHORT)) {
0268           imPana.LensManufacturer = fgetc(ifp);
0269         } else if (type == 258) {
0270           imPana.LensManufacturer = get4();
0271           if (imPana.LensManufacturer >= 257) {
0272             ilm.LensMount = LIBRAW_MOUNT_LPS_L;
0273             ilm.LensFormat = LIBRAW_FORMAT_FF;
0274           }
0275         }
0276         break;
0277       case 0x1202:
0278         if (ilm.LensMount == LIBRAW_MOUNT_LPS_L) {
0279           if ((utmp = get2())) ilm.LensID = utmp;
0280         } else if ((imPana.LensManufacturer != 0xff) &&
0281                    (imPana.LensManufacturer != 0xffffffff)) {
0282           if ((utmp = (fgetc(ifp) << 8) | fgetc(ifp)))
0283             ilm.LensID = (imPana.LensManufacturer << 16) + utmp;
0284         }
0285         break;
0286       case 0x1203: /* 4611, FocalLengthIn35mmFormat, contained in 0x0120
0287                       CameraIFD */
0288         if (imgdata.lens.FocalLengthIn35mmFormat < 0.65f)
0289           imgdata.lens.FocalLengthIn35mmFormat = get2();
0290         break;
0291       case 0x2009: /* 8201, contained in 0x0120 CameraIFD */
0292         if ((pana_encoding == 4) || (pana_encoding == 5))
0293         {
0294           i = MIN(8, len);
0295           int permut[8] = {3, 2, 1, 0, 3 + 4, 2 + 4, 1 + 4, 0 + 4};
0296           imPana.BlackLevelDim = len;
0297           for (j = 0; j < i; j++)
0298           {
0299             imPana.BlackLevel[permut[j]] =
0300                 (float)(get2()) / (float)(powf(2.f, 14.f - pana_bpp));
0301           }
0302         }
0303         break;
0304       case 0x3420: /* 13344, WB_RedLevelAuto, contained in 0x0120 CameraIFD */
0305         icWBC[LIBRAW_WBI_Auto][0] = get2();
0306         icWBC[LIBRAW_WBI_Auto][1] = icWBC[LIBRAW_WBI_Auto][3] = 1024.0f;
0307         break;
0308       case 0x3421: /* 13345, WB_BlueLevelAuto, contained in 0x0120 CameraIFD */
0309         icWBC[LIBRAW_WBI_Auto][2] = get2();
0310         break;
0311       case 0x0002: /*   2, ImageWidth */
0312         tiff_ifd[ifd].t_width = getint(type);
0313         break;
0314       case 0x0003: /*   3, ImageHeight */
0315         tiff_ifd[ifd].t_height = getint(type);
0316         break;
0317       case 0x0005: /*   5, SensorLeftBorder */
0318         width = get2();
0319         imgdata.sizes.raw_inset_crops[0].cleft = width;
0320         break;
0321       case 0x0006: /*   6, SensorBottomBorder */
0322         height = get2();
0323         imgdata.sizes.raw_inset_crops[0].cheight =
0324             height - imgdata.sizes.raw_inset_crops[0].ctop;
0325         break;
0326       case 0x0007: /*   7, SensorRightBorder */
0327         i = get2();
0328         width += i;
0329         imgdata.sizes.raw_inset_crops[0].cwidth =
0330             i - imgdata.sizes.raw_inset_crops[0].cleft;
0331         break;
0332       case 0x0009: /*   9, CFAPattern */
0333         if ((i = get2()))
0334           filters = i;
0335         break;
0336       case 0x0011: /*  17, RedBalance */
0337       case 0x0012: /*  18, BlueBalance */
0338         if (tagtypeIs(LIBRAW_EXIFTAG_TYPE_SHORT) && len == 1)
0339           cam_mul[(tag - 0x0011) * 2] = get2() / 256.0;
0340         break;
0341       case 0x0017: /*  23, ISO */
0342         if (tagtypeIs(LIBRAW_EXIFTAG_TYPE_SHORT))
0343           iso_speed = get2();
0344         break;
0345       case 0x0024: /*  36, WBRedLevel */
0346       case 0x0025: /*  37, WBGreenLevel */
0347       case 0x0026: /*  38, WBBlueLevel */
0348         cam_mul[tag - 0x0024] = get2();
0349         break;
0350       case 0x0027: /*  39, WBInfo2 */
0351         if ((i = get2()) > 0x100)
0352           break;
0353         for (c = 0; c < i; c++)
0354         {
0355           if ((j = get2()) < 0x100)
0356           {
0357             icWBC[j][0] = get2();
0358             icWBC[j][1] = icWBC[j][3] = get2();
0359             icWBC[j][2] = get2();
0360             if (c == 1 && i > 6 && cam_mul[0] <= 0.001f)
0361                 for (int q = 0; q < 4; q++)
0362                     cam_mul[q] = icWBC[j][q];
0363           }
0364           else
0365             fseek(ifp, 6, SEEK_CUR);
0366         }
0367         break;
0368       case 0x002e: /*  46, JpgFromRaw */
0369         if ((type != LIBRAW_EXIFTAG_TYPE_UNDEFINED) || (fgetc(ifp) != 0xff) || (fgetc(ifp) != 0xd8))
0370           break;
0371         thumb_offset = ftell(ifp) - 2;
0372         thumb_length = len;
0373         break;
0374 
0375       case 0x0118: /* 280, Panasonic RW2 offset */
0376         if (type != LIBRAW_EXIFTAG_TYPE_LONG)
0377           break;
0378         load_raw = &LibRaw::panasonic_load_raw;
0379         load_flags = 0x2008;
0380       case 0x0111: /* 273, StripOffset */
0381         if (len > 1 && len < 16384)
0382         {
0383           off_t sav = ftell(ifp);
0384           tiff_ifd[ifd].strip_offsets = (int *)calloc(len, sizeof(int));
0385           tiff_ifd[ifd].strip_offsets_count = len;
0386           for (int ii = 0; ii < (int)len; ii++)
0387             tiff_ifd[ifd].strip_offsets[ii] = get4() + base;
0388           fseek(ifp, sav, SEEK_SET); // restore position
0389         }
0390         /* fallthrough */
0391         tiff_ifd[ifd].offset = get4() + base;
0392         if (!tiff_ifd[ifd].bps && tiff_ifd[ifd].offset > 0)
0393         {
0394           fseek(ifp, tiff_ifd[ifd].offset, SEEK_SET);
0395           if (ljpeg_start(&jh, 1))
0396           {
0397             tiff_ifd[ifd].comp = 6;
0398             tiff_ifd[ifd].t_width = jh.wide;
0399             tiff_ifd[ifd].t_height = jh.high;
0400             tiff_ifd[ifd].bps = jh.bits;
0401             tiff_ifd[ifd].samples = jh.clrs;
0402             if (!(jh.sraw || (jh.clrs & 1)))
0403               tiff_ifd[ifd].t_width *= jh.clrs;
0404             if ((tiff_ifd[ifd].t_width > 4 * tiff_ifd[ifd].t_height) & ~jh.clrs)
0405             {
0406               tiff_ifd[ifd].t_width /= 2;
0407               tiff_ifd[ifd].t_height *= 2;
0408             }
0409             i = order;
0410             parse_tiff(tiff_ifd[ifd].offset + 12);
0411             order = i;
0412           }
0413         }
0414         break;
0415       }
0416 
0417     } /* processing of Panasonic-specific tags finished */
0418 
0419     switch (tag)
0420     {            /* processing of general EXIF tags */
0421     case 0xf000: /* 61440, Fuji HS10 table */
0422       fseek(ifp, get4() + base, SEEK_SET);
0423       parse_tiff_ifd(base);
0424       break;
0425     case 0x00fe: /* NewSubfileType */
0426       tiff_ifd[ifd].newsubfiletype = getreal(type);
0427       break;
0428     case 0x0100: /* 256, ImageWidth */
0429     case 0xf001: /* 61441, Fuji RAF RawImageFullWidth */
0430       tiff_ifd[ifd].t_width = getint(type);
0431       break;
0432     case 0x0101: /* 257, ImageHeight */
0433     case 0xf002: /* 61442, Fuji RAF RawImageFullHeight */
0434       tiff_ifd[ifd].t_height = getint(type);
0435       break;
0436     case 0x0102: /* 258, BitsPerSample */
0437     case 0xf003: /* 61443, Fuji RAF 0xf003 */
0438       if(!tiff_ifd[ifd].samples || tag != 0x0102) // ??? already set by tag 0x115
0439         tiff_ifd[ifd].samples = len & 7;
0440       tiff_ifd[ifd].bps = getint(type);
0441       if (tiff_bps < (unsigned)tiff_ifd[ifd].bps)
0442         tiff_bps = tiff_ifd[ifd].bps;
0443       break;
0444     case 0xf006: /* 61446, Fuji RAF 0xf006 */
0445       raw_height = 0;
0446       if (tiff_ifd[ifd].bps > 12)
0447         break;
0448       load_raw = &LibRaw::packed_load_raw;
0449       load_flags = get4() ? 24 : 80;
0450       break;
0451     case 0x0103: /* 259, Compression */
0452                  /*
0453                     262  = Kodak 262
0454                   32767  = Sony ARW Compressed
0455                   32769  = Packed RAW
0456                   32770  = Samsung SRW Compressed
0457                   32772  = Samsung SRW Compressed 2
0458                   32867  = Kodak KDC Compressed
0459                   34713  = Nikon NEF Compressed
0460                   65000  = Kodak DCR Compressed
0461                   65535  = Pentax PEF Compressed
0462                  */
0463       tiff_ifd[ifd].comp = getint(type);
0464       break;
0465     case 0x0106: /* 262, PhotometricInterpretation */
0466       tiff_ifd[ifd].phint = get2();
0467       break;
0468     case 0x010e: /* 270, ImageDescription */
0469       fread(desc, 512, 1, ifp);
0470       break;
0471     case 0x010f: /* 271, Make */
0472       fgets(make, 64, ifp);
0473       break;
0474     case 0x0110: /* 272, Model */
0475       if (!strncmp(make, "Hasselblad", 10) && model[0] &&
0476           (imHassy.format != LIBRAW_HF_Imacon))
0477         break;
0478       fgets(model, 64, ifp);
0479       break;
0480     case 0x0116: // 278
0481       tiff_ifd[ifd].rows_per_strip = getint(type);
0482       break;
0483     case 0x0112: /* 274, Orientation */
0484       tiff_ifd[ifd].t_flip = "50132467"[get2() & 7] - '0';
0485       break;
0486     case 0x0115: /* 277, SamplesPerPixel */
0487       tiff_ifd[ifd].samples = getint(type) & 7;
0488       break;
0489     case 0x0152: /* Extrasamples */
0490       tiff_ifd[ifd].extrasamples = (getint(type) & 0xff) + 1024;
0491       break;
0492     case 0x0117: /* 279, StripByteCounts */
0493       if (len > 1 && len < 16384)
0494       {
0495         off_t sav = ftell(ifp);
0496         tiff_ifd[ifd].strip_byte_counts = (int *)calloc(len, sizeof(int));
0497         tiff_ifd[ifd].strip_byte_counts_count = len;
0498         for (int ii = 0; ii < (int)len; ii++)
0499           tiff_ifd[ifd].strip_byte_counts[ii] = get4();
0500         fseek(ifp, sav, SEEK_SET); // restore position
0501       }
0502       /* fallback */
0503     case 0x0202: // 514
0504     case 0xf008: // 61448
0505       tiff_ifd[ifd].bytes = get4();
0506       break;
0507     case 0xf00e: // 61454, FujiFilm "As Shot"
0508       FORC3 cam_mul[GRBG_2_RGBG(c)] = getint(type);
0509       break;
0510     case 0x0131: /* 305, Software */
0511       fgets(software, 64, ifp);
0512       if (!strncmp(software, "Adobe", 5) || !strncmp(software, "dcraw", 5) ||
0513           !strncmp(software, "UFRaw", 5) || !strncmp(software, "Bibble", 6) ||
0514           !strcmp(software, "Digital Photo Professional"))
0515         is_raw = 0;
0516       break;
0517     case 0x0132: /* 306, DateTime */
0518       get_timestamp(0);
0519       break;
0520     case 0x013b: /* 315, Artist */
0521       fread(artist, 64, 1, ifp);
0522       break;
0523     case 0x013d: // 317
0524       tiff_ifd[ifd].predictor = getint(type);
0525       break;
0526     case 0x0142: /* 322, TileWidth */
0527       tiff_ifd[ifd].t_tile_width = getint(type);
0528       break;
0529     case 0x0143: /* 323, TileLength */
0530       tiff_ifd[ifd].t_tile_length = getint(type);
0531       break;
0532     case 0x0144: /* 324, TileOffsets */
0533       tiff_ifd[ifd].offset = len > 1 ? ftell(ifp) : get4();
0534       if (len == 1)
0535         tiff_ifd[ifd].t_tile_width = tiff_ifd[ifd].t_tile_length = 0;
0536       if (len == 4)
0537       {
0538         load_raw = &LibRaw::sinar_4shot_load_raw;
0539         is_raw = 5;
0540       }
0541       break;
0542     case 0x0145: // 325
0543       tiff_ifd[ifd].bytes = len > 1 ? ftell(ifp) : get4();
0544       break;
0545     case 0x014a: /* 330, SubIFDs */
0546       if (!strcmp(model, "DSLR-A100") && tiff_ifd[ifd].t_width == 3872)
0547       {
0548         load_raw = &LibRaw::sony_arw_load_raw;
0549         data_offset = get4() + base;
0550         ifd++;
0551         if (ifd >= int(sizeof tiff_ifd / sizeof tiff_ifd[0]))
0552           throw LIBRAW_EXCEPTION_IO_CORRUPT;
0553         break;
0554       }
0555       if (!strncmp(make, "Hasselblad", 10) &&
0556           libraw_internal_data.unpacker_data.hasselblad_parser_flag)
0557       {
0558         fseek(ifp, ftell(ifp) + 4, SEEK_SET);
0559         fseek(ifp, get4() + base, SEEK_SET);
0560         parse_tiff_ifd(base);
0561         break;
0562       }
0563       if (len > 1000)
0564         len = 1000; /* 1000 SubIFDs is enough */
0565       while (len--)
0566       {
0567         i = ftell(ifp);
0568         fseek(ifp, get4() + base, SEEK_SET);
0569         if (parse_tiff_ifd(base))
0570           break;
0571         fseek(ifp, i + 4, SEEK_SET);
0572       }
0573       break;
0574     case 0x0153: // 339
0575       tiff_ifd[ifd].sample_format = getint(type);
0576       break;
0577     case 0x0190: // 400
0578       strcpy(make, "Sarnoff");
0579       maximum = 0xfff;
0580       break;
0581     case 0x02bc: // 700
0582       if ((tagtypeIs(LIBRAW_EXIFTAG_TYPE_BYTE) ||
0583           tagtypeIs(LIBRAW_EXIFTAG_TYPE_ASCII) ||
0584           tagtypeIs(LIBRAW_EXIFTAG_TYPE_SBYTE) ||
0585           tagtypeIs(LIBRAW_EXIFTOOLTAGTYPE_binary)) &&
0586           (len > 1) && (len < 5100000))
0587       {
0588         xmpdata = (char *)malloc(xmplen = len + 1);
0589         fread(xmpdata, len, 1, ifp);
0590         xmpdata[len] = 0;
0591       }
0592       break;
0593     case 0x7000:
0594       imSony.SonyRawFileType = get2();
0595       break;
0596     case 0x7010: // 28688
0597       FORC4 sony_curve[c + 1] = get2() >> 2 & 0xfff;
0598       for (i = 0; i < 5; i++)
0599         for (j = sony_curve[i] + 1; j <= (int)sony_curve[i + 1]; j++)
0600           curve[j] = curve[j - 1] + (1 << i);
0601       break;
0602     case 0x7200: // 29184, Sony SR2Private
0603       sony_offset = get4();
0604       break;
0605     case 0x7201: // 29185, Sony SR2Private
0606       sony_length = get4();
0607       break;
0608     case 0x7221: // 29217, Sony SR2Private
0609       sony_key = get4();
0610       break;
0611     case 0x7250: // 29264, Sony SR2Private
0612       parse_minolta(ftell(ifp));
0613       raw_width = 0;
0614       break;
0615     case 0x7303: // 29443, Sony SR2SubIFD
0616       FORC4 cam_mul[GRBG_2_RGBG(c)] = get2();
0617       break;
0618     case 0x7313: // 29459, Sony SR2SubIFD
0619       FORC4 cam_mul[RGGB_2_RGBG(c)] = get2();
0620       break;
0621     case 0x7310: // 29456, Sony SR2SubIFD
0622       FORC4 cblack[RGGB_2_RGBG(c)] = get2();
0623       i = cblack[3];
0624       FORC3 if (i > (int)cblack[c]) i = cblack[c];
0625       FORC4 cblack[c] -= i;
0626       black = i;
0627       break;
0628     case 0x827d: /* 33405, Model2 */
0629                  /*
0630                   for Kodak ProBack 645 PB645x-yyyy 'x' is:
0631                   'M' for Mamiya 645
0632                   'C' for Contax 645
0633                   'H' for Hasselblad H-series
0634                  */
0635       fgets(model2, 64, ifp);
0636       break;
0637     case 0x828d: /* 33421, CFARepeatPatternDim */
0638       if (get2() == 6 && get2() == 6)
0639         tiff_ifd[ifd].t_filters = filters = 9;
0640       break;
0641     case 0x828e: /* 33422, CFAPattern */
0642       if (filters == 9)
0643       {
0644         FORC(36)((char *)xtrans)[c] = fgetc(ifp) & 3;
0645         break;
0646       }
0647     case 0xfd09: /* 64777, Kodak P-series */
0648       if (len == 36)
0649       {
0650         tiff_ifd[ifd].t_filters = filters = 9;
0651         colors = 3;
0652         FORC(36)((char *)xtrans)[c] = fgetc(ifp) & 3;
0653       }
0654       else if (len > 0)
0655       {
0656         if ((plen = len) > 16)
0657           plen = 16;
0658         fread(cfa_pat, 1, plen, ifp);
0659         for (colors = cfa = i = 0; i < (int)plen && colors < 4; i++)
0660         {
0661           if (cfa_pat[i] > 31)
0662             continue; // Skip wrong data
0663           colors += !(cfa & (1 << cfa_pat[i]));
0664           cfa |= 1 << cfa_pat[i];
0665         }
0666         if (cfa == 070)
0667           memcpy(cfa_pc, "\003\004\005", 3); /* CMY */
0668         if (cfa == 072)
0669           memcpy(cfa_pc, "\005\003\004\001", 4); /* GMCY */
0670         goto guess_cfa_pc;
0671       }
0672       break;
0673     case 0x8290: // 33424
0674     case 0xfe00: // 65024
0675       fseek(ifp, get4() + base, SEEK_SET);
0676       parse_kodak_ifd(base);
0677       break;
0678     case 0x829a: /* 33434, ExposureTime */
0679       tiff_ifd[ifd].t_shutter = shutter = getreal(type);
0680       break;
0681     case 0x829d: /* 33437, FNumber */
0682       aperture = getreal(type);
0683       break;
0684     case 0x9400:
0685       imCommon.exifAmbientTemperature = getreal(type);
0686       if ((imCommon.CameraTemperature > -273.15f) &&
0687           ((OlyID == OlyID_TG_5) || (OlyID == OlyID_TG_6)))
0688         imCommon.CameraTemperature +=
0689             imCommon.exifAmbientTemperature;
0690       break;
0691     case 0x9401:
0692       imCommon.exifHumidity = getreal(type);
0693       break;
0694     case 0x9402:
0695       imCommon.exifPressure = getreal(type);
0696       break;
0697     case 0x9403:
0698       imCommon.exifWaterDepth = getreal(type);
0699       break;
0700     case 0x9404:
0701       imCommon.exifAcceleration = getreal(type);
0702       break;
0703     case 0x9405:
0704       imCommon.exifCameraElevationAngle = getreal(type);
0705       break;
0706     case 0xa405: // FocalLengthIn35mmFormat
0707       imgdata.lens.FocalLengthIn35mmFormat = get2();
0708       break;
0709     case 0xa431: // BodySerialNumber
0710     case 0xc62f:
0711       stmread(imgdata.shootinginfo.BodySerial, len, ifp);
0712       break;
0713     case 0xa432: // LensInfo, 42034dec, Lens Specification per EXIF standard
0714       imgdata.lens.MinFocal = getreal(type);
0715       imgdata.lens.MaxFocal = getreal(type);
0716       imgdata.lens.MaxAp4MinFocal = getreal(type);
0717       imgdata.lens.MaxAp4MaxFocal = getreal(type);
0718       break;
0719     case 0xa435: // LensSerialNumber
0720       stmread(imgdata.lens.LensSerial, len, ifp);
0721       break;
0722     case 0xc630: // DNG LensInfo, Lens Specification per EXIF standard
0723       imgdata.lens.MinFocal = getreal(type);
0724       imgdata.lens.MaxFocal = getreal(type);
0725       imgdata.lens.MaxAp4MinFocal = getreal(type);
0726       imgdata.lens.MaxAp4MaxFocal = getreal(type);
0727       break;
0728     case 0xa420: /* 42016, ImageUniqueID */
0729       stmread(imgdata.color.ImageUniqueID, len, ifp);
0730       break;
0731     case 0xc65d: /* 50781, RawDataUniqueID */
0732       imgdata.color.RawDataUniqueID[16] = 0;
0733       fread(imgdata.color.RawDataUniqueID, 1, 16, ifp);
0734       break;
0735     case 0xa433: // LensMake
0736       stmread(imgdata.lens.LensMake, len, ifp);
0737       break;
0738     case 0xa434: // LensModel
0739       stmread(imgdata.lens.Lens, len, ifp);
0740       if (!strncmp(imgdata.lens.Lens, "----", 4))
0741         imgdata.lens.Lens[0] = 0;
0742       break;
0743     case 0x9205:
0744       imgdata.lens.EXIF_MaxAp = libraw_powf64l(2.0f, (getreal(type) / 2.0f));
0745       break;
0746     case 0x8602: /* 34306, Leaf white balance */
0747       FORC4
0748       {
0749         int q = get2();
0750         if (q)
0751           cam_mul[GRGB_2_RGBG(c)] = 4096.0 / q;
0752       }
0753       break;
0754     case 0x8603: /* 34307, Leaf CatchLight color matrix */
0755       fread(software, 1, 7, ifp);
0756       if (strncmp(software, "MATRIX", 6))
0757         break;
0758       colors = 4;
0759       for (raw_color = i = 0; i < 3; i++)
0760       {
0761         FORC4 fscanf(ifp, "%f", &rgb_cam[i][GRGB_2_RGBG(c)]);
0762         if (!use_camera_wb)
0763           continue;
0764         num = 0;
0765         FORC4 num += rgb_cam[i][c];
0766         FORC4 rgb_cam[i][c] /= MAX(1, num);
0767       }
0768       break;
0769     case 0x8606: /* 34310, Leaf metadata */
0770       parse_mos(ftell(ifp));
0771     case 0x85ff: // 34303
0772       strcpy(make, "Leaf");
0773       break;
0774     case 0x8769: /* 34665, EXIF tag */
0775       fseek(ifp, get4() + base, SEEK_SET);
0776       parse_exif(base);
0777       break;
0778     case 0x8825: /* 34853, GPSInfo tag */
0779     {
0780       unsigned pos;
0781       fseek(ifp, pos = (get4() + base), SEEK_SET);
0782       parse_gps(base);
0783       fseek(ifp, pos, SEEK_SET);
0784       parse_gps_libraw(base);
0785     }
0786     break;
0787     case 0x8773: /* 34675, InterColorProfile */
0788     case 0xc68f: /* 50831, AsShotICCProfile */
0789       profile_offset = ftell(ifp);
0790       profile_length = len;
0791       break;
0792     case 0x9102: /* 37122, CompressedBitsPerPixel */
0793       kodak_cbpp = get4();
0794       break;
0795     case 0x920a: /* 37386, FocalLength */
0796       focal_len = getreal(type);
0797       break;
0798     case 0x9211: /* 37393, ImageNumber */
0799       shot_order = getint(type);
0800       break;
0801     case 0x9215: /* 37397, ExposureIndex */
0802       imCommon.exifExposureIndex = getreal(type);
0803       break;
0804     case 0x9218: /* 37400, old Kodak KDC tag */
0805       for (raw_color = i = 0; i < 3; i++)
0806       {
0807         getreal(type);
0808         FORC3 rgb_cam[i][c] = getreal(type);
0809       }
0810       break;
0811     case 0xa010: // 40976
0812       strip_offset = get4();
0813       switch (tiff_ifd[ifd].comp)
0814       {
0815       case 0x8002: // 32770
0816         load_raw = &LibRaw::samsung_load_raw;
0817         break;
0818       case 0x8004: // 32772
0819         load_raw = &LibRaw::samsung2_load_raw;
0820         break;
0821       case 0x8005: // 32773
0822         load_raw = &LibRaw::samsung3_load_raw;
0823         break;
0824       }
0825       break;
0826     case 0xb4c3: /* 46275, Imacon tags */
0827       imHassy.format = LIBRAW_HF_Imacon;
0828       strcpy(make, "Imacon");
0829       data_offset = ftell(ifp);
0830       ima_len = len;
0831       break;
0832     case 0xb4c7: // 46279
0833       if (!ima_len)
0834         break;
0835       fseek(ifp, 38, SEEK_CUR);
0836     case 0xb4c2: // 46274
0837       fseek(ifp, 40, SEEK_CUR);
0838       raw_width = get4();
0839       raw_height = get4();
0840       left_margin = get4() & 7;
0841       width = raw_width - left_margin - (get4() & 7);
0842       top_margin = get4() & 7;
0843       height = raw_height - top_margin - (get4() & 7);
0844       if (raw_width == 7262 && ima_len == 234317952)
0845       {
0846         height = 5412;
0847         width = 7216;
0848         left_margin = 7;
0849         filters = 0;
0850       }
0851       else if (raw_width == 7262)
0852       {
0853         height = 5444;
0854         width = 7244;
0855         left_margin = 7;
0856       }
0857       fseek(ifp, 52, SEEK_CUR);
0858       FORC3 cam_mul[c] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
0859       fseek(ifp, 114, SEEK_CUR);
0860       flip = (get2() >> 7) * 90;
0861       if (width * (height * 6l) == ima_len)
0862       {
0863         if (flip % 180 == 90)
0864           SWAP(width, height);
0865         raw_width = width;
0866         raw_height = height;
0867         left_margin = top_margin = filters = flip = 0;
0868       }
0869       c = unsigned(height) * unsigned(width) / 1000000;
0870       if (c == 32)
0871         c--;
0872       sprintf(model, "Ixpress %d-Mp", c);
0873       load_raw = &LibRaw::imacon_full_load_raw;
0874       if (filters)
0875       {
0876         if (left_margin & 1)
0877           filters = 0x61616161;
0878         load_raw = &LibRaw::unpacked_load_raw;
0879       }
0880       maximum = 0xffff;
0881       break;
0882     case 0xc516: /* 50454, Sinar tag */
0883     case 0xc517: // 50455
0884       if (len < 1 || len > 2560000 || !(cbuf = (char *)malloc(len)))
0885         break;
0886       if (fread(cbuf, 1, len, ifp) != (int)len)
0887         throw LIBRAW_EXCEPTION_IO_CORRUPT; // cbuf to be free'ed in recycle
0888       cbuf[len - 1] = 0;
0889       for (cp = cbuf - 1; cp && cp < cbuf + len; cp = strchr(cp, '\n'))
0890         if (!strncmp(++cp, "Neutral ", 8))
0891           sscanf(cp + 8, "%f %f %f", cam_mul, cam_mul + 1, cam_mul + 2);
0892       free(cbuf);
0893       break;
0894     case 0xc51a: // 50458
0895       if (!make[0])
0896         strcpy(make, "Hasselblad");
0897       break;
0898     case 0xc51b: /* 50459, Hasselblad tag */
0899       if (!libraw_internal_data.unpacker_data.hasselblad_parser_flag)
0900       {
0901         libraw_internal_data.unpacker_data.hasselblad_parser_flag = 1;
0902         i = order;
0903         j = ftell(ifp);
0904         c = tiff_nifds;
0905         order = get2();
0906         fseek(ifp, j + (get2(), get4()), SEEK_SET);
0907         parse_tiff_ifd(j);
0908         maximum = 0xffff;
0909         tiff_nifds = c;
0910         order = i;
0911         break;
0912       }
0913     case 0xc612: /* 50706, DNGVersion */
0914       FORC4 dng_version = (dng_version << 8) + fgetc(ifp);
0915       if (!make[0])
0916         strcpy(make, "DNG");
0917       is_raw = 1;
0918       break;
0919     case 0xc614: /* 50708, UniqueCameraModel */
0920       stmread(imgdata.color.UniqueCameraModel, len, ifp);
0921       if (model[0])
0922         break;
0923       strncpy(make, imgdata.color.UniqueCameraModel,
0924               MIN(len, sizeof(imgdata.color.UniqueCameraModel)));
0925       if ((cp = strchr(make, ' ')))
0926       {
0927         strcpy(model, cp + 1);
0928         *cp = 0;
0929       }
0930       break;
0931     case 0xc616: /* 50710, CFAPlaneColor */
0932       if (filters == 9)
0933         break;
0934       if (len > 4)
0935         len = 4;
0936       colors = len;
0937       fread(cfa_pc, 1, colors, ifp);
0938     guess_cfa_pc:
0939       FORCC tab[cfa_pc[c]] = c;
0940       cdesc[c] = 0;
0941       for (i = 16; i--;)
0942         filters = filters << 2 | tab[cfa_pat[i % plen]];
0943       filters -= !filters;
0944       tiff_ifd[ifd].t_filters = filters;
0945       break;
0946     case 0xc617: /* 50711, CFALayout */
0947       if (get2() == 2)
0948         tiff_ifd[ifd].t_fuji_width = fuji_width = 1;
0949       break;
0950     case 0x0123: // 291
0951     case 0xc618: /* 50712, LinearizationTable */
0952       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_LINTABLE;
0953       tiff_ifd[ifd].lineartable_offset = ftell(ifp);
0954       tiff_ifd[ifd].lineartable_len = len;
0955       linear_table(len);
0956       break;
0957     case 0xc619: /* 50713, BlackLevelRepeatDim */
0958       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
0959       tiff_ifd[ifd].dng_levels.dng_fcblack[4] =
0960           tiff_ifd[ifd].dng_levels.dng_cblack[4] = cblack[4] = get2();
0961       tiff_ifd[ifd].dng_levels.dng_fcblack[5] =
0962           tiff_ifd[ifd].dng_levels.dng_cblack[5] = cblack[5] = get2();
0963       if (cblack[4] * cblack[5] >
0964           (LIBRAW_CBLACK_SIZE -
0965            7)) // Use last cblack item as DNG black level count
0966         tiff_ifd[ifd].dng_levels.dng_fcblack[4] =
0967             tiff_ifd[ifd].dng_levels.dng_fcblack[5] =
0968                 tiff_ifd[ifd].dng_levels.dng_cblack[4] =
0969                     tiff_ifd[ifd].dng_levels.dng_cblack[5] = cblack[4] =
0970                         cblack[5] = 1;
0971       break;
0972 
0973     case 0xf00c:
0974       if (imFuji.RAFDataGeneration != 4096)
0975       {
0976         unsigned fwb[4];
0977         FORC4 fwb[c] = get4();
0978         if (fwb[3] < 0x100)
0979         {
0980           FORC3 icWBC[fwb[3]][GRBG_2_RGBG(c)] = fwb[c];
0981           icWBC[fwb[3]][3] = icWBC[fwb[3]][1];
0982           if ((fwb[3] == 17) &&                                      // Tungsten WB
0983               (libraw_internal_data.unpacker_data.lenRAFData > 3) &&
0984               (libraw_internal_data.unpacker_data.lenRAFData < 10240000))
0985           {
0986             INT64 f_save = ftell(ifp);
0987             rafdata = (ushort *)malloc(
0988                 sizeof(ushort) * libraw_internal_data.unpacker_data.lenRAFData);
0989             fseek(ifp, libraw_internal_data.unpacker_data.posRAFData, SEEK_SET);
0990             fread(rafdata, sizeof(ushort),
0991                   libraw_internal_data.unpacker_data.lenRAFData, ifp);
0992             fseek(ifp, f_save, SEEK_SET);
0993 
0994             uchar *PrivateMknBuf = (uchar *)rafdata;
0995             int PrivateMknLength = libraw_internal_data.unpacker_data.lenRAFData
0996                                    << 1;
0997             for (int pos = 0; pos < PrivateMknLength - 16; pos++)
0998             {
0999               if (!memcmp(PrivateMknBuf + pos, "TSNERDTS", 8)) // STDRENST
1000               {
1001                 imFuji.isTSNERDTS = 1;
1002                 break;
1003               }
1004             }
1005             int fj; // 31? (fj<<1)-0x3c : 34? (fj<<1)-0x4e : undef
1006             int is34 = 0;
1007             if ((imFuji.RAFDataVersion == 0x0260) || // X-Pro3, GFX 100S
1008                 (imFuji.RAFDataVersion == 0x0261) || // X100V, GFX 50S II
1009                 (imFuji.RAFDataVersion == 0x0262) || // X-T4
1010                 (imFuji.RAFDataVersion == 0x0263) || // X-H2S
1011                 (imFuji.RAFDataVersion == 0x0264) || // X-S10
1012                 (imFuji.RAFDataVersion == 0x0265) || // X-E4
1013                 (imFuji.RAFDataVersion == 0x0266) || // X-T30 II
1014                 !strcmp(model, "X-Pro3")     ||
1015                 !strcmp(model, "GFX 100S")   ||
1016                 !strcmp(model, "GFX100S")    ||
1017                 !strcmp(model, "GFX 50S II") ||
1018                 !strcmp(model, "GFX50S II")  ||
1019                 !strcmp(model, "X100V")      ||
1020                 !strcmp(model, "X-T4")       ||
1021                 !strcmp(model, "X-H2S")      ||
1022                 !strcmp(model, "X-E4")       ||
1023                 !strcmp(model, "X-T30 II")   ||
1024                 !strcmp(model, "X-S10"))
1025 // is34 cameras have 34 CCT values instead of 31, manual still claims 2500 to 10000 K
1026 // aligned 3000 K to Incandescent, as it is usual w/ other Fujifilm cameras
1027               is34 = 1;
1028 
1029             for (int fi = 0;
1030                  fi < int(libraw_internal_data.unpacker_data.lenRAFData - 3); fi++) // looking for Tungsten WB
1031             {
1032               if ((fwb[0] == rafdata[fi]) && (fwb[1] == rafdata[fi + 1]) &&
1033                   (fwb[2] == rafdata[fi + 2])) // found Tungsten WB
1034               {
1035                 if (rafdata[fi - 15] !=
1036                     fwb[0]) // 15 is offset of Tungsten WB from the first
1037                             // preset, Fine Weather WB
1038                   continue;
1039                 for (int wb_ind = 0, ofst = fi - 15; wb_ind < (int)Fuji_wb_list1.size();
1040                      wb_ind++, ofst += 3)
1041                 {
1042                   icWBC[Fuji_wb_list1[wb_ind]][1] =
1043                       icWBC[Fuji_wb_list1[wb_ind]][3] = rafdata[ofst];
1044                   icWBC[Fuji_wb_list1[wb_ind]][0] = rafdata[ofst + 1];
1045                   icWBC[Fuji_wb_list1[wb_ind]][2] = rafdata[ofst + 2];
1046                 }
1047 
1048                 if (is34)
1049                   fi += 24;
1050                 fi += 96;
1051                 for (fj = fi; fj < (fi + 15); fj += 3) // looking for the end of the WB table
1052                 {
1053                   if (rafdata[fj] != rafdata[fi])
1054                   {
1055                     fj -= 93;
1056                     if (is34)
1057                       fj -= 9;
1058 // printf ("wb start in DNG: 0x%04x\n", fj*2-0x4e);
1059                     for (int iCCT = 0, ofst = fj; iCCT < 31;
1060                          iCCT++, ofst += 3)
1061                     {
1062                       icWBCCTC[iCCT][0] = FujiCCT_K[iCCT];
1063                       icWBCCTC[iCCT][1] = rafdata[ofst + 1];
1064                       icWBCCTC[iCCT][2] = icWBCCTC[iCCT][4] = rafdata[ofst];
1065                       icWBCCTC[iCCT][3] = rafdata[ofst + 2];
1066                     }
1067                     break;
1068                   }
1069                 }
1070                 free(rafdata);
1071                 break;
1072               }
1073             }
1074           }
1075         }
1076         FORC4 fwb[c] = get4();
1077         if (fwb[3] < 0x100) {
1078           FORC3 icWBC[fwb[3]][GRBG_2_RGBG(c)] = fwb[c];
1079           icWBC[fwb[3]][3] = icWBC[fwb[3]][1];
1080         }
1081       }
1082       break;
1083     case 0xf00d:
1084       if (imFuji.RAFDataGeneration != 4096)
1085       {
1086         FORC3 icWBC[LIBRAW_WBI_Auto][GRBG_2_RGBG(c)] = getint(type);
1087         icWBC[LIBRAW_WBI_Auto][3] = icWBC[LIBRAW_WBI_Auto][1];
1088       }
1089       break;
1090     case 0xc615: /* 50709, LocalizedCameraModel */
1091       stmread(imgdata.color.LocalizedCameraModel, len, ifp);
1092       break;
1093     case 0xf00a: // 61450
1094       cblack[4] = cblack[5] = MIN(sqrt((double)len), 64);
1095     case 0xc61a: /* 50714, BlackLevel */
1096       if (tiff_ifd[ifd].samples > 1 &&
1097           tiff_ifd[ifd].samples == (int)len) // LinearDNG, per-channel black
1098       {
1099         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
1100         for (i = 0; i < 4 && i < (int)len; i++)
1101         {
1102           tiff_ifd[ifd].dng_levels.dng_fcblack[i] = getreal(type);
1103           tiff_ifd[ifd].dng_levels.dng_cblack[i] = cblack[i] =
1104               tiff_ifd[ifd].dng_levels.dng_fcblack[i] + 0.5;
1105         }
1106         // Record len in last cblack field
1107         tiff_ifd[ifd].dng_levels.dng_cblack[LIBRAW_CBLACK_SIZE - 1] = len;
1108 
1109         tiff_ifd[ifd].dng_levels.dng_fblack =
1110             tiff_ifd[ifd].dng_levels.dng_black = black = 0;
1111       }
1112       else if (tiff_ifd[ifd].samples > 1 // Linear DNG w repeat dim
1113                && (tiff_ifd[ifd].samples * cblack[4] * cblack[5] == len))
1114       {
1115         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
1116         tiff_ifd[ifd].dng_levels.dng_cblack[LIBRAW_CBLACK_SIZE - 1] =
1117             cblack[LIBRAW_CBLACK_SIZE - 1] = len;
1118         for (i = 0; i < (int)len && i < LIBRAW_CBLACK_SIZE - 7; i++)
1119         {
1120           tiff_ifd[ifd].dng_levels.dng_fcblack[i + 6] = getreal(type);
1121           tiff_ifd[ifd].dng_levels.dng_cblack[i + 6] = cblack[i + 6] =
1122               tiff_ifd[ifd].dng_levels.dng_fcblack[i + 6] + 0.5;
1123         }
1124       }
1125       else if ((cblack[4] * cblack[5] < 2) && len == 1)
1126       {
1127         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
1128         tiff_ifd[ifd].dng_levels.dng_fblack = getreal(type);
1129         black = tiff_ifd[ifd].dng_levels.dng_black =
1130             tiff_ifd[ifd].dng_levels.dng_fblack;
1131       }
1132       else if (cblack[4] * cblack[5] <= len)
1133       {
1134         FORC(int(cblack[4] * cblack[5]))
1135         {
1136           tiff_ifd[ifd].dng_levels.dng_fcblack[6 + c] = getreal(type);
1137           cblack[6 + c] = tiff_ifd[ifd].dng_levels.dng_fcblack[6 + c];
1138         }
1139         black = 0;
1140         FORC4
1141         cblack[c] = 0;
1142 
1143         if (tag == 0xc61a)
1144         {
1145           tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
1146           FORC(int(cblack[4] * cblack[5]))
1147           tiff_ifd[ifd].dng_levels.dng_cblack[6 + c] = cblack[6 + c];
1148           tiff_ifd[ifd].dng_levels.dng_fblack = 0;
1149           tiff_ifd[ifd].dng_levels.dng_black = 0;
1150           FORC4
1151           tiff_ifd[ifd].dng_levels.dng_fcblack[c] =
1152               tiff_ifd[ifd].dng_levels.dng_cblack[c] = 0;
1153         }
1154       }
1155       break;
1156     case 0xc61b: /* 50715, BlackLevelDeltaH */
1157     case 0xc61c: /* 50716, BlackLevelDeltaV */
1158       for (num = i = 0; i < (int)len && i < 65536; i++)
1159         num += getreal(type);
1160       if (len > 0)
1161       {
1162         black += num / len + 0.5;
1163         tiff_ifd[ifd].dng_levels.dng_fblack += num / float(len);
1164         tiff_ifd[ifd].dng_levels.dng_black += num / len + 0.5;
1165         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BLACK;
1166       }
1167       break;
1168     case 0xc61d: /* 50717, WhiteLevel */
1169       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_WHITE;
1170       tiff_ifd[ifd].dng_levels.dng_whitelevel[0] = maximum = getint(type);
1171       if (tiff_ifd[ifd].samples > 1) // Linear DNG case
1172         for (i = 1; i < 4 && i < (int)len; i++)
1173           tiff_ifd[ifd].dng_levels.dng_whitelevel[i] = getint(type);
1174       break;
1175     case 0xc61e: /* DefaultScale */
1176     {
1177       float q1 = getreal(type);
1178       float q2 = getreal(type);
1179       if (q1 > 0.00001f && q2 > 0.00001f)
1180       {
1181         pixel_aspect = q1 / q2;
1182         if (pixel_aspect > 0.995 && pixel_aspect < 1.005)
1183           pixel_aspect = 1.0;
1184       }
1185     }
1186     break;
1187     case 0xc61f: /* 50719, DefaultCropOrigin */
1188       if (len == 2)
1189       {
1190         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_CROPORIGIN;
1191         tiff_ifd[ifd].dng_levels.default_crop[0] = getreal(type);
1192         tiff_ifd[ifd].dng_levels.default_crop[1] = getreal(type);
1193         if (!strncasecmp(make, "SONY", 4))
1194         {
1195           imgdata.sizes.raw_inset_crops[0].cleft =
1196               tiff_ifd[ifd].dng_levels.default_crop[0];
1197           imgdata.sizes.raw_inset_crops[0].ctop =
1198               tiff_ifd[ifd].dng_levels.default_crop[1];
1199         }
1200       }
1201       break;
1202 
1203     case 0xc620: /* 50720, DefaultCropSize */
1204       if (len == 2)
1205       {
1206         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_CROPSIZE;
1207         tiff_ifd[ifd].dng_levels.default_crop[2] = getreal(type);
1208         tiff_ifd[ifd].dng_levels.default_crop[3] = getreal(type);
1209         if (!strncasecmp(make, "SONY", 4))
1210         {
1211           imgdata.sizes.raw_inset_crops[0].cwidth =
1212               tiff_ifd[ifd].dng_levels.default_crop[2];
1213           imgdata.sizes.raw_inset_crops[0].cheight =
1214               tiff_ifd[ifd].dng_levels.default_crop[3];
1215         }
1216       }
1217       break;
1218 
1219     case 0xc7b5: /* 51125 DefaultUserCrop */
1220       if (len == 4)
1221       {
1222           int cnt = 0;
1223           FORC4
1224           {
1225               float v = getreal(type);
1226               if (v >= 0.f && v <= 1.f)
1227               {
1228                   tiff_ifd[ifd].dng_levels.user_crop[c] = v;
1229                   cnt++;
1230               }
1231           }
1232           if(cnt == 4 // valid values
1233               && tiff_ifd[ifd].dng_levels.user_crop[0] < tiff_ifd[ifd].dng_levels.user_crop[2] // top < bottom
1234               && tiff_ifd[ifd].dng_levels.user_crop[1] < tiff_ifd[ifd].dng_levels.user_crop[3] // left < right
1235               )
1236             tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_USERCROP;
1237       }
1238       break;
1239     case 0x74c7:
1240       if ((len == 2) && !strncasecmp(make, "SONY", 4))
1241       {
1242         imgdata.sizes.raw_inset_crops[0].cleft = get4();
1243         imgdata.sizes.raw_inset_crops[0].ctop = get4();
1244       }
1245       break;
1246 
1247     case 0x74c8:
1248       if ((len == 2) && !strncasecmp(make, "SONY", 4))
1249       {
1250         imgdata.sizes.raw_inset_crops[0].cwidth = get4();
1251         imgdata.sizes.raw_inset_crops[0].cheight = get4();
1252       }
1253       break;
1254 
1255     case 0xc65a: // 50778
1256       tiff_ifd[ifd].dng_color[0].illuminant = get2();
1257       tiff_ifd[ifd].dng_color[0].parsedfields |= LIBRAW_DNGFM_ILLUMINANT;
1258       break;
1259     case 0xc65b: // 50779
1260       tiff_ifd[ifd].dng_color[1].illuminant = get2();
1261       tiff_ifd[ifd].dng_color[1].parsedfields |= LIBRAW_DNGFM_ILLUMINANT;
1262       break;
1263 
1264     case 0xc621: /* 50721, ColorMatrix1 */
1265     case 0xc622: /* 50722, ColorMatrix2 */
1266     {
1267       int chan = (len == 9) ? 3 : (len == 12 ? 4 : 0);
1268       i = tag == 0xc621 ? 0 : 1;
1269       if (chan)
1270       {
1271         tiff_ifd[ifd].dng_color[i].parsedfields |= LIBRAW_DNGFM_COLORMATRIX;
1272         imHassy.nIFD_CM[i] = ifd;
1273       }
1274       FORC(chan) for (j = 0; j < 3; j++)
1275       {
1276         tiff_ifd[ifd].dng_color[i].colormatrix[c][j] = cm[c][j] = getreal(type);
1277       }
1278       use_cm = 1;
1279     }
1280     break;
1281 
1282     case 0xc714: /* ForwardMatrix1 */
1283     case 0xc715: /* ForwardMatrix2 */
1284     {
1285       int chan = (len == 9) ? 3 : (len == 12 ? 4 : 0);
1286       i = tag == 0xc714 ? 0 : 1;
1287       if (chan)
1288         tiff_ifd[ifd].dng_color[i].parsedfields |= LIBRAW_DNGFM_FORWARDMATRIX;
1289       for (j = 0; j < 3; j++)
1290         FORC(chan)
1291         {
1292           tiff_ifd[ifd].dng_color[i].forwardmatrix[j][c] = fm[j][c] =
1293               getreal(type);
1294         }
1295     }
1296     break;
1297 
1298     case 0xc623: /* 50723, CameraCalibration1 */
1299     case 0xc624: /* 50724, CameraCalibration2 */
1300     {
1301       int chan = (len == 9) ? 3 : (len == 16 ? 4 : 0);
1302       j = tag == 0xc623 ? 0 : 1;
1303       if (chan)
1304         tiff_ifd[ifd].dng_color[j].parsedfields |= LIBRAW_DNGFM_CALIBRATION;
1305       for (i = 0; i < chan; i++)
1306         FORC(chan)
1307         {
1308           tiff_ifd[ifd].dng_color[j].calibration[i][c] = cc[i][c] =
1309               getreal(type);
1310         }
1311     }
1312     break;
1313     case 0xc627: /* 50727, AnalogBalance */
1314       if (len >= 3)
1315         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_ANALOGBALANCE;
1316       for (c = 0; c < (int)len && c < 4; c++)
1317       {
1318         tiff_ifd[ifd].dng_levels.analogbalance[c] = ab[c] = getreal(type);
1319       }
1320       break;
1321     case 0xc628: /* 50728, AsShotNeutral */
1322       if (len >= 3)
1323         tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_ASSHOTNEUTRAL;
1324       for (c = 0; c < (int)len && c < 4; c++)
1325         tiff_ifd[ifd].dng_levels.asshotneutral[c] = asn[c] = getreal(type);
1326       break;
1327     case 0xc629: /* 50729, AsShotWhiteXY */
1328       xyz[0] = getreal(type);
1329       xyz[1] = getreal(type);
1330       xyz[2] = 1 - xyz[0] - xyz[1];
1331       FORC3 xyz[c] /= LibRaw_constants::d65_white[c];
1332       break;
1333     case 0xc62a: /* DNG: 50730 BaselineExposure */
1334       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_BASELINEEXPOSURE;
1335       tiff_ifd[ifd].dng_levels.baseline_exposure = getreal(type);
1336       break;
1337     case 0xc62e: /* DNG: 50734 LinearResponseLimit */
1338       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_LINEARRESPONSELIMIT;
1339       tiff_ifd[ifd].dng_levels.LinearResponseLimit = getreal(type);
1340       break;
1341 
1342     case 0xc634: /* 50740 : DNG Adobe, DNG Pentax, Sony SR2, DNG Private */
1343       {
1344         char mbuf[64];
1345         INT64 curr_pos, start_pos = ftell(ifp);
1346         unsigned MakN_order, m_sorder = order;
1347         unsigned MakN_length;
1348         unsigned pos_in_original_raw;
1349         fread(mbuf, 1, 6, ifp);
1350 
1351         if (!strcmp(mbuf, "Adobe"))
1352         {
1353           order = 0x4d4d; // Adobe header is always in "MM" / big endian
1354           curr_pos = start_pos + 6;
1355           while (curr_pos + 8 - start_pos <= len)
1356           {
1357             fread(mbuf, 1, 4, ifp);
1358             curr_pos += 8;
1359 
1360             if (!strncmp(mbuf, "Pano", 4))
1361             { // PanasonicRaw, yes, they use "Pano" as signature
1362               parseAdobePanoMakernote();
1363             }
1364 
1365             if (!strncmp(mbuf, "MakN", 4))
1366             {
1367               MakN_length = get4();
1368               MakN_order = get2();
1369               pos_in_original_raw = get4();
1370               order = MakN_order;
1371 
1372               INT64 save_pos = ifp->tell();
1373               parse_makernote_0xc634(curr_pos + 6 - pos_in_original_raw, 0,
1374                                      AdobeDNG);
1375 
1376               curr_pos = save_pos + MakN_length - 6;
1377               fseek(ifp, curr_pos, SEEK_SET);
1378 
1379               fread(mbuf, 1, 4, ifp);
1380               curr_pos += 8;
1381 
1382               if (!strncmp(mbuf, "Pano ", 4))
1383               {
1384                 parseAdobePanoMakernote();
1385               }
1386 
1387               if (!strncmp(mbuf, "RAF ", 4))
1388               { // Fujifilm Raw, AdobeRAF
1389                 parseAdobeRAFMakernote();
1390               }
1391 
1392               if (!strncmp(mbuf, "SR2 ", 4))
1393               {
1394                 order = 0x4d4d;
1395                 MakN_length = get4();
1396                 MakN_order = get2();
1397                 pos_in_original_raw = get4();
1398                 order = MakN_order;
1399 
1400                 unsigned *buf_SR2;
1401                 unsigned SR2SubIFDOffset = 0;
1402                 unsigned SR2SubIFDLength = 0;
1403                 unsigned SR2SubIFDKey = 0;
1404                 {
1405                   int _base = curr_pos + 6 - pos_in_original_raw;
1406                   unsigned _entries, _tag, _type, _len, _save;
1407                   _entries = get2();
1408                   while (_entries--)
1409                   {
1410                     tiff_get(_base, &_tag, &_type, &_len, &_save);
1411 
1412                     if (_tag == 0x7200)
1413                     {
1414                       SR2SubIFDOffset = get4();
1415                     }
1416                     else if (_tag == 0x7201)
1417                     {
1418                       SR2SubIFDLength = get4();
1419                     }
1420                     else if (_tag == 0x7221)
1421                     {
1422                       SR2SubIFDKey = get4();
1423                     }
1424                     fseek(ifp, _save, SEEK_SET);
1425                   }
1426                 }
1427 
1428                 if (SR2SubIFDLength && (SR2SubIFDLength < 10240000) &&
1429                     (buf_SR2 = (unsigned *)malloc(SR2SubIFDLength + 1024)))
1430                 { // 1024b for safety
1431                   fseek(ifp, SR2SubIFDOffset + base, SEEK_SET);
1432                   fread(buf_SR2, SR2SubIFDLength, 1, ifp);
1433                   sony_decrypt(buf_SR2, SR2SubIFDLength / 4, 1, SR2SubIFDKey);
1434                   parseSonySR2((uchar *)buf_SR2, SR2SubIFDOffset,
1435                                SR2SubIFDLength, AdobeDNG);
1436 
1437                   free(buf_SR2);
1438                 }
1439 
1440               } /* SR2 processed */
1441               break;
1442             }
1443           }
1444         }
1445         else
1446         {
1447           fread(mbuf + 6, 1, 2, ifp);
1448           if (!strcmp(mbuf, "RICOH") && ((sget2((uchar *)mbuf + 6) == 0x4949) ||
1449                                          (sget2((uchar *)mbuf + 6) == 0x4d4d)))
1450           {
1451             is_PentaxRicohMakernotes = 1;
1452           }
1453           if (!strcmp(mbuf, "PENTAX ") || !strcmp(mbuf, "SAMSUNG") ||
1454               is_PentaxRicohMakernotes)
1455           {
1456             fseek(ifp, start_pos, SEEK_SET);
1457             parse_makernote_0xc634(base, 0, CameraDNG);
1458           }
1459         }
1460         fseek(ifp, start_pos, SEEK_SET);
1461         order = m_sorder;
1462       }
1463       if (dng_version)
1464       {
1465         break;
1466       }
1467       parse_minolta(j = get4() + base);
1468       fseek(ifp, j, SEEK_SET);
1469       parse_tiff_ifd(base);
1470       break;
1471     case 0xc640: // 50752
1472       read_shorts(cr2_slice, 3);
1473       break;
1474     case 0xc68b: /* 50827, OriginalRawFileName */
1475       stmread(imgdata.color.OriginalRawFileName, len, ifp);
1476       break;
1477     case 0xc68d: /* 50829 ActiveArea */
1478       tiff_ifd[ifd].t_tm = top_margin = getint(type);
1479       tiff_ifd[ifd].t_lm = left_margin = getint(type);
1480       tiff_ifd[ifd].t_vheight = height = getint(type) - top_margin;
1481       tiff_ifd[ifd].t_vwidth = width = getint(type) - left_margin;
1482       break;
1483     case 0xc68e: /* 50830 MaskedAreas */
1484       for (i = 0; i < (int)len && i < 32; i++)
1485         ((int *)mask)[i] = getint(type);
1486       black = 0;
1487       break;
1488     case 0xc71a: /* 50970, PreviewColorSpace */
1489       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_PREVIEWCS;
1490       tiff_ifd[ifd].dng_levels.preview_colorspace = getint(type);
1491       break;
1492     case 0xc740: /* 51008, OpcodeList1 */
1493       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_OPCODE1;
1494       break;
1495     case 0xc741: /* 51009, OpcodeList2 */
1496       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_OPCODE2;
1497       tiff_ifd[ifd].opcode2_offset = meta_offset = ftell(ifp);
1498       break;
1499     case 0xc74e: /* 51022, OpcodeList3 */
1500       tiff_ifd[ifd].dng_levels.parsedfields |= LIBRAW_DNGFM_OPCODE3;
1501       break;
1502     case 0xfd04: /* 64772, Kodak P-series */
1503       if (len < 13)
1504         break;
1505       fseek(ifp, 16, SEEK_CUR);
1506       data_offset = get4();
1507       fseek(ifp, 28, SEEK_CUR);
1508       data_offset += get4();
1509       load_raw = &LibRaw::packed_load_raw;
1510       break;
1511     case 0xfe02: // 65026
1512       if (tagtypeIs(LIBRAW_EXIFTAG_TYPE_ASCII))
1513         fgets(model2, 64, ifp);
1514     }
1515     fseek(ifp, save, SEEK_SET);
1516   }
1517   if (sony_length && sony_length < 10240000 &&
1518       (buf = (unsigned *)malloc(sony_length)))
1519   {
1520     fseek(ifp, sony_offset, SEEK_SET);
1521     fread(buf, sony_length, 1, ifp);
1522     sony_decrypt(buf, sony_length / 4, 1, sony_key);
1523     parseSonySR2((uchar *)buf, sony_offset, sony_length, nonDNG);
1524     free(buf);
1525   }
1526   for (i = 0; i < colors && i < 4; i++)
1527     FORCC cc[i][c] *= ab[i];
1528   if (use_cm)
1529   {
1530     FORCC for (i = 0; i < 3; i++) for (cam_xyz[c][i] = j = 0; j < colors; j++)
1531         cam_xyz[c][i] += cc[c][j] * cm[j][i] * xyz[i];
1532     cam_xyz_coeff(cmatrix, cam_xyz);
1533   }
1534   if (asn[0])
1535   {
1536     cam_mul[3] = 0;
1537     FORCC
1538     if (fabs(asn[c]) > 0.0001)
1539       cam_mul[c] = 1 / asn[c];
1540   }
1541   if (!use_cm)
1542     FORCC if (fabs(cc[c][c]) > 0.0001) pre_mul[c] /= cc[c][c];
1543   return 0;
1544 }
1545 
1546 int LibRaw::parse_tiff(int _base)
1547 {
1548   INT64 base = _base;
1549   int doff;
1550   fseek(ifp, base, SEEK_SET);
1551   order = get2();
1552   if (order != 0x4949 && order != 0x4d4d)
1553     return 0;
1554   get2();
1555   while ((doff = get4()))
1556   {
1557     INT64 doff64 = doff;
1558     if (doff64 + base > ifp->size()) break;
1559     fseek(ifp, doff64 + base, SEEK_SET);
1560     if (parse_tiff_ifd(_base))
1561       break;
1562   }
1563   return 1;
1564 }
1565 
1566 struct ifd_size_t
1567 {
1568   int ifdi;
1569   INT64 databits;
1570 };
1571 
1572 int ifd_size_t_cmp(const void *a, const void *b)
1573 {
1574   if (!a || !b)
1575     return 0;
1576   const ifd_size_t *ai = (ifd_size_t *)a;
1577   const ifd_size_t *bi = (ifd_size_t *)b;
1578   return bi->databits > ai->databits ? 1
1579                                      : (bi->databits < ai->databits ? -1 : 0);
1580 }
1581 
1582 static LibRaw_internal_thumbnail_formats tiff2thumbformat(int _comp, int _phint, int _bps, const char *_make);
1583 
1584 void LibRaw::apply_tiff()
1585 {
1586   int max_samp = 0, ties = 0, raw = -1, thm = -1, i;
1587   unsigned long long ns, os;
1588   struct jhead jh;
1589 
1590   thumb_misc = 16;
1591   if (thumb_offset)
1592   {
1593     fseek(ifp, thumb_offset, SEEK_SET);
1594     if (ljpeg_start(&jh, 1))
1595     {
1596       if ((unsigned)jh.bits < 17 && (unsigned)jh.wide < 0x10000 &&
1597           (unsigned)jh.high < 0x10000)
1598       {
1599         thumb_misc = jh.bits;
1600         thumb_width = jh.wide;
1601         thumb_height = jh.high;
1602       }
1603     }
1604   }
1605   for (i = tiff_nifds; i--;)
1606   {
1607     if (tiff_ifd[i].t_shutter)
1608       shutter = tiff_ifd[i].t_shutter;
1609     tiff_ifd[i].t_shutter = shutter;
1610   }
1611 
1612   if (dng_version)
1613   {
1614     int ifdc = 0;
1615     for (i = 0; i < (int)tiff_nifds; i++)
1616     {
1617       if (tiff_ifd[i].t_width < 1 || tiff_ifd[i].t_width > 65535 ||
1618           tiff_ifd[i].t_height < 1 || tiff_ifd[i].t_height > 65535)
1619         continue; /* wrong image dimensions */
1620 
1621       int samp = tiff_ifd[i].samples;
1622       if (samp == 2)
1623         samp = 1; // Fuji 2-frame
1624       max_samp = LIM(MAX(max_samp, samp), 1,
1625                      3); // max_samp is needed for thumbnail selection below
1626 
1627      if ( // Check phint only for RAW subfiletype
1628          (tiff_ifd[i].newsubfiletype == 16
1629              || tiff_ifd[i].newsubfiletype == 0
1630              || (tiff_ifd[i].newsubfiletype & 0xffff) == 1)
1631          &&
1632           (tiff_ifd[i].phint != 32803 && tiff_ifd[i].phint != 34892)
1633          )
1634               continue;
1635 
1636       if ((tiff_ifd[i].newsubfiletype == 0) // main image
1637                                             // Enhanced demosaiced:
1638           || (tiff_ifd[i].newsubfiletype == 16 &&
1639               (imgdata.rawparams.options & LIBRAW_RAWOPTIONS_DNG_ADD_ENHANCED))
1640           // Preview: 0x1 or 0x10001
1641           || ((tiff_ifd[i].newsubfiletype & 0xffff) == 1 &&
1642               (imgdata.rawparams.options & LIBRAW_RAWOPTIONS_DNG_ADD_PREVIEWS))
1643         // Transparency mask: 0x4 
1644           || ((tiff_ifd[i].newsubfiletype & 0xffff) == 4 &&
1645           (imgdata.rawparams.options & LIBRAW_RAWOPTIONS_DNG_ADD_MASKS)))
1646       {
1647         // Add this IFD to dng_frames
1648         libraw_internal_data.unpacker_data.dng_frames[ifdc] =
1649             ((tiff_ifd[i].newsubfiletype & 0xffff) << 16) | ((i << 8) & 0xff00);
1650         ifdc++;
1651         // Fuji SuperCCD: second frame:
1652         if ((tiff_ifd[i].newsubfiletype == 0) && tiff_ifd[i].samples == 2)
1653         {
1654           libraw_internal_data.unpacker_data.dng_frames[ifdc] =
1655               ((tiff_ifd[i].newsubfiletype & 0xffff) << 16) |
1656               ((i << 8) & 0xff00) | 1;
1657           ifdc++;
1658         }
1659       }
1660     }
1661     if (ifdc)
1662     {
1663       if (ifdc > 1 && (imgdata.rawparams.options & LIBRAW_RAWOPTIONS_DNG_PREFER_LARGEST_IMAGE))
1664       {
1665         ifd_size_t arr[LIBRAW_IFD_MAXCOUNT * 2];
1666         memset(arr, 0, sizeof(arr));
1667         for (int q = 0; q < ifdc && q < LIBRAW_IFD_MAXCOUNT * 2; q++)
1668         {
1669           int ifdidx =
1670               (libraw_internal_data.unpacker_data.dng_frames[q] >> 8) & 0xff;
1671           arr[q].ifdi = libraw_internal_data.unpacker_data.dng_frames[q];
1672           arr[q].databits =
1673               tiff_ifd[ifdidx].t_width * tiff_ifd[ifdidx].t_height *
1674                   tiff_ifd[ifdidx].samples * tiff_ifd[ifdidx].bps +
1675               (0x100 -
1676                (arr[q].ifdi & 0xff)); // add inverted frame # to ensure same
1677                                       // sort order for similar sized frames.
1678           if (tiff_ifd[ifdidx].phint == 4)
1679               arr[q].databits /= 4; // Force lower bit count for Transp. mask images 
1680         }
1681         qsort(arr, MIN(ifdc, LIBRAW_IFD_MAXCOUNT * 2), sizeof(arr[0]),
1682               ifd_size_t_cmp);
1683         for (int q = 0; q < ifdc && q < LIBRAW_IFD_MAXCOUNT * 2; q++)
1684           libraw_internal_data.unpacker_data.dng_frames[q] = arr[q].ifdi;
1685       }
1686 
1687       int idx = LIM((int)shot_select, 0, ifdc - 1);
1688       i = (libraw_internal_data.unpacker_data.dng_frames[idx] >> 8) &
1689           0xff; // extract frame# back
1690 
1691       raw_width = tiff_ifd[i].t_width;
1692       raw_height = tiff_ifd[i].t_height;
1693       tiff_bps = tiff_ifd[i].bps;
1694       tiff_compress = tiff_ifd[i].comp;
1695       tiff_sampleformat = tiff_ifd[i].sample_format;
1696       data_offset = tiff_ifd[i].offset;
1697       data_size = tiff_ifd[i].bytes;
1698       tiff_flip = tiff_ifd[i].t_flip;
1699       tiff_samples = tiff_ifd[i].samples;
1700       tile_width = tiff_ifd[i].t_tile_width;
1701       tile_length = tiff_ifd[i].t_tile_length;
1702       fuji_width = tiff_ifd[i].t_fuji_width;
1703       if (tiff_samples != 2) /* special case: Fuji SuperCCD */
1704       {
1705         if (tiff_ifd[i].phint == 34892)
1706           filters = 0;
1707         else if (i > 0 && tiff_ifd[i].phint == 32803 &&
1708                  tiff_ifd[0].phint == 32803 && !tiff_ifd[i].t_filters &&
1709                  tiff_ifd[0].t_filters)
1710           filters = tiff_ifd[0].t_filters;
1711         else
1712           filters = tiff_ifd[i].t_filters;
1713         width = tiff_ifd[i].t_vwidth;
1714         height = tiff_ifd[i].t_vheight;
1715         top_margin = tiff_ifd[i].t_tm;
1716         left_margin = tiff_ifd[i].t_lm;
1717         shutter = tiff_ifd[i].t_shutter;
1718         if (tiff_ifd[i].dng_levels.dng_whitelevel[0])
1719           maximum = tiff_ifd[i].dng_levels.dng_whitelevel[0];
1720         else if (tiff_ifd[i].sample_format <= 2 && tiff_bps > 0 &&
1721                  tiff_bps < 32) // SampleFormat: 0-default(1), 1 - Uint, 2 - Int
1722           maximum = (1 << tiff_bps) - 1;
1723         else if (tiff_ifd[i].sample_format == 3)
1724           maximum = 1; // Defaults for FP
1725       }
1726       raw = i;
1727       is_raw = ifdc;
1728     }
1729     else
1730       is_raw = 0;
1731   }
1732   else
1733   {
1734     // Fix for broken Sony bps tag
1735     if (!strncasecmp(make, "Sony", 4))
1736     {
1737         for (i = 0; i < (int)tiff_nifds; i++)
1738         {
1739             if (tiff_ifd[i].bps > 33 && tiff_ifd[i].samples == 1)
1740             {
1741                 int bps = 14; // default
1742                 if (tiff_ifd[i].dng_levels.dng_whitelevel[0] > 0)
1743                 {
1744                     for(int c = 0,j=1; c < 16; c++, j<<=1)
1745                         if (j > (int)tiff_ifd[i].dng_levels.dng_whitelevel[0])
1746                         {
1747                             bps = c; break;
1748                         }
1749                 }
1750                 tiff_ifd[i].bps = bps;
1751             }
1752         }
1753     }
1754 
1755     for (i = 0; i < (int)tiff_nifds; i++)
1756     {
1757       if (tiff_ifd[i].t_width < 1 || tiff_ifd[i].t_width > 65535 ||
1758           tiff_ifd[i].t_height < 1 || tiff_ifd[i].t_height > 65535)
1759         continue; /* wrong image dimensions */
1760       if (max_samp < tiff_ifd[i].samples)
1761         max_samp = tiff_ifd[i].samples;
1762       if (max_samp > 3)
1763         max_samp = 3;
1764 
1765       os = unsigned(raw_width) * unsigned(raw_height);
1766       ns = unsigned(tiff_ifd[i].t_width) * unsigned(tiff_ifd[i].t_height);
1767       if (tiff_bps)
1768       {
1769         os *= tiff_bps;
1770         ns *= tiff_ifd[i].bps;
1771       }
1772       /* too complex if below, so separate if to skip RGB+Alpha TIFFs*/
1773       if (tiff_ifd[i].phint == 2 && tiff_ifd[i].extrasamples > 0 && tiff_ifd[i].samples > 3)
1774           continue; // SKIP RGB+Alpha IFDs
1775 
1776       if ((tiff_ifd[i].comp != 6 || tiff_ifd[i].samples != 3) &&
1777             unsigned(tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 &&
1778             (unsigned)tiff_ifd[i].bps < 33 &&
1779             (unsigned)tiff_ifd[i].samples < 13 && ns &&
1780             ((ns > os && (ties = 1)) || (ns == os && (int)shot_select == ties++)))
1781       {
1782         raw_width = tiff_ifd[i].t_width;
1783         raw_height = tiff_ifd[i].t_height;
1784         tiff_bps = tiff_ifd[i].bps;
1785         tiff_compress = tiff_ifd[i].comp;
1786         tiff_sampleformat = tiff_ifd[i].sample_format;
1787         data_offset = tiff_ifd[i].offset;
1788         data_size = tiff_ifd[i].bytes;
1789         tiff_flip = tiff_ifd[i].t_flip;
1790         tiff_samples = tiff_ifd[i].samples;
1791         tile_width = tiff_ifd[i].t_tile_width;
1792         tile_length = tiff_ifd[i].t_tile_length;
1793         shutter = tiff_ifd[i].t_shutter;
1794         raw = i;
1795       }
1796     }
1797     if (is_raw == 1 && ties)
1798       is_raw = ties;
1799   }
1800   if (is_NikonTransfer && raw >= 0)
1801   {
1802     if (tiff_ifd[raw].bps == 16)
1803     {
1804       if (tiff_compress == 1)
1805       {
1806         if ((raw_width * raw_height * 3) == (tiff_ifd[raw].bytes << 1))
1807         {
1808           tiff_bps = tiff_ifd[raw].bps = 12;
1809         }
1810         else
1811         {
1812           tiff_bps = tiff_ifd[raw].bps = 14;
1813         }
1814       }
1815     }
1816     else if (tiff_ifd[raw].bps == 8)
1817     {
1818       if (tiff_compress == 1)
1819       {
1820         is_NikonTransfer = 2; // 8-bit debayered TIFF, like CoolScan NEFs
1821         imgdata.rawparams.coolscan_nef_gamma = 2.2f;
1822       }
1823     }
1824   }
1825 
1826   if (!tile_width)
1827     tile_width = INT_MAX;
1828   if (!tile_length)
1829     tile_length = INT_MAX;
1830   for (i = tiff_nifds; i--;)
1831     if (tiff_ifd[i].t_flip)
1832       tiff_flip = tiff_ifd[i].t_flip;
1833 
1834 #if 0
1835   if (raw < 0 && is_raw)
1836       is_raw = 0;
1837 #endif
1838 
1839   if (raw >= 0 && !load_raw)
1840     switch (tiff_compress)
1841     {
1842     case 32767:
1843       if (!dng_version &&
1844           INT64(tiff_ifd[raw].bytes) == INT64(raw_width) * INT64(raw_height))
1845       {
1846         tiff_bps = 14;
1847         load_raw = &LibRaw::sony_arw2_load_raw;
1848         break;
1849       }
1850       if (!dng_version && !strncasecmp(make, "Sony", 4) &&
1851           INT64(tiff_ifd[raw].bytes) ==
1852               INT64(raw_width) * INT64(raw_height) * 2LL)
1853       {
1854         tiff_bps = 14;
1855         load_raw = &LibRaw::unpacked_load_raw;
1856         break;
1857       }
1858       if (INT64(tiff_ifd[raw].bytes) * 8LL !=
1859           INT64(raw_width) * INT64(raw_height) * INT64(tiff_bps))
1860       {
1861         raw_height += 8;
1862         load_raw = &LibRaw::sony_arw_load_raw;
1863         break;
1864       }
1865       load_flags = 79;
1866     case 32769:
1867       load_flags++;
1868     case 32770:
1869     case 32773:
1870       goto slr;
1871     case 0:
1872     case 1:
1873         if (dng_version && tiff_sampleformat == 3 &&
1874           (tiff_bps > 8 && (tiff_bps % 8 == 0) && (tiff_bps <= 32))) // only 16,24, and 32 are allowed
1875         {
1876             load_raw = &LibRaw::uncompressed_fp_dng_load_raw;
1877             break;
1878         }
1879       // Sony 14-bit uncompressed
1880       if (!dng_version && !strncasecmp(make, "Sony", 4) &&
1881           INT64(tiff_ifd[raw].bytes) ==
1882               INT64(raw_width) * INT64(raw_height) * 2LL)
1883       {
1884         tiff_bps = 14;
1885         load_raw = &LibRaw::unpacked_load_raw;
1886         break;
1887       }
1888       if (!dng_version && !strncasecmp(make, "Sony", 4) &&
1889           tiff_ifd[raw].samples == 4 &&
1890           INT64(tiff_ifd[raw].bytes) ==
1891               INT64(raw_width) * INT64(raw_height) * 8LL) // Sony ARQ
1892       {
1893         // maybe to detect ARQ with the following:
1894         // if (tiff_ifd[raw].phint == 32892)
1895         tiff_bps = 14;
1896         tiff_samples = 4;
1897         load_raw = &LibRaw::sony_arq_load_raw;
1898         filters = 0;
1899         strcpy(cdesc, "RGBG");
1900         break;
1901       }
1902       if (!strncasecmp(make, "Nikon", 5) &&
1903           (!strncmp(software, "Nikon Scan", 10) || (is_NikonTransfer == 2) ||
1904            strcasestr(model, "COOLSCAN")))
1905       {
1906         load_raw = &LibRaw::nikon_coolscan_load_raw;
1907         raw_color = 1;
1908         colors = (tiff_samples == 3) ? 3 : 1;
1909         filters = 0;
1910         break;
1911       }
1912       if ((!strncmp(make, "OLYMPUS", 7) || !strncmp(make, "OM Digi", 7) ||
1913            (!strncasecmp(make, "CLAUSS", 6) &&
1914             !strncasecmp(model, "piX 5oo", 7))) && // 0x5330303539 works here
1915           (INT64(tiff_ifd[raw].bytes) * 2ULL ==
1916            INT64(raw_width) * INT64(raw_height) * 3ULL))
1917         load_flags = 24;
1918       if (!dng_version && INT64(tiff_ifd[raw].bytes) * 5ULL ==
1919                               INT64(raw_width) * INT64(raw_height) * 8ULL)
1920       {
1921         load_flags = 81;
1922         tiff_bps = 12;
1923       }
1924     slr:
1925       switch (tiff_bps)
1926       {
1927       case 8:
1928         load_raw = &LibRaw::eight_bit_load_raw;
1929         break;
1930       case 12:
1931         if (tiff_ifd[raw].phint == 2)
1932           load_flags = 6;
1933         if (!strncasecmp(make, "NIKON", 5) &&
1934             !strncasecmp(model, "COOLPIX A1000", 13) &&
1935             data_size == raw_width * raw_height * 2u)
1936           load_raw = &LibRaw::unpacked_load_raw;
1937         else
1938           load_raw = &LibRaw::packed_load_raw;
1939         break;
1940       case 14:
1941         load_flags = 0;
1942       case 16:
1943         load_raw = &LibRaw::unpacked_load_raw;
1944         if ((!strncmp(make, "OLYMPUS", 7) || !strncmp(make, "OM Digi", 7) ||
1945              (!strncasecmp(make, "CLAUSS", 6) &&
1946               !strncasecmp(model, "piX 5oo", 7))) && // 0x5330303539 works here
1947             (INT64(tiff_ifd[raw].bytes) * 7LL >
1948              INT64(raw_width) * INT64(raw_height)))
1949           load_raw = &LibRaw::olympus_load_raw;
1950       }
1951       break;
1952     case 6:
1953     case 7:
1954     case 99:
1955       if (!dng_version && tiff_compress == 6 && !strcasecmp(make, "SONY"))
1956         load_raw = &LibRaw::sony_ljpeg_load_raw;
1957       else
1958         load_raw = &LibRaw::lossless_jpeg_load_raw;
1959       break;
1960     case 262:
1961       load_raw = &LibRaw::kodak_262_load_raw;
1962       break;
1963     case 34713:
1964       if ((INT64(raw_width) + 9LL) / 10LL * 16LL * INT64(raw_height) ==
1965           INT64(tiff_ifd[raw].bytes))
1966       {
1967         load_raw = &LibRaw::packed_load_raw;
1968         load_flags = 1;
1969       }
1970       else if (INT64(raw_width) * INT64(raw_height) * 3LL ==
1971                INT64(tiff_ifd[raw].bytes) * 2LL)
1972       {
1973         load_raw = &LibRaw::packed_load_raw;
1974         if (model[0] == 'N')
1975           load_flags = 80;
1976       }
1977       else if (INT64(raw_width) * INT64(raw_height) * 3LL ==
1978                INT64(tiff_ifd[raw].bytes))
1979       {
1980         load_raw = &LibRaw::nikon_yuv_load_raw;
1981         gamma_curve(1 / 2.4, 12.92, 1, 4095);
1982         memset(cblack, 0, sizeof cblack);
1983         filters = 0;
1984       }
1985       else if (INT64(raw_width) * INT64(raw_height) * 2LL ==
1986                INT64(tiff_ifd[raw].bytes))
1987       {
1988         load_raw = &LibRaw::unpacked_load_raw;
1989         load_flags = 4;
1990         order = 0x4d4d;
1991       }
1992 #if 0 /* Never used because of same condition above, but need to recheck */
1993       else if (INT64(raw_width) * INT64(raw_height) * 3LL ==
1994                INT64(tiff_ifd[raw].bytes) * 2LL)
1995       {
1996         load_raw = &LibRaw::packed_load_raw;
1997         load_flags = 80;
1998       }
1999 #endif
2000       else if (tiff_ifd[raw].rows_per_strip &&
2001                tiff_ifd[raw].strip_offsets_count &&
2002                tiff_ifd[raw].strip_offsets_count ==
2003                    tiff_ifd[raw].strip_byte_counts_count)
2004       {
2005         int fit = 1;
2006         for (int q = 0; q < tiff_ifd[raw].strip_byte_counts_count - 1;
2007              q++) // all but last
2008           if (INT64(tiff_ifd[raw].strip_byte_counts[q]) * 2LL !=
2009               INT64(tiff_ifd[raw].rows_per_strip) * INT64(raw_width) * 3LL)
2010           {
2011             fit = 0;
2012             break;
2013           }
2014         if (fit)
2015           load_raw = &LibRaw::nikon_load_striped_packed_raw;
2016         else
2017           load_raw = &LibRaw::nikon_load_raw; // fallback
2018       }
2019       else if ((((INT64(raw_width) * 3LL / 2LL) + 15LL) / 16LL) * 16LL *
2020                    INT64(raw_height) ==
2021                INT64(tiff_ifd[raw].bytes))
2022       {
2023         load_raw = &LibRaw::nikon_load_padded_packed_raw;
2024         load_flags = (((INT64(raw_width) * 3ULL / 2ULL) + 15ULL) / 16ULL) *
2025                      16ULL; // bytes per row
2026       }
2027       else if (!strncmp(model, "NIKON Z 9", 9) && tiff_ifd[raw].offset)
2028       {
2029           INT64 pos = ftell(ifp);
2030           unsigned char cmp[] = "CONTACT_INTOPIX"; // 15
2031           unsigned char buf[16];
2032           fseek(ifp, INT64(tiff_ifd[raw].offset) + 6LL, SEEK_SET);
2033           fread(buf, 1, 16, ifp);
2034           fseek(ifp, pos, SEEK_SET);
2035           if(!memcmp(buf,cmp,15))
2036             load_raw = &LibRaw::nikon_he_load_raw_placeholder;
2037           else
2038             load_raw = &LibRaw::nikon_load_raw;
2039       }
2040       else
2041         load_raw = &LibRaw::nikon_load_raw;
2042       break;
2043     case 65535:
2044       load_raw = &LibRaw::pentax_load_raw;
2045       break;
2046     case 65000:
2047       switch (tiff_ifd[raw].phint)
2048       {
2049       case 2:
2050         load_raw = &LibRaw::kodak_rgb_load_raw;
2051         filters = 0;
2052         break;
2053       case 6:
2054         load_raw = &LibRaw::kodak_ycbcr_load_raw;
2055         filters = 0;
2056         break;
2057       case 32803:
2058         load_raw = &LibRaw::kodak_65000_load_raw;
2059       }
2060     case 32867:
2061     case 34892:
2062       break;
2063     case 8:
2064       break;
2065 #ifdef USE_GPRSDK
2066     case 9:
2067       if (dng_version)
2068         break; /* Compression=9 supported for dng if we compiled with GPR SDK */
2069                /* Else: fallthrough */
2070 #endif
2071     default:
2072       is_raw = 0;
2073     }
2074   if (!dng_version)
2075   {
2076       if (((tiff_samples == 3 && tiff_ifd[raw].bytes &&
2077           !(tiff_bps == 16 &&
2078               !strncmp(make, "Leaf", 4)) && // Allow Leaf/16bit/3color files
2079           tiff_bps != 14 &&
2080           (tiff_compress & -16) != 32768) ||
2081           (tiff_bps == 8 && strncmp(make, "Phase", 5) &&
2082               strncmp(make, "Leaf", 4) && !strcasestr(make, "Kodak") &&
2083               !strstr(model2, "DEBUG RAW"))) &&
2084           !strcasestr(model, "COOLSCAN") && strncmp(software, "Nikon Scan", 10) &&
2085           is_NikonTransfer != 2)
2086           is_raw = 0;
2087 
2088       if (is_raw && raw >= 0 && tiff_ifd[raw].phint == 2 && tiff_ifd[raw].extrasamples > 0 && tiff_ifd[raw].samples > 3)
2089           is_raw = 0; // SKIP RGB+Alpha IFDs
2090   }
2091 
2092   INT64 fsizecheck = 0ULL;
2093 
2094   if (imgdata.rawparams.options & LIBRAW_RAWOPTIONS_CHECK_THUMBNAILS_ALL_VENDORS)
2095       fsizecheck = ifp->size();
2096   else if ((imgdata.rawparams.options & LIBRAW_RAWOPTIONS_CHECK_THUMBNAILS_KNOWN_VENDORS)
2097       && !strncasecmp(make,"Ricoh",5))
2098       fsizecheck = ifp->size();
2099 
2100   for (i = 0; i < (int)tiff_nifds; i++)
2101     if (i != raw &&
2102         (tiff_ifd[i].samples == max_samp ||
2103          (tiff_ifd[i].comp == 7 &&
2104           tiff_ifd[i].samples == 1)) /* Allow 1-bps JPEGs */
2105         && tiff_ifd[i].bps > 0 && tiff_ifd[i].bps < 33 &&
2106         tiff_ifd[i].phint != 32803 && tiff_ifd[i].phint != 34892 &&
2107         unsigned(tiff_ifd[i].t_width | tiff_ifd[i].t_height) < 0x10000 &&
2108         tiff_ifd[i].comp != 34892)
2109     {
2110         if (fsizecheck > 0LL)
2111         {
2112             bool ok = true;
2113             if (tiff_ifd[i].strip_byte_counts_count && tiff_ifd[i].strip_offsets_count)
2114                 for (int s = 0; s < MIN(tiff_ifd[i].strip_byte_counts_count, tiff_ifd[i].strip_offsets_count); s++)
2115                 {
2116                     if (tiff_ifd[i].strip_offsets[s] + tiff_ifd[i].strip_byte_counts[s] > fsizecheck)
2117                     {
2118                         ok = false;
2119                         break;
2120                     }
2121                 }
2122             else if (tiff_ifd[i].bytes > 0)
2123                 if (tiff_ifd[i].offset + tiff_ifd[i].bytes > fsizecheck)
2124                     ok = false;
2125 
2126             if(!ok)
2127                 continue;
2128         }
2129         if ( (INT64(tiff_ifd[i].t_width) * INT64(tiff_ifd[i].t_height) / INT64(SQR(tiff_ifd[i].bps) + 1)) >
2130              (INT64(thumb_width) * INT64(thumb_height) / INT64(SQR(thumb_misc) + 1)) ) 
2131         {
2132 
2133             thumb_width = tiff_ifd[i].t_width;
2134             thumb_height = tiff_ifd[i].t_height;
2135             thumb_offset = tiff_ifd[i].offset;
2136             thumb_length = tiff_ifd[i].bytes;
2137             thumb_misc = tiff_ifd[i].bps;
2138             thm = i;
2139         }
2140         if (imgdata.thumbs_list.thumbcount < LIBRAW_THUMBNAIL_MAXCOUNT && tiff_ifd[i].bytes > 0)
2141         {
2142             bool already = false;
2143             for(int idx = 0; idx < imgdata.thumbs_list.thumbcount ; idx++)
2144                 if (imgdata.thumbs_list.thumblist[idx].toffset == tiff_ifd[i].offset)
2145                 {
2146                     already = true;
2147                     break;
2148                 }
2149             if (!already)
2150             {
2151                 int idx = imgdata.thumbs_list.thumbcount;
2152                 imgdata.thumbs_list.thumblist[idx].tformat = tiff2thumbformat(tiff_ifd[i].comp, tiff_ifd[i].phint,
2153                     tiff_ifd[i].bps, make);
2154                 imgdata.thumbs_list.thumblist[idx].twidth = tiff_ifd[i].t_width;
2155                 imgdata.thumbs_list.thumblist[idx].theight = tiff_ifd[i].t_height;
2156                 imgdata.thumbs_list.thumblist[idx].tflip = tiff_ifd[i].t_flip;
2157                 imgdata.thumbs_list.thumblist[idx].tlength = tiff_ifd[i].bytes;
2158                 imgdata.thumbs_list.thumblist[idx].tmisc = tiff_ifd[i].bps | (tiff_ifd[i].samples << 5);
2159                 imgdata.thumbs_list.thumblist[idx].toffset = tiff_ifd[i].offset;
2160                 imgdata.thumbs_list.thumbcount++;
2161             }
2162         }
2163     }
2164   if (thm >= 0)
2165   {
2166     thumb_misc |= tiff_ifd[thm].samples << 5;
2167     thumb_format = tiff2thumbformat(tiff_ifd[thm].comp, tiff_ifd[thm].phint, tiff_ifd[thm].bps, make);
2168   }
2169 }
2170 
2171 static LibRaw_internal_thumbnail_formats tiff2thumbformat(int _comp, int _phint, int _bps, const char *_make)
2172 {
2173   switch (_comp)
2174   {
2175   case 0:
2176     return LIBRAW_INTERNAL_THUMBNAIL_LAYER;
2177   case 1:
2178     if (_bps <= 8)
2179       return LIBRAW_INTERNAL_THUMBNAIL_PPM;
2180     else if (!strncmp(_make, "Imacon", 6))
2181       return LIBRAW_INTERNAL_THUMBNAIL_PPM16;
2182     else
2183       return LIBRAW_INTERNAL_THUMBNAIL_KODAK_THUMB;
2184   case 65000:
2185     return _phint == 6 ? LIBRAW_INTERNAL_THUMBNAIL_KODAK_YCBCR : LIBRAW_INTERNAL_THUMBNAIL_KODAK_RGB;
2186   }
2187   return LIBRAW_INTERNAL_THUMBNAIL_JPEG; // default
2188 }