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