NCEPLIBS-g2  3.4.7
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 
47  /* Initialize Jasper. */
48 #ifdef JASPER3
49  jas_conf_clear();
50  /* static jas_std_allocator_t allocator; */
51  /* jas_std_allocator_init(&allocator); */
52  /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
53  jas_conf_set_max_mem_usage(10000000);
54  jas_conf_set_multithread(true);
55  if (jas_init_library())
56  return G2_JASPER_INIT;
57  if (jas_init_thread())
58  return G2_JASPER_INIT;
59 #else
60  if (jas_init())
61  return G2_JASPER_INIT;
62 #endif /* JASPER3 */
63 
64  /* Create jas_stream_t containing input JPEG200 codestream in
65  * memory. */
66  jpcstream = jas_stream_memopen(injpc, bufsize);
67 
68  /* Get jasper ID of JPEG encoder. */
69  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
70 
71  /* Decode JPEG200 codestream into jas_image_t structure. */
72  if (!(image = jas_image_decode(jpcstream, fmt, opts)))
73  return G2_JASPER_DECODE;
74 
75  pcmpt = image->cmpts_[0];
76  /*
77  printf(" SAGOUT DECODE:\n");
78  printf(" tlx %d \n", image->tlx_);
79  printf(" tly %d \n", image->tly_);
80  printf(" brx %d \n", image->brx_);
81  printf(" bry %d \n", image->bry_);
82  printf(" numcmpts %d \n", image->numcmpts_);
83  printf(" maxcmpts %d \n", image->maxcmpts_);
84  printf(" colorspace %d \n", image->clrspc_);
85  printf(" inmem %d \n", image->inmem_);
86  printf(" COMPONENT:\n");
87  printf(" tlx %d \n", pcmpt->tlx_);
88  printf(" tly %d \n", pcmpt->tly_);
89  printf(" hstep %d \n", pcmpt->hstep_);
90  printf(" vstep %d \n", pcmpt->vstep_);
91  printf(" width %d \n", pcmpt->width_);
92  printf(" height %d \n", pcmpt->height_);
93  printf(" prec %d \n", pcmpt->prec_);
94  printf(" sgnd %d \n", pcmpt->sgnd_);
95  printf(" cps %d \n", pcmpt->cps_);
96  printf(" type %d \n", pcmpt->type_);
97  */
98 
99  /* Expecting jpeg2000 image to be grayscale only. No color components. */
100  if (image->numcmpts_ != 1)
101  return G2_JASPER_DECODE_COLOR;
102 
103  /* Create a data matrix of grayscale image values decoded from the
104  * jpeg2000 codestream. */
105  data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
106  jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
107  jas_image_height(image), data);
108 
109  /* Copy data matrix to output integer array. */
110  k = 0;
111  if (out_is_g2int)
112  {
113  for (i = 0; i < pcmpt->height_; i++)
114  for (j = 0; j < pcmpt->width_; j++)
115  ((g2int *)outfld)[k++] = data->rows_[i][j];
116  }
117  else
118  {
119  for (i = 0; i < pcmpt->height_; i++)
120  for (j = 0; j < pcmpt->width_; j++)
121  ((int *)outfld)[k++] = data->rows_[i][j];
122  }
123 
124  /* Clean up JasPer work structures. */
125  jas_matrix_destroy(data);
126  jas_stream_close(jpcstream);
127  jas_image_destroy(image);
128 
129  /* Finalize jasper. */
130 #ifdef JASPER3
131  jas_cleanup_thread();
132  jas_cleanup_library();
133 #else
134  jas_cleanup();
135 #endif /* JASPER3 */
136 
137  return 0;
138 }
139 
160 int
161 g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
162 {
163  return int_dec_jpeg2000(injpc, bufsize, outfld, 0);
164 }
165 
187 int
188 dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
189 {
190  return int_dec_jpeg2000(injpc, bufsize, outfld, 1);
191 }
192 
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:161
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:188
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 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