NCEPLIBS-g2c  1.6.4
enc_jpeg2000.c
Go to the documentation of this file.
1 
6 #ifndef USE_JPEG2000
7 void dummy(void) {}
8 #else /* USE_JPEG2000 */
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "grib2.h"
13 #include "jasper/jasper.h"
14 #define JAS_1_700_2
15 #define MAXOPTSSIZE 1024
56 int
57 enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits,
58  g2int ltype, g2int ratio, g2int retry, char *outjpc,
59  g2int jpclen)
60 {
61  int ier, rwcnt;
62  jas_image_t image;
63  jas_stream_t *jpcstream, *istream;
64  jas_image_cmpt_t cmpt, *pcmpt;
65  char opts[MAXOPTSSIZE];
66 
67  /* Set lossy compression options, if requested. */
68  if (ltype != 1)
69  opts[0] = (char)0;
70  else
71  snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)ratio);
72 
73  if (retry == 1) /* option to increase number of guard bits */
74  strcat(opts,"\nnumgbits=4");
75 
76  /* Initialize the JasPer image structure describing the grayscale
77  * image to encode into the JPEG2000 code stream. */
78  image.tlx_ = 0;
79  image.tly_ = 0;
80 #ifdef JAS_1_500_4
81  image.brx_ = (uint_fast32_t)width;
82  image.bry_ = (uint_fast32_t)height;
83 #endif
84 #ifdef JAS_1_700_2
85  image.brx_ = (jas_image_coord_t)width;
86  image.bry_ = (jas_image_coord_t)height;
87 #endif
88  image.numcmpts_ = 1;
89  image.maxcmpts_ = 1;
90 #ifdef JAS_1_500_4
91  image.colormodel_ = JAS_IMAGE_CM_GRAY; /* grayscale Image */
92 #endif
93 #ifdef JAS_1_700_2
94  image.clrspc_ = JAS_CLRSPC_SGRAY; /* grayscale Image */
95  image.cmprof_ = 0;
96 #endif
97 
98  cmpt.tlx_ = 0;
99  cmpt.tly_ = 0;
100  cmpt.hstep_ = 1;
101  cmpt.vstep_ = 1;
102 #ifdef JAS_1_500_4
103  cmpt.width_ = (uint_fast32_t)width;
104  cmpt.height_ = (uint_fast32_t)height;
105 #endif
106 #ifdef JAS_1_700_2
107  cmpt.width_ = (jas_image_coord_t)width;
108  cmpt.height_ = (jas_image_coord_t)height;
109  cmpt.type_ = JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
110 #endif
111  cmpt.prec_ = nbits;
112  cmpt.sgnd_ = 0;
113  cmpt.cps_ = (nbits + 7) / 8;
114 
115  pcmpt = &cmpt;
116  image.cmpts_ = &pcmpt;
117 
118  /* Open a JasPer stream containing the input grayscale values. */
119  istream = jas_stream_memopen((char *)cin, height * width * cmpt.cps_);
120  cmpt.stream_ = istream;
121 
122  /* Open an output stream that will contain the encoded jpeg2000
123  * code stream. */
124  jpcstream = jas_stream_memopen(outjpc, (int)jpclen);
125 
126  /* Encode image. */
127  if ((ier = jpc_encode(&image, jpcstream, opts)))
128  {
129  printf(" jpc_encode return = %d \n",ier);
130  return -3;
131  }
132 
133  /* Clean up JasPer work structures. */
134  rwcnt = jpcstream->rwcnt_;
135  ier = jas_stream_close(istream);
136  ier = jas_stream_close(jpcstream);
137 
138  /* Return size of jpeg2000 code stream. */
139  return (rwcnt);
140 }
141 
142 #endif /* USE_JPEG2000 */
#define MAXOPTSSIZE
Maximum size of options.
Definition: enc_jpeg2000.c:15
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:57
Header file for NCEPLIBS-g2c library.
int64_t g2int
Long integer type.
Definition: grib2.h:20