NCEPLIBS-g2c  1.8.0
enc_jpeg2000.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "grib2_int.h"
9 #include "jasper/jasper.h"
10 
11 #define MAXOPTSSIZE 1024
50 int
51 g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits,
52  int ltype, int ratio, int retry, char *outjpc,
53  size_t jpclen)
54 {
55  g2int width8 = width, height8 = height, nbits8 = nbits, ltype8 = ltype;
56  g2int ratio8 = ratio, retry8 = retry, jpclen8 = jpclen;
57 
58  return enc_jpeg2000(cin, width8, height8, nbits8, ltype8, ratio8, retry8,
59  outjpc, jpclen8);
60 }
61 
99 int
100 enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits,
101  g2int ltype, g2int ratio, g2int retry, char *outjpc,
102  g2int jpclen)
103 {
104  int ier, rwcnt;
105  jas_image_t image;
106  jas_stream_t *jpcstream, *istream;
107  jas_image_cmpt_t cmpt, *pcmpt;
108  char opts[MAXOPTSSIZE];
109  int fmt;
110 
111  LOG((3, "enc_jpeg2000 width %ld height %ld nbits %ld ltype %ld ratio %ld retry %ld jpclen %d",
112  width, height, nbits, ltype, ratio, retry, jpclen));
113 
114  /* Set lossy compression options, if requested. */
115  if (ltype != 1)
116  opts[0] = (char)0;
117  else
118  snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)ratio);
119 
120  if (retry == 1) /* option to increase number of guard bits */
121  strcat(opts,"\nnumgbits=4");
122 
123  /* Initialize the JasPer image structure describing the grayscale
124  * image to encode into the JPEG2000 code stream. */
125  image.tlx_ = 0;
126  image.tly_ = 0;
127  image.brx_ = (jas_image_coord_t)width;
128  image.bry_ = (jas_image_coord_t)height;
129  image.numcmpts_ = 1;
130  image.maxcmpts_ = 1;
131  image.clrspc_ = JAS_CLRSPC_SGRAY; /* grayscale Image */
132  image.cmprof_ = 0;
133 
134  cmpt.tlx_ = 0;
135  cmpt.tly_ = 0;
136  cmpt.hstep_ = 1;
137  cmpt.vstep_ = 1;
138  cmpt.width_ = (jas_image_coord_t)width;
139  cmpt.height_ = (jas_image_coord_t)height;
140  cmpt.type_ = JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
141  cmpt.prec_ = nbits;
142  cmpt.sgnd_ = 0;
143  cmpt.cps_ = (nbits + 7) / 8;
144 
145  pcmpt = &cmpt;
146  image.cmpts_ = &pcmpt;
147 
148  /* Initialize Jasper. */
149 #ifdef JASPER3
150  jas_conf_clear();
151  /* static jas_std_allocator_t allocator; */
152  /* jas_std_allocator_init(&allocator); */
153  /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
154  jas_conf_set_max_mem_usage(10000000);
155  jas_conf_set_multithread(true);
156  if (jas_init_library())
157  return G2_JASPER_INIT;
158  if (jas_init_thread())
159  return G2_JASPER_INIT;
160 #else
161  if (jas_init())
162  return G2_JASPER_INIT;
163 #endif /* JASPER3 */
164 
165  /* Open a JasPer stream containing the input grayscale values. */
166  istream = jas_stream_memopen((char *)cin, height * width * cmpt.cps_);
167  cmpt.stream_ = istream;
168 
169  /* Open an output stream that will contain the encoded jpeg2000
170  * code stream. */
171  jpcstream = jas_stream_memopen(outjpc, (int)jpclen);
172 
173  /* Get jasper ID of JPEG encoder. */
174  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
175 
176  /* Encode image. */
177  if ((ier = jas_image_encode(&image, jpcstream, fmt, opts)))
178  return G2_JASPER_ENCODE;
179 
180  /* Rememeber the length in bytes of the encoded JPEG code
181  * stream. */
182  rwcnt = jpcstream->rwcnt_;
183 
184  /* Clean up JasPer work structures. */
185  ier = jas_stream_close(istream);
186  ier = jas_stream_close(jpcstream);
187 
188  /* Finalize jasper. */
189 #ifdef JASPER3
190  jas_cleanup_thread();
191  jas_cleanup_library();
192 #else
193  jas_cleanup();
194 #endif /* JASPER3 */
195 
196  /* Return size of jpeg2000 code stream. */
197  return rwcnt;
198 }
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: enc_jpeg2000.c:51
#define MAXOPTSSIZE
Maximum size of options.
Definition: enc_jpeg2000.c:11
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: enc_jpeg2000.c:100
#define G2_JASPER_INIT
In enc_jpeg2000()/dec_jpeg2000() error initializing jasper library.
Definition: grib2.h:485
#define G2_JASPER_ENCODE
In enc_jpeg2000() error encoding image with jasper.
Definition: grib2.h:486
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