1 #include "grib2.h"
2
3 #include "cpl_port.h"
4
5 #ifndef USE_JPEG2000
enc_jpeg2000(CPL_UNUSED unsigned char * cin,CPL_UNUSED g2int width,CPL_UNUSED g2int height,CPL_UNUSED g2int nbits,CPL_UNUSED g2int ltype,CPL_UNUSED g2int ratio,CPL_UNUSED g2int retry,CPL_UNUSED char * outjpc,CPL_UNUSED g2int jpclen)6 int enc_jpeg2000(CPL_UNUSED unsigned char *cin,
7 CPL_UNUSED g2int width,
8 CPL_UNUSED g2int height,
9 CPL_UNUSED g2int nbits,
10 CPL_UNUSED g2int ltype,
11 CPL_UNUSED g2int ratio,
12 CPL_UNUSED g2int retry,
13 CPL_UNUSED char *outjpc,
14 CPL_UNUSED g2int jpclen) { return 0; }
15
16 #else /* USE_JPEG2000 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #ifdef USE_JPEG2000_J2KSUBFILE
22 // J2KSUBFILE includes .. TODO!!
23 #else
24 #include <jasper/jasper.h>
25 #define JAS_1_700_2
26 #endif /* USE_JPEG2000_J2KSUBFILE */
27
enc_jpeg2000(unsigned char * cin,g2int width,g2int height,g2int nbits,g2int ltype,g2int ratio,g2int retry,char * outjpc,g2int jpclen)28 int enc_jpeg2000(unsigned char *cin,g2int width,g2int height,g2int nbits,
29 g2int ltype, g2int ratio, g2int retry, char *outjpc,
30 g2int jpclen)
31 /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
32 * . . . .
33 * SUBPROGRAM: enc_jpeg2000 Encodes JPEG2000 code stream
34 * PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-12-02
35 *
36 * ABSTRACT: This Function encodes a grayscale image into a JPEG2000 code stream
37 * specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1)
38 * using JasPer Software version 1.500.4 (or 1.700.2 ) written by the
39 * University of British Columbia, Image Power Inc, and others.
40 * JasPer is available at http://www.ece.uvic.ca/~mdadams/jasper/.
41 *
42 * PROGRAM HISTORY LOG:
43 * 2002-12-02 Gilbert
44 * 2004-12-16 Gilbert - Added retry argument/option to allow option of
45 * increasing the maximum number of guard bits to the
46 * JPEG2000 algorithm.
47 *
48 * USAGE: int enc_jpeg2000(unsigned char *cin,g2int width,g2int height,
49 * g2int nbits, g2int ltype, g2int ratio,
50 * g2int retry, char *outjpc, g2int jpclen)
51 *
52 * INPUT ARGUMENTS:
53 * cin - Packed matrix of Grayscale image values to encode.
54 * width - width of image
55 * height - height of image
56 * nbits - depth (in bits) of image. i.e number of bits
57 * used to hold each data value
58 * ltype - indicator of lossless or lossy compression
59 * = 1, for lossy compression
60 * != 1, for lossless compression
61 * ratio - target compression ratio. (ratio:1)
62 * Used only when ltype == 1.
63 * retry - Pointer to option type.
64 * 1 = try increasing number of guard bits
65 * otherwise, no additional options
66 * jpclen - Number of bytes allocated for new JPEG2000 code stream in
67 * outjpc.
68 *
69 * INPUT ARGUMENTS:
70 * outjpc - Output encoded JPEG2000 code stream
71 *
72 * RETURN VALUES :
73 * > 0 = Length in bytes of encoded JPEG2000 code stream
74 * -3 = Error decode jpeg2000 code stream.
75 * -5 = decoded image had multiple color components.
76 * Only grayscale is expected.
77 *
78 * REMARKS:
79 *
80 * Requires JasPer Software version 1.500.4 or 1.700.2
81 *
82 * ATTRIBUTES:
83 * LANGUAGE: C
84 * MACHINE: IBM SP
85 *
86 *$$$*/
87 {
88
89 #ifdef USE_JPEG2000_J2KSUBFILE
90
91 // J2KSUBFILE method ... TODO!!
92 return 0;
93
94 #else /* USE_JPEG2000_J2KSUBFILE */
95
96 // JasPer method
97
98 int ier,rwcnt;
99 jas_image_t image;
100 jas_stream_t *jpcstream,*istream;
101 jas_image_cmpt_t cmpt,*pcmpt;
102 #define MAXOPTSSIZE 1024
103 char opts[MAXOPTSSIZE];
104
105 /*
106 printf(" enc_jpeg2000:width %ld\n",width);
107 printf(" enc_jpeg2000:height %ld\n",height);
108 printf(" enc_jpeg2000:nbits %ld\n",nbits);
109 printf(" enc_jpeg2000:jpclen %ld\n",jpclen);
110 */
111 // jas_init();
112
113 //
114 // Set lossy compression options, if requested.
115 //
116 if ( ltype != 1 ) {
117 opts[0]=(char)0;
118 }
119 else {
120 snprintf(opts,MAXOPTSSIZE,"mode=real\nrate=%f",1.0/(float)ratio);
121 }
122 if ( retry == 1 ) { // option to increase number of guard bits
123 strcat(opts,"\nnumgbits=4");
124 }
125 //printf("SAGopts: %s\n",opts);
126
127 //
128 // Initialize the JasPer image structure describing the grayscale
129 // image to encode into the JPEG2000 code stream.
130 //
131 image.tlx_=0;
132 image.tly_=0;
133 #ifdef JAS_1_500_4
134 image.brx_=(uint_fast32_t)width;
135 image.bry_=(uint_fast32_t)height;
136 #endif
137 #ifdef JAS_1_700_2
138 image.brx_=(jas_image_coord_t)width;
139 image.bry_=(jas_image_coord_t)height;
140 #endif
141 image.numcmpts_=1;
142 image.maxcmpts_=1;
143 #ifdef JAS_1_500_4
144 image.colormodel_=JAS_IMAGE_CM_GRAY; /* grayscale Image */
145 #endif
146 #ifdef JAS_1_700_2
147 image.clrspc_=JAS_CLRSPC_SGRAY; /* grayscale Image */
148 image.cmprof_=0;
149 #endif
150 image.inmem_=1;
151
152 cmpt.tlx_=0;
153 cmpt.tly_=0;
154 cmpt.hstep_=1;
155 cmpt.vstep_=1;
156 #ifdef JAS_1_500_4
157 cmpt.width_=(uint_fast32_t)width;
158 cmpt.height_=(uint_fast32_t)height;
159 #endif
160 #ifdef JAS_1_700_2
161 cmpt.width_=(jas_image_coord_t)width;
162 cmpt.height_=(jas_image_coord_t)height;
163 cmpt.type_=JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y);
164 #endif
165 cmpt.prec_=nbits;
166 cmpt.sgnd_=0;
167 cmpt.cps_=(nbits+7)/8;
168
169 pcmpt=&cmpt;
170 image.cmpts_=&pcmpt;
171
172 //
173 // Open a JasPer stream containing the input grayscale values
174 //
175 istream=jas_stream_memopen((char *)cin,height*width*cmpt.cps_);
176 cmpt.stream_=istream;
177
178 //
179 // Open an output stream that will contain the encoded jpeg2000
180 // code stream.
181 //
182 jpcstream=jas_stream_memopen(outjpc,(int)jpclen);
183
184 //
185 // Encode image.
186 //
187 ier=jpc_encode(&image,jpcstream,opts);
188 if ( ier != 0 ) {
189 printf(" jpc_encode return = %d \n",ier);
190 return -3;
191 }
192 //
193 // Clean up JasPer work structures.
194 //
195 rwcnt=jpcstream->rwcnt_;
196 ier=jas_stream_close(istream);
197 ier=jas_stream_close(jpcstream);
198 //
199 // Return size of jpeg2000 code stream
200 //
201 return (rwcnt);
202
203 #endif /* USE_JPEG2000_J2KSUBFILE */
204
205 }
206
207 #endif /* USE_JPEG2000 */
208