29 #include <libopenraw/debug.h> 30 #include <libopenraw/metadata.h> 31 #include <libopenraw/cameraids.h> 33 #include "rawdata.hpp" 34 #include "metavalue.hpp" 35 #include "cfapattern.hpp" 36 #include "rawfile.hpp" 38 #include "io/streamclone.hpp" 39 #include "io/memstream.hpp" 40 #include "crwfile.hpp" 41 #include "ciffcontainer.hpp" 42 #include "jfifcontainer.hpp" 43 #include "crwdecompressor.hpp" 44 #include "rawfile_private.hpp" 46 using namespace Debug;
54 #define OR_MAKE_CANON_TYPEID(camid) \ 55 OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_CANON,camid) 58 static const BuiltinColourMatrix s_matrices[] = {
59 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_D30), 0, 0,
60 { 9805,-2689,-1312,-5803,13064,3068,-2438,3075,8775 } },
61 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_D60), 0, 0xfa0,
62 { 6188,-1341,-890,-7168,14489,2937,-2640,3228,8483 } },
63 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_10D), 0, 0xfa0,
64 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
65 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_300D), 0, 0xfa0,
66 { 8197,-2000,-1118,-6714,14335,2592,-2536,3178,8266 } },
69 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G2), 0, 0,
70 { 9087,-2693,-1049,-6715,14382,2537,-2291,2819,7790 } },
71 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G3), 0, 0,
72 { 9212,-2781,-1073,-6573,14189,2605,-2300,2844,7664 } },
73 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G5), 0, 0,
74 { 9757,-2872,-933,-5972,13861,2301,-1622,2328,7212 } },
75 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G6), 0, 0,
76 { 9877,-3775,-871,-7613,14807,3072,-1448,1305,7485 } },
77 { OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_PRO1), 0, 0,
78 { 10062,-3522,-999,-7643,15117,2730,-765,817,7323 } },
79 { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
82 const RawFile::camera_ids_t CRWFile::s_def[] = {
83 {
"Canon EOS D30" , OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_D30) },
84 {
"Canon EOS D60" , OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_D60) },
85 {
"Canon EOS 10D" , OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_10D) },
86 {
"Canon EOS 300D DIGITAL", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_300D) },
87 {
"Canon PowerShot G1", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G1) },
88 {
"Canon PowerShot G2", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G2) },
89 {
"Canon PowerShot G3", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G3) },
90 {
"Canon PowerShot G5", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G5) },
91 {
"Canon PowerShot G6", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G6) },
92 {
"Canon PowerShot G7", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G7) },
93 {
"Canon PowerShot Pro1", OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_PRO1) },
97 RawFile *CRWFile::factory(
const IO::Stream::Ptr &s)
99 return new CRWFile(s);
102 CRWFile::CRWFile(
const IO::Stream::Ptr &s)
103 : RawFile(OR_RAWFILE_TYPE_CRW),
105 m_container(new CIFFContainer(m_io)),
109 _setMatrices(s_matrices);
119 ::or_error err = OR_ERROR_NOT_FOUND;
121 Heap::Ref heap = m_container->heap();
126 const RecordEntry::List & records = heap->records();
127 RecordEntry::List::const_iterator iter;
128 iter = std::find_if(records.cbegin(), records.cend(), std::bind(
130 static_cast<uint16_t>(TAG_JPEGIMAGE)));
131 if (iter != records.end()) {
132 Trace(DEBUG2) <<
"JPEG @" << (*iter).offset <<
"\n";
134 uint32_t offset = heap->offset() + (*iter).offset;
136 std::unique_ptr<JfifContainer> jfif(
new JfifContainer(s, 0));
138 jfif->getDimensions(m_x, m_y);
139 Trace(DEBUG1) <<
"JPEG dimensions x=" << m_x
140 <<
" y=" << m_y <<
"\n";
141 uint32_t dim = std::max(m_x,m_y);
142 _addThumbnail(dim,
ThumbDesc(m_x, m_y, OR_DATA_TYPE_JPEG, offset, (*iter).length));
157 ::or_error err = OR_ERROR_NOT_FOUND;
158 Heap::Ref props = m_container->getImageProps();
161 return OR_ERROR_NOT_FOUND;
163 const ImageSpec * img_spec = m_container->getImageSpec();
167 x = img_spec->imageWidth;
168 y = img_spec->imageHeight;
172 const CIFF::RecordEntry::List & propsRecs = props->records();
173 auto iter = std::find_if(propsRecs.cbegin(), propsRecs.cend(), std::bind(
175 static_cast<uint16_t>(TAG_EXIFINFORMATION)));
176 if (iter == propsRecs.end()) {
177 Trace(ERROR) <<
"Couldn't find the Exif information.\n";
181 Heap exifProps(iter->offset + props->offset(), iter->length, m_container);
183 const RecordEntry::List & exifPropsRecs = exifProps.records();
184 iter = std::find_if(exifPropsRecs.cbegin(), exifPropsRecs.cend(),
187 static_cast<uint16_t>(TAG_DECODERTABLE)));
188 if (iter == exifPropsRecs.end()) {
189 Trace(ERROR) <<
"Couldn't find the decoder table.\n";
192 Trace(DEBUG2) <<
"length = " << iter->length <<
"\n";
193 Trace(DEBUG2) <<
"offset = " << exifProps.offset() + iter->offset <<
"\n";
194 auto file = m_container->file();
195 file->seek(exifProps.offset() + iter->offset, SEEK_SET);
196 uint32_t decoderTable;
197 if(m_container->
readUInt32(file, decoderTable)) {
198 Trace(DEBUG2) <<
"decoder table = " << decoderTable <<
"\n";
202 uint16_t cfa_x, cfa_y;
203 iter = std::find_if(exifPropsRecs.cbegin(), exifPropsRecs.cend(), std::bind(
205 static_cast<uint16_t>(TAG_SENSORINFO)));
206 if (iter == exifPropsRecs.end()) {
207 Trace(ERROR) <<
"Couldn't find the sensor info.\n";
210 Trace(DEBUG2) <<
"length = " << iter->length <<
"\n";
211 Trace(DEBUG2) <<
"offset = " << exifProps.offset() + iter->offset <<
"\n";
214 file->seek(exifProps.offset() + iter->offset + 2, SEEK_SET);
217 Trace(ERROR) <<
"Couldn't find the sensor size.\n";
224 CIFF::Heap::Ref heap = m_container->heap();
225 Trace(DEBUG2) <<
"RAW @" << heap->offset() + entry->offset <<
"\n";
226 size_t byte_size = entry->length;
227 void *buf = data.allocData(byte_size);
228 size_t real_size = entry->
fetchData(heap.get(), buf, byte_size);
229 if (real_size != byte_size) {
230 Trace(WARNING) <<
"wrong size\n";
233 data.setCfaPatternType(OR_CFA_PATTERN_RGGB);
237 if((options & OR_OPTIONS_DONT_DECOMPRESS) == 0) {
244 decomp.setOutputDimensions(cfa_x, cfa_y);
245 decomp.setDecoderTable(decoderTable);
246 RawData *dData = decomp.decompress();
248 Trace(DEBUG1) <<
"Out size is " << dData->width()
249 <<
"x" << dData->height() <<
"\n";
260 MetaValue *CRWFile::_getMetaValue(int32_t meta_index)
264 switch(META_INDEX_MASKOUT(meta_index)) {
267 uint32_t index = META_NS_MASKOUT(meta_index);
269 case EXIF_TAG_ORIENTATION:
271 const ImageSpec * img_spec = m_container->getImageSpec();
273 val =
new MetaValue(static_cast<uint32_t>(
274 img_spec->exifOrientation()));
281 if (index == EXIF_TAG_MAKE && !m_make.empty()) {
285 if (index == EXIF_TAG_MODEL && !m_model.empty()) {
290 CIFF::Heap::Ref heap = m_container->getCameraProps();
292 auto propsRecs = heap->records();
294 = std::find_if(propsRecs.cbegin(), propsRecs.cend(),
296 return e.isA(static_cast<uint16_t>(CIFF::TAG_RAWMAKEMODEL));
298 if (iter == propsRecs.end()) {
299 Trace(ERROR) <<
"Couldn't find the image info.\n";
303 size_t sz = iter->length;
307 iter->fetchData(heap.get(),
313 m_make = std::string(buf, p - buf);
317 if (index == EXIF_TAG_MODEL) {
320 else if (index == EXIF_TAG_MAKE) {
323 Trace(DEBUG1) <<
"Make " << m_make <<
"\n";
324 Trace(DEBUG1) <<
"Model " << m_model <<
"\n";
337 Trace(ERROR) <<
"Unknown Meta Namespace\n";
344 void CRWFile::_identifyId()
349 MetaValue * v = _getMetaValue(META_NS_TIFF | EXIF_TAG_MODEL);
351 model = v->getString(0);
354 v = _getMetaValue(META_NS_TIFF | EXIF_TAG_MAKE);
356 make = v->getString(0);
359 _setTypeId(_typeIdFromModel(make, model));
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
bool readUInt32(const IO::Stream::Ptr &f, uint32_t &v)
size_t fetchData(Heap *heap, void *buf, size_t size) const
cloned stream. Allow reading from a different offset
::or_cfa_pattern patternType() const
virtual ::or_error _getRawData(RawData &data, uint32_t options) override
bool readUInt16(const IO::Stream::Ptr &f, uint16_t &v)
const CfaPattern * cfaPattern() const
bool isA(uint16_t _typeCode) const
void setDataType(DataType _type)
virtual RawContainer * getContainer() const override
virtual void setDimensions(uint32_t x, uint32_t y) override
virtual ::or_error _enumThumbnailSizes(std::vector< uint32_t > &list) override