libopenraw
mrwfile.cpp
1 /*
2  * libopenraw - mrwfile.cpp
3  *
4  * Copyright (C) 2006-2016 Hubert Figuiere
5  * Copyright (C) 2008 Bradley Broom
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 License
9  * as published by the Free Software Foundation, either version 3 of
10  * 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, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #include <stddef.h>
24 #include <sys/types.h>
25 #include <cstdint>
26 #include <string>
27 #include <memory>
28 
29 #include <libopenraw/cameraids.h>
30 #include <libopenraw/debug.h>
31 
32 #include "thumbnail.hpp"
33 #include "rawdata.hpp"
34 
35 #include "trace.hpp"
36 #include "io/stream.hpp"
37 #include "mrwcontainer.hpp"
38 #include "ifd.hpp"
39 #include "ifdentry.hpp"
40 #include "ifdfilecontainer.hpp"
41 #include "mrwfile.hpp"
42 #include "unpack.hpp"
43 #include "rawfile_private.hpp"
44 
45 using namespace Debug;
46 
47 namespace OpenRaw {
48 namespace Internals {
49 
50 #define OR_MAKE_MINOLTA_TYPEID(camid) \
51  OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,camid)
52 
53 /* taken from dcraw, by default */
54 static const BuiltinColourMatrix s_matrices[] = {
55  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_MAXXUM_5D), 0, 0xffb,
56  { 10284,-3283,-1086,-7957,15762,2316,-829,882,6644 } },
57  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_MAXXUM_7D), 0, 0xffb,
58  { 10239,-3104,-1099,-8037,15727,2451,-927,925,6871 } },
59  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE5), 0, 0xf7d,
60  { 8983,-2942,-963,-6556,14476,2237,-2426,2887,8014 } },
61  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE7), 0, 0xf7d,
62  { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
63  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE7I), 0, 0xf7d,
64  { 9144,-2777,-998,-6676,14556,2281,-2470,3019,7744 } },
65  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE7HI), 0, 0xf7d,
66  { 11368,-3894,-1242,-6521,14358,2339,-2475,3056,7285 } },
67  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_A1), 0, 0xf8b,
68  { 9274,-2547,-1167,-8220,16323,1943,-2273,2720,8340 } },
69  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_A2), 0, 0xf8f,
70  { 9097,-2726,-1053,-8073,15506,2762,-966,981,7763 } },
71  { OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_A200), 0, 0,
72  { 8560,-2487,-986,-8112,15535,2771,-1209,1324,7743 } },
73 
74  { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
75 };
76 
77 const struct IfdFile::camera_ids_t MRWFile::s_def[] = {
78  { "21860002", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_MAXXUM_5D) },
79  { "21810002", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_MAXXUM_7D) },
80  { "27730001", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE5) },
81  { "27660001", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE7) },
82  { "27790001", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE7I) },
83  { "27780001", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_DIMAGE7HI) },
84  { "27820001", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_A1) },
85  { "27200001", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_A2) },
86  { "27470002", OR_MAKE_MINOLTA_TYPEID(OR_TYPEID_MINOLTA_A200) },
87  { 0, 0 }
88 };
89 
90 RawFile *MRWFile::factory(const IO::Stream::Ptr &_f)
91 {
92  return new MRWFile(_f);
93 }
94 
95 MRWFile::MRWFile(const IO::Stream::Ptr &_f)
96  : IfdFile(_f, OR_RAWFILE_TYPE_MRW, false)
97 {
98  _setIdMap(s_def);
99  _setMatrices(s_matrices);
100  m_container = new MRWContainer (m_io, 0);
101 }
102 
103 MRWFile::~MRWFile()
104 {
105 }
106 
107 IfdDir::Ref MRWFile::_locateCfaIfd()
108 {
109  // in MRW the CFA IFD is the main IFD
110  return mainIfd();
111 }
112 
113 
114 IfdDir::Ref MRWFile::_locateMainIfd()
115 {
116  return m_container->setDirectory(0);
117 }
118 
119 
120 void MRWFile::_identifyId()
121 {
122  MRWContainer *mc = (MRWContainer *)m_container;
123 
124  // it is important that the main IFD be loaded.
125  // this ensures it.
126  const IfdDir::Ref & _mainIfd = mainIfd();
127 
128  if(_mainIfd && mc->prd) {
129  std::string version = mc->prd->string_val(MRW::PRD_VERSION);
130  _setTypeId(_typeIdFromModel("Minolta", version));
131  }
132 }
133 
134 
135 /* This code only knows about Dimage 5/7, in which the thumbnail position is special. */
136 ::or_error MRWFile::_enumThumbnailSizes(std::vector<uint32_t> &list)
137 {
138  ::or_error err = OR_ERROR_NOT_FOUND;
139  list.push_back (640);
140  err = OR_ERROR_NONE;
141  return err;
142 }
143 
144 /* This code only knows about Dimage 5/7, in which the thumbnail position is special. */
145 ::or_error MRWFile::_getThumbnail(uint32_t /*size*/, Thumbnail & thumbnail)
146 {
147  IfdDir::Ref dir;
148  IfdEntry::Ref maker_ent; /* Make note directory entry. */
149  IfdEntry::Ref thumb_ent; /* Thumbnail data directory entry. */
150  ::or_error ret = OR_ERROR_NOT_FOUND;
151  MRWContainer *mc = (MRWContainer *)m_container;
152 
153  dir = _locateExifIfd();
154  if (!dir) {
155  Trace(WARNING) << "EXIF dir not found\n";
156  return ret;
157  }
158 
159  maker_ent = dir->getEntry(IFD::EXIF_TAG_MAKER_NOTE);
160  if (!maker_ent) {
161  Trace(WARNING) << "maker note offset entry not found\n";
162  return ret;
163  }
164  uint32_t off = 0;
165  off = maker_ent->offset();
166 
167  IfdDir::Ref ref(std::make_shared<IfdDir>(
168  mc->ttw->offset()
169  + MRW::DataBlockHeaderLength + off,
170  *m_container));
171  ref->load();
172 
173  uint32_t tnail_offset = 0;
174  uint32_t tnail_len = 0;
175  thumb_ent = ref->getEntry(MRW::MRWTAG_THUMBNAIL);
176  if (thumb_ent) {
177  tnail_offset = thumb_ent->offset();
178  tnail_len = thumb_ent->count();
179  }
180  else if(ref->getValue(MRW::MRWTAG_THUMBNAIL_OFFSET, tnail_offset)) {
181  if(!ref->getValue(MRW::MRWTAG_THUMBNAIL_LENGTH, tnail_len)) {
182  Trace(WARNING) << "thumbnail lenght entry not found\n";
183  return ret;
184  }
185  }
186  else {
187  Trace(WARNING) << "thumbnail offset entry not found\n";
188  return ret;
189  }
190 
191  Trace(DEBUG1) << "thumbnail offset found, "
192  << " offset == " << tnail_offset << " count == "
193  << tnail_len << "\n";
194  void *p = thumbnail.allocData (tnail_len);
195  size_t fetched = m_container->fetchData(p, mc->ttw->offset()
196  + MRW::DataBlockHeaderLength
197  + tnail_offset,
198  tnail_len);
199  if (fetched != tnail_len) {
200  Trace(WARNING) << "Unable to fetch all thumbnail data: "
201  << fetched << " not " << tnail_len
202  << " bytes\n";
203  }
204  /* Need to patch first byte. */
205  ((unsigned char *)p)[0] = 0xFF;
206 
207  thumbnail.setDataType (OR_DATA_TYPE_JPEG);
208  thumbnail.setDimensions (640, 480);
209  return OR_ERROR_NONE;
210 }
211 
212 
213 ::or_error MRWFile::_getRawData(RawData & data, uint32_t options)
214 {
215  or_error ret = OR_ERROR_NONE;
216  MRWContainer *mc = (MRWContainer *)m_container;
217 
218  if(!mc->prd) {
219  return OR_ERROR_NOT_FOUND;
220  }
221  /* Obtain sensor dimensions from PRD block. */
222  uint16_t y = mc->prd->uint16_val (MRW::PRD_SENSOR_LENGTH);
223  uint16_t x = mc->prd->uint16_val (MRW::PRD_SENSOR_WIDTH);
224  uint8_t bpc = mc->prd->uint8_val (MRW::PRD_PIXEL_SIZE);
225 
226  bool is_compressed = (mc->prd->uint8_val(MRW::PRD_STORAGE_TYPE) == 0x59);
227  /* Allocate space for and retrieve pixel data.
228  * Currently only for cameras that don't compress pixel data.
229  */
230  /* Set pixel array parameters. */
231  uint32_t finaldatalen = 2 * x * y;
232  uint32_t datalen =
233  (is_compressed ? x * y + ((x * y) >> 1) : finaldatalen);
234 
235  if(options & OR_OPTIONS_DONT_DECOMPRESS) {
236  finaldatalen = datalen;
237  }
238  if(is_compressed && (options & OR_OPTIONS_DONT_DECOMPRESS)) {
239  data.setDataType (OR_DATA_TYPE_COMPRESSED_RAW);
240  }
241  else {
242  data.setDataType (OR_DATA_TYPE_RAW);
243  }
244  data.setBpc(bpc);
245  // this seems to be the hardcoded value.
246  uint16_t black = 0;
247  uint16_t white = 0;
248  RawFile::_getBuiltinLevels(_getMatrices(), typeId(),
249  black, white);
250  data.setBlackLevel(black);
251  data.setWhiteLevel(white);
252  Trace(DEBUG1) << "datalen = " << datalen <<
253  " final datalen = " << finaldatalen << "\n";
254  void *p = data.allocData(finaldatalen);
255  size_t fetched = 0;
256  off_t offset = mc->pixelDataOffset();
257  if(!is_compressed || (options & OR_OPTIONS_DONT_DECOMPRESS)) {
258  fetched = m_container->fetchData (p, offset, datalen);
259  }
260  else {
261  Unpack unpack(x, IFD::COMPRESS_NONE);
262  size_t blocksize = unpack.block_size();
263  std::unique_ptr<uint8_t[]> block(new uint8_t[blocksize]);
264  uint8_t * outdata = (uint8_t*)data.data();
265  size_t outsize = finaldatalen;
266  size_t got;
267  do {
268  Trace(DEBUG2) << "fatchData @offset " << offset << "\n";
269  got = m_container->fetchData (block.get(),
270  offset, blocksize);
271  fetched += got;
272  offset += got;
273  Trace(DEBUG2) << "got " << got << "\n";
274  if(got) {
275  size_t out;
276  or_error err = unpack.unpack_be12to16(outdata, outsize,
277  block.get(), got, out);
278  outdata += out;
279  outsize -= out;
280  Trace(DEBUG2) << "unpacked " << out
281  << " bytes from " << got << "\n";
282  if(err != OR_ERROR_NONE) {
283  ret = err;
284  break;
285  }
286  }
287  } while((got != 0) && (fetched < datalen));
288  }
289  if (fetched < datalen) {
290  Trace(WARNING) << "Fetched only " << fetched <<
291  " of " << datalen << ": continuing anyway.\n";
292  }
293  uint16_t bpat = mc->prd->uint16_val (MRW::PRD_BAYER_PATTERN);
294  or_cfa_pattern cfa_pattern = OR_CFA_PATTERN_NONE;
295  switch(bpat)
296  {
297  case 0x0001:
298  cfa_pattern = OR_CFA_PATTERN_RGGB;
299  break;
300  case 0x0004:
301  cfa_pattern = OR_CFA_PATTERN_GBRG;
302  break;
303  default:
304  break;
305  }
306  data.setCfaPatternType(cfa_pattern);
307  data.setDimensions (x, y);
308 
309  return ret;
310 }
311 
312 }
313 }
314 /*
315  Local Variables:
316  mode:c++
317  c-file-style:"stroustrup"
318  c-file-offsets:((innamespace . 0))
319  indent-tabs-mode:nil
320  fill-column:80
321  End:
322 */
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:30
or_error unpack_be12to16(uint8_t *dest, size_t destsize, const uint8_t *src, size_t size, size_t &outsize)
Definition: unpack.cpp:58
virtual void setDimensions(uint32_t x, uint32_t y)
Definition: bitmapdata.cpp:169
void setBpc(uint32_t _bpc)
Definition: bitmapdata.cpp:164
std::shared_ptr< IfdEntry > Ref
Definition: ifdentry.hpp:178
Definition: trace.cpp:30
void setDataType(DataType _type)
Definition: bitmapdata.cpp:100
generic IFD based raw file.
Definition: ifdfile.hpp:48
virtual void setDimensions(uint32_t x, uint32_t y) override
Definition: rawdata.cpp:260