1 /******************************************************************************
2  * $Id: rmfdataset.h 20996 2010-10-28 18:38:15Z rouault $
3  *
4  * Project:  Raster Matrix Format
5  * Purpose:  Private class declarations for the RMF classes used to read/write
6  *           GIS "Integratsia" raster files (also known as "Panorama" GIS).
7  * Author:   Andrey Kiselev, dron@ak4719.spb.edu
8  *
9  ******************************************************************************
10  * Copyright (c) 2007, Andrey Kiselev <dron@ak4719.spb.edu>
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 #include "gdal_priv.h"
32 
33 #define RMF_HEADER_SIZE         320
34 #define RMF_EXT_HEADER_SIZE     320
35 
36 #define RMF_COMPRESSION_NONE    0
37 #define RMF_COMPRESSION_LZW     1
38 #define RMF_COMPRESSION_DEM     32
39 
40 enum RMFType
41 {
42     RMFT_RSW,       // Raster map
43     RMFT_MTW        // Digital elevation model
44 };
45 
46 /************************************************************************/
47 /*                            RMFHeader                                 */
48 /************************************************************************/
49 
50 typedef struct
51 {
52 #define RMF_SIGNATURE_SIZE 4
53     char        bySignature[RMF_SIGNATURE_SIZE];// "RSW" for raster
54                                                 // map or "MTW" for DEM
55     GUInt32     iVersion;
56     GUInt32     nSize;                          // File size in bytes
57     GUInt32     nOvrOffset;                     // Offset to overview
58     GUInt32     iUserID;
59 #define RMF_NAME_SIZE 32
60     GByte       byName[RMF_NAME_SIZE];
61     GUInt32     nBitDepth;                      // Number of bits per pixel
62     GUInt32     nHeight;                        // Image length
63     GUInt32     nWidth;                         // Image width
64     GUInt32     nXTiles;                        // Number of tiles in line
65     GUInt32     nYTiles;                        // Number of tiles in column
66     GUInt32     nTileHeight;
67     GUInt32     nTileWidth;
68     GUInt32     nLastTileHeight;
69     GUInt32     nLastTileWidth;
70     GUInt32     nROIOffset;
71     GUInt32     nROISize;
72     GUInt32     nClrTblOffset;                  // Position and size
73     GUInt32     nClrTblSize;                    // of the colour table
74     GUInt32     nTileTblOffset;                 // Position and size of the
75     GUInt32     nTileTblSize;                   // tile offsets/sizes table
76     GInt32      iMapType;
77     GInt32      iProjection;
78     double      dfScale;
79     double      dfResolution;
80     double      dfPixelSize;
81     double      dfLLX;
82     double      dfLLY;
83     double      dfStdP1;
84     double      dfStdP2;
85     double      dfCenterLong;
86     double      dfCenterLat;
87     GByte       iCompression;
88     GByte       iMaskType;
89     GByte       iMaskStep;
90     GByte       iFrameFlag;
91     GUInt32     nFlagsTblOffset;
92     GUInt32     nFlagsTblSize;
93     GUInt32     nFileSize0;
94     GUInt32     nFileSize1;
95     GByte       iUnknown;
96     GByte       iGeorefFlag;
97     GByte       iInverse;
98 #define RMF_INVISIBLE_COLORS_SIZE 32
99     GByte       abyInvisibleColors[RMF_INVISIBLE_COLORS_SIZE];
100     double      adfElevMinMax[2];
101     double      dfNoData;
102     GUInt32     iElevationUnit;
103     GByte       iElevationType;
104     GUInt32     nExtHdrOffset;
105     GUInt32     nExtHdrSize;
106 } RMFHeader;
107 
108 /************************************************************************/
109 /*                            RMFExtHeader                              */
110 /************************************************************************/
111 
112 typedef struct
113 {
114     GInt32      nEllipsoid;
115     GInt32      nDatum;
116     GInt32      nZone;
117 } RMFExtHeader;
118 
119 /************************************************************************/
120 /*                              RMFDataset                              */
121 /************************************************************************/
122 
123 class RMFDataset : public GDALDataset
124 {
125     friend class RMFRasterBand;
126 
127     RMFHeader       sHeader;
128     RMFExtHeader    sExtHeader;
129     RMFType         eRMFType;
130     GUInt32         nXTiles;
131     GUInt32         nYTiles;
132     GUInt32         *paiTiles;
133 
134     GUInt32         nColorTableSize;
135     GByte           *pabyColorTable;
136     GDALColorTable  *poColorTable;
137     double          adfGeoTransform[6];
138     char            *pszProjection;
139 
140     char            *pszUnitType;
141 
142     int             bBigEndian;
143     int             bHeaderDirty;
144 
145     const char      *pszFilename;
146     VSILFILE        *fp;
147 
148     CPLErr          WriteHeader();
149     static int      LZWDecompress( const GByte*, GUInt32, GByte*, GUInt32 );
150     static int      DEMDecompress( const GByte*, GUInt32, GByte*, GUInt32 );
151    int             (*Decompress)( const GByte*, GUInt32, GByte*, GUInt32 );
152 
153   public:
154                 RMFDataset();
155                 ~RMFDataset();
156 
157     static int          Identify( GDALOpenInfo * poOpenInfo );
158     static GDALDataset  *Open( GDALOpenInfo * );
159     static GDALDataset  *Create( const char *, int, int, int,
160                                  GDALDataType, char ** );
161     virtual void        FlushCache( void );
162 
163     virtual CPLErr      GetGeoTransform( double * padfTransform );
164     virtual CPLErr      SetGeoTransform( double * );
165     virtual const char  *GetProjectionRef();
166     virtual CPLErr      SetProjection( const char * );
167 };
168 
169 /************************************************************************/
170 /*                            RMFRasterBand                             */
171 /************************************************************************/
172 
173 class RMFRasterBand : public GDALRasterBand
174 {
175     friend class RMFDataset;
176 
177   private:
178 
179     GUInt32     nBytesPerPixel;
180     GUInt32     nBlockSize, nBlockBytes;
181     GUInt32     nLastTileXBytes, nLastTileHeight;
182     GUInt32     nDataSize;
183 
184     CPLErr   ReadBuffer( GByte *, GUInt32 ) const;
185 
186   public:
187 
188                 RMFRasterBand( RMFDataset *, int, GDALDataType );
189                 ~RMFRasterBand();
190 
191     virtual CPLErr          IReadBlock( int, int, void * );
192     virtual CPLErr          IWriteBlock( int, int, void * );
193     virtual const char      *GetUnitType();
194     virtual GDALColorInterp GetColorInterpretation();
195     virtual GDALColorTable  *GetColorTable();
196     virtual CPLErr          SetUnitType(const char *);
197     virtual CPLErr          SetColorTable( GDALColorTable * );
198 };
199 
200