From 05e8255c78045d9ec4f82ec9767c570607f4836d Mon Sep 17 00:00:00 2001 From: Andreas Huggel Date: Sat, 9 May 2009 04:00:43 +0000 Subject: [PATCH] #628: Improved version: insert comments just before SOFn. Updated test cases accordingly. --- src/jpgimage.cpp | 35 +++++++++++++++++++++++++++++++++-- test/data/smiley1.jpg.c1gd | Bin 2969 -> 2969 bytes test/data/smiley1.jpg.c2gd | Bin 2969 -> 2969 bytes test/data/smiley1.jpg.c3gd | Bin 11165 -> 11165 bytes test/data/smiley1.jpg.i1gd | Bin 2969 -> 2969 bytes test/data/smiley1.jpg.i2gd | Bin 2969 -> 2969 bytes test/data/smiley1.jpg.i3gd | Bin 2871 -> 2871 bytes test/data/smiley2.jpg.c1gd | Bin 3309 -> 3309 bytes test/data/smiley2.jpg.c2gd | Bin 3309 -> 3309 bytes test/data/smiley2.jpg.c3gd | Bin 11511 -> 11511 bytes test/data/smiley2.jpg.i1gd | Bin 11609 -> 11609 bytes test/data/smiley2.jpg.i2gd | Bin 11609 -> 11609 bytes test/data/smiley2.jpg.i3gd | Bin 11511 -> 11511 bytes test/data/table.jpg.c1gd | Bin 568 -> 568 bytes test/data/table.jpg.c2gd | Bin 568 -> 568 bytes test/data/table.jpg.c3gd | Bin 8764 -> 8764 bytes test/data/table.jpg.i1gd | Bin 568 -> 568 bytes test/data/table.jpg.i2gd | Bin 568 -> 568 bytes test/data/table.jpg.i3gd | Bin 470 -> 470 bytes 19 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/jpgimage.cpp b/src/jpgimage.cpp index e2c530e2..6a95e412 100644 --- a/src/jpgimage.cpp +++ b/src/jpgimage.cpp @@ -486,6 +486,7 @@ namespace Exiv2 { int count = 0; int search = 0; int insertPos = 0; + int comPos = 0; int skipApp1Exif = -1; int skipApp1Xmp = -1; int skipApp13Ps3 = -1; @@ -503,7 +504,7 @@ namespace Exiv2 { // First find segments of interest. Normally app0 is first and we want // to insert after it. But if app0 comes after com, app1 and app13 then // don't bother. - while (marker != sos_ && marker != eoi_ && search < 4) { + while (marker != sos_ && marker != eoi_ && search < 5) { // Read size and signature (ok if this hits EOF) bufRead = io_->read(buf.pData_, bufMinSize); if (io_->error()) throw Error(20); @@ -556,11 +557,37 @@ namespace Exiv2 { if (size < 2) throw Error(22); if (io_->seek(size-bufRead, BasicIo::cur)) throw Error(22); } + // As in jpeg-6b/wrjpgcom.c: + // We will insert the new comment marker just before SOFn. + // This (a) causes the new comment to appear after, rather than before, + // existing comments; and (b) ensures that comments come after any JFIF + // or JFXX markers, as required by the JFIF specification. + if ( comPos == 0 + && ( marker == sof0_ + || marker == sof1_ + || marker == sof2_ + || marker == sof3_ + || marker == sof5_ + || marker == sof6_ + || marker == sof7_ + || marker == sof9_ + || marker == sof10_ + || marker == sof11_ + || marker == sof13_ + || marker == sof14_ + || marker == sof15_)) { + comPos = count; + ++search; + } marker = advanceToMarker(); if (marker < 0) throw Error(22); ++count; } - + if (comPos == 0) { + if (marker == eoi_) comPos = count; + else comPos = insertPos; + ++search; + } if (exifData_.count() > 0) ++search; if (writeXmpFromPacket() == false && xmpData_.count() > 0) ++search; if (writeXmpFromPacket() == true && xmpPacket_.size() > 0) ++search; @@ -672,7 +699,10 @@ namespace Exiv2 { --search; } } + } + if (comPos == count) { if (!comment_.empty()) { + byte tmpBuf[4]; // Write COM marker, size of comment, and string tmpBuf[0] = 0xff; tmpBuf[1] = com_; @@ -687,6 +717,7 @@ namespace Exiv2 { if (outIo.error()) throw Error(21); --search; } + --search; } if (marker == eoi_) { break; diff --git a/test/data/smiley1.jpg.c1gd b/test/data/smiley1.jpg.c1gd index 79b0df60644b84922414308cfe5c1d930d97ee25..4aa43943921553140483ce7e154110d6270e4bea 100644 GIT binary patch delta 19 bcmbO!K2x0O|Ba1iKbR&mn{3u&&gK9BQ2+-z delta 20 ccmbO!K2x0O|BcB%nM^k_e`ngP$DGXp09|5n;;q@2wG0BGU}^#A|> diff --git a/test/data/smiley1.jpg.i1gd b/test/data/smiley1.jpg.i1gd index 79b0df60644b84922414308cfe5c1d930d97ee25..4aa43943921553140483ce7e154110d6270e4bea 100644 GIT binary patch delta 19 bcmbO!K2x0O|Ba1iKbR&mn{3u&&gK9BQ2+-z delta 20 ccmbO!K2x0O|BcB%nM^k_e`ngP$DGXp09n+a diff --git a/test/data/table.jpg.c2gd b/test/data/table.jpg.c2gd index 3d9332fd6d6c890e7d67994997997da1a5b4c92d..55970117e404a0e4b626a4fd0fb0eb4ca82ef2db 100644 GIT binary patch delta 15 XcmdnNvV&!U=tj?Xj1xso7;gdqFG2;o delta 16 YcmdnNvV&!U=wxOllZ~S97#VK@04^~Fc>n+a diff --git a/test/data/table.jpg.c3gd b/test/data/table.jpg.c3gd index 765579ba828904a41d3f0e4db38a74a0d8bdd41f..0696dd87a0dd75c1cae9c6ba185915cfa35af82e 100644 GIT binary patch delta 15 Xcmdnvvd3kD=tj>^iW5am7;gdqHx348 delta 16 Ycmdnvvd3kD=wx;!lZ~RE6d7*<05?4bNdN!< diff --git a/test/data/table.jpg.i1gd b/test/data/table.jpg.i1gd index 718515b633527c12d1e2a625fee3bc68663efd4c..c0f8bbb4d0b9d9e73c6789d911035da705c19cc1 100644 GIT binary patch delta 15 XcmdnNvV&!U=tj?Xj1xso7;gdqFG2;o delta 16 YcmdnNvV&!U=wxOllZ~S97#VK@04^~Fc>n+a diff --git a/test/data/table.jpg.i2gd b/test/data/table.jpg.i2gd index 3d9332fd6d6c890e7d67994997997da1a5b4c92d..55970117e404a0e4b626a4fd0fb0eb4ca82ef2db 100644 GIT binary patch delta 15 XcmdnNvV&!U=tj?Xj1xso7;gdqFG2;o delta 16 YcmdnNvV&!U=wxOllZ~S97#VK@04^~Fc>n+a diff --git a/test/data/table.jpg.i3gd b/test/data/table.jpg.i3gd index e39baf544412262e80d176495e325d9c95450cbd..b5ce9730d70503127dfc22d68aa6c10be9c5b78a 100644 GIT binary patch delta 15 Wcmcb{e2sa6=tj>T#)+aPj5h%