1 : #include "grib2.h"
2 :
3 : #include <stdio.h>
4 : #include <stdlib.h>
5 : #include <string.h>
6 :
7 : /* -------------------------------------------------------------------- */
8 : /* ==================================================================== */
9 : /* We prefer to use JasPer directly if it is available. If not */
10 : /* we fallback on calling back to GDAL to try and process the */
11 : /* jpeg2000 chunks. */
12 : /* ==================================================================== */
13 : /* -------------------------------------------------------------------- */
14 :
15 : #include <cpl_port.h>
16 :
17 : #ifdef HAVE_JASPER
18 : #include <jasper/jasper.h>
19 : #define JAS_1_700_2
20 : #else
21 : #include <gdal_pam.h>
22 : #include <cpl_conv.h>
23 : #endif
24 :
25 : CPL_C_START
26 : // Cripes ... shouldn't this go in an include files!
27 : int dec_jpeg2000(char *injpc,g2int bufsize,g2int *outfld);
28 : CPL_C_END
29 :
30 0 : int dec_jpeg2000(char *injpc,g2int bufsize,g2int *outfld)
31 : /*$$$ SUBPROGRAM DOCUMENTATION BLOCK
32 : * . . . .
33 : * SUBPROGRAM: dec_jpeg2000 Decodes JPEG2000 code stream
34 : * PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-12-02
35 : *
36 : * ABSTRACT: This Function decodes a JPEG2000 code stream specified in the
37 : * JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using JasPer
38 : * Software version 1.500.4 (or 1.700.2) written by the University of British
39 : * Columbia and 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 : *
45 : * USAGE: int dec_jpeg2000(char *injpc,g2int bufsize,g2int *outfld)
46 : *
47 : * INPUT ARGUMENTS:
48 : * injpc - Input JPEG2000 code stream.
49 : * bufsize - Length (in bytes) of the input JPEG2000 code stream.
50 : *
51 : * OUTPUT ARGUMENTS:
52 : * outfld - Output matrix of grayscale image values.
53 : *
54 : * RETURN VALUES :
55 : * 0 = Successful decode
56 : * -3 = Error decode jpeg2000 code stream.
57 : * -5 = decoded image had multiple color components.
58 : * Only grayscale is expected.
59 : *
60 : * REMARKS:
61 : *
62 : * Requires JasPer Software version 1.500.4 or 1.700.2
63 : *
64 : * ATTRIBUTES:
65 : * LANGUAGE: C
66 : * MACHINE: IBM SP
67 : *
68 : *$$$*/
69 :
70 : {
71 : #ifndef HAVE_JASPER
72 : // J2K_SUBFILE method
73 :
74 : // create "memory file" from buffer
75 : int fileNumber = 0;
76 : VSIStatBufL sStatBuf;
77 : CPLString osFileName = "/vsimem/work.jpc";
78 :
79 : // ensure we don't overwrite an existing file accidentally
80 : while ( VSIStatL( osFileName, &sStatBuf ) == 0 ) {
81 : osFileName.Printf( "/vsimem/work%d.jpc", ++fileNumber );
82 : }
83 :
84 : VSIFCloseL( VSIFileFromMemBuffer(
85 : osFileName, (unsigned char*)injpc, bufsize,
86 : FALSE ) ); // TRUE to let vsi delete the buffer when done
87 :
88 : /* -------------------------------------------------------------------- */
89 : /* Currently the JP2ECW driver doesn't support /vsimem and */
90 : /* other virtual files *unless* we force processing to go */
91 : /* through J2K_SUBFILE. Grr. It would be ideal to fix this in */
92 : /* the ECW driver eventually. */
93 : /* -------------------------------------------------------------------- */
94 : CPLString osSubfileName;
95 :
96 : osSubfileName.Printf( "J2K_SUBFILE:%d,%d,%s",
97 : 0, bufsize, osFileName.c_str() );
98 :
99 : // Open memory buffer for reading
100 : GDALDataset* poJ2KDataset = (GDALDataset *)
101 : GDALOpen( osSubfileName, GA_ReadOnly );
102 :
103 : if( poJ2KDataset == NULL )
104 : {
105 : printf("dec_jpeg2000: Unable to open JPEG2000 image within GRIB file.\n"
106 : "Is the JPEG2000 driver available?" );
107 : return -3;
108 : }
109 :
110 : if( poJ2KDataset->GetRasterCount() != 1 )
111 : {
112 : printf("dec_jpeg2000: Found color image. Grayscale expected.\n");
113 : return (-5);
114 : }
115 :
116 : // Fulfill administration: initialize parameters required for RasterIO
117 : int nXSize = poJ2KDataset->GetRasterXSize();
118 : int nYSize = poJ2KDataset->GetRasterYSize();
119 : int nXOff = 0;
120 : int nYOff = 0;
121 : int nBufXSize = nXSize;
122 : int nBufYSize = nYSize;
123 : GDALDataType eBufType = GDT_Int32; // map to type of "outfld" buffer: g2int*
124 : int nBandCount = 1;
125 : int* panBandMap = NULL;
126 : int nPixelSpace = 0;
127 : int nLineSpace = 0;
128 : int nBandSpace = 0;
129 :
130 : // Decompress the JPEG2000 into the output integer array.
131 : poJ2KDataset->RasterIO( GF_Read, nXOff, nYOff, nXSize, nYSize,
132 : outfld, nBufXSize, nBufYSize, eBufType,
133 : nBandCount, panBandMap,
134 : nPixelSpace, nLineSpace, nBandSpace );
135 :
136 : // close source file, and "unlink" it.
137 : GDALClose( poJ2KDataset );
138 : VSIUnlink( osFileName );
139 :
140 : return 0;
141 :
142 : #else
143 :
144 : // JasPer method
145 :
146 : int ier;
147 : g2int i,j,k;
148 0 : jas_image_t *image=0;
149 : jas_stream_t *jpcstream;
150 : jas_image_cmpt_t *pcmpt;
151 0 : char *opts=0;
152 : jas_matrix_t *data;
153 :
154 : // jas_init();
155 :
156 0 : ier=0;
157 : //
158 : // Create jas_stream_t containing input JPEG200 codestream in memory.
159 : //
160 :
161 0 : jpcstream=jas_stream_memopen(injpc,bufsize);
162 :
163 : //
164 : // Decode JPEG200 codestream into jas_image_t structure.
165 : //
166 :
167 0 : image=jpc_decode(jpcstream,opts);
168 0 : if ( image == 0 ) {
169 0 : printf(" jpc_decode return = %d \n",ier);
170 0 : return -3;
171 : }
172 :
173 0 : pcmpt=image->cmpts_[0];
174 :
175 : // Expecting jpeg2000 image to be grayscale only.
176 : // No color components.
177 : //
178 0 : if (image->numcmpts_ != 1 ) {
179 0 : printf("dec_jpeg2000: Found color image. Grayscale expected.\n");
180 0 : return (-5);
181 : }
182 :
183 : //
184 : // Create a data matrix of grayscale image values decoded from
185 : // the jpeg2000 codestream.
186 : //
187 0 : data=jas_matrix_create(jas_image_height(image), jas_image_width(image));
188 : jas_image_readcmpt(image,0,0,0,jas_image_width(image),
189 0 : jas_image_height(image),data);
190 : //
191 : // Copy data matrix to output integer array.
192 : //
193 0 : k=0;
194 0 : for (i=0;i<pcmpt->height_;i++)
195 0 : for (j=0;j<pcmpt->width_;j++)
196 0 : outfld[k++]=data->rows_[i][j];
197 : //
198 : // Clean up JasPer work structures.
199 : //
200 0 : jas_matrix_destroy(data);
201 0 : ier=jas_stream_close(jpcstream);
202 0 : jas_image_destroy(image);
203 :
204 0 : return 0;
205 : #endif
206 : }
|