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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2006 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================
#include "PNG_Handler.hpp"
#include "PNG_Support.hpp"
using namespace std;
// =================================================================================================
/// \file PNG_Handler.hpp
/// \brief File format handler for PNG.
///
/// This handler ...
///
// =================================================================================================
// =================================================================================================
// PNG_MetaHandlerCTor
// ====================
XMPFileHandler * PNG_MetaHandlerCTor ( XMPFiles * parent )
{
return new PNG_MetaHandler ( parent );
} // PNG_MetaHandlerCTor
// =================================================================================================
// PNG_CheckFormat
// ===============
bool PNG_CheckFormat ( XMP_FileFormat format,
XMP_StringPtr filePath,
LFA_FileRef fileRef,
XMPFiles * parent )
{
IgnoreParam(format); IgnoreParam(fileRef); IgnoreParam(parent);
XMP_Assert ( format == kXMP_PNGFile );
IOBuffer ioBuf;
LFA_Seek ( fileRef, 0, SEEK_SET );
if ( ! CheckFileSpace ( fileRef, &ioBuf, PNG_SIGNATURE_LEN ) ) return false; // We need at least 8, the buffer is filled anyway.
if ( ! CheckBytes ( ioBuf.ptr, PNG_SIGNATURE_DATA, PNG_SIGNATURE_LEN ) ) return false;
return true;
} // PNG_CheckFormat
// =================================================================================================
// PNG_MetaHandler::PNG_MetaHandler
// ==================================
PNG_MetaHandler::PNG_MetaHandler ( XMPFiles * _parent )
{
this->parent = _parent;
this->handlerFlags = kPNG_HandlerFlags;
this->stdCharForm = kXMP_Char8Bit;
}
// =================================================================================================
// PNG_MetaHandler::~PNG_MetaHandler
// ===================================
PNG_MetaHandler::~PNG_MetaHandler()
{
}
// =================================================================================================
// PNG_MetaHandler::CacheFileData
// ===============================
void PNG_MetaHandler::CacheFileData()
{
this->containsXMP = false;
LFA_FileRef fileRef ( this->parent->fileRef );
if ( fileRef == 0) return;
PNG_Support::ChunkState chunkState;
long numChunks = PNG_Support::OpenPNG ( fileRef, chunkState );
if ( numChunks == 0 ) return;
if (chunkState.xmpLen != 0)
{
// XMP present
this->xmpPacket.reserve(chunkState.xmpLen);
this->xmpPacket.assign(chunkState.xmpLen, ' ');
if (PNG_Support::ReadBuffer ( fileRef, chunkState.xmpPos, chunkState.xmpLen, const_cast<char *>(this->xmpPacket.data()) ))
{
this->packetInfo.offset = chunkState.xmpPos;
this->packetInfo.length = chunkState.xmpLen;
this->containsXMP = true;
}
}
else
{
// no XMP
}
} // PNG_MetaHandler::CacheFileData
// =================================================================================================
// PNG_MetaHandler::ProcessXMP
// ============================
//
// Process the raw XMP and legacy metadata that was previously cached.
void PNG_MetaHandler::ProcessXMP()
{
this->processedXMP = true; // Make sure we only come through here once.
// Process the XMP packet.
if ( ! this->xmpPacket.empty() ) {
XMP_Assert ( this->containsXMP );
XMP_StringPtr packetStr = this->xmpPacket.c_str();
XMP_StringLen packetLen = (XMP_StringLen)this->xmpPacket.size();
this->xmpObj.ParseFromBuffer ( packetStr, packetLen );
this->containsXMP = true;
}
} // PNG_MetaHandler::ProcessXMP
// =================================================================================================
// PNG_MetaHandler::UpdateFile
// ============================
void PNG_MetaHandler::UpdateFile ( bool doSafeUpdate )
{
bool updated = false;
if ( ! this->needsUpdate ) return;
if ( doSafeUpdate ) XMP_Throw ( "PNG_MetaHandler::UpdateFile: Safe update not supported", kXMPErr_Unavailable );
XMP_StringPtr packetStr = xmpPacket.c_str();
XMP_StringLen packetLen = (XMP_StringLen)xmpPacket.size();
if ( packetLen == 0 ) return;
LFA_FileRef fileRef(this->parent->fileRef);
if ( fileRef == 0 ) return;
PNG_Support::ChunkState chunkState;
long numChunks = PNG_Support::OpenPNG ( fileRef, chunkState );
if ( numChunks == 0 ) return;
// write/update chunk
if (chunkState.xmpLen == 0)
{
// no current chunk -> inject
updated = SafeWriteFile();
}
else if (chunkState.xmpLen >= packetLen )
{
// current chunk size is sufficient -> write and update CRC (in place update)
updated = PNG_Support::WriteBuffer(fileRef, chunkState.xmpPos, packetLen, packetStr );
PNG_Support::UpdateChunkCRC(fileRef, chunkState.xmpChunk );
}
else if (chunkState.xmpLen < packetLen)
{
// XMP is too large for current chunk -> expand
updated = SafeWriteFile();
}
if ( ! updated )return; // If there's an error writing the chunk, bail.
this->needsUpdate = false;
} // PNG_MetaHandler::UpdateFile
// =================================================================================================
// PNG_MetaHandler::WriteFile
// ===========================
void PNG_MetaHandler::WriteFile ( LFA_FileRef sourceRef, const std::string & sourcePath )
{
LFA_FileRef destRef = this->parent->fileRef;
PNG_Support::ChunkState chunkState;
long numChunks = PNG_Support::OpenPNG ( sourceRef, chunkState );
if ( numChunks == 0 ) return;
LFA_Truncate(destRef, 0);
LFA_Write(destRef, PNG_SIGNATURE_DATA, PNG_SIGNATURE_LEN);
PNG_Support::ChunkIterator curPos = chunkState.chunks.begin();
PNG_Support::ChunkIterator endPos = chunkState.chunks.end();
for (; (curPos != endPos); ++curPos)
{
PNG_Support::ChunkData chunk = *curPos;
// discard existing XMP chunk
if (chunk.xmp)
continue;
// copy any other chunk
PNG_Support::CopyChunk(sourceRef, destRef, chunk);
// place XMP chunk immediately after IHDR-chunk
if (PNG_Support::CheckIHDRChunkHeader(chunk))
{
XMP_StringPtr packetStr = xmpPacket.c_str();
XMP_StringLen packetLen = (XMP_StringLen)xmpPacket.size();
PNG_Support::WriteXMPChunk(destRef, packetLen, packetStr );
}
}
} // PNG_MetaHandler::WriteFile
// =================================================================================================
// PNG_MetaHandler::SafeWriteFile
// ===========================
bool PNG_MetaHandler::SafeWriteFile ()
{
bool ret = false;
std::string origPath = this->parent->filePath;
LFA_FileRef origRef = this->parent->fileRef;
std::string updatePath;
LFA_FileRef updateRef = 0;
CreateTempFile ( origPath, &updatePath, kCopyMacRsrc );
updateRef = LFA_Open ( updatePath.c_str(), 'w' );
this->parent->filePath = updatePath;
this->parent->fileRef = updateRef;
try {
this->WriteFile ( origRef, origPath );
ret = true;
} catch ( ... ) {
LFA_Close ( updateRef );
LFA_Delete ( updatePath.c_str() );
this->parent->filePath = origPath;
this->parent->fileRef = origRef;
throw;
}
LFA_Close ( origRef );
LFA_Delete ( origPath.c_str() );
LFA_Close ( updateRef );
LFA_Rename ( updatePath.c_str(), origPath.c_str() );
this->parent->filePath = origPath;
this->parent->fileRef = 0;
return ret;
} // PNG_MetaHandler::SafeWriteFile
// =================================================================================================
|