NCEPLIBS-g2c 2.0.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 jas_conf_clear();
145 /* static jas_std_allocator_t allocator; */
146 /* jas_std_allocator_init(&allocator); */
147 /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
148 jas_conf_set_max_mem_usage(10000000);
149 jas_conf_set_multithread(true);
150 if (jas_init_library())
151 return G2_JASPER_INIT;
152 if (jas_init_thread())
153 return G2_JASPER_INIT;
154#else
155 if (jas_init())
156 return G2_JASPER_INIT;
157#endif /* JASPER3 */
158
159 /* Open a JasPer stream containing the input grayscale values. */
160 istream = jas_stream_memopen((char *)cin, height * width * cmpt.cps_);
161 cmpt.stream_ = istream;
162
163 /* Open an output stream that will contain the encoded jpeg2000
164 * code stream. */
165 jpcstream = jas_stream_memopen(outjpc, (int)jpclen);
166
167 /* Get jasper ID of JPEG encoder. */
168 fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
169
170 /* Encode image. */
171 if ((ier = jas_image_encode(&image, jpcstream, fmt, opts)))
172 return G2_JASPER_ENCODE;
173
174 /* Rememeber the length in bytes of the encoded JPEG code
175 * stream. */
176 rwcnt = jpcstream->rwcnt_;
177
178 /* Clean up JasPer work structures. */
179 ier = jas_stream_close(istream);
180 ier = jas_stream_close(jpcstream);
181
182 /* Finalize jasper. */
183#ifdef JASPER3
184 jas_cleanup_thread();
185 jas_cleanup_library();
186#else
187 jas_cleanup();
188#endif /* JASPER3 */
189
190 /* Return size of jpeg2000 code stream. */
191 return rwcnt;
192}
193
219static int
220int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
221{
222 g2int i, j, k;
223 jas_image_t *image = NULL;
224 jas_stream_t *jpcstream;
225 jas_image_cmpt_t *pcmpt;
226 char *opts = NULL;
227 jas_matrix_t *data;
228 int fmt;
229
230 LOG((3, "int_dec_jpeg2000 bufsize %ld out_is_g2int %d", bufsize, out_is_g2int));
231
232 /* Initialize Jasper. */
233#ifdef JASPER3
234 jas_conf_clear();
235 /* static jas_std_allocator_t allocator; */
236 /* jas_std_allocator_init(&allocator); */
237 /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
238 jas_conf_set_max_mem_usage(G2C_JASPER_MAX_MEM);
239 jas_conf_set_multithread(true);
240 if (jas_init_library())
241 return G2_JASPER_INIT;
242 if (jas_init_thread())
243 return G2_JASPER_INIT;
244#else
245 if (jas_init())
246 return G2_JASPER_INIT;
247#endif /* JASPER3 */
248
249 /* Create jas_stream_t containing input JPEG200 codestream in
250 * memory. */
251 jpcstream = jas_stream_memopen(injpc, bufsize);
252
253 /* Get jasper ID of JPEG encoder. */
254 fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
255
256 /* Decode JPEG200 codestream into jas_image_t structure. */
257 if (!(image = jas_image_decode(jpcstream, fmt, opts)))
258 return G2_JASPER_DECODE;
259
260 pcmpt = image->cmpts_[0];
261 /*
262 printf(" SAGOUT DECODE:\n");
263 printf(" tlx %d \n", image->tlx_);
264 printf(" tly %d \n", image->tly_);
265 printf(" brx %d \n", image->brx_);
266 printf(" bry %d \n", image->bry_);
267 printf(" numcmpts %d \n", image->numcmpts_);
268 printf(" maxcmpts %d \n", image->maxcmpts_);
269 printf(" colorspace %d \n", image->clrspc_);
270 printf(" inmem %d \n", image->inmem_);
271 printf(" COMPONENT:\n");
272 printf(" tlx %d \n", pcmpt->tlx_);
273 printf(" tly %d \n", pcmpt->tly_);
274 printf(" hstep %d \n", pcmpt->hstep_);
275 printf(" vstep %d \n", pcmpt->vstep_);
276 printf(" width %d \n", pcmpt->width_);
277 printf(" height %d \n", pcmpt->height_);
278 printf(" prec %d \n", pcmpt->prec_);
279 printf(" sgnd %d \n", pcmpt->sgnd_);
280 printf(" cps %d \n", pcmpt->cps_);
281 printf(" type %d \n", pcmpt->type_);
282 */
283
284 /* Expecting jpeg2000 image to be grayscale only. No color components. */
285 if (image->numcmpts_ != 1)
287
288 /* Create a data matrix of grayscale image values decoded from the
289 * jpeg2000 codestream. */
290 data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
291 jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
292 jas_image_height(image), data);
293
294 LOG((3, "pcmpt->height_ %d pcmpt->width_ %d", pcmpt->height_, pcmpt->width_));
295
296 /* Copy data matrix to output integer array. */
297 k = 0;
298 if (out_is_g2int)
299 {
300 for (i = 0; i < pcmpt->height_; i++)
301 for (j = 0; j < pcmpt->width_; j++)
302 ((g2int *)outfld)[k++] = data->rows_[i][j];
303 }
304 else
305 {
306 for (i = 0; i < pcmpt->height_; i++)
307 for (j = 0; j < pcmpt->width_; j++)
308 ((int *)outfld)[k++] = data->rows_[i][j];
309 }
310
311 /* Clean up JasPer work structures. */
312 jas_matrix_destroy(data);
313 jas_stream_close(jpcstream);
314 jas_image_destroy(image);
315
316 /* Finalize jasper. */
317#ifdef JASPER3
318 jas_cleanup_thread();
319 jas_cleanup_library();
320#else
321 jas_cleanup();
322#endif /* JASPER3 */
323
324 return 0;
325}
326
348int
349g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
350{
351 return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
352}
353
375int
376dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
377{
378 return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
379}
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:470
#define G2_JASPER_ENCODE
In enc_jpeg2000() error encoding image with jasper.
Definition grib2.h:471
#define G2_JASPER_DECODE
In dec_jpeg2000() error decoding image with jasper.
Definition grib2.h:472
#define G2C_JASPER_MAX_MEM
Maximum size for the Jasper memory buffer.
Definition grib2.h:420
#define G2_JASPER_DECODE_COLOR
In dec_jpeg2000() decoded image had multiple color components.
Definition grib2.h:473
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