forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmfdataset.h
395 lines (348 loc) · 14.5 KB
/
rmfdataset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/******************************************************************************
* $Id$
*
* Project: Raster Matrix Format
* Purpose: Private class declarations for the RMF classes used to read/write
* GIS "Integratsia" raster files (also known as "Panorama" GIS).
* Author: Andrey Kiselev, [email protected]
*
******************************************************************************
* Copyright (c) 2007, Andrey Kiselev <[email protected]>
* Copyright (c) 2023, NextGIS <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef RMFDATASET_H_INCLUDED
#define RMFDATASET_H_INCLUDED
#include <list>
#include "gdal_priv.h"
#include "cpl_worker_thread_pool.h"
#define RMF_HEADER_SIZE 320
#define RMF_EXT_HEADER_SIZE 320
#define RMF_MIN_EXT_HEADER_SIZE (36 + 4)
#define RMF_MAX_EXT_HEADER_SIZE 1000000
#define RMF_COMPRESSION_NONE 0
#define RMF_COMPRESSION_LZW 1
#define RMF_COMPRESSION_JPEG 2
#define RMF_COMPRESSION_DEM 32
enum RMFType
{
RMFT_RSW, // Raster map
RMFT_MTW // Digital elevation model
};
enum RMFVersion
{
RMF_VERSION = 0x0200, // Version for "small" files less than 4 Gb
RMF_VERSION_HUGE = 0x0201 // Version for "huge" files less than 4 Tb. Since
// GIS Panorama v11
};
class RMFDataset;
#define RMF_HUGE_OFFSET_FACTOR 256
#define RMF_JPEG_BAND_COUNT 3
#define RMF_DEM_BAND_COUNT 1
/************************************************************************/
/* RMFHeader */
/************************************************************************/
typedef struct
{
#define RMF_SIGNATURE_SIZE 4
char bySignature[RMF_SIGNATURE_SIZE]; // "RSW" for raster
// map or "MTW" for DEM
GUInt32 iVersion;
GUInt32 nSize; // File size in bytes
GUInt32 nOvrOffset; // Offset to overview
GUInt32 iUserID;
#define RMF_NAME_SIZE 32
GByte byName[RMF_NAME_SIZE];
GUInt32 nBitDepth; // Number of bits per pixel
GUInt32 nHeight; // Image length
GUInt32 nWidth; // Image width
GUInt32 nXTiles; // Number of tiles in line
GUInt32 nYTiles; // Number of tiles in column
GUInt32 nTileHeight;
GUInt32 nTileWidth;
GUInt32 nLastTileHeight;
GUInt32 nLastTileWidth;
GUInt32 nROIOffset;
GUInt32 nROISize;
GUInt32 nClrTblOffset; // Position and size
GUInt32 nClrTblSize; // of the colour table
GUInt32 nTileTblOffset; // Position and size of the
GUInt32 nTileTblSize; // tile offsets/sizes table
GInt32 iMapType;
GInt32 iProjection;
GInt32 iEPSGCode;
double dfScale;
double dfResolution;
double dfPixelSize;
double dfLLX;
double dfLLY;
double dfStdP1;
double dfStdP2;
double dfCenterLong;
double dfCenterLat;
GByte iCompression;
GByte iMaskType;
GByte iMaskStep;
GByte iFrameFlag;
GUInt32 nFlagsTblOffset;
GUInt32 nFlagsTblSize;
GUInt32 nFileSize0;
GUInt32 nFileSize1;
GByte iUnknown;
GByte iGeorefFlag;
GByte iInverse;
GByte iJpegQuality;
#define RMF_INVISIBLE_COLORS_SIZE 32
GByte abyInvisibleColors[RMF_INVISIBLE_COLORS_SIZE];
double adfElevMinMax[2];
double dfNoData;
GUInt32 iElevationUnit;
GByte iElevationType;
GUInt32 nExtHdrOffset;
GUInt32 nExtHdrSize;
} RMFHeader;
/************************************************************************/
/* RMFExtHeader */
/************************************************************************/
typedef struct
{
GInt32 nEllipsoid;
GInt32 nVertDatum;
GInt32 nDatum;
GInt32 nZone;
} RMFExtHeader;
/************************************************************************/
/* RSWFrame */
/************************************************************************/
typedef struct
{
GInt32 nType;
GInt32 nSize;
GInt32 nSubCount;
GInt32 nCoordsSize;
} RSWFrame;
typedef struct
{
GInt32 nX, nY;
} RSWFrameCoord;
/************************************************************************/
/* RMFCompressionJob */
/************************************************************************/
struct RMFCompressionJob
{
RMFDataset *poDS = nullptr;
CPLErr eResult = CE_None;
int nBlockXOff = -1;
int nBlockYOff = -1;
GByte *pabyUncompressedData = nullptr;
size_t nUncompressedBytes = 0;
GByte *pabyCompressedData = nullptr;
size_t nCompressedBytes = 0;
GUInt32 nXSize = 0;
GUInt32 nYSize = 0;
};
/************************************************************************/
/* RMFCompressData */
/************************************************************************/
struct RMFCompressData
{
CPLWorkerThreadPool oThreadPool;
std::vector<RMFCompressionJob> asJobs;
std::list<RMFCompressionJob *> asReadyJobs;
GByte *pabyBuffers = nullptr;
CPLMutex *hReadyJobMutex = nullptr;
CPLMutex *hWriteTileMutex = nullptr;
RMFCompressData(const RMFCompressData &) = delete;
RMFCompressData &operator=(const RMFCompressData &) = delete;
RMFCompressData();
~RMFCompressData();
};
/************************************************************************/
/* RMFTileData */
/************************************************************************/
struct RMFTileData
{
std::vector<GByte> oData;
int nBandsWritten = 0;
};
/************************************************************************/
/* RMFDataset */
/************************************************************************/
class RMFDataset final : public GDALDataset
{
friend class RMFRasterBand;
private:
RMFHeader sHeader;
RMFExtHeader sExtHeader;
RMFType eRMFType;
GUInt32 nXTiles;
GUInt32 nYTiles;
GUInt32 *paiTiles;
GByte *pabyDecompressBuffer;
GByte *pabyCurrentTile;
bool bCurrentTileIsNull;
int nCurrentTileXOff;
int nCurrentTileYOff;
GUInt32 nCurrentTileBytes;
GUInt32 nColorTableSize;
GByte *pabyColorTable;
GDALColorTable *poColorTable;
double adfGeoTransform[6];
OGRSpatialReference m_oSRS{};
char *pszUnitType;
bool bBigEndian;
bool bHeaderDirty;
VSILFILE *fp;
std::shared_ptr<RMFCompressData> poCompressData;
std::map<GUInt32, RMFTileData> oUnfinishedTiles;
CPLErr WriteHeader();
static size_t LZWDecompress(const GByte *, GUInt32, GByte *, GUInt32,
GUInt32, GUInt32);
static size_t LZWCompress(const GByte *, GUInt32, GByte *, GUInt32, GUInt32,
GUInt32, const RMFDataset *);
#ifdef HAVE_LIBJPEG
static size_t JPEGDecompress(const GByte *, GUInt32, GByte *, GUInt32,
GUInt32, GUInt32);
static size_t JPEGCompress(const GByte *, GUInt32, GByte *, GUInt32,
GUInt32, GUInt32, const RMFDataset *);
#endif // HAVE_LIBJPEG
static size_t DEMDecompress(const GByte *, GUInt32, GByte *, GUInt32,
GUInt32, GUInt32);
static size_t DEMCompress(const GByte *, GUInt32, GByte *, GUInt32, GUInt32,
GUInt32, const RMFDataset *);
/*!
Tile decompress callback
pabyIn - input compressed data
nSizeIn - input compressed data size (in bytes)
pabyOut - pointer to uncompressed data
nSizeOut - maximum uncompressed data size
nTileSx - width of uncompressed tile (in pixels)
nTileSy - height of uncompressed tile (in pixels)
Returns: actual uncompressed data size or 0 on error (if nSizeOut is
small returns 0 too).
*/
size_t (*Decompress)(const GByte *pabyIn, GUInt32 nSizeIn, GByte *pabyOut,
GUInt32 nSizeOut, GUInt32 nTileSx, GUInt32 nTileSy);
/*!
Tile compress callback
pabyIn - input uncompressed data
nSizeIn - input uncompressed data size (in bytes)
pabyOut - pointer to compressed data
nSizeOut - maximum compressed data size
nTileSx - width of uncompressed tile (in pixels)
nTileSy - height of uncompressed tile (in pixels)
poDS - pointer to parent dataset
Returns: actual compressed data size or 0 on error (if nSizeOut is
small returns 0 too).
*/
size_t (*Compress)(const GByte *pabyIn, GUInt32 nSizeIn, GByte *pabyOut,
GUInt32 nSizeOut, GUInt32 nTileSx, GUInt32 nTileSy,
const RMFDataset *poDS);
std::vector<RMFDataset *> poOvrDatasets;
vsi_l_offset nHeaderOffset;
RMFDataset *poParentDS;
public:
RMFDataset();
virtual ~RMFDataset();
static int Identify(GDALOpenInfo *poOpenInfo);
static GDALDataset *Open(GDALOpenInfo *);
static RMFDataset *Open(GDALOpenInfo *, RMFDataset *poParentDS,
vsi_l_offset nNextHeaderOffset);
static GDALDataset *Create(const char *, int, int, int, GDALDataType,
char **);
static GDALDataset *Create(const char *, int, int, int, GDALDataType,
char **, RMFDataset *poParentDS,
double dfOvFactor);
virtual CPLErr FlushCache(bool bAtClosing) override;
virtual CPLErr GetGeoTransform(double *padfTransform) override;
virtual CPLErr SetGeoTransform(double *) override;
const OGRSpatialReference *GetSpatialRef() const override;
CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
virtual CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
const int *panOverviewList, int nBandsIn,
const int *panBandList,
GDALProgressFunc pfnProgress,
void *pProgressData,
CSLConstList papszOptions) override;
virtual CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
int nXSize, int nYSize, void *pData, int nBufXSize,
int nBufYSize, GDALDataType eBufType,
int nBandCount, int *panBandMap,
GSpacing nPixelSpace, GSpacing nLineSpace,
GSpacing nBandSpace,
GDALRasterIOExtraArg *psExtraArg) override;
virtual CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
const char *pszDomain = "") override;
virtual CPLErr SetMetadata(char **papszMetadata,
const char *pszDomain = "") override;
vsi_l_offset GetFileOffset(GUInt32 iRMFOffset) const;
GUInt32 GetRMFOffset(vsi_l_offset iFileOffset,
vsi_l_offset *piNewFileOffset) const;
RMFDataset *OpenOverview(RMFDataset *poParentDS, GDALOpenInfo *);
vsi_l_offset GetLastOffset() const;
CPLErr CleanOverviews();
static GByte GetCompressionType(const char *pszCompressName);
int SetupCompression(GDALDataType eType, const char *pszFilename);
static void WriteTileJobFunc(void *pData);
CPLErr InitCompressorData(char **papszParamList);
CPLErr WriteTile(int nBlockXOff, int nBlockYOff, GByte *pabyData,
size_t nBytes, GUInt32 nRawXSize, GUInt32 nRawYSize);
CPLErr WriteRawTile(int nBlockXOff, int nBlockYOff, GByte *pabyData,
size_t nBytes);
CPLErr ReadTile(int nBlockXOff, int nBlockYOff, GByte *pabyData,
size_t nBytes, GUInt32 nRawXSize, GUInt32 nRawYSize,
bool &bNullTile);
void SetupNBits();
};
/************************************************************************/
/* RMFRasterBand */
/************************************************************************/
class RMFRasterBand final : public GDALRasterBand
{
friend class RMFDataset;
private:
GUInt32 nBlockSize;
GUInt32 nBlockBytes;
GUInt32 nLastTileWidth;
GUInt32 nLastTileHeight;
GUInt32 nDataSize;
public:
RMFRasterBand(RMFDataset *, int, GDALDataType);
virtual ~RMFRasterBand();
virtual CPLErr IReadBlock(int, int, void *) override;
virtual CPLErr IWriteBlock(int, int, void *) override;
virtual GDALSuggestedBlockAccessPattern
GetSuggestedBlockAccessPattern() const override;
virtual double GetNoDataValue(int *pbSuccess = nullptr) override;
virtual CPLErr SetNoDataValue(double dfNoData) override;
virtual const char *GetUnitType() override;
virtual GDALColorInterp GetColorInterpretation() override;
virtual GDALColorTable *GetColorTable() override;
virtual CPLErr SetUnitType(const char *) override;
virtual CPLErr SetColorTable(GDALColorTable *) override;
virtual int GetOverviewCount() override;
virtual GDALRasterBand *GetOverview(int i) override;
virtual CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
int nXSize, int nYSize, void *pData, int nBufXSize,
int nBufYSize, GDALDataType eBufType,
GSpacing nPixelSpace, GSpacing nLineSpace,
GDALRasterIOExtraArg *psExtraArg) override;
};
#endif