NCEPLIBS-g2c 2.1.0
Loading...
Searching...
No Matches
decenc_jpeg2000.c
Go to the documentation of this file.
1
6#include "grib2_int.h"
7#include "jasper/jasper.h"
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define MAXOPTSSIZE 1024
44int
45g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits,
46 int ltype, int ratio, int retry, char *outjpc,
47 size_t jpclen)
48{
49 g2int width8 = width, height8 = height, nbits8 = nbits, ltype8 = ltype;
50 g2int ratio8 = ratio, retry8 = retry, jpclen8 = jpclen;
51
52 return enc_jpeg2000(cin, width8, height8, nbits8, ltype8, ratio8, retry8,
53 outjpc, jpclen8);
54}
55
93int
94enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits,
95 g2int ltype, g2int ratio, g2int retry, char *outjpc,
96 g2int jpclen)
97{
98 int ier, rwcnt;
99 jas_image_t image;
100 jas_stream_t *jpcstream, *istream;
101 jas_image_cmpt_t cmpt, *pcmpt;
102 char opts[MAXOPTSSIZE];
103 int fmt;
104
105 LOG((3, "enc_jpeg2000 width %ld height %ld nbits %ld ltype %ld ratio %ld retry %ld jpclen %d",
106 width, height, nbits, ltype, ratio, retry, jpclen));
107
108 /* Set lossy compression options, if requested. */
109 if (ltype != 1)
110 opts[0] = (char)0;
111 else
112 snprintf(opts, MAXOPTSSIZE, "mode=real\nrate=%f", 1.0 / (float)ratio);
113
114 if (retry == 1) /* option to increase number of guard bits */
115 strcat(opts, "\nnumgbits=4");
116
117 /* Initialize the JasPer image structure describing the grayscale
118 * image to encode into the JPEG2000 code stream. */
119 image.tlx_ = 0;
120 image.tly_ = 0;
121 image.brx_ = (jas_image_coord_t)width;
122 image.bry_ = (jas_image_coord_t)height;
123 image.numcmpts_ = 1;
124 image.maxcmpts_ = 1;
125 image.clrspc_ = JAS_CLRSPC_SGRAY; /* grayscale Image */
126 image.cmprof_ = 0;
127
128 cmpt.tlx_ = 0;
129 cmpt.tly_ = 0;
130 cmpt.hstep_ = 1;
131 cmpt.vstep_ = 1;
132 cmpt.width_ = (jas_image_coord_t)width;
133 cmpt.height_ = (jas_image_coord_t)height;
134 cmpt.type_ = JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
135 cmpt.prec_ = nbits;
136 cmpt.sgnd_ = 0;
137 cmpt.cps_ = (nbits + 7) / 8;
138
139 pcmpt = &cmpt;
140 image.cmpts_ = &pcmpt;
141
142 /* Initialize Jasper. */
143#ifdef JASPER3
144#define HUNDRED_MB 100000000
145 jas_conf_clear();
146 /* static jas_std_allocator_t allocator; */
147 /* jas_std_allocator_init(&allocator); */
148 /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
149 jas_conf_set_max_mem_usage(HUNDRED_MB);
150 jas_conf_set_multithread(true);
151 if (jas_init_library())
152 return G2_JASPER_INIT;
153 if (jas_init_thread())
154 return G2_JASPER_INIT;
155#else
156 if (jas_init())
157 return G2_JASPER_INIT;
158#endif /* JASPER3 */
159
160 /* Open a JasPer stream containing the input grayscale values. */
161 istream = jas_stream_memopen((char *)cin, height * width * cmpt.cps_);
162 cmpt.stream_ = istream;
163
164 /* Open an output stream that will contain the encoded jpeg2000
165 * code stream. */
166 jpcstream = jas_stream_memopen(outjpc, (int)jpclen);
167
168 /* Get jasper ID of JPEG encoder. */
169 fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
170
171 /* Encode image. */
172 if ((ier = jas_image_encode(&image, jpcstream, fmt, opts)))
173 return G2_JASPER_ENCODE;
174
175 /* Rememeber the length in bytes of the encoded JPEG code
176 * stream. */
177 rwcnt = jpcstream->rwcnt_;
178
179 /* Clean up JasPer work structures. */
180 ier = jas_stream_close(istream);
181 ier = jas_stream_close(jpcstream);
182
183 /* Finalize jasper. */
184#ifdef JASPER3
185 jas_cleanup_thread();
186 jas_cleanup_library();
187#else
188 jas_cleanup();
189#endif /* JASPER3 */
190
191 /* Return size of jpeg2000 code stream. */
192 return rwcnt;
193}
194
220static int
221int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
222{
223 g2int i, j, k;
224 jas_image_t *image = NULL;
225 jas_stream_t *jpcstream;
226 jas_image_cmpt_t *pcmpt;
227 char *opts = NULL;
228 jas_matrix_t *data;
229 int fmt;
230
231 LOG((3, "int_dec_jpeg2000 bufsize %ld out_is_g2int %d", bufsize, out_is_g2int));
232
233 /* Initialize Jasper. */
234#ifdef JASPER3
235 jas_conf_clear();
236 /* static jas_std_allocator_t allocator; */
237 /* jas_std_allocator_init(&allocator); */
238 /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
239 jas_conf_set_max_mem_usage(G2C_JASPER_MAX_MEM);
240 jas_conf_set_multithread(true);
241 if (jas_init_library())
242 return G2_JASPER_INIT;
243 if (jas_init_thread())
244 return G2_JASPER_INIT;
245#else
246 if (jas_init())
247 return G2_JASPER_INIT;
248#endif /* JASPER3 */
249
250 /* Create jas_stream_t containing input JPEG200 codestream in
251 * memory. */
252 jpcstream = jas_stream_memopen(injpc, bufsize);
253
254 /* Get jasper ID of JPEG encoder. */
255 fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
256
257 /* Decode JPEG200 codestream into jas_image_t structure. */
258 if (!(image = jas_image_decode(jpcstream, fmt, opts)))
259 return G2_JASPER_DECODE;
260
261 pcmpt = image->cmpts_[0];
262 /*
263 printf(" SAGOUT DECODE:\n");
264 printf(" tlx %d \n", image->tlx_);
265 printf(" tly %d \n", image->tly_);
266 printf(" brx %d \n", image->brx_);
267 printf(" bry %d \n", image->bry_);
268 printf(" numcmpts %d \n", image->numcmpts_);
269 printf(" maxcmpts %d \n", image->maxcmpts_);
270 printf(" colorspace %d \n", image->clrspc_);
271 printf(" inmem %d \n", image->inmem_);
272 printf(" COMPONENT:\n");
273 printf(" tlx %d \n", pcmpt->tlx_);
274 printf(" tly %d \n", pcmpt->tly_);
275 printf(" hstep %d \n", pcmpt->hstep_);
276 printf(" vstep %d \n", pcmpt->vstep_);
277 printf(" width %d \n", pcmpt->width_);
278 printf(" height %d \n", pcmpt->height_);
279 printf(" prec %d \n", pcmpt->prec_);
280 printf(" sgnd %d \n", pcmpt->sgnd_);
281 printf(" cps %d \n", pcmpt->cps_);
282 printf(" type %d \n", pcmpt->type_);
283 */
284
285 /* Expecting jpeg2000 image to be grayscale only. No color components. */
286 if (image->numcmpts_ != 1)
288
289 /* Create a data matrix of grayscale image values decoded from the
290 * jpeg2000 codestream. */
291 data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
292 jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
293 jas_image_height(image), data);
294
295 LOG((3, "pcmpt->height_ %d pcmpt->width_ %d", pcmpt->height_, pcmpt->width_));
296
297 /* Copy data matrix to output integer array. */
298 k = 0;
299 if (out_is_g2int)
300 {
301 for (i = 0; i < pcmpt->height_; i++)
302 for (j = 0; j < pcmpt->width_; j++)
303 ((g2int *)outfld)[k++] = data->rows_[i][j];
304 }
305 else
306 {
307 for (i = 0; i < pcmpt->height_; i++)
308 for (j = 0; j < pcmpt->width_; j++)
309 ((int *)outfld)[k++] = data->rows_[i][j];
310 }
311
312 /* Clean up JasPer work structures. */
313 jas_matrix_destroy(data);
314 jas_stream_close(jpcstream);
315 jas_image_destroy(image);
316
317 /* Finalize jasper. */
318#ifdef JASPER3
319 jas_cleanup_thread();
320 jas_cleanup_library();
321#else
322 jas_cleanup();
323#endif /* JASPER3 */
324
325 return 0;
326}
327
349int
350g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
351{
352 return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
353}
354
376int
377dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
378{
379 return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
380}
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...
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....
#define MAXOPTSSIZE
Maximum size of options.
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...
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....
#define G2_JASPER_INIT
In enc_jpeg2000()/dec_jpeg2000() error initializing jasper library.
Definition grib2.h:478
#define G2_JASPER_ENCODE
In enc_jpeg2000() error encoding image with jasper.
Definition grib2.h:479
#define G2_JASPER_DECODE
In dec_jpeg2000() error decoding image with jasper.
Definition grib2.h:480
#define G2C_JASPER_MAX_MEM
Maximum size for the Jasper memory buffer.
Definition grib2.h:426
#define G2_JASPER_DECODE_COLOR
In dec_jpeg2000() decoded image had multiple color components.
Definition grib2.h:481
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.
#define G2C_JASPER_JPEG_FORMAT_NAME
Name of JPEG codec in Jasper.
Definition grib2_int.h:33
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428