Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImage/PluginJPEG.cpp view on Meta::CPAN
dest->pub.term_destination = term_destination;
dest->outfile = outfile;
dest->m_io = io;
}
// ----------------------------------------------------------
// Special markers read functions
// ----------------------------------------------------------
/**
Read JPEG_COM marker (comment)
*/
static BOOL
jpeg_read_comment(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
size_t length = datalen;
BYTE *profile = (BYTE*)dataptr;
// read the comment
char *value = (char*)malloc((length + 1) * sizeof(char));
if(value == NULL) return FALSE;
memcpy(value, profile, length);
value[length] = '\0';
// create a tag
FITAG *tag = FreeImage_CreateTag();
if(tag) {
unsigned int count = (unsigned int)length + 1; // includes the null value
FreeImage_SetTagID(tag, JPEG_COM);
FreeImage_SetTagKey(tag, "Comment");
FreeImage_SetTagLength(tag, count);
FreeImage_SetTagCount(tag, count);
FreeImage_SetTagType(tag, FIDT_ASCII);
FreeImage_SetTagValue(tag, value);
// store the tag
FreeImage_SetMetadata(FIMD_COMMENTS, dib, FreeImage_GetTagKey(tag), tag);
// destroy the tag
FreeImage_DeleteTag(tag);
}
free(value);
return TRUE;
}
/**
Read JPEG_APP2 marker (ICC profile)
*/
/**
Handy subroutine to test whether a saved marker is an ICC profile marker.
*/
static BOOL
marker_is_icc(jpeg_saved_marker_ptr marker) {
// marker identifying string "ICC_PROFILE" (null-terminated)
const BYTE icc_signature[12] = { 0x49, 0x43, 0x43, 0x5F, 0x50, 0x52, 0x4F, 0x46, 0x49, 0x4C, 0x45, 0x00 };
if(marker->marker == ICC_MARKER) {
// verify the identifying string
if(marker->data_length >= ICC_HEADER_SIZE) {
if(memcmp(icc_signature, marker->data, sizeof(icc_signature)) == 0) {
return TRUE;
}
}
}
return FALSE;
}
/**
See if there was an ICC profile in the JPEG file being read;
if so, reassemble and return the profile data.
TRUE is returned if an ICC profile was found, FALSE if not.
If TRUE is returned, *icc_data_ptr is set to point to the
returned data, and *icc_data_len is set to its length.
IMPORTANT: the data at **icc_data_ptr has been allocated with malloc()
and must be freed by the caller with free() when the caller no longer
needs it. (Alternatively, we could write this routine to use the
IJG library's memory allocator, so that the data would be freed implicitly
at jpeg_finish_decompress() time. But it seems likely that many apps
will prefer to have the data stick around after decompression finishes.)
NOTE: if the file contains invalid ICC APP2 markers, we just silently
return FALSE. You might want to issue an error message instead.
*/
static BOOL
jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr, unsigned *icc_data_len) {
jpeg_saved_marker_ptr marker;
int num_markers = 0;
int seq_no;
JOCTET *icc_data;
unsigned total_length;
const int MAX_SEQ_NO = 255; // sufficient since marker numbers are bytes
BYTE marker_present[MAX_SEQ_NO+1]; // 1 if marker found
unsigned data_length[MAX_SEQ_NO+1]; // size of profile data in marker
unsigned data_offset[MAX_SEQ_NO+1]; // offset for data in marker
*icc_data_ptr = NULL; // avoid confusion if FALSE return
*icc_data_len = 0;
/**
this first pass over the saved markers discovers whether there are
any ICC markers and verifies the consistency of the marker numbering.
*/
memset(marker_present, 0, (MAX_SEQ_NO + 1));
for(marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
if (marker_is_icc(marker)) {
if (num_markers == 0) {
// number of markers
num_markers = GETJOCTET(marker->data[13]);
}
else if (num_markers != GETJOCTET(marker->data[13])) {
return FALSE; // inconsistent num_markers fields
}
src/Source/FreeImage/PluginJPEG.cpp view on Meta::CPAN
if (total_length <= 0)
return FALSE; // found only empty markers ?
// allocate space for assembled data
icc_data = (JOCTET *) malloc(total_length * sizeof(JOCTET));
if (icc_data == NULL)
return FALSE; // out of memory
// and fill it in
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
if (marker_is_icc(marker)) {
JOCTET FAR *src_ptr;
JOCTET *dst_ptr;
unsigned length;
seq_no = GETJOCTET(marker->data[12]);
dst_ptr = icc_data + data_offset[seq_no];
src_ptr = marker->data + ICC_HEADER_SIZE;
length = data_length[seq_no];
while (length--) {
*dst_ptr++ = *src_ptr++;
}
}
}
*icc_data_ptr = icc_data;
*icc_data_len = total_length;
return TRUE;
}
/**
Read JPEG_APPD marker (IPTC or Adobe Photoshop profile)
*/
static BOOL
jpeg_read_iptc_profile(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
return read_iptc_profile(dib, dataptr, datalen);
}
/**
Read JPEG_APP1 marker (XMP profile)
@param dib Input FIBITMAP
@param dataptr Pointer to the APP1 marker
@param datalen APP1 marker length
@return Returns TRUE if successful, FALSE otherwise
*/
static BOOL
jpeg_read_xmp_profile(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
// marker identifying string for XMP (null terminated)
const char *xmp_signature = "http://ns.adobe.com/xap/1.0/";
// XMP signature is 29 bytes long
const size_t xmp_signature_size = strlen(xmp_signature) + 1;
size_t length = datalen;
BYTE *profile = (BYTE*)dataptr;
if(length <= xmp_signature_size) {
// avoid reading corrupted or empty data
return FALSE;
}
// verify the identifying string
if(memcmp(xmp_signature, profile, strlen(xmp_signature)) == 0) {
// XMP profile
profile += xmp_signature_size;
length -= xmp_signature_size;
// create a tag
FITAG *tag = FreeImage_CreateTag();
if(tag) {
FreeImage_SetTagID(tag, JPEG_APP0+1); // 0xFFE1
FreeImage_SetTagKey(tag, g_TagLib_XMPFieldName);
FreeImage_SetTagLength(tag, (DWORD)length);
FreeImage_SetTagCount(tag, (DWORD)length);
FreeImage_SetTagType(tag, FIDT_ASCII);
FreeImage_SetTagValue(tag, profile);
// store the tag
FreeImage_SetMetadata(FIMD_XMP, dib, FreeImage_GetTagKey(tag), tag);
// destroy the tag
FreeImage_DeleteTag(tag);
}
return TRUE;
}
return FALSE;
}
/**
Read JFIF "JFXX" extension APP0 marker
@param dib Input FIBITMAP
@param dataptr Pointer to the APP0 marker
@param datalen APP0 marker length
@return Returns TRUE if successful, FALSE otherwise
*/
static BOOL
jpeg_read_jfxx(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
if(datalen < 6) {
return FALSE;
}
const int id_length = 5;
const BYTE *data = dataptr + id_length;
unsigned remaining = datalen - id_length;
const BYTE type = *data;
++data, --remaining;
switch(type) {
case JFXX_TYPE_JPEG:
{
// load the thumbnail
FIMEMORY* hmem = FreeImage_OpenMemory(const_cast<BYTE*>(data), remaining);
FIBITMAP* thumbnail = FreeImage_LoadFromMemory(FIF_JPEG, hmem);
FreeImage_CloseMemory(hmem);
// store the thumbnail
FreeImage_SetThumbnail(dib, thumbnail);
// then delete it
src/Source/FreeImage/PluginJPEG.cpp view on Meta::CPAN
return FALSE;
}
/**
Write JPEG_APP1 marker (XMP profile)
@return Returns TRUE if successful, FALSE otherwise
*/
static BOOL
jpeg_write_xmp_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
// marker identifying string for XMP (null terminated)
const char *xmp_signature = "http://ns.adobe.com/xap/1.0/";
FITAG *tag_xmp = NULL;
FreeImage_GetMetadata(FIMD_XMP, dib, g_TagLib_XMPFieldName, &tag_xmp);
if(tag_xmp) {
const BYTE *tag_value = (BYTE*)FreeImage_GetTagValue(tag_xmp);
if(NULL != tag_value) {
// XMP signature is 29 bytes long
unsigned int xmp_header_size = (unsigned int)strlen(xmp_signature) + 1;
DWORD tag_length = FreeImage_GetTagLength(tag_xmp);
BYTE *profile = (BYTE*)malloc((tag_length + xmp_header_size) * sizeof(BYTE));
if(profile == NULL) return FALSE;
memcpy(profile, xmp_signature, xmp_header_size);
for(DWORD i = 0; i < tag_length; i += 65504L) {
unsigned length = MIN((long)(tag_length - i), 65504L);
memcpy(profile + xmp_header_size, tag_value + i, length);
jpeg_write_marker(cinfo, EXIF_MARKER, profile, (length + xmp_header_size));
}
free(profile);
return TRUE;
}
}
return FALSE;
}
/**
Write JPEG_APP1 marker (Exif profile)
@return Returns TRUE if successful, FALSE otherwise
*/
static BOOL
jpeg_write_exif_profile_raw(j_compress_ptr cinfo, FIBITMAP *dib) {
// marker identifying string for Exif = "Exif\0\0"
BYTE exif_signature[6] = { 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 };
FITAG *tag_exif = NULL;
FreeImage_GetMetadata(FIMD_EXIF_RAW, dib, g_TagLib_ExifRawFieldName, &tag_exif);
if(tag_exif) {
const BYTE *tag_value = (BYTE*)FreeImage_GetTagValue(tag_exif);
// verify the identifying string
if(memcmp(exif_signature, tag_value, sizeof(exif_signature)) != 0) {
// not an Exif profile
return FALSE;
}
if(NULL != tag_value) {
DWORD tag_length = FreeImage_GetTagLength(tag_exif);
BYTE *profile = (BYTE*)malloc(tag_length * sizeof(BYTE));
if(profile == NULL) return FALSE;
for(DWORD i = 0; i < tag_length; i += 65504L) {
unsigned length = MIN((long)(tag_length - i), 65504L);
memcpy(profile, tag_value + i, length);
jpeg_write_marker(cinfo, EXIF_MARKER, profile, length);
}
free(profile);
return TRUE;
}
}
return FALSE;
}
/**
Write thumbnail (JFXX segment, JPEG compressed)
*/
static BOOL
jpeg_write_jfxx(j_compress_ptr cinfo, FIBITMAP *dib) {
// get the thumbnail to be stored
FIBITMAP* thumbnail = FreeImage_GetThumbnail(dib);
if(!thumbnail) {
return TRUE;
}
// check for a compatible output format
if((FreeImage_GetImageType(thumbnail) != FIT_BITMAP) || (FreeImage_GetBPP(thumbnail) != 8) && (FreeImage_GetBPP(thumbnail) != 24)) {
FreeImage_OutputMessageProc(s_format_id, FI_MSG_WARNING_INVALID_THUMBNAIL);
return FALSE;
}
// stores the thumbnail as a baseline JPEG into a memory block
// return the memory block only if its size is within JFXX marker size limit!
FIMEMORY *stream = FreeImage_OpenMemory();
if(FreeImage_SaveToMemory(FIF_JPEG, thumbnail, stream, JPEG_BASELINE)) {
// check that the memory block size is within JFXX marker size limit
FreeImage_SeekMemory(stream, 0, SEEK_END);
const long eof = FreeImage_TellMemory(stream);
if(eof > MAX_JFXX_THUMB_SIZE) {
FreeImage_OutputMessageProc(s_format_id, "Warning: attached thumbnail is %d bytes larger than maximum supported size - Thumbnail saving aborted", eof - MAX_JFXX_THUMB_SIZE);
FreeImage_CloseMemory(stream);
return FALSE;
}
} else {
FreeImage_CloseMemory(stream);
return FALSE;
}
( run in 0.609 second using v1.01-cache-2.11-cpan-119454b85a5 )