#1074 Work in progress. fixed pngimage.cpp doWriteMetadata().

v0.27.3
Robin Mills 9 years ago
parent e84513dbf4
commit f2ec4ce36a

@ -541,21 +541,6 @@ namespace Exiv2 {
} // PngChunk::makeAsciiTxtChunk } // PngChunk::makeAsciiTxtChunk
std::string PngChunk::makeICCPChunkHeader(const std::string& keyword,long compressedLength)
{
// Chunk structure: length (4 bytes) + chunk type
std::string chunkData = keyword + '\0';
std::string chunkType;
chunkData += '\0' ;// byte 0 = standard compression
chunkType = "iCCP";
// Determine length of the chunk data
byte length[4];
ul2Data(length, static_cast<uint32_t>(chunkData.size()+compressedLength), bigEndian);
// Assemble chunk
return std::string((const char*)length, 4) + chunkType + chunkData;
} // PngChunk::makeICCPChunkHeader
std::string PngChunk::makeUtf8TxtChunk(const std::string& keyword, std::string PngChunk::makeUtf8TxtChunk(const std::string& keyword,
const std::string& text, const std::string& text,
bool compress) bool compress)

@ -116,17 +116,6 @@ namespace Exiv2 {
static std::string makeMetadataChunk(const std::string& metadata, static std::string makeMetadataChunk(const std::string& metadata,
MetadataId type); MetadataId type);
/*!
@brief Return PNG iCCP chunk header
(length + chunk type) as a string.
@param keyword Keyword for the PNG iCCP chunk
@param compressLength length of compress data to follow chunk header
@return string containing the PNG chunk header
*/
static std::string makeICCPChunkHeader(const std::string& keyword,long compressedLength);
private: private:
/*! /*!
@brief Parse PNG Text chunk to determine type and extract content. @brief Parse PNG Text chunk to determine type and extract content.

@ -21,11 +21,9 @@
/* /*
File: pngimage.cpp File: pngimage.cpp
Version: $Rev$ Version: $Rev$
Author(s): Gilles Caulier (cgilles) <caulier dot gilles at gmail dot com>
History: 12-Jun-06, gc: submitted
Credits: See header file
*/ */
// ***************************************************************************** // *****************************************************************************
#include "rcsid_int.hpp" #include "rcsid_int.hpp"
EXIV2_RCSID("@(#) $Id$") EXIV2_RCSID("@(#) $Id$")
@ -53,7 +51,6 @@ EXIV2_RCSID("@(#) $Id$")
#include <cassert> #include <cassert>
#include <zlib.h> // To uncompress IccProfiles #include <zlib.h> // To uncompress IccProfiles
// Signature from front of PNG file // Signature from front of PNG file
const unsigned char pngSignature[8] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; const unsigned char pngSignature[8] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
@ -106,6 +103,12 @@ namespace Exiv2 {
do { do {
result.alloc(uncompressedLen); result.alloc(uncompressedLen);
zlibResult = uncompress((Bytef*)result.pData_,&uncompressedLen,bytes,length); zlibResult = uncompress((Bytef*)result.pData_,&uncompressedLen,bytes,length);
// if result buffer is large than necessary, redo to fit perfectly.
if (zlibResult == Z_OK && (long) uncompressedLen < result.size_ ) {
result.release();
result.alloc(uncompressedLen);
zlibResult = uncompress((Bytef*)result.pData_,&uncompressedLen,bytes,length);
}
if (zlibResult == Z_BUF_ERROR) { if (zlibResult == Z_BUF_ERROR) {
// the uncompressed buffer needs to be larger // the uncompressed buffer needs to be larger
result.release(); result.release();
@ -132,6 +135,7 @@ namespace Exiv2 {
result.release(); result.release();
compressedLen *= 2; compressedLen *= 2;
} else { } else {
result.release();
result.alloc(compressedLen); result.alloc(compressedLen);
zlibResult = compress((Bytef*)result.pData_,&compressedLen,bytes,length); zlibResult = compress((Bytef*)result.pData_,&compressedLen,bytes,length);
} }
@ -512,6 +516,10 @@ namespace Exiv2 {
if (io_->error()) throw Error(14); if (io_->error()) throw Error(14);
if (bufRead != (long)(dataOffset + 4)) throw Error(20); if (bufRead != (long)(dataOffset + 4)) throw Error(20);
char szChunk[5];
memcpy(szChunk,cheaderBuf.pData_ + 4,4);
szChunk[4] = 0;
if (!memcmp(cheaderBuf.pData_ + 4, "IEND", 4)) if (!memcmp(cheaderBuf.pData_ + 4, "IEND", 4))
{ {
// Last chunk found: we write it and done. // Last chunk found: we write it and done.
@ -575,12 +583,34 @@ namespace Exiv2 {
if ( iccProfileDefined() ) { if ( iccProfileDefined() ) {
DataBuf compressed; DataBuf compressed;
if ( zlibToCompressed(iccProfile_.pData_,iccProfile_.size_,compressed) ) { if ( zlibToCompressed(iccProfile_.pData_,iccProfile_.size_,compressed) ) {
std::string chunk = PngChunk::makeICCPChunkHeader("ICC Profile",compressed.size_);
if( outIo.write((const byte*)chunk.data(), static_cast<long>(chunk.size())) != (long)chunk.size() const byte* header = (const byte*) "ICC PROFILE\0\0" ; // \0 = default compression
const byte* type = (const byte*) "iCCP";
uint32_t headerLen = 13 ;
uint32_t typeLen = 4;
uint32_t chunkLength = headerLen + compressed.size_ ;
byte length[4];
ul2Data (length,chunkLength,bigEndian);
// calculate CRC
uLong tmp = crc32(0L, Z_NULL, 0);
tmp = crc32(tmp, (const Bytef*)header ,headerLen);
tmp = crc32(tmp, (const Bytef*)compressed.pData_,compressed.size_);
byte crc[4];
ul2Data(crc, tmp, bigEndian);
if( outIo.write(length,4) != 4
|| outIo.write(type ,typeLen) != typeLen
|| outIo.write(header,headerLen) != headerLen
|| outIo.write (compressed.pData_,compressed.size_) != compressed.size_ || outIo.write (compressed.pData_,compressed.size_) != compressed.size_
|| outIo.write(crc,4) != 4
){ ){
throw Error(21); throw Error(21);
} }
#ifdef DEBUG
std::cout << "Exiv2::PngImage::doWriteMetadata: build iCCP"
<< " chunk (length: " << compressed.size_ + headerLen << ")" << std::endl;
#endif
} }
} }
@ -610,19 +640,20 @@ namespace Exiv2 {
memcmp("Raw profile type iptc", key.pData_, 21) == 0 || memcmp("Raw profile type iptc", key.pData_, 21) == 0 ||
memcmp("Raw profile type xmp", key.pData_, 20) == 0 || memcmp("Raw profile type xmp", key.pData_, 20) == 0 ||
memcmp("XML:com.adobe.xmp", key.pData_, 17) == 0 || memcmp("XML:com.adobe.xmp", key.pData_, 17) == 0 ||
memcmp("icc", key.pData_, 3) == 0 || memcmp("icc", key.pData_, 3) == 0 || // see test/data/imagemagick.png
memcmp("ICC", key.pData_, 3) == 0 ||
memcmp("Description", key.pData_, 11) == 0) memcmp("Description", key.pData_, 11) == 0)
{ {
#ifdef DEBUG #ifdef DEBUG
std::cout << "Exiv2::PngImage::doWriteMetadata: strip " << cheaderBuf.pData_ + 4 std::cout << "Exiv2::PngImage::doWriteMetadata: strip " << szChunk
<< " chunk (key: " << key.pData_ << ")\n"; << " chunk (length: " << dataOffset << ")" << std::endl;
#endif #endif
} }
else else
{ {
#ifdef DEBUG #ifdef DEBUG
std::cout << "Exiv2::PngImage::doWriteMetadata: write " << cheaderBuf.pData_ + 4 std::cout << "Exiv2::PngImage::doWriteMetadata: write " << szChunk
<< " chunk (length: " << dataOffset << ")\n"; << " chunk (length: " << dataOffset << ")" << std::endl;
#endif #endif
if (outIo.write(chunkBuf.pData_, chunkBuf.size_) != chunkBuf.size_) throw Error(21); if (outIo.write(chunkBuf.pData_, chunkBuf.size_) != chunkBuf.size_) throw Error(21);
} }
@ -631,8 +662,8 @@ namespace Exiv2 {
{ {
// Write all others chunk as well. // Write all others chunk as well.
#ifdef DEBUG #ifdef DEBUG
std::cout << "Exiv2::PngImage::doWriteMetadata: write " << cheaderBuf.pData_ + 4 std::cout << "Exiv2::PngImage::doWriteMetadata: copy " << szChunk
<< " chunk (length: " << dataOffset << ")\n"; << " chunk (length: " << dataOffset << ")" << std::endl;
#endif #endif
if (outIo.write(chunkBuf.pData_, chunkBuf.size_) != chunkBuf.size_) throw Error(21); if (outIo.write(chunkBuf.pData_, chunkBuf.size_) != chunkBuf.size_) throw Error(21);

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

@ -152,124 +152,50 @@ STRUCTURE OF JPEG FILE: Reagan.jpg
1628273 | 0xffdd DRI | 4 1628273 | 0xffdd DRI | 4
1628279 | 0xffc4 DHT | 418 1628279 | 0xffc4 DHT | 418
1628699 | 0xffda SOS 1628699 | 0xffda SOS
Exiv2 exception in insert action for file Reagan.jpg:
Not a valid ICC Profile
STRUCTURE OF JPEG FILE: Reagan.jpg STRUCTURE OF JPEG FILE: Reagan.jpg
address | marker | length | data address | marker | length | data
0 | 0xffd8 SOI 0 | 0xffd8 SOI
2 | 0xffe1 APP1 | 5704 | Exif..MM.*...................... 2 | 0xffe1 APP1 | 5704 | Exif..MM.*......................
5708 | 0xffe1 APP1 | 5287 | http://ns.adobe.com/xap/1.0/.<?x 5708 | 0xffe1 APP1 | 5287 | http://ns.adobe.com/xap/1.0/.<?x
10997 | 0xffe2 APP2 | 65512 | ICC_PROFILE... APPL....prtrRGB L chunk 1/-97 10997 | 0xffe2 APP2 | 576 | ICC_PROFILE.....0ADBE....mntrRGB chunk 1/0
76512 | 0xffe2 APP2 | 65512 | ICC_PROFILE...Ih.V...j.U..4mVT<. chunk 2/-99 11576 | 0xffed APP13 | 3030 | Photoshop 3.0.8BIM..........Z...
142027 | 0xffe2 APP2 | 65512 | ICC_PROFILE..f...~mcx...`.....] chunk 3/102 14608 | 0xffee APP14 | 14 | Adobe.d@......
207542 | 0xffe2 APP2 | 65512 | ICC_PROFILE.....S...^...v....... chunk 4/-99 14624 | 0xffdb DQT | 132
273057 | 0xffe2 APP2 | 65512 | ICC_PROFILE..bXf2..`Og...^0g..I\ chunk 5/98 14758 | 0xffc0 SOF0 | 17
338572 | 0xffe2 APP2 | 65512 | ICC_PROFILE..~.|...{.}P..y.}..Vv chunk 6/126 14777 | 0xffdd DRI | 4
404087 | 0xffe2 APP2 | 65512 | ICC_PROFILE...b.....:...?.....E. chunk 7/-94 14783 | 0xffc4 DHT | 418
469602 | 0xffe2 APP2 | 65512 | ICC_PROFILE..yq].R.wW].S.uJ]eT6s chunk 8/121 15203 | 0xffda SOS
535117 | 0xffe2 APP2 | 65512 | ICC_PROFILE..T'..RA.Y..P,....N%. chunk 9/84
600632 | 0xffe2 APP2 | 65512 | ICC_PROFILE..}/..key...l.v..cn-r chunk 10/125
666147 | 0xffe2 APP2 | 65512 | ICC_PROFILE.....O{.....|..c..|S. chunk 11/-67
731662 | 0xffe2 APP2 | 65512 | ICC_PROFILE..;.O-F.-.R>J...a.`1 chunk 12/59
797177 | 0xffe2 APP2 | 65512 | ICC_PROFILE....up............... chunk 13/-1
862692 | 0xffe2 APP2 | 65512 | ICC_PROFILE.....<............... chunk 14/-1
928207 | 0xffe2 APP2 | 65512 | ICC_PROFILE...........,...'..... chunk 15/-96
993722 | 0xffe2 APP2 | 65512 | ICC_PROFILE....g.....m%....qw.. chunk 16/0
1059237 | 0xffe2 APP2 | 65512 | ICC_PROFILE...s....xX.M..n.....g chunk 17/-126
1124752 | 0xffe2 APP2 | 65512 | ICC_PROFILE.........0......E.... chunk 18/22
1190267 | 0xffe2 APP2 | 65512 | ICC_PROFILE.....(.n.B.........J} chunk 19/-1
1255782 | 0xffe2 APP2 | 65512 | ICC_PROFILE..0.282.0.282.0.281.0 chunk 20/48
1321297 | 0xffe2 APP2 | 65512 | ICC_PROFILE..5.0.176.0.175.0.174 chunk 21/53
1386812 | 0xffe2 APP2 | 65512 | ICC_PROFILE..3.0.114.0.126.0.136 chunk 22/51
1452327 | 0xffe2 APP2 | 65512 | ICC_PROFILE..0.049.0.053.0.059.0 chunk 23/48
1517842 | 0xffe2 APP2 | 65512 | ICC_PROFILE...670.0.653.0.634.0. chunk 24/46
1583357 | 0xffe2 APP2 | 41712 | ICC_PROFILE...0.584.0.555.0.509. chunk 25/9
1625072 | 0xffed APP13 | 3030 | Photoshop 3.0.8BIM..........Z...
1628104 | 0xffee APP14 | 14 | Adobe.d@......
1628120 | 0xffdb DQT | 132
1628254 | 0xffc0 SOF0 | 17
1628273 | 0xffdd DRI | 4
1628279 | 0xffc4 DHT | 418
1628699 | 0xffda SOS
STRUCTURE OF JPEG FILE: Reagan.jpg STRUCTURE OF JPEG FILE: Reagan.jpg
address | marker | length | data address | marker | length | data
0 | 0xffd8 SOI 0 | 0xffd8 SOI
2 | 0xffe1 APP1 | 5704 | Exif..MM.*...................... 2 | 0xffe1 APP1 | 5704 | Exif..MM.*......................
5708 | 0xffe1 APP1 | 5287 | http://ns.adobe.com/xap/1.0/.<?x 5708 | 0xffe1 APP1 | 5287 | http://ns.adobe.com/xap/1.0/.<?x
10997 | 0xffe2 APP2 | 65512 | ICC_PROFILE.. APPL....prtrRGB La chunk 1/32 10997 | 0xffe2 APP2 | 576 | ICC_PROFILE....0ADBE....mntrRGB chunk 1/0
76512 | 0xffe2 APP2 | 65512 | ICC_PROFILE..Ih.V...j.U..4mVT<.7 chunk 2/73 11576 | 0xffed APP13 | 3030 | Photoshop 3.0.8BIM..........Z...
142027 | 0xffe2 APP2 | 65512 | ICC_PROFILE.....~mcx...`.....]' chunk 3/-87 14608 | 0xffee APP14 | 14 | Adobe.d@......
207542 | 0xffe2 APP2 | 65512 | ICC_PROFILE....S...^...v........ chunk 4/-60 14624 | 0xffdb DQT | 132
273057 | 0xffe2 APP2 | 65512 | ICC_PROFILE..Xf2..`Og...^0g..I\% chunk 5/88 14758 | 0xfffe COM | 10 | abcdefg
338572 | 0xffe2 APP2 | 65512 | ICC_PROFILE...|...{.}P..y.}..Vvh chunk 6/-63 14770 | 0xffc0 SOF0 | 17
404087 | 0xffe2 APP2 | 65512 | ICC_PROFILE..b.....:...?.....E.. chunk 7/98 14789 | 0xffdd DRI | 4
469602 | 0xffe2 APP2 | 65512 | ICC_PROFILE..q].R.wW].S.uJ]eT6s. chunk 8/113 14795 | 0xffc4 DHT | 418
535117 | 0xffe2 APP2 | 65512 | ICC_PROFILE..'..RA.Y..P,....N%.Z chunk 9/39 15215 | 0xffda SOS
600632 | 0xffe2 APP2 | 65512 | ICC_PROFILE../..key...l.v..cn-r. chunk 10/47
666147 | 0xffe2 APP2 | 65512 | ICC_PROFILE....O{.....|..c..|S.d chunk 11/-75
731662 | 0xffe2 APP2 | 65512 | ICC_PROFILE...O-F.-.R>J...a.`1. chunk 12/-113
797177 | 0xffe2 APP2 | 65512 | ICC_PROFILE...up................ chunk 13/-1
862692 | 0xffe2 APP2 | 65512 | ICC_PROFILE....<................ chunk 14/-1
928207 | 0xffe2 APP2 | 65512 | ICC_PROFILE..........,...'.....` chunk 15/9
993722 | 0xffe2 APP2 | 65512 | ICC_PROFILE...g.....m%....qw... chunk 16/0
1059237 | 0xffe2 APP2 | 65512 | ICC_PROFILE..s....xX.M..n.....gO chunk 17/115
1124752 | 0xffe2 APP2 | 65512 | ICC_PROFILE........0......E..... chunk 18/-31
1190267 | 0xffe2 APP2 | 65512 | ICC_PROFILE....(.n.B.........J}a chunk 19/-1
1255782 | 0xffe2 APP2 | 65512 | ICC_PROFILE...282.0.282.0.281.0. chunk 20/46
1321297 | 0xffe2 APP2 | 65512 | ICC_PROFILE...0.176.0.175.0.174. chunk 21/9
1386812 | 0xffe2 APP2 | 65512 | ICC_PROFILE...0.114.0.126.0.136. chunk 22/9
1452327 | 0xffe2 APP2 | 65512 | ICC_PROFILE...049.0.053.0.059.0. chunk 23/46
1517842 | 0xffe2 APP2 | 65512 | ICC_PROFILE..670.0.653.0.634.0.6 chunk 24/54
1583357 | 0xffe2 APP2 | 41712 | ICC_PROFILE..0.584.0.555.0.509.0 chunk 25/48
1625072 | 0xffed APP13 | 3030 | Photoshop 3.0.8BIM..........Z...
1628104 | 0xffee APP14 | 14 | Adobe.d@......
1628120 | 0xffdb DQT | 132
1628254 | 0xfffe COM | 10 | abcdefg
1628266 | 0xffc0 SOF0 | 17
1628285 | 0xffdd DRI | 4
1628291 | 0xffc4 DHT | 418
1628711 | 0xffda SOS
abcdefg abcdefg
STRUCTURE OF JPEG FILE: Reagan.jpg STRUCTURE OF JPEG FILE: Reagan.jpg
address | marker | length | data address | marker | length | data
0 | 0xffd8 SOI 0 | 0xffd8 SOI
2 | 0xffe1 APP1 | 5704 | Exif..MM.*...................... 2 | 0xffe1 APP1 | 5704 | Exif..MM.*......................
5708 | 0xffe1 APP1 | 5287 | http://ns.adobe.com/xap/1.0/.<?x 5708 | 0xffe1 APP1 | 5287 | http://ns.adobe.com/xap/1.0/.<?x
10997 | 0xffe2 APP2 | 65512 | ICC_PROFILE..APPL....prtrRGB Lab chunk 1/65 10997 | 0xffe2 APP2 | 576 | ICC_PROFILE...0ADBE....mntrRGB X chunk 1/2
76512 | 0xffe2 APP2 | 65512 | ICC_PROFILE..h.V...j.U..4mVT<.7o chunk 2/104 11576 | 0xffed APP13 | 3030 | Photoshop 3.0.8BIM..........Z...
142027 | 0xffe2 APP2 | 65512 | ICC_PROFILE....~mcx...`.....]'. chunk 3/-85 14608 | 0xffee APP14 | 14 | Adobe.d@......
207542 | 0xffe2 APP2 | 65512 | ICC_PROFILE...S...^...v......... chunk 4/-50 14624 | 0xffdb DQT | 132
273057 | 0xffe2 APP2 | 65512 | ICC_PROFILE..f2..`Og...^0g..I\%h chunk 5/102 14758 | 0xffc0 SOF0 | 17
338572 | 0xffe2 APP2 | 65512 | ICC_PROFILE..|...{.}P..y.}..Vvh} chunk 6/124 14777 | 0xffdd DRI | 4
404087 | 0xffe2 APP2 | 65512 | ICC_PROFILE.......:...?.....E... chunk 7/-110 14783 | 0xffc4 DHT | 418
469602 | 0xffe2 APP2 | 65512 | ICC_PROFILE..].R.wW].S.uJ]eT6s.] chunk 8/93 15203 | 0xffda SOS
535117 | 0xffe2 APP2 | 65512 | ICC_PROFILE....RA.Y..P,....N%.Z. chunk 9/-127
600632 | 0xffe2 APP2 | 65512 | ICC_PROFILE....key...l.v..cn-r.. chunk 10/-56
666147 | 0xffe2 APP2 | 65512 | ICC_PROFILE...O{.....|..c..|S.d. chunk 11/-19
731662 | 0xffe2 APP2 | 65512 | ICC_PROFILE..O-F.-.R>J...a.`1..g chunk 12/79
797177 | 0xffe2 APP2 | 65512 | ICC_PROFILE..up................. chunk 13/117
862692 | 0xffe2 APP2 | 65512 | ICC_PROFILE...<................. chunk 14/-102
928207 | 0xffe2 APP2 | 65512 | ICC_PROFILE.........,...'.....` chunk 15/9
993722 | 0xffe2 APP2 | 65512 | ICC_PROFILE..g.....m%....qw....u chunk 16/103
1059237 | 0xffe2 APP2 | 65512 | ICC_PROFILE......xX.M..n.....gO. chunk 17/-112
1124752 | 0xffe2 APP2 | 65512 | ICC_PROFILE.......0......E.....H chunk 18/-120
1190267 | 0xffe2 APP2 | 65512 | ICC_PROFILE...(.n.B.........J}am chunk 19/-9
1255782 | 0xffe2 APP2 | 65512 | ICC_PROFILE..282.0.282.0.281.0.2 chunk 20/50
1321297 | 0xffe2 APP2 | 65512 | ICC_PROFILE..0.176.0.175.0.174.0 chunk 21/48
1386812 | 0xffe2 APP2 | 65512 | ICC_PROFILE..0.114.0.126.0.136.0 chunk 22/48
1452327 | 0xffe2 APP2 | 65512 | ICC_PROFILE..049.0.053.0.059.0.0 chunk 23/48
1517842 | 0xffe2 APP2 | 65512 | ICC_PROFILE..70.0.653.0.634.0.60 chunk 24/55
1583357 | 0xffe2 APP2 | 41712 | ICC_PROFILE...584.0.555.0.509.0. chunk 25/46
1625072 | 0xffed APP13 | 3030 | Photoshop 3.0.8BIM..........Z...
1628104 | 0xffee APP14 | 14 | Adobe.d@......
1628120 | 0xffdb DQT | 132
1628254 | 0xffc0 SOF0 | 17
1628273 | 0xffdd DRI | 4
1628279 | 0xffc4 DHT | 418
1628699 | 0xffda SOS
50b9125494306a6fc1b7c4f2a1a8d49d 50b9125494306a6fc1b7c4f2a1a8d49d
50b9125494306a6fc1b7c4f2a1a8d49d 50b9125494306a6fc1b7c4f2a1a8d49d
daa5193ef28659d126c48d4010479428 a78f7a71f1ea79f2f6708be6394e1305
daa5193ef28659d126c48d4010479428 a78f7a71f1ea79f2f6708be6394e1305
d890d988d312ae8d497d21e936628ecc d890d988d312ae8d497d21e936628ecc
d890d988d312ae8d497d21e936628ecc d890d988d312ae8d497d21e936628ecc

Binary file not shown.
Loading…
Cancel
Save