1 /*****************************************************************************
2  * $Id: IngrTypes.h df398e80769422a4bbd5d4a295f4ede443c9fec6 2021-04-04 00:17:15 +0200 Even Rouault $
3  *
4  * Project:  Intergraph Raster Format support
5  * Purpose:  Types, constants and functions definition
6  * Author:   Ivan Lucena, [lucena_ivan at hotmail.com]
7  *
8  ******************************************************************************
9  * Copyright (c) 2007, Ivan Lucena
10  * Copyright (c) 2007-2010, Even Rouault <even dot rouault at spatialys.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files ( the "Software" ),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  *****************************************************************************/
30 
31 #ifndef INGR_TYPES_H_INCLUDED
32 #define INGR_TYPES_H_INCLUDED
33 
34 #include "cpl_conv.h"
35 #include "cpl_port.h"
36 #include "gdal.h"
37 #include "gdal_priv.h"
38 #include <cmath>
39 #include <cstdint>
40 
41 CPL_C_START
42 #include "tiffio.h"
43 CPL_C_END
44 
45 //  ----------------------------------------------------------------------------
46 //    Magic number, identification and limits
47 //  ----------------------------------------------------------------------------
48 
49 #define INGR_HEADER_TYPE     9
50 #define INGR_HEADER_VERSION  8
51 #define INGR_HEADER_2D       0
52 #define INGR_HEADER_3D       3
53 #define INGR_RSVC_MAX_NAME   32
54 #define INGR_JPEGQDEFAULT    30
55 
56 //  ----------------------------------------------------------------------------
57 //    Data type convention
58 //  ----------------------------------------------------------------------------
59 
60 typedef double              real64;
61 typedef float               real32;
62 
63 //  ----------------------------------------------------------------------------
64 //    Header Element Type Word ( HTC )
65 //  ----------------------------------------------------------------------------
66 
67 typedef struct {
68 #if defined(CPL_LSB)
69     uint16_t Version   : 6;        // ??????00 00000000
70     uint16_t Is2Dor3D  : 2;        // 000000?? 00000000
71 #else
72     uint16_t Is2Dor3D  : 2;        // 000000?? 00000000
73     uint16_t Version   : 6;        // ??????00 00000000
74 #endif
75     uint16_t Type      : 8;        // 00000000 ????????
76 } INGR_HeaderType;
77 
78 //  ----------------------------------------------------------------------------
79 //    Data type dependent Minimum and Maximum type
80 //  ----------------------------------------------------------------------------
81 
82 typedef union
83 {
84     uint8_t   AsUint8;
85     uint16_t  AsUint16;
86     uint32_t  AsUint32;
87     real32  AsReal32;
88     real64  AsReal64;
89 } INGR_MinMax;
90 
91 //  ----------------------------------------------------------------------------
92 //    Raster Format Types
93 //  ----------------------------------------------------------------------------
94 
95 typedef enum {
96     IngrUnknownFrmt                  = 0,
97     PackedBinary                     = 1,   // 1 bit / pixel
98     ByteInteger                      = 2,   // 8 bits / pixel
99     WordIntegers                     = 3,   // 16 bits / pixel
100     Integers32Bit                    = 4,
101     FloatingPoint32Bit               = 5,
102     FloatingPoint64Bit               = 6,
103     Complex                          = 7,   // 64 bits / pixel
104     DoublePrecisionComplex           = 8,
105     RunLengthEncoded                 = 9,   // Bi-level Images
106     RunLengthEncodedC                = 10,  // Gray Scale, Color
107     FigureOfMerit                    = 11,  // FOM
108     DTMFlags                         = 12,
109     RLEVariableValuesWithZS          = 13,  // Simple
110     RLEBinaryValues                  = 14,  // w/ Edge Type
111     RLEVariableValues                = 15,  // w/ Edge Type
112     RLEVariableValuesWithZ           = 16,  // w/ Edge Type
113     RLEVariableValuesC               = 17,  // Color Table and Shade
114     RLEVariableValuesN               = 18,  // w/ Normals
115     QuadTreeEncoded                  = 19,
116     CCITTGroup4                      = 24,  // Bi-level Images
117     RunLengthEncodedRGB              = 25,  // Full Color
118     VariableRunLength                = 26,
119     AdaptiveRGB                      = 27,  // Full Color
120     Uncompressed24bit                = 28,  // Full Color
121     AdaptiveGrayScale                = 29,
122     JPEGGRAY                         = 30,  // Gray Scale
123     JPEGRGB                          = 31,  // Full Color RGB
124     JPEGCMYK                         = 32,  // CMYK
125     TiledRasterData                  = 65,  // See tile directory Data Type Code (DTC)
126     NotUsedReserved                  = 66,
127     ContinuousTone                   = 67,  // CMYK
128     LineArt                          = 68   // CMYK/RGB
129 } INGR_Format;
130 
131 struct INGR_FormatDescription {
132     INGR_Format      eFormatCode;
133     const char       *pszName;
134     GDALDataType     eDataType;
135 };
136 
137 #define FORMAT_TAB_COUNT 32
138 
139 //  ----------------------------------------------------------------------------
140 //    Raster Application Types
141 //  ----------------------------------------------------------------------------
142 
143 typedef enum {
144     GenericRasterImageFile           = 0,
145     DigitalTerrainModeling           = 1,
146     GridDataUtilities                = 2,
147     DrawingScanning                  = 3,
148     ImageProcessing                  = 4,
149     HiddenSurfaces                   = 5,
150     ImagitexScannerProduct           = 6,
151     ScreenCopyPlotting               = 7,
152     IMAGEandMicroStationImager       = 8,
153     ModelView                        = 9
154 } INGR_Application;
155 
156 //  ----------------------------------------------------------------------------
157 //    Scan line orientation codes
158 //  ----------------------------------------------------------------------------
159 
160 typedef enum {
161     UpperLeftVertical                = 0,
162     UpperRightVertical               = 1,
163     LowerLeftVertical                = 2,
164     LowerRightVertical               = 3,
165     UpperLeftHorizontal              = 4,
166     UpperRightHorizontal             = 5,
167     LowerLeftHorizontal              = 6,
168     LowerRightHorizontal             = 7
169 } INGR_Orientation;
170 
171 //  ----------------------------------------------------------------------------
172 //    Scannable flag field codes
173 //  ----------------------------------------------------------------------------
174 
175 typedef enum {
176     HasLineHeader                    = 1,
177     // Every line of raster data has a 4 word
178     // raster line header at the beginning of
179     // the line. In the line header, the Words
180     // to Follow field specifies the amount
181     // of data following the field, indicating
182     // the start of the next scanline of raster
183     // data
184     NoLineHeader                     = 0
185     // No raster line headers exist. The application
186     // must calculate where lines of raster data
187     // start and end. This process is simple for
188     // non-run length encoded data. It is a fixed
189     // value; therefore, the line length can be
190     // calculated from the data type ( DTC ) and
191     // the number of pixels per line ( PPL ). In
192     // a run length compression case, the data must
193     // be decoded to find the end of a raster line.
194 } INGR_IndexingMethod;
195 
196 //  ----------------------------------------------------------------------------
197 //    Color Table Values ( CTV )
198 //  ----------------------------------------------------------------------------
199 
200 typedef enum {
201     NoColorTable                     = 0,
202     IGDSColorTable                   = 1,
203     EnvironVColorTable               = 2
204 } INGR_ColorTableType;
205 
206 //  ----------------------------------------------------------------------------
207 //    Environ-V Color Tables Entry
208 //  ----------------------------------------------------------------------------
209 
210 struct vlt_slot
211 {
212     uint16_t v_slot;
213     uint16_t v_red;
214     uint16_t v_green;
215     uint16_t v_blue;
216 };
217 
218 //  ----------------------------------------------------------------------------
219 //    IGDS Color Tables Entry
220 //  ----------------------------------------------------------------------------
221 
222 struct igds_slot
223 {
224     uint8_t v_red;
225     uint8_t v_green;
226     uint8_t v_blue;
227 };
228 
229 //  ----------------------------------------------------------------------------
230 //    Header Block One data items
231 //  ----------------------------------------------------------------------------
232 
233 typedef struct {
234     INGR_HeaderType     HeaderType;
235     uint16_t              WordsToFollow;
236     uint16_t              DataTypeCode;
237     uint16_t              ApplicationType;
238     real64              XViewOrigin;
239     real64              YViewOrigin;
240     real64              ZViewOrigin;
241     real64              XViewExtent;
242     real64              YViewExtent;
243     real64              ZViewExtent;
244     real64              TransformationMatrix[16];
245     uint32_t              PixelsPerLine;
246     uint32_t              NumberOfLines;
247     int16_t               DeviceResolution;
248     uint8_t               ScanlineOrientation;
249     uint8_t               ScannableFlag;
250     real64              RotationAngle;
251     real64              SkewAngle;
252     uint16_t              DataTypeModifier;
253     char                DesignFileName[66];
254     char                DataBaseFileName[66];
255     char                ParentGridFileName[66];
256     char                FileDescription[80];
257     INGR_MinMax         Minimum;
258     INGR_MinMax         Maximum;
259     char                Reserved[3];
260     uint8_t               GridFileVersion;
261 } INGR_HeaderOne;
262 
263 //  ----------------------------------------------------------------------------
264 //    Block two field descriptions
265 //  ----------------------------------------------------------------------------
266 
267 typedef struct {
268     uint8_t               Gain;
269     uint8_t               OffsetThreshold;
270     uint8_t               View1;
271     uint8_t               View2;
272     uint8_t               ViewNumber;
273     uint8_t               Reserved2;
274     uint16_t              Reserved3;
275     real64              AspectRatio;
276     uint32_t              CatenatedFilePointer;
277     uint16_t              ColorTableType;
278     uint16_t              Reserved8;
279     uint32_t              NumberOfCTEntries;
280     uint32_t              ApplicationPacketPointer;
281     uint32_t              ApplicationPacketLength;
282     uint16_t              Reserved[110];
283 } INGR_HeaderTwoA;
284 
285 typedef    struct {
286     uint16_t              ApplicationData[128];
287 } INGR_HeaderTwoB;
288 
289 //  ----------------------------------------------------------------------------
290 //    2nd half of Block two plus another block as a static 256 entries color table
291 //  ----------------------------------------------------------------------------
292 
293 typedef    struct {
294     igds_slot           Entry[256];
295 } INGR_ColorTable256;
296 
297 //  ----------------------------------------------------------------------------
298 //    Extra Block(s) for dynamic allocated color table with intensity level entries.
299 //  ----------------------------------------------------------------------------
300 
301 typedef    struct {
302     vlt_slot           *Entry;
303 } INGR_ColorTableVar;
304 
305 //  ----------------------------------------------------------------------------
306 //    Tile Directory Item
307 //  ----------------------------------------------------------------------------
308 
309 typedef     struct {
310     uint32_t              Start;
311     uint32_t              Allocated;
312     uint32_t              Used;
313 } INGR_TileItem;
314 
315 //  ----------------------------------------------------------------------------
316 //    Tile Directory Header
317 //  ----------------------------------------------------------------------------
318 
319 typedef struct INGR_TileHeader {
320     INGR_TileHeader();
321     uint16_t              ApplicationType;
322     uint16_t              SubTypeCode;
323     uint32_t              WordsToFollow;
324     uint16_t              PacketVersion;
325     uint16_t              Identifier;
326     uint16_t              Reserved[2];
327     uint16_t              Properties;
328     uint16_t              DataTypeCode;
329     uint8_t               Reserved2[100];
330     uint32_t              TileSize;
331     uint32_t              Reserved3;
332     INGR_TileItem       First;
333 } INGR_TileHeader;
334 
335 //  ----------------------------------------------------------------------------
336 //    In Memory Tiff holder
337 //  ----------------------------------------------------------------------------
338 
339 typedef     struct {
340     GDALDataset     *poDS;
341     GDALRasterBand  *poBand;
342     const char      *pszFileName;
343 } INGR_VirtualFile;
344 
345 //  ----------------------------------------------------------------------------
346 //    Header Size
347 //  ----------------------------------------------------------------------------
348 
349 /*
350                    Headers Blocks without Color Table
351 
352             +-------------+  -  0    -  Header Block One
353             |     512     |
354             |             |
355             +-------------+  -  512  -  Header Block Two ( First Half )
356             |     256     |
357             +-------------+  -  768  -  Header Block Two ( Second Half )
358             |     256     |             ( Application Data )
359             +-------------+  -  1024 -  Extra Header Info or Image Data
360             |     ...     |
361 
362                    Headers Blocks with IGDS Color Table
363 
364             +-------------+  -  0    -  Header Block One
365             |     512     |
366             |             |
367             +-------------+  -  512  -  Header Block Two ( First Half )
368             |     256     |
369             +-------------+  -  768  -  IGDS 256 Entries
370             |     768     |             Color Table
371             |             |
372             |             |
373             +-------------+  -  1536 -  Extra Header Info or Image Data
374             |     ...     |
375 
376                    Headers Blocks with EnvironV Color Table
377 
378             +-------------+  -  0    -  Header Block One
379             |     512     |
380             |             |
381             +-------------+  -  512  -  Header Block Two
382             |     512     |
383             |             |
384             +-------------+  -  1024 -  EnvironV Color
385             :   n x 512   :             Table
386             :             :
387             :             :
388             +-------------+  ( n+2 )x512  - Extra Header Info or Image Data
389             |     ...     |
390 
391 */
392 
393 #define SIZEOF_HDR1     512
394 #define SIZEOF_HDR2_A   256
395 #define SIZEOF_HDR2_B   256
396 #define SIZEOF_HDR2     512
397 #define SIZEOF_CTAB     768
398 #define SIZEOF_TDIR     140
399 #define SIZEOF_TILE     12
400 #define SIZEOF_JPGAD    12
401 #define SIZEOF_VLTS     8
402 #define SIZEOF_IGDS     3
403 
404 //  ----------------------------------------------------------------------------
405 //                                 Functions
406 //  ----------------------------------------------------------------------------
407 
408 //  ------------------------------------------------------------------
409 //    Copied the DNG OGR Driver
410 //  ------------------------------------------------------------------
411 
412 void   INGR_DGN2IEEEDouble(void * dbl);
413 
414 //  ------------------------------------------------------------------
415 //    Compression, Data Format, Data Type related functions
416 //  ------------------------------------------------------------------
417 
418 uint32_t CPL_STDCALL INGR_GetDataBlockSize( const char *pszFileName,
419                                           uint32_t nBandOffset,
420                                           uint32_t nDataOffset );
421 
422 uint32_t CPL_STDCALL INGR_GetTileDirectory( VSILFILE *fp,
423                                           uint32_t nOffset,
424                                           int nBandXSize,
425                                           int nBandYSize,
426                                           INGR_TileHeader *pTileDir,
427                                           INGR_TileItem **pahTiles);
428 
429 INGR_Format CPL_STDCALL INGR_GetFormat( GDALDataType eType,
430                                         const char *pszCompression );
431 
432 const char * CPL_STDCALL INGR_GetFormatName( uint16_t eCode );
433 
434 GDALDataType CPL_STDCALL INGR_GetDataType( uint16_t eCode );
435 
436 const char * CPL_STDCALL INGR_GetOrientation( uint8_t nIndex );
437 
438 //  ------------------------------------------------------------------
439 //    Transformation Matrix conversion
440 //  ------------------------------------------------------------------
441 
442 void CPL_STDCALL INGR_GetTransMatrix( INGR_HeaderOne *pHeaderOne,
443                                       double *padfGeoTransform );
444 void CPL_STDCALL INGR_SetTransMatrix( real64 *padfMatrix,
445                                       double *padfGeoTransform );
446 
447 //  ------------------------------------------------------------------
448 //    Color Table conversion
449 //  ------------------------------------------------------------------
450 
451 void CPL_STDCALL INGR_GetIGDSColors( VSILFILE *fp,
452                                      uint32_t nOffset,
453                                      uint32_t nEntries,
454                                      GDALColorTable *poColorTable );
455 uint32_t CPL_STDCALL INGR_SetIGDSColors( GDALColorTable *poColorTable,
456                                        INGR_ColorTable256 *pColorTableIGDS );
457 
458 void CPL_STDCALL INGR_GetEnvironVColors( VSILFILE *fp,
459                                          uint32_t nOffset,
460                                          uint32_t nEntries,
461                                          GDALColorTable *poColorTable );
462 uint32_t CPL_STDCALL INGR_SetEnvironColors( GDALColorTable *poColorTable,
463                                           INGR_ColorTableVar *pEnvironTable );
464 
465 //  ------------------------------------------------------------------
466 //    Get, Set Min & Max
467 //  ------------------------------------------------------------------
468 
469 INGR_MinMax CPL_STDCALL INGR_SetMinMax( GDALDataType eType, double dVal );
470 double CPL_STDCALL INGR_GetMinMax( GDALDataType eType, INGR_MinMax hVal );
471 
472 //  ------------------------------------------------------------------
473 //    Run Length decoders
474 //  ------------------------------------------------------------------
475 
476 int CPL_STDCALL
477 INGR_Decode( INGR_Format eFormat,
478              GByte *pabySrcData, GByte *pabyDstData,
479              uint32_t nSrcBytes, uint32_t nBlockSize,
480              uint32_t *pnBytesConsumed );
481 
482 int CPL_STDCALL
483 INGR_DecodeRunLength( const GByte *pabySrcData, GByte *pabyDstData,
484                       uint32_t nSrcBytes, uint32_t nBlockSize,
485                       uint32_t *pnBytesConsumed );
486 
487 int CPL_STDCALL
488 INGR_DecodeRunLengthBitonal( GByte *pabySrcData, GByte *pabyDstData,
489                              uint32_t nSrcBytes, uint32_t nBlockSize,
490                              uint32_t *pnBytesConsumed );
491 
492 int CPL_STDCALL
493 INGR_DecodeRunLengthBitonalTiled( GByte *pabySrcData, GByte *pabyDstData,
494                                   uint32_t nSrcBytes, uint32_t nBlockSize,
495                                   uint32_t *pnBytesConsumed );
496 
497 int CPL_STDCALL
498 INGR_DecodeRunLengthPaletted( GByte *pabySrcData, GByte *pabyDstData,
499                               uint32_t nSrcBytes, uint32_t nBlockSize,
500                               uint32_t *pnBytesConsumed );
501 
502 //  ------------------------------------------------------------------
503 //    GeoTiff in memory helper
504 //  ------------------------------------------------------------------
505 
506 #include "tifvsi.h"
507 
508 INGR_VirtualFile CPL_STDCALL INGR_CreateVirtualFile( const char *pszFilename,
509                                                      INGR_Format eFormat,
510                                                      int nXSize,
511                                                      int nYSize,
512                                                      int nTileSize,
513                                                      int nQuality,
514                                                      GByte *pabyBuffer,
515                                                      int nBufferSize,
516                                                      int nBand );
517 
518 void CPL_STDCALL INGR_ReleaseVirtual( INGR_VirtualFile *poTiffMen );
519 
520 int CPL_STDCALL INGR_ReadJpegQuality( VSILFILE *fp,
521                           uint32_t nAppDataOfseet,
522                           uint32_t nSeekLimit );
523 
524 typedef     struct {
525     uint16_t              ApplicationType;
526     uint16_t              SubTypeCode;
527     uint32_t              RemainingLength;
528     uint16_t              PacketVersion;
529     uint16_t              JpegQuality;
530 } INGR_JPEGAppData;
531 
532 //  ------------------------------------------------------------------
533 //    Reverse Bit order for CCITT data
534 //  ------------------------------------------------------------------
535 
536 #define REVERSEBITS(b)            (BitReverseTable[b])
537 #define REVERSEBITSBUFFER(bb, bz)           \
538     int ibb;                                \
539     for( ibb = 0; ibb < bz; ibb++ )         \
540         bb[ibb] = REVERSEBITS( bb[ibb] )
541 
542 //  ------------------------------------------------------------------
543 //    Struct reading helpers
544 //  ------------------------------------------------------------------
545 
BUF2STRC_fct(const GByte * bb,unsigned int & nn,void * pDest,size_t nSize)546 static inline void BUF2STRC_fct( const GByte* bb, unsigned int& nn, void* pDest, size_t nSize )
547 {
548     memcpy( pDest, &bb[nn], nSize );
549     nn += static_cast<unsigned int>(nSize);
550 }
551 
552 #define BUF2STRC(bb, nn, ff)    BUF2STRC_fct(bb, nn, &ff, sizeof(ff))
553 
STRC2BUF_fct(GByte * bb,unsigned int & nn,const void * pSrc,size_t nSize)554 static inline void STRC2BUF_fct( GByte* bb, unsigned int& nn, const void* pSrc, size_t nSize )
555 {
556     memcpy( &bb[nn], pSrc, nSize );
557     nn += static_cast<unsigned int>(nSize);
558 }
559 
560 #define STRC2BUF(bb, nn, ff)    STRC2BUF_fct(bb, nn, &ff, sizeof(ff))
561 
562 //  ------------------------------------------------------------------
563 //    Fix Endianness issues
564 //  ------------------------------------------------------------------
565 
566 void CPL_STDCALL INGR_HeaderOneDiskToMem(INGR_HeaderOne* pHeaderOne, const GByte *pabyBuf);
567 void CPL_STDCALL INGR_HeaderOneMemToDisk(const INGR_HeaderOne* pHeaderOne, GByte *pabyBuf);
568 void CPL_STDCALL INGR_HeaderTwoADiskToMem(INGR_HeaderTwoA* pHeaderTwo, const GByte *pabyBuf);
569 void CPL_STDCALL INGR_HeaderTwoAMemToDisk(const INGR_HeaderTwoA* pHeaderTwo, GByte *pabyBuf);
570 void CPL_STDCALL INGR_TileHeaderDiskToMem(INGR_TileHeader* pTileHeader, const GByte *pabyBuf);
571 void CPL_STDCALL INGR_TileItemDiskToMem(INGR_TileItem* pTileItem, const GByte *pabyBuf);
572 void CPL_STDCALL INGR_JPEGAppDataDiskToMem(INGR_JPEGAppData* pJPEGAppData, const GByte *pabyBuf);
573 
574 #endif
575