NCEPLIBS-g2  3.4.8
dec_jpeg2000.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "jasper/jasper.h"
10 #include "jpeg.h"
11 
36 static int
37 int_dec_jpeg2000(char *injpc, g2int bufsize, void *outfld, int out_is_g2int)
38 {
39  g2int i, j, k;
40  jas_image_t *image = NULL;
41  jas_stream_t *jpcstream;
42  jas_image_cmpt_t *pcmpt;
43  char *opts = NULL;
44  jas_matrix_t *data;
45  int fmt;
46  char *g2jaspermaxmem;
47 
48  /* Initialize Jasper. */
49 #ifdef JASPER3
50  jas_conf_clear();
51  /* static jas_std_allocator_t allocator; */
52  /* jas_std_allocator_init(&allocator); */
53  /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
54  if (( g2jaspermaxmem = getenv("G2_JASPER_MAXMEM")) != NULL )
55  jas_conf_set_max_mem_usage(atoi(g2jaspermaxmem));
56  else
57  jas_conf_set_max_mem_usage(G2C_JASPER_MAX_MEMORY);
58  jas_conf_set_multithread(true);
59  if (jas_init_library())
60  return G2_JASPER_INIT;
61  if (jas_init_thread())
62  return G2_JASPER_INIT;
63 #else
64  if (jas_init())
65  return G2_JASPER_INIT;
66 #endif /* JASPER3 */
67 
68  /* Create jas_stream_t containing input JPEG200 codestream in
69  * memory. */
70  jpcstream = jas_stream_memopen(injpc, bufsize);
71 
72  /* Get jasper ID of JPEG encoder. */
73  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
74 
75  /* Decode JPEG200 codestream into jas_image_t structure. */
76  if (!(image = jas_image_decode(jpcstream, fmt, opts)))
77  return G2_JASPER_DECODE;
78 
79  pcmpt = image->cmpts_[0];
80  /*
81  printf(" SAGOUT DECODE:\n");
82  printf(" tlx %d \n", image->tlx_);
83  printf(" tly %d \n", image->tly_);
84  printf(" brx %d \n", image->brx_);
85  printf(" bry %d \n", image->bry_);
86  printf(" numcmpts %d \n", image->numcmpts_);
87  printf(" maxcmpts %d \n", image->maxcmpts_);
88  printf(" colorspace %d \n", image->clrspc_);
89  printf(" inmem %d \n", image->inmem_);
90  printf(" COMPONENT:\n");
91  printf(" tlx %d \n", pcmpt->tlx_);
92  printf(" tly %d \n", pcmpt->tly_);
93  printf(" hstep %d \n", pcmpt->hstep_);
94  printf(" vstep %d \n", pcmpt->vstep_);
95  printf(" width %d \n", pcmpt->width_);
96  printf(" height %d \n", pcmpt->height_);
97  printf(" prec %d \n", pcmpt->prec_);
98  printf(" sgnd %d \n", pcmpt->sgnd_);
99  printf(" cps %d \n", pcmpt->cps_);
100  printf(" type %d \n", pcmpt->type_);
101  */
102 
103  /* Expecting jpeg2000 image to be grayscale only. No color components. */
104  if (image->numcmpts_ != 1)
105  return G2_JASPER_DECODE_COLOR;
106 
107  /* Create a data matrix of grayscale image values decoded from the
108  * jpeg2000 codestream. */
109  data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
110  jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
111  jas_image_height(image), data);
112 
113  /* Copy data matrix to output integer array. */
114  k = 0;
115  if (out_is_g2int)
116  {
117  for (i = 0; i < pcmpt->height_; i++)
118  for (j = 0; j < pcmpt->width_; j++)
119  ((g2int *)outfld)[k++] = data->rows_[i][j];
120  }
121  else
122  {
123  for (i = 0; i < pcmpt->height_; i++)
124  for (j = 0; j < pcmpt->width_; j++)
125  ((int *)outfld)[k++] = data->rows_[i][j];
126  }
127 
128  /* Clean up JasPer work structures. */
129  jas_matrix_destroy(data);
130  jas_stream_close(jpcstream);
131  jas_image_destroy(image);
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 0;
142 }
143 
164 int
165 g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
166 {
167  return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
168 }
169 
191 int
192 dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
193 {
194  return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
195 }
196 
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:165
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:37
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:192
int g2int
Integer type.
Definition: dec_png.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 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