NCEPLIBS-g2c  1.7.0
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 "grib2_int.h"
10 #include "jasper/jasper.h"
11 
37 int
38 dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
39 {
40  g2int i, j, k;
41  jas_image_t *image = NULL;
42  jas_stream_t *jpcstream;
43  jas_image_cmpt_t *pcmpt;
44  char *opts = NULL;
45  jas_matrix_t *data;
46  int fmt;
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  jas_conf_set_max_mem_usage(10000000);
55  jas_conf_set_multithread(true);
56  if (jas_init_library())
57  return G2_JASPER_INIT;
58  if (jas_init_thread())
59  return G2_JASPER_INIT;
60 #else
61  if (jas_init())
62  return G2_JASPER_INIT;
63 #endif /* JASPER3 */
64 
65  /* Create jas_stream_t containing input JPEG200 codestream in
66  * memory. */
67  jpcstream = jas_stream_memopen(injpc, bufsize);
68 
69  /* Get jasper ID of JPEG encoder. */
70  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
71 
72  /* Decode JPEG200 codestream into jas_image_t structure. */
73  if (!(image = jas_image_decode(jpcstream, fmt, opts)))
74  return G2_JASPER_DECODE;
75 
76  pcmpt = image->cmpts_[0];
77  /*
78  printf(" SAGOUT DECODE:\n");
79  printf(" tlx %d \n", image->tlx_);
80  printf(" tly %d \n", image->tly_);
81  printf(" brx %d \n", image->brx_);
82  printf(" bry %d \n", image->bry_);
83  printf(" numcmpts %d \n", image->numcmpts_);
84  printf(" maxcmpts %d \n", image->maxcmpts_);
85  printf(" colorspace %d \n", image->clrspc_);
86  printf(" inmem %d \n", image->inmem_);
87  printf(" COMPONENT:\n");
88  printf(" tlx %d \n", pcmpt->tlx_);
89  printf(" tly %d \n", pcmpt->tly_);
90  printf(" hstep %d \n", pcmpt->hstep_);
91  printf(" vstep %d \n", pcmpt->vstep_);
92  printf(" width %d \n", pcmpt->width_);
93  printf(" height %d \n", pcmpt->height_);
94  printf(" prec %d \n", pcmpt->prec_);
95  printf(" sgnd %d \n", pcmpt->sgnd_);
96  printf(" cps %d \n", pcmpt->cps_);
97  printf(" type %d \n", pcmpt->type_);
98  */
99 
100  /* Expecting jpeg2000 image to be grayscale only. No color components. */
101  if (image->numcmpts_ != 1)
102  return G2_JASPER_DECODE_COLOR;
103 
104  /* Create a data matrix of grayscale image values decoded from the
105  * jpeg2000 codestream. */
106  data = jas_matrix_create(jas_image_height(image), jas_image_width(image));
107  jas_image_readcmpt(image, 0, 0, 0, jas_image_width(image),
108  jas_image_height(image), data);
109 
110  /* Copy data matrix to output integer array. */
111  k = 0;
112  for (i = 0; i < pcmpt->height_; i++)
113  for (j = 0; j < pcmpt->width_; j++)
114  outfld[k++] = data->rows_[i][j];
115 
116  /* Clean up JasPer work structures. */
117  jas_matrix_destroy(data);
118  jas_stream_close(jpcstream);
119  jas_image_destroy(image);
120 
121  /* Finalize jasper. */
122 #ifdef JASPER3
123  jas_cleanup_thread();
124  jas_cleanup_library();
125 #else
126  jas_cleanup();
127 #endif /* JASPER3 */
128 
129  return 0;
130 }
G2_JASPER_INIT
#define G2_JASPER_INIT
In enc_jpeg2000()/dec_jpeg2000() error initializing jasper library.
Definition: grib2.h:322
G2C_JASPER_JPEG_FORMAT_NAME
#define G2C_JASPER_JPEG_FORMAT_NAME
Name of JPEG codec in Jasper.
Definition: grib2_int.h:23
G2_JASPER_DECODE_COLOR
#define G2_JASPER_DECODE_COLOR
In dec_jpeg2000() decoded image had multiple color components.
Definition: grib2.h:325
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
dec_jpeg2000
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:38
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
G2_JASPER_DECODE
#define G2_JASPER_DECODE
In dec_jpeg2000() error decoding image with jasper.
Definition: grib2.h:324