summaryrefslogtreecommitdiff
path: root/lib/mrwfile.cpp
blob: e1d75bf74626ea943dbf1a298244d6df45978b82 (plain)
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
268
269
270
271
272
273
274
275
/*
 * libopenraw - mrwfile.cpp
 *
 * Copyright (C) 2006,2008 Hubert Figuiere
 * Copyright (C) 2008 Bradley Broom
 *
 * 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 <boost/scoped_array.hpp>
#include <libopenraw/libopenraw.h>
#include <libopenraw++/thumbnail.h>
#include <libopenraw++/rawdata.h>

#include "debug.h"
#include "io/stream.h"
#include "io/file.h"
#include "mrwcontainer.h"
#include "ifd.h"
#include "mrwfile.h"
#include "unpack.h"

using namespace Debug;

namespace OpenRaw {


	namespace Internals {

		const struct IFDFile::camera_ids_t MRWFile::s_def[] = {
			{ "21860002", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_MAXXUM_5D) },
			{ "21810002", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_MAXXUM_7D) },
			{ "27730001", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_DIMAGE5) },
			{ "27660001", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_DIMAGE7) },
			{ "27790001", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_DIMAGE7I) },
			{ "27780001", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_DIMAGE7HI) },
			{ "27820001", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_A1) },
			{ "27200001", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_A2) },
			{ "27470002", OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_MINOLTA,
											  OR_TYPEID_MINOLTA_A200) },
			{ 0, 0 }
		};

		RawFile *MRWFile::factory(IO::Stream *_f)
		{
			return new MRWFile(_f);
		}

		MRWFile::MRWFile(IO::Stream* _f)
			: IFDFile(_f, OR_RAWFILE_TYPE_MRW, false)
		{
			_setIdMap(s_def);
			m_container = new MRWContainer (m_io, 0);
		}

		MRWFile::~MRWFile()
		{
		}

		IFDDir::Ref  MRWFile::_locateCfaIfd()
		{
			// in MRW the CFA IFD is the main IFD
			if(!m_mainIfd) {
				m_mainIfd = _locateMainIfd();
			}
			return m_mainIfd;
		}


		IFDDir::Ref  MRWFile::_locateMainIfd()
		{
			return m_container->setDirectory(0);
		}


		void MRWFile::_identifyId()
		{
			MRWContainer *mc = (MRWContainer *)m_container;
			if(!m_mainIfd) {
				m_mainIfd = _locateMainIfd();
			}

			if(mc->prd) {
				std::string version = mc->prd->string_val(MRW::PRD_VERSION);
				_setTypeId(_typeIdFromModel(version));
			}
		}

	
		/* This code only knows about Dimage 5/7, in which the thumbnail position is special. */
		::or_error MRWFile::_enumThumbnailSizes(std::vector<uint32_t> &list)
		{
			::or_error err = OR_ERROR_NOT_FOUND;
			list.push_back (640);
			err = OR_ERROR_NONE;
			return err;
		}

		/* This code only knows about Dimage 5/7, in which the thumbnail position is special. */
		::or_error MRWFile::_getThumbnail(uint32_t /*size*/, Thumbnail & thumbnail)
		{
			IFDDir::Ref dir;
			IFDEntry::Ref maker_ent;	/* Make note directory entry. */
			IFDEntry::Ref thumb_ent;	/* Thumbnail data directory entry. */
			::or_error ret = OR_ERROR_NOT_FOUND;
			MRWContainer *mc = (MRWContainer *)m_container;
			
			dir = _locateExifIfd();
			if (!dir) {
				Trace(WARNING) << "EXIF dir not found\n";
				return ret;
			}

			maker_ent = dir->getEntry(IFD::EXIF_TAG_MAKER_NOTE);
			if (!maker_ent) {
				Trace(WARNING) << "maker note offset entry not found\n";
				return ret;
			}
			uint32_t off = 0;
			off = maker_ent->offset();

			IFDDir::Ref ref(new IFDDir(mc->ttw->offset() + 
									   MRW::DataBlockHeaderLength + off, 
									   *m_container));
			ref->load();
			
			uint32_t tnail_offset = 0;
			uint32_t tnail_len = 0;
			thumb_ent = ref->getEntry(MRW::MRWTAG_THUMBNAIL);
			if (thumb_ent) {
				tnail_offset = thumb_ent->offset();
				tnail_len = thumb_ent->count();
			}
			else if(ref->getValue(MRW::MRWTAG_THUMBNAIL_OFFSET, tnail_offset)) {
				if(!ref->getValue(MRW::MRWTAG_THUMBNAIL_LENGTH, tnail_len)) {
					Trace(WARNING) << "thumbnail lenght entry not found\n";
					return ret;
				}
			}
			else 
			{
				Trace(WARNING) << "thumbnail offset entry not found\n";
				return ret;
			}
			
			Trace(DEBUG1) << "thumbnail offset found, "
						  << " offset == " << tnail_offset  << " count == " 
						  << tnail_len << "\n";
			void *p = thumbnail.allocData (tnail_len);
			size_t fetched = m_container->fetchData(p, mc->ttw->offset() 
													+ MRW::DataBlockHeaderLength 
													+ tnail_offset, 
													tnail_len);
			if (fetched != tnail_len) {
				Trace(WARNING) << "Unable to fetch all thumbnail data: " 
							   << fetched << " not " << tnail_len 
							   << " bytes\n";
			}
			/* Need to patch first byte. */
			((unsigned char *)p)[0] = 0xFF;

			thumbnail.setDataType (OR_DATA_TYPE_JPEG);
			thumbnail.setDimensions (640, 480);
			return OR_ERROR_NONE;
		}


		::or_error MRWFile::_getRawData(RawData & data, uint32_t options) 
		{ 
			MRWContainer *mc = (MRWContainer *)m_container;

			if(!mc->prd) {
				return OR_ERROR_NOT_FOUND;
			}
			/* Obtain sensor dimensions from PRD block. */
			uint16_t y = mc->prd->uint16_val (MRW::PRD_SENSOR_LENGTH);
			uint16_t x = mc->prd->uint16_val (MRW::PRD_SENSOR_WIDTH);
			
			bool is_compressed = (mc->prd->uint8_val(MRW::PRD_STORAGE_TYPE) == 0x59);
			/* Allocate space for and retrieve pixel data.
			 * Currently only for cameras that don't compress pixel data.
			 */
			/* Set pixel array parameters. */
			uint32_t finaldatalen = 2 * x * y;
			uint32_t datalen =
				(is_compressed ? x * y + ((x * y) >> 1) : finaldatalen);

			if(options & OR_OPTIONS_DONT_DECOMPRESS) {
				finaldatalen = datalen;
			}
			if(is_compressed && (options & OR_OPTIONS_DONT_DECOMPRESS)) {
				data.setDataType (OR_DATA_TYPE_COMPRESSED_CFA);
			}
			else {
				data.setDataType (OR_DATA_TYPE_CFA);
			}
			Trace(DEBUG1) << "datalen = " << datalen <<
				" final datalen = " << finaldatalen << "\n";
			void *p = data.allocData(finaldatalen);
			size_t fetched = 0;
			off_t offset = mc->pixelDataOffset();
			if(!is_compressed || (options & OR_OPTIONS_DONT_DECOMPRESS)) {
				fetched = m_container->fetchData (p, offset, datalen);
			}
			else {
				Unpack unpack(x, y, IFD::COMPRESS_NONE);
				size_t blocksize = unpack.block_size();
				boost::scoped_array<uint8_t> block(new uint8_t[blocksize]);
				uint8_t * outdata = (uint8_t*)data.data();
				size_t outleft = finaldatalen;
				size_t got;
				do {
					Trace(DEBUG2) << "fatchData @offset " << offset << "\n";
					got = m_container->fetchData (block.get(), 
												  offset, blocksize);
					fetched += got;
					offset += got;
					Trace(DEBUG2) << "got " << got << "\n";
					if(got) {
						size_t out = unpack.unpack_be12to16(outdata, outleft, 
															block.get(), got);
						outdata += out;
						outleft -= out;
						Trace(DEBUG2) << "unpacked " << out
									  << " bytes from " << got << "\n";
					}
				} while((got != 0) && (fetched < datalen));
			}
			if (fetched < datalen) {
				Trace(WARNING) << "Fetched only " << fetched <<
					" of " << datalen << ": continuing anyway.\n";
			}
			uint16_t bpat = mc->prd->uint16_val (MRW::PRD_BAYER_PATTERN);
			or_cfa_pattern cfa_pattern = OR_CFA_PATTERN_NONE;
			switch(bpat) 
			{
			case 0x0001:
				cfa_pattern = OR_CFA_PATTERN_RGGB;
				break;
			case 0x0004:
				cfa_pattern = OR_CFA_PATTERN_GBRG;
				break;
			default:
				break;
			}
			data.setCfaPattern(cfa_pattern);
			data.setDimensions (x, y);

			return OR_ERROR_NONE; 
		}

	}
}