1 : #include <cpl_port.h>
2 :
3 : #include "grib2.h"
4 :
5 : #include <stdio.h>
6 : #include <stdlib.h>
7 : #include <string.h>
8 :
9 : /* -------------------------------------------------------------------- */
10 : /* ==================================================================== */
11 : /* We prefer to use JasPer directly if it is available. If not */
12 : /* we fallback on calling back to GDAL to try and process the */
13 : /* jpeg2000 chunks. */
14 : /* ==================================================================== */
15 : /* -------------------------------------------------------------------- */
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 : // Open memory buffer for reading
89 : GDALDataset* poJ2KDataset = (GDALDataset *)
90 : GDALOpen( osFileName, GA_ReadOnly );
91 :
92 : if( poJ2KDataset == NULL )
93 : {
94 : printf("dec_jpeg2000: Unable to open JPEG2000 image within GRIB file.\n"
95 : "Is the JPEG2000 driver available?" );
96 : return -3;
97 : }
98 :
99 : if( poJ2KDataset->GetRasterCount() != 1 )
100 : {
101 : printf("dec_jpeg2000: Found color image. Grayscale expected.\n");
102 : return (-5);
103 : }
104 :
105 : // Fulfill administration: initialize parameters required for RasterIO
106 : int nXSize = poJ2KDataset->GetRasterXSize();
107 : int nYSize = poJ2KDataset->GetRasterYSize();
108 : int nXOff = 0;
109 : int nYOff = 0;
110 : int nBufXSize = nXSize;
111 : int nBufYSize = nYSize;
112 : GDALDataType eBufType = GDT_Int32; // map to type of "outfld" buffer: g2int*
113 : int nBandCount = 1;
114 : int* panBandMap = NULL;
115 : int nPixelSpace = 0;
116 : int nLineSpace = 0;
117 : int nBandSpace = 0;
118 :
119 : // Decompress the JPEG2000 into the output integer array.
120 : poJ2KDataset->RasterIO( GF_Read, nXOff, nYOff, nXSize, nYSize,
121 : outfld, nBufXSize, nBufYSize, eBufType,
122 : nBandCount, panBandMap,
123 : nPixelSpace, nLineSpace, nBandSpace );
124 :
125 : // close source file, and "unlink" it.
126 : GDALClose( poJ2KDataset );
127 : VSIUnlink( osFileName );
128 :
129 : return 0;
130 :
131 : #else
132 :
133 : // JasPer method
134 :
135 : int ier;
136 : g2int i,j,k;
137 0 : jas_image_t *image=0;
138 : jas_stream_t *jpcstream;
139 : jas_image_cmpt_t *pcmpt;
140 0 : char *opts=0;
141 : jas_matrix_t *data;
142 :
143 : // jas_init();
144 :
145 0 : ier=0;
146 : //
147 : // Create jas_stream_t containing input JPEG200 codestream in memory.
148 : //
149 :
150 0 : jpcstream=jas_stream_memopen(injpc,bufsize);
151 :
152 : //
153 : // Decode JPEG200 codestream into jas_image_t structure.
154 : //
155 :
156 0 : image=jpc_decode(jpcstream,opts);
157 0 : if ( image == 0 ) {
158 0 : printf(" jpc_decode return = %d \n",ier);
159 0 : return -3;
160 : }
161 :
162 0 : pcmpt=image->cmpts_[0];
163 :
164 : // Expecting jpeg2000 image to be grayscale only.
165 : // No color components.
166 : //
167 0 : if (image->numcmpts_ != 1 ) {
168 0 : printf("dec_jpeg2000: Found color image. Grayscale expected.\n");
169 0 : return (-5);
170 : }
171 :
172 : //
173 : // Create a data matrix of grayscale image values decoded from
174 : // the jpeg2000 codestream.
175 : //
176 0 : data=jas_matrix_create(jas_image_height(image), jas_image_width(image));
177 : jas_image_readcmpt(image,0,0,0,jas_image_width(image),
178 0 : jas_image_height(image),data);
179 : //
180 : // Copy data matrix to output integer array.
181 : //
182 0 : k=0;
183 0 : for (i=0;i<pcmpt->height_;i++)
184 0 : for (j=0;j<pcmpt->width_;j++)
185 0 : outfld[k++]=data->rows_[i][j];
186 : //
187 : // Clean up JasPer work structures.
188 : //
189 0 : jas_matrix_destroy(data);
190 0 : ier=jas_stream_close(jpcstream);
191 0 : jas_image_destroy(image);
192 :
193 0 : return 0;
194 : #endif
195 : }
|