"Fossies" - the Fresh Open Source Software Archive

Member "darktable-2.6.3/src/external/rawspeed/src/librawspeed/decoders/NefDecoder.cpp" (19 Oct 2019, 29524 Bytes) of package /linux/misc/darktable-2.6.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "NefDecoder.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.6.2_vs_2.6.3.

    1 /*
    2     RawSpeed - RAW file decoder.
    3 
    4     Copyright (C) 2009-2014 Klaus Post
    5     Copyright (C) 2015 Pedro CĂ´rte-Real
    6 
    7     This library is free software; you can redistribute it and/or
    8     modify it under the terms of the GNU Lesser General Public
    9     License as published by the Free Software Foundation; either
   10     version 2 of the License, or (at your option) any later version.
   11 
   12     This library is distributed in the hope that it will be useful,
   13     but WITHOUT ANY WARRANTY; without even the implied warranty of
   14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15     Lesser General Public License for more details.
   16 
   17     You should have received a copy of the GNU Lesser General Public
   18     License along with this library; if not, write to the Free Software
   19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   20 */
   21 
   22 #include "decoders/NefDecoder.h"
   23 #include "common/Common.h"                          // for uint32_t, uint8_t
   24 #include "common/Point.h"                           // for iPoint2D
   25 #include "decoders/RawDecoderException.h"           // for ThrowRDE
   26 #include "decompressors/NikonDecompressor.h"        // for NikonDecompressor
   27 #include "decompressors/UncompressedDecompressor.h" // for UncompressedDeco...
   28 #include "io/BitPumpMSB.h"                          // for BitPumpMSB
   29 #include "io/Buffer.h"                              // for Buffer
   30 #include "io/ByteStream.h"                          // for ByteStream
   31 #include "io/Endianness.h"                          // for getU16BE, Endian...
   32 #include "io/IOException.h"                         // for ThrowIOE
   33 #include "metadata/Camera.h"                        // for Hints
   34 #include "metadata/CameraMetaData.h"                // for CameraMetaData
   35 #include "metadata/ColorFilterArray.h"              // for CFA_GREEN, CFA_BLUE
   36 #include "tiff/TiffEntry.h"                         // for TiffEntry, TIFF_...
   37 #include "tiff/TiffIFD.h"                           // for TiffRootIFD, Tif...
   38 #include "tiff/TiffTag.h"                           // for TiffTag, IMAGELE...
   39 #include <algorithm>                                // for min
   40 #include <cassert>                                  // for assert
   41 #include <cmath>                                    // for pow, exp, log
   42 #include <memory>                                   // for unique_ptr, allo...
   43 #include <sstream>                                  // for operator<<, ostr...
   44 #include <string>                                   // for string, operator==
   45 #include <vector>                                   // for vector
   46 // IWYU pragma: no_include <ext/alloc_traits.h>
   47 
   48 using std::vector;
   49 using std::string;
   50 using std::min;
   51 using std::ostringstream;
   52 
   53 namespace rawspeed {
   54 
   55 bool NefDecoder::isAppropriateDecoder(const TiffRootIFD* rootIFD,
   56                                       const Buffer* file) {
   57   const auto id = rootIFD->getID();
   58   const std::string& make = id.make;
   59 
   60   // FIXME: magic
   61 
   62   return make == "NIKON CORPORATION" || make == "NIKON";
   63 }
   64 
   65 RawImage NefDecoder::decodeRawInternal() {
   66   auto raw = mRootIFD->getIFDWithTag(CFAPATTERN);
   67   auto compression = raw->getEntry(COMPRESSION)->getU32();
   68 
   69   TiffEntry *offsets = raw->getEntry(STRIPOFFSETS);
   70   TiffEntry *counts = raw->getEntry(STRIPBYTECOUNTS);
   71 
   72   if (mRootIFD->getEntryRecursive(MODEL)->getString() == "NIKON D100 ") { /**Sigh**/
   73     if (!mFile->isValid(offsets->getU32()))
   74       ThrowRDE("Image data outside of file.");
   75     if (!D100IsCompressed(offsets->getU32())) {
   76       DecodeD100Uncompressed();
   77       return mRaw;
   78     }
   79   }
   80 
   81   if (compression == 1 || (hints.has("force_uncompressed")) ||
   82       NEFIsUncompressed(raw)) {
   83     DecodeUncompressed();
   84     return mRaw;
   85   }
   86 
   87   if (NEFIsUncompressedRGB(raw)) {
   88     DecodeSNefUncompressed();
   89     return mRaw;
   90   }
   91 
   92   if (offsets->count != 1) {
   93     ThrowRDE("Multiple Strips found: %u", offsets->count);
   94   }
   95   if (counts->count != offsets->count) {
   96     ThrowRDE(
   97         "Byte count number does not match strip size: count:%u, strips:%u ",
   98         counts->count, offsets->count);
   99   }
  100   if (!mFile->isValid(offsets->getU32(), counts->getU32()))
  101     ThrowRDE("Invalid strip byte count. File probably truncated.");
  102 
  103   if (34713 != compression)
  104     ThrowRDE("Unsupported compression");
  105 
  106   uint32_t width = raw->getEntry(IMAGEWIDTH)->getU32();
  107   uint32_t height = raw->getEntry(IMAGELENGTH)->getU32();
  108   uint32_t bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getU32();
  109 
  110   mRaw->dim = iPoint2D(width, height);
  111 
  112   raw = mRootIFD->getIFDWithTag(static_cast<TiffTag>(0x8c));
  113 
  114   TiffEntry *meta;
  115   if (raw->hasEntry(static_cast<TiffTag>(0x96))) {
  116     meta = raw->getEntry(static_cast<TiffTag>(0x96));
  117   } else {
  118     meta = raw->getEntry(static_cast<TiffTag>(0x8c)); // Fall back
  119   }
  120 
  121   ByteStream rawData(
  122       DataBuffer(mFile->getSubView(offsets->getU32(), counts->getU32()),
  123                  Endianness::little));
  124 
  125   NikonDecompressor n(mRaw, meta->getData(), bitPerPixel);
  126   mRaw->createData();
  127   n.decompress(rawData, uncorrectedRawValues);
  128 
  129   return mRaw;
  130 }
  131 
  132 /*
  133 Figure out if a NEF file is compressed.  These fancy heuristics
  134 are only needed for the D100, thanks to a bug in some cameras
  135 that tags all images as "compressed".
  136 */
  137 bool NefDecoder::D100IsCompressed(uint32_t offset) {
  138   const uint8_t* test = mFile->getData(offset, 256);
  139   int i;
  140 
  141   for (i = 15; i < 256; i += 16)
  142     if (test[i])
  143       return true;
  144 
  145   return false;
  146 }
  147 
  148 /* At least the D810 has a broken firmware that tags uncompressed images
  149    as if they were compressed. For those cases we set uncompressed mode
  150    by figuring out that the image is the size of uncompressed packing */
  151 bool NefDecoder::NEFIsUncompressed(const TiffIFD* raw) {
  152   TiffEntry* counts = raw->getEntry(STRIPBYTECOUNTS);
  153   uint32_t width = raw->getEntry(IMAGEWIDTH)->getU32();
  154   uint32_t height = raw->getEntry(IMAGELENGTH)->getU32();
  155   uint32_t bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getU32();
  156 
  157   if (!width || !height || !bitPerPixel)
  158     return false;
  159 
  160   const auto avaliableInputBytes = counts->getU32(0);
  161   const auto requiredPixels = iPoint2D(width, height).area();
  162 
  163   // Now, there can be three situations.
  164 
  165   // We might have not enough input to produce the requested image size.
  166   const uint64_t avaliableInputBits = uint64_t(8) * avaliableInputBytes;
  167   const auto avaliablePixels = avaliableInputBits / bitPerPixel; // round down!
  168   if (avaliablePixels < requiredPixels)
  169     return false;
  170 
  171   // We might have exactly enough input with no padding whatsoever.
  172   if (avaliablePixels == requiredPixels)
  173     return true;
  174 
  175   // Or, we might have too much input. And sadly this is the worst case.
  176   // We can't just accept this. Some *compressed* NEF's also pass this check :(
  177   // Thus, let's accept *some* *small* padding.
  178   const auto requiredInputBits = bitPerPixel * requiredPixels;
  179   const auto requiredInputBytes = roundUpDivision(requiredInputBits, 8);
  180   // While we might have more *pixels* than needed, it does not nessesairly mean
  181   // that we have more input *bytes*. We might be off by a few pixels, and with
  182   // small image dimensions and bpp, we might still be in the same byte.
  183   assert(avaliableInputBytes >= requiredInputBytes);
  184   const auto totalPadding = avaliableInputBytes - requiredInputBytes;
  185   if (totalPadding % height != 0)
  186     return false; // Inconsistent padding makes no sense here.
  187   const auto perRowPadding = totalPadding / height;
  188   return perRowPadding < 16;
  189 }
  190 
  191 /* At least the D810 has a broken firmware that tags uncompressed images
  192    as if they were compressed. For those cases we set uncompressed mode
  193    by figuring out that the image is the size of uncompressed packing */
  194 bool NefDecoder::NEFIsUncompressedRGB(const TiffIFD* raw) {
  195   uint32_t byteCount = raw->getEntry(STRIPBYTECOUNTS)->getU32(0);
  196   uint32_t width = raw->getEntry(IMAGEWIDTH)->getU32();
  197   uint32_t height = raw->getEntry(IMAGELENGTH)->getU32();
  198 
  199   if (byteCount % 3 != 0)
  200     return false;
  201 
  202   return byteCount / 3 == iPoint2D(width, height).area();
  203 }
  204 
  205 void NefDecoder::DecodeUncompressed() {
  206   auto raw = getIFDWithLargestImage(CFAPATTERN);
  207   TiffEntry *offsets = raw->getEntry(STRIPOFFSETS);
  208   TiffEntry *counts = raw->getEntry(STRIPBYTECOUNTS);
  209   uint32_t yPerSlice = raw->getEntry(ROWSPERSTRIP)->getU32();
  210   uint32_t width = raw->getEntry(IMAGEWIDTH)->getU32();
  211   uint32_t height = raw->getEntry(IMAGELENGTH)->getU32();
  212   uint32_t bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getU32();
  213 
  214   mRaw->dim = iPoint2D(width, height);
  215 
  216   if (width == 0 || height == 0 || width > 8288 || height > 5520)
  217     ThrowRDE("Unexpected image dimensions found: (%u; %u)", width, height);
  218 
  219   if (counts->count != offsets->count) {
  220     ThrowRDE("Byte count number does not match strip size: "
  221              "count:%u, stips:%u ",
  222              counts->count, offsets->count);
  223   }
  224 
  225   if (yPerSlice == 0 || yPerSlice > static_cast<uint32_t>(mRaw->dim.y) ||
  226       roundUpDivision(mRaw->dim.y, yPerSlice) != counts->count) {
  227     ThrowRDE("Invalid y per slice %u or strip count %u (height = %u)",
  228              yPerSlice, counts->count, mRaw->dim.y);
  229   }
  230 
  231   vector<NefSlice> slices;
  232   slices.reserve(counts->count);
  233   uint32_t offY = 0;
  234 
  235   for (uint32_t s = 0; s < counts->count; s++) {
  236     NefSlice slice;
  237     slice.offset = offsets->getU32(s);
  238     slice.count = counts->getU32(s);
  239 
  240     if (slice.count < 1)
  241       ThrowRDE("Slice %u is empty", s);
  242 
  243     if (offY + yPerSlice > height)
  244       slice.h = height - offY;
  245     else
  246       slice.h = yPerSlice;
  247 
  248     offY = min(height, offY + yPerSlice);
  249 
  250     if (!mFile->isValid(slice.offset, slice.count))
  251       ThrowRDE("Slice offset/count invalid");
  252 
  253     slices.push_back(slice);
  254   }
  255 
  256   if (slices.empty())
  257     ThrowRDE("No valid slices found. File probably truncated.");
  258 
  259   assert(height == offY);
  260   assert(slices.size() == counts->count);
  261 
  262   mRaw->createData();
  263   if (bitPerPixel == 14 && width*slices[0].h*2 == slices[0].count)
  264     bitPerPixel = 16; // D3 & D810
  265 
  266   bitPerPixel = hints.get("real_bpp", bitPerPixel);
  267 
  268   switch (bitPerPixel) {
  269   case 12:
  270   case 14:
  271   case 16:
  272     break;
  273   default:
  274     ThrowRDE("Invalid bpp found: %u", bitPerPixel);
  275   }
  276 
  277   bool bitorder = ! hints.has("msb_override");
  278 
  279   offY = 0;
  280   for (const NefSlice& slice : slices) {
  281     ByteStream in(DataBuffer(mFile->getSubView(slice.offset, slice.count),
  282                              Endianness::little));
  283     iPoint2D size(width, slice.h);
  284     iPoint2D pos(0, offY);
  285 
  286     if (hints.has("coolpixmangled")) {
  287       UncompressedDecompressor u(in, mRaw);
  288       u.readUncompressedRaw(size, pos, width * bitPerPixel / 8, 12,
  289                             BitOrder_MSB32);
  290     } else {
  291       if (hints.has("coolpixsplit"))
  292         readCoolpixSplitRaw(in, size, pos, width * bitPerPixel / 8);
  293       else {
  294         UncompressedDecompressor u(in, mRaw);
  295         if (in.getSize() % size.y != 0)
  296           ThrowRDE("Inconsistent row size");
  297         const auto inputPitchBytes = in.getSize() / size.y;
  298         u.readUncompressedRaw(size, pos, inputPitchBytes, bitPerPixel,
  299                               bitorder ? BitOrder_MSB : BitOrder_LSB);
  300       }
  301     }
  302 
  303     offY += slice.h;
  304   }
  305 }
  306 
  307 void NefDecoder::readCoolpixSplitRaw(const ByteStream& input,
  308                                      const iPoint2D& size,
  309                                      const iPoint2D& offset, int inputPitch) {
  310   uint8_t* data = mRaw->getData();
  311   uint32_t outPitch = mRaw->pitch;
  312   uint32_t w = size.x;
  313   uint32_t h = size.y;
  314   uint32_t cpp = mRaw->getCpp();
  315   if (input.getRemainSize() < (inputPitch*h)) {
  316     if (static_cast<int>(input.getRemainSize()) > inputPitch)
  317       h = input.getRemainSize() / inputPitch - 1;
  318     else
  319       ThrowIOE(
  320           "Not enough data to decode a single line. Image file truncated.");
  321   }
  322 
  323   if (offset.y > mRaw->dim.y)
  324     ThrowRDE("Invalid y offset");
  325   if (offset.x + size.x > mRaw->dim.x)
  326     ThrowRDE("Invalid x offset");
  327 
  328   uint32_t y = offset.y;
  329   h = min(h + static_cast<uint32_t>(offset.y),
  330           static_cast<uint32_t>(mRaw->dim.y));
  331   w *= cpp;
  332   h /= 2;
  333   BitPumpMSB in(input);
  334   for (; y < h; y++) {
  335     auto* dest = reinterpret_cast<uint16_t*>(
  336         &data[offset.x * sizeof(uint16_t) * cpp + y * 2 * outPitch]);
  337     for (uint32_t x = 0; x < w; x++) {
  338       dest[x] =  in.getBits(12);
  339     }
  340   }
  341   for (y = offset.y; y < h; y++) {
  342     auto* dest = reinterpret_cast<uint16_t*>(
  343         &data[offset.x * sizeof(uint16_t) * cpp + (y * 2 + 1) * outPitch]);
  344     for (uint32_t x = 0; x < w; x++) {
  345       dest[x] =  in.getBits(12);
  346     }
  347   }
  348 }
  349 
  350 void NefDecoder::DecodeD100Uncompressed() {
  351   auto ifd = mRootIFD->getIFDWithTag(STRIPOFFSETS, 1);
  352 
  353   uint32_t offset = ifd->getEntry(STRIPOFFSETS)->getU32();
  354   // Hardcode the sizes as at least the width is not correctly reported
  355   uint32_t width = 3040;
  356   uint32_t height = 2024;
  357 
  358   mRaw->dim = iPoint2D(width, height);
  359   mRaw->createData();
  360 
  361   UncompressedDecompressor u(
  362       ByteStream(DataBuffer(mFile->getSubView(offset), Endianness::little)),
  363       mRaw);
  364 
  365   u.decode12BitRaw<Endianness::big, false, true>(width, height);
  366 }
  367 
  368 void NefDecoder::DecodeSNefUncompressed() {
  369   auto raw = getIFDWithLargestImage(CFAPATTERN);
  370   uint32_t offset = raw->getEntry(STRIPOFFSETS)->getU32();
  371   uint32_t width = raw->getEntry(IMAGEWIDTH)->getU32();
  372   uint32_t height = raw->getEntry(IMAGELENGTH)->getU32();
  373 
  374   if (width == 0 || height == 0 || width % 2 != 0 || width > 3680 ||
  375       height > 2456)
  376     ThrowRDE("Unexpected image dimensions found: (%u; %u)", width, height);
  377 
  378   mRaw->dim = iPoint2D(width, height);
  379   mRaw->setCpp(3);
  380   mRaw->isCFA = false;
  381   mRaw->createData();
  382 
  383   ByteStream in(DataBuffer(mFile->getSubView(offset), Endianness::little));
  384 
  385   DecodeNikonSNef(&in, width, height);
  386 }
  387 
  388 void NefDecoder::checkSupportInternal(const CameraMetaData* meta) {
  389   auto id = mRootIFD->getID();
  390   string mode = getMode();
  391   string extended_mode = getExtendedMode(mode);
  392 
  393   if (meta->hasCamera(id.make, id.model, extended_mode))
  394     checkCameraSupported(meta, id, extended_mode);
  395   else
  396     checkCameraSupported(meta, id, mode);
  397 }
  398 
  399 string NefDecoder::getMode() {
  400   ostringstream mode;
  401   auto raw = getIFDWithLargestImage(CFAPATTERN);
  402   int compression = raw->getEntry(COMPRESSION)->getU32();
  403   uint32_t bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getU32();
  404 
  405   if (NEFIsUncompressedRGB(raw))
  406     mode << "sNEF-uncompressed";
  407   else {
  408     if (1 == compression || NEFIsUncompressed(raw))
  409       mode << bitPerPixel << "bit-uncompressed";
  410     else
  411       mode << bitPerPixel << "bit-compressed";
  412   }
  413   return mode.str();
  414 }
  415 
  416 string NefDecoder::getExtendedMode(const string &mode) {
  417   ostringstream extended_mode;
  418 
  419   auto ifd = mRootIFD->getIFDWithTag(CFAPATTERN);
  420   uint32_t width = ifd->getEntry(IMAGEWIDTH)->getU32();
  421   uint32_t height = ifd->getEntry(IMAGELENGTH)->getU32();
  422 
  423   extended_mode << width << "x" << height << "-" << mode;
  424   return extended_mode.str();
  425 }
  426 
  427 // We use this for the D50 and D2X whacky WB "encryption"
  428 const std::array<uint8_t, 256> NefDecoder::serialmap = {
  429     {0xc1, 0xbf, 0x6d, 0x0d, 0x59, 0xc5, 0x13, 0x9d, 0x83, 0x61, 0x6b, 0x4f,
  430      0xc7, 0x7f, 0x3d, 0x3d, 0x53, 0x59, 0xe3, 0xc7, 0xe9, 0x2f, 0x95, 0xa7,
  431      0x95, 0x1f, 0xdf, 0x7f, 0x2b, 0x29, 0xc7, 0x0d, 0xdf, 0x07, 0xef, 0x71,
  432      0x89, 0x3d, 0x13, 0x3d, 0x3b, 0x13, 0xfb, 0x0d, 0x89, 0xc1, 0x65, 0x1f,
  433      0xb3, 0x0d, 0x6b, 0x29, 0xe3, 0xfb, 0xef, 0xa3, 0x6b, 0x47, 0x7f, 0x95,
  434      0x35, 0xa7, 0x47, 0x4f, 0xc7, 0xf1, 0x59, 0x95, 0x35, 0x11, 0x29, 0x61,
  435      0xf1, 0x3d, 0xb3, 0x2b, 0x0d, 0x43, 0x89, 0xc1, 0x9d, 0x9d, 0x89, 0x65,
  436      0xf1, 0xe9, 0xdf, 0xbf, 0x3d, 0x7f, 0x53, 0x97, 0xe5, 0xe9, 0x95, 0x17,
  437      0x1d, 0x3d, 0x8b, 0xfb, 0xc7, 0xe3, 0x67, 0xa7, 0x07, 0xf1, 0x71, 0xa7,
  438      0x53, 0xb5, 0x29, 0x89, 0xe5, 0x2b, 0xa7, 0x17, 0x29, 0xe9, 0x4f, 0xc5,
  439      0x65, 0x6d, 0x6b, 0xef, 0x0d, 0x89, 0x49, 0x2f, 0xb3, 0x43, 0x53, 0x65,
  440      0x1d, 0x49, 0xa3, 0x13, 0x89, 0x59, 0xef, 0x6b, 0xef, 0x65, 0x1d, 0x0b,
  441      0x59, 0x13, 0xe3, 0x4f, 0x9d, 0xb3, 0x29, 0x43, 0x2b, 0x07, 0x1d, 0x95,
  442      0x59, 0x59, 0x47, 0xfb, 0xe5, 0xe9, 0x61, 0x47, 0x2f, 0x35, 0x7f, 0x17,
  443      0x7f, 0xef, 0x7f, 0x95, 0x95, 0x71, 0xd3, 0xa3, 0x0b, 0x71, 0xa3, 0xad,
  444      0x0b, 0x3b, 0xb5, 0xfb, 0xa3, 0xbf, 0x4f, 0x83, 0x1d, 0xad, 0xe9, 0x2f,
  445      0x71, 0x65, 0xa3, 0xe5, 0x07, 0x35, 0x3d, 0x0d, 0xb5, 0xe9, 0xe5, 0x47,
  446      0x3b, 0x9d, 0xef, 0x35, 0xa3, 0xbf, 0xb3, 0xdf, 0x53, 0xd3, 0x97, 0x53,
  447      0x49, 0x71, 0x07, 0x35, 0x61, 0x71, 0x2f, 0x43, 0x2f, 0x11, 0xdf, 0x17,
  448      0x97, 0xfb, 0x95, 0x3b, 0x7f, 0x6b, 0xd3, 0x25, 0xbf, 0xad, 0xc7, 0xc5,
  449      0xc5, 0xb5, 0x8b, 0xef, 0x2f, 0xd3, 0x07, 0x6b, 0x25, 0x49, 0x95, 0x25,
  450      0x49, 0x6d, 0x71, 0xc7}};
  451 const std::array<uint8_t, 256> NefDecoder::keymap = {
  452     {0xa7, 0xbc, 0xc9, 0xad, 0x91, 0xdf, 0x85, 0xe5, 0xd4, 0x78, 0xd5, 0x17,
  453      0x46, 0x7c, 0x29, 0x4c, 0x4d, 0x03, 0xe9, 0x25, 0x68, 0x11, 0x86, 0xb3,
  454      0xbd, 0xf7, 0x6f, 0x61, 0x22, 0xa2, 0x26, 0x34, 0x2a, 0xbe, 0x1e, 0x46,
  455      0x14, 0x68, 0x9d, 0x44, 0x18, 0xc2, 0x40, 0xf4, 0x7e, 0x5f, 0x1b, 0xad,
  456      0x0b, 0x94, 0xb6, 0x67, 0xb4, 0x0b, 0xe1, 0xea, 0x95, 0x9c, 0x66, 0xdc,
  457      0xe7, 0x5d, 0x6c, 0x05, 0xda, 0xd5, 0xdf, 0x7a, 0xef, 0xf6, 0xdb, 0x1f,
  458      0x82, 0x4c, 0xc0, 0x68, 0x47, 0xa1, 0xbd, 0xee, 0x39, 0x50, 0x56, 0x4a,
  459      0xdd, 0xdf, 0xa5, 0xf8, 0xc6, 0xda, 0xca, 0x90, 0xca, 0x01, 0x42, 0x9d,
  460      0x8b, 0x0c, 0x73, 0x43, 0x75, 0x05, 0x94, 0xde, 0x24, 0xb3, 0x80, 0x34,
  461      0xe5, 0x2c, 0xdc, 0x9b, 0x3f, 0xca, 0x33, 0x45, 0xd0, 0xdb, 0x5f, 0xf5,
  462      0x52, 0xc3, 0x21, 0xda, 0xe2, 0x22, 0x72, 0x6b, 0x3e, 0xd0, 0x5b, 0xa8,
  463      0x87, 0x8c, 0x06, 0x5d, 0x0f, 0xdd, 0x09, 0x19, 0x93, 0xd0, 0xb9, 0xfc,
  464      0x8b, 0x0f, 0x84, 0x60, 0x33, 0x1c, 0x9b, 0x45, 0xf1, 0xf0, 0xa3, 0x94,
  465      0x3a, 0x12, 0x77, 0x33, 0x4d, 0x44, 0x78, 0x28, 0x3c, 0x9e, 0xfd, 0x65,
  466      0x57, 0x16, 0x94, 0x6b, 0xfb, 0x59, 0xd0, 0xc8, 0x22, 0x36, 0xdb, 0xd2,
  467      0x63, 0x98, 0x43, 0xa1, 0x04, 0x87, 0x86, 0xf7, 0xa6, 0x26, 0xbb, 0xd6,
  468      0x59, 0x4d, 0xbf, 0x6a, 0x2e, 0xaa, 0x2b, 0xef, 0xe6, 0x78, 0xb6, 0x4e,
  469      0xe0, 0x2f, 0xdc, 0x7c, 0xbe, 0x57, 0x19, 0x32, 0x7e, 0x2a, 0xd0, 0xb8,
  470      0xba, 0x29, 0x00, 0x3c, 0x52, 0x7d, 0xa8, 0x49, 0x3b, 0x2d, 0xeb, 0x25,
  471      0x49, 0xfa, 0xa3, 0xaa, 0x39, 0xa7, 0xc5, 0xa7, 0x50, 0x11, 0x36, 0xfb,
  472      0xc6, 0x67, 0x4a, 0xf5, 0xa5, 0x12, 0x65, 0x7e, 0xb0, 0xdf, 0xaf, 0x4e,
  473      0xb3, 0x61, 0x7f, 0x2f}};
  474 
  475 void NefDecoder::decodeMetaDataInternal(const CameraMetaData* meta) {
  476   int iso = 0;
  477   mRaw->cfa.setCFA(iPoint2D(2,2), CFA_RED, CFA_GREEN, CFA_GREEN, CFA_BLUE);
  478 
  479   int white = mRaw->whitePoint;
  480   int black = mRaw->blackLevel;
  481 
  482   if (mRootIFD->hasEntryRecursive(ISOSPEEDRATINGS))
  483     iso = mRootIFD->getEntryRecursive(ISOSPEEDRATINGS)->getU32();
  484 
  485   // Read the whitebalance
  486 
  487   if (mRootIFD->hasEntryRecursive(static_cast<TiffTag>(12))) {
  488     TiffEntry* wb = mRootIFD->getEntryRecursive(static_cast<TiffTag>(12));
  489     if (wb->count == 4) {
  490       mRaw->metadata.wbCoeffs[0] = wb->getFloat(0);
  491       mRaw->metadata.wbCoeffs[1] = wb->getFloat(2);
  492       mRaw->metadata.wbCoeffs[2] = wb->getFloat(1);
  493       if (mRaw->metadata.wbCoeffs[1] <= 0.0F)
  494         mRaw->metadata.wbCoeffs[1] = 1.0F;
  495     }
  496   } else if (mRootIFD->hasEntryRecursive(static_cast<TiffTag>(0x0097))) {
  497     TiffEntry* wb = mRootIFD->getEntryRecursive(static_cast<TiffTag>(0x0097));
  498     if (wb->count > 4) {
  499       uint32_t version = 0;
  500       for (uint32_t i = 0; i < 4; i++) {
  501         const auto v = wb->getByte(i);
  502         if (v < '0' || v > '9')
  503           ThrowRDE("Bad version component: %c - not a digit", v);
  504         version = (version << 4) + v - '0';
  505       }
  506 
  507       if (version == 0x100 && wb->count >= 80 && wb->type == TIFF_UNDEFINED) {
  508         mRaw->metadata.wbCoeffs[0] = static_cast<float>(wb->getU16(36));
  509         mRaw->metadata.wbCoeffs[2] = static_cast<float>(wb->getU16(37));
  510         mRaw->metadata.wbCoeffs[1] = static_cast<float>(wb->getU16(38));
  511       } else if (version == 0x103 && wb->count >= 26 && wb->type == TIFF_UNDEFINED) {
  512         mRaw->metadata.wbCoeffs[0] = static_cast<float>(wb->getU16(10));
  513         mRaw->metadata.wbCoeffs[1] = static_cast<float>(wb->getU16(11));
  514         mRaw->metadata.wbCoeffs[2] = static_cast<float>(wb->getU16(12));
  515       } else if (((version == 0x204 && wb->count >= 564) ||
  516                   (version == 0x205 && wb->count >= 284)) &&
  517                  mRootIFD->hasEntryRecursive(static_cast<TiffTag>(0x001d)) &&
  518                  mRootIFD->hasEntryRecursive(static_cast<TiffTag>(0x00a7))) {
  519         // Get the serial number
  520         string serial =
  521             mRootIFD->getEntryRecursive(static_cast<TiffTag>(0x001d))
  522                 ->getString();
  523         if (serial.length() > 9)
  524           ThrowRDE("Serial number is too long (%zu)", serial.length());
  525         uint32_t serialno = 0;
  526         for (unsigned char c : serial) {
  527           if (c >= '0' && c <= '9')
  528             serialno = serialno*10 + c-'0';
  529           else
  530             serialno = serialno*10 + c%10;
  531         }
  532 
  533         // Get the decryption key
  534         TiffEntry* key =
  535             mRootIFD->getEntryRecursive(static_cast<TiffTag>(0x00a7));
  536         const uint8_t* keydata = key->getData(4);
  537         uint32_t keyno = keydata[0] ^ keydata[1] ^ keydata[2] ^ keydata[3];
  538 
  539         // "Decrypt" the block using the serial and key
  540         uint8_t ci = serialmap[serialno & 0xff];
  541         uint8_t cj = keymap[keyno & 0xff];
  542         uint8_t ck = 0x60;
  543 
  544         ByteStream bs = wb->getData();
  545         bs.skipBytes(version == 0x204 ? 284 : 4);
  546 
  547         std::array<uint8_t, 14 + 8> buf;
  548         for (unsigned char& i : buf) {
  549           cj = uint8_t(cj + ci * ck); // modulo arithmetics.
  550           i = bs.getByte() ^ cj;
  551           ck++;
  552         }
  553 
  554         // Finally set the WB coeffs
  555         uint32_t off = (version == 0x204) ? 6 : 14;
  556         mRaw->metadata.wbCoeffs[0] =
  557             static_cast<float>(getU16BE(buf.data() + off + 0));
  558         mRaw->metadata.wbCoeffs[1] =
  559             static_cast<float>(getU16BE(buf.data() + off + 2));
  560         mRaw->metadata.wbCoeffs[2] =
  561             static_cast<float>(getU16BE(buf.data() + off + 6));
  562       }
  563     }
  564   } else if (mRootIFD->hasEntryRecursive(static_cast<TiffTag>(0x0014))) {
  565     TiffEntry* wb = mRootIFD->getEntryRecursive(static_cast<TiffTag>(0x0014));
  566     ByteStream bs = wb->getData();
  567     if (wb->count == 2560 && wb->type == TIFF_UNDEFINED) {
  568       bs.skipBytes(1248);
  569       bs.setByteOrder(Endianness::big);
  570       mRaw->metadata.wbCoeffs[0] = static_cast<float>(bs.getU16()) / 256.0;
  571       mRaw->metadata.wbCoeffs[1] = 1.0F;
  572       mRaw->metadata.wbCoeffs[2] = static_cast<float>(bs.getU16()) / 256.0;
  573     } else if (bs.hasPatternAt("NRW ", 4, 0)) {
  574       uint32_t offset = 0;
  575       if (!bs.hasPatternAt("0100", 4, 4) && wb->count > 72)
  576         offset = 56;
  577       else if (wb->count > 1572)
  578         offset = 1556;
  579 
  580       if (offset) {
  581         bs.skipBytes(offset);
  582         bs.setByteOrder(Endianness::little);
  583         mRaw->metadata.wbCoeffs[0] = 4.0 * bs.getU32();
  584         mRaw->metadata.wbCoeffs[1] = bs.getU32();
  585         mRaw->metadata.wbCoeffs[1] += bs.getU32();
  586         mRaw->metadata.wbCoeffs[2] = 4.0 * bs.getU32();
  587       }
  588     }
  589   }
  590 
  591   if (hints.has("nikon_wb_adjustment")) {
  592     mRaw->metadata.wbCoeffs[0] *= 256/527.0;
  593     mRaw->metadata.wbCoeffs[2] *= 256/317.0;
  594   }
  595 
  596   auto id = mRootIFD->getID();
  597   string mode = getMode();
  598   string extended_mode = getExtendedMode(mode);
  599   if (meta->hasCamera(id.make, id.model, extended_mode)) {
  600     setMetaData(meta, id, extended_mode, iso);
  601   } else if (meta->hasCamera(id.make, id.model, mode)) {
  602     setMetaData(meta, id, mode, iso);
  603   } else {
  604     setMetaData(meta, id, "", iso);
  605   }
  606 
  607   if (white != 65536)
  608     mRaw->whitePoint = white;
  609   if (black != -1)
  610     mRaw->blackLevel = black;
  611 }
  612 
  613 
  614 // DecodeNikonYUY2 decodes 12 bit data in an YUY2-like pattern (2 Luma, 1 Chroma per 2 pixels).
  615 // We un-apply the whitebalance, so output matches lossless.
  616 // Note that values are scaled. See comment below on details.
  617 // OPTME: It would be trivial to run this multithreaded.
  618 void NefDecoder::DecodeNikonSNef(ByteStream* input, uint32_t w, uint32_t h) {
  619   if (w < 6)
  620     ThrowIOE("got a %u wide sNEF, aborting", w);
  621 
  622   if (input->getRemainSize() < (w * h * 3))
  623     ThrowIOE("Not enough data to decode. Image file truncated.");
  624 
  625   // We need to read the applied whitebalance, since we should return
  626   // data before whitebalance, so we "unapply" it.
  627   TiffEntry* wb = mRootIFD->getEntryRecursive(static_cast<TiffTag>(12));
  628   if (!wb)
  629     ThrowRDE("Unable to locate whitebalance needed for decompression");
  630 
  631   assert(wb != nullptr);
  632   if (wb->count != 4 || wb->type != TIFF_RATIONAL)
  633     ThrowRDE("Whitebalance has unknown count or type");
  634 
  635   float wb_r = wb->getFloat(0);
  636   float wb_b = wb->getFloat(1);
  637 
  638   // ((1024/x)*((1<<16)-1)+(1<<9))<=((1<<31)-1), x>0  gives: (0.0312495)
  639   const float lower_limit = 13'421'568.0 / 429'496'627.0;
  640   if (wb_r < lower_limit || wb_b < lower_limit || wb_r > 10.0F || wb_b > 10.0F)
  641     ThrowRDE("Whitebalance has bad values (%f, %f)", wb_r, wb_b);
  642 
  643   mRaw->metadata.wbCoeffs[0] = wb_r;
  644   mRaw->metadata.wbCoeffs[1] = 1.0F;
  645   mRaw->metadata.wbCoeffs[2] = wb_b;
  646 
  647   auto inv_wb_r = static_cast<int>(1024.0 / wb_r);
  648   auto inv_wb_b = static_cast<int>(1024.0 / wb_b);
  649 
  650   auto curve = gammaCurve(1 / 2.4, 12.92, 1, 4095);
  651 
  652   // Scale output values to 16 bits.
  653   for (int i = 0 ; i < 4096; i++) {
  654     curve[i] = clampBits(static_cast<int>(curve[i]) << 2, 16);
  655   }
  656 
  657   curve.resize(4095);
  658 
  659   RawImageCurveGuard curveHandler(&mRaw, curve, false);
  660 
  661   uint16_t tmp;
  662   auto* tmpch = reinterpret_cast<uint8_t*>(&tmp);
  663 
  664   uint8_t* data = mRaw->getData();
  665   uint32_t pitch = mRaw->pitch;
  666   const uint8_t* in = input->getData(w * h * 3);
  667 
  668   for (uint32_t y = 0; y < h; y++) {
  669     auto* dest = reinterpret_cast<uint16_t*>(&data[y * pitch]);
  670     uint32_t random = in[0] + (in[1] << 8) + (in[2] << 16);
  671     for (uint32_t x = 0; x < w * 3; x += 6) {
  672       uint32_t g1 = in[0];
  673       uint32_t g2 = in[1];
  674       uint32_t g3 = in[2];
  675       uint32_t g4 = in[3];
  676       uint32_t g5 = in[4];
  677       uint32_t g6 = in[5];
  678 
  679       in+=6;
  680       auto y1 = static_cast<float>(g1 | ((g2 & 0x0f) << 8));
  681       auto y2 = static_cast<float>((g2 >> 4) | (g3 << 4));
  682       auto cb = static_cast<float>(g4 | ((g5 & 0x0f) << 8));
  683       auto cr = static_cast<float>((g5 >> 4) | (g6 << 4));
  684 
  685       float cb2 = cb;
  686       float cr2 = cr;
  687       // Interpolate right pixel. We assume the sample is aligned with left pixel.
  688       if ((x+6) < w*3) {
  689         g4 = in[3];
  690         g5 = in[4];
  691         g6 = in[5];
  692         cb2 = (static_cast<float>((g4 | ((g5 & 0x0f) << 8))) + cb) * 0.5F;
  693         cr2 = (static_cast<float>(((g5 >> 4) | (g6 << 4))) + cr) * 0.5F;
  694       }
  695 
  696       cb -= 2048;
  697       cr -= 2048;
  698       cb2 -= 2048;
  699       cr2 -= 2048;
  700 
  701       mRaw->setWithLookUp(clampBits(static_cast<int>(y1 + 1.370705 * cr), 12),
  702                           tmpch, &random);
  703       dest[x] = clampBits((inv_wb_r * tmp + (1<<9)) >> 10, 15);
  704 
  705       mRaw->setWithLookUp(
  706           clampBits(static_cast<int>(y1 - 0.337633 * cb - 0.698001 * cr), 12),
  707           reinterpret_cast<uint8_t*>(&dest[x + 1]), &random);
  708 
  709       mRaw->setWithLookUp(clampBits(static_cast<int>(y1 + 1.732446 * cb), 12),
  710                           tmpch, &random);
  711       dest[x+2]   = clampBits((inv_wb_b * tmp + (1<<9)) >> 10, 15);
  712 
  713       mRaw->setWithLookUp(clampBits(static_cast<int>(y2 + 1.370705 * cr2), 12),
  714                           tmpch, &random);
  715       dest[x+3] = clampBits((inv_wb_r * tmp + (1<<9)) >> 10, 15);
  716 
  717       mRaw->setWithLookUp(
  718           clampBits(static_cast<int>(y2 - 0.337633 * cb2 - 0.698001 * cr2), 12),
  719           reinterpret_cast<uint8_t*>(&dest[x + 4]), &random);
  720 
  721       mRaw->setWithLookUp(clampBits(static_cast<int>(y2 + 1.732446 * cb2), 12),
  722                           tmpch, &random);
  723       dest[x+5] = clampBits((inv_wb_b * tmp + (1<<9)) >> 10, 15);
  724     }
  725   }
  726 }
  727 
  728 // From:  dcraw.c -- Dave Coffin's raw photo decoder
  729 #define SQR(x) ((x)*(x))
  730 std::vector<uint16_t> NefDecoder::gammaCurve(double pwr, double ts, int mode,
  731                                              int imax) {
  732   std::vector<uint16_t> curve(65536);
  733 
  734   int i;
  735   std::array<double, 6> g;
  736   std::array<double, 2> bnd = {{}};
  737   double r;
  738   g[0] = pwr;
  739   g[1] = ts;
  740   g[2] = g[3] = g[4] = 0;
  741   bnd[g[1] >= 1] = 1;
  742   if (g[1] && (g[1]-1)*(g[0]-1) <= 0) {
  743     for (i=0; i < 48; i++) {
  744       g[2] = (bnd[0] + bnd[1])/2;
  745       if (g[0])
  746         bnd[(pow(g[2] / g[1], -g[0]) - 1) / g[0] - 1 / g[2] > -1] = g[2];
  747       else
  748         bnd[g[2] / exp(1 - 1 / g[2]) < g[1]] = g[2];
  749     }
  750     g[3] = g[2] / g[1];
  751     if (g[0])
  752       g[4] = g[2] * (1 / g[0] - 1);
  753   }
  754   if (g[0]) {
  755     g[5] = 1 / (g[1] * SQR(g[3]) / 2 - g[4] * (1 - g[3]) +
  756                 (1 - pow(g[3], 1 + g[0])) * (1 + g[4]) / (1 + g[0])) -
  757            1;
  758   } else {
  759     g[5] = 1 / (g[1] * SQR(g[3]) / 2 + 1 - g[2] - g[3] -
  760                 g[2] * g[3] * (log(g[3]) - 1)) -
  761            1;
  762   }
  763 
  764   if (mode == 0)
  765     ThrowRDE("Unimplemented mode");
  766 
  767   mode--;
  768 
  769   for (i=0; i < 0x10000; i++) {
  770     curve[i] = 0xffff;
  771     if ((r = static_cast<double>(i) / imax) < 1) {
  772       curve[i] = static_cast<uint16_t>(
  773           0x10000 *
  774           (mode ? (r < g[3] ? r * g[1]
  775                             : (g[0] ? pow(r, g[0]) * (1 + g[4]) - g[4]
  776                                     : log(r) * g[2] + 1))
  777                 : (r < g[2] ? r / g[1]
  778                             : (g[0] ? pow((r + g[4]) / (1 + g[4]), 1 / g[0])
  779                                     : exp((r - 1) / g[2])))));
  780     }
  781   }
  782 
  783   assert(curve.size() == 65536);
  784 
  785   return curve;
  786 }
  787 #undef SQR
  788 
  789 } // namespace rawspeed