19 #if !defined USE_JPEG2000 && defined USE_OPENJPEG
37 static void openjpeg_warning(
const char *msg,
void *client_data)
40 fprintf(stderr,
"openjpeg: %s",msg);
51 static void openjpeg_error(
const char *msg,
void *client_data)
54 fprintf(stderr,
"openjpeg: %s",msg);
65 static void openjpeg_info(
const char *msg,
void *client_data)
95 static OPJ_SIZE_T opj_memory_stream_read(
void *buffer, OPJ_SIZE_T nb_bytes,
void * p_user_data)
97 opj_memory_stream* mstream = (opj_memory_stream*) p_user_data;
98 OPJ_SIZE_T nb_bytes_read = nb_bytes;
101 if (mstream->offset >= mstream->dataSize)
102 return (OPJ_SIZE_T) -1;
105 if (nb_bytes > (mstream->dataSize - mstream->offset))
106 nb_bytes_read = mstream->dataSize - mstream->offset;
108 memcpy(buffer, &(mstream->pData[mstream->offset]), nb_bytes_read);
109 mstream->offset += nb_bytes_read;
110 return nb_bytes_read;
124 static OPJ_SIZE_T opj_memory_stream_write(
void *buffer, OPJ_SIZE_T nb_bytes,
void *user_data)
126 opj_memory_stream* mstream = (opj_memory_stream*) user_data;
127 OPJ_SIZE_T nb_bytes_write = nb_bytes;
130 if (mstream->offset >= mstream->dataSize)
131 return (OPJ_SIZE_T)-1;
134 if (nb_bytes > (mstream->dataSize - mstream->offset))
135 nb_bytes_write = mstream->dataSize - mstream->offset;
138 memcpy(&(mstream->pData[mstream->offset]), buffer, nb_bytes_write);
139 mstream->offset += nb_bytes_write;
140 return nb_bytes_write;
153 static OPJ_OFF_T opj_memory_stream_skip(OPJ_OFF_T nb_bytes,
void *user_data)
155 opj_memory_stream* mstream = (opj_memory_stream*) user_data;
156 OPJ_SIZE_T l_nb_bytes;
160 l_nb_bytes = (OPJ_SIZE_T) nb_bytes;
162 if (l_nb_bytes > mstream->dataSize - mstream->offset)
163 l_nb_bytes = mstream->dataSize - mstream->offset;
164 mstream->offset += l_nb_bytes;
165 return (OPJ_OFF_T)l_nb_bytes;
178 static OPJ_BOOL opj_memory_stream_seek(OPJ_OFF_T nb_bytes,
void * user_data)
180 opj_memory_stream* mstream = (opj_memory_stream*) user_data;
184 if (nb_bytes >(OPJ_OFF_T) mstream->dataSize)
186 mstream->offset = (OPJ_SIZE_T) nb_bytes;
197 static void opj_memory_stream_do_nothing(
void * p_user_data)
199 OPJ_ARG_NOT_USED(p_user_data);
212 static opj_stream_t* opj_stream_create_default_memory_stream(opj_memory_stream* memoryStream, OPJ_BOOL is_read_stream)
214 opj_stream_t* stream;
216 if (!(stream = opj_stream_default_create(is_read_stream)))
220 opj_stream_set_read_function(stream, opj_memory_stream_read);
222 opj_stream_set_write_function(stream, opj_memory_stream_write);
224 opj_stream_set_seek_function(stream, opj_memory_stream_seek);
225 opj_stream_set_skip_function(stream, opj_memory_stream_skip);
226 opj_stream_set_user_data(stream, memoryStream, opj_memory_stream_do_nothing);
227 opj_stream_set_user_data_length(stream, memoryStream->dataSize);
257 opj_stream_t *stream = NULL;
258 opj_image_t *image = NULL;
259 opj_codec_t *codec = NULL;
262 opj_dparameters_t parameters = {0,};
263 opj_set_default_decoder_parameters(¶meters);
264 parameters.decod_format = 1;
267 #define DUMP_JPEG_STREAM
268 #ifdef DUMP_JPEG_STREAM
269 FILE *fp=fopen(
"dump_openjpeg.j2k",
"wb");
270 fwrite(injpc, 1, (
size_t)bufsize, fp);
275 codec = opj_create_decompress(OPJ_CODEC_J2K);
278 opj_set_info_handler(codec, openjpeg_info, NULL);
279 opj_set_warning_handler(codec, openjpeg_warning, NULL);
280 opj_set_error_handler(codec, openjpeg_error,NULL);
283 opj_memory_stream mstream;
284 mstream.pData = (OPJ_UINT8 *)injpc;
285 mstream.dataSize = (OPJ_SIZE_T)bufsize;
288 stream = opj_stream_create_default_memory_stream( &mstream, OPJ_STREAM_READ);
291 if (!opj_setup_decoder(codec, ¶meters)) {
292 fprintf(stderr,
"openjpeg: failed to setup decoder");
296 if (!opj_read_header(stream, codec, &image)) {
297 fprintf(stderr,
"openjpeg: failed to read the header");
301 if (!opj_decode(codec, stream, image)) {
302 fprintf(stderr,
"openjpeg: failed to decode");
307 if ( (image->numcomps != 1) || (image->x1 * image->y1)==0 ) {
312 assert(image->comps[0].sgnd == 0);
313 assert(image->comps[0].prec <
sizeof(mask)*8-1);
315 mask = (1 << image->comps[0].prec) - 1;
317 for(
unsigned int i = 0; i < image->comps[0].w * image->comps[0].h ; i++)
318 outfld[i] = (
g2int) (image->comps[0].data[i] & mask);
320 if (!opj_end_decompress(codec, stream)) {
321 fprintf(stderr,
"openjpeg: failed in opj_end_decompress");
327 if (codec) opj_destroy_codec(codec);
328 if (stream) opj_stream_destroy(stream);
329 if (image) opj_image_destroy(image);
373 const int numcomps = 1;
375 opj_codec_t *codec = NULL;
376 opj_image_t *image = NULL;
377 opj_stream_t *stream = NULL;
380 opj_cparameters_t parameters = {0,};
381 opj_set_default_encoder_parameters(¶meters);
383 parameters.tcp_numlayers = 1;
384 parameters.cp_disto_alloc = 1;
386 assert(ratio != 255);
387 parameters.tcp_rates[0] = 1.0f/(float)ratio;
393 parameters.numresolution = 6;
394 while ( (width < (1 << (parameters.numresolution - 1)) ) ||
395 (height < (1 << (parameters.numresolution - 1)) ))
397 parameters.numresolution--;
401 opj_image_cmptparm_t cmptparm = {0,};
402 cmptparm.prec = (OPJ_UINT32)nbits;
403 cmptparm.bpp = (OPJ_UINT32)nbits;
407 cmptparm.w = (OPJ_UINT32)width;
408 cmptparm.h = (OPJ_UINT32)height;
411 image = opj_image_create(numcomps, &cmptparm, OPJ_CLRSPC_GRAY);
418 image->x1 = (OPJ_UINT32)width;
419 image->y1 = (OPJ_UINT32)height;
421 assert(cmptparm.prec <=
sizeof(image->comps[0].data[0])*8 - 1);
424 for(
int i=0; i< width*height; i++){
425 image->comps[0].data[i] = cin[i];
429 codec = opj_create_compress(OPJ_CODEC_J2K);
431 opj_set_info_handler(codec, openjpeg_info, NULL);
432 opj_set_warning_handler(codec, openjpeg_warning, NULL);
433 opj_set_error_handler(codec, openjpeg_error,NULL);
436 if (!opj_setup_encoder(codec, ¶meters, image)) {
437 fprintf(stderr,
"openjpeg: failed to setup encoder");
443 opj_memory_stream mstream;
444 mstream.pData = (OPJ_UINT8*) outjpc;
446 mstream.dataSize = (OPJ_SIZE_T)jpclen;
447 stream = opj_stream_create_default_memory_stream(&mstream, OPJ_STREAM_WRITE);
448 if (stream == NULL) {
449 fprintf(stderr,
"openjpeg: failed create default memory stream");
454 if (!opj_start_compress(codec, image, stream)) {
455 fprintf(stderr,
"openjpeg: failed to setup encoder");
461 if (!opj_encode(codec, stream)) {
462 fprintf(stderr,
"openjpeg: opj_encode failed");
467 if (!opj_end_compress(codec, stream)) {
468 fprintf(stderr,
"openjpeg: opj_end_compress failed");
472 iret = (int)mstream.offset;
475 if (codec) opj_destroy_codec(codec);
476 if (stream) opj_stream_destroy(stream);
477 if (image) opj_image_destroy(image);
int dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
int enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, g2int ltype, g2int ratio, g2int retry, char *outjpc, g2int jpclen)
This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 ...
Header file for NCEPLIBS-g2c library.
int64_t g2int
Long integer type.