libopenraw  0.3.7
erffile.cpp
1 /*
2  * libopenraw - erffile.cpp
3  *
4  * Copyright (C) 2006-2022 Hubert Figuière
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #include <libopenraw/cameraids.h>
23 
24 #include "ifddir.hpp"
25 #include "rawfile_private.hpp"
26 #include "erffile.hpp"
27 #include "rawdata.hpp"
28 #include "thumbnail.hpp"
29 
30 using namespace Debug;
31 
32 namespace OpenRaw {
33 
34 class RawData;
35 
36 namespace Internals {
37 
38 /* taken from dcraw, by default */
39 static const BuiltinColourMatrix s_matrices[] = {
40  { OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_EPSON, OR_TYPEID_EPSON_RD1), 0, 0,
41  { 6827, -1878, -732, -8429, 16012, 2564, -704, 592, 7145 } },
42  { OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_EPSON, OR_TYPEID_EPSON_RD1S), 0, 0,
43  { 6827, -1878, -732, -8429, 16012, 2564, -704, 592, 7145 } },
44  { OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_EPSON, OR_TYPEID_EPSON_RD1X), 0, 0,
45  { 6827, -1878, -732, -8429, 16012, 2564, -704, 592, 7145 } },
46  { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
47 };
48 
49 const IfdFile::camera_ids_t ERFFile::s_def[] = {
50  { "R-D1", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_EPSON,
51  OR_TYPEID_EPSON_RD1) },
52  { "R-D1s", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_EPSON,
53  OR_TYPEID_EPSON_RD1S) },
54  { "R-D1x", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_EPSON,
55  OR_TYPEID_EPSON_RD1X) },
56  { 0, 0 }
57 };
58 
59 RawFile *ERFFile::factory(const IO::Stream::Ptr &s)
60 {
61  return new ERFFile(s);
62 }
63 
64 ERFFile::ERFFile(const IO::Stream::Ptr &s)
65  : TiffEpFile(s, OR_RAWFILE_TYPE_ERF)
66 {
67  _setIdMap(s_def);
68  _setMatrices(s_matrices);
69 }
70 
71 ERFFile::~ERFFile()
72 {
73 }
74 
75 ::or_error ERFFile::getMakerNoteThumbnail(Thumbnail& thumbnail)
76 {
77  auto mnote = std::dynamic_pointer_cast<MakerNoteDir>(makerNoteIfd());
78  if (!mnote) {
79  LOGERR("Couldn't find the MakerNote.");
80  return OR_ERROR_NOT_FOUND;
81  }
82  auto thumb = mnote->getEntry(ERF_TAG_PREVIEW_IMAGE);
83  if (!thumb) {
84  LOGERR("Couldn't find the preview image.");
85  return OR_ERROR_NOT_FOUND;
86  }
87 
88  auto count = thumb->count();
89  void *p = thumbnail.allocData(count);
90  auto size = thumb->loadDataInto((uint8_t*)p, count, 0);
91  if (size != count) {
92  LOGERR("Couldn't load the preview image. Read only %lu bytes, expected %d", (LSIZE)size, count);
93  return OR_ERROR_NOT_FOUND;
94  }
95 
96  // The data start by 0xee instead of 0xff for a JPEG. Not sure why.
97  ((uint8_t*)p)[0] = 0xff;
98 
99  // It is 640x424 (3:2 aspect ratio)
100  thumbnail.setDataType(OR_DATA_TYPE_JPEG);
101  thumbnail.setDimensions(640, 424);
102 
103  return OR_ERROR_NONE;
104 }
105 
106 ::or_error ERFFile::_enumThumbnailSizes(std::vector<uint32_t>& list)
107 {
108  auto err = this->TiffEpFile::_enumThumbnailSizes(list);
109  if (err == OR_ERROR_NONE) {
110  // EPSON R-D1 and R-D1s have a 640 pixel JPEG in the MakerNote
111  // We don't need to bother detecting, there won't be a new file format.
112  list.push_back(640);
113  }
114  return err;
115 }
116 
117 ::or_error ERFFile::_getThumbnail(uint32_t size, Thumbnail& thumbnail)
118 {
119  if (size == 640) {
120  getMakerNoteThumbnail(thumbnail);
121  return OR_ERROR_NONE;
122  } else {
123  return TiffEpFile::_getThumbnail(size, thumbnail);
124  }
125 }
126 
127 ::or_error ERFFile::_getRawData(RawData & data, uint32_t /*options*/)
128 {
129  ::or_error err;
130  const IfdDir::Ref & _cfaIfd = cfaIfd();
131  if(_cfaIfd) {
132  err = _getRawDataFromDir(data, _cfaIfd);
133  auto mnote = makerNoteIfd();
134  auto sensor_area = mnote->getEntry(MNOTE_EPSON_SENSORAREA);
135  if (sensor_area) {
136  auto x = mnote->getEntryValue<uint16_t>(*sensor_area, 0, true);
137  auto y = mnote->getEntryValue<uint16_t>(*sensor_area, 1, true);
138  auto w = mnote->getEntryValue<uint16_t>(*sensor_area, 2, true);
139  auto h = mnote->getEntryValue<uint16_t>(*sensor_area, 3, true);
140  data.setActiveArea(x, y, w, h);
141  }
142  }
143  else {
144  err = OR_ERROR_NOT_FOUND;
145  }
146  return err;
147 }
148 
149 }
150 }
151 
152 /*
153  Local Variables:
154  mode:c++
155  c-file-style:"stroustrup"
156  c-file-offsets:((innamespace . 0))
157  indent-tabs-mode:nil
158  fill-column:80
159  End:
160 */
std::shared_ptr< IfdDir > Ref
Shared ptr of an IfdDir.
Definition: ifddir.hpp:56
Represent camera raw data.
Definition: rawdata.hpp:38
Represent a thumbnail.
Definition: thumbnail.hpp:32
#define OR_MAKE_FILE_TYPEID(vendor, camera)
Make a or_rawfile_typeid with a vendor and camera.
Definition: consts.h:133
or_error
Error codes returned by libopenraw.
Definition: consts.h:42
@ OR_DATA_TYPE_JPEG
Definition: consts.h:84
@ OR_RAWFILE_TYPE_ERF
Definition: consts.h:69
@ OR_ERROR_NONE
Definition: consts.h:43
@ OR_ERROR_NOT_FOUND
Definition: consts.h:48
Global namespace for libopenraw.
Definition: arwfile.cpp:29