Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImage/PluginJP2.cpp view on Meta::CPAN
dib = J2KImageToFIBITMAP(s_format_id, image, header_only);
if(!dib) {
throw "Failed to import JPEG2000 image";
}
// free image data structure
opj_image_destroy(image);
return dib;
} catch (const char *text) {
if(dib) {
FreeImage_Unload(dib);
}
// free remaining structures
opj_destroy_codec(d_codec);
opj_image_destroy(image);
FreeImage_OutputMessageProc(s_format_id, text);
return NULL;
}
}
return NULL;
}
static BOOL DLL_CALLCONV
Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
J2KFIO_t *fio = (J2KFIO_t*)data;
if (dib && handle && fio) {
BOOL bSuccess;
opj_codec_t *c_codec = NULL; // handle to a compressor
opj_cparameters_t parameters; // compression parameters
opj_image_t *image = NULL; // image to encode
// get the OpenJPEG stream
opj_stream_t *c_stream = fio->stream;
// set encoding parameters to default values
opj_set_default_encoder_parameters(¶meters);
try {
parameters.tcp_numlayers = 0;
// if no rate entered, apply a 16:1 rate by default
if(flags == JP2_DEFAULT) {
parameters.tcp_rates[0] = (float)16;
} else {
// for now, the flags parameter is only used to specify the rate
parameters.tcp_rates[0] = (float)(flags & 0x3FF);
}
parameters.tcp_numlayers++;
parameters.cp_disto_alloc = 1;
// convert the dib to a OpenJPEG image
image = FIBITMAPToJ2KImage(s_format_id, dib, ¶meters);
if(!image) {
return FALSE;
}
// decide if MCT should be used
parameters.tcp_mct = (image->numcomps == 3) ? 1 : 0;
// encode the destination image
// get a JP2 compressor handle
c_codec = opj_create_compress(OPJ_CODEC_JP2);
// configure the event callbacks
// catch events using our callbacks (no local context needed here)
opj_set_info_handler(c_codec, NULL, NULL);
opj_set_warning_handler(c_codec, jp2_warning_callback, NULL);
opj_set_error_handler(c_codec, jp2_error_callback, NULL);
// setup the encoder parameters using the current image and using user parameters
opj_setup_encoder(c_codec, ¶meters, image);
// encode the image
bSuccess = opj_start_compress(c_codec, image, c_stream);
if(bSuccess) {
bSuccess = bSuccess && opj_encode(c_codec, c_stream);
if(bSuccess) {
bSuccess = bSuccess && opj_end_compress(c_codec, c_stream);
}
}
if (!bSuccess) {
throw "Failed to encode image";
}
// free remaining compression structures
opj_destroy_codec(c_codec);
// free image data
opj_image_destroy(image);
return TRUE;
} catch (const char *text) {
if(c_codec) opj_destroy_codec(c_codec);
if(image) opj_image_destroy(image);
FreeImage_OutputMessageProc(s_format_id, text);
return FALSE;
}
}
return FALSE;
}
// ==========================================================
// Init
// ==========================================================
void DLL_CALLCONV
InitJP2(Plugin *plugin, int format_id) {
s_format_id = format_id;
plugin->format_proc = Format;
plugin->description_proc = Description;
plugin->extension_proc = Extension;
plugin->regexpr_proc = RegExpr;
plugin->open_proc = Open;
( run in 1.414 second using v1.01-cache-2.11-cpan-172d661cebc )