18#if !defined USE_JPEG2000 && defined USE_OPENJPEG
37openjpeg_warning(
const char *msg,
void *client_data)
40 fprintf(stderr,
"openjpeg: %s", msg);
52openjpeg_error(
const char *msg,
void *client_data)
55 fprintf(stderr,
"openjpeg: %s", msg);
67openjpeg_info(
const char *msg,
void *client_data)
98opj_memory_stream_read(
void *buffer, OPJ_SIZE_T nb_bytes,
void *p_user_data)
100 opj_memory_stream *mstream = (opj_memory_stream *)p_user_data;
101 OPJ_SIZE_T nb_bytes_read = nb_bytes;
104 if (mstream->offset >= mstream->dataSize)
105 return (OPJ_SIZE_T)-1;
108 if (nb_bytes > (mstream->dataSize - mstream->offset))
109 nb_bytes_read = mstream->dataSize - mstream->offset;
111 memcpy(buffer, &(mstream->pData[mstream->offset]), nb_bytes_read);
112 mstream->offset += nb_bytes_read;
113 return nb_bytes_read;
128opj_memory_stream_write(
void *buffer, OPJ_SIZE_T nb_bytes,
void *user_data)
130 opj_memory_stream *mstream = (opj_memory_stream *)user_data;
131 OPJ_SIZE_T nb_bytes_write = nb_bytes;
134 if (mstream->offset >= mstream->dataSize)
135 return (OPJ_SIZE_T)-1;
138 if (nb_bytes > (mstream->dataSize - mstream->offset))
139 nb_bytes_write = mstream->dataSize - mstream->offset;
142 memcpy(&(mstream->pData[mstream->offset]), buffer, nb_bytes_write);
143 mstream->offset += nb_bytes_write;
144 return nb_bytes_write;
158opj_memory_stream_skip(OPJ_OFF_T nb_bytes,
void *user_data)
160 opj_memory_stream *mstream = (opj_memory_stream *)user_data;
161 OPJ_SIZE_T l_nb_bytes;
165 l_nb_bytes = (OPJ_SIZE_T)nb_bytes;
167 if (l_nb_bytes > mstream->dataSize - mstream->offset)
168 l_nb_bytes = mstream->dataSize - mstream->offset;
169 mstream->offset += l_nb_bytes;
170 return (OPJ_OFF_T)l_nb_bytes;
184opj_memory_stream_seek(OPJ_OFF_T nb_bytes,
void *user_data)
186 opj_memory_stream *mstream = (opj_memory_stream *)user_data;
190 if (nb_bytes > (OPJ_OFF_T)mstream->dataSize)
192 mstream->offset = (OPJ_SIZE_T)nb_bytes;
204opj_memory_stream_do_nothing(
void *p_user_data)
206 OPJ_ARG_NOT_USED(p_user_data);
220opj_stream_create_default_memory_stream(opj_memory_stream *memoryStream, OPJ_BOOL is_read_stream)
222 opj_stream_t *stream;
224 if (!(stream = opj_stream_default_create(is_read_stream)))
228 opj_stream_set_read_function(stream, opj_memory_stream_read);
230 opj_stream_set_write_function(stream, opj_memory_stream_write);
232 opj_stream_set_seek_function(stream, opj_memory_stream_seek);
233 opj_stream_set_skip_function(stream, opj_memory_stream_skip);
234 opj_stream_set_user_data(stream, memoryStream, opj_memory_stream_do_nothing);
235 opj_stream_set_user_data_length(stream, memoryStream->dataSize);
266 opj_stream_t *stream = NULL;
267 opj_image_t *image = NULL;
268 opj_codec_t *codec = NULL;
271 opj_dparameters_t parameters = {
274 opj_set_default_decoder_parameters(¶meters);
275 parameters.decod_format = 1;
278 codec = opj_create_decompress(OPJ_CODEC_J2K);
281 opj_set_info_handler(codec, openjpeg_info, NULL);
282 opj_set_warning_handler(codec, openjpeg_warning, NULL);
283 opj_set_error_handler(codec, openjpeg_error, NULL);
286 opj_memory_stream mstream;
287 mstream.pData = (OPJ_UINT8 *)injpc;
288 mstream.dataSize = (OPJ_SIZE_T)bufsize;
291 stream = opj_stream_create_default_memory_stream(&mstream, OPJ_STREAM_READ);
294 if (!opj_setup_decoder(codec, ¶meters))
296 fprintf(stderr,
"openjpeg: failed to setup decoder");
300 if (!opj_read_header(stream, codec, &image))
302 fprintf(stderr,
"openjpeg: failed to read the header");
306 if (!opj_decode(codec, stream, image))
308 fprintf(stderr,
"openjpeg: failed to decode");
313 if ((image->numcomps != 1) || (image->x1 * image->y1) == 0)
319 assert(image->comps[0].sgnd == 0);
320 assert(image->comps[0].prec <
sizeof(mask) * 8 - 1);
322 mask = (1 << image->comps[0].prec) - 1;
324 for (
unsigned int i = 0; i < image->comps[0].w * image->comps[0].h; i++)
325 outfld[i] = (
g2int)(image->comps[0].data[i] & mask);
327 if (!opj_end_decompress(codec, stream))
329 fprintf(stderr,
"openjpeg: failed in opj_end_decompress");
336 opj_destroy_codec(codec);
338 opj_stream_destroy(stream);
340 opj_image_destroy(image);
386 const int numcomps = 1;
389 opj_codec_t *codec = NULL;
390 opj_image_t *image = NULL;
391 opj_stream_t *stream = NULL;
394 opj_cparameters_t parameters = {
397 opj_set_default_encoder_parameters(¶meters);
399 parameters.tcp_numlayers = 1;
400 parameters.cp_disto_alloc = 1;
403 assert(ratio != 255);
404 parameters.tcp_rates[0] = 1.0f / (float)ratio;
410 parameters.numresolution = 6;
411 while ((width < (1 << (parameters.numresolution - 1))) ||
412 (height < (1 << (parameters.numresolution - 1))))
414 parameters.numresolution--;
418 opj_image_cmptparm_t cmptparm = {
421 cmptparm.prec = (OPJ_UINT32)nbits;
422 cmptparm.bpp = (OPJ_UINT32)nbits;
426 cmptparm.w = (OPJ_UINT32)width;
427 cmptparm.h = (OPJ_UINT32)height;
430 image = opj_image_create(numcomps, &cmptparm, OPJ_CLRSPC_GRAY);
438 image->x1 = (OPJ_UINT32)width;
439 image->y1 = (OPJ_UINT32)height;
441 assert(cmptparm.prec <=
sizeof(image->comps[0].data[0]) * 8 - 1);
443 ifld = malloc(width * height *
sizeof(
g2int));
444 nbytes = (nbits + 7) / 8;
445 gbits(cin, ifld, 0, nbytes * 8, 0, width * height);
447 for (
int i = 0; i < width * height; i++)
449 image->comps[0].data[i] = ifld[i];
454 codec = opj_create_compress(OPJ_CODEC_J2K);
456 opj_set_info_handler(codec, openjpeg_info, NULL);
457 opj_set_warning_handler(codec, openjpeg_warning, NULL);
458 opj_set_error_handler(codec, openjpeg_error, NULL);
461 if (!opj_setup_encoder(codec, ¶meters, image))
463 fprintf(stderr,
"openjpeg: failed to setup encoder");
469 opj_memory_stream mstream;
470 mstream.pData = (OPJ_UINT8 *)outjpc;
472 mstream.dataSize = (OPJ_SIZE_T)jpclen;
473 stream = opj_stream_create_default_memory_stream(&mstream, OPJ_STREAM_WRITE);
476 fprintf(stderr,
"openjpeg: failed create default memory stream");
481 if (!opj_start_compress(codec, image, stream))
483 fprintf(stderr,
"openjpeg: failed to setup encoder");
489 if (!opj_encode(codec, stream))
491 fprintf(stderr,
"openjpeg: opj_encode failed");
496 if (!opj_end_compress(codec, stream))
498 fprintf(stderr,
"openjpeg: opj_end_compress failed");
502 iret = (int)mstream.offset;
506 opj_destroy_codec(codec);
508 opj_stream_destroy(stream);
510 opj_image_destroy(image);
int dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
Decode a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using...
int enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, g2int ltype, g2int ratio, g2int retry, char *outjpc, g2int jpclen)
Encode a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits, g2int nskip, g2int n)
Unpack arbitrary size values from a packed bit string, right justifying each value in the unpacked io...
int64_t g2int
Long integer type.
Header file with internal function prototypes NCEPLIBS-g2c library.