Loading...
Searching...
No Matches
decenc_jpeg2000.c
Go to the documentation of this file.
#include "grib2_int.h"
#include "jasper/jasper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXOPTSSIZE 1024
int
g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits,
int ltype, int ratio, int retry, char *outjpc,
size_t jpclen)
{
g2int width8 = width, height8 = height, nbits8 = nbits, ltype8 = ltype;
g2int ratio8 = ratio, retry8 = retry, jpclen8 = jpclen;
return enc_jpeg2000(cin, width8, height8, nbits8, ltype8, ratio8, retry8,
outjpc, jpclen8);
}
int
enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits,
g2int ltype, g2int ratio, g2int retry, char *outjpc,
g2int jpclen)
{
int ier, rwcnt;
jas_image_t image;
jas_stream_t *jpcstream, *istream;
jas_image_cmpt_t cmpt, *pcmpt;
char opts[MAXOPTSSIZE];
int fmt;
LOG((3, "enc_jpeg2000 width %ld height %ld nbits %ld ltype %ld ratio %ld retry %ld jpclen %d",
width, height, nbits, ltype, ratio, retry, jpclen));
/* Set lossy compression options, if requested. */
if (ltype != 1)
opts[0] = (char)0;
else
snprintf(opts, MAXOPTSSIZE, "mode=real\nrate=%f", 1.0 / (float)ratio);
if (retry == 1) /* option to increase number of guard bits */
strcat(opts, "\nnumgbits=4");
/* Initialize the JasPer image structure describing the grayscale
* image to encode into the JPEG2000 code stream. */
image.tlx_ = 0;
image.tly_ = 0;
image.brx_ = (jas_image_coord_t)width;
image.bry_ = (jas_image_coord_t)height;
image.numcmpts_ = 1;
image.maxcmpts_ = 1;
image.clrspc_ = JAS_CLRSPC_SGRAY; /* grayscale Image */
image.cmprof_ = 0;
cmpt.tlx_ = 0;
cmpt.tly_ = 0;
cmpt.hstep_ = 1;
cmpt.vstep_ = 1;
cmpt.width_ = (jas_image_coord_t)width;
cmpt.height_ = (jas_image_coord_t)height;
cmpt.type_ = JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
cmpt.prec_ = nbits;
cmpt.sgnd_ = 0;
cmpt.cps_ = (nbits + 7) / 8;
pcmpt = &cmpt;
image.cmpts_ = &pcmpt;
/* Initialize Jasper. */
#ifdef JASPER3
#define HUNDRED_MB 100000000
jas_conf_clear();
/* static jas_std_allocator_t allocator; */
/* jas_std_allocator_init(&allocator); */
/* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
jas_conf_set_max_mem_usage(HUNDRED_MB);
jas_conf_set_multithread(true);
if (jas_init_library())
return G2_JASPER_INIT;
if (jas_init_thread())
return G2_JASPER_INIT;
#else
if (jas_init())
return G2_JASPER_INIT;
#endif /* JASPER3 */
/* Open a JasPer stream containing the input grayscale values. */
istream = jas_stream_memopen((char *)cin, height * width * cmpt.cps_);
cmpt.stream_ = istream;
/* Open an output stream that will contain the encoded jpeg2000
* code stream. */
jpcstream = jas_stream_memopen(outjpc, (int)jpclen);
/* Get jasper ID of JPEG encoder. */
fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
/* Encode image. */
if ((ier = jas_image_encode(&image, jpcstream, fmt, opts)))
return G2_JASPER_ENCODE;
/* Rememeber the length in bytes of the encoded JPEG code
* stream. */
rwcnt = jpcstream->rwcnt_;
/* Clean up JasPer work structures. */
ier = jas_stream_close(istream);
ier = jas_stream_close(jpcstream);
/* Finalize jasper. */
#ifdef JASPER3
jas_cleanup_thread();
jas_cleanup_library();
#else
jas_cleanup();
#endif /* JASPER3 */
/* Return size of jpeg2000 code stream. */
return rwcnt;
}
static int
int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
{
g2int i, j, k;
jas_image_t *image = NULL;
jas_stream_t *jpcstream;
jas_image_cmpt_t *pcmpt;
char *opts = NULL;
jas_matrix_t *data;
int fmt;
LOG((3, "int_dec_jpeg2000 bufsize %ld out_is_g2int %d", bufsize, out_is_g2int));
/* Initialize Jasper. */
#ifdef JASPER3
jas_conf_clear();
/* static jas_std_allocator_t allocator; */
/* jas_std_allocator_init(&allocator); */
/* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
jas_conf_set_max_mem_usage(G2C_JASPER_MAX_MEM);
jas_conf_set_multithread(true);
if (jas_init_library())
return G2_JASPER_INIT;
if (jas_init_thread())
return G2_JASPER_INIT;
#else
if (jas_init())
return G2_JASPER_INIT;
#endif /* JASPER3 */
/* Create jas_stream_t containing input JPEG200 codestream in
* memory. */
jpcstream = jas_stream_memopen(injpc, bufsize);
/* Get jasper ID of JPEG encoder. */
fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
/* Decode JPEG200 codestream into jas_image_t structure. */
if (!(image = jas_image_decode(jpcstream, fmt, opts)))
return G2_JASPER_DECODE;
pcmpt = image->cmpts_[0];
/*
printf(" SAGOUT DECODE:\n");
printf(" tlx %d \n", image->tlx_);
printf(" tly %d \n", image->tly_);
printf(" brx %d \n", image->brx_);
printf(" bry %d \n", image->bry_);
printf(" numcmpts %d \n", image->numcmpts_);
printf(" maxcmpts %d \n", image->maxcmpts_);
printf(" colorspace %d \n", image->clrspc_);
printf(" inmem %d \n", image->inmem_);
printf(" COMPONENT:\n");
printf(" tlx %d \n", pcmpt->tlx_);
printf(" tly %d \n", pcmpt->tly_);
printf(" hstep %d \n", pcmpt->hstep_);
printf(" vstep %d \n", pcmpt->vstep_);
printf(" width %d \n", pcmpt->width_);
printf(" height %d \n", pcmpt->height_);
printf(" prec %d \n", pcmpt->prec_);
printf(" sgnd %d \n", pcmpt->sgnd_);
printf(" cps %d \n", pcmpt->cps_);
printf(" type %d \n", pcmpt->type_);
*/
/* Expecting jpeg2000 image to be grayscale only. No color components. */
if (image->numcmpts_ != 1)
return G2_JASPER_DECODE_COLOR;
/* Create a data matrix of grayscale image values decoded from the
* jpeg2000 codestream. */
data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
jas_image_height(image), data);
LOG((3, "pcmpt->height_ %d pcmpt->width_ %d", pcmpt->height_, pcmpt->width_));
/* Copy data matrix to output integer array. */
k = 0;
if (out_is_g2int)
{
for (i = 0; i < pcmpt->height_; i++)
for (j = 0; j < pcmpt->width_; j++)
((g2int *)outfld)[k++] = data->rows_[i][j];
}
else
{
for (i = 0; i < pcmpt->height_; i++)
for (j = 0; j < pcmpt->width_; j++)
((int *)outfld)[k++] = data->rows_[i][j];
}
/* Clean up JasPer work structures. */
jas_matrix_destroy(data);
jas_stream_close(jpcstream);
jas_image_destroy(image);
/* Finalize jasper. */
#ifdef JASPER3
jas_cleanup_thread();
jas_cleanup_library();
#else
jas_cleanup();
#endif /* JASPER3 */
return 0;
}
int
g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
{
return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
}
int
dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
{
return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
}
g2c_dec_jpeg2000
int g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
Decode a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using...
Definition decenc_jpeg2000.c:350
g2c_enc_jpeg2000
int g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits, int ltype, int ratio, int retry, char *outjpc, size_t jpclen)
Encode a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
Definition decenc_jpeg2000.c:45
MAXOPTSSIZE
#define MAXOPTSSIZE
Maximum size of options.
Definition decenc_jpeg2000.c:12
int_dec_jpeg2000
static int int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
Decode a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using...
Definition decenc_jpeg2000.c:221
dec_jpeg2000
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...
Definition decenc_jpeg2000.c:377
enc_jpeg2000
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....
Definition decenc_jpeg2000.c:94
G2_JASPER_INIT
#define G2_JASPER_INIT
In enc_jpeg2000()/dec_jpeg2000() error initializing jasper library.
Definition grib2.h:484
G2_JASPER_ENCODE
#define G2_JASPER_ENCODE
In enc_jpeg2000() error encoding image with jasper.
Definition grib2.h:485
G2_JASPER_DECODE
#define G2_JASPER_DECODE
In dec_jpeg2000() error decoding image with jasper.
Definition grib2.h:486
G2C_JASPER_MAX_MEM
#define G2C_JASPER_MAX_MEM
Maximum size for the Jasper memory buffer.
Definition grib2.h:432
G2_JASPER_DECODE_COLOR
#define G2_JASPER_DECODE_COLOR
In dec_jpeg2000() decoded image had multiple color components.
Definition grib2.h:487
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
G2C_JASPER_JPEG_FORMAT_NAME
#define G2C_JASPER_JPEG_FORMAT_NAME
Name of JPEG codec in Jasper.
Definition grib2_int.h:33
LOG
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428