File indexing completed on 2025-01-05 03:56:53

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 
0021 #define radc_token(tree) ((signed char)getbithuff(8, huff + (tree) * 256))
0022 
0023 #define FORYX                                                                  \
0024   for (y = 1; y < 3; y++)                                                      \
0025     for (x = col + 1; x >= col; x--)
0026 
0027 #define PREDICTOR                                                              \
0028   (c ? (buf[c][y - 1][x] + buf[c][y][x + 1]) / 2                               \
0029      : (buf[c][y - 1][x + 1] + 2 * buf[c][y - 1][x] + buf[c][y][x + 1]) / 4)
0030 
0031 #ifdef __GNUC__
0032 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
0033 #pragma GCC optimize("no-aggressive-loop-optimizations")
0034 #endif
0035 #endif
0036 
0037 void LibRaw::kodak_radc_load_raw()
0038 {
0039   // All kodak radc images are 768x512
0040   if (width > 768 || raw_width > 768 || height > 512 || raw_height > 512)
0041     throw LIBRAW_EXCEPTION_IO_CORRUPT;
0042   static const signed char src[] = {
0043       1, 1,   2, 3,   3, 4,   4, 2,   5, 7,   6, 5,   7, 6,   7, 8,   1, 0,
0044       2, 1,   3, 3,   4, 4,   5, 2,   6, 7,   7, 6,   8, 5,   8, 8,   2, 1,
0045       2, 3,   3, 0,   3, 2,   3, 4,   4, 6,   5, 5,   6, 7,   6, 8,   2, 0,
0046       2, 1,   2, 3,   3, 2,   4, 4,   5, 6,   6, 7,   7, 5,   7, 8,   2, 1,
0047       2, 4,   3, 0,   3, 2,   3, 3,   4, 7,   5, 5,   6, 6,   6, 8,   2, 3,
0048       3, 1,   3, 2,   3, 4,   3, 5,   3, 6,   4, 7,   5, 0,   5, 8,   2, 3,
0049       2, 6,   3, 0,   3, 1,   4, 4,   4, 5,   4, 7,   5, 2,   5, 8,   2, 4,
0050       2, 7,   3, 3,   3, 6,   4, 1,   4, 2,   4, 5,   5, 0,   5, 8,   2, 6,
0051       3, 1,   3, 3,   3, 5,   3, 7,   3, 8,   4, 0,   5, 2,   5, 4,   2, 0,
0052       2, 1,   3, 2,   3, 3,   4, 4,   4, 5,   5, 6,   5, 7,   4, 8,   1, 0,
0053       2, 2,   2, -2,  1, -3,  1, 3,   2, -17, 2, -5,  2, 5,   2, 17,  2, -7,
0054       2, 2,   2, 9,   2, 18,  2, -18, 2, -9,  2, -2,  2, 7,   2, -28, 2, 28,
0055       3, -49, 3, -9,  3, 9,   4, 49,  5, -79, 5, 79,  2, -1,  2, 13,  2, 26,
0056       3, 39,  4, -16, 5, 55,  6, -37, 6, 76,  2, -26, 2, -13, 2, 1,   3, -39,
0057       4, 16,  5, -55, 6, -76, 6, 37};
0058   std::vector<ushort> huff_buffer(19 * 256);
0059   ushort* huff = &huff_buffer[0];
0060   int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val;
0061   short last[3] = {16, 16, 16}, mul[3], buf[3][3][386];
0062   static const ushort pt[] = {0,    0,    1280, 1344,  2320,  3616,
0063                               3328, 8000, 4095, 16383, 65535, 16383};
0064 
0065   for (i = 2; i < 12; i += 2)
0066     for (c = pt[i - 2]; c <= pt[i]; c++)
0067       curve[c] = (float)(c - pt[i - 2]) / (pt[i] - pt[i - 2]) *
0068                      (pt[i + 1] - pt[i - 1]) +
0069                  pt[i - 1] + 0.5;
0070   for (s = i = 0; i < int(sizeof src); i += 2)
0071     FORC(256 >> src[i])
0072   ((ushort *)huff)[s++] = src[i] << 8 | (uchar)src[i + 1];
0073   s = kodak_cbpp == 243 ? 2 : 3;
0074   FORC(256) huff[18 * 256 + c] = (8 - s) << 8 | c >> s << s | 1 << (s - 1);
0075   getbits(-1);
0076   for (i = 0; i < int(sizeof(buf) / sizeof(short)); i++)
0077     ((short *)buf)[i] = 2048;
0078   for (row = 0; row < height; row += 4)
0079   {
0080     checkCancel();
0081     FORC3 mul[c] = getbits(6);
0082     if (!mul[0] || !mul[1] || !mul[2])
0083       throw LIBRAW_EXCEPTION_IO_CORRUPT;
0084     FORC3
0085     {
0086       val = ((0x1000000 / last[c] + 0x7ff) >> 12) * mul[c];
0087       s = val > 65564 ? 10 : 12;
0088       x = ~((~0u) << (s - 1));
0089       val <<= 12 - s;
0090       for (i = 0; i < int(sizeof(buf[0]) / sizeof(short)); i++)
0091         ((short *)buf[c])[i] = MIN(0x7FFFFFFF, (((short *)buf[c])[i] * static_cast<long long>(val) + x)) >> s;
0092       last[c] = mul[c];
0093       for (r = 0; r <= int(!c); r++)
0094       {
0095         buf[c][1][width / 2] = buf[c][2][width / 2] = mul[c] << 7;
0096         for (tree = 1, col = width / 2; col > 0;)
0097         {
0098           if ((tree = radc_token(tree)))
0099           {
0100             col -= 2;
0101             if (col >= 0)
0102             {
0103               if (tree == 8)
0104                 FORYX buf[c][y][x] = (uchar)radc_token(18) * mul[c];
0105               else
0106                 FORYX buf[c][y][x] = radc_token(tree + 10) * 16 + PREDICTOR;
0107             }
0108           }
0109           else
0110             do
0111             {
0112               nreps = (col > 2) ? radc_token(9) + 1 : 1;
0113               for (rep = 0; rep < 8 && rep < nreps && col > 0; rep++)
0114               {
0115                 col -= 2;
0116                 if (col >= 0)
0117                   FORYX buf[c][y][x] = PREDICTOR;
0118                 if (rep & 1)
0119                 {
0120                   step = radc_token(10) << 4;
0121                   FORYX buf[c][y][x] += step;
0122                 }
0123               }
0124             } while (nreps == 9);
0125         }
0126         for (y = 0; y < 2; y++)
0127           for (x = 0; x < width / 2; x++)
0128           {
0129             val = (buf[c][y + 1][x] << 4) / mul[c];
0130             if (val < 0)
0131               val = 0;
0132             if (c)
0133               RAW(row + y * 2 + c - 1, x * 2 + 2 - c) = val;
0134             else
0135               RAW(row + r * 2 + y, x * 2 + y) = val;
0136           }
0137         memcpy(buf[c][0] + !c, buf[c][2], sizeof buf[c][0] - 2 * !c);
0138       }
0139     }
0140     for (y = row; y < row + 4; y++)
0141       for (x = 0; x < width; x++)
0142         if ((x + y) & 1)
0143         {
0144           r = x ? x - 1 : x + 1;
0145           s = x + 1 < width ? x + 1 : x - 1;
0146           val = (RAW(y, x) - 2048) * 2 + (RAW(y, r) + RAW(y, s)) / 2;
0147           if (val < 0)
0148             val = 0;
0149           RAW(y, x) = val;
0150         }
0151   }
0152   for (i = 0; i < height * width; i++)
0153     raw_image[i] = curve[raw_image[i]];
0154   maximum = 0x3fff;
0155 }
0156 
0157 #undef FORYX
0158 #undef PREDICTOR
0159 
0160 #ifdef NO_JPEG
0161 void LibRaw::kodak_jpeg_load_raw() {}
0162 #else
0163 static void jpegErrorExit_k(j_common_ptr /*cinfo*/)
0164 {
0165   throw LIBRAW_EXCEPTION_DECODE_JPEG;
0166 }
0167 
0168 // LibRaw's Kodak_jpeg_load_raw
0169 void LibRaw::kodak_jpeg_load_raw()
0170 {
0171   if (data_size < 1)
0172     throw LIBRAW_EXCEPTION_DECODE_JPEG;
0173 
0174   int row, col;
0175   struct jpeg_decompress_struct cinfo;
0176   struct jpeg_error_mgr pub;
0177   cinfo.err = jpeg_std_error(&pub);
0178   pub.error_exit = jpegErrorExit_k;
0179 
0180   if (INT64(data_size) >
0181           INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
0182       throw LIBRAW_EXCEPTION_TOOBIG;
0183 
0184   unsigned char *jpg_buf = (unsigned char *)malloc(data_size);
0185   std::vector<uchar> pixel_buf(width * 3);
0186   jpeg_create_decompress(&cinfo);
0187 
0188   fread(jpg_buf, data_size, 1, ifp);
0189   libraw_swab(jpg_buf, data_size);
0190   try
0191   {
0192     jpeg_mem_src(&cinfo, jpg_buf, data_size);
0193     int rc = jpeg_read_header(&cinfo, TRUE);
0194     if (rc != 1)
0195       throw LIBRAW_EXCEPTION_DECODE_JPEG;
0196 
0197     jpeg_start_decompress(&cinfo);
0198     if ((cinfo.output_width != width) || (cinfo.output_height * 2 != height) ||
0199         (cinfo.output_components != 3))
0200     {
0201       throw LIBRAW_EXCEPTION_DECODE_JPEG;
0202     }
0203 
0204     unsigned char *buf[1];
0205     buf[0] = pixel_buf.data();
0206 
0207     while (cinfo.output_scanline < cinfo.output_height)
0208     {
0209       checkCancel();
0210       row = cinfo.output_scanline * 2;
0211       jpeg_read_scanlines(&cinfo, buf, 1);
0212       unsigned char(*pixel)[3] = (unsigned char(*)[3])buf[0];
0213       for (col = 0; col < width; col += 2)
0214       {
0215         RAW(row + 0, col + 0) = pixel[col + 0][1] << 1;
0216         RAW(row + 1, col + 1) = pixel[col + 1][1] << 1;
0217         RAW(row + 0, col + 1) = pixel[col][0] + pixel[col + 1][0];
0218         RAW(row + 1, col + 0) = pixel[col][2] + pixel[col + 1][2];
0219       }
0220     }
0221   }
0222   catch (...)
0223   {
0224     jpeg_finish_decompress(&cinfo);
0225     jpeg_destroy_decompress(&cinfo);
0226     free(jpg_buf);
0227     throw;
0228   }
0229   jpeg_finish_decompress(&cinfo);
0230   jpeg_destroy_decompress(&cinfo);
0231   free(jpg_buf);
0232   maximum = 0xff << 1;
0233 }
0234 #endif
0235 
0236 void LibRaw::kodak_dc120_load_raw()
0237 {
0238   static const int mul[4] = {162, 192, 187, 92};
0239   static const int add[4] = {0, 636, 424, 212};
0240   uchar pixel[848];
0241   int row, shift, col;
0242 
0243   for (row = 0; row < height; row++)
0244   {
0245     checkCancel();
0246     if (fread(pixel, 1, 848, ifp) < 848)
0247       derror();
0248     shift = row * mul[row & 3] + add[row & 3];
0249     for (col = 0; col < width; col++)
0250       RAW(row, col) = (ushort)pixel[(col + shift) % 848];
0251   }
0252   maximum = 0xff;
0253 }
0254 void LibRaw::kodak_c330_load_raw()
0255 {
0256   if (!image)
0257     throw LIBRAW_EXCEPTION_IO_CORRUPT;
0258   int row, col, y, cb, cr, rgb[3], c;
0259 
0260   std::vector<uchar> pixel(raw_width*2 + 4);
0261 
0262   for (row = 0; row < height; row++)
0263   {
0264       checkCancel();
0265       if (fread(pixel.data(), raw_width, 2, ifp) < 2)
0266           derror();
0267       if (load_flags && (row & 31) == 31)
0268           fseek(ifp, raw_width * 32, SEEK_CUR);
0269       for (col = 0; col < width; col++)
0270       {
0271           y = pixel[col * 2];
0272           cb = pixel[(col * 2 & -4) | 1] - 128;
0273           cr = pixel[(col * 2 & -4) | 3] - 128;
0274           rgb[1] = y - ((cb + cr + 2) >> 2);
0275           rgb[2] = rgb[1] + cb;
0276           rgb[0] = rgb[1] + cr;
0277           FORC3 image[row * width + col][c] = curve[LIM(rgb[c], 0, 255)];
0278       }
0279   }
0280   maximum = curve[0xff];
0281 }
0282 
0283 void LibRaw::kodak_c603_load_raw()
0284 {
0285   if (!image)
0286     throw LIBRAW_EXCEPTION_IO_CORRUPT;
0287   int row, col, y, cb, cr, rgb[3], c;
0288 
0289   std::vector<uchar> pixel(raw_width * 3);
0290   for (row = 0; row < height; row++)
0291   {
0292       checkCancel();
0293       if (~row & 1)
0294           if (fread(pixel.data(), raw_width, 3, ifp) < 3)
0295               derror();
0296       for (col = 0; col < width; col++)
0297       {
0298           y = pixel[width * 2 * (row & 1) + col];
0299           cb = pixel[width + (col & -2)] - 128;
0300           cr = pixel[width + (col & -2) + 1] - 128;
0301           rgb[1] = y - ((cb + cr + 2) >> 2);
0302           rgb[2] = rgb[1] + cb;
0303           rgb[0] = rgb[1] + cr;
0304           FORC3 image[row * width + col][c] = curve[LIM(rgb[c], 0, 255)];
0305       }
0306   }
0307   maximum = curve[0xff];
0308 }
0309 
0310 void LibRaw::kodak_262_load_raw()
0311 {
0312   static const uchar kodak_tree[2][26] = {
0313       {0, 1, 5, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0,
0314        0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
0315       {0, 3, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0,
0316        0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
0317   ushort *huff[2];
0318   int *strip, ns, c, row, col, chess, pi = 0, pi1, pi2, pred, val;
0319 
0320   FORC(2) huff[c] = make_decoder(kodak_tree[c]);
0321   ns = (raw_height + 63) >> 5;
0322   std::vector<uchar> pixel(raw_width * 32 + ns * 4);
0323   strip = (int *)(pixel.data() + raw_width * 32);
0324   order = 0x4d4d;
0325   FORC(ns) strip[c] = get4();
0326   try
0327   {
0328     for (row = 0; row < raw_height; row++)
0329     {
0330       checkCancel();
0331       if ((row & 31) == 0)
0332       {
0333         fseek(ifp, strip[row >> 5], SEEK_SET);
0334         getbits(-1);
0335         pi = 0;
0336       }
0337       for (col = 0; col < raw_width; col++)
0338       {
0339         chess = (row + col) & 1;
0340         pi1 = chess ? pi - 2 : pi - raw_width - 1;
0341         pi2 = chess ? pi - 2 * raw_width : pi - raw_width + 1;
0342         if (col <= chess)
0343           pi1 = -1;
0344         if (pi1 < 0)
0345           pi1 = pi2;
0346         if (pi2 < 0)
0347           pi2 = pi1;
0348         if (pi1 < 0 && col > 1)
0349           pi1 = pi2 = pi - 2;
0350         pred = (pi1 < 0) ? 0 : (pixel[pi1] + pixel[pi2]) >> 1;
0351         pixel[pi] = val = pred + ljpeg_diff(huff[chess]);
0352         if (val >> 8)
0353           derror();
0354         val = curve[pixel[pi++]];
0355         RAW(row, col) = val;
0356       }
0357     }
0358   }
0359   catch (...)
0360   {
0361       FORC(2) free(huff[c]);
0362       throw;
0363   }
0364   FORC(2) free(huff[c]);
0365 }
0366 
0367 int LibRaw::kodak_65000_decode(short *out, int bsize)
0368 {
0369   uchar c, blen[768];
0370   ushort raw[6];
0371   INT64 bitbuf = 0;
0372   int save, bits = 0, i, j, len, diff;
0373 
0374   save = ftell(ifp);
0375   bsize = (bsize + 3) & -4;
0376   for (i = 0; i < bsize; i += 2)
0377   {
0378     c = fgetc(ifp);
0379     if ((blen[i] = c & 15) > 12 || (blen[i + 1] = c >> 4) > 12)
0380     {
0381       fseek(ifp, save, SEEK_SET);
0382       for (i = 0; i < bsize; i += 8)
0383       {
0384         read_shorts(raw, 6);
0385         out[i] = raw[0] >> 12 << 8 | raw[2] >> 12 << 4 | raw[4] >> 12;
0386         out[i + 1] = raw[1] >> 12 << 8 | raw[3] >> 12 << 4 | raw[5] >> 12;
0387         for (j = 0; j < 6; j++)
0388           out[i + 2 + j] = raw[j] & 0xfff;
0389       }
0390       return 1;
0391     }
0392   }
0393   if ((bsize & 7) == 4)
0394   {
0395     bitbuf = fgetc(ifp) << 8;
0396     bitbuf += fgetc(ifp);
0397     bits = 16;
0398   }
0399   for (i = 0; i < bsize; i++)
0400   {
0401     len = blen[i];
0402     if (bits < len)
0403     {
0404       for (j = 0; j < 32; j += 8)
0405         bitbuf += (INT64)fgetc(ifp) << (bits + (j ^ 8));
0406       bits += 32;
0407     }
0408     diff = bitbuf & (0xffff >> (16 - len));
0409     bitbuf >>= len;
0410     bits -= len;
0411     if (len > 0 && (diff & (1 << (len - 1))) == 0)
0412       diff -= (1 << len) - 1;
0413     out[i] = diff;
0414   }
0415   return 0;
0416 }
0417 
0418 void LibRaw::kodak_65000_load_raw()
0419 {
0420   short buf[272]; /* 264 looks enough */
0421   int row, col, len, pred[2], ret, i;
0422 
0423   for (row = 0; row < height; row++)
0424   {
0425     checkCancel();
0426     for (col = 0; col < width; col += 256)
0427     {
0428       pred[0] = pred[1] = 0;
0429       len = MIN(256, width - col);
0430       ret = kodak_65000_decode(buf, len);
0431       for (i = 0; i < len; i++)
0432       {
0433         int idx = ret ? buf[i] : (pred[i & 1] += buf[i]);
0434         if (idx >= 0 && idx < 0xffff)
0435         {
0436           if ((RAW(row, col + i) = curve[idx]) >> 12)
0437             derror();
0438         }
0439         else
0440           derror();
0441       }
0442     }
0443   }
0444 }
0445 
0446 void LibRaw::kodak_ycbcr_load_raw()
0447 {
0448   if (!image)
0449     throw LIBRAW_EXCEPTION_IO_CORRUPT;
0450   short buf[384], *bp;
0451   int row, col, len, c, i, j, k, y[2][2], cb, cr, rgb[3];
0452   ushort *ip;
0453 
0454   unsigned int bits =
0455       (load_flags && load_flags > 9 && load_flags < 17) ? load_flags : 10;
0456   const int pixels = int(width)*int(height);
0457   for (row = 0; row < height; row += 2)
0458   {
0459     checkCancel();
0460     for (col = 0; col < width; col += 128)
0461     {
0462       len = MIN(128, width - col);
0463       kodak_65000_decode(buf, len * 3);
0464       y[0][1] = y[1][1] = cb = cr = 0;
0465       for (bp = buf, i = 0; i < len; i += 2, bp += 2)
0466       {
0467         cb += bp[4];
0468         cr += bp[5];
0469         rgb[1] = -((cb + cr + 2) >> 2);
0470         rgb[2] = rgb[1] + cb;
0471         rgb[0] = rgb[1] + cr;
0472         for (j = 0; j < 2; j++)
0473           for (k = 0; k < 2; k++)
0474           {
0475             if ((y[j][k] = y[j][k ^ 1] + *bp++) >> bits)
0476               derror();
0477             int indx = (row + j) * width + col + i + k;
0478             if(indx>=0 && indx < pixels)
0479             {
0480                 ip = image[indx];
0481                 FORC3 ip[c] = curve[LIM(y[j][k] + rgb[c], 0, 0xfff)];
0482             }
0483           }
0484       }
0485     }
0486   }
0487 }
0488 
0489 void LibRaw::kodak_rgb_load_raw()
0490 {
0491   if (!image)
0492     throw LIBRAW_EXCEPTION_IO_CORRUPT;
0493   short buf[768], *bp;
0494   int row, col, len, c, i, rgb[3], ret;
0495   ushort *ip = image[0];
0496 
0497   for (row = 0; row < height; row++)
0498   {
0499     checkCancel();
0500     for (col = 0; col < width; col += 256)
0501     {
0502       len = MIN(256, width - col);
0503       ret = kodak_65000_decode(buf, len * 3);
0504       memset(rgb, 0, sizeof rgb);
0505       for (bp = buf, i = 0; i < len; i++, ip += 4)
0506         if (load_flags == 12)
0507           FORC3 ip[c] = ret ? (*bp++) : (rgb[c] += *bp++);
0508         else
0509           FORC3 if ((ip[c] = ret ? (*bp++) : (rgb[c] += *bp++)) >> 12) derror();
0510     }
0511   }
0512 }
0513 
0514 void LibRaw::kodak_thumb_load_raw()
0515 {
0516   if (!image)
0517     throw LIBRAW_EXCEPTION_IO_CORRUPT;
0518   int row, col;
0519   colors = thumb_misc >> 5;
0520   for (row = 0; row < height; row++)
0521     for (col = 0; col < width; col++)
0522       read_shorts(image[row * width + col], colors);
0523   maximum = (1 << (thumb_misc & 31)) - 1;
0524 }