forked from Exiv2/exiv2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmrwimage.cpp
186 lines (165 loc) · 6.2 KB
/
mrwimage.cpp
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
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2018 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
/*
File: mrwimage.cpp
Author(s): Andreas Huggel (ahu) <[email protected]>
History: 13-May-06, ahu: created
Credits: See header file
*/
// *****************************************************************************
// included header files
#include "config.h"
#include "mrwimage.hpp"
#include "tiffimage.hpp"
#include "image.hpp"
#include "basicio.hpp"
#include "error.hpp"
#include "enforce.hpp"
#include "futils.hpp"
// + standard includes
#include <string>
#include <cstring>
#include <iostream>
#include <cassert>
// *****************************************************************************
// class member definitions
namespace Exiv2 {
MrwImage::MrwImage(BasicIo::UniquePtr io, bool /*create*/)
: Image(ImageType::mrw, mdExif | mdIptc | mdXmp, std::move(io))
{
} // MrwImage::MrwImage
std::string MrwImage::mimeType() const
{
return "image/x-minolta-mrw";
}
int MrwImage::pixelWidth() const
{
ExifData::const_iterator imageWidth = exifData_.findKey(Exiv2::ExifKey("Exif.Image.ImageWidth"));
if (imageWidth != exifData_.end() && imageWidth->count() > 0) {
return imageWidth->toLong();
}
return 0;
}
int MrwImage::pixelHeight() const
{
ExifData::const_iterator imageHeight = exifData_.findKey(Exiv2::ExifKey("Exif.Image.ImageLength"));
if (imageHeight != exifData_.end() && imageHeight->count() > 0) {
return imageHeight->toLong();
}
return 0;
}
void MrwImage::setExifData(const ExifData& /*exifData*/)
{
// Todo: implement me!
throw(Error(kerInvalidSettingForImage, "Exif metadata", "MRW"));
}
void MrwImage::setIptcData(const IptcData& /*iptcData*/)
{
// Todo: implement me!
throw(Error(kerInvalidSettingForImage, "IPTC metadata", "MRW"));
}
void MrwImage::setComment(const std::string& /*comment*/)
{
// not supported
throw(Error(kerInvalidSettingForImage, "Image comment", "MRW"));
}
void MrwImage::readMetadata()
{
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Reading MRW file " << io_->path() << "\n";
#endif
if (io_->open() != 0) {
throw Error(kerDataSourceOpenFailed, io_->path(), strError());
}
IoCloser closer(*io_);
// Ensure that this is the correct image type
if (!isMrwType(*io_, false)) {
if (io_->error() || io_->eof()) throw Error(kerFailedToReadImageData);
throw Error(kerNotAnImage, "MRW");
}
clearMetadata();
// Find the TTW block and read it into a buffer
uint32_t const len = 8;
byte tmp[len];
io_->read(tmp, len);
uint32_t pos = len;
uint32_t const end = getULong(tmp + 4, bigEndian);
pos += len;
enforce(pos <= end, kerFailedToReadImageData);
io_->read(tmp, len);
if (io_->error() || io_->eof()) throw Error(kerFailedToReadImageData);
while (memcmp(tmp + 1, "TTW", 3) != 0) {
uint32_t const siz = getULong(tmp + 4, bigEndian);
enforce(siz <= end - pos, kerFailedToReadImageData);
pos += siz;
io_->seek(siz, BasicIo::cur);
enforce(!io_->error() && !io_->eof(), kerFailedToReadImageData);
enforce(len <= end - pos, kerFailedToReadImageData);
pos += len;
io_->read(tmp, len);
enforce(!io_->error() && !io_->eof(), kerFailedToReadImageData);
}
const uint32_t siz = getULong(tmp + 4, bigEndian);
// First do an approximate bounds check of siz, so that we don't
// get DOS-ed by a 4GB allocation on the next line. If siz is
// greater than io_->size() then it is definitely invalid. But the
// exact bounds checking is done by the call to io_->read, which
// will fail if there are fewer than siz bytes left to read.
enforce(siz <= io_->size(), kerFailedToReadImageData);
DataBuf buf(siz);
io_->read(buf.pData_, buf.size_);
enforce(!io_->error() && !io_->eof(), kerFailedToReadImageData);
ByteOrder bo = TiffParser::decode(exifData_,
iptcData_,
xmpData_,
buf.pData_,
(uint32_t)buf.size_);
setByteOrder(bo);
} // MrwImage::readMetadata
void MrwImage::writeMetadata()
{
// Todo: implement me!
throw(Error(kerWritingImageFormatUnsupported, "MRW"));
} // MrwImage::writeMetadata
// *************************************************************************
// free functions
Image::UniquePtr newMrwInstance(BasicIo::UniquePtr io, bool create)
{
Image::UniquePtr image(new MrwImage(std::move(io), create));
if (!image->good()) {
image.reset();
}
return image;
}
bool isMrwType(BasicIo& iIo, bool advance)
{
const int32_t len = 4;
byte buf[len];
iIo.read(buf, len);
if (iIo.error() || iIo.eof()) {
return false;
}
int rc = memcmp(buf, "\0MRM", 4);
if (!advance || rc != 0) {
iIo.seek(-len, BasicIo::cur);
}
return rc == 0;
}
} // namespace Exiv2