1 : #include "grib2.h"
2 : #ifndef USE_JPEG2000
3 0 : int enc_jpeg2000(unsigned char *cin,g2int width,g2int height,g2int nbits,
4 : g2int ltype, g2int ratio, g2int retry, char *outjpc,
5 0 : g2int jpclen){return 0;}
6 : #else /* USE_JPEG2000 */
7 :
8 : #include <stdio.h>
9 : #include <stdlib.h>
10 :
11 : #ifdef USE_JPEG2000_J2KSUBFILE
12 : // J2KSUBFILE includes .. TODO!!
13 : #else
14 : #include <jasper/jasper.h>
15 : #define JAS_1_700_2
16 : #endif /* USE_JPEG2000_J2KSUBFILE */
17 :
18 : int enc_jpeg2000(unsigned char *cin,g2int width,g2int height,g2int nbits,
19 : g2int ltype, g2int ratio, g2int retry, char *outjpc,
20 : g2int jpclen)
21 : /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
22 : * . . . .
23 : * SUBPROGRAM: enc_jpeg2000 Encodes JPEG2000 code stream
24 : * PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-12-02
25 : *
26 : * ABSTRACT: This Function encodes a grayscale image into a JPEG2000 code stream
27 : * specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1)
28 : * using JasPer Software version 1.500.4 (or 1.700.2 ) written by the
29 : * University of British Columbia, Image Power Inc, and others.
30 : * JasPer is available at http://www.ece.uvic.ca/~mdadams/jasper/.
31 : *
32 : * PROGRAM HISTORY LOG:
33 : * 2002-12-02 Gilbert
34 : * 2004-12-16 Gilbert - Added retry argument/option to allow option of
35 : * increasing the maximum number of guard bits to the
36 : * JPEG2000 algorithm.
37 : *
38 : * USAGE: int enc_jpeg2000(unsigned char *cin,g2int width,g2int height,
39 : * g2int nbits, g2int ltype, g2int ratio,
40 : * g2int retry, char *outjpc, g2int jpclen)
41 : *
42 : * INPUT ARGUMENTS:
43 : * cin - Packed matrix of Grayscale image values to encode.
44 : * width - width of image
45 : * height - height of image
46 : * nbits - depth (in bits) of image. i.e number of bits
47 : * used to hold each data value
48 : * ltype - indicator of lossless or lossy compression
49 : * = 1, for lossy compression
50 : * != 1, for lossless compression
51 : * ratio - target compression ratio. (ratio:1)
52 : * Used only when ltype == 1.
53 : * retry - Pointer to option type.
54 : * 1 = try increasing number of guard bits
55 : * otherwise, no additional options
56 : * jpclen - Number of bytes allocated for new JPEG2000 code stream in
57 : * outjpc.
58 : *
59 : * INPUT ARGUMENTS:
60 : * outjpc - Output encoded JPEG2000 code stream
61 : *
62 : * RETURN VALUES :
63 : * > 0 = Length in bytes of encoded JPEG2000 code stream
64 : * -3 = Error decode jpeg2000 code stream.
65 : * -5 = decoded image had multiple color components.
66 : * Only grayscale is expected.
67 : *
68 : * REMARKS:
69 : *
70 : * Requires JasPer Software version 1.500.4 or 1.700.2
71 : *
72 : * ATTRIBUTES:
73 : * LANGUAGE: C
74 : * MACHINE: IBM SP
75 : *
76 : *$$$*/
77 : {
78 :
79 : #ifdef USE_JPEG2000_J2KSUBFILE
80 :
81 : // J2KSUBFILE method ... TODO!!
82 : return 0;
83 :
84 : #else /* USE_JPEG2000_J2KSUBFILE */
85 :
86 : // JasPer method
87 :
88 : int ier,rwcnt;
89 : jas_image_t image;
90 : jas_stream_t *jpcstream,*istream;
91 : jas_image_cmpt_t cmpt,*pcmpt;
92 : #define MAXOPTSSIZE 1024
93 : char opts[MAXOPTSSIZE];
94 :
95 : /*
96 : printf(" enc_jpeg2000:width %ld\n",width);
97 : printf(" enc_jpeg2000:height %ld\n",height);
98 : printf(" enc_jpeg2000:nbits %ld\n",nbits);
99 : printf(" enc_jpeg2000:jpclen %ld\n",jpclen);
100 : */
101 : // jas_init();
102 :
103 : //
104 : // Set lossy compression options, if requested.
105 : //
106 : if ( ltype != 1 ) {
107 : opts[0]=(char)0;
108 : }
109 : else {
110 : sprintf(opts,"mode=real\nrate=%f",1.0/(float)ratio);
111 : }
112 : if ( retry == 1 ) { // option to increase number of guard bits
113 : strcat(opts,"\nnumgbits=4");
114 : }
115 : //printf("SAGopts: %s\n",opts);
116 :
117 : //
118 : // Initialize the JasPer image structure describing the grayscale
119 : // image to encode into the JPEG2000 code stream.
120 : //
121 : image.tlx_=0;
122 : image.tly_=0;
123 : #ifdef JAS_1_500_4
124 : image.brx_=(uint_fast32_t)width;
125 : image.bry_=(uint_fast32_t)height;
126 : #endif
127 : #ifdef JAS_1_700_2
128 : image.brx_=(jas_image_coord_t)width;
129 : image.bry_=(jas_image_coord_t)height;
130 : #endif
131 : image.numcmpts_=1;
132 : image.maxcmpts_=1;
133 : #ifdef JAS_1_500_4
134 : image.colormodel_=JAS_IMAGE_CM_GRAY; /* grayscale Image */
135 : #endif
136 : #ifdef JAS_1_700_2
137 : image.clrspc_=JAS_CLRSPC_SGRAY; /* grayscale Image */
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 : #endif /* USE_JPEG2000_J2KSUBFILE */
194 :
195 : }
196 :
197 : #endif /* USE_JPEG2000 */
|