1
// ***************************************************************** -*- C++ -*-
3
* Copyright (C) 2004, 2005 Andreas Huggel <ahuggel@gmx.net>
5
* This program is part of the Exiv2 distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program 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
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
25
Brad Schick (brad) <brad@robotbattle.com>
26
History: 15-Jan-05, brad: split out from image.cpp
29
// *****************************************************************************
31
EXIV2_RCSID("@(#) $Id: jpgimage.cpp 563 2005-04-21 07:21:53Z ahuggel $");
33
// *****************************************************************************
34
// included header files
36
# include "exv_msvc.h"
38
# include "exv_conf.h"
41
#include "jpgimage.hpp"
45
// + standard includes
49
// *****************************************************************************
50
// class member definitions
53
// Local functions. These could be static private functions on Image
54
// subclasses but then ImageFactory needs to be made a friend.
56
@brief Create a new ExvImage instance and return an auto-pointer to it.
57
Caller owns the returned object and the auto-pointer ensures that
60
Image::AutoPtr newExvInstance(BasicIo::AutoPtr io, bool create);
61
//! Check if the file iIo is an EXV file
62
bool isExvType(BasicIo& iIo, bool advance);
64
@brief Create a new JpegImage instance and return an auto-pointer to it.
65
Caller owns the returned object and the auto-pointer ensures that
68
Image::AutoPtr newJpegInstance(BasicIo::AutoPtr io, bool create);
69
//! Check if the file iIo is a JPEG image.
70
bool isJpegType(BasicIo& iIo, bool advance);
72
const byte JpegBase::sos_ = 0xda;
73
const byte JpegBase::eoi_ = 0xd9;
74
const byte JpegBase::app0_ = 0xe0;
75
const byte JpegBase::app1_ = 0xe1;
76
const byte JpegBase::app13_ = 0xed;
77
const byte JpegBase::com_ = 0xfe;
78
const uint16_t JpegBase::iptc_ = 0x0404;
79
const char JpegBase::exifId_[] = "Exif\0\0";
80
const char JpegBase::jfifId_[] = "JFIF\0";
81
const char JpegBase::ps3Id_[] = "Photoshop 3.0\0";
82
const char JpegBase::bimId_[] = "8BIM";
84
JpegBase::JpegBase(BasicIo::AutoPtr io, bool create,
85
const byte initData[], long dataSize)
89
initImage(initData, dataSize);
93
int JpegBase::initImage(const byte initData[], long dataSize)
95
if (io_->open() != 0) {
98
IoCloser closer(*io_);
99
if (io_->write(initData, dataSize) != dataSize) {
105
bool JpegBase::good() const
107
if (io_->open() != 0) return false;
108
IoCloser closer(*io_);
109
return isThisType(*io_, false);
112
void JpegBase::clearMetadata()
119
void JpegBase::clearIptcData()
124
void JpegBase::clearExifData()
129
void JpegBase::clearComment()
134
void JpegBase::setExifData(const ExifData& exifData)
136
exifData_ = exifData;
139
void JpegBase::setIptcData(const IptcData& iptcData)
141
iptcData_ = iptcData;
144
void JpegBase::setComment(const std::string& comment)
149
void JpegBase::setMetadata(const Image& image)
151
setIptcData(image.iptcData());
152
setExifData(image.exifData());
153
setComment(image.comment());
156
int JpegBase::advanceToMarker() const
159
// Skips potential padding between markers
160
while ((c=io_->getb()) != 0xff) {
161
if (c == EOF) return -1;
164
// Markers can start with any number of 0xff
165
while ((c=io_->getb()) == 0xff) {
166
if (c == EOF) return -1;
171
void JpegBase::readMetadata()
173
if (io_->open() != 0) {
174
throw Error(9, io_->path(), strError());
176
IoCloser closer(*io_);
177
// Ensure that this is the correct image type
178
if (!isThisType(*io_, true)) {
179
if (io_->error() || io_->eof()) throw Error(14);
184
const long bufMinSize = 16;
186
DataBuf buf(bufMinSize);
188
// Read section marker
189
int marker = advanceToMarker();
190
if (marker < 0) throw Error(15);
192
while (marker != sos_ && marker != eoi_ && search > 0) {
193
// Read size and signature (ok if this hits EOF)
194
bufRead = io_->read(buf.pData_, bufMinSize);
195
if (io_->error()) throw Error(14);
196
uint16_t size = getUShort(buf.pData_, bigEndian);
198
if (marker == app1_ && memcmp(buf.pData_ + 2, exifId_, 6) == 0) {
199
if (size < 8) throw Error(15);
200
// Seek to begining and read the Exif data
201
io_->seek(8-bufRead, BasicIo::cur);
202
long sizeExifData = size - 8;
203
DataBuf rawExif(sizeExifData);
204
io_->read(rawExif.pData_, sizeExifData);
205
if (io_->error() || io_->eof()) throw Error(14);
206
if (exifData_.load(rawExif.pData_, sizeExifData)) throw Error(15);
209
else if (marker == app13_ && memcmp(buf.pData_ + 2, ps3Id_, 14) == 0) {
210
if (size < 16) throw Error(15);
211
// Read the rest of the APP13 segment
212
// needed if bufMinSize!=16: io_->seek(16-bufRead, BasicIo::cur);
213
DataBuf psData(size - 16);
214
io_->read(psData.pData_, psData.size_);
215
if (io_->error() || io_->eof()) throw Error(14);
216
const byte *record = 0;
217
uint16_t sizeIptc = 0;
218
uint16_t sizeHdr = 0;
219
// Find actual Iptc data within the APP13 segment
220
if (!locateIptcData(psData.pData_, psData.size_, &record,
221
&sizeHdr, &sizeIptc)) {
223
if (iptcData_.load(record + sizeHdr, sizeIptc)) throw Error(15);
227
else if (marker == com_ && comment_.empty())
229
if (size < 2) throw Error(15);
230
// Jpegs can have multiple comments, but for now only read
231
// the first one (most jpegs only have one anyway). Comments
232
// are simple single byte ISO-8859-1 strings.
233
io_->seek(2-bufRead, BasicIo::cur);
235
io_->read(buf.pData_, size-2);
236
if (io_->error() || io_->eof()) throw Error(14);
237
comment_.assign(reinterpret_cast<char*>(buf.pData_), size-2);
238
while ( comment_.length()
239
&& comment_.at(comment_.length()-1) == '\0') {
240
comment_.erase(comment_.length()-1);
245
if (size < 2) throw Error(15);
246
// Skip the remainder of the unknown segment
247
if (io_->seek(size-bufRead, BasicIo::cur)) throw Error(15);
249
// Read the beginning of the next segment
250
marker = advanceToMarker();
251
if (marker < 0) throw Error(15);
253
} // JpegBase::readMetadata
256
// Operates on raw data (rather than file streams) to simplify reuse
257
int JpegBase::locateIptcData(const byte *pPsData,
260
uint16_t *const sizeHdr,
261
uint16_t *const sizeIptc) const
266
// Used for error checking
269
// Data should follow Photoshop format, if not exit
270
while (position <= (sizePsData - 14) &&
271
memcmp(pPsData + position, bimId_, 4)==0) {
272
const byte *hrd = pPsData + position;
274
uint16_t type = getUShort(pPsData+ position, bigEndian);
277
// Pascal string is padded to have an even size (including size byte)
278
byte psSize = pPsData[position] + 1;
279
psSize += (psSize & 1);
281
if (position >= sizePsData) return -2;
283
// Data is also padded to be even
284
long dataSize = getULong(pPsData + position, bigEndian);
286
if (dataSize > sizePsData - position) return -2;
289
*sizeIptc = static_cast<uint16_t>(dataSize);
290
*sizeHdr = psSize + 10;
294
position += dataSize + (dataSize & 1);
297
} // JpegBase::locateIptcData
299
void JpegBase::writeMetadata()
301
if (io_->open() != 0) {
302
throw Error(9, io_->path(), strError());
304
IoCloser closer(*io_);
305
BasicIo::AutoPtr tempIo(io_->temporary()); // may throw
306
assert (tempIo.get() != 0);
308
doWriteMetadata(*tempIo); // may throw
310
io_->transfer(*tempIo); // may throw
311
} // JpegBase::writeMetadata
313
void JpegBase::doWriteMetadata(BasicIo& outIo)
315
if (!io_->isopen()) throw Error(20);
316
if (!outIo.isopen()) throw Error(21);
318
// Ensure that this is the correct image type
319
if (!isThisType(*io_, true)) {
320
if (io_->error() || io_->eof()) throw Error(20);
324
const long bufMinSize = 16;
326
DataBuf buf(bufMinSize);
327
const long seek = io_->tell();
331
int skipApp1Exif = -1;
332
int skipApp13Ps3 = -1;
336
// Write image header
337
if (writeHeader(outIo)) throw Error(21);
339
// Read section marker
340
int marker = advanceToMarker();
341
if (marker < 0) throw Error(22);
343
// First find segments of interest. Normally app0 is first and we want
344
// to insert after it. But if app0 comes after com, app1 and app13 then
346
while (marker != sos_ && marker != eoi_ && search < 3) {
347
// Read size and signature (ok if this hits EOF)
348
bufRead = io_->read(buf.pData_, bufMinSize);
349
if (io_->error()) throw Error(20);
350
uint16_t size = getUShort(buf.pData_, bigEndian);
352
if (marker == app0_) {
353
if (size < 2) throw Error(22);
354
insertPos = count + 1;
355
if (io_->seek(size-bufRead, BasicIo::cur)) throw Error(22);
357
else if (marker == app1_ && memcmp(buf.pData_ + 2, exifId_, 6) == 0) {
358
if (size < 8) throw Error(22);
359
skipApp1Exif = count;
361
if (io_->seek(size-bufRead, BasicIo::cur)) throw Error(22);
363
else if (marker == app13_ && memcmp(buf.pData_ + 2, ps3Id_, 14) == 0) {
364
if (size < 16) throw Error(22);
365
skipApp13Ps3 = count;
367
// needed if bufMinSize!=16: io_->seek(16-bufRead, BasicIo::cur);
368
psData.alloc(size - 16);
369
// Load PS data now to allow reinsertion at any point
370
io_->read(psData.pData_, psData.size_);
371
if (io_->error() || io_->eof()) throw Error(20);
373
else if (marker == com_ && skipCom == -1) {
374
if (size < 2) throw Error(22);
375
// Jpegs can have multiple comments, but for now only handle
376
// the first one (most jpegs only have one anyway).
379
if (io_->seek(size-bufRead, BasicIo::cur)) throw Error(22);
382
if (size < 2) throw Error(22);
383
if (io_->seek(size-bufRead, BasicIo::cur)) throw Error(22);
385
marker = advanceToMarker();
386
if (marker < 0) throw Error(22);
390
if (exifData_.count() > 0) ++search;
391
if (iptcData_.count() > 0) ++search;
392
if (!comment_.empty()) ++search;
394
io_->seek(seek, BasicIo::beg);
396
marker = advanceToMarker();
397
if (marker < 0) throw Error(22);
399
// To simplify this a bit, new segments are inserts at either the start
400
// or right after app0. This is standard in most jpegs, but has the
401
// potential to change segment ordering (which is allowed).
402
// Segments are erased if there is no assigned metadata.
403
while (marker != sos_ && search > 0) {
404
// Read size and signature (ok if this hits EOF)
405
bufRead = io_->read(buf.pData_, bufMinSize);
406
if (io_->error()) throw Error(20);
407
// Careful, this can be a meaningless number for empty
408
// images with only an eoi_ marker
409
uint16_t size = getUShort(buf.pData_, bigEndian);
411
if (insertPos == count) {
413
if (!comment_.empty()) {
414
// Write COM marker, size of comment, and string
418
static_cast<uint16_t>(comment_.length()+3), bigEndian);
419
if (outIo.write(tmpBuf, 4) != 4) throw Error(21);
420
if (outIo.write((byte*)comment_.data(), (long)comment_.length())
421
!= (long)comment_.length()) throw Error(21);
422
if (outIo.putb(0)==EOF) throw Error(21);
423
if (outIo.error()) throw Error(21);
426
if (exifData_.count() > 0) {
427
// Write APP1 marker, size of APP1 field, Exif id and Exif data
428
DataBuf rawExif(exifData_.copy());
432
static_cast<uint16_t>(rawExif.size_+8),
434
memcpy(tmpBuf + 4, exifId_, 6);
435
if (outIo.write(tmpBuf, 10) != 10) throw Error(21);
436
if (outIo.write(rawExif.pData_, rawExif.size_)
437
!= rawExif.size_) throw Error(21);
438
if (outIo.error()) throw Error(21);
442
const byte *record = psData.pData_;
443
uint16_t sizeIptc = 0;
444
uint16_t sizeHdr = 0;
445
// Safe to call with zero psData.size_
446
locateIptcData(psData.pData_, psData.size_, &record, &sizeHdr, &sizeIptc);
448
// Data is rounded to be even
449
const int sizeOldData = sizeHdr + sizeIptc + (sizeIptc & 1);
450
if (psData.size_ > sizeOldData || iptcData_.count() > 0) {
451
// rawIptc may have size of zero.
452
DataBuf rawIptc(iptcData_.copy());
453
// write app13 marker, new size, and ps3Id
456
const int sizeNewData = rawIptc.size_ ?
457
rawIptc.size_ + (rawIptc.size_ & 1) + 12 : 0;
459
static_cast<uint16_t>(psData.size_-sizeOldData+sizeNewData+16),
461
memcpy(tmpBuf + 4, ps3Id_, 14);
462
if (outIo.write(tmpBuf, 18) != 18) throw Error(21);
463
if (outIo.error()) throw Error(21);
465
const long sizeFront = (long)(record - psData.pData_);
466
const long sizeEnd = psData.size_ - sizeFront - sizeOldData;
467
// write data before old record.
468
if (outIo.write(psData.pData_, sizeFront) != sizeFront) throw Error(21);
470
// write new iptc record if we have it
471
if (iptcData_.count() > 0) {
472
memcpy(tmpBuf, bimId_, 4);
473
us2Data(tmpBuf+4, iptc_, bigEndian);
476
ul2Data(tmpBuf + 8, rawIptc.size_, bigEndian);
477
if (outIo.write(tmpBuf, 12) != 12) throw Error(21);
478
if (outIo.write(rawIptc.pData_, rawIptc.size_)
479
!= rawIptc.size_) throw Error(21);
480
// data is padded to be even (but not included in size)
481
if (rawIptc.size_ & 1) {
482
if (outIo.putb(0)==EOF) throw Error(21);
484
if (outIo.error()) throw Error(21);
488
// write existing stuff after record
489
if (outIo.write(record+sizeOldData, sizeEnd)
490
!= sizeEnd) throw Error(21);
491
if (outIo.error()) throw Error(21);
494
if (marker == eoi_) {
497
else if (skipApp1Exif==count || skipApp13Ps3==count || skipCom==count) {
499
io_->seek(size-bufRead, BasicIo::cur);
502
if (size < 2) throw Error(22);
504
io_->seek(-bufRead-2, BasicIo::cur);
505
io_->read(buf.pData_, size+2);
506
if (io_->error() || io_->eof()) throw Error(20);
507
if (outIo.write(buf.pData_, size+2) != size+2) throw Error(21);
508
if (outIo.error()) throw Error(21);
512
marker = advanceToMarker();
513
if (marker < 0) throw Error(22);
517
// Copy rest of the Io
518
io_->seek(-2, BasicIo::cur);
521
while ((readSize=io_->read(buf.pData_, buf.size_))) {
522
if (outIo.write(buf.pData_, readSize) != readSize) throw Error(21);
524
if (outIo.error()) throw Error(21);
526
} // JpegBase::doWriteMetadata
529
const byte JpegImage::soi_ = 0xd8;
530
const byte JpegImage::blank_[] = {
531
0xFF,0xD8,0xFF,0xDB,0x00,0x84,0x00,0x10,0x0B,0x0B,0x0B,0x0C,0x0B,0x10,0x0C,0x0C,
532
0x10,0x17,0x0F,0x0D,0x0F,0x17,0x1B,0x14,0x10,0x10,0x14,0x1B,0x1F,0x17,0x17,0x17,
533
0x17,0x17,0x1F,0x1E,0x17,0x1A,0x1A,0x1A,0x1A,0x17,0x1E,0x1E,0x23,0x25,0x27,0x25,
534
0x23,0x1E,0x2F,0x2F,0x33,0x33,0x2F,0x2F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,
535
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x01,0x11,0x0F,0x0F,0x11,0x13,0x11,0x15,0x12,
536
0x12,0x15,0x14,0x11,0x14,0x11,0x14,0x1A,0x14,0x16,0x16,0x14,0x1A,0x26,0x1A,0x1A,
537
0x1C,0x1A,0x1A,0x26,0x30,0x23,0x1E,0x1E,0x1E,0x1E,0x23,0x30,0x2B,0x2E,0x27,0x27,
538
0x27,0x2E,0x2B,0x35,0x35,0x30,0x30,0x35,0x35,0x40,0x40,0x3F,0x40,0x40,0x40,0x40,
539
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0xFF,0xC0,0x00,0x11,0x08,0x00,0x01,0x00,
540
0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,0x01,0xFF,0xC4,0x00,0x4B,0x00,
541
0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
542
0x00,0x07,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
543
0x00,0x00,0x00,0x00,0x10,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
544
0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
545
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xDA,0x00,0x0C,0x03,0x01,0x00,0x02,
546
0x11,0x03,0x11,0x00,0x3F,0x00,0xA0,0x00,0x0F,0xFF,0xD9 };
548
JpegImage::JpegImage(BasicIo::AutoPtr io, bool create)
549
: JpegBase(io, create, blank_, sizeof(blank_))
554
JpegImage::JpegRegister::JpegRegister()
556
ImageFactory::registerImage(
557
Image::jpeg, newJpegInstance, isJpegType);
561
int JpegImage::writeHeader(BasicIo& outIo) const
567
if (outIo.write(tmpBuf, 2) != 2) return 4;
568
if (outIo.error()) return 4;
572
bool JpegImage::isThisType(BasicIo& iIo, bool advance) const
574
return isJpegType(iIo, advance);
577
Image::AutoPtr newJpegInstance(BasicIo::AutoPtr io, bool create)
579
Image::AutoPtr image = Image::AutoPtr(new JpegImage(io, create));
580
if (!image->good()) {
586
bool isJpegType(BasicIo& iIo, bool advance)
591
if (iIo.error() || iIo.eof()) return false;
593
if (0xff!=tmpBuf[0] || JpegImage::soi_!=tmpBuf[1]) {
596
if (!advance || !result ) iIo.seek(-2, BasicIo::cur);
600
const char ExvImage::exiv2Id_[] = "Exiv2";
601
const byte ExvImage::blank_[] = { 0xff,0x01,'E','x','i','v','2',0xff,0xd9 };
603
ExvImage::ExvImage(BasicIo::AutoPtr io, bool create)
604
: JpegBase(io, create, blank_, sizeof(blank_))
609
ExvImage::ExvRegister::ExvRegister()
611
ImageFactory::registerImage(
612
Image::exv, newExvInstance, isExvType);
616
int ExvImage::writeHeader(BasicIo& outIo) const
622
memcpy(tmpBuf + 2, exiv2Id_, 5);
623
if (outIo.write(tmpBuf, 7) != 7) return 4;
624
if (outIo.error()) return 4;
628
bool ExvImage::isThisType(BasicIo& iIo, bool advance) const
630
return isExvType(iIo, advance);
633
Image::AutoPtr newExvInstance(BasicIo::AutoPtr io, bool create)
635
Image::AutoPtr image;
637
image = Image::AutoPtr(new ExvImage(io, true));
640
image = Image::AutoPtr(new ExvImage(io, false));
642
if (!image->good()) image.reset();
646
bool isExvType(BasicIo& iIo, bool advance)
651
if (iIo.error() || iIo.eof()) return false;
653
if ( 0xff != tmpBuf[0] || 0x01 != tmpBuf[1]
654
|| memcmp(tmpBuf + 2, ExvImage::exiv2Id_, 5) != 0) {
657
if (!advance || !result ) iIo.seek(-7, BasicIo::cur);