1 : /******************************************************************************
2 : * $Id: ceosopen.c 12821 2007-11-16 22:44:35Z 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 12821 2007-11-16 22:44:35Z rouault $");
33 :
34 : /************************************************************************/
35 : /* CEOSScanInt() */
36 : /* */
37 : /* Read up to nMaxChars from the passed string, and interpret */
38 : /* as an integer. */
39 : /************************************************************************/
40 :
41 9 : static long CEOSScanInt( const char * pszString, int nMaxChars )
42 :
43 : {
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 1 : CEOSRecord * CEOSReadRecord( CEOSImage *psImage )
67 :
68 : {
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 2 : psRecord->nRecordNum = abyHeader[0] * 256 * 256 * 256
96 1 : + abyHeader[1] * 256 * 256
97 1 : + abyHeader[2] * 256
98 3 : + abyHeader[3];
99 :
100 2 : psRecord->nRecordType = abyHeader[4] * 256 * 256 * 256
101 1 : + abyHeader[5] * 256 * 256
102 1 : + abyHeader[6] * 256
103 3 : + abyHeader[7];
104 :
105 2 : psRecord->nLength = abyHeader[8] * 256 * 256 * 256
106 1 : + abyHeader[9] * 256 * 256
107 1 : + abyHeader[10] * 256
108 3 : + 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 4 : if( psRecord->nRecordNum < 0 || psRecord->nRecordNum > 200000
115 3 : || 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 : return NULL;
137 : }
138 :
139 1 : memcpy( psRecord->pachData, abyHeader, 12 );
140 :
141 2 : if( (int)VSIFRead( psRecord->pachData + 12, 1, psRecord->nLength-12,
142 : psImage->fpImage )
143 1 : != psRecord->nLength - 12 )
144 : {
145 0 : CPLError( CE_Failure, CPLE_FileIO,
146 : "Short read on CEOS record data.\n" );
147 0 : CPLFree( psRecord );
148 0 : return NULL;
149 : }
150 :
151 1 : return psRecord;
152 : }
153 :
154 : /************************************************************************/
155 : /* CEOSDestroyRecord() */
156 : /* */
157 : /* Free a record. */
158 : /************************************************************************/
159 :
160 1 : void CEOSDestroyRecord( CEOSRecord * psRecord )
161 :
162 : {
163 1 : CPLFree( psRecord->pachData );
164 1 : CPLFree( psRecord );
165 1 : }
166 :
167 : /************************************************************************/
168 : /* CEOSOpen() */
169 : /************************************************************************/
170 :
171 : /**
172 : * Open a CEOS transfer.
173 : *
174 : * @param Filename The name of the CEOS imagery file (ie. imag_01.dat).
175 : * @param Access An fopen() style access string. Should be either "rb" for
176 : * read-only access, or "r+b" for read, and update access.
177 : *
178 : * @return A CEOSImage pointer as a handle to the image. The CEOSImage also
179 : * has various information about the image available. A NULL is returned
180 : * if an error occurs.
181 : */
182 :
183 1 : CEOSImage * CEOSOpen( const char * pszFilename, const char * pszAccess )
184 :
185 : {
186 : FILE *fp;
187 : CEOSRecord *psRecord;
188 : CEOSImage *psImage;
189 : int nSeqNum, i;
190 : GByte abyHeader[16];
191 :
192 : /* -------------------------------------------------------------------- */
193 : /* Try to open the imagery file. */
194 : /* -------------------------------------------------------------------- */
195 1 : fp = VSIFOpen( pszFilename, pszAccess );
196 :
197 1 : if( fp == NULL )
198 : {
199 0 : CPLError( CE_Failure, CPLE_OpenFailed,
200 : "Failed to open CEOS file `%s' with access `%s'.\n",
201 : pszFilename, pszAccess );
202 0 : return NULL;
203 : }
204 :
205 : /* -------------------------------------------------------------------- */
206 : /* Create a CEOSImage structure, and initialize it. */
207 : /* -------------------------------------------------------------------- */
208 1 : psImage = (CEOSImage *) CPLCalloc(1,sizeof(CEOSImage));
209 1 : psImage->fpImage = fp;
210 :
211 1 : psImage->nPixels = psImage->nLines = psImage->nBands = 0;
212 :
213 : /* -------------------------------------------------------------------- */
214 : /* Preread info on the first record, to establish if it is */
215 : /* little endian. */
216 : /* -------------------------------------------------------------------- */
217 1 : VSIFRead( abyHeader, 16, 1, fp );
218 1 : VSIFSeek( fp, 0, SEEK_SET );
219 :
220 1 : if( abyHeader[0] != 0 || abyHeader[1] != 0 )
221 1 : psImage->bLittleEndian = TRUE;
222 :
223 : /* -------------------------------------------------------------------- */
224 : /* Try to read the header record. */
225 : /* -------------------------------------------------------------------- */
226 1 : psRecord = CEOSReadRecord( psImage );
227 1 : if( psRecord == NULL )
228 0 : return NULL;
229 :
230 1 : if( psRecord->nRecordType != CRT_IMAGE_FDR )
231 : {
232 0 : CPLError( CE_Failure, CPLE_AppDefined,
233 : "Got a %X type record, instead of the expected\n"
234 : "file descriptor record on file %s.\n",
235 : psRecord->nRecordType, pszFilename );
236 :
237 0 : CEOSDestroyRecord( psRecord );
238 0 : return NULL;
239 : }
240 :
241 : /* -------------------------------------------------------------------- */
242 : /* The sequence number should be 2 indicating this is the */
243 : /* imagery file. */
244 : /* -------------------------------------------------------------------- */
245 1 : nSeqNum = CEOSScanInt( psRecord->pachData + 44, 4 );
246 1 : if( nSeqNum != 2 )
247 : {
248 0 : CPLError( CE_Warning, CPLE_AppDefined,
249 : "Got a %d file sequence number, instead of the expected\n"
250 : "2 indicating imagery on file %s.\n"
251 : "Continuing to access anyways.\n",
252 : nSeqNum, pszFilename );
253 : }
254 :
255 : /* -------------------------------------------------------------------- */
256 : /* Extract various information. */
257 : /* -------------------------------------------------------------------- */
258 1 : psImage->nImageRecCount = CEOSScanInt( psRecord->pachData+180, 6 );
259 1 : psImage->nImageRecLength = CEOSScanInt( psRecord->pachData+186, 6 );
260 1 : psImage->nBitsPerPixel = CEOSScanInt( psRecord->pachData+216, 4 );
261 1 : psImage->nBands = CEOSScanInt( psRecord->pachData+232, 4 );
262 1 : psImage->nLines = CEOSScanInt( psRecord->pachData+236, 8 );
263 1 : psImage->nPixels = CEOSScanInt( psRecord->pachData+248, 8 );
264 :
265 1 : psImage->nPrefixBytes = CEOSScanInt( psRecord->pachData+276, 4 );
266 1 : psImage->nSuffixBytes = CEOSScanInt( psRecord->pachData+288, 4 );
267 :
268 : /* -------------------------------------------------------------------- */
269 : /* Try to establish the layout of the imagery data. */
270 : /* -------------------------------------------------------------------- */
271 1 : psImage->nLineOffset = psImage->nBands * psImage->nImageRecLength;
272 :
273 1 : psImage->panDataStart = (int *) CPLMalloc(sizeof(int) * psImage->nBands);
274 :
275 5 : for( i = 0; i < psImage->nBands; i++ )
276 : {
277 8 : psImage->panDataStart[i] =
278 4 : psRecord->nLength + i * psImage->nImageRecLength
279 4 : + 12 + psImage->nPrefixBytes;
280 : }
281 :
282 1 : CEOSDestroyRecord( psRecord );
283 :
284 1 : return psImage;
285 : }
286 :
287 : /************************************************************************/
288 : /* CEOSReadScanline() */
289 : /************************************************************************/
290 :
291 : /**
292 : * Read a scanline of image.
293 : *
294 : * @param psCEOS The CEOS dataset handle returned by CEOSOpen().
295 : * @param nBand The band number (ie. 1, 2, 3).
296 : * @param nScanline The scanline requested, one based.
297 : * @param pData The data buffer to read into. Must be at least nPixels *
298 : * nBitesPerPixel bits long.
299 : *
300 : * @return CPLErr Returns error indicator or CE_None if the read succeeds.
301 : */
302 :
303 3 : CPLErr CEOSReadScanline( CEOSImage * psCEOS, int nBand, int nScanline,
304 : void * pData )
305 :
306 : {
307 : int nOffset, nBytes;
308 :
309 : /*
310 : * As a short cut, I currently just seek to the data, and read it
311 : * raw, rather than trying to read ceos records properly.
312 : */
313 :
314 6 : nOffset = psCEOS->panDataStart[nBand-1]
315 3 : + (nScanline-1) * psCEOS->nLineOffset;
316 :
317 3 : if( VSIFSeek( psCEOS->fpImage, nOffset, SEEK_SET ) != 0 )
318 : {
319 0 : CPLError( CE_Failure, CPLE_FileIO,
320 : "Seek to %d for scanline %d failed.\n",
321 : nOffset, nScanline );
322 0 : return CE_Failure;
323 : }
324 :
325 : /* -------------------------------------------------------------------- */
326 : /* Read the data. */
327 : /* -------------------------------------------------------------------- */
328 3 : nBytes = psCEOS->nPixels * psCEOS->nBitsPerPixel / 8;
329 3 : if( (int) VSIFRead( pData, 1, nBytes, psCEOS->fpImage ) != nBytes )
330 : {
331 0 : CPLError( CE_Failure, CPLE_FileIO,
332 : "Read of %d bytes for scanline %d failed.\n",
333 : nBytes, nScanline );
334 0 : return CE_Failure;
335 : }
336 :
337 3 : return CE_None;
338 : }
339 :
340 : /************************************************************************/
341 : /* CEOSClose() */
342 : /************************************************************************/
343 :
344 : /**
345 : * Close a CEOS transfer. Any open files are closed, and memory deallocated.
346 : *
347 : * @param psCEOS The CEOSImage handle from CEOSOpen to be closed.
348 : */
349 :
350 1 : void CEOSClose( CEOSImage * psCEOS )
351 :
352 : {
353 1 : CPLFree( psCEOS->panDataStart );
354 1 : VSIFClose( psCEOS->fpImage );
355 1 : CPLFree( psCEOS );
356 1 : }
357 :
|