NCEPLIBS-g2  3.5.0
g2cjpeg2000.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "jasper/jasper.h"
9 #include "jpeg.h"
10 
11 #define MAXOPTSSIZE 1024
43 int
44 enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits,
45  g2int ltype, g2int ratio, g2int retry, char *outjpc,
46  g2int jpclen)
47 {
48  int ier, rwcnt;
49  jas_image_t image;
50  jas_stream_t *jpcstream, *istream;
51  jas_image_cmpt_t cmpt, *pcmpt;
52  char opts[MAXOPTSSIZE];
53  int fmt;
54  char *g2jaspermaxmem;
55 
56  /* Set lossy compression options, if requested. */
57  if (ltype != 1)
58  opts[0] = (char)0;
59  else
60  snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)ratio);
61 
62  if (retry == 1) /* option to increase number of guard bits */
63  strcat(opts,"\nnumgbits=4");
64 
65  /* Initialize the JasPer image structure describing the grayscale
66  * image to encode into the JPEG2000 code stream. */
67  image.tlx_ = 0;
68  image.tly_ = 0;
69  image.brx_ = (jas_image_coord_t)width;
70  image.bry_ = (jas_image_coord_t)height;
71  image.numcmpts_ = 1;
72  image.maxcmpts_ = 1;
73  image.clrspc_ = JAS_CLRSPC_SGRAY; /* grayscale Image */
74  image.cmprof_ = 0;
75 
76  cmpt.tlx_ = 0;
77  cmpt.tly_ = 0;
78  cmpt.hstep_ = 1;
79  cmpt.vstep_ = 1;
80  cmpt.width_ = (jas_image_coord_t)width;
81  cmpt.height_ = (jas_image_coord_t)height;
82  cmpt.type_ = JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
83  cmpt.prec_ = nbits;
84  cmpt.sgnd_ = 0;
85  cmpt.cps_ = (nbits + 7) / 8;
86 
87  pcmpt = &cmpt;
88  image.cmpts_ = &pcmpt;
89 
90  /* Initialize Jasper. */
91 #ifdef JASPER3
92  jas_conf_clear();
93  /* static jas_std_allocator_t allocator; */
94  /* jas_std_allocator_init(&allocator); */
95  /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
96  if (( g2jaspermaxmem = getenv("G2_JASPER_MAXMEM")) != NULL )
97  jas_conf_set_max_mem_usage(atoi(g2jaspermaxmem));
98  else
99  jas_conf_set_max_mem_usage(G2C_JASPER_MAX_MEMORY);
100  jas_conf_set_multithread(true);
101  if (jas_init_library())
102  return G2_JASPER_INIT;
103  if (jas_init_thread())
104  return G2_JASPER_INIT;
105 #else
106  if (jas_init())
107  return G2_JASPER_INIT;
108 #endif /* JASPER3 */
109 
110  /* Open a JasPer stream containing the input grayscale values. */
111  istream = jas_stream_memopen((char *)cin, height * width * cmpt.cps_);
112  cmpt.stream_ = istream;
113 
114  /* Open an output stream that will contain the encoded jpeg2000
115  * code stream. */
116  jpcstream = jas_stream_memopen(outjpc, (int)jpclen);
117 
118  /* Get jasper ID of JPEG encoder. */
119  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
120 
121  /* Encode image. */
122  if ((ier = jas_image_encode(&image, jpcstream, fmt, opts)))
123  return G2_JASPER_ENCODE;
124 
125  /* Rememeber the length in bytes of the encoded JPEG code
126  * stream. */
127  rwcnt = jpcstream->rwcnt_;
128 
129  /* Clean up JasPer work structures. */
130  ier = jas_stream_close(istream);
131  ier = jas_stream_close(jpcstream);
132 
133  /* Finalize jasper. */
134 #ifdef JASPER3
135  jas_cleanup_thread();
136  jas_cleanup_library();
137 #else
138  jas_cleanup();
139 #endif /* JASPER3 */
140 
141  /* Return size of jpeg2000 code stream. */
142  return rwcnt;
143 }
144 
175 int
176 g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits,
177  int ltype, int ratio, int retry, char *outjpc,
178  size_t jpclen)
179 {
180  g2int width8 = width, height8 = height, nbits8 = nbits, ltype8 = ltype;
181  g2int ratio8 = ratio, retry8 = retry, jpclen8 = jpclen;
182 
183  return enc_jpeg2000(cin, width8, height8, nbits8, ltype8, ratio8, retry8,
184  outjpc, jpclen8);
185 }
186 
211 static int
212 int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
213 {
214  g2int i, j, k;
215  jas_image_t *image = NULL;
216  jas_stream_t *jpcstream;
217  jas_image_cmpt_t *pcmpt;
218  char *opts = NULL;
219  jas_matrix_t *data;
220  int fmt;
221  char *g2jaspermaxmem;
222 
223  /* Initialize Jasper. */
224 #ifdef JASPER3
225  jas_conf_clear();
226  /* static jas_std_allocator_t allocator; */
227  /* jas_std_allocator_init(&allocator); */
228  /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
229  if (( g2jaspermaxmem = getenv("G2_JASPER_MAXMEM")) != NULL )
230  jas_conf_set_max_mem_usage(atoi(g2jaspermaxmem));
231  else
232  jas_conf_set_max_mem_usage(G2C_JASPER_MAX_MEMORY);
233  jas_conf_set_multithread(true);
234  if (jas_init_library())
235  return G2_JASPER_INIT;
236  if (jas_init_thread())
237  return G2_JASPER_INIT;
238 #else
239  if (jas_init())
240  return G2_JASPER_INIT;
241 #endif /* JASPER3 */
242 
243  /* Create jas_stream_t containing input JPEG200 codestream in
244  * memory. */
245  jpcstream = jas_stream_memopen(injpc, bufsize);
246 
247  /* Get jasper ID of JPEG encoder. */
248  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
249 
250  /* Decode JPEG200 codestream into jas_image_t structure. */
251  if (!(image = jas_image_decode(jpcstream, fmt, opts)))
252  return G2_JASPER_DECODE;
253 
254  pcmpt = image->cmpts_[0];
255  /*
256  printf(" SAGOUT DECODE:\n");
257  printf(" tlx %d \n", image->tlx_);
258  printf(" tly %d \n", image->tly_);
259  printf(" brx %d \n", image->brx_);
260  printf(" bry %d \n", image->bry_);
261  printf(" numcmpts %d \n", image->numcmpts_);
262  printf(" maxcmpts %d \n", image->maxcmpts_);
263  printf(" colorspace %d \n", image->clrspc_);
264  printf(" inmem %d \n", image->inmem_);
265  printf(" COMPONENT:\n");
266  printf(" tlx %d \n", pcmpt->tlx_);
267  printf(" tly %d \n", pcmpt->tly_);
268  printf(" hstep %d \n", pcmpt->hstep_);
269  printf(" vstep %d \n", pcmpt->vstep_);
270  printf(" width %d \n", pcmpt->width_);
271  printf(" height %d \n", pcmpt->height_);
272  printf(" prec %d \n", pcmpt->prec_);
273  printf(" sgnd %d \n", pcmpt->sgnd_);
274  printf(" cps %d \n", pcmpt->cps_);
275  printf(" type %d \n", pcmpt->type_);
276  */
277 
278  /* Expecting jpeg2000 image to be grayscale only. No color components. */
279  if (image->numcmpts_ != 1)
280  return G2_JASPER_DECODE_COLOR;
281 
282  /* Create a data matrix of grayscale image values decoded from the
283  * jpeg2000 codestream. */
284  data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
285  jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
286  jas_image_height(image), data);
287 
288  /* Copy data matrix to output integer array. */
289  k = 0;
290  if (out_is_g2int)
291  {
292  for (i = 0; i < pcmpt->height_; i++)
293  for (j = 0; j < pcmpt->width_; j++)
294  ((g2int *)outfld)[k++] = data->rows_[i][j];
295  }
296  else
297  {
298  for (i = 0; i < pcmpt->height_; i++)
299  for (j = 0; j < pcmpt->width_; j++)
300  ((int *)outfld)[k++] = data->rows_[i][j];
301  }
302 
303  /* Clean up JasPer work structures. */
304  jas_matrix_destroy(data);
305  jas_stream_close(jpcstream);
306  jas_image_destroy(image);
307 
308  /* Finalize jasper. */
309 #ifdef JASPER3
310  jas_cleanup_thread();
311  jas_cleanup_library();
312 #else
313  jas_cleanup();
314 #endif /* JASPER3 */
315 
316  return 0;
317 }
318 
339 int
340 g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
341 {
342  return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
343 }
344 
366 int
367 dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
368 {
369  return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
370 }
371 
int g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
Definition: g2cjpeg2000.c:340
int g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits, int ltype, int ratio, int retry, char *outjpc, size_t jpclen)
This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 ...
Definition: g2cjpeg2000.c:176
#define MAXOPTSSIZE
Maximum size of options.
Definition: g2cjpeg2000.c:11
static int int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
Definition: g2cjpeg2000.c:212
int dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
Definition: g2cjpeg2000.c:367
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 ...
Definition: g2cjpeg2000.c:44
int g2int
Integer type.
Definition: g2cpng.c:12
Header for JPEG C code.
#define G2C_JASPER_JPEG_FORMAT_NAME
Name of JPEG codec in Jasper.
Definition: jpeg.h:25
#define G2_JASPER_INIT
In enc_jpeg2000()/dec_jpeg2000() error initializing jasper library.
Definition: jpeg.h:19
#define G2_JASPER_ENCODE
In enc_jpeg2000() error encoding image with jasper.
Definition: jpeg.h:20
#define G2C_JASPER_MAX_MEMORY
Max memory size setting for Jasper.
Definition: jpeg.h:29
#define G2_JASPER_DECODE
In dec_jpeg2000() error decoding image with jasper.
Definition: jpeg.h:21
#define G2_JASPER_DECODE_COLOR
In dec_jpeg2000() decoded image had multiple color components.
Definition: jpeg.h:22
integer j
loop iterator