NCEPLIBS-g2  3.4.5
enc_jpeg2000.c
Go to the documentation of this file.
1 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "jasper/jasper.h"
12 #define JAS_1_700_2
14 #ifdef __64BIT__
15  typedef int g2int;
16 #else
17  typedef long g2int;
18 #endif
19 
20 #if defined CRAY90
21  #include <fortran.h>
22  #define SUB_NAME ENC_JPEG2000
23 #elif defined LINUXF90
24  #define SUB_NAME ENC_JPEG2000
25 #elif defined LINUXG95
26  #define SUB_NAME enc_jpeg2000__
27 #elif defined HP || defined AIX
28  #define SUB_NAME enc_jpeg2000
29 #elif defined SGI || defined LINUX || defined VPP5000 || defined APPLE
30  #define SUB_NAME enc_jpeg2000_
31 #endif
32 
70 int SUB_NAME(unsigned char *cin,g2int *pwidth,g2int *pheight,g2int *pnbits,
71  g2int *ltype, g2int *ratio, g2int *retry, char *outjpc,
72  g2int *jpclen)
73 {
74  int ier,rwcnt;
75  jas_image_t image;
76  jas_stream_t *jpcstream,*istream;
77  jas_image_cmpt_t cmpt,*pcmpt;
82 #define MAXOPTSSIZE 1024
83  char opts[MAXOPTSSIZE];
84 
85  g2int width,height,nbits;
86  width=*pwidth;
87  height=*pheight;
88  nbits=*pnbits;
89 /*
90  printf(" enc_jpeg2000:width %ld\n",width);
91  printf(" enc_jpeg2000:height %ld\n",height);
92  printf(" enc_jpeg2000:nbits %ld\n",nbits);
93  printf(" enc_jpeg2000:jpclen %ld\n",*jpclen);
94 */
95 /* jas_init(); */
96 
97 /*
98 ** Set lossy compression options, if requested.
99 */
100  if ( *ltype != 1 ) {
101  opts[0]=(char)0;
102  }
103  else {
104  snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)*ratio);
105  }
106  if ( *retry == 1 ) { // option to increase number of guard bits
107  strcat(opts,"\nnumgbits=4");
108  }
109 /* printf("SAGopts: %s\n",opts); */
110 
111 /*
112 ** Initialize the JasPer image structure describing the grayscale
113 ** image to encode into the JPEG2000 code stream.
114 */
115  image.tlx_=0;
116  image.tly_=0;
117 #ifdef JAS_1_500_4
118  image.brx_=(uint_fast32_t)width;
119  image.bry_=(uint_fast32_t)height;
120 #endif
121 #ifdef JAS_1_700_2
122  image.brx_=(jas_image_coord_t)width;
123  image.bry_=(jas_image_coord_t)height;
124 #endif
125  image.numcmpts_=1;
126  image.maxcmpts_=1;
127 #ifdef JAS_1_500_4
128 /*
129 ** grayscale Image
130 */
131  image.colormodel_=JAS_IMAGE_CM_GRAY;
132 #endif
133 #ifdef JAS_1_700_2
134 /*
135 ** grayscale Image
136 */
137  image.clrspc_=JAS_CLRSPC_SGRAY;
138  image.cmprof_=0;
139 #endif
140 /* image.inmem_=1; */
141 
142  cmpt.tlx_=0;
143  cmpt.tly_=0;
144  cmpt.hstep_=1;
145  cmpt.vstep_=1;
146 #ifdef JAS_1_500_4
147  cmpt.width_=(uint_fast32_t)width;
148  cmpt.height_=(uint_fast32_t)height;
149 #endif
150 #ifdef JAS_1_700_2
151  cmpt.width_=(jas_image_coord_t)width;
152  cmpt.height_=(jas_image_coord_t)height;
153  cmpt.type_=JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
154 #endif
155  cmpt.prec_=nbits;
156  cmpt.sgnd_=0;
157  cmpt.cps_=(nbits+7)/8;
158 
159  pcmpt=&cmpt;
160  image.cmpts_=&pcmpt;
161 
162 /*
163 ** Open a JasPer stream containing the input grayscale values
164 */
165  istream=jas_stream_memopen((char *)cin,height*width*cmpt.cps_);
166  cmpt.stream_=istream;
167 
168 /*
169 ** Open an output stream that will contain the encoded jpeg2000
170 ** code stream.
171 */
172  jpcstream=jas_stream_memopen(outjpc,(int)(*jpclen));
173 
174 /*
175 ** Encode image.
176 */
177  ier=jpc_encode(&image,jpcstream,opts);
178  if ( ier != 0 ) {
179  printf(" jpc_encode return = %d \n",ier);
180  return -3;
181  }
182 /*
183 ** Clean up JasPer work structures.
184 */
185  rwcnt=jpcstream->rwcnt_;
186  ier=jas_stream_close(istream);
187  ier=jas_stream_close(jpcstream);
188 /*
189 ** Return size of jpeg2000 code stream
190 */
191  return (rwcnt);
192 
193 }
194 
SUB_NAME
int SUB_NAME(char *injpc, g2int *bufsize, g2int *outfld)
This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard.
Definition: dec_jpeg2000.c:52
MAXOPTSSIZE
#define MAXOPTSSIZE
This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 ...
g2int
long g2int
Long Integer type.
Definition: enc_jpeg2000.c:17
g2int
long g2int
Long Integer type.
Definition: dec_jpeg2000.c:18