RawImage NefDecoder::decodeRawInternal() { vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(CFAPATTERN); if (data.empty()) ThrowRDE("NEF Decoder: No image data found"); TiffIFD* raw = data[0]; int compression = raw->getEntry(COMPRESSION)->getInt(); data = mRootIFD->getIFDsWithTag(MODEL); if (data.empty()) ThrowRDE("NEF Decoder: No model data found"); TiffEntry *offsets = raw->getEntry(STRIPOFFSETS); TiffEntry *counts = raw->getEntry(STRIPBYTECOUNTS); if (!data[0]->getEntry(MODEL)->getString().compare("NIKON D100 ")) { /**Sigh**/ if (!mFile->isValid(offsets->getInt())) ThrowRDE("NEF Decoder: Image data outside of file."); if (!D100IsCompressed(offsets->getInt())) { DecodeD100Uncompressed(); return mRaw; } } if (compression == 1 || (hints.find(string("force_uncompressed")) != hints.end()) || NEFIsUncompressed(raw)) { DecodeUncompressed(); return mRaw; } if (NEFIsUncompressedRGB(raw)) { DecodeSNefUncompressed(); return mRaw; } if (offsets->count != 1) { ThrowRDE("NEF Decoder: Multiple Strips found: %u", offsets->count); } if (counts->count != offsets->count) { ThrowRDE("NEF Decoder: Byte count number does not match strip size: count:%u, strips:%u ", counts->count, offsets->count); } if (!mFile->isValid(offsets->getInt(), counts->getInt())) ThrowRDE("NEF Decoder: Invalid strip byte count. File probably truncated."); if (34713 != compression) ThrowRDE("NEF Decoder: Unsupported compression"); uint32 width = raw->getEntry(IMAGEWIDTH)->getInt(); uint32 height = raw->getEntry(IMAGELENGTH)->getInt(); uint32 bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getInt(); mRaw->dim = iPoint2D(width, height); mRaw->createData(); data = mRootIFD->getIFDsWithTag((TiffTag)0x8c); if (data.empty()) ThrowRDE("NEF Decoder: Decompression info tag not found"); TiffEntry *meta; if (data[0]->hasEntry((TiffTag)0x96)) { meta = data[0]->getEntry((TiffTag)0x96); } else { meta = data[0]->getEntry((TiffTag)0x8c); // Fall back } try { NikonDecompressor decompressor(mFile, mRaw); decompressor.uncorrectedRawValues = uncorrectedRawValues; ByteStream* metastream; if (getHostEndianness() == data[0]->endian) metastream = new ByteStream(meta->getData(), meta->count); else metastream = new ByteStreamSwap(meta->getData(), meta->count); decompressor.DecompressNikon(metastream, width, height, bitPerPixel, offsets->getInt(), counts->getInt()); delete metastream; } catch (IOException &e) { mRaw->setError(e.what()); // Let's ignore it, it may have delivered somewhat useful data. } return mRaw; }
RawImage NefDecoder::decodeRawInternal() { vector<TiffIFD*> data = mRootIFD->getIFDsWithTag(CFAPATTERN); if (data.empty()) ThrowRDE("NEF Decoder: No image data found"); TiffIFD* raw = data[0]; int compression = raw->getEntry(COMPRESSION)->getInt(); data = mRootIFD->getIFDsWithTag(MODEL); if (data.empty()) ThrowRDE("NEF Decoder: No model data found"); TiffEntry *offsets = raw->getEntry(STRIPOFFSETS); TiffEntry *counts = raw->getEntry(STRIPBYTECOUNTS); if (!data[0]->getEntry(MODEL)->getString().compare("NIKON D100 ")) { /**Sigh**/ if (!mFile->isValid(offsets->getInt())) ThrowRDE("NEF Decoder: Image data outside of file."); if (!D100IsCompressed(offsets->getInt())) { DecodeD100Uncompressed(); return mRaw; } } if (compression == 1) { DecodeUncompressed(); return mRaw; } if (offsets->count != 1) { ThrowRDE("NEF Decoder: Multiple Strips found: %u", offsets->count); } if (counts->count != offsets->count) { ThrowRDE("NEF Decoder: Byte count number does not match strip size: count:%u, strips:%u ", counts->count, offsets->count); } if (!mFile->isValid(offsets->getInt() + counts->getInt())) ThrowRDE("NEF Decoder: Invalid strip byte count. File probably truncated."); if (34713 != compression) ThrowRDE("NEF Decoder: Unsupported compression"); uint32 width = raw->getEntry(IMAGEWIDTH)->getInt(); uint32 height = raw->getEntry(IMAGELENGTH)->getInt(); uint32 bitPerPixel = raw->getEntry(BITSPERSAMPLE)->getInt(); mRaw->dim = iPoint2D(width, height); mRaw->createData(); data = mRootIFD->getIFDsWithTag(MAKERNOTE); if (data.empty()) ThrowRDE("NEF Decoder: No EXIF data found"); TiffIFD* exif = data[0]; TiffEntry *makernoteEntry = exif->getEntry(MAKERNOTE); const uchar8* makernote = makernoteEntry->getData(); FileMap makermap((uchar8*)&makernote[10], mFile->getSize() - makernoteEntry->getDataOffset() - 10); TiffParser makertiff(&makermap); makertiff.parseData(); data = makertiff.RootIFD()->getIFDsWithTag((TiffTag)0x8c); if (data.empty()) ThrowRDE("NEF Decoder: Decompression info tag not found"); TiffEntry *meta; try { meta = data[0]->getEntry((TiffTag)0x96); } catch (TiffParserException) { meta = data[0]->getEntry((TiffTag)0x8c); // Fall back } try { NikonDecompressor decompressor(mFile, mRaw); ByteStream* metastream; if (getHostEndianness() == data[0]->endian) metastream = new ByteStream(meta->getData(), meta->count); else metastream = new ByteStreamSwap(meta->getData(), meta->count); decompressor.DecompressNikon(metastream, width, height, bitPerPixel, offsets->getInt(), counts->getInt()); delete metastream; } catch (IOException &e) { mRaw->setError(e.what()); // Let's ignore it, it may have delivered somewhat useful data. } return mRaw; }