1 : /*****************************************************************************
2 : * $Id: IngrTypes.cpp 21680 2011-02-11 21:12:07Z warmerdam $
3 : *
4 : * Project: Intergraph Raster Format support
5 : * Purpose: Types support function
6 : * Author: Ivan Lucena, ivan.lucena@pmldnet.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2007, Ivan Lucena
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 "IngrTypes.h"
31 : #include "JpegHelper.h"
32 :
33 : #ifdef DEBUG
34 : #include "stdio.h"
35 : #endif
36 :
37 : static const INGR_FormatDescription INGR_FormatTable[] = {
38 : {PackedBinary, "Packed Binary", GDT_Byte},
39 : {ByteInteger, "Byte Integer", GDT_Byte},
40 : {WordIntegers, "Word Integers", GDT_Int16},
41 : {Integers32Bit, "Integers 32Bit", GDT_Int32},
42 : {FloatingPoint32Bit, "Floating Point 32Bit", GDT_Float32},
43 : {FloatingPoint64Bit, "Floating Point 64Bit", GDT_Float64},
44 : {Complex, "Complex", GDT_CFloat32},
45 : {DoublePrecisionComplex, "Double Precision Complex", GDT_CFloat64},
46 : {RunLengthEncoded, "Run Length Encoded Bitonal", GDT_Byte},
47 : {RunLengthEncodedC, "Run Length Encoded Color", GDT_Byte},
48 : {FigureOfMerit, "Figure of Merit", GDT_Byte},
49 : {DTMFlags, "DTMFlags", GDT_Byte},
50 : {RLEVariableValuesWithZS, "RLE Variable Values With ZS", GDT_Byte},
51 : {RLEBinaryValues, "RLE Binary Values", GDT_Byte},
52 : {RLEVariableValues, "RLE Variable Values", GDT_Byte},
53 : {RLEVariableValuesWithZ, "RLE Variable Values With Z", GDT_Byte},
54 : {RLEVariableValuesC, "RLE Variable Values C", GDT_Byte},
55 : {RLEVariableValuesN, "RLE Variable Values N", GDT_Byte},
56 : {QuadTreeEncoded, "Quad Tree Encoded", GDT_Byte},
57 : {CCITTGroup4, "CCITT Group 4", GDT_Byte},
58 : {RunLengthEncodedRGB, "Run Length Encoded RGB", GDT_Byte},
59 : {VariableRunLength, "Variable Run Length", GDT_Byte},
60 : {AdaptiveRGB, "Adaptive RGB", GDT_Byte},
61 : {Uncompressed24bit, "Uncompressed 24bit", GDT_Byte},
62 : {AdaptiveGrayScale, "Adaptive Gray Scale", GDT_Byte},
63 : {JPEGGRAY, "JPEG GRAY", GDT_Byte},
64 : {JPEGRGB, "JPEG RGB", GDT_Byte},
65 : {JPEGCYMK, "JPEG CYMK", GDT_Byte},
66 : {TiledRasterData, "Tiled Raste Data", GDT_Byte},
67 : {NotUsedReserved, "Not Used( Reserved )", GDT_Byte},
68 : {ContinuousTone, "Continuous Tone", GDT_Byte},
69 : {LineArt, "LineArt", GDT_Byte}
70 : };
71 :
72 : static const char *IngrOrientation[] = {
73 : "Upper Left Vertical",
74 : "Upper Right Vertical",
75 : "Lower Left Vertical",
76 : "Lower Right Vertical",
77 : "Upper Left Horizontal",
78 : "Upper Right Horizontal",
79 : "Lower Left Horizontal",
80 : "Lower Right Horizontal"};
81 :
82 : static const GByte BitReverseTable[256] =
83 : {
84 : 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
85 : 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
86 : 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
87 : 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
88 : 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
89 : 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
90 : 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
91 : 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
92 : 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
93 : 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
94 : 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
95 : 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
96 : 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
97 : 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
98 : 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
99 : 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
100 : 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
101 : 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
102 : 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
103 : 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
104 : 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
105 : 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
106 : 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
107 : 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
108 : 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
109 : 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
110 : 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
111 : 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
112 : 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
113 : 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
114 : 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
115 : 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
116 : };
117 :
118 : // -----------------------------------------------------------------------------
119 : // Scanline Orientation Flip Matrix
120 : // -----------------------------------------------------------------------------
121 :
122 : static const double INGR_URV_Flip[16] =
123 : {
124 : 1.0, 0.0, 0.0, 0.0,
125 : 0.0, -1.0, 0.0, 0.0,
126 : 0.0, 0.0, 1.0, 0.0,
127 : 0.0, 0.0, 0.0, 1.0
128 : };
129 : static const double INGR_LLV_Flip[16] =
130 : {
131 : -1.0, 0.0, 0.0, 0.0,
132 : 0.0, 1.0, 0.0, 0.0,
133 : 0.0, 0.0, 1.0, 0.0,
134 : 0.0, 0.0, 0.0, 1.0
135 : };
136 : static const double INGR_LRV_Flip[16] =
137 : {
138 : -1.0, 0.0, 0.0, 0.0,
139 : 0.0, -1.0, 0.0, 0.0,
140 : 0.0, 0.0, 1.0, 0.0,
141 : 0.0, 0.0, 0.0, 1.0
142 : };
143 : static const double INGR_ULH_Flip[16] =
144 : {
145 : 1.0, 0.0, 0.0, 0.0,
146 : 0.0, 1.0, 0.0, 0.0,
147 : 0.0, 0.0, -1.0, 0.0,
148 : 0.0, 0.0, 0.0, 1.0
149 : };
150 : static const double INGR_URH_Flip[16] =
151 : {
152 : 1.0, 0.0, 0.0, 0.0,
153 : 0.0, -1.0, 0.0, 0.0,
154 : 0.0, 0.0, -1.0, 0.0,
155 : 0.0, 0.0, 0.0, 1.0
156 : };
157 : static const double INGR_LLH_Flip[16] =
158 : {
159 : -1.0, 0.0, 0.0, 0.0,
160 : 0.0, 1.0, 0.0, 0.0,
161 : 0.0, 0.0, -1.0, 0.0,
162 : 0.0, 0.0, 0.0, 1.0
163 : };
164 : static const double INGR_LRH_Flip[16] =
165 : {
166 : -1.0, 0.0, 0.0, 0.0,
167 : 0.0, -1.0, 0.0, 0.0,
168 : 0.0, 0.0, -1.0, 0.0,
169 : 0.0, 0.0, 0.0, 1.0
170 : };
171 :
172 9 : void INGR_MultiplyMatrix( double *padfA, real64 *padfB, const double *padfC )
173 : {
174 : int i;
175 : int j;
176 :
177 45 : for( i = 0; i < 4; i++ )
178 : {
179 180 : for( j = 0; j < 4; j++ )
180 : {
181 144 : padfA[(i * 4) + j] = (double)
182 288 : padfB[(i * 4) + 0] * padfC[(0 * 4) + j] +
183 288 : padfB[(i * 4) + 1] * padfC[(1 * 4) + j] +
184 288 : padfB[(i * 4) + 2] * padfC[(2 * 4) + j] +
185 864 : padfB[(i * 4) + 3] * padfC[(3 * 4) + j];
186 : }
187 : }
188 9 : }
189 :
190 : // -----------------------------------------------------------------------------
191 : // INGR_GetDataType()
192 : // -----------------------------------------------------------------------------
193 :
194 109 : GDALDataType CPL_STDCALL INGR_GetDataType( uint16 eCode )
195 : {
196 : unsigned int i;
197 :
198 919 : for( i = 0; i < FORMAT_TAB_COUNT; i++ )
199 : {
200 919 : if( eCode == INGR_FormatTable[i].eFormatCode )
201 : {
202 109 : return INGR_FormatTable[i].eDataType;
203 : }
204 : }
205 :
206 0 : return GDT_Unknown;
207 : }
208 :
209 : // -----------------------------------------------------------------------------
210 : // INGR_GetFormatName()
211 : // -----------------------------------------------------------------------------
212 :
213 109 : const char * CPL_STDCALL INGR_GetFormatName( uint16 eCode )
214 : {
215 : unsigned int i;
216 :
217 919 : for( i = 0; i < FORMAT_TAB_COUNT; i++ )
218 : {
219 919 : if( eCode == INGR_FormatTable[i].eFormatCode )
220 : {
221 109 : return INGR_FormatTable[i].pszName;
222 : }
223 : }
224 :
225 0 : return "Not Identified";
226 : }
227 :
228 : // -----------------------------------------------------------------------------
229 : // INGR_GetOrientation()
230 : // -----------------------------------------------------------------------------
231 :
232 109 : const char * CPL_STDCALL INGR_GetOrientation( uint8 nIndex )
233 : {
234 109 : if (nIndex < sizeof(IngrOrientation) / sizeof(IngrOrientation[0]))
235 109 : return IngrOrientation[nIndex];
236 : else
237 0 : return "invalid orientation";
238 : }
239 :
240 : // -----------------------------------------------------------------------------
241 : // INGR_GetFormat()
242 : // -----------------------------------------------------------------------------
243 :
244 34 : INGR_Format CPL_STDCALL INGR_GetFormat( GDALDataType eType,
245 : const char *pszCompression )
246 : {
247 34 : if( EQUAL( pszCompression, "None" ) ||
248 : EQUAL( pszCompression, "" ) )
249 : {
250 34 : switch ( eType )
251 : {
252 16 : case GDT_Byte: return ByteInteger;
253 3 : case GDT_Int16: return WordIntegers;
254 3 : case GDT_UInt16: return WordIntegers;
255 3 : case GDT_Int32: return Integers32Bit;
256 3 : case GDT_UInt32: return Integers32Bit;
257 3 : case GDT_Float32: return FloatingPoint32Bit;
258 3 : case GDT_Float64: return FloatingPoint64Bit;
259 0 : default: return ByteInteger;
260 : }
261 : }
262 :
263 : unsigned int i;
264 :
265 0 : for( i = 0; i < FORMAT_TAB_COUNT; i++ )
266 : {
267 0 : if( EQUAL( pszCompression, INGR_FormatTable[i].pszName ) )
268 : {
269 0 : return (INGR_Format) INGR_FormatTable[i].eFormatCode;
270 : }
271 : }
272 :
273 0 : return ByteInteger;
274 : }
275 :
276 : // -----------------------------------------------------------------------------
277 : // INGR_GetTransMatrix()
278 : // -----------------------------------------------------------------------------
279 :
280 73 : void CPL_STDCALL INGR_GetTransMatrix( INGR_HeaderOne *pHeaderOne,
281 : double *padfGeoTransform )
282 : {
283 : // -------------------------------------------------------------
284 : // Check for empty transformation matrix
285 : // -------------------------------------------------------------
286 :
287 393 : if( pHeaderOne->TransformationMatrix[0] == 0.0 &&
288 64 : pHeaderOne->TransformationMatrix[2] == 0.0 &&
289 64 : pHeaderOne->TransformationMatrix[3] == 0.0 &&
290 64 : pHeaderOne->TransformationMatrix[4] == 0.0 &&
291 64 : pHeaderOne->TransformationMatrix[5] == 0.0 &&
292 64 : pHeaderOne->TransformationMatrix[7] == 0.0 )
293 : {
294 64 : padfGeoTransform[0] = 0.0;
295 64 : padfGeoTransform[1] = 1.0;
296 64 : padfGeoTransform[2] = 0.0;
297 64 : padfGeoTransform[3] = 0.0;
298 64 : padfGeoTransform[4] = 0.0;
299 64 : padfGeoTransform[5] = 1.0;
300 64 : return;
301 : }
302 :
303 : // -------------------------------------------------------------
304 : // Calculate Concatened Tranformation Matrix based on Orientation
305 : // -------------------------------------------------------------
306 :
307 : double adfConcat[16];
308 :
309 9 : switch( (INGR_Orientation ) pHeaderOne->ScanlineOrientation )
310 : {
311 : case UpperLeftVertical:
312 : {
313 0 : unsigned int i = 0;
314 0 : for(i = 0; i < 16; i++)
315 : {
316 0 : adfConcat[i] = (double) pHeaderOne->TransformationMatrix[i];
317 : }
318 : }
319 0 : break;
320 : case UpperRightVertical:
321 0 : INGR_MultiplyMatrix( adfConcat, pHeaderOne->TransformationMatrix, INGR_URV_Flip );
322 0 : break;
323 : case LowerLeftVertical:
324 0 : INGR_MultiplyMatrix( adfConcat, pHeaderOne->TransformationMatrix, INGR_LLV_Flip );
325 0 : break;
326 : case LowerRightVertical:
327 0 : INGR_MultiplyMatrix( adfConcat, pHeaderOne->TransformationMatrix, INGR_LRV_Flip );
328 0 : break;
329 : case UpperLeftHorizontal:
330 3 : INGR_MultiplyMatrix( adfConcat, pHeaderOne->TransformationMatrix, INGR_ULH_Flip );
331 3 : break;
332 : case UpperRightHorizontal:
333 0 : INGR_MultiplyMatrix( adfConcat, pHeaderOne->TransformationMatrix, INGR_URH_Flip );
334 0 : break;
335 : case LowerLeftHorizontal:
336 6 : INGR_MultiplyMatrix( adfConcat, pHeaderOne->TransformationMatrix, INGR_LLH_Flip );
337 6 : break;
338 : case LowerRightHorizontal:
339 0 : INGR_MultiplyMatrix( adfConcat, pHeaderOne->TransformationMatrix, INGR_LRH_Flip );
340 : break;
341 : }
342 :
343 : // -------------------------------------------------------------
344 : // Convert to GDAL GeoTransformation Matrix
345 : // -------------------------------------------------------------
346 :
347 9 : padfGeoTransform[0] = adfConcat[3] - adfConcat[0] / 2;
348 9 : padfGeoTransform[1] = adfConcat[0];
349 9 : padfGeoTransform[2] = adfConcat[1];
350 9 : padfGeoTransform[3] = adfConcat[7] + adfConcat[5] / 2;
351 9 : padfGeoTransform[4] = adfConcat[4];
352 9 : padfGeoTransform[5] = - adfConcat[5];
353 : }
354 :
355 : // -----------------------------------------------------------------------------
356 : // INGR_SetTransMatrix()
357 : // -----------------------------------------------------------------------------
358 :
359 30 : void CPL_STDCALL INGR_SetTransMatrix( real64 *padfMatrix, double *padfGeoTransform )
360 : {
361 : unsigned int i;
362 :
363 480 : for( i = 0; i < 15; i++ )
364 : {
365 450 : padfMatrix[i] = 0.0;
366 : }
367 :
368 30 : padfMatrix[10] = 1.0;
369 30 : padfMatrix[15] = 1.0;
370 :
371 30 : padfMatrix[3] = padfGeoTransform[0] + padfGeoTransform[1] / 2;
372 30 : padfMatrix[0] = padfGeoTransform[1];
373 30 : padfMatrix[1] = padfGeoTransform[2];
374 30 : padfMatrix[7] = padfGeoTransform[3] + padfGeoTransform[5] / 2;
375 30 : padfMatrix[4] = padfGeoTransform[4];
376 30 : padfMatrix[5] = - padfGeoTransform[5];
377 30 : }
378 :
379 : // -----------------------------------------------------------------------------
380 : // INGR_SetIGDSColors()
381 : // -----------------------------------------------------------------------------
382 :
383 0 : uint32 CPL_STDCALL INGR_SetIGDSColors( GDALColorTable *poColorTable,
384 : INGR_ColorTable256 *pColorTableIGDS )
385 : {
386 : GDALColorEntry oEntry;
387 : int i;
388 :
389 0 : for( i = 0; i < poColorTable->GetColorEntryCount(); i++ )
390 : {
391 0 : poColorTable->GetColorEntryAsRGB( i, &oEntry );
392 0 : pColorTableIGDS->Entry[i].v_red = (uint8) oEntry.c1;
393 0 : pColorTableIGDS->Entry[i].v_green = (uint8) oEntry.c2;
394 0 : pColorTableIGDS->Entry[i].v_blue = (uint8) oEntry.c3;
395 : }
396 :
397 0 : return i;
398 : }
399 :
400 : // -----------------------------------------------------------------------------
401 : // INGR_GetTileDirectory()
402 : // -----------------------------------------------------------------------------
403 :
404 5 : uint32 CPL_STDCALL INGR_GetTileDirectory( VSILFILE *fp,
405 : uint32 nOffset,
406 : int nBandXSize,
407 : int nBandYSize,
408 : INGR_TileHeader *pTileDir,
409 : INGR_TileItem **pahTiles)
410 : {
411 5 : if( fp == NULL ||
412 : nBandXSize < 1 ||
413 : nBandYSize < 1 ||
414 : pTileDir == NULL )
415 : {
416 0 : return 0;
417 : }
418 :
419 : // -------------------------------------------------------------
420 : // Read it from the begging of the data segment
421 : // -------------------------------------------------------------
422 :
423 : GByte abyBuf[SIZEOF_TDIR];
424 :
425 5 : if( ( VSIFSeekL( fp, nOffset, SEEK_SET ) == -1 ) ||
426 : ( VSIFReadL( abyBuf, 1, SIZEOF_TDIR, fp ) == 0 ) )
427 : {
428 0 : CPLDebug("INGR", "Error reading tiles header");
429 0 : return 0;
430 : }
431 :
432 5 : INGR_TileHeaderDiskToMem( pTileDir, abyBuf );
433 :
434 5 : if (pTileDir->TileSize == 0)
435 : {
436 : CPLError(CE_Failure, CPLE_AppDefined,
437 0 : "Invalid tile size : %d", pTileDir->TileSize);
438 0 : return 0;
439 : }
440 :
441 : // ----------------------------------------------------------------
442 : // Calculate the number of tiles
443 : // ----------------------------------------------------------------
444 :
445 5 : int nTilesPerCol = (int) ceil( (float) nBandXSize / pTileDir->TileSize );
446 5 : int nTilesPerRow = (int) ceil( (float) nBandYSize / pTileDir->TileSize );
447 :
448 5 : uint32 nTiles = nTilesPerCol * nTilesPerRow;
449 :
450 : // ----------------------------------------------------------------
451 : // Load the tile table (first tile s already read)
452 : // ----------------------------------------------------------------
453 :
454 5 : *pahTiles = (INGR_TileItem*) VSICalloc( nTiles, SIZEOF_TILE );
455 5 : GByte *pabyBuf = (GByte*) VSICalloc( ( nTiles - 1 ), SIZEOF_TILE );
456 :
457 5 : if (*pahTiles == NULL || pabyBuf == NULL)
458 : {
459 0 : CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
460 0 : CPLFree( *pahTiles );
461 0 : *pahTiles = NULL;
462 0 : CPLFree( pabyBuf );
463 0 : return 0;
464 : }
465 :
466 5 : (*pahTiles)[0].Start = pTileDir->First.Start;
467 5 : (*pahTiles)[0].Allocated = pTileDir->First.Allocated;
468 5 : (*pahTiles)[0].Used = pTileDir->First.Used;
469 :
470 5 : if( nTiles > 1 &&
471 : ( VSIFReadL( pabyBuf, ( nTiles - 1 ), SIZEOF_TILE, fp ) == 0 ) )
472 : {
473 0 : CPLDebug("INGR", "Error reading tiles table");
474 0 : CPLFree( *pahTiles );
475 0 : *pahTiles = NULL;
476 0 : CPLFree( pabyBuf );
477 0 : return 0;
478 : }
479 :
480 : unsigned int i;
481 :
482 20 : for( i = 1; i < nTiles; i++ )
483 : {
484 : INGR_TileItemDiskToMem( &((*pahTiles)[i]),
485 15 : &pabyBuf[ (i - 1) * SIZEOF_TILE] );
486 : }
487 :
488 5 : CPLFree( pabyBuf );
489 5 : return nTiles;
490 : }
491 :
492 : // -----------------------------------------------------------------------------
493 : // INGR_GetIGDSColors()
494 : // -----------------------------------------------------------------------------
495 :
496 0 : void CPL_STDCALL INGR_GetIGDSColors( VSILFILE *fp,
497 : uint32 nOffset,
498 : uint32 nEntries,
499 : GDALColorTable *poColorTable )
500 : {
501 0 : if( fp == NULL ||
502 : nEntries == 0 ||
503 : nEntries > 256 ||
504 : poColorTable == NULL )
505 : {
506 0 : return;
507 : }
508 :
509 : // -------------------------------------------------------------
510 : // Read it from the middle of the second block
511 : // -------------------------------------------------------------
512 :
513 0 : uint32 nStart = nOffset + SIZEOF_HDR1 + SIZEOF_HDR2_A;
514 :
515 : INGR_ColorTable256 hIGDSColors;
516 :
517 : /* nEntries >= 0 && nEntries <= 256, so alloc is safe */
518 0 : GByte *pabyBuf = (GByte*) CPLCalloc( nEntries, SIZEOF_IGDS );
519 :
520 0 : if( ( VSIFSeekL( fp, nStart, SEEK_SET ) == -1 ) ||
521 : ( VSIFReadL( pabyBuf, nEntries, SIZEOF_IGDS, fp ) == 0 ) )
522 : {
523 0 : CPLFree( pabyBuf );
524 0 : return;
525 : }
526 :
527 0 : unsigned int i = 0;
528 0 : unsigned int n = 0;
529 :
530 0 : for( i = 0; i < nEntries; i++ )
531 : {
532 0 : BUF2STRC( pabyBuf, n, hIGDSColors.Entry[i].v_red );
533 0 : BUF2STRC( pabyBuf, n, hIGDSColors.Entry[i].v_green );
534 0 : BUF2STRC( pabyBuf, n, hIGDSColors.Entry[i].v_blue );
535 : }
536 :
537 0 : CPLFree( pabyBuf );
538 :
539 : // -------------------------------------------------------------
540 : // Read it to poColorTable
541 : // -------------------------------------------------------------
542 :
543 : GDALColorEntry oEntry;
544 :
545 0 : oEntry.c4 = 255;
546 :
547 0 : for( i = 0; i < nEntries; i++ )
548 : {
549 0 : oEntry.c1 = hIGDSColors.Entry[i].v_red;
550 0 : oEntry.c2 = hIGDSColors.Entry[i].v_green;
551 0 : oEntry.c3 = hIGDSColors.Entry[i].v_blue;
552 0 : poColorTable->SetColorEntry( i, &oEntry );
553 : }
554 : }
555 :
556 : // -----------------------------------------------------------------------------
557 : // INGR_SetEnvironColors()
558 : // -----------------------------------------------------------------------------
559 :
560 0 : uint32 CPL_STDCALL INGR_SetEnvironColors( GDALColorTable *poColorTable,
561 : INGR_ColorTableVar *pEnvironTable )
562 : {
563 : GDALColorEntry oEntry;
564 0 : real32 fNormFactor = 0xfff / 255;
565 : int i;
566 :
567 0 : for( i = 0; i < poColorTable->GetColorEntryCount(); i++ )
568 : {
569 0 : poColorTable->GetColorEntryAsRGB( i, &oEntry );
570 0 : pEnvironTable->Entry[i].v_slot = (uint16) i;
571 0 : pEnvironTable->Entry[i].v_red = (uint16) ( oEntry.c1 * fNormFactor );
572 0 : pEnvironTable->Entry[i].v_green = (uint16) ( oEntry.c2 * fNormFactor );
573 0 : pEnvironTable->Entry[i].v_blue = (uint16) ( oEntry.c3 * fNormFactor );
574 : }
575 :
576 0 : return i;
577 : }
578 :
579 : // -----------------------------------------------------------------------------
580 : // INGR_GetEnvironVColors()
581 : // -----------------------------------------------------------------------------
582 :
583 5 : void CPL_STDCALL INGR_GetEnvironVColors( VSILFILE *fp,
584 : uint32 nOffset,
585 : uint32 nEntries,
586 : GDALColorTable *poColorTable )
587 : {
588 5 : if( fp == NULL ||
589 : nEntries == 0 ||
590 : poColorTable == NULL )
591 : {
592 0 : return;
593 : }
594 :
595 : // -------------------------------------------------------------
596 : // Read it from the third block
597 : // -------------------------------------------------------------
598 :
599 5 : uint32 nStart = nOffset + SIZEOF_HDR1 + SIZEOF_HDR2;
600 :
601 : INGR_ColorTableVar hVLTColors;
602 :
603 5 : hVLTColors.Entry = (vlt_slot*) VSICalloc( nEntries, SIZEOF_VLTS );
604 :
605 5 : GByte *pabyBuf = (GByte*) VSICalloc( nEntries, SIZEOF_VLTS );
606 :
607 5 : if (hVLTColors.Entry == NULL || pabyBuf == NULL)
608 : {
609 0 : CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
610 0 : CPLFree( pabyBuf );
611 0 : CPLFree( hVLTColors.Entry );
612 0 : return;
613 : }
614 :
615 5 : if( ( VSIFSeekL( fp, nStart, SEEK_SET ) == -1 ) ||
616 : ( VSIFReadL( pabyBuf, nEntries, SIZEOF_VLTS, fp ) == 0 ) )
617 : {
618 0 : CPLFree( pabyBuf );
619 0 : CPLFree( hVLTColors.Entry );
620 0 : return;
621 : }
622 :
623 5 : unsigned int i = 0;
624 5 : unsigned int n = 0;
625 :
626 1285 : for( i = 0; i < nEntries; i++ )
627 : {
628 1280 : BUF2STRC( pabyBuf, n, hVLTColors.Entry[i].v_slot );
629 1280 : BUF2STRC( pabyBuf, n, hVLTColors.Entry[i].v_red );
630 1280 : BUF2STRC( pabyBuf, n, hVLTColors.Entry[i].v_green );
631 1280 : BUF2STRC( pabyBuf, n, hVLTColors.Entry[i].v_blue );
632 : }
633 :
634 5 : CPLFree( pabyBuf );
635 :
636 :
637 : #if defined(CPL_MSB)
638 : for (i = 0; i < nEntries; i++)
639 : {
640 : CPL_LSBPTR16(&hVLTColors.Entry[i].v_slot);
641 : CPL_LSBPTR16(&hVLTColors.Entry[i].v_red);
642 : CPL_LSBPTR16(&hVLTColors.Entry[i].v_green);
643 : CPL_LSBPTR16(&hVLTColors.Entry[i].v_blue);
644 : }
645 : #endif
646 :
647 : // -------------------------------------------------------------
648 : // Get Maximum Intensity and Index Values
649 : // -------------------------------------------------------------
650 :
651 5 : real32 fMaxRed = 0.0;
652 5 : real32 fMaxGreen = 0.0;
653 5 : real32 fMaxBlues = 0.0;
654 :
655 1285 : for( i = 0; i < nEntries; i++ )
656 : {
657 1280 : fMaxRed = MAX( fMaxRed , hVLTColors.Entry[i].v_red );
658 1280 : fMaxGreen = MAX( fMaxGreen, hVLTColors.Entry[i].v_green );
659 1280 : fMaxBlues = MAX( fMaxBlues, hVLTColors.Entry[i].v_blue );
660 : }
661 :
662 : // -------------------------------------------------------------
663 : // Calculate Normalization Factor
664 : // -------------------------------------------------------------
665 :
666 5 : real32 fNormFactor = 0.0;
667 :
668 5 : fNormFactor = ( fMaxRed > fMaxGreen ? fMaxRed : fMaxGreen );
669 5 : fNormFactor = ( fNormFactor > fMaxBlues ? fNormFactor : fMaxBlues );
670 5 : if (fNormFactor)
671 5 : fNormFactor = 255 / fNormFactor;
672 :
673 : // -------------------------------------------------------------
674 : // Loads GDAL Color Table ( filling the wholes )
675 : // -------------------------------------------------------------
676 :
677 : GDALColorEntry oEntry;
678 :
679 1285 : for( i = 0; i < nEntries; i++ )
680 : {
681 1280 : oEntry.c1 = (short) ( hVLTColors.Entry[i].v_red * fNormFactor );
682 1280 : oEntry.c2 = (short) ( hVLTColors.Entry[i].v_green * fNormFactor );
683 1280 : oEntry.c3 = (short) ( hVLTColors.Entry[i].v_blue * fNormFactor );
684 1280 : oEntry.c4 = (short) 255;
685 1280 : poColorTable->SetColorEntry( hVLTColors.Entry[i].v_slot, &oEntry );
686 : }
687 :
688 5 : CPLFree( hVLTColors.Entry );
689 : }
690 :
691 : // -----------------------------------------------------------------------------
692 : // SetMiniMax()
693 : // -----------------------------------------------------------------------------
694 :
695 108 : INGR_MinMax CPL_STDCALL INGR_SetMinMax( GDALDataType eType, double dValue )
696 : {
697 : INGR_MinMax uResult;
698 :
699 108 : switch ( eType )
700 : {
701 : case GDT_Byte:
702 60 : uResult.AsUint8 = (uint8) dValue;
703 60 : break;
704 : case GDT_Int16:
705 10 : uResult.AsUint16 = (int16) dValue;
706 10 : break;
707 : case GDT_UInt16:
708 6 : uResult.AsUint16 = (uint16) dValue;
709 6 : break;
710 : case GDT_Int32:
711 10 : uResult.AsUint32 = (int32) dValue;
712 10 : break;
713 : case GDT_UInt32:
714 6 : uResult.AsUint32 = (uint32) dValue;
715 6 : break;
716 : case GDT_Float32:
717 8 : uResult.AsReal32 = (real32) dValue;
718 8 : break;
719 : case GDT_Float64:
720 8 : uResult.AsReal64 = (real64) dValue;
721 : default:
722 8 : uResult.AsUint8 = (uint8) 0;
723 : }
724 :
725 108 : return uResult;
726 : }
727 :
728 : // -----------------------------------------------------------------------------
729 : // INGR_GetMinMax()
730 : // -----------------------------------------------------------------------------
731 :
732 0 : double CPL_STDCALL INGR_GetMinMax( GDALDataType eType, INGR_MinMax hValue )
733 : {
734 0 : switch ( eType )
735 : {
736 0 : case GDT_Byte: return (double) hValue.AsUint8;
737 0 : case GDT_Int16: return (double) hValue.AsUint16;
738 0 : case GDT_UInt16: return (double) hValue.AsUint16;
739 0 : case GDT_Int32: return (double) hValue.AsUint32;
740 0 : case GDT_UInt32: return (double) hValue.AsUint32;
741 0 : case GDT_Float32: return (double) hValue.AsReal32;
742 0 : case GDT_Float64: return (double) hValue.AsReal64;
743 0 : default: return (double) 0.0;
744 : }
745 : }
746 :
747 : // -----------------------------------------------------------------------------
748 : // INGR_GetDataBlockSize()
749 : // -----------------------------------------------------------------------------
750 :
751 7 : uint32 CPL_STDCALL INGR_GetDataBlockSize( const char *pszFilename,
752 : uint32 nBandOffset,
753 : uint32 nDataOffset )
754 : {
755 7 : if( nBandOffset == 0 )
756 : {
757 : // -------------------------------------------------------------
758 : // Until the end of the file
759 : // -------------------------------------------------------------
760 :
761 : VSIStatBufL sStat;
762 7 : VSIStatL( pszFilename, &sStat );
763 7 : return (uint32) (sStat.st_size - nDataOffset);
764 : }
765 : else
766 : {
767 : // -------------------------------------------------------------
768 : // Until the end of the band
769 : // -------------------------------------------------------------
770 :
771 0 : return nBandOffset - nDataOffset;
772 : }
773 : }
774 :
775 : // -----------------------------------------------------------------------------
776 : // INGR_CreateVirtualFile()
777 : // -----------------------------------------------------------------------------
778 :
779 9 : INGR_VirtualFile CPL_STDCALL INGR_CreateVirtualFile( const char *pszFilename,
780 : INGR_Format eFormat,
781 : int nXSize,
782 : int nYSize,
783 : int nTileSize,
784 : int nQuality,
785 : GByte *pabyBuffer,
786 : int nBufferSize,
787 : int nBand )
788 : {
789 : INGR_VirtualFile hVirtual;
790 :
791 : hVirtual.pszFileName = CPLSPrintf( "/vsimem/%s.virtual",
792 9 : CPLGetBasename( pszFilename ) );
793 :
794 9 : int nJPGComponents = 1;
795 :
796 9 : switch( eFormat )
797 : {
798 : case JPEGRGB:
799 4 : nJPGComponents = 3;
800 : case JPEGGRAY:
801 : {
802 8 : GByte *pabyHeader = (GByte*) CPLCalloc( 1, 2048 );
803 : int nHeaderSize = JPGHLP_HeaderMaker( pabyHeader,
804 : nTileSize,
805 : nTileSize,
806 : nJPGComponents,
807 : 0,
808 8 : nQuality );
809 8 : VSILFILE *fp = VSIFOpenL( hVirtual.pszFileName, "w+" );
810 8 : VSIFWriteL( pabyHeader, 1, nHeaderSize, fp );
811 8 : VSIFWriteL( pabyBuffer, 1, nBufferSize, fp );
812 8 : VSIFCloseL( fp );
813 8 : CPLFree( pabyHeader );
814 8 : break;
815 : }
816 : case CCITTGroup4:
817 : {
818 1 : REVERSEBITSBUFFER( pabyBuffer, nBufferSize );
819 1 : TIFF *hTIFF = VSI_TIFFOpen( hVirtual.pszFileName, "w+" );
820 1 : TIFFSetField( hTIFF, TIFFTAG_IMAGEWIDTH, nXSize );
821 1 : TIFFSetField( hTIFF, TIFFTAG_IMAGELENGTH, nYSize );
822 1 : TIFFSetField( hTIFF, TIFFTAG_BITSPERSAMPLE, 1 );
823 1 : TIFFSetField( hTIFF, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT );
824 1 : TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
825 1 : TIFFSetField( hTIFF, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB );
826 1 : TIFFSetField( hTIFF, TIFFTAG_ROWSPERSTRIP, -1 );
827 1 : TIFFSetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, 1 );
828 1 : TIFFSetField( hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE );
829 1 : TIFFSetField( hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4 );
830 1 : TIFFWriteRawStrip( hTIFF, 0, pabyBuffer, nBufferSize );
831 1 : TIFFWriteDirectory( hTIFF );
832 1 : TIFFClose( hTIFF );
833 1 : break;
834 : }
835 : default:
836 0 : return hVirtual;
837 : }
838 :
839 9 : hVirtual.poDS = (GDALDataset*) GDALOpen( hVirtual.pszFileName, GA_ReadOnly );
840 :
841 9 : if( hVirtual.poDS )
842 : {
843 9 : hVirtual.poBand = (GDALRasterBand*) GDALGetRasterBand( hVirtual.poDS, nBand );
844 : }
845 :
846 9 : return hVirtual;
847 : }
848 :
849 : // -----------------------------------------------------------------------------
850 : // INGR_ReleaseVirtual()
851 : // -----------------------------------------------------------------------------
852 :
853 9 : void CPL_STDCALL INGR_ReleaseVirtual( INGR_VirtualFile *poTiffMem )
854 : {
855 9 : delete poTiffMem->poDS;
856 9 : VSIUnlink( poTiffMem->pszFileName );
857 9 : }
858 :
859 : // -----------------------------------------------------------------------------
860 : // INGR_ReleaseVirtual()
861 : // -----------------------------------------------------------------------------
862 :
863 4 : int CPL_STDCALL INGR_ReadJpegQuality( VSILFILE *fp, uint32 nAppDataOfseet,
864 : uint32 nSeekLimit )
865 : {
866 4 : if( nAppDataOfseet == 0 )
867 : {
868 0 : return INGR_JPEGQDEFAULT;
869 : }
870 :
871 : INGR_JPEGAppData hJpegData;
872 4 : uint32 nNext = nAppDataOfseet;
873 :
874 : GByte abyBuf[SIZEOF_JPGAD];
875 :
876 4 : do
877 : {
878 4 : if( ( VSIFSeekL( fp, nNext, SEEK_SET ) == -1 ) ||
879 : ( VSIFReadL( abyBuf, 1, SIZEOF_JPGAD, fp ) == 0 ) )
880 : {
881 0 : return INGR_JPEGQDEFAULT;
882 : }
883 :
884 4 : INGR_JPEGAppDataDiskToMem(&hJpegData, abyBuf);
885 :
886 4 : nNext += hJpegData.RemainingLength;
887 :
888 4 : if( nNext > ( nSeekLimit - SIZEOF_JPGAD ) )
889 : {
890 0 : return INGR_JPEGQDEFAULT;
891 : }
892 : }
893 : while( ! ( hJpegData.ApplicationType == 2 &&
894 : hJpegData.SubTypeCode == 12 ) );
895 :
896 4 : return hJpegData.JpegQuality;
897 : }
898 :
899 : // -----------------------------------------------------------------------------
900 : // INGR_Decode()
901 : //
902 : // Decode the various RLE compression options.
903 : // -----------------------------------------------------------------------------
904 :
905 : int CPL_STDCALL
906 237 : INGR_Decode( INGR_Format eFormat, GByte *pabySrcData, GByte *pabyDstData,
907 : uint32 nSrcBytes, uint32 nBlockSize, uint32 *pnBytesConsumed )
908 :
909 : {
910 237 : switch( eFormat )
911 : {
912 : case RunLengthEncoded:
913 : return INGR_DecodeRunLengthBitonal( pabySrcData, pabyDstData,
914 : nSrcBytes, nBlockSize,
915 135 : pnBytesConsumed );
916 :
917 : case RunLengthEncodedC:
918 : return INGR_DecodeRunLengthPaletted( pabySrcData, pabyDstData,
919 : nSrcBytes, nBlockSize,
920 100 : pnBytesConsumed );
921 :
922 : default:
923 : return INGR_DecodeRunLength( pabySrcData, pabyDstData,
924 : nSrcBytes, nBlockSize,
925 2 : pnBytesConsumed );
926 : }
927 : }
928 :
929 : // -----------------------------------------------------------------------------
930 : // INGR_DecodeRunLength()
931 : // -----------------------------------------------------------------------------
932 :
933 2 : int CPL_STDCALL INGR_DecodeRunLength( GByte *pabySrcData, GByte *pabyDstData,
934 : uint32 nSrcBytes, uint32 nBlockSize,
935 : uint32 *pnBytesConsumed )
936 : {
937 : signed char cAtomHead;
938 :
939 : unsigned int nRun;
940 : unsigned int i;
941 : unsigned int iInput;
942 : unsigned int iOutput;
943 :
944 2 : iInput = 0;
945 2 : iOutput = 0;
946 :
947 2405 : while( ( iInput < nSrcBytes ) && ( iOutput < nBlockSize ) )
948 : {
949 2401 : cAtomHead = (char) pabySrcData[iInput++];
950 :
951 2401 : if( cAtomHead > 0 )
952 : {
953 1411 : nRun = cAtomHead;
954 :
955 73365 : for( i = 0; i < nRun && iInput < nSrcBytes && iOutput < nBlockSize; i++ )
956 : {
957 71954 : pabyDstData[iOutput++] = pabySrcData[iInput++];
958 : }
959 : }
960 990 : else if( cAtomHead < 0 )
961 : {
962 990 : nRun = abs( cAtomHead );
963 :
964 4036 : for( i = 0; i < nRun && iInput < nSrcBytes && iOutput < nBlockSize; i++ )
965 : {
966 3046 : pabyDstData[iOutput++] = pabySrcData[iInput];
967 : }
968 990 : iInput++;
969 : }
970 : }
971 :
972 2 : if( pnBytesConsumed != NULL )
973 0 : *pnBytesConsumed = iInput;
974 :
975 2 : return iOutput;
976 : }
977 :
978 : // -----------------------------------------------------------------------------
979 : // INGR_DecodeRunLengthPaletted()
980 : // -----------------------------------------------------------------------------
981 :
982 : int CPL_STDCALL
983 100 : INGR_DecodeRunLengthPaletted( GByte *pabySrcData, GByte *pabyDstData,
984 : uint32 nSrcBytes, uint32 nBlockSize,
985 : uint32 *pnBytesConsumed )
986 : {
987 : unsigned short nColor;
988 : unsigned short nCount;
989 :
990 : unsigned int i;
991 : unsigned int iInput;
992 : unsigned int iOutput;
993 :
994 100 : unsigned short *pauiSrc = (unsigned short *) pabySrcData;
995 100 : unsigned int nSrcShorts = nSrcBytes / 2;
996 :
997 100 : iInput = 0;
998 100 : iOutput = 0;
999 :
1000 100 : if ( nSrcShorts == 0 )
1001 0 : return 0;
1002 :
1003 13720 : do
1004 : {
1005 13720 : nCount = 0;
1006 13720 : nColor = CPL_LSBWORD16( pauiSrc[ iInput ] );
1007 13720 : iInput++;
1008 :
1009 13720 : if( nColor == 0x5900 ||
1010 : nColor == 0x5901 )
1011 : {
1012 100 : iInput++;
1013 100 : continue;
1014 : }
1015 :
1016 13620 : if ( iInput < nSrcShorts )
1017 : {
1018 13620 : nCount = CPL_LSBWORD16( pauiSrc[ iInput ] );
1019 13620 : iInput++;
1020 : }
1021 :
1022 28620 : for( i = 0; i < nCount && iOutput < nBlockSize; i++ )
1023 : {
1024 15000 : pabyDstData[iOutput++] = (unsigned char) nColor;
1025 : }
1026 : }
1027 : while( ( iInput < nSrcShorts ) && ( iOutput < nBlockSize) );
1028 :
1029 100 : if( pnBytesConsumed != NULL )
1030 100 : *pnBytesConsumed = iInput * 2;
1031 :
1032 100 : return iOutput;
1033 : }
1034 :
1035 : // -----------------------------------------------------------------------------
1036 : // INGR_DecodeRunLengthBitonal()
1037 : // -----------------------------------------------------------------------------
1038 :
1039 : int CPL_STDCALL
1040 135 : INGR_DecodeRunLengthBitonal( GByte *pabySrcData, GByte *pabyDstData,
1041 : uint32 nSrcBytes, uint32 nBlockSize,
1042 : uint32 *pnBytesConsumed )
1043 : {
1044 : unsigned short i;
1045 135 : unsigned int iInput = 0;
1046 135 : unsigned int iOutput = 0;
1047 135 : unsigned short *pauiSrc = (unsigned short *) pabySrcData;
1048 135 : unsigned int nSrcShorts = nSrcBytes / 2;
1049 : unsigned short nRun;
1050 135 : unsigned char nValue = 0;
1051 :
1052 135 : if (nSrcShorts == 0)
1053 0 : return 0;
1054 :
1055 135 : if( CPL_LSBWORD16(pauiSrc[0]) != 0x5900 )
1056 126 : nValue = 1;
1057 :
1058 3986 : do
1059 : {
1060 3986 : nRun = CPL_LSBWORD16(pauiSrc[ iInput ]);
1061 3986 : iInput++;
1062 :
1063 3986 : if( nRun == 0x5900 )
1064 : {
1065 135 : iInput++; // line id
1066 135 : iInput++; // line data size
1067 135 : continue;
1068 : }
1069 :
1070 29366 : for( i = 0; i < nRun && iOutput < nBlockSize; i++ )
1071 : {
1072 25515 : pabyDstData[ iOutput++ ] = nValue;
1073 : }
1074 :
1075 3851 : nValue = ( nValue == 1 ? 0 : 1 );
1076 : }
1077 : while( ( iInput < nSrcShorts ) && ( iOutput < nBlockSize ) );
1078 :
1079 135 : if( pnBytesConsumed != NULL )
1080 135 : *pnBytesConsumed = iInput * 2;
1081 :
1082 135 : return iOutput;
1083 : }
1084 :
1085 : // -----------------------------------------------------------------------------
1086 : // INGR_DecodeRunLengthBitonalTiled()
1087 : // -----------------------------------------------------------------------------
1088 :
1089 : int CPL_STDCALL
1090 4 : INGR_DecodeRunLengthBitonalTiled( GByte *pabySrcData, GByte *pabyDstData,
1091 : uint32 nSrcBytes, uint32 nBlockSize,
1092 : uint32 *pnBytesConsumed )
1093 : {
1094 : unsigned short i;
1095 4 : unsigned int iInput = 0;
1096 4 : unsigned int iOutput = 0;
1097 4 : unsigned short *pauiSrc = (unsigned short *) pabySrcData;
1098 4 : unsigned int nSrcShorts = nSrcBytes / 2;
1099 4 : unsigned short nRun = 0;
1100 4 : unsigned char nValue = 0;
1101 4 : unsigned short previous = 0;
1102 :
1103 4 : if (nSrcShorts == 0)
1104 0 : return 0;
1105 :
1106 4 : if( CPL_LSBWORD16(pauiSrc[0]) != 0x5900 )
1107 : {
1108 4 : nRun = 256;
1109 4 : nValue = 0;
1110 4 : previous = 0;
1111 10313 : do
1112 : {
1113 10313 : previous = nRun;
1114 :
1115 10313 : nRun = CPL_LSBWORD16(pauiSrc[ iInput ]);
1116 10313 : iInput++;
1117 :
1118 10313 : if( nRun == 0 && previous == 0 ) // new line
1119 : {
1120 565 : nValue = 0;
1121 : }
1122 :
1123 75849 : for( i = 0; i < nRun && iOutput < nBlockSize; i++ )
1124 : {
1125 65536 : pabyDstData[ iOutput++ ] = nValue;
1126 : }
1127 :
1128 10313 : if( nRun != 0 )
1129 : {
1130 9059 : nValue = ( nValue == 1 ? 0 : 1 );
1131 : }
1132 : }
1133 : while( ( iInput < nSrcShorts ) && ( iOutput < nBlockSize ) );
1134 : }
1135 : else
1136 : {
1137 0 : nValue = 1;
1138 0 : do
1139 : {
1140 0 : nRun = CPL_LSBWORD16(pauiSrc[ iInput ]);
1141 0 : iInput++;
1142 :
1143 0 : if( nRun == 0x5900 )
1144 : {
1145 0 : iInput++; // line id
1146 0 : iInput++; // line data size
1147 0 : continue;
1148 : }
1149 :
1150 0 : for( i = 0; i < nRun && iOutput < nBlockSize; i++ )
1151 : {
1152 0 : pabyDstData[ iOutput++ ] = nValue;
1153 : }
1154 :
1155 0 : nValue = ( nValue == 1 ? 0 : 1 );
1156 : }
1157 : while( ( iInput < nSrcShorts ) && ( iOutput < nBlockSize ) );
1158 : }
1159 :
1160 4 : if( pnBytesConsumed != NULL )
1161 : {
1162 0 : *pnBytesConsumed = iInput * 2;
1163 : }
1164 :
1165 4 : return iOutput;
1166 : }
1167 :
1168 800 : void CPL_STDCALL INGR_HeaderOneDiskToMem(INGR_HeaderOne* pHeaderOne, const GByte *pabyBuf)
1169 : {
1170 800 : unsigned int n = 0;
1171 :
1172 800 : BUF2STRC( pabyBuf, n, pHeaderOne->HeaderType );
1173 800 : BUF2STRC( pabyBuf, n, pHeaderOne->WordsToFollow );
1174 800 : BUF2STRC( pabyBuf, n, pHeaderOne->DataTypeCode );
1175 800 : BUF2STRC( pabyBuf, n, pHeaderOne->ApplicationType );
1176 800 : BUF2STRC( pabyBuf, n, pHeaderOne->XViewOrigin );
1177 800 : BUF2STRC( pabyBuf, n, pHeaderOne->YViewOrigin );
1178 800 : BUF2STRC( pabyBuf, n, pHeaderOne->ZViewOrigin );
1179 800 : BUF2STRC( pabyBuf, n, pHeaderOne->XViewExtent );
1180 800 : BUF2STRC( pabyBuf, n, pHeaderOne->YViewExtent );
1181 800 : BUF2STRC( pabyBuf, n, pHeaderOne->ZViewExtent );
1182 800 : BUF2STRC( pabyBuf, n, pHeaderOne->TransformationMatrix );
1183 800 : BUF2STRC( pabyBuf, n, pHeaderOne->PixelsPerLine );
1184 800 : BUF2STRC( pabyBuf, n, pHeaderOne->NumberOfLines );
1185 800 : BUF2STRC( pabyBuf, n, pHeaderOne->DeviceResolution );
1186 800 : BUF2STRC( pabyBuf, n, pHeaderOne->ScanlineOrientation );
1187 800 : BUF2STRC( pabyBuf, n, pHeaderOne->ScannableFlag );
1188 800 : BUF2STRC( pabyBuf, n, pHeaderOne->RotationAngle );
1189 800 : BUF2STRC( pabyBuf, n, pHeaderOne->SkewAngle );
1190 800 : BUF2STRC( pabyBuf, n, pHeaderOne->DataTypeModifier );
1191 800 : BUF2STRC( pabyBuf, n, pHeaderOne->DesignFileName );
1192 800 : BUF2STRC( pabyBuf, n, pHeaderOne->DataBaseFileName );
1193 800 : BUF2STRC( pabyBuf, n, pHeaderOne->ParentGridFileName );
1194 800 : BUF2STRC( pabyBuf, n, pHeaderOne->FileDescription );
1195 800 : BUF2STRC( pabyBuf, n, pHeaderOne->Minimum );
1196 800 : BUF2STRC( pabyBuf, n, pHeaderOne->Maximum );
1197 800 : BUF2STRC( pabyBuf, n, pHeaderOne->Reserved );
1198 800 : BUF2STRC( pabyBuf, n, pHeaderOne->GridFileVersion );
1199 :
1200 : #if defined(CPL_MSB)
1201 : CPL_LSBPTR16(&pHeaderOne->WordsToFollow);
1202 : CPL_LSBPTR16(&pHeaderOne->DataTypeCode);
1203 : CPL_LSBPTR16(&pHeaderOne->ApplicationType);
1204 : CPL_LSBPTR32(&pHeaderOne->PixelsPerLine);
1205 : CPL_LSBPTR32(&pHeaderOne->NumberOfLines);
1206 : CPL_LSBPTR16(&pHeaderOne->DeviceResolution);
1207 : CPL_LSBPTR16(&pHeaderOne->DataTypeModifier);
1208 : switch (INGR_GetDataType(pHeaderOne->DataTypeCode))
1209 : {
1210 : case GDT_Byte:
1211 : pHeaderOne->Minimum.AsUint8 = *(uint8*)&(pHeaderOne->Minimum);
1212 : pHeaderOne->Maximum.AsUint8 = *(uint8*)&(pHeaderOne->Maximum);
1213 : break;
1214 : case GDT_Int16:
1215 : pHeaderOne->Minimum.AsUint16 = CPL_LSBWORD16(*(uint16*)&(pHeaderOne->Minimum));
1216 : pHeaderOne->Maximum.AsUint16 = CPL_LSBWORD16(*(uint16*)&(pHeaderOne->Maximum));
1217 : break;
1218 : case GDT_UInt16:
1219 : pHeaderOne->Minimum.AsUint16 = CPL_LSBWORD16(*(uint16*)&(pHeaderOne->Minimum));
1220 : pHeaderOne->Maximum.AsUint16 = CPL_LSBWORD16(*(uint16*)&(pHeaderOne->Maximum));
1221 : break;
1222 : case GDT_Int32:
1223 : pHeaderOne->Minimum.AsUint32 = CPL_LSBWORD32(*(uint32*)&(pHeaderOne->Minimum));
1224 : pHeaderOne->Maximum.AsUint32 = CPL_LSBWORD32(*(uint32*)&(pHeaderOne->Maximum));
1225 : break;
1226 : case GDT_UInt32:
1227 : pHeaderOne->Minimum.AsUint32 = CPL_LSBWORD32(*(uint32*)&(pHeaderOne->Minimum));
1228 : pHeaderOne->Maximum.AsUint32 = CPL_LSBWORD32(*(uint32*)&(pHeaderOne->Maximum));
1229 : break;
1230 : /* FIXME ? I'm not sure this is correct for floats */
1231 : case GDT_Float32:
1232 : pHeaderOne->Minimum.AsUint32 = CPL_LSBWORD32(*(uint32*)&(pHeaderOne->Minimum));
1233 : pHeaderOne->Maximum.AsUint32 = CPL_LSBWORD32(*(uint32*)&(pHeaderOne->Maximum));
1234 : break;
1235 : case GDT_Float64:
1236 : CPL_LSBPTR64(&pHeaderOne->Minimum.AsReal64); CPL_LSBPTR64(&pHeaderOne->Maximum.AsReal64);
1237 : break;
1238 : default: break;
1239 : }
1240 : #endif
1241 :
1242 : // --------------------------------------------------------------------
1243 : // Convert WAX REAL*8 to IEEE double
1244 : // --------------------------------------------------------------------
1245 :
1246 806 : if( pHeaderOne->GridFileVersion == 1 ||
1247 : ( pHeaderOne->GridFileVersion == 2 &&
1248 3 : ( pHeaderOne->TransformationMatrix[10] != 1.0 &&
1249 3 : pHeaderOne->TransformationMatrix[15] != 1.0 ) ) )
1250 : {
1251 5 : INGR_DGN2IEEEDouble( &pHeaderOne->XViewOrigin );
1252 5 : INGR_DGN2IEEEDouble( &pHeaderOne->YViewOrigin );
1253 5 : INGR_DGN2IEEEDouble( &pHeaderOne->ZViewOrigin );
1254 5 : INGR_DGN2IEEEDouble( &pHeaderOne->XViewExtent );
1255 5 : INGR_DGN2IEEEDouble( &pHeaderOne->YViewExtent );
1256 5 : INGR_DGN2IEEEDouble( &pHeaderOne->ZViewExtent );
1257 5 : INGR_DGN2IEEEDouble( &pHeaderOne->RotationAngle );
1258 5 : INGR_DGN2IEEEDouble( &pHeaderOne->SkewAngle );
1259 :
1260 : uint8 i;
1261 :
1262 85 : for( i = 0; i < 16; i++ )
1263 : {
1264 80 : INGR_DGN2IEEEDouble( &pHeaderOne->TransformationMatrix[i]);
1265 : }
1266 : }
1267 : else if (pHeaderOne->GridFileVersion == 3)
1268 : {
1269 : #ifdef CPL_MSB
1270 : CPL_LSBPTR64( &pHeaderOne->XViewOrigin );
1271 : CPL_LSBPTR64( &pHeaderOne->YViewOrigin );
1272 : CPL_LSBPTR64( &pHeaderOne->ZViewOrigin );
1273 : CPL_LSBPTR64( &pHeaderOne->XViewExtent );
1274 : CPL_LSBPTR64( &pHeaderOne->YViewExtent );
1275 : CPL_LSBPTR64( &pHeaderOne->ZViewExtent );
1276 : CPL_LSBPTR64( &pHeaderOne->RotationAngle );
1277 : CPL_LSBPTR64( &pHeaderOne->SkewAngle );
1278 :
1279 : uint8 i;
1280 :
1281 : for( i = 0; i < 16; i++ )
1282 : {
1283 : CPL_LSBPTR64( &pHeaderOne->TransformationMatrix[i]);
1284 : }
1285 : #endif
1286 : }
1287 800 : }
1288 :
1289 54 : void CPL_STDCALL INGR_HeaderOneMemToDisk(const INGR_HeaderOne* pHeaderOne, GByte *pabyBuf)
1290 : {
1291 54 : unsigned int n = 0;
1292 : INGR_HeaderOne* pLSBHeaderOne;
1293 : #if defined(CPL_MSB)
1294 : pLSBHeaderOne = (INGR_HeaderOne* )CPLMalloc(sizeof(INGR_HeaderOne));
1295 : memcpy(pLSBHeaderOne, pHeaderOne, sizeof(INGR_HeaderOne));
1296 :
1297 : switch (INGR_GetDataType(pLSBHeaderOne->DataTypeCode))
1298 : {
1299 : case GDT_Byte: *(uint8*)&(pLSBHeaderOne->Minimum) = pLSBHeaderOne->Minimum.AsUint8;
1300 : *(uint8*)&(pLSBHeaderOne->Maximum) = pLSBHeaderOne->Maximum.AsUint8; break;
1301 : case GDT_Int16: *(uint16*)&(pLSBHeaderOne->Minimum) = CPL_LSBWORD16(pLSBHeaderOne->Minimum.AsUint16);
1302 : *(uint16*)&(pLSBHeaderOne->Maximum) = CPL_LSBWORD16(pLSBHeaderOne->Maximum.AsUint16); break;
1303 : case GDT_UInt16: *(uint16*)&(pLSBHeaderOne->Minimum) = CPL_LSBWORD16(pLSBHeaderOne->Minimum.AsUint16);
1304 : *(uint16*)&(pLSBHeaderOne->Maximum) = CPL_LSBWORD16(pLSBHeaderOne->Maximum.AsUint16); break;
1305 : case GDT_Int32: *(uint32*)&(pLSBHeaderOne->Minimum) = CPL_LSBWORD32(pLSBHeaderOne->Minimum.AsUint32);
1306 : *(uint32*)&(pLSBHeaderOne->Maximum) = CPL_LSBWORD32(pLSBHeaderOne->Maximum.AsUint32); break;
1307 : case GDT_UInt32: *(uint32*)&(pLSBHeaderOne->Minimum) = CPL_LSBWORD32(pLSBHeaderOne->Minimum.AsUint32);
1308 : *(uint32*)&(pLSBHeaderOne->Maximum) = CPL_LSBWORD32(pLSBHeaderOne->Maximum.AsUint32); break;
1309 : /* FIXME ? I'm not sure this is correct for floats */
1310 : case GDT_Float32: *(uint32*)&(pLSBHeaderOne->Minimum) = CPL_LSBWORD32(pLSBHeaderOne->Minimum.AsUint32);
1311 : *(uint32*)&(pLSBHeaderOne->Maximum) = CPL_LSBWORD32(pLSBHeaderOne->Maximum.AsUint32); break;
1312 : case GDT_Float64: CPL_LSBPTR64(&pLSBHeaderOne->Minimum.AsReal64); CPL_LSBPTR64(&pLSBHeaderOne->Maximum.AsReal64); break;
1313 : default: break;
1314 : }
1315 :
1316 : CPL_LSBPTR16(&pLSBHeaderOne->WordsToFollow);
1317 : CPL_LSBPTR16(&pLSBHeaderOne->DataTypeCode);
1318 : CPL_LSBPTR16(&pLSBHeaderOne->ApplicationType);
1319 : CPL_LSBPTR32(&pLSBHeaderOne->PixelsPerLine);
1320 : CPL_LSBPTR32(&pLSBHeaderOne->NumberOfLines);
1321 : CPL_LSBPTR16(&pLSBHeaderOne->DeviceResolution);
1322 : CPL_LSBPTR16(&pLSBHeaderOne->DataTypeModifier);
1323 :
1324 : if (pLSBHeaderOne->GridFileVersion == 3)
1325 : {
1326 : CPL_LSBPTR64( &pLSBHeaderOne->XViewOrigin );
1327 : CPL_LSBPTR64( &pLSBHeaderOne->YViewOrigin );
1328 : CPL_LSBPTR64( &pLSBHeaderOne->ZViewOrigin );
1329 : CPL_LSBPTR64( &pLSBHeaderOne->XViewExtent );
1330 : CPL_LSBPTR64( &pLSBHeaderOne->YViewExtent );
1331 : CPL_LSBPTR64( &pLSBHeaderOne->ZViewExtent );
1332 : CPL_LSBPTR64( &pLSBHeaderOne->RotationAngle );
1333 : CPL_LSBPTR64( &pLSBHeaderOne->SkewAngle );
1334 :
1335 : uint8 i;
1336 :
1337 : for( i = 0; i < 16; i++ )
1338 : {
1339 : CPL_LSBPTR64( &pLSBHeaderOne->TransformationMatrix[i]);
1340 : }
1341 : }
1342 : #else
1343 54 : pLSBHeaderOne = (INGR_HeaderOne* )pHeaderOne;
1344 : #endif
1345 :
1346 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->HeaderType );
1347 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->WordsToFollow );
1348 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->DataTypeCode );
1349 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->ApplicationType );
1350 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->XViewOrigin );
1351 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->YViewOrigin );
1352 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->ZViewOrigin );
1353 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->XViewExtent );
1354 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->YViewExtent );
1355 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->ZViewExtent );
1356 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->TransformationMatrix );
1357 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->PixelsPerLine );
1358 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->NumberOfLines );
1359 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->DeviceResolution );
1360 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->ScanlineOrientation );
1361 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->ScannableFlag );
1362 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->RotationAngle );
1363 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->SkewAngle );
1364 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->DataTypeModifier );
1365 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->DesignFileName );
1366 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->DataBaseFileName );
1367 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->ParentGridFileName );
1368 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->FileDescription );
1369 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->Minimum );
1370 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->Maximum );
1371 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->Reserved );
1372 54 : STRC2BUF( pabyBuf, n, pLSBHeaderOne->GridFileVersion );
1373 :
1374 : #if defined(CPL_MSB)
1375 : CPLFree(pLSBHeaderOne);
1376 : #endif
1377 54 : }
1378 :
1379 73 : void CPL_STDCALL INGR_HeaderTwoADiskToMem(INGR_HeaderTwoA* pHeaderTwo, const GByte *pabyBuf)
1380 : {
1381 73 : unsigned int n = 0;
1382 :
1383 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->Gain );
1384 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->OffsetThreshold );
1385 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->View1 );
1386 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->View2 );
1387 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->ViewNumber );
1388 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->Reserved2 );
1389 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->Reserved3 );
1390 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->AspectRatio );
1391 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->CatenatedFilePointer );
1392 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->ColorTableType );
1393 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->Reserved8 );
1394 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->NumberOfCTEntries );
1395 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->ApplicationPacketPointer );
1396 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->ApplicationPacketLength );
1397 73 : BUF2STRC( pabyBuf, n, pHeaderTwo->Reserved );
1398 :
1399 : #if defined(CPL_MSB)
1400 : CPL_LSBPTR64(&pHeaderTwo->AspectRatio);
1401 : CPL_LSBPTR32(&pHeaderTwo->CatenatedFilePointer);
1402 : CPL_LSBPTR16(&pHeaderTwo->ColorTableType);
1403 : CPL_LSBPTR32(&pHeaderTwo->NumberOfCTEntries);
1404 : CPL_LSBPTR32(&pHeaderTwo->ApplicationPacketPointer);
1405 : CPL_LSBPTR32(&pHeaderTwo->ApplicationPacketLength);
1406 : #endif
1407 73 : }
1408 :
1409 54 : void CPL_STDCALL INGR_HeaderTwoAMemToDisk(const INGR_HeaderTwoA* pHeaderTwo, GByte *pabyBuf)
1410 : {
1411 54 : unsigned int n = 0;
1412 : INGR_HeaderTwoA* pLSBHeaderTwo;
1413 : #if defined(CPL_MSB)
1414 : pLSBHeaderTwo = (INGR_HeaderTwoA* )CPLMalloc(sizeof(INGR_HeaderTwoA));
1415 : memcpy(pLSBHeaderTwo, pHeaderTwo, sizeof(INGR_HeaderTwoA));
1416 :
1417 : CPL_LSBPTR64(&pLSBHeaderTwo->AspectRatio);
1418 : CPL_LSBPTR32(&pLSBHeaderTwo->CatenatedFilePointer);
1419 : CPL_LSBPTR16(&pLSBHeaderTwo->ColorTableType);
1420 : CPL_LSBPTR32(&pLSBHeaderTwo->NumberOfCTEntries);
1421 : CPL_LSBPTR32(&pLSBHeaderTwo->ApplicationPacketPointer);
1422 : CPL_LSBPTR32(&pLSBHeaderTwo->ApplicationPacketLength);
1423 : #else
1424 54 : pLSBHeaderTwo = (INGR_HeaderTwoA* )pHeaderTwo;
1425 : #endif
1426 :
1427 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->Gain );
1428 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->OffsetThreshold );
1429 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->View1 );
1430 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->View2 );
1431 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->ViewNumber );
1432 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->Reserved2 );
1433 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->Reserved3 );
1434 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->AspectRatio );
1435 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->CatenatedFilePointer );
1436 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->ColorTableType );
1437 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->Reserved8 );
1438 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->NumberOfCTEntries );
1439 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->ApplicationPacketPointer );
1440 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->ApplicationPacketLength );
1441 54 : STRC2BUF( pabyBuf, n, pLSBHeaderTwo->Reserved );
1442 :
1443 : #if defined(CPL_MSB)
1444 : CPLFree(pLSBHeaderTwo);
1445 : #endif
1446 54 : }
1447 :
1448 8 : void CPL_STDCALL INGR_TileHeaderDiskToMem(INGR_TileHeader* pTileHeader, const GByte *pabyBuf)
1449 : {
1450 8 : unsigned int n = 0;
1451 :
1452 8 : BUF2STRC( pabyBuf, n, pTileHeader->ApplicationType );
1453 8 : BUF2STRC( pabyBuf, n, pTileHeader->SubTypeCode );
1454 8 : BUF2STRC( pabyBuf, n, pTileHeader->WordsToFollow );
1455 8 : BUF2STRC( pabyBuf, n, pTileHeader->PacketVersion );
1456 8 : BUF2STRC( pabyBuf, n, pTileHeader->Identifier );
1457 8 : BUF2STRC( pabyBuf, n, pTileHeader->Reserved );
1458 8 : BUF2STRC( pabyBuf, n, pTileHeader->Properties );
1459 8 : BUF2STRC( pabyBuf, n, pTileHeader->DataTypeCode );
1460 8 : BUF2STRC( pabyBuf, n, pTileHeader->Reserved2 );
1461 8 : BUF2STRC( pabyBuf, n, pTileHeader->TileSize );
1462 8 : BUF2STRC( pabyBuf, n, pTileHeader->Reserved3 );
1463 8 : BUF2STRC( pabyBuf, n, pTileHeader->First.Start );
1464 8 : BUF2STRC( pabyBuf, n, pTileHeader->First.Allocated );
1465 8 : BUF2STRC( pabyBuf, n, pTileHeader->First.Used );
1466 :
1467 : #if defined(CPL_MSB)
1468 : CPL_LSBPTR16(&pTileHeader->ApplicationType);
1469 : CPL_LSBPTR16(&pTileHeader->SubTypeCode);
1470 : CPL_LSBPTR32(&pTileHeader->WordsToFollow);
1471 : CPL_LSBPTR16(&pTileHeader->PacketVersion);
1472 : CPL_LSBPTR16(&pTileHeader->Identifier);
1473 : CPL_LSBPTR16(&pTileHeader->Properties);
1474 : CPL_LSBPTR16(&pTileHeader->DataTypeCode);
1475 : CPL_LSBPTR32(&pTileHeader->TileSize);
1476 : CPL_LSBPTR32(&pTileHeader->First.Start);
1477 : CPL_LSBPTR32(&pTileHeader->First.Allocated);
1478 : CPL_LSBPTR32(&pTileHeader->First.Used);
1479 : #endif
1480 8 : }
1481 :
1482 15 : void CPL_STDCALL INGR_TileItemDiskToMem(INGR_TileItem* pTileItem, const GByte *pabyBuf)
1483 : {
1484 15 : unsigned int n = 0;
1485 :
1486 15 : BUF2STRC( pabyBuf, n, pTileItem->Start );
1487 15 : BUF2STRC( pabyBuf, n, pTileItem->Allocated );
1488 15 : BUF2STRC( pabyBuf, n, pTileItem->Used );
1489 :
1490 : #if defined(CPL_MSB)
1491 : CPL_LSBPTR32(&pTileItem->Start);
1492 : CPL_LSBPTR32(&pTileItem->Allocated);
1493 : CPL_LSBPTR32(&pTileItem->Used);
1494 : #endif
1495 15 : }
1496 :
1497 4 : void CPL_STDCALL INGR_JPEGAppDataDiskToMem(INGR_JPEGAppData* pJPEGAppData, const GByte *pabyBuf)
1498 : {
1499 4 : unsigned int n = 0;
1500 :
1501 4 : BUF2STRC( pabyBuf, n, pJPEGAppData->ApplicationType );
1502 4 : BUF2STRC( pabyBuf, n, pJPEGAppData->SubTypeCode );
1503 4 : BUF2STRC( pabyBuf, n, pJPEGAppData->RemainingLength );
1504 4 : BUF2STRC( pabyBuf, n, pJPEGAppData->PacketVersion );
1505 4 : BUF2STRC( pabyBuf, n, pJPEGAppData->JpegQuality );
1506 :
1507 : #if defined(CPL_MSB)
1508 : CPL_LSBPTR16(&pJPEGAppData->ApplicationType);
1509 : CPL_LSBPTR16(&pJPEGAppData->SubTypeCode);
1510 : CPL_LSBPTR32(&pJPEGAppData->RemainingLength);
1511 : CPL_LSBPTR16(&pJPEGAppData->PacketVersion);
1512 : CPL_LSBPTR16(&pJPEGAppData->JpegQuality);
1513 : #endif
1514 4 : }
1515 :
1516 :
1517 : // ------------------------------------------------------------------
1518 : // Pasted from the DNG OGR Driver to avoid dependency on OGR
1519 : // ------------------------------------------------------------------
1520 :
1521 : typedef struct dbl {
1522 : GUInt32 hi;
1523 : GUInt32 lo;
1524 : } double64_t;
1525 :
1526 : /************************************************************************/
1527 : /* INGR_DGN2IEEEDouble() */
1528 : /************************************************************************/
1529 :
1530 120 : void INGR_DGN2IEEEDouble(void * dbl)
1531 :
1532 : {
1533 : double64_t dt;
1534 : GUInt32 sign;
1535 : GUInt32 exponent;
1536 : GUInt32 rndbits;
1537 : unsigned char *src;
1538 : unsigned char *dest;
1539 :
1540 : /* -------------------------------------------------------------------- */
1541 : /* Arrange the VAX double so that it may be accessed by a */
1542 : /* double64_t structure, (two GUInt32s). */
1543 : /* -------------------------------------------------------------------- */
1544 120 : src = (unsigned char *) dbl;
1545 120 : dest = (unsigned char *) &dt;
1546 : #ifdef CPL_LSB
1547 120 : dest[2] = src[0];
1548 120 : dest[3] = src[1];
1549 120 : dest[0] = src[2];
1550 120 : dest[1] = src[3];
1551 120 : dest[6] = src[4];
1552 120 : dest[7] = src[5];
1553 120 : dest[4] = src[6];
1554 120 : dest[5] = src[7];
1555 : #else
1556 : dest[1] = src[0];
1557 : dest[0] = src[1];
1558 : dest[3] = src[2];
1559 : dest[2] = src[3];
1560 : dest[5] = src[4];
1561 : dest[4] = src[5];
1562 : dest[7] = src[6];
1563 : dest[6] = src[7];
1564 : #endif
1565 :
1566 : /* -------------------------------------------------------------------- */
1567 : /* Save the sign of the double */
1568 : /* -------------------------------------------------------------------- */
1569 120 : sign = dt.hi & 0x80000000;
1570 :
1571 : /* -------------------------------------------------------------------- */
1572 : /* Adjust the exponent so that we may work with it */
1573 : /* -------------------------------------------------------------------- */
1574 120 : exponent = dt.hi >> 23;
1575 120 : exponent = exponent & 0x000000ff;
1576 :
1577 120 : if (exponent)
1578 71 : exponent = exponent -129 + 1023;
1579 :
1580 : /* -------------------------------------------------------------------- */
1581 : /* Save the bits that we are discarding so we can round properly */
1582 : /* -------------------------------------------------------------------- */
1583 120 : rndbits = dt.lo & 0x00000007;
1584 :
1585 120 : dt.lo = dt.lo >> 3;
1586 120 : dt.lo = (dt.lo & 0x1fffffff) | (dt.hi << 29);
1587 :
1588 120 : if (rndbits)
1589 60 : dt.lo = dt.lo | 0x00000001;
1590 :
1591 : /* -------------------------------------------------------------------- */
1592 : /* Shift the hi-order int over 3 and insert the exponent and sign */
1593 : /* -------------------------------------------------------------------- */
1594 120 : dt.hi = dt.hi >> 3;
1595 120 : dt.hi = dt.hi & 0x000fffff;
1596 120 : dt.hi = dt.hi | (exponent << 20) | sign;
1597 :
1598 :
1599 :
1600 : #ifdef CPL_LSB
1601 : /* -------------------------------------------------------------------- */
1602 : /* Change the number to a byte swapped format */
1603 : /* -------------------------------------------------------------------- */
1604 120 : src = (unsigned char *) &dt;
1605 120 : dest = (unsigned char *) dbl;
1606 :
1607 120 : dest[0] = src[4];
1608 120 : dest[1] = src[5];
1609 120 : dest[2] = src[6];
1610 120 : dest[3] = src[7];
1611 120 : dest[4] = src[0];
1612 120 : dest[5] = src[1];
1613 120 : dest[6] = src[2];
1614 120 : dest[7] = src[3];
1615 : #else
1616 : memcpy( dbl, &dt, 8 );
1617 : #endif
1618 120 : }
|