NCEPLIBS-g2c  1.8.0
dec_jpeg2000.c
Go to the documentation of this file.
1 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "grib2_int.h"
16 #include "jasper/jasper.h"
17 
43 static int
44 int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
45 {
46  g2int i, j, k;
47  jas_image_t *image = NULL;
48  jas_stream_t *jpcstream;
49  jas_image_cmpt_t *pcmpt;
50  char *opts = NULL;
51  jas_matrix_t *data;
52  int fmt;
53 
54  LOG((3, "int_dec_jpeg2000 bufsize %ld out_is_g2int %d", bufsize, out_is_g2int));
55 
56  /* Initialize Jasper. */
57 #ifdef JASPER3
58  jas_conf_clear();
59  /* static jas_std_allocator_t allocator; */
60  /* jas_std_allocator_init(&allocator); */
61  /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
62  jas_conf_set_max_mem_usage(G2C_JASPER_MAX_MEM);
63  jas_conf_set_multithread(true);
64  if (jas_init_library())
65  return G2_JASPER_INIT;
66  if (jas_init_thread())
67  return G2_JASPER_INIT;
68 #else
69  if (jas_init())
70  return G2_JASPER_INIT;
71 #endif /* JASPER3 */
72 
73  /* Create jas_stream_t containing input JPEG200 codestream in
74  * memory. */
75  jpcstream = jas_stream_memopen(injpc, bufsize);
76 
77  /* Get jasper ID of JPEG encoder. */
78  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
79 
80  /* Decode JPEG200 codestream into jas_image_t structure. */
81  if (!(image = jas_image_decode(jpcstream, fmt, opts)))
82  return G2_JASPER_DECODE;
83 
84  pcmpt = image->cmpts_[0];
85  /*
86  printf(" SAGOUT DECODE:\n");
87  printf(" tlx %d \n", image->tlx_);
88  printf(" tly %d \n", image->tly_);
89  printf(" brx %d \n", image->brx_);
90  printf(" bry %d \n", image->bry_);
91  printf(" numcmpts %d \n", image->numcmpts_);
92  printf(" maxcmpts %d \n", image->maxcmpts_);
93  printf(" colorspace %d \n", image->clrspc_);
94  printf(" inmem %d \n", image->inmem_);
95  printf(" COMPONENT:\n");
96  printf(" tlx %d \n", pcmpt->tlx_);
97  printf(" tly %d \n", pcmpt->tly_);
98  printf(" hstep %d \n", pcmpt->hstep_);
99  printf(" vstep %d \n", pcmpt->vstep_);
100  printf(" width %d \n", pcmpt->width_);
101  printf(" height %d \n", pcmpt->height_);
102  printf(" prec %d \n", pcmpt->prec_);
103  printf(" sgnd %d \n", pcmpt->sgnd_);
104  printf(" cps %d \n", pcmpt->cps_);
105  printf(" type %d \n", pcmpt->type_);
106  */
107 
108  /* Expecting jpeg2000 image to be grayscale only. No color components. */
109  if (image->numcmpts_ != 1)
110  return G2_JASPER_DECODE_COLOR;
111 
112  /* Create a data matrix of grayscale image values decoded from the
113  * jpeg2000 codestream. */
114  data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
115  jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
116  jas_image_height(image), data);
117 
118 
119  LOG((3, "pcmpt->height_ %d pcmpt->width_ %d", pcmpt->height_, pcmpt->width_));
120 
121  /* Copy data matrix to output integer array. */
122  k = 0;
123  if (out_is_g2int)
124  {
125  for (i = 0; i < pcmpt->height_; i++)
126  for (j = 0; j < pcmpt->width_; j++)
127  ((g2int *)outfld)[k++] = data->rows_[i][j];
128  }
129  else
130  {
131  for (i = 0; i < pcmpt->height_; i++)
132  for (j = 0; j < pcmpt->width_; j++)
133  ((int *)outfld)[k++] = data->rows_[i][j];
134  }
135 
136  /* Clean up JasPer work structures. */
137  jas_matrix_destroy(data);
138  jas_stream_close(jpcstream);
139  jas_image_destroy(image);
140 
141  /* Finalize jasper. */
142 #ifdef JASPER3
143  jas_cleanup_thread();
144  jas_cleanup_library();
145 #else
146  jas_cleanup();
147 #endif /* JASPER3 */
148 
149  return 0;
150 }
151 
173 int
174 g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
175 {
176  return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
177 }
178 
200 int
201 dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
202 {
203  return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
204 }
205 
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: dec_jpeg2000.c:174
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: dec_jpeg2000.c:44
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: dec_jpeg2000.c:201
#define G2_JASPER_INIT
In enc_jpeg2000()/dec_jpeg2000() error initializing jasper library.
Definition: grib2.h:485
#define G2_JASPER_DECODE
In dec_jpeg2000() error decoding image with jasper.
Definition: grib2.h:487
#define G2C_JASPER_MAX_MEM
Maximum size for the Jasper memory buffer.
Definition: grib2.h:435
#define G2_JASPER_DECODE_COLOR
In dec_jpeg2000() decoded image had multiple color components.
Definition: grib2.h:488
int64_t g2int
Long integer type.
Definition: grib2.h:33
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:426