NCEPLIBS-g2c  1.7.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 enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits,
52  g2int ltype, g2int ratio, g2int retry, char *outjpc,
53  g2int jpclen)
54 {
55  int ier, rwcnt;
56  jas_image_t image;
57  jas_stream_t *jpcstream, *istream;
58  jas_image_cmpt_t cmpt, *pcmpt;
59  char opts[MAXOPTSSIZE];
60  int fmt;
61 
62  /* Set lossy compression options, if requested. */
63  if (ltype != 1)
64  opts[0] = (char)0;
65  else
66  snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)ratio);
67 
68  if (retry == 1) /* option to increase number of guard bits */
69  strcat(opts,"\nnumgbits=4");
70 
71  /* Initialize the JasPer image structure describing the grayscale
72  * image to encode into the JPEG2000 code stream. */
73  image.tlx_ = 0;
74  image.tly_ = 0;
75  image.brx_ = (jas_image_coord_t)width;
76  image.bry_ = (jas_image_coord_t)height;
77  image.numcmpts_ = 1;
78  image.maxcmpts_ = 1;
79  image.clrspc_ = JAS_CLRSPC_SGRAY; /* grayscale Image */
80  image.cmprof_ = 0;
81 
82  cmpt.tlx_ = 0;
83  cmpt.tly_ = 0;
84  cmpt.hstep_ = 1;
85  cmpt.vstep_ = 1;
86  cmpt.width_ = (jas_image_coord_t)width;
87  cmpt.height_ = (jas_image_coord_t)height;
88  cmpt.type_ = JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
89  cmpt.prec_ = nbits;
90  cmpt.sgnd_ = 0;
91  cmpt.cps_ = (nbits + 7) / 8;
92 
93  pcmpt = &cmpt;
94  image.cmpts_ = &pcmpt;
95 
96  /* Initialize Jasper. */
97 #ifdef JASPER3
98  jas_conf_clear();
99  /* static jas_std_allocator_t allocator; */
100  /* jas_std_allocator_init(&allocator); */
101  /* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
102  jas_conf_set_max_mem_usage(10000000);
103  jas_conf_set_multithread(true);
104  if (jas_init_library())
105  return G2_JASPER_INIT;
106  if (jas_init_thread())
107  return G2_JASPER_INIT;
108 #else
109  if (jas_init())
110  return G2_JASPER_INIT;
111 #endif /* JASPER3 */
112 
113  /* Open a JasPer stream containing the input grayscale values. */
114  istream = jas_stream_memopen((char *)cin, height * width * cmpt.cps_);
115  cmpt.stream_ = istream;
116 
117  /* Open an output stream that will contain the encoded jpeg2000
118  * code stream. */
119  jpcstream = jas_stream_memopen(outjpc, (int)jpclen);
120 
121  /* Get jasper ID of JPEG encoder. */
122  fmt = jas_image_strtofmt(G2C_JASPER_JPEG_FORMAT_NAME);
123 
124  /* Encode image. */
125  if ((ier = jas_image_encode(&image, jpcstream, fmt, opts)))
126  return G2_JASPER_ENCODE;
127 
128  /* Rememeber the length in bytes of the encoded JPEG code
129  * stream. */
130  rwcnt = jpcstream->rwcnt_;
131 
132  /* Clean up JasPer work structures. */
133  ier = jas_stream_close(istream);
134  ier = jas_stream_close(jpcstream);
135 
136  /* Finalize jasper. */
137 #ifdef JASPER3
138  jas_cleanup_thread();
139  jas_cleanup_library();
140 #else
141  jas_cleanup();
142 #endif /* JASPER3 */
143 
144  /* Return size of jpeg2000 code stream. */
145  return rwcnt;
146 }
MAXOPTSSIZE
#define MAXOPTSSIZE
Maximum size of options.
Definition: enc_jpeg2000.c:11
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_ENCODE
#define G2_JASPER_ENCODE
In enc_jpeg2000() error encoding image with jasper.
Definition: grib2.h:323
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
enc_jpeg2000
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:51