1 : /******************************************************************************
2 : * $Id: ceosopen.c 18570 2010-01-17 13:13:07Z rouault $
3 : *
4 : * Project: CEOS Translator
5 : * Purpose: Implementation of non-GDAL dependent CEOS support.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "ceosopen.h"
31 :
32 : CPL_CVSID("$Id: ceosopen.c 18570 2010-01-17 13:13:07Z rouault $");
33 :
34 : /************************************************************************/
35 : /* CEOSScanInt() */
36 : /* */
37 : /* Read up to nMaxChars from the passed string, and interpret */
38 : /* as an integer. */
39 : /************************************************************************/
40 :
41 : static long CEOSScanInt( const char * pszString, int nMaxChars )
42 :
43 9 : {
44 : char szWorking[33];
45 : int i;
46 :
47 9 : if( nMaxChars > 32 || nMaxChars == 0 )
48 0 : nMaxChars = 32;
49 :
50 57 : for( i = 0; i < nMaxChars && pszString[i] != '\0'; i++ )
51 48 : szWorking[i] = pszString[i];
52 :
53 9 : szWorking[i] = '\0';
54 :
55 9 : return( atoi(szWorking) );
56 : }
57 :
58 : /************************************************************************/
59 : /* CEOSReadRecord() */
60 : /* */
61 : /* Read a single CEOS record at the current point in the file. */
62 : /* Return NULL after reporting an error if it fails, otherwise */
63 : /* return the record. */
64 : /************************************************************************/
65 :
66 : CEOSRecord * CEOSReadRecord( CEOSImage *psImage )
67 :
68 1 : {
69 : GByte abyHeader[12];
70 : CEOSRecord *psRecord;
71 :
72 : /* -------------------------------------------------------------------- */
73 : /* Read the standard CEOS header. */
74 : /* -------------------------------------------------------------------- */
75 1 : if( VSIFEof( psImage->fpImage ) )
76 0 : return NULL;
77 :
78 1 : if( VSIFRead( abyHeader, 1, 12, psImage->fpImage ) != 12 )
79 : {
80 0 : CPLError( CE_Failure, CPLE_FileIO,
81 : "Ran out of data reading CEOS record." );
82 0 : return NULL;
83 : }
84 :
85 : /* -------------------------------------------------------------------- */
86 : /* Extract this information. */
87 : /* -------------------------------------------------------------------- */
88 1 : psRecord = (CEOSRecord *) CPLMalloc(sizeof(CEOSRecord));
89 1 : if( psImage->bLittleEndian )
90 : {
91 1 : CPL_SWAP32PTR( abyHeader + 0 );
92 1 : CPL_SWAP32PTR( abyHeader + 8 );
93 : }
94 :
95 1 : psRecord->nRecordNum = abyHeader[0] * 256 * 256 * 256
96 : + abyHeader[1] * 256 * 256
97 : + abyHeader[2] * 256
98 : + abyHeader[3];
99 :
100 1 : psRecord->nRecordType = abyHeader[4] * 256 * 256 * 256
101 : + abyHeader[5] * 256 * 256
102 : + abyHeader[6] * 256
103 : + abyHeader[7];
104 :
105 1 : psRecord->nLength = abyHeader[8] * 256 * 256 * 256
106 : + abyHeader[9] * 256 * 256
107 : + abyHeader[10] * 256
108 : + abyHeader[11];
109 :
110 : /* -------------------------------------------------------------------- */
111 : /* Does it look reasonable? We assume there can't be too many */
112 : /* records and that the length must be between 12 and 200000. */
113 : /* -------------------------------------------------------------------- */
114 1 : if( psRecord->nRecordNum < 0 || psRecord->nRecordNum > 200000
115 : || psRecord->nLength < 12 || psRecord->nLength > 200000 )
116 : {
117 0 : CPLError( CE_Failure, CPLE_AppDefined,
118 : "CEOS record leader appears to be corrupt.\n"
119 : "Record Number = %d, Record Length = %d\n",
120 : psRecord->nRecordNum, psRecord->nLength );
121 0 : CPLFree( psRecord );
122 0 : return NULL;
123 : }
124 :
125 : /* -------------------------------------------------------------------- */
126 : /* Read the remainder of the record into a buffer. Ensure that */
127 : /* the first 12 bytes gets moved into this buffer as well. */
128 : /* -------------------------------------------------------------------- */
129 1 : psRecord->pachData = (char *) VSIMalloc(psRecord->nLength );
130 1 : if( psRecord->pachData == NULL )
131 : {
132 0 : CPLError( CE_Failure, CPLE_OutOfMemory,
133 : "Out of memory allocated %d bytes for CEOS record data.\n"
134 : "Are you sure you aren't leaking CEOSRecords?\n",
135 : psRecord->nLength );
136 0 : CPLFree( psRecord );
137 0 : return NULL;
138 : }
139 :
140 1 : memcpy( psRecord->pachData, abyHeader, 12 );
141 :
142 1 : if( (int)VSIFRead( psRecord->pachData + 12, 1, psRecord->nLength-12,
143 : psImage->fpImage )
144 : != psRecord->nLength - 12 )
145 : {
146 0 : CPLError( CE_Failure, CPLE_FileIO,
147 : "Short read on CEOS record data.\n" );
148 0 : CPLFree( psRecord );
149 0 : return NULL;
150 : }
151 :
152 1 : return psRecord;
153 : }
154 :
155 : /************************************************************************/
156 : /* CEOSDestroyRecord() */
157 : /* */
158 : /* Free a record. */
159 : /************************************************************************/
160 :
161 : void CEOSDestroyRecord( CEOSRecord * psRecord )
162 :
163 1 : {
164 1 : CPLFree( psRecord->pachData );
165 1 : CPLFree( psRecord );
166 1 : }
167 :
168 : /************************************************************************/
169 : /* CEOSOpen() */
170 : /************************************************************************/
171 :
172 : /**
173 : * Open a CEOS transfer.
174 : *
175 : * @param Filename The name of the CEOS imagery file (ie. imag_01.dat).
176 : * @param Access An fopen() style access string. Should be either "rb" for
177 : * read-only access, or "r+b" for read, and update access.
178 : *
179 : * @return A CEOSImage pointer as a handle to the image. The CEOSImage also
180 : * has various information about the image available. A NULL is returned
181 : * if an error occurs.
182 : */
183 :
184 : CEOSImage * CEOSOpen( const char * pszFilename, const char * pszAccess )
185 :
186 1 : {
187 : FILE *fp;
188 : CEOSRecord *psRecord;
189 : CEOSImage *psImage;
190 : int nSeqNum, i;
191 : GByte abyHeader[16];
192 :
193 : /* -------------------------------------------------------------------- */
194 : /* Try to open the imagery file. */
195 : /* -------------------------------------------------------------------- */
196 1 : fp = VSIFOpen( pszFilename, pszAccess );
197 :
198 1 : if( fp == NULL )
199 : {
200 0 : CPLError( CE_Failure, CPLE_OpenFailed,
201 : "Failed to open CEOS file `%s' with access `%s'.\n",
202 : pszFilename, pszAccess );
203 0 : return NULL;
204 : }
205 :
206 : /* -------------------------------------------------------------------- */
207 : /* Create a CEOSImage structure, and initialize it. */
208 : /* -------------------------------------------------------------------- */
209 1 : psImage = (CEOSImage *) CPLCalloc(1,sizeof(CEOSImage));
210 1 : psImage->fpImage = fp;
211 :
212 1 : psImage->nPixels = psImage->nLines = psImage->nBands = 0;
213 :
214 : /* -------------------------------------------------------------------- */
215 : /* Preread info on the first record, to establish if it is */
216 : /* little endian. */
217 : /* -------------------------------------------------------------------- */
218 1 : VSIFRead( abyHeader, 16, 1, fp );
219 1 : VSIFSeek( fp, 0, SEEK_SET );
220 :
221 1 : if( abyHeader[0] != 0 || abyHeader[1] != 0 )
222 1 : psImage->bLittleEndian = TRUE;
223 :
224 : /* -------------------------------------------------------------------- */
225 : /* Try to read the header record. */
226 : /* -------------------------------------------------------------------- */
227 1 : psRecord = CEOSReadRecord( psImage );
228 1 : if( psRecord == NULL )
229 : {
230 0 : CEOSClose( psImage );
231 0 : return NULL;
232 : }
233 :
234 1 : if( psRecord->nRecordType != CRT_IMAGE_FDR )
235 : {
236 0 : CPLError( CE_Failure, CPLE_AppDefined,
237 : "Got a %X type record, instead of the expected\n"
238 : "file descriptor record on file %s.\n",
239 : psRecord->nRecordType, pszFilename );
240 :
241 0 : CEOSDestroyRecord( psRecord );
242 0 : CEOSClose( psImage );
243 0 : return NULL;
244 : }
245 :
246 : /* -------------------------------------------------------------------- */
247 : /* The sequence number should be 2 indicating this is the */
248 : /* imagery file. */
249 : /* -------------------------------------------------------------------- */
250 1 : nSeqNum = CEOSScanInt( psRecord->pachData + 44, 4 );
251 1 : if( nSeqNum != 2 )
252 : {
253 0 : CPLError( CE_Warning, CPLE_AppDefined,
254 : "Got a %d file sequence number, instead of the expected\n"
255 : "2 indicating imagery on file %s.\n"
256 : "Continuing to access anyways.\n",
257 : nSeqNum, pszFilename );
258 : }
259 :
260 : /* -------------------------------------------------------------------- */
261 : /* Extract various information. */
262 : /* -------------------------------------------------------------------- */
263 1 : psImage->nImageRecCount = CEOSScanInt( psRecord->pachData+180, 6 );
264 1 : psImage->nImageRecLength = CEOSScanInt( psRecord->pachData+186, 6 );
265 1 : psImage->nBitsPerPixel = CEOSScanInt( psRecord->pachData+216, 4 );
266 1 : psImage->nBands = CEOSScanInt( psRecord->pachData+232, 4 );
267 1 : psImage->nLines = CEOSScanInt( psRecord->pachData+236, 8 );
268 1 : psImage->nPixels = CEOSScanInt( psRecord->pachData+248, 8 );
269 :
270 1 : psImage->nPrefixBytes = CEOSScanInt( psRecord->pachData+276, 4 );
271 1 : psImage->nSuffixBytes = CEOSScanInt( psRecord->pachData+288, 4 );
272 :
273 :
274 1 : if( psImage->nImageRecLength <= 0 ||
275 : psImage->nPrefixBytes < 0 ||
276 : psImage->nBands > INT_MAX / psImage->nImageRecLength ||
277 : psImage->nBands > INT_MAX / sizeof(int))
278 : {
279 0 : CEOSDestroyRecord( psRecord );
280 0 : CEOSClose( psImage );
281 0 : return NULL;
282 : }
283 :
284 : /* -------------------------------------------------------------------- */
285 : /* Try to establish the layout of the imagery data. */
286 : /* -------------------------------------------------------------------- */
287 1 : psImage->nLineOffset = psImage->nBands * psImage->nImageRecLength;
288 :
289 1 : psImage->panDataStart = (int *) VSIMalloc(sizeof(int) * psImage->nBands);
290 1 : if( psImage->panDataStart == NULL )
291 : {
292 0 : CEOSDestroyRecord( psRecord );
293 0 : CEOSClose( psImage );
294 0 : return NULL;
295 : }
296 :
297 5 : for( i = 0; i < psImage->nBands; i++ )
298 : {
299 4 : psImage->panDataStart[i] =
300 : psRecord->nLength + i * psImage->nImageRecLength
301 : + 12 + psImage->nPrefixBytes;
302 : }
303 :
304 1 : CEOSDestroyRecord( psRecord );
305 :
306 1 : return psImage;
307 : }
308 :
309 : /************************************************************************/
310 : /* CEOSReadScanline() */
311 : /************************************************************************/
312 :
313 : /**
314 : * Read a scanline of image.
315 : *
316 : * @param psCEOS The CEOS dataset handle returned by CEOSOpen().
317 : * @param nBand The band number (ie. 1, 2, 3).
318 : * @param nScanline The scanline requested, one based.
319 : * @param pData The data buffer to read into. Must be at least nPixels *
320 : * nBitesPerPixel bits long.
321 : *
322 : * @return CPLErr Returns error indicator or CE_None if the read succeeds.
323 : */
324 :
325 : CPLErr CEOSReadScanline( CEOSImage * psCEOS, int nBand, int nScanline,
326 : void * pData )
327 :
328 3 : {
329 : int nOffset, nBytes;
330 :
331 : /*
332 : * As a short cut, I currently just seek to the data, and read it
333 : * raw, rather than trying to read ceos records properly.
334 : */
335 :
336 3 : nOffset = psCEOS->panDataStart[nBand-1]
337 : + (nScanline-1) * psCEOS->nLineOffset;
338 :
339 3 : if( VSIFSeek( psCEOS->fpImage, nOffset, SEEK_SET ) != 0 )
340 : {
341 0 : CPLError( CE_Failure, CPLE_FileIO,
342 : "Seek to %d for scanline %d failed.\n",
343 : nOffset, nScanline );
344 0 : return CE_Failure;
345 : }
346 :
347 : /* -------------------------------------------------------------------- */
348 : /* Read the data. */
349 : /* -------------------------------------------------------------------- */
350 3 : nBytes = psCEOS->nPixels * psCEOS->nBitsPerPixel / 8;
351 3 : if( (int) VSIFRead( pData, 1, nBytes, psCEOS->fpImage ) != nBytes )
352 : {
353 0 : CPLError( CE_Failure, CPLE_FileIO,
354 : "Read of %d bytes for scanline %d failed.\n",
355 : nBytes, nScanline );
356 0 : return CE_Failure;
357 : }
358 :
359 3 : return CE_None;
360 : }
361 :
362 : /************************************************************************/
363 : /* CEOSClose() */
364 : /************************************************************************/
365 :
366 : /**
367 : * Close a CEOS transfer. Any open files are closed, and memory deallocated.
368 : *
369 : * @param psCEOS The CEOSImage handle from CEOSOpen to be closed.
370 : */
371 :
372 : void CEOSClose( CEOSImage * psCEOS )
373 :
374 1 : {
375 1 : CPLFree( psCEOS->panDataStart );
376 1 : VSIFClose( psCEOS->fpImage );
377 1 : CPLFree( psCEOS );
378 1 : }
379 :
|