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

0001 /* -*- C++ -*-
0002  * Copyright 2019-2022 LibRaw LLC (info@libraw.org)
0003  *
0004  * PhaseOne IIQ-Sv2 decoder is inspired by code provided by Daniel Vogelbacher <daniel@chaospixel.com>
0005  *
0006 
0007  LibRaw is free software; you can redistribute it and/or modify
0008  it under the terms of the one of two licenses as you choose:
0009 
0010 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
0011    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
0012 
0013 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
0014    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
0015 
0016  */
0017 
0018 #include "../../internal/libraw_cxx_defs.h"
0019 #include <vector>
0020 #include <algorithm> // for std::sort
0021 
0022 void LibRaw::sony_arq_load_raw()
0023 {
0024   int row, col;
0025   if (imgdata.idata.filters || imgdata.idata.colors < 3)
0026       throw LIBRAW_EXCEPTION_IO_CORRUPT;
0027 
0028   read_shorts(imgdata.rawdata.raw_image,
0029               imgdata.sizes.raw_width * imgdata.sizes.raw_height * 4);
0030   libraw_internal_data.internal_data.input->seek(
0031       -2, SEEK_CUR); // avoid wrong eof error
0032 
0033   if(imgdata.rawparams.options & LIBRAW_RAWOPTIONS_ARQ_SKIP_CHANNEL_SWAP)
0034     return;
0035 
0036   for (row = 0; row < imgdata.sizes.raw_height; row++)
0037   {
0038     unsigned short(*rowp)[4] =
0039         (unsigned short(*)[4]) &
0040         imgdata.rawdata.raw_image[row * imgdata.sizes.raw_width * 4];
0041     for (col = 0; col < imgdata.sizes.raw_width; col++)
0042     {
0043       unsigned short g2 = rowp[col][2];
0044       rowp[col][2] = rowp[col][3];
0045       rowp[col][3] = g2;
0046       if (((unsigned)(row - imgdata.sizes.top_margin) < imgdata.sizes.height) &&
0047           ((unsigned)(col - imgdata.sizes.left_margin) < imgdata.sizes.width) &&
0048           (MAX(MAX(rowp[col][0], rowp[col][1]),
0049                MAX(rowp[col][2], rowp[col][3])) > imgdata.color.maximum))
0050         derror();
0051     }
0052   }
0053 }
0054 
0055 void LibRaw::pentax_4shot_load_raw()
0056 {
0057   ushort *plane = (ushort *)malloc(imgdata.sizes.raw_width *
0058                                    imgdata.sizes.raw_height * sizeof(ushort));
0059   int alloc_sz = imgdata.sizes.raw_width * (imgdata.sizes.raw_height + 16) * 4 *
0060                  sizeof(ushort);
0061   ushort(*result)[4] = (ushort(*)[4])malloc(alloc_sz);
0062   struct movement_t
0063   {
0064     int row, col;
0065   } _move[4] = {
0066       {1, 1},
0067       {0, 1},
0068       {0, 0},
0069       {1, 0},
0070   };
0071 
0072   int tidx = 0;
0073   for (int i = 0; i < 4; i++)
0074   {
0075     int move_row, move_col;
0076     if (imgdata.rawparams.p4shot_order[i] >= '0' &&
0077         imgdata.rawparams.p4shot_order[i] <= '3')
0078     {
0079       move_row = ((imgdata.rawparams.p4shot_order[i] - '0') & 2) ? 1 : 0;
0080       move_col = ((imgdata.rawparams.p4shot_order[i] - '0') & 1) ? 1 : 0;
0081     }
0082     else
0083     {
0084       move_row = _move[i].row;
0085       move_col = _move[i].col;
0086     }
0087     for (; tidx < 16; tidx++)
0088       if (tiff_ifd[tidx].t_width == imgdata.sizes.raw_width &&
0089           tiff_ifd[tidx].t_height == imgdata.sizes.raw_height &&
0090           tiff_ifd[tidx].bps > 8 && tiff_ifd[tidx].samples == 1)
0091         break;
0092     if (tidx >= 16)
0093       break;
0094     imgdata.rawdata.raw_image = plane;
0095     ID.input->seek(tiff_ifd[tidx].offset, SEEK_SET);
0096     imgdata.idata.filters = 0xb4b4b4b4;
0097     libraw_internal_data.unpacker_data.data_offset = tiff_ifd[tidx].offset;
0098     (this->*pentax_component_load_raw)();
0099     for (int row = 0; row < imgdata.sizes.raw_height - move_row; row++)
0100     {
0101       int colors[2];
0102       for (int c = 0; c < 2; c++)
0103         colors[c] = COLOR(row, c);
0104       ushort *srcrow = &plane[imgdata.sizes.raw_width * row];
0105       ushort(*dstrow)[4] =
0106           &result[(imgdata.sizes.raw_width) * (row + move_row) + move_col];
0107       for (int col = 0; col < imgdata.sizes.raw_width - move_col; col++)
0108         dstrow[col][colors[col % 2]] = srcrow[col];
0109     }
0110     tidx++;
0111   }
0112 
0113   if (imgdata.color.cblack[4] == 2 && imgdata.color.cblack[5] == 2)
0114     for (int c = 0; c < 4; c++)
0115       imgdata.color.cblack[FC(c / 2, c % 2)] +=
0116           imgdata.color.cblack[6 +
0117                                c / 2 % imgdata.color.cblack[4] *
0118                                    imgdata.color.cblack[5] +
0119                                c % 2 % imgdata.color.cblack[5]];
0120   imgdata.color.cblack[4] = imgdata.color.cblack[5] = 0;
0121 
0122   // assign things back:
0123   imgdata.sizes.raw_pitch = imgdata.sizes.raw_width * 8;
0124   imgdata.idata.filters = 0;
0125   imgdata.rawdata.raw_alloc = imgdata.rawdata.color4_image = result;
0126   free(plane);
0127   imgdata.rawdata.raw_image = 0;
0128 }
0129 
0130 void LibRaw::hasselblad_full_load_raw()
0131 {
0132   int row, col;
0133 
0134   for (row = 0; row < S.height; row++)
0135     for (col = 0; col < S.width; col++)
0136     {
0137       read_shorts(&imgdata.image[row * S.width + col][2], 1); // B
0138       read_shorts(&imgdata.image[row * S.width + col][1], 1); // G
0139       read_shorts(&imgdata.image[row * S.width + col][0], 1); // R
0140     }
0141 }
0142 
0143 static inline void unpack7bytesto4x16(unsigned char *src, unsigned short *dest)
0144 {
0145   dest[0] = (src[0] << 6) | (src[1] >> 2);
0146   dest[1] = ((src[1] & 0x3) << 12) | (src[2] << 4) | (src[3] >> 4);
0147   dest[2] = (src[3] & 0xf) << 10 | (src[4] << 2) | (src[5] >> 6);
0148   dest[3] = ((src[5] & 0x3f) << 8) | src[6];
0149 }
0150 
0151 static inline void unpack28bytesto16x16ns(unsigned char *src,
0152                                           unsigned short *dest)
0153 {
0154   dest[0] = (src[3] << 6) | (src[2] >> 2);
0155   dest[1] = ((src[2] & 0x3) << 12) | (src[1] << 4) | (src[0] >> 4);
0156   dest[2] = (src[0] & 0xf) << 10 | (src[7] << 2) | (src[6] >> 6);
0157   dest[3] = ((src[6] & 0x3f) << 8) | src[5];
0158   dest[4] = (src[4] << 6) | (src[11] >> 2);
0159   dest[5] = ((src[11] & 0x3) << 12) | (src[10] << 4) | (src[9] >> 4);
0160   dest[6] = (src[9] & 0xf) << 10 | (src[8] << 2) | (src[15] >> 6);
0161   dest[7] = ((src[15] & 0x3f) << 8) | src[14];
0162   dest[8] = (src[13] << 6) | (src[12] >> 2);
0163   dest[9] = ((src[12] & 0x3) << 12) | (src[19] << 4) | (src[18] >> 4);
0164   dest[10] = (src[18] & 0xf) << 10 | (src[17] << 2) | (src[16] >> 6);
0165   dest[11] = ((src[16] & 0x3f) << 8) | src[23];
0166   dest[12] = (src[22] << 6) | (src[21] >> 2);
0167   dest[13] = ((src[21] & 0x3) << 12) | (src[20] << 4) | (src[27] >> 4);
0168   dest[14] = (src[27] & 0xf) << 10 | (src[26] << 2) | (src[25] >> 6);
0169   dest[15] = ((src[25] & 0x3f) << 8) | src[24];
0170 }
0171 
0172 #define swab32(x)                                                              \
0173   ((unsigned int)((((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) |   \
0174                   (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) |    \
0175                   (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) |    \
0176                   (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24)))
0177 
0178 static inline void swab32arr(unsigned *arr, unsigned len)
0179 {
0180   for (unsigned i = 0; i < len; i++)
0181     arr[i] = swab32(arr[i]);
0182 }
0183 #undef swab32
0184 
0185 static inline void unpack7bytesto4x16_nikon(unsigned char *src,
0186                                             unsigned short *dest)
0187 {
0188   dest[3] = (src[6] << 6) | (src[5] >> 2);
0189   dest[2] = ((src[5] & 0x3) << 12) | (src[4] << 4) | (src[3] >> 4);
0190   dest[1] = (src[3] & 0xf) << 10 | (src[2] << 2) | (src[1] >> 6);
0191   dest[0] = ((src[1] & 0x3f) << 8) | src[0];
0192 }
0193 
0194 void LibRaw::nikon_14bit_load_raw()
0195 {
0196   const unsigned linelen =
0197       (unsigned)(ceilf((float)(S.raw_width * 7 / 4) / 16.0)) *
0198       16; // 14512; // S.raw_width * 7 / 4;
0199   const unsigned pitch = S.raw_pitch ? S.raw_pitch / 2 : S.raw_width;
0200   unsigned char *buf = (unsigned char *)malloc(linelen);
0201   for (int row = 0; row < S.raw_height; row++)
0202   {
0203     unsigned bytesread =
0204         libraw_internal_data.internal_data.input->read(buf, 1, linelen);
0205     unsigned short *dest = &imgdata.rawdata.raw_image[pitch * row];
0206     // swab32arr((unsigned *)buf, bytesread / 4);
0207     for (unsigned int sp = 0, dp = 0;
0208          dp < pitch - 3 && sp < linelen - 6 && sp < bytesread - 6;
0209          sp += 7, dp += 4)
0210       unpack7bytesto4x16_nikon(buf + sp, dest + dp);
0211   }
0212   free(buf);
0213 }
0214 
0215 void LibRaw::fuji_14bit_load_raw()
0216 {
0217   const unsigned linelen = S.raw_width * 7 / 4;
0218   const unsigned pitch = S.raw_pitch ? S.raw_pitch / 2 : S.raw_width;
0219   unsigned char *buf = (unsigned char *)malloc(linelen);
0220 
0221   for (int row = 0; row < S.raw_height; row++)
0222   {
0223     unsigned bytesread =
0224         libraw_internal_data.internal_data.input->read(buf, 1, linelen);
0225     unsigned short *dest = &imgdata.rawdata.raw_image[pitch * row];
0226     if (bytesread % 28)
0227     {
0228       swab32arr((unsigned *)buf, bytesread / 4);
0229       for (unsigned int sp = 0, dp = 0;
0230            dp < pitch - 3 && sp < linelen - 6 && sp < bytesread - 6;
0231            sp += 7, dp += 4)
0232         unpack7bytesto4x16(buf + sp, dest + dp);
0233     }
0234     else
0235       for (unsigned int sp = 0, dp = 0;
0236            dp < pitch - 15 && sp < linelen - 27 && sp < bytesread - 27;
0237            sp += 28, dp += 16)
0238         unpack28bytesto16x16ns(buf + sp, dest + dp);
0239   }
0240   free(buf);
0241 }
0242 void LibRaw::nikon_load_padded_packed_raw() // 12 bit per pixel, padded to 16
0243                                             // bytes
0244 {
0245   // libraw_internal_data.unpacker_data.load_flags -> row byte count
0246   if (libraw_internal_data.unpacker_data.load_flags < 2000 ||
0247       libraw_internal_data.unpacker_data.load_flags > 64000)
0248     return;
0249   unsigned char *buf =
0250       (unsigned char *)malloc(libraw_internal_data.unpacker_data.load_flags);
0251   for (int row = 0; row < S.raw_height; row++)
0252   {
0253     checkCancel();
0254     libraw_internal_data.internal_data.input->read(
0255         buf, libraw_internal_data.unpacker_data.load_flags, 1);
0256     for (int icol = 0; icol < S.raw_width / 2; icol++)
0257     {
0258       imgdata.rawdata.raw_image[(row)*S.raw_width + (icol * 2)] =
0259           ((buf[icol * 3 + 1] & 0xf) << 8) | buf[icol * 3];
0260       imgdata.rawdata.raw_image[(row)*S.raw_width + (icol * 2 + 1)] =
0261           buf[icol * 3 + 2] << 4 | ((buf[icol * 3 + 1] & 0xf0) >> 4);
0262     }
0263   }
0264   free(buf);
0265 }
0266 
0267 void LibRaw::nikon_load_striped_packed_raw()
0268 {
0269   int vbits = 0, bwide, rbits, bite, row, col, i;
0270 
0271   UINT64 bitbuf = 0;
0272   unsigned load_flags = 24; // libraw_internal_data.unpacker_data.load_flags;
0273   unsigned tiff_bps = libraw_internal_data.unpacker_data.tiff_bps;
0274 
0275   struct tiff_ifd_t *ifd = &tiff_ifd[0];
0276   while (ifd < &tiff_ifd[libraw_internal_data.identify_data.tiff_nifds] &&
0277          ifd->offset != libraw_internal_data.unpacker_data.data_offset)
0278     ++ifd;
0279   if (ifd == &tiff_ifd[libraw_internal_data.identify_data.tiff_nifds])
0280     throw LIBRAW_EXCEPTION_DECODE_RAW;
0281 
0282   if (!ifd->rows_per_strip || !ifd->strip_offsets_count)
0283     return; // not unpacked
0284   int stripcnt = 0;
0285 
0286   bwide = S.raw_width * tiff_bps / 8;
0287   bwide += bwide & load_flags >> 7;
0288   rbits = bwide * 8 - S.raw_width * tiff_bps;
0289   if (load_flags & 1)
0290     bwide = bwide * 16 / 15;
0291   bite = 8 + (load_flags & 24);
0292   for (row = 0; row < S.raw_height; row++)
0293   {
0294     checkCancel();
0295     if (!(row % ifd->rows_per_strip))
0296     {
0297       if (stripcnt >= ifd->strip_offsets_count)
0298         return; // run out of data
0299       libraw_internal_data.internal_data.input->seek(
0300           ifd->strip_offsets[stripcnt], SEEK_SET);
0301       stripcnt++;
0302     }
0303     for (col = 0; col < S.raw_width; col++)
0304     {
0305       for (vbits -= tiff_bps; vbits < 0; vbits += bite)
0306       {
0307         bitbuf <<= bite;
0308         for (i = 0; i < bite; i += 8)
0309           bitbuf |=
0310               (unsigned)(libraw_internal_data.internal_data.input->get_char()
0311                          << i);
0312       }
0313       imgdata.rawdata.raw_image[(row)*S.raw_width + (col)] =
0314           bitbuf << (64 - tiff_bps - vbits) >> (64 - tiff_bps);
0315     }
0316     vbits -= rbits;
0317   }
0318 }
0319 
0320 struct pana_cs6_page_decoder
0321 {
0322   unsigned int pixelbuffer[18], lastoffset, maxoffset;
0323   unsigned char current, *buffer;
0324   pana_cs6_page_decoder(unsigned char *_buffer, unsigned int bsize)
0325       : lastoffset(0), maxoffset(bsize), current(0), buffer(_buffer)
0326   {
0327   }
0328   void read_page(); // will throw IO error if not enough space in buffer
0329   void read_page12(); // 12-bit variant
0330   unsigned int nextpixel() { return current < 14 ? pixelbuffer[current++] : 0; }
0331   unsigned int nextpixel12() { return current < 18 ? pixelbuffer[current++] : 0; }
0332 };
0333 
0334 void pana_cs6_page_decoder::read_page()
0335 {
0336   if (!buffer || (maxoffset - lastoffset < 16))
0337     throw LIBRAW_EXCEPTION_IO_EOF;
0338 #define wbuffer(i) ((unsigned short)buffer[lastoffset + 15 - i])
0339   pixelbuffer[0] = (wbuffer(0) << 6) | (wbuffer(1) >> 2);                                         // 14 bit
0340   pixelbuffer[1] = (((wbuffer(1) & 0x3) << 12) | (wbuffer(2) << 4) | (wbuffer(3) >> 4)) & 0x3fff; // 14 bit
0341   pixelbuffer[2] = (wbuffer(3) >> 2) & 0x3;                                                       // 2
0342   pixelbuffer[3] = ((wbuffer(3) & 0x3) << 8) | wbuffer(4);                                        // 10
0343   pixelbuffer[4] = (wbuffer(5) << 2) | (wbuffer(6) >> 6);                                         // 10
0344   pixelbuffer[5] = ((wbuffer(6) & 0x3f) << 4) | (wbuffer(7) >> 4);                                // 10
0345   pixelbuffer[6] = (wbuffer(7) >> 2) & 0x3;
0346   pixelbuffer[7] = ((wbuffer(7) & 0x3) << 8) | wbuffer(8);
0347   pixelbuffer[8] = ((wbuffer(9) << 2) & 0x3fc) | (wbuffer(10) >> 6);
0348   pixelbuffer[9] = ((wbuffer(10) << 4) | (wbuffer(11) >> 4)) & 0x3ff;
0349   pixelbuffer[10] = (wbuffer(11) >> 2) & 0x3;
0350   pixelbuffer[11] = ((wbuffer(11) & 0x3) << 8) | wbuffer(12);
0351   pixelbuffer[12] = (((wbuffer(13) << 2) & 0x3fc) | wbuffer(14) >> 6) & 0x3ff;
0352   pixelbuffer[13] = ((wbuffer(14) << 4) | (wbuffer(15) >> 4)) & 0x3ff;
0353 #undef wbuffer
0354   current = 0;
0355   lastoffset += 16;
0356 }
0357 
0358 void pana_cs6_page_decoder::read_page12()
0359 {
0360   if (!buffer || (maxoffset - lastoffset < 16))
0361     throw LIBRAW_EXCEPTION_IO_EOF;
0362 #define wb(i) ((unsigned short)buffer[lastoffset + 15 - i])
0363   pixelbuffer[0] = (wb(0) << 4) | (wb(1) >> 4);              // 12 bit: 8/0 + 4 upper bits of /1
0364   pixelbuffer[1] = (((wb(1) & 0xf) << 8) | (wb(2))) & 0xfff; // 12 bit: 4l/1 + 8/2
0365   
0366   pixelbuffer[2] = (wb(3) >> 6) & 0x3;                       // 2; 2u/3, 6 low bits remains in wb(3) 
0367   pixelbuffer[3] = ((wb(3) & 0x3f) << 2) | (wb(4) >> 6);     // 8; 6l/3 + 2u/4; 6 low bits remains in wb(4)
0368   pixelbuffer[4] = ((wb(4) & 0x3f) << 2) | (wb(5) >> 6);     // 8: 6l/4 + 2u/5; 6 low bits remains in wb(5)
0369   pixelbuffer[5] = ((wb(5) & 0x3f) << 2) | (wb(6) >> 6);     // 8: 6l/5 + 2u/6, 6 low bits remains in wb(6)
0370 
0371   pixelbuffer[6] = (wb(6) >> 4) & 0x3;                       // 2, 4 low bits remains in wb(6)
0372   pixelbuffer[7] = ((wb(6) & 0xf) << 4) | (wb(7) >> 4);      // 8: 4 low bits from wb(6), 4 upper bits from wb(7)
0373   pixelbuffer[8] = ((wb(7) & 0xf) << 4) | (wb(8) >> 4);      // 8: 4 low bits from wb7, 4 upper bits from wb8
0374   pixelbuffer[9] = ((wb(8) & 0xf) << 4) | (wb(9) >> 4);      // 8: 4 low bits from wb8, 4 upper bits from wb9
0375 
0376   pixelbuffer[10] = (wb(9) >> 2) & 0x3;                      // 2: bits 2-3 from wb9, two low bits remain in wb9
0377   pixelbuffer[11] = ((wb(9) & 0x3) << 6) | (wb(10) >> 2);    // 8: 2 bits from wb9, 6 bits from wb10
0378   pixelbuffer[12] = ((wb(10) & 0x3) << 6) | (wb(11) >> 2);   // 8: 2 bits from wb10, 6 bits from wb11
0379   pixelbuffer[13] = ((wb(11) & 0x3) << 6) | (wb(12) >> 2);   // 8: 2 bits from wb11, 6 bits from wb12
0380 
0381   pixelbuffer[14] = wb(12) & 0x3;                            // 2: low bits from wb12
0382   pixelbuffer[15] = wb(13);
0383   pixelbuffer[16] = wb(14);
0384   pixelbuffer[17] = wb(15);
0385 #undef wb
0386   current = 0;
0387   lastoffset += 16;
0388 }
0389 
0390 void LibRaw::panasonicC6_load_raw()
0391 {
0392   const int rowstep = 16;
0393   const bool _12bit = libraw_internal_data.unpacker_data.pana_bpp == 12;
0394   const int pixperblock =  _12bit ? 14 : 11;
0395   const int blocksperrow = imgdata.sizes.raw_width / pixperblock;
0396   const int rowbytes = blocksperrow * 16;
0397   const unsigned pixelbase0 = _12bit ? 0x80 : 0x200;
0398   const unsigned pixelbase_compare = _12bit ? 0x800 : 0x2000;
0399   const unsigned spix_compare = _12bit ? 0x3fff : 0xffff;
0400   const unsigned pixel_mask = _12bit ? 0xfff : 0x3fff;
0401   std::vector<unsigned char> iobuf;
0402   try
0403   {
0404       iobuf.resize(rowbytes * rowstep);
0405   }
0406   catch (...)
0407   {
0408     throw LIBRAW_EXCEPTION_ALLOC;
0409   }
0410 
0411   for (int row = 0; row < imgdata.sizes.raw_height - rowstep + 1;
0412        row += rowstep)
0413   {
0414     int rowstoread = MIN(rowstep, imgdata.sizes.raw_height - row);
0415     if (libraw_internal_data.internal_data.input->read(
0416             iobuf.data(), rowbytes, rowstoread) != rowstoread)
0417       throw LIBRAW_EXCEPTION_IO_EOF;
0418     pana_cs6_page_decoder page(iobuf.data(), rowbytes * rowstoread);
0419     for (int crow = 0, col = 0; crow < rowstoread; crow++, col = 0)
0420     {
0421       unsigned short *rowptr =
0422           &imgdata.rawdata
0423                .raw_image[(row + crow) * imgdata.sizes.raw_pitch / 2];
0424       for (int rblock = 0; rblock < blocksperrow; rblock++)
0425       {
0426           if (_12bit)
0427               page.read_page12();
0428           else
0429               page.read_page();
0430         unsigned oddeven[2] = {0, 0}, nonzero[2] = {0, 0};
0431         unsigned pmul = 0, pixel_base = 0;
0432         for (int pix = 0; pix < pixperblock; pix++)
0433         {
0434           if (pix % 3 == 2)
0435           {
0436             unsigned base = _12bit ? page.nextpixel12(): page.nextpixel();
0437             if (base > 3)
0438               throw LIBRAW_EXCEPTION_IO_CORRUPT; // not possible b/c of 2-bit
0439                                                  // field, but....
0440             if (base == 3)
0441               base = 4;
0442             pixel_base = pixelbase0 << base;
0443             pmul = 1 << base;
0444           }
0445           unsigned epixel = _12bit ? page.nextpixel12() : page.nextpixel();
0446           if (oddeven[pix % 2])
0447           {
0448             epixel *= pmul;
0449             if (pixel_base < pixelbase_compare && nonzero[pix % 2] > pixel_base)
0450               epixel += nonzero[pix % 2] - pixel_base;
0451             nonzero[pix % 2] = epixel;
0452           }
0453           else
0454           {
0455             oddeven[pix % 2] = epixel;
0456             if (epixel)
0457               nonzero[pix % 2] = epixel;
0458             else
0459               epixel = nonzero[pix % 2];
0460           }
0461           unsigned spix = epixel - 0xf;
0462           if (spix <= spix_compare)
0463             rowptr[col++] = spix & spix_compare;
0464           else
0465           {
0466             epixel = (((signed int)(epixel + 0x7ffffff1)) >> 0x1f);
0467             rowptr[col++] = epixel & pixel_mask;
0468           }
0469         }
0470       }
0471     }
0472   }
0473 }
0474 
0475 
0476 void LibRaw::panasonicC7_load_raw()
0477 {
0478   const int rowstep = 16;
0479   int pixperblock = libraw_internal_data.unpacker_data.pana_bpp == 14 ? 9 : 10;
0480   int rowbytes = imgdata.sizes.raw_width / pixperblock * 16;
0481   unsigned char *iobuf = (unsigned char *)malloc(rowbytes * rowstep);
0482   for (int row = 0; row < imgdata.sizes.raw_height - rowstep + 1;
0483        row += rowstep)
0484   {
0485     int rowstoread = MIN(rowstep, imgdata.sizes.raw_height - row);
0486     if (libraw_internal_data.internal_data.input->read(
0487             iobuf, rowbytes, rowstoread) != rowstoread)
0488       throw LIBRAW_EXCEPTION_IO_EOF;
0489     unsigned char *bytes = iobuf;
0490     for (int crow = 0; crow < rowstoread; crow++)
0491     {
0492       unsigned short *rowptr =
0493           &imgdata.rawdata
0494                .raw_image[(row + crow) * imgdata.sizes.raw_pitch / 2];
0495       for (int col = 0; col < imgdata.sizes.raw_width - pixperblock + 1;
0496            col += pixperblock, bytes += 16)
0497       {
0498         if (libraw_internal_data.unpacker_data.pana_bpp == 14)
0499         {
0500           rowptr[col] = bytes[0] + ((bytes[1] & 0x3F) << 8);
0501           rowptr[col + 1] =
0502               (bytes[1] >> 6) + 4 * (bytes[2]) + ((bytes[3] & 0xF) << 10);
0503           rowptr[col + 2] =
0504               (bytes[3] >> 4) + 16 * (bytes[4]) + ((bytes[5] & 3) << 12);
0505           rowptr[col + 3] = ((bytes[5] & 0xFC) >> 2) + (bytes[6] << 6);
0506           rowptr[col + 4] = bytes[7] + ((bytes[8] & 0x3F) << 8);
0507           rowptr[col + 5] =
0508               (bytes[8] >> 6) + 4 * bytes[9] + ((bytes[10] & 0xF) << 10);
0509           rowptr[col + 6] =
0510               (bytes[10] >> 4) + 16 * bytes[11] + ((bytes[12] & 3) << 12);
0511           rowptr[col + 7] = ((bytes[12] & 0xFC) >> 2) + (bytes[13] << 6);
0512           rowptr[col + 8] = bytes[14] + ((bytes[15] & 0x3F) << 8);
0513         }
0514         else if (libraw_internal_data.unpacker_data.pana_bpp ==
0515                  12) // have not seen in the wild yet
0516         {
0517           rowptr[col] = ((bytes[1] & 0xF) << 8) + bytes[0];
0518           rowptr[col + 1] = 16 * bytes[2] + (bytes[1] >> 4);
0519           rowptr[col + 2] = ((bytes[4] & 0xF) << 8) + bytes[3];
0520           rowptr[col + 3] = 16 * bytes[5] + (bytes[4] >> 4);
0521           rowptr[col + 4] = ((bytes[7] & 0xF) << 8) + bytes[6];
0522           rowptr[col + 5] = 16 * bytes[8] + (bytes[7] >> 4);
0523           rowptr[col + 6] = ((bytes[10] & 0xF) << 8) + bytes[9];
0524           rowptr[col + 7] = 16 * bytes[11] + (bytes[10] >> 4);
0525           rowptr[col + 8] = ((bytes[13] & 0xF) << 8) + bytes[12];
0526           rowptr[col + 9] = 16 * bytes[14] + (bytes[13] >> 4);
0527         }
0528       }
0529     }
0530   }
0531   free(iobuf);
0532 }
0533 
0534 void LibRaw::unpacked_load_raw_fuji_f700s20()
0535 {
0536   int base_offset = 0;
0537   int row_size = imgdata.sizes.raw_width * 2; // in bytes
0538   if (imgdata.idata.raw_count == 2 && imgdata.rawparams.shot_select)
0539   {
0540     libraw_internal_data.internal_data.input->seek(-row_size, SEEK_CUR);
0541     base_offset = row_size; // in bytes
0542   }
0543   unsigned char *buffer = (unsigned char *)malloc(row_size * 2);
0544   for (int row = 0; row < imgdata.sizes.raw_height; row++)
0545   {
0546     read_shorts((ushort *)buffer, imgdata.sizes.raw_width * 2);
0547     memmove(&imgdata.rawdata.raw_image[row * imgdata.sizes.raw_pitch / 2],
0548             buffer + base_offset, row_size);
0549   }
0550   free(buffer);
0551 }
0552 
0553 void LibRaw::nikon_load_sraw()
0554 {
0555   // We're already seeked to data!
0556   unsigned char *rd =
0557       (unsigned char *)malloc(3 * (imgdata.sizes.raw_width + 2));
0558   if (!rd)
0559     throw LIBRAW_EXCEPTION_ALLOC;
0560   try
0561   {
0562     int row, col;
0563     for (row = 0; row < imgdata.sizes.raw_height; row++)
0564     {
0565       checkCancel();
0566       libraw_internal_data.internal_data.input->read(rd, 3,
0567                                                      imgdata.sizes.raw_width);
0568       for (col = 0; col < imgdata.sizes.raw_width - 1; col += 2)
0569       {
0570         int bi = col * 3;
0571         ushort bits1 = (rd[bi + 1] & 0xf) << 8 | rd[bi];            // 3,0,1
0572         ushort bits2 = rd[bi + 2] << 4 | ((rd[bi + 1] >> 4) & 0xf); // 452
0573         ushort bits3 = ((rd[bi + 4] & 0xf) << 8) | rd[bi + 3];      // 967
0574         ushort bits4 = rd[bi + 5] << 4 | ((rd[bi + 4] >> 4) & 0xf); // ab8
0575         imgdata.image[row * imgdata.sizes.raw_width + col][0] = bits1;
0576         imgdata.image[row * imgdata.sizes.raw_width + col][1] = bits3;
0577         imgdata.image[row * imgdata.sizes.raw_width + col][2] = bits4;
0578         imgdata.image[row * imgdata.sizes.raw_width + col + 1][0] = bits2;
0579         imgdata.image[row * imgdata.sizes.raw_width + col + 1][1] = 2048;
0580         imgdata.image[row * imgdata.sizes.raw_width + col + 1][2] = 2048;
0581       }
0582     }
0583   }
0584   catch (...)
0585   {
0586     free(rd);
0587     throw;
0588   }
0589   free(rd);
0590   C.maximum = 0xfff; // 12 bit?
0591   if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE)
0592   {
0593     return; // no CbCr interpolation
0594   }
0595   // Interpolate CC channels
0596   int row, col;
0597   for (row = 0; row < imgdata.sizes.raw_height; row++)
0598   {
0599     checkCancel(); // will throw out
0600     for (col = 0; col < imgdata.sizes.raw_width; col += 2)
0601     {
0602       int col2 = col < imgdata.sizes.raw_width - 2 ? col + 2 : col;
0603       imgdata.image[row * imgdata.sizes.raw_width + col + 1][1] =
0604           (unsigned short)(int(imgdata.image[row * imgdata.sizes.raw_width +
0605                                              col][1] +
0606                                imgdata.image[row * imgdata.sizes.raw_width +
0607                                              col2][1]) /
0608                            2);
0609       imgdata.image[row * imgdata.sizes.raw_width + col + 1][2] =
0610           (unsigned short)(int(imgdata.image[row * imgdata.sizes.raw_width +
0611                                              col][2] +
0612                                imgdata.image[row * imgdata.sizes.raw_width +
0613                                              col2][2]) /
0614                            2);
0615     }
0616   }
0617   if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_RGB)
0618     return;
0619 
0620   for (row = 0; row < imgdata.sizes.raw_height; row++)
0621   {
0622     checkCancel(); // will throw out
0623     for (col = 0; col < imgdata.sizes.raw_width; col++)
0624     {
0625       float Y =
0626           float(imgdata.image[row * imgdata.sizes.raw_width + col][0]) / 2549.f;
0627       float Ch2 =
0628           float(imgdata.image[row * imgdata.sizes.raw_width + col][1] - 1280) /
0629           1536.f;
0630       float Ch3 =
0631           float(imgdata.image[row * imgdata.sizes.raw_width + col][2] - 1280) /
0632           1536.f;
0633       if (Y > 1.f)
0634         Y = 1.f;
0635       if (Y > 0.803f)
0636         Ch2 = Ch3 = 0.5f;
0637       float r = Y + 1.40200f * (Ch3 - 0.5f);
0638       if (r < 0.f)
0639         r = 0.f;
0640       if (r > 1.f)
0641         r = 1.f;
0642       float g = Y - 0.34414f * (Ch2 - 0.5f) - 0.71414 * (Ch3 - 0.5f);
0643       if (g > 1.f)
0644         g = 1.f;
0645       if (g < 0.f)
0646         g = 0.f;
0647       float b = Y + 1.77200 * (Ch2 - 0.5f);
0648       if (b > 1.f)
0649         b = 1.f;
0650       if (b < 0.f)
0651         b = 0.f;
0652       imgdata.image[row * imgdata.sizes.raw_width + col][0] =
0653           imgdata.color.curve[int(r * 3072.f)];
0654       imgdata.image[row * imgdata.sizes.raw_width + col][1] =
0655           imgdata.color.curve[int(g * 3072.f)];
0656       imgdata.image[row * imgdata.sizes.raw_width + col][2] =
0657           imgdata.color.curve[int(b * 3072.f)];
0658     }
0659   }
0660   C.maximum = 16383;
0661 }
0662 
0663 /*
0664  Each row is decoded independently. Each row starts with a 16 bit prefix.
0665  The hi byte is zero, the lo byte (first 3 bits) indicates a bit_base.
0666  Other bits remain unused.
0667 
0668  |0000 0000|0000 0XXX| => XXX is bit_base
0669 
0670  After the prefix the pixel data starts. Pixels are grouped into clusters
0671  forming 8 output pixel. Each cluster starts with a variable length of
0672  bits, indicating decompression flags.
0673 
0674 
0675 */
0676 
0677 #undef MIN
0678 #undef MAX
0679 #undef LIM
0680 
0681 #define MIN(a, b) ((a) < (b) ? (a) : (b))
0682 #define MAX(a, b) ((a) > (b) ? (a) : (b))
0683 #define LIM(x, min, max) MAX(min, MIN(x, max))
0684 
0685 struct iiq_bitstream_t
0686 {
0687     uint64_t curr;
0688     uint32_t *input;
0689     uint8_t used;
0690     iiq_bitstream_t(uint32_t *img_input): curr(0),input(img_input),used(0){}
0691 
0692     void fill()
0693     {
0694       if (used <= 32)
0695       {
0696         uint64_t bitpump_next = *input++;
0697         curr = (curr << 32) | bitpump_next;
0698         used += 32;
0699       }
0700     }
0701     uint64_t peek(uint8_t len)
0702     {
0703       if (len >= used)
0704         fill();
0705 
0706       uint64_t res = curr >> (used - len);
0707       return res & ((1 << (uint8_t)len) - 1);
0708     }
0709 
0710     void consume(uint8_t len)
0711     {
0712       peek(len); // fill buffer if needed
0713       used -= len;
0714     }
0715 
0716     uint64_t get(char len)
0717     {
0718       uint64_t val = peek(len);
0719       consume(len);
0720       return val;
0721     }
0722 
0723 };
0724 
0725 void decode_S_type(int32_t out_width, uint32_t *img_input, ushort *outbuf /*, int bit_depth*/)
0726 {
0727 #if 0
0728     if (((bit_depth - 12) & 0xFFFFFFFD) != 0)
0729         return 0;
0730 #endif
0731     iiq_bitstream_t stream(img_input);
0732 
0733     const int pix_corr_shift = 2; // 16 - bit_depth;
0734     unsigned int bit_check[2] = { 0, 0 };
0735 
0736     const uint8_t used_corr[8] = {
0737         3, 3, 3, 3, 1, 1, 1, 1,
0738     };
0739 
0740     const uint8_t extra_bits[8] = {
0741         1, 2, 3, 4, 0, 0, 0, 0,
0742     };
0743 
0744     const uint8_t bit_indicator[8 * 4] = {
0745         9, 8, 0, 7, 6, 6, 5, 5, 1, 1, 1, 1, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2,
0746     };
0747 
0748     const uint8_t skip_bits[8 * 4] = {5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
0749                                       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
0750 
0751     int block_count = ((out_width - 8) >> 3) + 1;
0752     int block_total_bytes = 8 * block_count;
0753 
0754     int32_t prev_pix_value[2] = { 0, 0 };
0755 
0756     uint8_t init_bits = stream.get(16) & 7;
0757 
0758     if (out_width - 7 > 0)
0759     {
0760       uint8_t pix_sub_init = 17 - init_bits;
0761 
0762       for (int blk_id = 0; blk_id < block_count; ++blk_id)
0763       {
0764         int8_t idx_even = stream.peek(7);
0765         stream.consume(2);
0766 
0767         if ((unsigned int)idx_even >= 32)
0768           bit_check[0] = ((unsigned int)idx_even >> 5) + bit_check[0] - 2;
0769         else
0770         {
0771           bit_check[0] = bit_indicator[idx_even];
0772           stream.consume(skip_bits[idx_even]);
0773         }
0774 
0775         int8_t idx_odd = stream.peek(7);
0776         stream.consume(2);
0777 
0778         if ((unsigned int)idx_odd >= 32)
0779           bit_check[1] = ((unsigned int)idx_odd >> 5) + bit_check[1] - 2;
0780         else
0781         {
0782           bit_check[1] = bit_indicator[idx_odd];
0783           stream.consume(skip_bits[idx_odd]);
0784         }
0785 
0786         uint8_t bidx = stream.peek(3);
0787         stream.consume(used_corr[bidx]);
0788 
0789         uint8_t take_bits = init_bits + extra_bits[bidx]; // 11 or less
0790 
0791         uint32_t bp_shift[2] = {bit_check[0] - extra_bits[bidx], bit_check[1] - extra_bits[bidx]};
0792 
0793         int pix_sub[2] = {0xFFFF >> (pix_sub_init - bit_check[0]), 0xFFFF >> (pix_sub_init - bit_check[1])};
0794 
0795         for (int i = 0; i < 8; i++) // MAIN LOOP for pixel decoding
0796         {
0797           int32_t value = 0;
0798           if (bit_check[i & 1] == 9)
0799             value = stream.get(14);
0800           else
0801             value = prev_pix_value[i & 1] + ((uint32_t)stream.get(take_bits) << bp_shift[i & 1]) - pix_sub[i & 1];
0802 
0803           outbuf[i] = LIM(value << pix_corr_shift, 0, 0xffff);
0804           prev_pix_value[i & 1] = value;
0805         }
0806           outbuf += 8; // always produce 8 pixels from this cluster
0807       }
0808     } // if width > 7                                            // End main if
0809 
0810     // Final block
0811     // maybe fill/unpack extra bytes if width % 8 <> 0?
0812     if (block_total_bytes < out_width)
0813     {
0814         do
0815         {
0816             stream.fill();
0817             uint32_t pix_value = stream.get(14);
0818             ++block_total_bytes;
0819             *outbuf++ = pix_value << pix_corr_shift;
0820         } while (block_total_bytes < out_width);
0821     }
0822 }
0823 
0824 struct p1_row_info_t
0825 {
0826     unsigned row;
0827     INT64 offset;
0828     p1_row_info_t(): row(0),offset(0){}
0829     p1_row_info_t(const p1_row_info_t& q): row(q.row),offset(q.offset){}
0830     bool operator < (const p1_row_info_t & rhs) const { return offset < rhs.offset; }
0831 };
0832 
0833 void LibRaw::phase_one_load_raw_s()
0834 {
0835     if(!libraw_internal_data.unpacker_data.strip_offset || !imgdata.rawdata.raw_image || !libraw_internal_data.unpacker_data.data_offset)
0836         throw LIBRAW_EXCEPTION_IO_CORRUPT;
0837     std::vector<p1_row_info_t> stripes(imgdata.sizes.raw_height+1);
0838     libraw_internal_data.internal_data.input->seek(libraw_internal_data.unpacker_data.strip_offset, SEEK_SET);
0839     for (unsigned row = 0; row < imgdata.sizes.raw_height; row++)
0840     {
0841         stripes[row].row = row;
0842         stripes[row].offset = INT64(get4()) + libraw_internal_data.unpacker_data.data_offset;
0843     }
0844     stripes[imgdata.sizes.raw_height].row = imgdata.sizes.raw_height;
0845     stripes[imgdata.sizes.raw_height].offset = libraw_internal_data.unpacker_data.data_offset + INT64(libraw_internal_data.unpacker_data.data_size);
0846     std::sort(stripes.begin(), stripes.end());
0847     INT64 maxsz = imgdata.sizes.raw_width * 3 + 2; // theor max: 17 bytes per 8 pix + row header
0848     std::vector<uint8_t> datavec(maxsz);
0849 
0850     for (unsigned row = 0; row < imgdata.sizes.raw_height; row++)
0851     {
0852         if (stripes[row].row >= imgdata.sizes.raw_height) continue; 
0853         ushort *datap = imgdata.rawdata.raw_image + stripes[row].row * imgdata.sizes.raw_width;
0854         libraw_internal_data.internal_data.input->seek(stripes[row].offset, SEEK_SET);
0855         INT64 readsz = stripes[row + 1].offset - stripes[row].offset;
0856         if (readsz > maxsz)
0857             throw LIBRAW_EXCEPTION_IO_CORRUPT;
0858 
0859         if(libraw_internal_data.internal_data.input->read(datavec.data(), 1, readsz) != readsz)
0860             derror(); // TODO: check read state
0861 
0862         decode_S_type(imgdata.sizes.raw_width, (uint32_t *)datavec.data(), datap /*, 14 */);
0863     }
0864 }