103 const char *s, *magic_s;
104 int ext_format, magic_format;
105 unsigned char buf[12];
106 OPJ_SIZE_T l_nb_read;
108 reader = fopen(fname,
"rb");
110 if (reader == NULL) {
115 l_nb_read = fread(buf, 1, 12, reader);
117 if (l_nb_read != 12) {
127 magic_s =
".j2k, .j2c, .jpc, .jpx, or .jpf";
132 if (magic_format == ext_format) {
136 if (strlen(fname) >= 4) {
137 s = fname + strlen(fname) - 4;
138 std::ostringstream buffer;
139 buffer <<
"The extension of this file is incorrect.\n"
140 <<
"Found " << s <<
" while it should be " << magic_s <<
".";
148 const QByteArray file_str = filename.toUtf8();
149 opj_codec_t *l_codec = 0;
150 opj_dparameters_t parameters;
151 bool hasColorSpaceInfo =
false;
152 opj_stream_t *l_stream = NULL;
153 opj_image_t *
image = NULL;
156 unsigned int numComponents = 0;
157 unsigned int precision = 0;
162 int32_t signedCorrection = 0;
166 opj_set_default_decoder_parameters(¶meters);
168 parameters.decod_format =
infile_format(file_str.constData());
169 if (parameters.decod_format == -1) {
177 switch (parameters.decod_format) {
179 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
183 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
184 hasColorSpaceInfo =
true;
190 opj_codec_set_threads( l_codec,QThread::idealThreadCount() );
193 opj_setup_decoder(l_codec, ¶meters);
195 l_stream = opj_stream_create_default_file_stream(file_str.constData(), 1);
207 if (!opj_read_header(l_stream, l_codec, &
image)) {
214 if (!(opj_decode(l_codec, l_stream,
image)
215 && opj_end_decompress(l_codec, l_stream))) {
222 numComponents =
image->numcomps;
223 if (
image->numcomps == 0) {
228 precision =
image->comps[0].prec;
229 for (uint32_t i = 1; i < numComponents; ++i) {
230 if (
image->comps[i].prec != precision) {
231 std::ostringstream buffer;
232 buffer <<
"All components must have the same bit depth "
240 for (uint32_t i = 0; i < numComponents; ++i) {
241 if ((
image->comps[i].dx != 1) || (
image->comps[i].dy != 1)) {
246 isSigned = isSigned || (
image->comps[0].sgnd);
249 signedCorrection = 1 << (precision - 1);
252 <<
"Image has " << numComponents <<
" numComponents and a bit depth of "
253 << precision <<
" for color space " <<
image->color_space;
255 if (!hasColorSpaceInfo) {
256 if (numComponents == 3) {
257 image->color_space = OPJ_CLRSPC_SRGB;
258 }
else if (numComponents == 1) {
259 image->color_space = OPJ_CLRSPC_GRAY;
262 switch (
image->color_space) {
263 case OPJ_CLRSPC_UNKNOWN:
264 case OPJ_CLRSPC_UNSPECIFIED:
266 case OPJ_CLRSPC_SRGB: {
267 if (precision == 16 || precision == 12) {
269 }
else if (precision == 8) {
272 if (numComponents != 3) {
273 std::ostringstream buffer;
274 buffer <<
"sRGB: number of numComponents " << numComponents
275 <<
" does not equal 3";
285 case OPJ_CLRSPC_GRAY: {
286 if (precision == 16 || precision == 12) {
289 }
else if (precision == 8) {
293 if (numComponents != 1) {
294 std::ostringstream buffer;
295 buffer <<
"Grayscale: number of numComponents " << numComponents
296 <<
" greater than 1";
304 case OPJ_CLRSPC_SYCC:
309 case OPJ_CLRSPC_EYCC:
314 case OPJ_CLRSPC_CMYK:
334 colorSpace,
"built image");
344 for (OPJ_UINT32
v = 0;
v <
image->y1; ++
v) {
345 if (precision == 16 || precision == 12) {
347 quint16 *px =
reinterpret_cast<quint16*
>(it->rawData());
348 for (uint32_t i = 0; i < numComponents; ++i) {
349 px[channelorder[i]] =
image->comps[i].data[pos]
355 }
while (it->nextPixel());
356 }
else if (precision == 8) {
358 quint8 *px = it->rawData();
359 for (uint32_t i = 0; i < numComponents; ++i) {
360 px[channelorder[i]] =
image->comps[i].data[pos]
366 }
while (it->nextPixel());
373 opj_stream_destroy(l_stream);
375 opj_destroy_codec(l_codec);
377 opj_image_destroy(
image);