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