8 #include "Jpeg2000Callbacks.h" 9 #include "Jpeg2000Color.h" 10 #include "Jpeg2000FormatDefs.h" 17 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a" 18 #define JP2_MAGIC "\x0d\x0a\x87\x0a" 19 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51" 25 void Jpeg2000::applyImageTweaks (opj_image_t *image)
const 27 if (image->color_space == OPJ_CLRSPC_SYCC) {
28 color_sycc_to_rgb (image);
31 if (image->color_space != OPJ_CLRSPC_SYCC &&
32 image->numcomps == 3 &&
33 image->comps[0].dx == image->comps[0].dy &&
34 image->comps[1].dx != 1) {
35 image->color_space = OPJ_CLRSPC_SYCC;
36 }
else if (image->numcomps <= 2) {
37 image->color_space = OPJ_CLRSPC_GRAY;
40 if (image->icc_profile_buf) {
41 #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) 42 color_apply_icc_profile (image);
44 free (image->icc_profile_buf);
45 image->icc_profile_buf = 0;
46 image->icc_profile_len = 0;
50 opj_codec_t *Jpeg2000::decode (
int decodeFormat)
const 55 return opj_create_decompress(OPJ_CODEC_J2K);
58 return opj_create_decompress(OPJ_CODEC_JP2);
61 return opj_create_decompress(OPJ_CODEC_JPT);
70 int Jpeg2000::getFileFormat(
const char *filename)
const 72 static const char *extension[] = {
"pgx",
"pnm",
"pgm",
"ppm",
"bmp",
73 "tif",
"raw",
"rawl",
"tga",
"png",
74 "j2k",
"jp2",
"jpt",
"j2c",
"jpc"};
75 static const int format[] = {PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT,
76 TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT,
77 J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT};
78 const char * ext = strrchr(filename,
'.');
84 for (
unsigned int i = 0; i <
sizeof(format)/
sizeof(*format); i++) {
85 if(strcasecmp(ext, extension[i]) == 0) {
94 void Jpeg2000::initializeParameters (opj_dparameters_t ¶meters)
const 96 parameters.cp_reduce = 0;
97 parameters.cp_layer = 0;
98 parameters.cod_format = 10;
99 parameters.decod_format = 1;
100 parameters.DA_x0 = 0;
101 parameters.DA_x1 = 0;
102 parameters.DA_y0 = 0;
103 parameters.DA_y1 = 0;
104 parameters.m_verbose = 0;
105 parameters.tile_index = 0;
106 parameters.nb_tile_to_decode = 0;
107 parameters.jpwl_correct = 0;
108 parameters.jpwl_exp_comps = 0;
109 parameters.jpwl_max_tiles = 0;
110 parameters.flags = 0;
113 int Jpeg2000::inputFormat(
const char *filename)
const 116 const char *s, *magic_s;
117 int ext_format, magic_format;
118 unsigned char buf[12];
119 OPJ_SIZE_T l_nb_read;
121 reader = fopen(filename,
124 if (reader == NULL) {
129 l_nb_read = fread(buf, 1, 12, reader);
131 if (l_nb_read != 12) {
135 ext_format = getFileFormat(filename);
137 if (ext_format == JPT_CFMT) {
141 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
142 magic_format = JP2_CFMT;
144 }
else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
145 magic_format = J2K_CFMT;
146 magic_s =
".j2k or .jpc or .j2c";
151 if (magic_format == ext_format) {
155 s = filename + strlen(filename) - 4;
157 LOG4CPP_ERROR_S ((*mainCat)) <<
"Jpeg2000::inputFormat" 158 <<
"The extension of this file is incorrect. Found " << s
159 <<
". Should be " << magic_s;
164 bool Jpeg2000::invalidFileExtension (
const QString &filename)
const 166 const int CHARACTER_IN_EXTENSION = 3;
172 QString extensionGot = filename.right (CHARACTER_IN_EXTENSION);
174 QStringList extensions = supportedFileExtensions();
175 QStringList::iterator itr;
176 for (itr = extensions.begin(); itr != extensions.end(); itr++) {
178 QString extensionWanted = *itr;
179 if (QString::compare (extensionGot,
181 Qt::CaseInsensitive)) {
193 QImage &imageResult)
const 195 LOG4CPP_INFO_S ((*mainCat)) <<
"Jpeg2000::load" 196 <<
" filename=" << filename.toLatin1().data();
198 if (invalidFileExtension (filename)) {
202 opj_dparameters_t parameters;
203 initializeParameters (parameters);
205 parameters.decod_format = inputFormat (filename.toLatin1().data());
207 opj_stream_t *inStream = opj_stream_create_default_file_stream (filename.toLatin1().data(), 1);
209 LOG4CPP_ERROR_S ((*mainCat)) <<
"Jpeg2000::load encountered error opening stream";
214 opj_codec_t *inCodec = decode (parameters.decod_format);
216 LOG4CPP_ERROR_S ((*mainCat)) <<
"Jpeg2000::load encountered error creating decoding stream";
217 opj_stream_destroy (inStream);
222 opj_set_info_handler (inCodec, infoCallback, 0);
223 opj_set_warning_handler (inCodec, warningCallback, 0);
224 opj_set_error_handler (inCodec, errorCallback, 0);
226 if (!opj_setup_decoder (inCodec,
228 LOG4CPP_ERROR_S ((*mainCat)) <<
"Jpeg2000::load encountered error decoding stream";
229 opj_stream_destroy (inStream);
230 opj_destroy_codec (inCodec);
236 if (!opj_read_header (inStream,
239 LOG4CPP_ERROR_S ((*mainCat)) <<
"Jpeg2000::load encountered error reading header";
240 opj_stream_destroy (inStream);
241 opj_destroy_codec (inCodec);
242 opj_image_destroy (image);
247 if (!(opj_decode (inCodec,
250 opj_end_decompress (inCodec,
252 LOG4CPP_ERROR_S ((*mainCat)) <<
"Jpeg2000::load failed to decode image";
253 opj_destroy_codec (inCodec);
254 opj_stream_destroy (inStream);
255 opj_image_destroy (image);
260 opj_stream_destroy (inStream);
262 applyImageTweaks (image);
267 buffer.open (QBuffer::WriteOnly);
268 if (imagetopnm (image,
270 LOG4CPP_ERROR_S ((*mainCat)) <<
"Jpeg2000::load failed to generate new image";
282 imageResult.loadFromData(buffer.data());
288 opj_destroy_codec (inCodec);
290 opj_image_destroy (image);
295 QStringList Jpeg2000::supportedFileExtensions ()
const 297 QStringList extensions;
300 extensions <<
"j2k" <<
"jp2" <<
"jpc" <<
"jpt";
307 QStringList extensions = supportedFileExtensions();
308 QStringList wildcards;
310 QStringList::iterator itr;
311 for (itr = extensions.begin(); itr != extensions.end(); itr++) {
312 QString extension = *itr;
313 QString wildcard = QString (
"*.%1").arg (extension);
314 wildcards << wildcard;
QStringList supportedImageWildcards() const
List the supported jpeg2000 file extensions, for filtering import files.
bool load(const QString &filename, QImage &image) const
Load image from jpeg2000 file.
Jpeg2000()
Single constructor.