1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/* -*- tab-width:4; c-basic-offset:4 -*- */
/*
* libopenraw - neffile.cpp
*
* Copyright (C) 2006-2008 Hubert Figuiere
* Copyright (C) 2008 Novell, Inc.
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <vector>
#include <libopenraw++/thumbnail.h>
#include <libopenraw++/rawdata.h>
#include "debug.h"
#include "ifd.h"
#include "ifdfilecontainer.h"
#include "ifddir.h"
#include "ifdentry.h"
#include "io/file.h"
#include "huffman.h"
#include "nefdiffiterator.h"
#include "nefcfaiterator.h"
#include "neffile.h"
using namespace Debug;
namespace OpenRaw {
namespace Internals {
const IFDFile::camera_ids_t NEFFile::s_def[] = {
{ "NIKON D1 ", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D1) },
{ "NIKON D100 ", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D100) },
{ "NIKON D1X", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D1X) },
{ "NIKON D200", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D200) },
{ "NIKON D2H", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D2H ) },
{ "NIKON D2X", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D2X ) },
{ "NIKON D3", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D3) },
{ "NIKON D300", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D300) },
{ "NIKON D40", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D40) },
{ "NIKON D40X", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D40X) },
{ "NIKON D50", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D50) },
{ "NIKON D70", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D70) },
{ "NIKON D70s", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D70S) },
{ "NIKON D80", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,
OR_TYPEID_NIKON_D80) },
{ 0, 0 }
};
RawFile *NEFFile::factory(IO::Stream* _filename)
{
return new NEFFile(_filename);
}
NEFFile::NEFFile(IO::Stream* _filename)
: TiffEpFile(_filename, OR_RAWFILE_TYPE_NEF)
{
_setIdMap(s_def);
}
NEFFile::~NEFFile()
{
}
bool NEFFile::isCompressed(RawContainer & container, uint32_t offset)
{
int i;
uint8_t buf[256];
size_t real_size = container.fetchData(buf, offset,
256);
if(real_size != 256) {
return true;
}
for(i = 15; i < 256; i+= 16) {
if(buf[i]) {
Trace(DEBUG1) << "isCompressed: true\n";
return true;
}
}
Trace(DEBUG1) << "isCompressed: false\n";
return false;
}
::or_error NEFFile::_decompressNikonQuantized(RawData & data)
{
NEFCompressionInfo c;
if (!_getCompressionCurve(data, c)) {
return OR_ERROR_NOT_FOUND;
}
const uint32_t rows = data.y();
const uint32_t raw_columns = data.x();
//FIXME: not always true
const uint32_t columns = raw_columns - 1;
NefDiffIterator
diffs(c.huffman, data.data());
NefCfaIterator iter(diffs, rows, raw_columns, c.vpred);
RawData newData;
uint16_t *p = (uint16_t *) newData.allocData(rows * columns * 2);
newData.setDimensions(columns, rows);
newData.setDataType(OR_DATA_TYPE_CFA);
uint16_t bpc = data.bpc();
newData.setBpc(bpc);
newData.setMax((1 << bpc) - 1);
newData.setCfaPattern(data.cfaPattern());
for (unsigned int i = 0; i < rows; i++) {
for (unsigned int j = 0; j < raw_columns; j++) {
uint16_t t = iter.get();
if (j < columns) {
unsigned shift = 16 - data.bpc();
p[i * columns + j] = c.curve[t & 0x3fff] << shift;
}
}
}
data.swap(newData);
return OR_ERROR_NONE;
}
::or_error NEFFile::_decompressIfNeeded(RawData & data,
uint32_t options)
{
uint32_t compression = data.compression();
if((options & OR_OPTIONS_DONT_DECOMPRESS) ||
compression == IFD::COMPRESS_NONE) {
return OR_ERROR_NONE;
} else if(compression == IFD::COMPRESS_NIKON_QUANTIZED) {
return _decompressNikonQuantized(data);
} else {
return OR_ERROR_INVALID_FORMAT;
}
}
int NEFFile::_getCompressionCurve(RawData & data, NEFFile::NEFCompressionInfo& c)
{
if(!m_exifIfd) {
m_exifIfd = _locateExifIfd();
}
if(!m_exifIfd) {
return 0;
}
IFDEntry::Ref maker_ent =
m_exifIfd->getEntry(IFD::EXIF_TAG_MAKER_NOTE);
if(!maker_ent) {
return 0;
}
uint32_t off = maker_ent->offset();
uint32_t base = off + 10;
IFDDir::Ref ref(new IFDDir(base + 8, *m_container));
ref->load();
IFDEntry::Ref curveEntry = ref->getEntry(0x0096);
if(!curveEntry) {
return 0;
}
size_t pos = base + curveEntry->offset();
IO::Stream *file = m_container->file();
file->seek(pos, SEEK_SET);
int16_t aux;
uint16_t header;
bool read = m_container->readInt16(file, aux);
header = aux;
if(!read) {
return 0;
}
if (header == 0x4410) {
c.huffman = NefDiffIterator::Lossy12Bit;
data.setBpc(12);
} else if (header == 0x4630) {
c.huffman = NefDiffIterator::LossLess14Bit;
data.setBpc(14);
} else {
return 0;
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
read = m_container->readInt16(file, aux);
if(!read) {
return 0;
}
c.vpred[i][j] = aux;
}
}
if (header == 0x4410) {
size_t nelems;
read = m_container->readInt16(file, aux);
nelems = aux;
for (size_t i = 0; i < nelems; ++i) {
read = m_container->readInt16(file, aux);
if (!read)
return 0;
c.curve.push_back(aux);
}
} else if (header == 0x4630) {
for (size_t i = 0; i <= 0x3fff; ++i) {
c.curve.push_back(i);
}
}
return 1;
}
::or_error NEFFile::_getRawData(RawData & data, uint32_t options)
{
::or_error ret = OR_ERROR_NONE;
m_cfaIfd = _locateCfaIfd();
Trace(DEBUG1) << "_getRawData()\n";
if(m_cfaIfd) {
ret = _getRawDataFromDir(data, m_cfaIfd);
if (ret != OR_ERROR_NONE) {
return ret;
}
ret = _decompressIfNeeded(data, options);
}
else {
ret = OR_ERROR_NOT_FOUND;
}
return ret;
}
}
}
|