File: mrwcontainer.cpp

package info (click to toggle)
libopenraw 0.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,964 kB
  • ctags: 1,936
  • sloc: sh: 10,199; cpp: 9,279; ansic: 1,466; makefile: 544; xml: 465; perl: 42
file content (238 lines) | stat: -rw-r--r-- 6,665 bytes parent folder | download
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
/*
 * libopenraw - mrwcontainer.cpp
 *
 * Copyright (C) 2006 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 "debug.h"
#include "mrwcontainer.h"
#include "io/file.h"


using namespace Debug;

namespace OpenRaw {

	namespace Internals {

		namespace MRW {

			DataBlock::DataBlock(off_t start, MRWContainer * _container)
				: m_start(start),
				  m_container(_container),
				  m_loaded(false)
			{
				Trace(DEBUG2) << "> DataBlock start == " << start << "\n";
				if (m_container->fetchData (m_name, m_start, 4) != 4) {
					// FIXME: Handle error
					Trace(WARNING) << "  Error reading block name " << start << "\n";
					return;
				}
				if (!m_container->readInt32 (m_container->file(), m_length)) {
					// FIXME: Handle error
					Trace(WARNING) << "  Error reading block length " << start << "\n";
					return;
				}
				Trace(DEBUG1) << "  DataBlock " << name() 
							  << ", length " << m_length 
							  << " at " << m_start << "\n";
				Trace(DEBUG2) << "< DataBlock\n";
				m_loaded = true;
			}

			int8_t DataBlock::int8_val (off_t off)
			{
				int8_t ret;
				MRWContainer *mc = m_container;
				mc->file()->seek (m_start + DataBlockHeaderLength + off, SEEK_SET);
				mc->readInt8 (mc->file(), ret);
				return ret;
			}
			
			uint8_t DataBlock::uint8_val (off_t off)
			{
				uint8_t ret;
				MRWContainer *mc = m_container;
				mc->file()->seek (m_start + DataBlockHeaderLength + off, SEEK_SET);
				mc->readUInt8 (mc->file(), ret);
				return ret;
			}
			
			uint16_t DataBlock::uint16_val (off_t off)
			{
				uint16_t ret;
				MRWContainer *mc = m_container;
				mc->file()->seek (m_start + DataBlockHeaderLength + off, SEEK_SET);
				mc->readUInt16 (mc->file(), ret);
				return ret;
			}
			
			std::string DataBlock::string_val(off_t off)
			{
				char buf[9];
				size_t s;
				MRWContainer *mc = m_container;
				s = mc->fetchData(buf, m_start + DataBlockHeaderLength + off, 8);
				if(s == 8) {
					buf[8] = 0;
				}
				else {
					*buf = 0;
				}
				return buf;
			}
		}

		MRWContainer::MRWContainer(IO::Stream *_file, off_t offset)
			: IFDFileContainer(_file, offset)
		{

		}


		MRWContainer::~MRWContainer()
		{
		}


		IFDFileContainer::EndianType 
		MRWContainer::isMagicHeader(const char *p, int len)
		{
			if (len < 4) {
				// we need at least 4 bytes to check
				return ENDIAN_NULL;
			}

			if ((p[0] == 0x00) && (p[1] == 'M') && 
				(p[2] == 'R') && (p[3] == 'M')) {

				Trace(DEBUG1) << "Identified MRW file\n";

				return ENDIAN_BIG;
			}

			Trace(DEBUG1) << "Unidentified MRW file\n";

			return ENDIAN_NULL;
		}

		bool MRWContainer::locateDirsPreHook()
		{
			char version[9];
			off_t position;

			Trace(DEBUG1) << "> MRWContainer::locateDirsPreHook()\n";
			m_endian = ENDIAN_BIG;
			
			/* MRW file always starts with an MRM datablock. */
			mrm = MRW::DataBlock::Ref (new MRW::DataBlock (m_offset, this));
			if (mrm->name() != "MRM") {
				Trace(WARNING) << "MRW file begins not with MRM block, "
					"but with unrecognized DataBlock :: name == " 
							   << mrm->name() << "\n";
				return false;
			}

			/* Subblocks are contained within the MRM block. Scan them and create
			 * appropriate block descriptors.
			 */
			position = mrm->offset() + MRW::DataBlockHeaderLength;
			while (position < pixelDataOffset()) {
				MRW::DataBlock::Ref ref (new MRW::DataBlock (position, this));
				Trace(DEBUG1) << "Loaded DataBlock :: name == " << ref->name() << "\n";
				if(!ref || !ref->loaded()) {
					break;
				}
				if (ref->name() == "PRD") {
				    if (prd != NULL) {
						Trace(WARNING) << "File contains duplicate DataBlock :: name == " 
									   << ref->name() << "\n";
					}
				    prd = ref;
				}
				else if (ref->name() == "TTW") {
				    if (ttw != NULL) {
						Trace(WARNING) << "File contains duplicate DataBlock :: name == " 
									   << ref->name() << "\n";
					}
				    ttw = ref;
				}
				else if (ref->name() == "WBG") {
				    if (wbg != NULL) {
						Trace(WARNING) << "File contains duplicate DataBlock :: name == " 
									   << ref->name() << "\n";
					}
				    wbg = ref;
				}
				else if (ref->name() == "RIF") {
				    if (rif != NULL) {
						Trace(WARNING) << "File contains duplicate DataBlock :: name == " 
									   << ref->name() << "\n";
					}
				    rif = ref;
				}
				else if (ref->name() != "PAD") {
				    Trace(WARNING) << "File contains unrecognized DataBlock :: name == " 
								   << ref->name() << "\n";
				}
				position = ref->offset() + MRW::DataBlockHeaderLength + ref->length();
			}

			/* Check that we found all the expected data blocks. */
			if (prd == NULL) {
				Trace(WARNING) << "File does NOT contain expected DataBlock :: name == PRD\n";
				return false;
			}
			if (ttw == NULL) {
				Trace(WARNING) << "File does NOT contain expected DataBlock :: name == TTW\n";
				return false;
			}
			if (wbg == NULL) {
				Trace(WARNING) << "File does NOT contain expected DataBlock :: name == WBG\n";
				return false;
			}
			if (rif == NULL) {
				Trace(WARNING) << "File does NOT contain expected DataBlock :: name == RIF\n";
				return false;
			}

			/* Extract the file version string. */
			if (fetchData (version, prd->offset()+MRW::DataBlockHeaderLength+MRW::PRD_VERSION, 8) != 8) {
				// FIXME: Handle error
				Debug::Trace(DEBUG1) << "  Error reading version string\n";
			}
			version[8] = '\0';
			m_version = std::string (version);
			Trace(DEBUG1) << "  MRW file version == " << m_version << "\n";

			/* For the benefit of our parent class, set the container offset to the beginning of
			 * the TIFF data (the contents of the TTW data block), and seek there.
			 */
			m_offset = ttw->offset() + MRW::DataBlockHeaderLength;
			if((version[2] != '7') || (version[3] != '3')) {
				setExifOffsetCorrection(m_offset);
				Trace(DEBUG1) << "setting correction to " << m_offset << "\n";
			}
			m_file->seek (m_offset, SEEK_SET);
			Trace(DEBUG1) << "< MRWContainer\n";
			
			return true;
		}

	}
}