104 const char *s, *magic_s;
105 int ext_format, magic_format;
106 unsigned char buf[12];
107 OPJ_SIZE_T l_nb_read;
109 reader = fopen(fname,
"rb");
111 if (reader == NULL) {
116 l_nb_read = fread(buf, 1, 12, reader);
118 if (l_nb_read != 12) {
128 magic_s =
".j2k, .j2c, .jpc, .jpx, or .jpf";
133 if (magic_format == ext_format) {
137 if (strlen(fname) >= 4) {
138 s = fname + strlen(fname) - 4;
139 std::ostringstream buffer;
140 buffer <<
"The extension of this file is incorrect.\n"
141 <<
"Found " << s <<
" while it should be " << magic_s <<
".";
149 const QByteArray file_str = filename.toUtf8();
150 opj_codec_t *l_codec = 0;
151 opj_dparameters_t parameters;
152 bool hasColorSpaceInfo =
false;
153 opj_stream_t *l_stream = NULL;
154 opj_image_t *
image = NULL;
157 unsigned int numComponents = 0;
158 unsigned int precision = 0;
163 int32_t signedCorrection = 0;
167 opj_set_default_decoder_parameters(¶meters);
169 parameters.decod_format =
infile_format(file_str.constData());
170 if (parameters.decod_format == -1) {
178 switch (parameters.decod_format) {
180 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
184 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
185 hasColorSpaceInfo =
true;
191 opj_codec_set_threads( l_codec,QThread::idealThreadCount() );
194 opj_setup_decoder(l_codec, ¶meters);
196 l_stream = opj_stream_create_default_file_stream(file_str.constData(), 1);
208 if (!opj_read_header(l_stream, l_codec, &
image)) {
215 if (!(opj_decode(l_codec, l_stream,
image)
216 && opj_end_decompress(l_codec, l_stream))) {
223 numComponents =
image->numcomps;
224 if (
image->numcomps == 0) {
229 precision =
image->comps[0].prec;
230 for (uint32_t i = 1; i < numComponents; ++i) {
231 if (
image->comps[i].prec != precision) {
232 std::ostringstream buffer;
233 buffer <<
"All components must have the same bit depth "
241 for (uint32_t i = 0; i < numComponents; ++i) {
242 if ((
image->comps[i].dx != 1) || (
image->comps[i].dy != 1)) {
247 isSigned = isSigned || (
image->comps[0].sgnd);
250 signedCorrection = 1 << (precision - 1);
253 <<
"Image has " << numComponents <<
" numComponents and a bit depth of "
254 << precision <<
" for color space " <<
image->color_space;
256 if (!hasColorSpaceInfo) {
257 if (numComponents == 3) {
258 image->color_space = OPJ_CLRSPC_SRGB;
259 }
else if (numComponents == 1) {
260 image->color_space = OPJ_CLRSPC_GRAY;
263 switch (
image->color_space) {
264 case OPJ_CLRSPC_UNKNOWN:
265 case OPJ_CLRSPC_UNSPECIFIED:
267 case OPJ_CLRSPC_SRGB: {
268 if (precision == 16 || precision == 12) {
270 }
else if (precision == 8) {
273 if (numComponents != 3) {
274 std::ostringstream buffer;
275 buffer <<
"sRGB: number of numComponents " << numComponents
276 <<
" does not equal 3";
286 case OPJ_CLRSPC_GRAY: {
287 if (precision == 16 || precision == 12) {
290 }
else if (precision == 8) {
294 if (numComponents != 1) {
295 std::ostringstream buffer;
296 buffer <<
"Grayscale: number of numComponents " << numComponents
297 <<
" greater than 1";
305 case OPJ_CLRSPC_SYCC:
310 case OPJ_CLRSPC_EYCC:
315 case OPJ_CLRSPC_CMYK:
335 colorSpace,
"built image");
345 for (OPJ_UINT32
v = 0;
v <
image->y1; ++
v) {
346 if (precision == 16 || precision == 12) {
348 quint16 *px =
reinterpret_cast<quint16*
>(it->rawData());
349 for (uint32_t i = 0; i < numComponents; ++i) {
350 px[channelorder[i]] =
image->comps[i].data[pos]
356 }
while (it->nextPixel());
357 }
else if (precision == 8) {
359 quint8 *px = it->rawData();
360 for (uint32_t i = 0; i < numComponents; ++i) {
361 px[channelorder[i]] =
image->comps[i].data[pos]
367 }
while (it->nextPixel());
374 opj_stream_destroy(l_stream);
376 opj_destroy_codec(l_codec);
378 opj_image_destroy(
image);