Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImage/PluginJPEG.cpp view on Meta::CPAN
// ==========================================================
// JPEG Loader and writer
// Based on code developed by The Independent JPEG Group
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Jan L. Nauta (jln@magentammt.com)
// - Markus Loibl (markus.loibl@epost.de)
// - Karl-Heinz Bussian (khbussian@moss.de)
// - Hervé Drolon (drolon@infonie.fr)
// - Jascha Wetzel (jascha@mainia.de)
// - Mihail Naydenov (mnaydenov@users.sourceforge.net)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#ifdef _MSC_VER
#pragma warning (disable : 4786) // identifier was truncated to 'number' characters
#endif
extern "C" {
#define XMD_H
#undef FAR
#include <setjmp.h>
#include "../LibJPEG/jinclude.h"
#include "../LibJPEG/jpeglib.h"
#include "../LibJPEG/jerror.h"
}
#include "FreeImage.h"
#include "Utilities.h"
#include "../Metadata/FreeImageTag.h"
// ==========================================================
// Plugin Interface
// ==========================================================
static int s_format_id;
// ----------------------------------------------------------
// Constant declarations
// ----------------------------------------------------------
#define INPUT_BUF_SIZE 4096 // choose an efficiently fread'able size
#define OUTPUT_BUF_SIZE 4096 // choose an efficiently fwrite'able size
#define EXIF_MARKER (JPEG_APP0+1) // EXIF marker / Adobe XMP marker
#define ICC_MARKER (JPEG_APP0+2) // ICC profile marker
#define IPTC_MARKER (JPEG_APP0+13) // IPTC marker / BIM marker
#define ICC_HEADER_SIZE 14 // size of non-profile data in APP2
#define MAX_BYTES_IN_MARKER 65533L // maximum data length of a JPEG marker
#define MAX_DATA_BYTES_IN_MARKER 65519L // maximum data length of a JPEG APP2 marker
#define MAX_JFXX_THUMB_SIZE (MAX_BYTES_IN_MARKER - 5 - 1)
#define JFXX_TYPE_JPEG 0x10 // JFIF extension marker: JPEG-compressed thumbnail image
#define JFXX_TYPE_8bit 0x11 // JFIF extension marker: palette thumbnail image
#define JFXX_TYPE_24bit 0x13 // JFIF extension marker: RGB thumbnail image
// ----------------------------------------------------------
// Typedef declarations
// ----------------------------------------------------------
typedef struct tagErrorManager {
/// "public" fields
struct jpeg_error_mgr pub;
/// for return to caller
jmp_buf setjmp_buffer;
} ErrorManager;
typedef struct tagSourceManager {
/// public fields
struct jpeg_source_mgr pub;
/// source stream
fi_handle infile;
FreeImageIO *m_io;
/// start of buffer
JOCTET * buffer;
/// have we gotten any data yet ?
boolean start_of_file;
} SourceManager;
typedef struct tagDestinationManager {
/// public fields
struct jpeg_destination_mgr pub;
/// destination stream
fi_handle outfile;
FreeImageIO *m_io;
/// start of buffer
JOCTET * buffer;
} DestinationManager;
typedef SourceManager* freeimage_src_ptr;
typedef DestinationManager* freeimage_dst_ptr;
typedef ErrorManager* freeimage_error_ptr;
// ----------------------------------------------------------
// Error handling
// ----------------------------------------------------------
/** Fatal errors (print message and exit) */
static inline void
JPEG_EXIT(j_common_ptr cinfo, int code) {
freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
src/Source/FreeImage/PluginJPEG.cpp view on Meta::CPAN
freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
error_ptr->pub.msg_code = code;
error_ptr->pub.emit_message(cinfo, -1);
}
/**
Receives control for a fatal error. Information sufficient to
generate the error message has been stored in cinfo->err; call
output_message to display it. Control must NOT return to the caller;
generally this routine will exit() or longjmp() somewhere.
*/
METHODDEF(void)
jpeg_error_exit (j_common_ptr cinfo) {
freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
// always display the message
error_ptr->pub.output_message(cinfo);
// allow JPEG with unknown markers
if(error_ptr->pub.msg_code != JERR_UNKNOWN_MARKER) {
// let the memory manager delete any temp files before we die
jpeg_destroy(cinfo);
// return control to the setjmp point
longjmp(error_ptr->setjmp_buffer, 1);
}
}
/**
Actual output of any JPEG message. Note that this method does not know
how to generate a message, only where to send it.
*/
METHODDEF(void)
jpeg_output_message (j_common_ptr cinfo) {
char buffer[JMSG_LENGTH_MAX];
freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
// create the message
error_ptr->pub.format_message(cinfo, buffer);
// send it to user's message proc
FreeImage_OutputMessageProc(s_format_id, buffer);
}
// ----------------------------------------------------------
// Destination manager
// ----------------------------------------------------------
/**
Initialize destination. This is called by jpeg_start_compress()
before any data is actually written. It must initialize
next_output_byte and free_in_buffer. free_in_buffer must be
initialized to a positive value.
*/
METHODDEF(void)
init_destination (j_compress_ptr cinfo) {
freeimage_dst_ptr dest = (freeimage_dst_ptr) cinfo->dest;
dest->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
OUTPUT_BUF_SIZE * sizeof(JOCTET));
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
/**
This is called whenever the buffer has filled (free_in_buffer
reaches zero). In typical applications, it should write out the
*entire* buffer (use the saved start address and buffer length;
ignore the current state of next_output_byte and free_in_buffer).
Then reset the pointer & count to the start of the buffer, and
return TRUE indicating that the buffer has been dumped.
free_in_buffer must be set to a positive value when TRUE is
returned. A FALSE return should only be used when I/O suspension is
desired.
*/
METHODDEF(boolean)
empty_output_buffer (j_compress_ptr cinfo) {
freeimage_dst_ptr dest = (freeimage_dst_ptr) cinfo->dest;
if (dest->m_io->write_proc(dest->buffer, 1, OUTPUT_BUF_SIZE, dest->outfile) != OUTPUT_BUF_SIZE) {
// let the memory manager delete any temp files before we die
jpeg_destroy((j_common_ptr)cinfo);
JPEG_EXIT((j_common_ptr)cinfo, JERR_FILE_WRITE);
}
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
/**
Terminate destination --- called by jpeg_finish_compress() after all
data has been written. In most applications, this must flush any
data remaining in the buffer. Use either next_output_byte or
free_in_buffer to determine how much data is in the buffer.
*/
METHODDEF(void)
term_destination (j_compress_ptr cinfo) {
freeimage_dst_ptr dest = (freeimage_dst_ptr) cinfo->dest;
size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
// write any data remaining in the buffer
if (datacount > 0) {
if (dest->m_io->write_proc(dest->buffer, 1, (unsigned int)datacount, dest->outfile) != datacount) {
// let the memory manager delete any temp files before we die
jpeg_destroy((j_common_ptr)cinfo);
JPEG_EXIT((j_common_ptr)cinfo, JERR_FILE_WRITE);
}
}
}
// ----------------------------------------------------------
// Source manager
// ----------------------------------------------------------
/**
Initialize source. This is called by jpeg_read_header() before any
data is actually read. Unlike init_destination(), it may leave
bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
will occur immediately).
*/
METHODDEF(void)
init_source (j_decompress_ptr cinfo) {
freeimage_src_ptr src = (freeimage_src_ptr) cinfo->src;
/* We reset the empty-input-file flag for each image,
* but we don't clear the input buffer.
* This is correct behavior for reading a series of images from one source.
*/
src->start_of_file = TRUE;
}
/**
This is called whenever bytes_in_buffer has reached zero and more
data is wanted. In typical applications, it should read fresh data
into the buffer (ignoring the current state of next_input_byte and
bytes_in_buffer), reset the pointer & count to the start of the
buffer, and return TRUE indicating that the buffer has been reloaded.
It is not necessary to fill the buffer entirely, only to obtain at
least one more byte. bytes_in_buffer MUST be set to a positive value
if TRUE is returned. A FALSE return should only be used when I/O
suspension is desired.
*/
METHODDEF(boolean)
fill_input_buffer (j_decompress_ptr cinfo) {
freeimage_src_ptr src = (freeimage_src_ptr) cinfo->src;
size_t nbytes = src->m_io->read_proc(src->buffer, 1, INPUT_BUF_SIZE, src->infile);
if (nbytes <= 0) {
if (src->start_of_file) {
// treat empty input file as fatal error
// let the memory manager delete any temp files before we die
jpeg_destroy((j_common_ptr)cinfo);
JPEG_EXIT((j_common_ptr)cinfo, JERR_INPUT_EMPTY);
}
JPEG_WARNING((j_common_ptr)cinfo, JWRN_JPEG_EOF);
/* Insert a fake EOI marker */
src->buffer[0] = (JOCTET) 0xFF;
src->buffer[1] = (JOCTET) JPEG_EOI;
nbytes = 2;
}
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = nbytes;
src->start_of_file = FALSE;
return TRUE;
}
/**
Skip num_bytes worth of data. The buffer pointer and count should
be advanced over num_bytes input bytes, refilling the buffer as
needed. This is used to skip over a potentially large amount of
uninteresting data (such as an APPn marker). In some applications
it may be possible to optimize away the reading of the skipped data,
but it's not clear that being smart is worth much trouble; large
skips are uncommon. bytes_in_buffer may be zero on return.
A zero or negative skip count should be treated as a no-op.
*/
METHODDEF(void)
skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
freeimage_src_ptr src = (freeimage_src_ptr) cinfo->src;
/* Just a dumb implementation for now. Could use fseek() except
* it doesn't work on pipes. Not clear that being smart is worth
* any trouble anyway --- large skips are infrequent.
*/
if (num_bytes > 0) {
while (num_bytes > (long) src->pub.bytes_in_buffer) {
num_bytes -= (long) src->pub.bytes_in_buffer;
(void) fill_input_buffer(cinfo);
/* note we assume that fill_input_buffer will never return FALSE,
* so suspension need not be handled.
*/
}
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
}
/**
Terminate source --- called by jpeg_finish_decompress
after all data has been read. Often a no-op.
NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
application must deal with any cleanup that should happen even
for error exit.
*/
METHODDEF(void)
term_source (j_decompress_ptr cinfo) {
// no work necessary here
}
// ----------------------------------------------------------
// Source manager & Destination manager setup
// ----------------------------------------------------------
/**
Prepare for input from a stdio stream.
The caller must have already opened the stream, and is responsible
for closing it after finishing decompression.
*/
GLOBAL(void)
jpeg_freeimage_src (j_decompress_ptr cinfo, fi_handle infile, FreeImageIO *io) {
freeimage_src_ptr src;
// allocate memory for the buffer. is released automatically in the end
if (cinfo->src == NULL) {
cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small)
((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(SourceManager));
src = (freeimage_src_ptr) cinfo->src;
src->buffer = (JOCTET *) (*cinfo->mem->alloc_small)
((j_common_ptr) cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof(JOCTET));
}
// initialize the jpeg pointer struct with pointers to functions
src = (freeimage_src_ptr) cinfo->src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; // use default method
src->pub.term_source = term_source;
src->infile = infile;
src->m_io = io;
src->pub.bytes_in_buffer = 0; // forces fill_input_buffer on first read
src->pub.next_input_byte = NULL; // until buffer loaded
}
/**
Prepare for output to a stdio stream.
The caller must have already opened the stream, and is responsible
for closing it after finishing compression.
*/
GLOBAL(void)
jpeg_freeimage_dst (j_compress_ptr cinfo, fi_handle outfile, FreeImageIO *io) {
freeimage_dst_ptr dest;
if (cinfo->dest == NULL) {
cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small)
((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(DestinationManager));
}
dest = (freeimage_dst_ptr) cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
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
( run in 0.443 second using v1.01-cache-2.11-cpan-119454b85a5 )