Coverage Report

Created: 2025-06-09 07:07

/src/gdal/frmts/rmf/rmfdataset.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Project:  Raster Matrix Format
4
 * Purpose:  Read/write raster files used in GIS "Integratsia"
5
 *           (also known as "Panorama" GIS).
6
 * Author:   Andrey Kiselev, dron@ak4719.spb.edu
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2005, Andrey Kiselev <dron@ak4719.spb.edu>
10
 * Copyright (c) 2007-2012, Even Rouault <even dot rouault at spatialys.com>
11
 * Copyright (c) 2023, NextGIS <info@nextgis.com>
12
 *
13
 * SPDX-License-Identifier: MIT
14
 ****************************************************************************/
15
#include <algorithm>
16
#include <array>
17
#include <limits>
18
19
#include "cpl_string.h"
20
#include "gdal_frmts.h"
21
#include "ogr_spatialref.h"
22
23
#include "rmfdataset.h"
24
25
#include "cpl_safemaths.hpp"
26
27
constexpr int RMF_DEFAULT_BLOCKXSIZE = 256;
28
constexpr int RMF_DEFAULT_BLOCKYSIZE = 256;
29
30
static const char RMF_SigRSW[] = {'R', 'S', 'W', '\0'};
31
static const char RMF_SigRSW_BE[] = {'\0', 'W', 'S', 'R'};
32
static const char RMF_SigMTW[] = {'M', 'T', 'W', '\0'};
33
34
static const char RMF_UnitsEmpty[] = "";
35
static const char RMF_UnitsM[] = "m";
36
static const char RMF_UnitsCM[] = "cm";
37
static const char RMF_UnitsDM[] = "dm";
38
static const char RMF_UnitsMM[] = "mm";
39
40
constexpr double RMF_DEFAULT_SCALE = 10000.0;
41
constexpr double RMF_DEFAULT_RESOLUTION = 100.0;
42
43
constexpr const char *MD_VERSION_KEY = "VERSION";
44
constexpr const char *MD_NAME_KEY = "NAME";
45
constexpr const char *MD_SCALE_KEY = "SCALE";
46
constexpr const char *MD_FRAME_KEY = "FRAME";
47
48
constexpr const char *MD_MATH_BASE_MAP_TYPE_KEY = "MATH_BASE.Map type";
49
constexpr const char *MD_MATH_BASE_PROJECTION_KEY = "MATH_BASE.Projection";
50
51
constexpr int nMaxFramePointCount = 2048;
52
constexpr GInt32 nPolygonType =
53
    2147385342;  // 2147385342 magic number for polygon
54
55
/* -------------------------------------------------------------------- */
56
/*  Note: Due to the fact that in the early versions of RMF             */
57
/*  format the field of the iEPSGCode was marked as a 'reserved',       */
58
/*  in the header on its place in many cases garbage values were written.*/
59
/*  Most of them can be weeded out by the minimum EPSG code value.      */
60
/*                                                                      */
61
/*  see: Surveying and Positioning Guidance Note Number 7, part 1       */
62
/*       Using the EPSG Geodetic Parameter Dataset p. 22                */
63
/*       http://www.epsg.org/Portals/0/373-07-1.pdf                     */
64
/* -------------------------------------------------------------------- */
65
constexpr GInt32 RMF_EPSG_MIN_CODE = 1024;
66
67
static char *RMFUnitTypeToStr(GUInt32 iElevationUnit)
68
0
{
69
0
    switch (iElevationUnit)
70
0
    {
71
0
        case 0:
72
0
            return CPLStrdup(RMF_UnitsM);
73
0
        case 1:
74
0
            return CPLStrdup(RMF_UnitsDM);
75
0
        case 2:
76
0
            return CPLStrdup(RMF_UnitsCM);
77
0
        case 3:
78
0
            return CPLStrdup(RMF_UnitsMM);
79
0
        default:
80
0
            return CPLStrdup(RMF_UnitsEmpty);
81
0
    }
82
0
}
83
84
static GUInt32 RMFStrToUnitType(const char *pszUnit, int *pbSuccess = nullptr)
85
0
{
86
0
    if (pbSuccess != nullptr)
87
0
    {
88
0
        *pbSuccess = TRUE;
89
0
    }
90
0
    if (EQUAL(pszUnit, RMF_UnitsM))
91
0
        return 0;
92
0
    else if (EQUAL(pszUnit, RMF_UnitsDM))
93
0
        return 1;
94
0
    else if (EQUAL(pszUnit, RMF_UnitsCM))
95
0
        return 2;
96
0
    else if (EQUAL(pszUnit, RMF_UnitsMM))
97
0
        return 3;
98
0
    else
99
0
    {
100
        // There is no 'invalid unit' in RMF format. So meter is default...
101
0
        if (pbSuccess != nullptr)
102
0
        {
103
0
            *pbSuccess = FALSE;
104
0
        }
105
0
        return 0;
106
0
    }
107
0
}
108
109
/************************************************************************/
110
/* ==================================================================== */
111
/*                            RMFRasterBand                             */
112
/* ==================================================================== */
113
/************************************************************************/
114
115
/************************************************************************/
116
/*                           RMFRasterBand()                            */
117
/************************************************************************/
118
119
RMFRasterBand::RMFRasterBand(RMFDataset *poDSIn, int nBandIn,
120
                             GDALDataType eType)
121
474
    : nLastTileWidth(poDSIn->GetRasterXSize() % poDSIn->sHeader.nTileWidth),
122
474
      nLastTileHeight(poDSIn->GetRasterYSize() % poDSIn->sHeader.nTileHeight),
123
474
      nDataSize(GDALGetDataTypeSizeBytes(eType))
124
474
{
125
474
    poDS = poDSIn;
126
474
    nBand = nBandIn;
127
128
474
    eDataType = eType;
129
474
    nBlockXSize = poDSIn->sHeader.nTileWidth;
130
474
    nBlockYSize = poDSIn->sHeader.nTileHeight;
131
474
    nBlockSize = nBlockXSize * nBlockYSize;
132
474
    nBlockBytes = nBlockSize * nDataSize;
133
134
#ifdef DEBUG
135
    CPLDebug("RMF",
136
             "Band %d: tile width is %d, tile height is %d, "
137
             " last tile width %u, last tile height %u, "
138
             "bytes per pixel is %d, data type size is %d",
139
             nBand, nBlockXSize, nBlockYSize, nLastTileWidth, nLastTileHeight,
140
             poDSIn->sHeader.nBitDepth / 8, nDataSize);
141
#endif
142
474
}
143
144
/************************************************************************/
145
/*                           ~RMFRasterBand()                           */
146
/************************************************************************/
147
148
RMFRasterBand::~RMFRasterBand()
149
474
{
150
474
}
151
152
/************************************************************************/
153
/*                             IReadBlock()                             */
154
/************************************************************************/
155
156
CPLErr RMFRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
157
0
{
158
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
159
160
0
    CPLAssert(poGDS != nullptr && nBlockXOff >= 0 && nBlockYOff >= 0 &&
161
0
              pImage != nullptr);
162
163
0
    memset(pImage, 0, nBlockBytes);
164
165
0
    GUInt32 nRawXSize = nBlockXSize;
166
0
    GUInt32 nRawYSize = nBlockYSize;
167
168
0
    if (nLastTileWidth &&
169
0
        static_cast<GUInt32>(nBlockXOff) == poGDS->nXTiles - 1)
170
0
        nRawXSize = nLastTileWidth;
171
172
0
    if (nLastTileHeight &&
173
0
        static_cast<GUInt32>(nBlockYOff) == poGDS->nYTiles - 1)
174
0
        nRawYSize = nLastTileHeight;
175
176
0
    GUInt32 nRawBytes = nRawXSize * nRawYSize * poGDS->sHeader.nBitDepth / 8;
177
178
    // Direct read optimization
179
0
    if (poGDS->nBands == 1 && poGDS->sHeader.nBitDepth >= 8 &&
180
0
        nRawXSize == static_cast<GUInt32>(nBlockXSize) &&
181
0
        nRawYSize == static_cast<GUInt32>(nBlockYSize))
182
0
    {
183
0
        bool bNullTile = false;
184
0
        if (CE_None != poGDS->ReadTile(nBlockXOff, nBlockYOff,
185
0
                                       reinterpret_cast<GByte *>(pImage),
186
0
                                       nRawBytes, nRawXSize, nRawYSize,
187
0
                                       bNullTile))
188
0
        {
189
0
            CPLError(CE_Failure, CPLE_FileIO,
190
0
                     "Failed to read tile xOff %d yOff %d", nBlockXOff,
191
0
                     nBlockYOff);
192
0
            return CE_Failure;
193
0
        }
194
0
        if (bNullTile)
195
0
        {
196
0
            const int nChunkSize =
197
0
                std::max(1, GDALGetDataTypeSizeBytes(eDataType));
198
0
            const GPtrDiff_t nWords =
199
0
                static_cast<GPtrDiff_t>(nBlockXSize) * nBlockYSize;
200
0
            GDALCopyWords64(&poGDS->sHeader.dfNoData, GDT_Float64, 0, pImage,
201
0
                            eDataType, nChunkSize, nWords);
202
0
        }
203
0
        return CE_None;
204
0
    }
205
#ifdef DEBUG
206
    CPLDebug("RMF", "IReadBlock nBand %d, RawSize [%d, %d], Bits %u", nBand,
207
             nRawXSize, nRawYSize, poGDS->sHeader.nBitDepth);
208
#endif  // DEBUG
209
0
    if (poGDS->pabyCurrentTile == nullptr ||
210
0
        poGDS->nCurrentTileXOff != nBlockXOff ||
211
0
        poGDS->nCurrentTileYOff != nBlockYOff ||
212
0
        poGDS->nCurrentTileBytes != nRawBytes)
213
0
    {
214
0
        if (poGDS->pabyCurrentTile == nullptr)
215
0
        {
216
0
            GUInt32 nMaxTileBytes = poGDS->sHeader.nTileWidth *
217
0
                                    poGDS->sHeader.nTileHeight *
218
0
                                    poGDS->sHeader.nBitDepth / 8;
219
0
            poGDS->pabyCurrentTile = reinterpret_cast<GByte *>(
220
0
                VSIMalloc(std::max(1U, nMaxTileBytes)));
221
0
            if (!poGDS->pabyCurrentTile)
222
0
            {
223
0
                CPLError(CE_Failure, CPLE_OutOfMemory,
224
0
                         "Can't allocate tile block of size %lu.\n%s",
225
0
                         static_cast<unsigned long>(nMaxTileBytes),
226
0
                         VSIStrerror(errno));
227
0
                poGDS->nCurrentTileBytes = 0;
228
0
                return CE_Failure;
229
0
            }
230
0
        }
231
232
0
        poGDS->nCurrentTileXOff = nBlockXOff;
233
0
        poGDS->nCurrentTileYOff = nBlockYOff;
234
0
        poGDS->nCurrentTileBytes = nRawBytes;
235
236
0
        if (CE_None != poGDS->ReadTile(nBlockXOff, nBlockYOff,
237
0
                                       poGDS->pabyCurrentTile, nRawBytes,
238
0
                                       nRawXSize, nRawYSize,
239
0
                                       poGDS->bCurrentTileIsNull))
240
0
        {
241
0
            CPLError(CE_Failure, CPLE_FileIO,
242
0
                     "Failed to read tile xOff %d yOff %d", nBlockXOff,
243
0
                     nBlockYOff);
244
0
            poGDS->nCurrentTileBytes = 0;
245
0
            return CE_Failure;
246
0
        }
247
0
    }
248
249
    /* -------------------------------------------------------------------- */
250
    /*  Deinterleave pixels from input buffer.                              */
251
    /* -------------------------------------------------------------------- */
252
253
0
    if (poGDS->bCurrentTileIsNull)
254
0
    {
255
0
        const int nChunkSize = std::max(1, GDALGetDataTypeSizeBytes(eDataType));
256
0
        const GPtrDiff_t nWords =
257
0
            static_cast<GPtrDiff_t>(nBlockXSize) * nBlockYSize;
258
0
        GDALCopyWords64(&poGDS->sHeader.dfNoData, GDT_Float64, 0, pImage,
259
0
                        eDataType, nChunkSize, nWords);
260
0
        return CE_None;
261
0
    }
262
0
    else if ((poGDS->eRMFType == RMFT_RSW &&
263
0
              (poGDS->sHeader.nBitDepth == 8 ||
264
0
               poGDS->sHeader.nBitDepth == 24 ||
265
0
               poGDS->sHeader.nBitDepth == 32)) ||
266
0
             (poGDS->eRMFType == RMFT_MTW))
267
0
    {
268
0
        const size_t nTilePixelSize = poGDS->sHeader.nBitDepth / 8;
269
0
        const size_t nTileLineSize = nTilePixelSize * nRawXSize;
270
0
        const size_t nBlockLineSize =
271
0
            static_cast<size_t>(nDataSize) * nBlockXSize;
272
0
        int iDstBand = (poGDS->nBands - nBand);
273
0
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
274
0
        {
275
0
            GByte *pabySrc;
276
0
            GByte *pabyDst;
277
0
            pabySrc = poGDS->pabyCurrentTile + iLine * nTileLineSize +
278
0
                      iDstBand * nDataSize;
279
0
            pabyDst =
280
0
                reinterpret_cast<GByte *>(pImage) + iLine * nBlockLineSize;
281
0
            GDALCopyWords(pabySrc, eDataType, static_cast<int>(nTilePixelSize),
282
0
                          pabyDst, eDataType, static_cast<int>(nDataSize),
283
0
                          nRawXSize);
284
0
        }
285
0
        return CE_None;
286
0
    }
287
0
    else if (poGDS->eRMFType == RMFT_RSW && poGDS->sHeader.nBitDepth == 16 &&
288
0
             poGDS->nBands == 3)
289
0
    {
290
0
        const size_t nTilePixelBits = poGDS->sHeader.nBitDepth;
291
0
        const size_t nTileLineSize = nTilePixelBits * nRawXSize / 8;
292
0
        const size_t nBlockLineSize =
293
0
            static_cast<size_t>(nDataSize) * nBlockXSize;
294
295
0
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
296
0
        {
297
0
            GUInt16 *pabySrc;
298
0
            GByte *pabyDst;
299
0
            pabySrc = reinterpret_cast<GUInt16 *>(poGDS->pabyCurrentTile +
300
0
                                                  iLine * nTileLineSize);
301
0
            pabyDst =
302
0
                reinterpret_cast<GByte *>(pImage) + iLine * nBlockLineSize;
303
304
0
            for (GUInt32 i = 0; i < nRawXSize; i++)
305
0
            {
306
0
                switch (nBand)
307
0
                {
308
0
                    case 1:
309
0
                        pabyDst[i] =
310
0
                            static_cast<GByte>((pabySrc[i] & 0x7c00) >> 7);
311
0
                        break;
312
0
                    case 2:
313
0
                        pabyDst[i] =
314
0
                            static_cast<GByte>((pabySrc[i] & 0x03e0) >> 2);
315
0
                        break;
316
0
                    case 3:
317
0
                        pabyDst[i] =
318
0
                            static_cast<GByte>((pabySrc[i] & 0x1F) << 3);
319
0
                        break;
320
0
                    default:
321
0
                        break;
322
0
                }
323
0
            }
324
0
        }
325
0
        return CE_None;
326
0
    }
327
0
    else if (poGDS->eRMFType == RMFT_RSW && poGDS->nBands == 1 &&
328
0
             poGDS->sHeader.nBitDepth == 4)
329
0
    {
330
0
        if (poGDS->nCurrentTileBytes != (nBlockSize + 1) / 2)
331
0
        {
332
0
            CPLError(CE_Failure, CPLE_AppDefined,
333
0
                     "Tile has %d bytes, %d were expected",
334
0
                     poGDS->nCurrentTileBytes, (nBlockSize + 1) / 2);
335
0
            return CE_Failure;
336
0
        }
337
338
0
        const size_t nTilePixelBits = poGDS->sHeader.nBitDepth;
339
0
        const size_t nTileLineSize = nTilePixelBits * nRawXSize / 8;
340
0
        const size_t nBlockLineSize =
341
0
            static_cast<size_t>(nDataSize) * nBlockXSize;
342
343
0
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
344
0
        {
345
0
            GByte *pabySrc;
346
0
            GByte *pabyDst;
347
0
            pabySrc = poGDS->pabyCurrentTile + iLine * nTileLineSize;
348
0
            pabyDst =
349
0
                reinterpret_cast<GByte *>(pImage) + iLine * nBlockLineSize;
350
0
            for (GUInt32 i = 0; i < nRawXSize; ++i)
351
0
            {
352
0
                if (i & 0x01)
353
0
                    pabyDst[i] = (*pabySrc++ & 0xF0) >> 4;
354
0
                else
355
0
                    pabyDst[i] = *pabySrc & 0x0F;
356
0
            }
357
0
        }
358
0
        return CE_None;
359
0
    }
360
0
    else if (poGDS->eRMFType == RMFT_RSW && poGDS->nBands == 1 &&
361
0
             poGDS->sHeader.nBitDepth == 1)
362
0
    {
363
0
        if (poGDS->nCurrentTileBytes != (nBlockSize + 7) / 8)
364
0
        {
365
0
            CPLError(CE_Failure, CPLE_AppDefined,
366
0
                     "Tile has %d bytes, %d were expected",
367
0
                     poGDS->nCurrentTileBytes, (nBlockSize + 7) / 8);
368
0
            return CE_Failure;
369
0
        }
370
371
0
        const size_t nTilePixelBits = poGDS->sHeader.nBitDepth;
372
0
        const size_t nTileLineSize = nTilePixelBits * nRawXSize / 8;
373
0
        const size_t nBlockLineSize =
374
0
            static_cast<size_t>(nDataSize) * nBlockXSize;
375
376
0
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
377
0
        {
378
0
            GByte *pabySrc;
379
0
            GByte *pabyDst;
380
0
            pabySrc = poGDS->pabyCurrentTile + iLine * nTileLineSize;
381
0
            pabyDst =
382
0
                reinterpret_cast<GByte *>(pImage) + iLine * nBlockLineSize;
383
384
0
            for (GUInt32 i = 0; i < nRawXSize; ++i)
385
0
            {
386
0
                switch (i & 0x7)
387
0
                {
388
0
                    case 0:
389
0
                        pabyDst[i] = (*pabySrc & 0x80) >> 7;
390
0
                        break;
391
0
                    case 1:
392
0
                        pabyDst[i] = (*pabySrc & 0x40) >> 6;
393
0
                        break;
394
0
                    case 2:
395
0
                        pabyDst[i] = (*pabySrc & 0x20) >> 5;
396
0
                        break;
397
0
                    case 3:
398
0
                        pabyDst[i] = (*pabySrc & 0x10) >> 4;
399
0
                        break;
400
0
                    case 4:
401
0
                        pabyDst[i] = (*pabySrc & 0x08) >> 3;
402
0
                        break;
403
0
                    case 5:
404
0
                        pabyDst[i] = (*pabySrc & 0x04) >> 2;
405
0
                        break;
406
0
                    case 6:
407
0
                        pabyDst[i] = (*pabySrc & 0x02) >> 1;
408
0
                        break;
409
0
                    case 7:
410
0
                        pabyDst[i] = *pabySrc++ & 0x01;
411
0
                        break;
412
0
                    default:
413
0
                        break;
414
0
                }
415
0
            }
416
0
        }
417
0
        return CE_None;
418
0
    }
419
420
0
    CPLError(CE_Failure, CPLE_AppDefined,
421
0
             "Invalid block data type. BitDepth %d, nBands %d",
422
0
             static_cast<int>(poGDS->sHeader.nBitDepth), poGDS->nBands);
423
424
0
    return CE_Failure;
425
0
}
426
427
/************************************************************************/
428
/*                            IWriteBlock()                             */
429
/************************************************************************/
430
431
CPLErr RMFRasterBand::IWriteBlock(int nBlockXOff, int nBlockYOff, void *pImage)
432
0
{
433
0
    CPLAssert(poDS != nullptr && nBlockXOff >= 0 && nBlockYOff >= 0 &&
434
0
              pImage != nullptr);
435
436
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
437
438
    // First drop current tile read by IReadBlock
439
0
    poGDS->nCurrentTileBytes = 0;
440
441
0
    GUInt32 nRawXSize = nBlockXSize;
442
0
    GUInt32 nRawYSize = nBlockYSize;
443
444
0
    if (nLastTileWidth &&
445
0
        static_cast<GUInt32>(nBlockXOff) == poGDS->nXTiles - 1)
446
0
        nRawXSize = nLastTileWidth;
447
448
0
    if (nLastTileHeight &&
449
0
        static_cast<GUInt32>(nBlockYOff) == poGDS->nYTiles - 1)
450
0
        nRawYSize = nLastTileHeight;
451
452
0
    const size_t nTilePixelSize =
453
0
        static_cast<size_t>(nDataSize) * poGDS->nBands;
454
0
    const size_t nTileLineSize = nTilePixelSize * nRawXSize;
455
0
    const size_t nTileSize = nTileLineSize * nRawYSize;
456
0
    const size_t nBlockLineSize = static_cast<size_t>(nDataSize) * nBlockXSize;
457
458
#ifdef DEBUG
459
    CPLDebug(
460
        "RMF",
461
        "IWriteBlock BlockSize [%d, %d], RawSize [%d, %d], size %d, nBand %d",
462
        nBlockXSize, nBlockYSize, nRawXSize, nRawYSize,
463
        static_cast<int>(nTileSize), nBand);
464
#endif  // DEBUG
465
466
0
    if (poGDS->nBands == 1 && nRawXSize == static_cast<GUInt32>(nBlockXSize) &&
467
0
        nRawYSize == static_cast<GUInt32>(nBlockYSize))
468
0
    {  // Immediate write
469
0
        return poGDS->WriteTile(
470
0
            nBlockXOff, nBlockYOff, reinterpret_cast<GByte *>(pImage),
471
0
            static_cast<size_t>(nRawXSize) * nRawYSize * nDataSize, nRawXSize,
472
0
            nRawYSize);
473
0
    }
474
0
    else
475
0
    {  // Try to construct full tile in memory and write later
476
0
        const GUInt32 nTile = nBlockYOff * poGDS->nXTiles + nBlockXOff;
477
478
        // Find tile
479
0
        auto poTile(poGDS->oUnfinishedTiles.find(nTile));
480
0
        if (poTile == poGDS->oUnfinishedTiles.end())
481
0
        {
482
0
            RMFTileData oTile;
483
0
            oTile.oData.resize(nTileSize);
484
            // If not found, but exist on disk than read it
485
0
            if (poGDS->paiTiles[2 * nTile + 1])
486
0
            {
487
0
                CPLErr eRes;
488
0
                bool bNullTile = false;
489
0
                eRes =
490
0
                    poGDS->ReadTile(nBlockXOff, nBlockYOff, oTile.oData.data(),
491
0
                                    nTileSize, nRawXSize, nRawYSize, bNullTile);
492
0
                if (eRes != CE_None)
493
0
                {
494
0
                    CPLError(CE_Failure, CPLE_FileIO,
495
0
                             "Can't read block with offset [%d, %d]",
496
0
                             nBlockXOff, nBlockYOff);
497
0
                    return eRes;
498
0
                }
499
0
            }
500
0
            poTile = poGDS->oUnfinishedTiles.insert(
501
0
                poGDS->oUnfinishedTiles.end(), std::make_pair(nTile, oTile));
502
0
        }
503
504
0
        GByte *pabyTileData = poTile->second.oData.data();
505
506
        // Copy new data to a tile
507
0
        int iDstBand = (poGDS->nBands - nBand);
508
0
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
509
0
        {
510
0
            const GByte *pabySrc;
511
0
            GByte *pabyDst;
512
0
            pabySrc = reinterpret_cast<const GByte *>(pImage) +
513
0
                      iLine * nBlockLineSize;
514
0
            pabyDst =
515
0
                pabyTileData + iLine * nTileLineSize + iDstBand * nDataSize;
516
0
            GDALCopyWords(pabySrc, eDataType, static_cast<int>(nDataSize),
517
0
                          pabyDst, eDataType, static_cast<int>(nTilePixelSize),
518
0
                          nRawXSize);
519
0
        }
520
0
        ++poTile->second.nBandsWritten;
521
522
        // Write to disk if tile is finished
523
0
        if (poTile->second.nBandsWritten == poGDS->nBands)
524
0
        {
525
0
            poGDS->WriteTile(nBlockXOff, nBlockYOff, pabyTileData, nTileSize,
526
0
                             nRawXSize, nRawYSize);
527
0
            poGDS->oUnfinishedTiles.erase(poTile);
528
0
        }
529
#ifdef DEBUG
530
        CPLDebug("RMF", "poGDS->oUnfinishedTiles.size() %d",
531
                 static_cast<int>(poGDS->oUnfinishedTiles.size()));
532
#endif  // DEBUG
533
0
    }
534
535
0
    return CE_None;
536
0
}
537
538
/************************************************************************/
539
/*                          GetNoDataValue()                            */
540
/************************************************************************/
541
542
double RMFRasterBand::GetNoDataValue(int *pbSuccess)
543
544
0
{
545
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
546
547
0
    if (pbSuccess)
548
0
        *pbSuccess = TRUE;
549
550
0
    return poGDS->sHeader.dfNoData;
551
0
}
552
553
CPLErr RMFRasterBand::SetNoDataValue(double dfNoData)
554
0
{
555
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
556
557
0
    poGDS->sHeader.dfNoData = dfNoData;
558
0
    poGDS->bHeaderDirty = true;
559
560
0
    return CE_None;
561
0
}
562
563
/************************************************************************/
564
/*                            GetUnitType()                             */
565
/************************************************************************/
566
567
const char *RMFRasterBand::GetUnitType()
568
569
0
{
570
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
571
572
0
    return poGDS->pszUnitType;
573
0
}
574
575
/************************************************************************/
576
/*                            SetUnitType()                             */
577
/************************************************************************/
578
579
CPLErr RMFRasterBand::SetUnitType(const char *pszNewValue)
580
581
0
{
582
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
583
0
    int bSuccess = FALSE;
584
0
    int iNewUnit = RMFStrToUnitType(pszNewValue, &bSuccess);
585
586
0
    if (bSuccess)
587
0
    {
588
0
        CPLFree(poGDS->pszUnitType);
589
0
        poGDS->pszUnitType = CPLStrdup(pszNewValue);
590
0
        poGDS->sHeader.iElevationUnit = iNewUnit;
591
0
        poGDS->bHeaderDirty = true;
592
0
        return CE_None;
593
0
    }
594
0
    else
595
0
    {
596
0
        CPLError(CE_Warning, CPLE_NotSupported,
597
0
                 "RMF driver does not support '%s' elevation units. "
598
0
                 "Possible values are: m, dm, cm, mm.",
599
0
                 pszNewValue);
600
0
        return CE_Failure;
601
0
    }
602
0
}
603
604
/************************************************************************/
605
/*                           GetColorTable()                            */
606
/************************************************************************/
607
608
GDALColorTable *RMFRasterBand::GetColorTable()
609
0
{
610
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
611
612
0
    return poGDS->poColorTable;
613
0
}
614
615
/************************************************************************/
616
/*                           SetColorTable()                            */
617
/************************************************************************/
618
619
CPLErr RMFRasterBand::SetColorTable(GDALColorTable *poColorTable)
620
0
{
621
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
622
623
0
    if (poColorTable)
624
0
    {
625
0
        if (poGDS->eRMFType == RMFT_RSW && poGDS->nBands == 1)
626
0
        {
627
0
            if (!poGDS->pabyColorTable)
628
0
                return CE_Failure;
629
630
0
            GDALColorEntry oEntry;
631
0
            for (GUInt32 i = 0; i < poGDS->nColorTableSize; i++)
632
0
            {
633
0
                poColorTable->GetColorEntryAsRGB(i, &oEntry);
634
                // Red
635
0
                poGDS->pabyColorTable[i * 4 + 0] =
636
0
                    static_cast<GByte>(oEntry.c1);
637
                // Green
638
0
                poGDS->pabyColorTable[i * 4 + 1] =
639
0
                    static_cast<GByte>(oEntry.c2);
640
                // Blue
641
0
                poGDS->pabyColorTable[i * 4 + 2] =
642
0
                    static_cast<GByte>(oEntry.c3);
643
0
                poGDS->pabyColorTable[i * 4 + 3] = 0;
644
0
            }
645
646
0
            poGDS->bHeaderDirty = true;
647
0
        }
648
0
        return CE_None;
649
0
    }
650
651
0
    return CE_Failure;
652
0
}
653
654
int RMFRasterBand::GetOverviewCount()
655
0
{
656
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
657
0
    if (poGDS->poOvrDatasets.empty())
658
0
        return GDALRasterBand::GetOverviewCount();
659
0
    else
660
0
        return static_cast<int>(poGDS->poOvrDatasets.size());
661
0
}
662
663
GDALRasterBand *RMFRasterBand::GetOverview(int i)
664
0
{
665
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
666
0
    size_t n = static_cast<size_t>(i);
667
0
    if (poGDS->poOvrDatasets.empty())
668
0
        return GDALRasterBand::GetOverview(i);
669
0
    else
670
0
        return poGDS->poOvrDatasets[n]->GetRasterBand(nBand);
671
0
}
672
673
CPLErr RMFRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
674
                                int nXSize, int nYSize, void *pData,
675
                                int nBufXSize, int nBufYSize,
676
                                GDALDataType eType, GSpacing nPixelSpace,
677
                                GSpacing nLineSpace,
678
                                GDALRasterIOExtraArg *psExtraArg)
679
0
{
680
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
681
682
0
    if (eRWFlag == GF_Read && poGDS->poCompressData != nullptr &&
683
0
        poGDS->poCompressData->oThreadPool.GetThreadCount() > 0)
684
0
    {
685
0
        poGDS->poCompressData->oThreadPool.WaitCompletion();
686
0
    }
687
688
0
    return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
689
0
                                     pData, nBufXSize, nBufYSize, eType,
690
0
                                     nPixelSpace, nLineSpace, psExtraArg);
691
0
}
692
693
/************************************************************************/
694
/*                       GetColorInterpretation()                       */
695
/************************************************************************/
696
697
GDALColorInterp RMFRasterBand::GetColorInterpretation()
698
0
{
699
0
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
700
701
0
    if (poGDS->nBands == 3)
702
0
    {
703
0
        if (nBand == 1)
704
0
            return GCI_RedBand;
705
0
        else if (nBand == 2)
706
0
            return GCI_GreenBand;
707
0
        else if (nBand == 3)
708
0
            return GCI_BlueBand;
709
710
0
        return GCI_Undefined;
711
0
    }
712
713
0
    if (poGDS->eRMFType == RMFT_RSW)
714
0
        return GCI_PaletteIndex;
715
716
0
    return GCI_Undefined;
717
0
}
718
719
/************************************************************************/
720
/* ==================================================================== */
721
/*                              RMFDataset                              */
722
/* ==================================================================== */
723
/************************************************************************/
724
725
/************************************************************************/
726
/*                           RMFDataset()                               */
727
/************************************************************************/
728
729
175
RMFDataset::RMFDataset() : pszUnitType(CPLStrdup(RMF_UnitsEmpty))
730
175
{
731
175
    m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
732
175
    nBands = 0;
733
175
    memset(&sHeader, 0, sizeof(sHeader));
734
175
    memset(&sExtHeader, 0, sizeof(sExtHeader));
735
175
}
736
737
/************************************************************************/
738
/*                            ~RMFDataset()                             */
739
/************************************************************************/
740
741
RMFDataset::~RMFDataset()
742
175
{
743
175
    RMFDataset::FlushCache(true);
744
175
    for (size_t n = 0; n != poOvrDatasets.size(); ++n)
745
0
    {
746
0
        poOvrDatasets[n]->RMFDataset::FlushCache(true);
747
0
    }
748
749
175
    VSIFree(paiTiles);
750
175
    VSIFree(pabyDecompressBuffer);
751
175
    VSIFree(pabyCurrentTile);
752
175
    CPLFree(pszUnitType);
753
175
    CPLFree(pabyColorTable);
754
175
    if (poColorTable != nullptr)
755
0
        delete poColorTable;
756
757
175
    for (size_t n = 0; n != poOvrDatasets.size(); ++n)
758
0
    {
759
0
        GDALClose(poOvrDatasets[n]);
760
0
    }
761
762
175
    if (fp != nullptr && poParentDS == nullptr)
763
175
    {
764
175
        VSIFCloseL(fp);
765
175
    }
766
175
}
767
768
/************************************************************************/
769
/*                          GetGeoTransform()                           */
770
/************************************************************************/
771
772
CPLErr RMFDataset::GetGeoTransform(double *padfTransform)
773
0
{
774
0
    memcpy(padfTransform, adfGeoTransform.data(), sizeof(adfGeoTransform));
775
776
0
    if (sHeader.iGeorefFlag)
777
0
        return CE_None;
778
779
0
    return CE_Failure;
780
0
}
781
782
/************************************************************************/
783
/*                          SetGeoTransform()                           */
784
/************************************************************************/
785
786
CPLErr RMFDataset::SetGeoTransform(double *padfTransform)
787
0
{
788
0
    memcpy(adfGeoTransform.data(), padfTransform, sizeof(adfGeoTransform));
789
0
    sHeader.dfPixelSize = adfGeoTransform[1];
790
0
    if (sHeader.dfPixelSize != 0.0)
791
0
        sHeader.dfResolution = sHeader.dfScale / sHeader.dfPixelSize;
792
0
    sHeader.dfLLX = adfGeoTransform[0];
793
0
    sHeader.dfLLY = adfGeoTransform[3] - nRasterYSize * sHeader.dfPixelSize;
794
0
    sHeader.iGeorefFlag = 1;
795
796
0
    bHeaderDirty = true;
797
798
0
    return CE_None;
799
0
}
800
801
/************************************************************************/
802
/*                          GetSpatialRef()                             */
803
/************************************************************************/
804
805
const OGRSpatialReference *RMFDataset::GetSpatialRef() const
806
807
0
{
808
0
    return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
809
0
}
810
811
/************************************************************************/
812
/*                           SetSpatialRef()                            */
813
/************************************************************************/
814
815
CPLErr RMFDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
816
817
0
{
818
0
    m_oSRS.Clear();
819
0
    if (poSRS)
820
0
        m_oSRS = *poSRS;
821
822
0
    bHeaderDirty = true;
823
824
0
    return CE_None;
825
0
}
826
827
/************************************************************************/
828
/*                           WriteHeader()                              */
829
/************************************************************************/
830
831
CPLErr RMFDataset::WriteHeader()
832
0
{
833
    /* -------------------------------------------------------------------- */
834
    /*  Setup projection.                                                   */
835
    /* -------------------------------------------------------------------- */
836
0
    if (!m_oSRS.IsEmpty())
837
0
    {
838
0
        long iProjection = 0;
839
0
        long iDatum = 0;
840
0
        long iEllips = 0;
841
0
        long iZone = 0;
842
0
        int iVertCS = 0;
843
0
        double adfPrjParams[7] = {};
844
845
0
        m_oSRS.exportToPanorama(&iProjection, &iDatum, &iEllips, &iZone,
846
0
                                adfPrjParams);
847
0
        m_oSRS.exportVertCSToPanorama(&iVertCS);
848
0
        sHeader.iProjection = static_cast<GInt32>(iProjection);
849
0
        sHeader.dfStdP1 = adfPrjParams[0];
850
0
        sHeader.dfStdP2 = adfPrjParams[1];
851
0
        sHeader.dfCenterLat = adfPrjParams[2];
852
0
        sHeader.dfCenterLong = adfPrjParams[3];
853
0
        if (m_oSRS.GetAuthorityName(nullptr) != nullptr &&
854
0
            m_oSRS.GetAuthorityCode(nullptr) != nullptr &&
855
0
            EQUAL(m_oSRS.GetAuthorityName(nullptr), "EPSG"))
856
0
        {
857
0
            sHeader.iEPSGCode = atoi(m_oSRS.GetAuthorityCode(nullptr));
858
0
        }
859
860
0
        sExtHeader.nEllipsoid = static_cast<GInt32>(iEllips);
861
0
        sExtHeader.nDatum = static_cast<GInt32>(iDatum);
862
0
        sExtHeader.nZone = static_cast<GInt32>(iZone);
863
0
        sExtHeader.nVertDatum = static_cast<GInt32>(iVertCS);
864
865
        // Set map type
866
0
        auto pszMapType = GetMetadataItem(MD_MATH_BASE_MAP_TYPE_KEY);
867
0
        if (pszMapType != nullptr)
868
0
        {
869
0
            sHeader.iMapType = static_cast<GInt32>(atoi(pszMapType));
870
0
        }
871
0
    }
872
873
0
#define RMF_WRITE_LONG(ptr, value, offset)                                     \
874
0
    do                                                                         \
875
0
    {                                                                          \
876
0
        GInt32 iLong = CPL_LSBWORD32(value);                                   \
877
0
        memcpy((ptr) + (offset), &iLong, 4);                                   \
878
0
    } while (false);
879
880
0
#define RMF_WRITE_ULONG(ptr, value, offset)                                    \
881
0
    do                                                                         \
882
0
    {                                                                          \
883
0
        GUInt32 iULong = CPL_LSBWORD32(value);                                 \
884
0
        memcpy((ptr) + (offset), &iULong, 4);                                  \
885
0
    } while (false);
886
887
0
#define RMF_WRITE_DOUBLE(ptr, value, offset)                                   \
888
0
    do                                                                         \
889
0
    {                                                                          \
890
0
        double dfDouble = (value);                                             \
891
0
        CPL_LSBPTR64(&dfDouble);                                               \
892
0
        memcpy((ptr) + (offset), &dfDouble, 8);                                \
893
0
    } while (false);
894
895
    // Frame if present
896
0
    std::vector<RSWFrameCoord> astFrameCoords;
897
0
    auto pszFrameWKT = GetMetadataItem(MD_FRAME_KEY);
898
0
    if (pszFrameWKT != nullptr)
899
0
    {
900
0
        CPLDebug("RMF", "Write to header frame: %s", pszFrameWKT);
901
0
        OGRGeometry *poFrameGeom = nullptr;
902
0
        if (OGRGeometryFactory::createFromWkt(pszFrameWKT, nullptr,
903
0
                                              &poFrameGeom) == OGRERR_NONE)
904
0
        {
905
0
            if (poFrameGeom->getGeometryType() == wkbPolygon)
906
0
            {
907
0
                std::array<double, 6> adfReverseGeoTransform = {0};
908
0
                if (GDALInvGeoTransform(adfGeoTransform.data(),
909
0
                                        adfReverseGeoTransform.data()) == TRUE)
910
0
                {
911
0
                    OGRPolygon *poFramePoly = poFrameGeom->toPolygon();
912
0
                    if (!poFramePoly->IsEmpty())
913
0
                    {
914
0
                        OGRLinearRing *poFrameRing =
915
0
                            poFramePoly->getExteriorRing();
916
0
                        for (int i = 0; i < poFrameRing->getNumPoints(); i++)
917
0
                        {
918
0
                            int nX = int(adfReverseGeoTransform[0] +
919
0
                                         poFrameRing->getX(i) *
920
0
                                             adfReverseGeoTransform[1] -
921
0
                                         0.5);
922
0
                            int nY = int(adfReverseGeoTransform[3] +
923
0
                                         poFrameRing->getY(i) *
924
0
                                             adfReverseGeoTransform[5] -
925
0
                                         0.5);
926
927
0
                            CPLDebug("RMF", "X: %d, Y: %d", nX, nY);
928
929
0
                            astFrameCoords.push_back({nX, nY});
930
0
                        }
931
0
                    }
932
933
0
                    if (astFrameCoords.empty() ||
934
0
                        astFrameCoords.size() > nMaxFramePointCount)
935
0
                    {
936
                        // CPLError(CE_Warning, CPLE_AppDefined, "Invalid frame WKT: %s", pszFrameWKT);
937
0
                        CPLDebug("RMF", "Write to header frame failed: no "
938
0
                                        "points or too many");
939
0
                        astFrameCoords.clear();
940
0
                    }
941
0
                    else
942
0
                    {
943
0
                        sHeader.nROISize = static_cast<GUInt32>(
944
0
                            sizeof(RSWFrame) +
945
0
                            sizeof(RSWFrameCoord) *
946
0
                                astFrameCoords
947
0
                                    .size());  // Set real size and real point count
948
0
                        sHeader.iFrameFlag = 0;
949
0
                    }
950
0
                }
951
0
                else
952
0
                {
953
0
                    CPLDebug("RMF", "Write to header frame failed: "
954
0
                                    "GDALInvGeoTransform == FALSE");
955
0
                }
956
0
            }
957
0
            OGRGeometryFactory::destroyGeometry(poFrameGeom);
958
0
        }
959
0
        else
960
0
        {
961
0
            CPLDebug("RMF", "Write to header frame failed: "
962
0
                            "OGRGeometryFactory::createFromWkt error");
963
0
        }
964
0
    }
965
966
0
    vsi_l_offset iCurrentFileSize(GetLastOffset());
967
0
    sHeader.nFileSize0 = GetRMFOffset(iCurrentFileSize, &iCurrentFileSize);
968
0
    sHeader.nSize = sHeader.nFileSize0 - GetRMFOffset(nHeaderOffset, nullptr);
969
    /* -------------------------------------------------------------------- */
970
    /*  Write out the main header.                                          */
971
    /* -------------------------------------------------------------------- */
972
0
    {
973
0
        GByte abyHeader[RMF_HEADER_SIZE] = {};
974
975
0
        memcpy(abyHeader, sHeader.bySignature, RMF_SIGNATURE_SIZE);
976
0
        RMF_WRITE_ULONG(abyHeader, sHeader.iVersion, 4);
977
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nSize, 8);
978
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nOvrOffset, 12);
979
0
        RMF_WRITE_ULONG(abyHeader, sHeader.iUserID, 16);
980
0
        memcpy(abyHeader + 20, sHeader.byName, RMF_NAME_SIZE);
981
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nBitDepth, 52);
982
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nHeight, 56);
983
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nWidth, 60);
984
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nXTiles, 64);
985
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nYTiles, 68);
986
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nTileHeight, 72);
987
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nTileWidth, 76);
988
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nLastTileHeight, 80);
989
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nLastTileWidth, 84);
990
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nROIOffset, 88);
991
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nROISize, 92);
992
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nClrTblOffset, 96);
993
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nClrTblSize, 100);
994
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nTileTblOffset, 104);
995
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nTileTblSize, 108);
996
0
        RMF_WRITE_LONG(abyHeader, sHeader.iMapType, 124);
997
0
        RMF_WRITE_LONG(abyHeader, sHeader.iProjection, 128);
998
0
        RMF_WRITE_LONG(abyHeader, sHeader.iEPSGCode, 132);
999
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfScale, 136);
1000
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfResolution, 144);
1001
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfPixelSize, 152);
1002
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfLLY, 160);
1003
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfLLX, 168);
1004
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfStdP1, 176);
1005
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfStdP2, 184);
1006
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfCenterLong, 192);
1007
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfCenterLat, 200);
1008
0
        *(abyHeader + 208) = sHeader.iCompression;
1009
0
        *(abyHeader + 209) = sHeader.iMaskType;
1010
0
        *(abyHeader + 210) = sHeader.iMaskStep;
1011
0
        *(abyHeader + 211) = sHeader.iFrameFlag;
1012
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nFlagsTblOffset, 212);
1013
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nFlagsTblSize, 216);
1014
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nFileSize0, 220);
1015
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nFileSize1, 224);
1016
0
        *(abyHeader + 228) = sHeader.iUnknown;
1017
0
        *(abyHeader + 244) = sHeader.iGeorefFlag;
1018
0
        *(abyHeader + 245) = sHeader.iInverse;
1019
0
        *(abyHeader + 246) = sHeader.iJpegQuality;
1020
0
        memcpy(abyHeader + 248, sHeader.abyInvisibleColors,
1021
0
               sizeof(sHeader.abyInvisibleColors));
1022
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.adfElevMinMax[0], 280);
1023
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.adfElevMinMax[1], 288);
1024
0
        RMF_WRITE_DOUBLE(abyHeader, sHeader.dfNoData, 296);
1025
0
        RMF_WRITE_ULONG(abyHeader, sHeader.iElevationUnit, 304);
1026
0
        *(abyHeader + 308) = sHeader.iElevationType;
1027
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nExtHdrOffset, 312);
1028
0
        RMF_WRITE_ULONG(abyHeader, sHeader.nExtHdrSize, 316);
1029
1030
0
        VSIFSeekL(fp, nHeaderOffset, SEEK_SET);
1031
0
        VSIFWriteL(abyHeader, 1, sizeof(abyHeader), fp);
1032
0
    }
1033
1034
    /* -------------------------------------------------------------------- */
1035
    /*  Write out the extended header.                                      */
1036
    /* -------------------------------------------------------------------- */
1037
1038
0
    if (sHeader.nExtHdrOffset && sHeader.nExtHdrSize >= RMF_MIN_EXT_HEADER_SIZE)
1039
0
    {
1040
0
        if (sHeader.nExtHdrSize > RMF_MAX_EXT_HEADER_SIZE)
1041
0
        {
1042
0
            CPLError(CE_Failure, CPLE_FileIO, "RMF File malformed");
1043
0
            return CE_Failure;
1044
0
        }
1045
0
        GByte *pabyExtHeader =
1046
0
            reinterpret_cast<GByte *>(CPLCalloc(sHeader.nExtHdrSize, 1));
1047
1048
0
        RMF_WRITE_LONG(pabyExtHeader, sExtHeader.nEllipsoid, 24);
1049
0
        RMF_WRITE_LONG(pabyExtHeader, sExtHeader.nVertDatum, 28);
1050
0
        RMF_WRITE_LONG(pabyExtHeader, sExtHeader.nDatum, 32);
1051
0
        RMF_WRITE_LONG(pabyExtHeader, sExtHeader.nZone, 36);
1052
1053
0
        VSIFSeekL(fp, GetFileOffset(sHeader.nExtHdrOffset), SEEK_SET);
1054
0
        VSIFWriteL(pabyExtHeader, 1, sHeader.nExtHdrSize, fp);
1055
1056
0
        CPLFree(pabyExtHeader);
1057
0
    }
1058
1059
    /* -------------------------------------------------------------------- */
1060
    /*  Write out the color table.                                          */
1061
    /* -------------------------------------------------------------------- */
1062
1063
0
    if (sHeader.nClrTblOffset && sHeader.nClrTblSize)
1064
0
    {
1065
0
        VSIFSeekL(fp, GetFileOffset(sHeader.nClrTblOffset), SEEK_SET);
1066
0
        VSIFWriteL(pabyColorTable, 1, sHeader.nClrTblSize, fp);
1067
0
    }
1068
1069
0
    if (sHeader.nROIOffset && sHeader.nROISize)
1070
0
    {
1071
0
        GByte *pabyROI =
1072
0
            reinterpret_cast<GByte *>(CPLCalloc(sHeader.nROISize, 1));
1073
0
        memset(pabyROI, 0, sHeader.nROISize);
1074
1075
0
        auto nPointCount = astFrameCoords.size();
1076
0
        size_t offset = 0;
1077
0
        RMF_WRITE_LONG(pabyROI, nPolygonType, offset);
1078
0
        offset += 4;
1079
0
        RMF_WRITE_LONG(pabyROI, static_cast<GInt32>((4 + nPointCount * 2) * 4),
1080
0
                       offset);
1081
0
        offset += 4;
1082
0
        RMF_WRITE_LONG(pabyROI, 0, offset);
1083
0
        offset += 4;
1084
0
        RMF_WRITE_LONG(pabyROI, static_cast<GInt32>(32768 * nPointCount * 2),
1085
0
                       offset);
1086
0
        offset += 4;
1087
1088
        // Write points
1089
0
        for (size_t i = 0; i < nPointCount; i++)
1090
0
        {
1091
0
            RMF_WRITE_LONG(pabyROI, astFrameCoords[i].nX, offset);
1092
0
            offset += 4;
1093
0
            RMF_WRITE_LONG(pabyROI, astFrameCoords[i].nY, offset);
1094
0
            offset += 4;
1095
0
        }
1096
1097
0
        VSIFSeekL(fp, GetFileOffset(sHeader.nROIOffset), SEEK_SET);
1098
0
        VSIFWriteL(pabyROI, 1, sHeader.nROISize, fp);
1099
1100
0
        CPLFree(pabyROI);
1101
0
    }
1102
1103
0
    if (sHeader.nFlagsTblOffset && sHeader.nFlagsTblSize)
1104
0
    {
1105
0
        GByte *pabyFlagsTbl =
1106
0
            reinterpret_cast<GByte *>(CPLCalloc(sHeader.nFlagsTblSize, 1));
1107
1108
0
        if (sHeader.iFrameFlag == 0)
1109
0
        {
1110
            // TODO: Add more strictly check for flag value
1111
0
            memset(
1112
0
                pabyFlagsTbl, 2,
1113
0
                sHeader
1114
0
                    .nFlagsTblSize);  // Mark all blocks as intersected with ROI. 0 - complete outside, 1 - complete inside.
1115
0
        }
1116
0
        else
1117
0
        {
1118
0
            memset(pabyFlagsTbl, 0, sHeader.nFlagsTblSize);
1119
0
        }
1120
1121
0
        VSIFSeekL(fp, GetFileOffset(sHeader.nFlagsTblOffset), SEEK_SET);
1122
0
        VSIFWriteL(pabyFlagsTbl, 1, sHeader.nFlagsTblSize, fp);
1123
1124
0
        CPLFree(pabyFlagsTbl);
1125
0
    }
1126
1127
0
#undef RMF_WRITE_DOUBLE
1128
0
#undef RMF_WRITE_ULONG
1129
0
#undef RMF_WRITE_LONG
1130
1131
    /* -------------------------------------------------------------------- */
1132
    /*  Write out the block table, swap if needed.                          */
1133
    /* -------------------------------------------------------------------- */
1134
1135
0
    VSIFSeekL(fp, GetFileOffset(sHeader.nTileTblOffset), SEEK_SET);
1136
1137
#ifdef CPL_MSB
1138
    GUInt32 *paiTilesSwapped =
1139
        reinterpret_cast<GUInt32 *>(CPLMalloc(sHeader.nTileTblSize));
1140
    if (!paiTilesSwapped)
1141
        return CE_Failure;
1142
1143
    memcpy(paiTilesSwapped, paiTiles, sHeader.nTileTblSize);
1144
    for (GUInt32 i = 0; i < sHeader.nTileTblSize / sizeof(GUInt32); i++)
1145
        CPL_SWAP32PTR(paiTilesSwapped + i);
1146
    VSIFWriteL(paiTilesSwapped, 1, sHeader.nTileTblSize, fp);
1147
1148
    CPLFree(paiTilesSwapped);
1149
#else
1150
0
    VSIFWriteL(paiTiles, 1, sHeader.nTileTblSize, fp);
1151
0
#endif
1152
1153
0
    bHeaderDirty = false;
1154
1155
0
    return CE_None;
1156
0
}
1157
1158
/************************************************************************/
1159
/*                             FlushCache()                             */
1160
/************************************************************************/
1161
1162
CPLErr RMFDataset::FlushCache(bool bAtClosing)
1163
1164
175
{
1165
175
    CPLErr eErr = GDALDataset::FlushCache(bAtClosing);
1166
1167
175
    if (poCompressData != nullptr &&
1168
175
        poCompressData->oThreadPool.GetThreadCount() > 0)
1169
0
    {
1170
0
        poCompressData->oThreadPool.WaitCompletion();
1171
0
    }
1172
1173
175
    if (bAtClosing && eRMFType == RMFT_MTW && eAccess == GA_Update)
1174
0
    {
1175
0
        GDALRasterBand *poBand = GetRasterBand(1);
1176
1177
0
        if (poBand)
1178
0
        {
1179
            // ComputeRasterMinMax can setup error in case of dataset full
1180
            // from NoData values, but it  makes no sense here.
1181
0
            CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
1182
0
            poBand->ComputeRasterMinMax(FALSE, sHeader.adfElevMinMax);
1183
0
            bHeaderDirty = true;
1184
0
        }
1185
0
    }
1186
175
    if (bHeaderDirty && WriteHeader() != CE_None)
1187
0
        eErr = CE_Failure;
1188
175
    return eErr;
1189
175
}
1190
1191
/************************************************************************/
1192
/*                              Identify()                              */
1193
/************************************************************************/
1194
1195
int RMFDataset::Identify(GDALOpenInfo *poOpenInfo)
1196
1197
16.2k
{
1198
16.2k
    if (poOpenInfo->pabyHeader == nullptr)
1199
441
        return FALSE;
1200
1201
15.8k
    if (memcmp(poOpenInfo->pabyHeader, RMF_SigRSW, sizeof(RMF_SigRSW)) != 0 &&
1202
15.8k
        memcmp(poOpenInfo->pabyHeader, RMF_SigRSW_BE, sizeof(RMF_SigRSW_BE)) !=
1203
15.8k
            0 &&
1204
15.8k
        memcmp(poOpenInfo->pabyHeader, RMF_SigMTW, sizeof(RMF_SigMTW)) != 0)
1205
15.4k
        return FALSE;
1206
1207
350
    return TRUE;
1208
15.8k
}
1209
1210
/************************************************************************/
1211
/*                                Open()                                */
1212
/************************************************************************/
1213
1214
GDALDataset *RMFDataset::Open(GDALOpenInfo *poOpenInfo)
1215
175
{
1216
175
    auto poDS = Open(poOpenInfo, nullptr, 0);
1217
175
    if (poDS == nullptr)
1218
17
    {
1219
17
        return nullptr;
1220
17
    }
1221
1222
158
    RMFDataset *poCurrentLayer = poDS;
1223
158
    RMFDataset *poParent = poCurrentLayer;
1224
158
    const int nMaxPossibleOvCount = 64;
1225
1226
158
    for (int iOv = 0; iOv < nMaxPossibleOvCount && poCurrentLayer != nullptr;
1227
158
         ++iOv)
1228
158
    {
1229
158
        poCurrentLayer = poCurrentLayer->OpenOverview(poParent, poOpenInfo);
1230
158
        if (poCurrentLayer == nullptr)
1231
158
            break;
1232
0
        poParent->poOvrDatasets.push_back(poCurrentLayer);
1233
0
    }
1234
1235
158
    return poDS;
1236
175
}
1237
1238
RMFDataset *RMFDataset::Open(GDALOpenInfo *poOpenInfo, RMFDataset *poParentDS,
1239
                             vsi_l_offset nNextHeaderOffset)
1240
329
{
1241
329
    if (!Identify(poOpenInfo) ||
1242
329
        (poParentDS == nullptr && poOpenInfo->fpL == nullptr))
1243
154
        return nullptr;
1244
1245
    /* -------------------------------------------------------------------- */
1246
    /*  Create a corresponding GDALDataset.                                 */
1247
    /* -------------------------------------------------------------------- */
1248
175
    RMFDataset *poDS = new RMFDataset();
1249
1250
175
    if (poParentDS == nullptr)
1251
175
    {
1252
175
        poDS->fp = poOpenInfo->fpL;
1253
175
        poOpenInfo->fpL = nullptr;
1254
175
        poDS->nHeaderOffset = 0;
1255
175
        poDS->poParentDS = nullptr;
1256
175
    }
1257
0
    else
1258
0
    {
1259
0
        poDS->fp = poParentDS->fp;
1260
0
        poDS->poParentDS = poParentDS;
1261
0
        poDS->nHeaderOffset = nNextHeaderOffset;
1262
0
    }
1263
175
    poDS->eAccess = poOpenInfo->eAccess;
1264
1265
175
#define RMF_READ_SHORT(ptr, value, offset)                                     \
1266
175
    do                                                                         \
1267
175
    {                                                                          \
1268
175
        memcpy(&(value), reinterpret_cast<GInt16 *>((ptr) + (offset)),         \
1269
175
               sizeof(GInt16));                                                \
1270
175
        if (poDS->bBigEndian)                                                  \
1271
175
        {                                                                      \
1272
175
            CPL_MSBPTR16(&(value));                                            \
1273
175
        }                                                                      \
1274
175
        else                                                                   \
1275
175
        {                                                                      \
1276
175
            CPL_LSBPTR16(&(value));                                            \
1277
175
        }                                                                      \
1278
175
    } while (false);
1279
1280
175
#define RMF_READ_ULONG(ptr, value, offset)                                     \
1281
5.65k
    do                                                                         \
1282
5.65k
    {                                                                          \
1283
5.65k
        memcpy(&(value), reinterpret_cast<GUInt32 *>((ptr) + (offset)),        \
1284
5.65k
               sizeof(GUInt32));                                               \
1285
5.65k
        if (poDS->bBigEndian)                                                  \
1286
5.65k
        {                                                                      \
1287
5.65k
            CPL_MSBPTR32(&(value));                                            \
1288
5.65k
        }                                                                      \
1289
5.65k
        else                                                                   \
1290
5.65k
        {                                                                      \
1291
0
            CPL_LSBPTR32(&(value));                                            \
1292
0
        }                                                                      \
1293
5.65k
    } while (false);
1294
1295
1.12k
#define RMF_READ_LONG(ptr, value, offset) RMF_READ_ULONG(ptr, value, offset)
1296
1297
175
#define RMF_READ_DOUBLE(ptr, value, offset)                                    \
1298
2.08k
    do                                                                         \
1299
2.08k
    {                                                                          \
1300
2.08k
        memcpy(&(value), reinterpret_cast<double *>((ptr) + (offset)),         \
1301
2.08k
               sizeof(double));                                                \
1302
2.08k
        if (poDS->bBigEndian)                                                  \
1303
2.08k
        {                                                                      \
1304
2.08k
            CPL_MSBPTR64(&(value));                                            \
1305
2.08k
        }                                                                      \
1306
2.08k
        else                                                                   \
1307
2.08k
        {                                                                      \
1308
0
            CPL_LSBPTR64(&(value));                                            \
1309
0
        }                                                                      \
1310
2.08k
    } while (false);
1311
1312
    /* -------------------------------------------------------------------- */
1313
    /*  Read the main header.                                               */
1314
    /* -------------------------------------------------------------------- */
1315
1316
175
    {
1317
175
        GByte abyHeader[RMF_HEADER_SIZE] = {};
1318
1319
175
        VSIFSeekL(poDS->fp, nNextHeaderOffset, SEEK_SET);
1320
175
        if (VSIFReadL(abyHeader, 1, sizeof(abyHeader), poDS->fp) !=
1321
175
            sizeof(abyHeader))
1322
1
        {
1323
1
            delete poDS;
1324
1
            return nullptr;
1325
1
        }
1326
1327
174
        if (memcmp(abyHeader, RMF_SigMTW, sizeof(RMF_SigMTW)) == 0)
1328
0
        {
1329
0
            poDS->eRMFType = RMFT_MTW;
1330
0
        }
1331
174
        else if (memcmp(abyHeader, RMF_SigRSW_BE, sizeof(RMF_SigRSW_BE)) == 0)
1332
174
        {
1333
174
            poDS->eRMFType = RMFT_RSW;
1334
174
            poDS->bBigEndian = true;
1335
174
        }
1336
0
        else
1337
0
        {
1338
0
            poDS->eRMFType = RMFT_RSW;
1339
0
        }
1340
1341
174
        memcpy(poDS->sHeader.bySignature, abyHeader, RMF_SIGNATURE_SIZE);
1342
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.iVersion, 4);
1343
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nSize, 8);
1344
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nOvrOffset, 12);
1345
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.iUserID, 16);
1346
174
        memcpy(poDS->sHeader.byName, abyHeader + 20,
1347
174
               sizeof(poDS->sHeader.byName));
1348
174
        poDS->sHeader.byName[sizeof(poDS->sHeader.byName) - 1] = '\0';
1349
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nBitDepth, 52);
1350
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nHeight, 56);
1351
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nWidth, 60);
1352
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nXTiles, 64);
1353
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nYTiles, 68);
1354
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileHeight, 72);
1355
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileWidth, 76);
1356
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nLastTileHeight, 80);
1357
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nLastTileWidth, 84);
1358
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nROIOffset, 88);
1359
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nROISize, 92);
1360
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nClrTblOffset, 96);
1361
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nClrTblSize, 100);
1362
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileTblOffset, 104);
1363
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileTblSize, 108);
1364
174
        RMF_READ_LONG(abyHeader, poDS->sHeader.iMapType, 124);
1365
174
        RMF_READ_LONG(abyHeader, poDS->sHeader.iProjection, 128);
1366
174
        RMF_READ_LONG(abyHeader, poDS->sHeader.iEPSGCode, 132);
1367
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfScale, 136);
1368
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfResolution, 144);
1369
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfPixelSize, 152);
1370
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfLLY, 160);
1371
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfLLX, 168);
1372
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfStdP1, 176);
1373
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfStdP2, 184);
1374
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfCenterLong, 192);
1375
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfCenterLat, 200);
1376
174
        poDS->sHeader.iCompression = *(abyHeader + 208);
1377
174
        poDS->sHeader.iMaskType = *(abyHeader + 209);
1378
174
        poDS->sHeader.iMaskStep = *(abyHeader + 210);
1379
174
        poDS->sHeader.iFrameFlag = *(abyHeader + 211);
1380
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFlagsTblOffset, 212);
1381
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFlagsTblSize, 216);
1382
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFileSize0, 220);
1383
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFileSize1, 224);
1384
174
        poDS->sHeader.iUnknown = *(abyHeader + 228);
1385
174
        poDS->sHeader.iGeorefFlag = *(abyHeader + 244);
1386
174
        poDS->sHeader.iInverse = *(abyHeader + 245);
1387
174
        poDS->sHeader.iJpegQuality = *(abyHeader + 246);
1388
174
        memcpy(poDS->sHeader.abyInvisibleColors, abyHeader + 248,
1389
174
               sizeof(poDS->sHeader.abyInvisibleColors));
1390
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.adfElevMinMax[0], 280);
1391
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.adfElevMinMax[1], 288);
1392
174
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfNoData, 296);
1393
1394
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.iElevationUnit, 304);
1395
174
        poDS->sHeader.iElevationType = *(abyHeader + 308);
1396
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nExtHdrOffset, 312);
1397
174
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nExtHdrSize, 316);
1398
174
        poDS->SetMetadataItem(MD_SCALE_KEY,
1399
174
                              CPLSPrintf("1 : %u", int(poDS->sHeader.dfScale)));
1400
174
        poDS->SetMetadataItem(MD_NAME_KEY,
1401
174
                              CPLSPrintf("%s", poDS->sHeader.byName));
1402
174
        poDS->SetMetadataItem(MD_VERSION_KEY,
1403
174
                              CPLSPrintf("%d", poDS->sHeader.iVersion));
1404
174
        poDS->SetMetadataItem(MD_MATH_BASE_MAP_TYPE_KEY,
1405
174
                              CPLSPrintf("%d", poDS->sHeader.iMapType));
1406
174
        poDS->SetMetadataItem(MD_MATH_BASE_PROJECTION_KEY,
1407
174
                              CPLSPrintf("%d", poDS->sHeader.iProjection));
1408
174
    }
1409
1410
174
    if (poDS->sHeader.nTileTblSize % (sizeof(GUInt32) * 2))
1411
1
    {
1412
1
        CPLError(CE_Warning, CPLE_IllegalArg, "Invalid tile table size.");
1413
1
        delete poDS;
1414
1
        return nullptr;
1415
1
    }
1416
1417
173
    bool bInvalidTileSize;
1418
173
    try
1419
173
    {
1420
173
        uint64_t nMaxTileBits =
1421
173
            (CPLSM(static_cast<uint64_t>(2)) *
1422
173
             CPLSM(static_cast<uint64_t>(poDS->sHeader.nTileWidth)) *
1423
173
             CPLSM(static_cast<uint64_t>(poDS->sHeader.nTileHeight)) *
1424
173
             CPLSM(static_cast<uint64_t>(poDS->sHeader.nBitDepth)))
1425
173
                .v();
1426
173
        bInvalidTileSize =
1427
173
            (nMaxTileBits >
1428
173
             static_cast<uint64_t>(std::numeric_limits<GUInt32>::max()));
1429
173
    }
1430
173
    catch (...)
1431
173
    {
1432
5
        bInvalidTileSize = true;
1433
5
    }
1434
173
    if (bInvalidTileSize)
1435
7
    {
1436
7
        CPLError(CE_Warning, CPLE_IllegalArg,
1437
7
                 "Invalid tile size. Width %lu, height %lu, bit depth %lu.",
1438
7
                 static_cast<unsigned long>(poDS->sHeader.nTileWidth),
1439
7
                 static_cast<unsigned long>(poDS->sHeader.nTileHeight),
1440
7
                 static_cast<unsigned long>(poDS->sHeader.nBitDepth));
1441
7
        delete poDS;
1442
7
        return nullptr;
1443
7
    }
1444
1445
166
    if (poDS->sHeader.nLastTileWidth > poDS->sHeader.nTileWidth ||
1446
166
        poDS->sHeader.nLastTileHeight > poDS->sHeader.nTileHeight)
1447
2
    {
1448
2
        CPLError(CE_Warning, CPLE_IllegalArg,
1449
2
                 "Invalid last tile size %lu x %lu. "
1450
2
                 "It can't be greater than %lu x %lu.",
1451
2
                 static_cast<unsigned long>(poDS->sHeader.nLastTileWidth),
1452
2
                 static_cast<unsigned long>(poDS->sHeader.nLastTileHeight),
1453
2
                 static_cast<unsigned long>(poDS->sHeader.nTileWidth),
1454
2
                 static_cast<unsigned long>(poDS->sHeader.nTileHeight));
1455
2
        delete poDS;
1456
2
        return nullptr;
1457
2
    }
1458
1459
164
    if (poParentDS != nullptr)
1460
0
    {
1461
0
        if (0 != memcmp(poDS->sHeader.bySignature,
1462
0
                        poParentDS->sHeader.bySignature, RMF_SIGNATURE_SIZE))
1463
0
        {
1464
0
            CPLError(CE_Warning, CPLE_IllegalArg,
1465
0
                     "Invalid subheader signature.");
1466
0
            delete poDS;
1467
0
            return nullptr;
1468
0
        }
1469
0
    }
1470
1471
    /* -------------------------------------------------------------------- */
1472
    /*  Read the extended header.                                           */
1473
    /* -------------------------------------------------------------------- */
1474
1475
164
    if (poDS->sHeader.nExtHdrOffset &&
1476
164
        poDS->sHeader.nExtHdrSize >= RMF_MIN_EXT_HEADER_SIZE)
1477
154
    {
1478
154
        if (poDS->sHeader.nExtHdrSize > RMF_MAX_EXT_HEADER_SIZE)
1479
3
        {
1480
3
            CPLError(CE_Failure, CPLE_FileIO, "RMF File malformed");
1481
3
            delete poDS;
1482
3
            return nullptr;
1483
3
        }
1484
151
        GByte *pabyExtHeader =
1485
151
            reinterpret_cast<GByte *>(CPLCalloc(poDS->sHeader.nExtHdrSize, 1));
1486
151
        if (pabyExtHeader == nullptr)
1487
0
        {
1488
0
            delete poDS;
1489
0
            return nullptr;
1490
0
        }
1491
1492
151
        VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nExtHdrOffset),
1493
151
                  SEEK_SET);
1494
151
        VSIFReadL(pabyExtHeader, 1, poDS->sHeader.nExtHdrSize, poDS->fp);
1495
1496
151
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nEllipsoid, 24);
1497
151
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nVertDatum, 28);
1498
151
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nDatum, 32);
1499
151
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nZone, 36);
1500
1501
151
        CPLFree(pabyExtHeader);
1502
151
    }
1503
1504
161
    CPLDebug("RMF", "Version %d", poDS->sHeader.iVersion);
1505
1506
161
    constexpr GUInt32 ROI_MAX_SIZE_TO_AVOID_EXCESSIVE_RAM_USAGE =
1507
161
        10 * 1024 * 1024;
1508
#ifdef DEBUG
1509
1510
    CPLDebug("RMF",
1511
             "%s image has width %d, height %d, bit depth %d, "
1512
             "compression scheme %d, %s, nodata %f",
1513
             (poDS->eRMFType == RMFT_MTW) ? "MTW" : "RSW", poDS->sHeader.nWidth,
1514
             poDS->sHeader.nHeight, poDS->sHeader.nBitDepth,
1515
             poDS->sHeader.iCompression,
1516
             poDS->bBigEndian ? "big endian" : "little endian",
1517
             poDS->sHeader.dfNoData);
1518
    CPLDebug("RMF",
1519
             "Size %d, offset to overview %#lx, user ID %d, "
1520
             "ROI offset %#lx, ROI size %d",
1521
             poDS->sHeader.nSize,
1522
             static_cast<unsigned long>(poDS->sHeader.nOvrOffset),
1523
             poDS->sHeader.iUserID,
1524
             static_cast<unsigned long>(poDS->sHeader.nROIOffset),
1525
             poDS->sHeader.nROISize);
1526
    CPLDebug("RMF", "Map type %d, projection %d, scale %f, resolution %f, ",
1527
             poDS->sHeader.iMapType, poDS->sHeader.iProjection,
1528
             poDS->sHeader.dfScale, poDS->sHeader.dfResolution);
1529
    CPLDebug("RMF", "EPSG %d ", poDS->sHeader.iEPSGCode);
1530
    CPLDebug("RMF", "Georeferencing: pixel size %f, LLX %f, LLY %f",
1531
             poDS->sHeader.dfPixelSize, poDS->sHeader.dfLLX,
1532
             poDS->sHeader.dfLLY);
1533
1534
    if (poDS->sHeader.nROIOffset &&
1535
        poDS->sHeader.nROISize >= sizeof(RSWFrame) &&
1536
        poDS->sHeader.nROISize <= ROI_MAX_SIZE_TO_AVOID_EXCESSIVE_RAM_USAGE)
1537
    {
1538
        GByte *pabyROI = reinterpret_cast<GByte *>(
1539
            VSI_MALLOC_VERBOSE(poDS->sHeader.nROISize));
1540
        if (pabyROI == nullptr)
1541
        {
1542
            delete poDS;
1543
            return nullptr;
1544
        }
1545
1546
        VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nROIOffset),
1547
                  SEEK_SET);
1548
        if (VSIFReadL(pabyROI, poDS->sHeader.nROISize, 1, poDS->fp) != 1)
1549
        {
1550
            CPLError(CE_Failure, CPLE_FileIO, "Cannot read ROI");
1551
            CPLFree(pabyROI);
1552
            delete poDS;
1553
            return nullptr;
1554
        }
1555
1556
        GInt32 nValue;
1557
1558
        CPLDebug("RMF", "ROI coordinates:");
1559
        /* coverity[tainted_data] */
1560
        for (GUInt32 i = 0; i + sizeof(nValue) <= poDS->sHeader.nROISize;
1561
             i += sizeof(nValue))
1562
        {
1563
            RMF_READ_LONG(pabyROI, nValue, i);
1564
            CPLDebug("RMF", "%d", nValue);
1565
        }
1566
1567
        CPLFree(pabyROI);
1568
    }
1569
#endif
1570
161
    if (poDS->sHeader.nWidth >= INT_MAX || poDS->sHeader.nHeight >= INT_MAX ||
1571
161
        !GDALCheckDatasetDimensions(poDS->sHeader.nWidth,
1572
160
                                    poDS->sHeader.nHeight))
1573
1
    {
1574
1
        delete poDS;
1575
1
        return nullptr;
1576
1
    }
1577
1578
    /* -------------------------------------------------------------------- */
1579
    /*  Read array of blocks offsets/sizes.                                 */
1580
    /* -------------------------------------------------------------------- */
1581
1582
    // To avoid useless excessive memory allocation
1583
160
    if (poDS->sHeader.nTileTblSize > 1000000)
1584
1
    {
1585
1
        VSIFSeekL(poDS->fp, 0, SEEK_END);
1586
1
        vsi_l_offset nFileSize = VSIFTellL(poDS->fp);
1587
1
        if (nFileSize < poDS->sHeader.nTileTblSize)
1588
1
        {
1589
1
            delete poDS;
1590
1
            return nullptr;
1591
1
        }
1592
1
    }
1593
1594
159
    if (VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nTileTblOffset),
1595
159
                  SEEK_SET) < 0)
1596
0
    {
1597
0
        delete poDS;
1598
0
        return nullptr;
1599
0
    }
1600
1601
159
    poDS->paiTiles =
1602
159
        reinterpret_cast<GUInt32 *>(VSIMalloc(poDS->sHeader.nTileTblSize));
1603
159
    if (!poDS->paiTiles)
1604
0
    {
1605
0
        delete poDS;
1606
0
        return nullptr;
1607
0
    }
1608
1609
159
    if (VSIFReadL(poDS->paiTiles, 1, poDS->sHeader.nTileTblSize, poDS->fp) <
1610
159
        poDS->sHeader.nTileTblSize)
1611
0
    {
1612
0
        CPLDebug("RMF", "Can't read tiles offsets/sizes table.");
1613
0
        delete poDS;
1614
0
        return nullptr;
1615
0
    }
1616
1617
#ifdef CPL_MSB
1618
    if (!poDS->bBigEndian)
1619
    {
1620
        for (GUInt32 i = 0; i < poDS->sHeader.nTileTblSize / sizeof(GUInt32);
1621
             i++)
1622
            CPL_SWAP32PTR(poDS->paiTiles + i);
1623
    }
1624
#else
1625
159
    if (poDS->bBigEndian)
1626
159
    {
1627
159
        for (GUInt32 i = 0; i < poDS->sHeader.nTileTblSize / sizeof(GUInt32);
1628
159
             i++)
1629
0
            CPL_SWAP32PTR(poDS->paiTiles + i);
1630
159
    }
1631
159
#endif
1632
1633
#ifdef DEBUG
1634
    CPLDebug("RMF", "List of block offsets/sizes:");
1635
1636
    for (GUInt32 i = 0; i < poDS->sHeader.nTileTblSize / sizeof(GUInt32);
1637
         i += 2)
1638
    {
1639
        CPLDebug("RMF", "    %u / %u", poDS->paiTiles[i],
1640
                 poDS->paiTiles[i + 1]);
1641
    }
1642
#endif
1643
1644
    /* -------------------------------------------------------------------- */
1645
    /*  Set up essential image parameters.                                  */
1646
    /* -------------------------------------------------------------------- */
1647
159
    GDALDataType eType = GDT_Byte;
1648
1649
159
    poDS->nRasterXSize = poDS->sHeader.nWidth;
1650
159
    poDS->nRasterYSize = poDS->sHeader.nHeight;
1651
1652
159
    if (poDS->eRMFType == RMFT_RSW)
1653
159
    {
1654
159
        switch (poDS->sHeader.nBitDepth)
1655
159
        {
1656
0
            case 32:
1657
0
            case 24:
1658
159
            case 16:
1659
159
                poDS->nBands = 3;
1660
159
                break;
1661
0
            case 1:
1662
0
            case 4:
1663
0
            case 8:
1664
0
                if (poParentDS != nullptr &&
1665
0
                    poParentDS->poColorTable != nullptr)
1666
0
                {
1667
0
                    poDS->poColorTable = poParentDS->poColorTable->Clone();
1668
0
                }
1669
0
                else
1670
0
                {
1671
                    // Allocate memory for colour table and read it
1672
0
                    poDS->nColorTableSize = 1 << poDS->sHeader.nBitDepth;
1673
0
                    GUInt32 nExpectedColorTableBytes =
1674
0
                        poDS->nColorTableSize * 4;
1675
0
                    if (nExpectedColorTableBytes > poDS->sHeader.nClrTblSize)
1676
0
                    {
1677
                        // We could probably test for strict equality in
1678
                        // the above test ???
1679
0
                        CPLDebug("RMF",
1680
0
                                 "Wrong color table size. "
1681
0
                                 "Expected %u, got %u.",
1682
0
                                 nExpectedColorTableBytes,
1683
0
                                 poDS->sHeader.nClrTblSize);
1684
0
                        delete poDS;
1685
0
                        return nullptr;
1686
0
                    }
1687
0
                    poDS->pabyColorTable = reinterpret_cast<GByte *>(
1688
0
                        VSIMalloc(nExpectedColorTableBytes));
1689
0
                    if (poDS->pabyColorTable == nullptr)
1690
0
                    {
1691
0
                        CPLDebug("RMF", "Can't allocate color table.");
1692
0
                        delete poDS;
1693
0
                        return nullptr;
1694
0
                    }
1695
0
                    if (VSIFSeekL(
1696
0
                            poDS->fp,
1697
0
                            poDS->GetFileOffset(poDS->sHeader.nClrTblOffset),
1698
0
                            SEEK_SET) < 0)
1699
0
                    {
1700
0
                        CPLDebug("RMF", "Can't seek to color table location.");
1701
0
                        delete poDS;
1702
0
                        return nullptr;
1703
0
                    }
1704
0
                    if (VSIFReadL(poDS->pabyColorTable, 1,
1705
0
                                  nExpectedColorTableBytes,
1706
0
                                  poDS->fp) < nExpectedColorTableBytes)
1707
0
                    {
1708
0
                        CPLDebug("RMF", "Can't read color table.");
1709
0
                        delete poDS;
1710
0
                        return nullptr;
1711
0
                    }
1712
1713
0
                    poDS->poColorTable = new GDALColorTable();
1714
0
                    for (GUInt32 i = 0; i < poDS->nColorTableSize; i++)
1715
0
                    {
1716
0
                        const GDALColorEntry oEntry = {
1717
0
                            poDS->pabyColorTable[i * 4],      // Red
1718
0
                            poDS->pabyColorTable[i * 4 + 1],  // Green
1719
0
                            poDS->pabyColorTable[i * 4 + 2],  // Blue
1720
0
                            255                               // Alpha
1721
0
                        };
1722
1723
0
                        poDS->poColorTable->SetColorEntry(i, &oEntry);
1724
0
                    }
1725
0
                }
1726
0
                poDS->nBands = 1;
1727
0
                break;
1728
0
            default:
1729
0
                CPLError(CE_Warning, CPLE_IllegalArg,
1730
0
                         "Invalid RSW bit depth %lu.",
1731
0
                         static_cast<unsigned long>(poDS->sHeader.nBitDepth));
1732
0
                delete poDS;
1733
0
                return nullptr;
1734
159
        }
1735
159
        eType = GDT_Byte;
1736
159
    }
1737
0
    else
1738
0
    {
1739
0
        poDS->nBands = 1;
1740
0
        if (poDS->sHeader.nBitDepth == 8)
1741
0
        {
1742
0
            eType = GDT_Byte;
1743
0
        }
1744
0
        else if (poDS->sHeader.nBitDepth == 16)
1745
0
        {
1746
0
            eType = GDT_Int16;
1747
0
        }
1748
0
        else if (poDS->sHeader.nBitDepth == 32)
1749
0
        {
1750
0
            eType = GDT_Int32;
1751
0
        }
1752
0
        else if (poDS->sHeader.nBitDepth == 64)
1753
0
        {
1754
0
            eType = GDT_Float64;
1755
0
        }
1756
0
        else
1757
0
        {
1758
0
            CPLError(CE_Warning, CPLE_IllegalArg, "Invalid MTW bit depth %lu.",
1759
0
                     static_cast<unsigned long>(poDS->sHeader.nBitDepth));
1760
0
            delete poDS;
1761
0
            return nullptr;
1762
0
        }
1763
0
    }
1764
1765
159
    if (poDS->sHeader.nTileWidth == 0 || poDS->sHeader.nTileWidth > INT_MAX ||
1766
159
        poDS->sHeader.nTileHeight == 0 || poDS->sHeader.nTileHeight > INT_MAX)
1767
0
    {
1768
0
        CPLDebug("RMF", "Invalid tile dimension : %u x %u",
1769
0
                 poDS->sHeader.nTileWidth, poDS->sHeader.nTileHeight);
1770
0
        delete poDS;
1771
0
        return nullptr;
1772
0
    }
1773
1774
159
    const int nDataSize = GDALGetDataTypeSizeBytes(eType);
1775
159
    const int nBlockXSize = static_cast<int>(poDS->sHeader.nTileWidth);
1776
159
    const int nBlockYSize = static_cast<int>(poDS->sHeader.nTileHeight);
1777
159
    if (nDataSize == 0 || nBlockXSize > INT_MAX / nBlockYSize ||
1778
159
        nBlockYSize > INT_MAX / nDataSize ||
1779
159
        nBlockXSize > INT_MAX / (nBlockYSize * nDataSize))
1780
0
    {
1781
0
        CPLDebug("RMF", "Too big raster / tile dimension");
1782
0
        delete poDS;
1783
0
        return nullptr;
1784
0
    }
1785
1786
159
    poDS->nXTiles = DIV_ROUND_UP(poDS->nRasterXSize, nBlockXSize);
1787
159
    poDS->nYTiles = DIV_ROUND_UP(poDS->nRasterYSize, nBlockYSize);
1788
1789
#ifdef DEBUG
1790
    CPLDebug("RMF", "Image is %d tiles wide, %d tiles long", poDS->nXTiles,
1791
             poDS->nYTiles);
1792
#endif
1793
1794
    /* -------------------------------------------------------------------- */
1795
    /*  Choose compression scheme.                                          */
1796
    /* -------------------------------------------------------------------- */
1797
159
    if (CE_None != poDS->SetupCompression(eType, poOpenInfo->pszFilename))
1798
1
    {
1799
1
        delete poDS;
1800
1
        return nullptr;
1801
1
    }
1802
1803
158
    if (poOpenInfo->eAccess == GA_Update)
1804
0
    {
1805
0
        if (poParentDS == nullptr)
1806
0
        {
1807
0
            if (CE_None !=
1808
0
                poDS->InitCompressorData(poOpenInfo->papszOpenOptions))
1809
0
            {
1810
0
                delete poDS;
1811
0
                return nullptr;
1812
0
            }
1813
0
        }
1814
0
        else
1815
0
        {
1816
0
            poDS->poCompressData = poParentDS->poCompressData;
1817
0
        }
1818
0
    }
1819
    /* -------------------------------------------------------------------- */
1820
    /*  Create band information objects.                                    */
1821
    /* -------------------------------------------------------------------- */
1822
632
    for (int iBand = 1; iBand <= poDS->nBands; iBand++)
1823
474
        poDS->SetBand(iBand, new RMFRasterBand(poDS, iBand, eType));
1824
1825
158
    poDS->SetupNBits();
1826
1827
158
    if (poDS->nBands > 1)
1828
158
    {
1829
158
        poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
1830
158
    }
1831
    /* -------------------------------------------------------------------- */
1832
    /*  Set up projection.                                                  */
1833
    /*                                                                      */
1834
    /*  XXX: If projection value is not specified, but image still have     */
1835
    /*  georeferencing information, assume Gauss-Kruger projection.         */
1836
    /* -------------------------------------------------------------------- */
1837
158
    if (poDS->sHeader.iEPSGCode > RMF_EPSG_MIN_CODE ||
1838
158
        poDS->sHeader.iProjection > 0 ||
1839
158
        (poDS->sHeader.dfPixelSize != 0.0 && poDS->sHeader.dfLLX != 0.0 &&
1840
6
         poDS->sHeader.dfLLY != 0.0))
1841
157
    {
1842
157
        GInt32 nProj =
1843
157
            (poDS->sHeader.iProjection) ? poDS->sHeader.iProjection : 1;
1844
157
        double padfPrjParams[8] = {poDS->sHeader.dfStdP1,
1845
157
                                   poDS->sHeader.dfStdP2,
1846
157
                                   poDS->sHeader.dfCenterLat,
1847
157
                                   poDS->sHeader.dfCenterLong,
1848
157
                                   1.0,
1849
157
                                   0.0,
1850
157
                                   0.0,
1851
157
                                   0.0};
1852
1853
        // XXX: Compute zone number for Gauss-Kruger (Transverse Mercator)
1854
        // projection if it is not specified.
1855
157
        if (nProj == 1L && poDS->sHeader.dfCenterLong == 0.0)
1856
43
        {
1857
43
            if (poDS->sExtHeader.nZone == 0)
1858
13
            {
1859
13
                double centerXCoord =
1860
13
                    poDS->sHeader.dfLLX +
1861
13
                    (poDS->nRasterXSize * poDS->sHeader.dfPixelSize / 2.0);
1862
13
                padfPrjParams[7] = floor((centerXCoord - 500000.0) / 1000000.0);
1863
13
            }
1864
30
            else
1865
30
            {
1866
30
                padfPrjParams[7] = poDS->sExtHeader.nZone;
1867
30
            }
1868
43
        }
1869
1870
157
        OGRErr res = OGRERR_FAILURE;
1871
157
        if (nProj >= 0 &&
1872
157
            (poDS->sExtHeader.nDatum >= 0 || poDS->sExtHeader.nEllipsoid >= 0))
1873
146
        {
1874
146
            res = poDS->m_oSRS.importFromPanorama(
1875
146
                nProj, poDS->sExtHeader.nDatum, poDS->sExtHeader.nEllipsoid,
1876
146
                padfPrjParams);
1877
146
        }
1878
1879
157
        if (poDS->sHeader.iEPSGCode > RMF_EPSG_MIN_CODE &&
1880
157
            (OGRERR_NONE != res || poDS->m_oSRS.IsLocal()))
1881
15
        {
1882
15
            res = poDS->m_oSRS.importFromEPSG(poDS->sHeader.iEPSGCode);
1883
15
        }
1884
1885
157
        const char *pszSetVertCS =
1886
157
            CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "RMF_SET_VERTCS",
1887
157
                                 CPLGetConfigOption("RMF_SET_VERTCS", "NO"));
1888
157
        if (CPLTestBool(pszSetVertCS) && res == OGRERR_NONE &&
1889
157
            poDS->sExtHeader.nVertDatum > 0)
1890
0
        {
1891
0
            poDS->m_oSRS.importVertCSFromPanorama(poDS->sExtHeader.nVertDatum);
1892
0
        }
1893
157
    }
1894
1895
    /* -------------------------------------------------------------------- */
1896
    /*  Set up georeferencing.                                              */
1897
    /* -------------------------------------------------------------------- */
1898
158
    if ((poDS->eRMFType == RMFT_RSW && poDS->sHeader.iGeorefFlag) ||
1899
158
        (poDS->eRMFType == RMFT_MTW && poDS->sHeader.dfPixelSize != 0.0))
1900
146
    {
1901
146
        poDS->adfGeoTransform[0] = poDS->sHeader.dfLLX;
1902
146
        poDS->adfGeoTransform[3] =
1903
146
            poDS->sHeader.dfLLY +
1904
146
            poDS->nRasterYSize * poDS->sHeader.dfPixelSize;
1905
146
        poDS->adfGeoTransform[1] = poDS->sHeader.dfPixelSize;
1906
146
        poDS->adfGeoTransform[5] = -poDS->sHeader.dfPixelSize;
1907
146
        poDS->adfGeoTransform[2] = 0.0;
1908
146
        poDS->adfGeoTransform[4] = 0.0;
1909
146
    }
1910
1911
    /* -------------------------------------------------------------------- */
1912
    /*  Set units.                                                          */
1913
    /* -------------------------------------------------------------------- */
1914
1915
158
    if (poDS->eRMFType == RMFT_MTW)
1916
0
    {
1917
0
        CPLFree(poDS->pszUnitType);
1918
0
        poDS->pszUnitType = RMFUnitTypeToStr(poDS->sHeader.iElevationUnit);
1919
0
    }
1920
1921
    /* -------------------------------------------------------------------- */
1922
    /*  Report some other dataset related information.                      */
1923
    /* -------------------------------------------------------------------- */
1924
1925
158
    if (poDS->eRMFType == RMFT_MTW)
1926
0
    {
1927
0
        char szTemp[256] = {};
1928
1929
0
        snprintf(szTemp, sizeof(szTemp), "%g", poDS->sHeader.adfElevMinMax[0]);
1930
0
        poDS->SetMetadataItem("ELEVATION_MINIMUM", szTemp);
1931
1932
0
        snprintf(szTemp, sizeof(szTemp), "%g", poDS->sHeader.adfElevMinMax[1]);
1933
0
        poDS->SetMetadataItem("ELEVATION_MAXIMUM", szTemp);
1934
1935
0
        poDS->SetMetadataItem("ELEVATION_UNITS", poDS->pszUnitType);
1936
1937
0
        snprintf(szTemp, sizeof(szTemp), "%d", poDS->sHeader.iElevationType);
1938
0
        poDS->SetMetadataItem("ELEVATION_TYPE", szTemp);
1939
0
    }
1940
1941
    /* -------------------------------------------------------------------- */
1942
    /*      Check for overviews.                                            */
1943
    /* -------------------------------------------------------------------- */
1944
158
    if (nNextHeaderOffset == 0 && poParentDS == nullptr)
1945
158
    {
1946
158
        poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
1947
158
    }
1948
1949
    /* Set frame */
1950
158
    if (poDS->sHeader.nROIOffset &&
1951
158
        poDS->sHeader.nROISize >= sizeof(RSWFrame) &&
1952
158
        poDS->sHeader.nROISize <= ROI_MAX_SIZE_TO_AVOID_EXCESSIVE_RAM_USAGE)
1953
0
    {
1954
0
        GByte *pabyROI = reinterpret_cast<GByte *>(
1955
0
            VSI_MALLOC_VERBOSE(poDS->sHeader.nROISize));
1956
0
        if (pabyROI == nullptr)
1957
0
        {
1958
0
            delete poDS;
1959
0
            return nullptr;
1960
0
        }
1961
1962
0
        VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nROIOffset),
1963
0
                  SEEK_SET);
1964
0
        if (VSIFReadL(pabyROI, poDS->sHeader.nROISize, 1, poDS->fp) != 1)
1965
0
        {
1966
0
            CPLError(CE_Failure, CPLE_FileIO, "Cannot read ROI");
1967
0
            CPLFree(pabyROI);
1968
0
            delete poDS;
1969
0
            return nullptr;
1970
0
        }
1971
1972
0
        GInt32 nFrameType;
1973
0
        RMF_READ_LONG(pabyROI, nFrameType, 0);
1974
0
        if (nFrameType == nPolygonType)
1975
0
        {
1976
0
            CPLString osWKT = "POLYGON((";
1977
0
            bool bFirst = true;
1978
1979
0
            CPLDebug("RMF", "ROI coordinates:");
1980
            /* coverity[tainted_data] */
1981
0
            for (GUInt32 i = sizeof(RSWFrame);
1982
0
                 i + sizeof(RSWFrameCoord) <= poDS->sHeader.nROISize;
1983
0
                 i += sizeof(RSWFrameCoord))
1984
0
            {
1985
0
                GInt32 nX, nY;
1986
0
                RMF_READ_LONG(pabyROI, nX, i);
1987
0
                RMF_READ_LONG(pabyROI, nY, i + 4);
1988
1989
0
                CPLDebug("RMF", "X: %d, Y: %d", nX, nY);
1990
1991
0
                double dfX = poDS->adfGeoTransform[0] +
1992
0
                             nX * poDS->adfGeoTransform[1] +
1993
0
                             nY * poDS->adfGeoTransform[2];
1994
0
                double dfY = poDS->adfGeoTransform[3] +
1995
0
                             nX * poDS->adfGeoTransform[4] +
1996
0
                             nY * poDS->adfGeoTransform[5];
1997
1998
0
                if (bFirst)
1999
0
                {
2000
0
                    osWKT += CPLSPrintf("%f %f", dfX, dfY);
2001
0
                    bFirst = false;
2002
0
                }
2003
0
                else
2004
0
                {
2005
0
                    osWKT += CPLSPrintf(", %f %f", dfX, dfY);
2006
0
                }
2007
0
            }
2008
0
            osWKT += "))";
2009
0
            CPLDebug("RMF", "Frame WKT: %s", osWKT.c_str());
2010
0
            poDS->SetMetadataItem(MD_FRAME_KEY, osWKT);
2011
0
        }
2012
0
        CPLFree(pabyROI);
2013
0
    }
2014
2015
158
#undef RMF_READ_DOUBLE
2016
158
#undef RMF_READ_LONG
2017
158
#undef RMF_READ_ULONG
2018
2019
158
    if (poDS->sHeader.nFlagsTblOffset && poDS->sHeader.nFlagsTblSize)
2020
82
    {
2021
82
        VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nFlagsTblOffset),
2022
82
                  SEEK_SET);
2023
82
        CPLDebug("RMF", "Blocks flags:");
2024
        /* coverity[tainted_data] */
2025
317k
        for (GUInt32 i = 0; i < poDS->sHeader.nFlagsTblSize; i += sizeof(GByte))
2026
317k
        {
2027
317k
            GByte nValue;
2028
317k
            if (VSIFReadL(&nValue, 1, sizeof(nValue), poDS->fp) !=
2029
317k
                sizeof(nValue))
2030
81
            {
2031
81
                CPLDebug("RMF", "Cannot read Block flag at index %u", i);
2032
81
                break;
2033
81
            }
2034
317k
            CPLDebug("RMF", "Block %u -- flag %d", i, nValue);
2035
317k
        }
2036
82
    }
2037
158
    return poDS;
2038
158
}
2039
2040
/************************************************************************/
2041
/*                               Create()                               */
2042
/************************************************************************/
2043
GDALDataset *RMFDataset::Create(const char *pszFilename, int nXSize, int nYSize,
2044
                                int nBandsIn, GDALDataType eType,
2045
                                char **papszParamList)
2046
0
{
2047
0
    return Create(pszFilename, nXSize, nYSize, nBandsIn, eType, papszParamList,
2048
0
                  nullptr, 1.0);
2049
0
}
2050
2051
GDALDataset *RMFDataset::Create(const char *pszFilename, int nXSize, int nYSize,
2052
                                int nBandsIn, GDALDataType eType,
2053
                                char **papszParamList, RMFDataset *poParentDS,
2054
                                double dfOvFactor)
2055
2056
0
{
2057
0
    if (nBandsIn != 1 && nBandsIn != 3)
2058
0
    {
2059
0
        CPLError(CE_Failure, CPLE_NotSupported,
2060
0
                 "RMF driver doesn't support %d bands. Must be 1 or 3.",
2061
0
                 nBandsIn);
2062
2063
0
        return nullptr;
2064
0
    }
2065
2066
0
    if (nBandsIn == 1 && eType != GDT_Byte && eType != GDT_Int16 &&
2067
0
        eType != GDT_Int32 && eType != GDT_Float64)
2068
0
    {
2069
0
        CPLError(
2070
0
            CE_Failure, CPLE_AppDefined,
2071
0
            "Attempt to create RMF dataset with an illegal data type (%s), "
2072
0
            "only Byte, Int16, Int32 and Float64 types supported "
2073
0
            "by the format for single-band images.",
2074
0
            GDALGetDataTypeName(eType));
2075
2076
0
        return nullptr;
2077
0
    }
2078
2079
0
    if (nBandsIn == 3 && eType != GDT_Byte)
2080
0
    {
2081
0
        CPLError(
2082
0
            CE_Failure, CPLE_AppDefined,
2083
0
            "Attempt to create RMF dataset with an illegal data type (%s), "
2084
0
            "only Byte type supported by the format for three-band images.",
2085
0
            GDALGetDataTypeName(eType));
2086
2087
0
        return nullptr;
2088
0
    }
2089
2090
    /* -------------------------------------------------------------------- */
2091
    /*  Create the dataset.                                                 */
2092
    /* -------------------------------------------------------------------- */
2093
0
    RMFDataset *poDS = new RMFDataset();
2094
2095
0
    GUInt32 nBlockXSize =
2096
0
        (nXSize < RMF_DEFAULT_BLOCKXSIZE) ? nXSize : RMF_DEFAULT_BLOCKXSIZE;
2097
0
    GUInt32 nBlockYSize =
2098
0
        (nYSize < RMF_DEFAULT_BLOCKYSIZE) ? nYSize : RMF_DEFAULT_BLOCKYSIZE;
2099
0
    double dfScale;
2100
0
    double dfResolution;
2101
0
    double dfPixelSize;
2102
0
    if (poParentDS == nullptr)
2103
0
    {
2104
0
        poDS->fp = VSIFOpenL(pszFilename, "w+b");
2105
0
        if (poDS->fp == nullptr)
2106
0
        {
2107
0
            CPLError(CE_Failure, CPLE_OpenFailed, "Unable to create file %s.",
2108
0
                     pszFilename);
2109
0
            delete poDS;
2110
0
            return nullptr;
2111
0
        }
2112
2113
0
        const char *pszScaleValue =
2114
0
            CSLFetchNameValue(papszParamList, MD_SCALE_KEY);
2115
0
        if (pszScaleValue != nullptr && CPLStrnlen(pszScaleValue, 10) > 4)
2116
0
        {
2117
0
            dfScale = atof(pszScaleValue + 4);
2118
0
        }
2119
0
        else
2120
0
        {
2121
0
            dfScale = RMF_DEFAULT_SCALE;
2122
0
        }
2123
0
        dfResolution = RMF_DEFAULT_RESOLUTION;
2124
0
        dfPixelSize = 1;
2125
2126
0
        if (CPLFetchBool(papszParamList, "MTW", false))
2127
0
            poDS->eRMFType = RMFT_MTW;
2128
0
        else
2129
0
            poDS->eRMFType = RMFT_RSW;
2130
2131
0
        GUInt32 iVersion = RMF_VERSION;
2132
0
        const char *pszRMFHUGE = CSLFetchNameValue(papszParamList, "RMFHUGE");
2133
2134
0
        if (pszRMFHUGE == nullptr)
2135
0
            pszRMFHUGE = "NO";  // Keep old behavior by default
2136
2137
0
        if (EQUAL(pszRMFHUGE, "NO"))
2138
0
        {
2139
0
            iVersion = RMF_VERSION;
2140
0
        }
2141
0
        else if (EQUAL(pszRMFHUGE, "YES"))
2142
0
        {
2143
0
            iVersion = RMF_VERSION_HUGE;
2144
0
        }
2145
0
        else if (EQUAL(pszRMFHUGE, "IF_SAFER"))
2146
0
        {
2147
0
            const double dfImageSize =
2148
0
                static_cast<double>(nXSize) * static_cast<double>(nYSize) *
2149
0
                static_cast<double>(nBandsIn) *
2150
0
                static_cast<double>(GDALGetDataTypeSizeBytes(eType));
2151
0
            if (dfImageSize > 3.0 * 1024.0 * 1024.0 * 1024.0)
2152
0
            {
2153
0
                iVersion = RMF_VERSION_HUGE;
2154
0
            }
2155
0
            else
2156
0
            {
2157
0
                iVersion = RMF_VERSION;
2158
0
            }
2159
0
        }
2160
2161
0
        const char *pszValue = CSLFetchNameValue(papszParamList, "BLOCKXSIZE");
2162
0
        if (pszValue != nullptr)
2163
0
            nBlockXSize = atoi(pszValue);
2164
0
        if (static_cast<int>(nBlockXSize) <= 0)
2165
0
            nBlockXSize = RMF_DEFAULT_BLOCKXSIZE;
2166
2167
0
        pszValue = CSLFetchNameValue(papszParamList, "BLOCKYSIZE");
2168
0
        if (pszValue != nullptr)
2169
0
            nBlockYSize = atoi(pszValue);
2170
0
        if (static_cast<int>(nBlockYSize) <= 0)
2171
0
            nBlockYSize = RMF_DEFAULT_BLOCKXSIZE;
2172
2173
0
        if (poDS->eRMFType == RMFT_MTW)
2174
0
            memcpy(poDS->sHeader.bySignature, RMF_SigMTW, RMF_SIGNATURE_SIZE);
2175
0
        else
2176
0
            memcpy(poDS->sHeader.bySignature, RMF_SigRSW, RMF_SIGNATURE_SIZE);
2177
0
        poDS->sHeader.iVersion = iVersion;
2178
0
        poDS->sHeader.nOvrOffset = 0x00;
2179
0
    }
2180
0
    else
2181
0
    {
2182
0
        poDS->fp = poParentDS->fp;
2183
0
        memcpy(poDS->sHeader.bySignature, poParentDS->sHeader.bySignature,
2184
0
               RMF_SIGNATURE_SIZE);
2185
0
        poDS->sHeader.iVersion = poParentDS->sHeader.iVersion;
2186
0
        poDS->eRMFType = poParentDS->eRMFType;
2187
0
        nBlockXSize = poParentDS->sHeader.nTileWidth;
2188
0
        nBlockYSize = poParentDS->sHeader.nTileHeight;
2189
0
        dfScale = poParentDS->sHeader.dfScale;
2190
0
        dfResolution = poParentDS->sHeader.dfResolution / dfOvFactor;
2191
0
        dfPixelSize = poParentDS->sHeader.dfPixelSize * dfOvFactor;
2192
2193
0
        poDS->nHeaderOffset = poParentDS->GetLastOffset();
2194
0
        poParentDS->sHeader.nOvrOffset =
2195
0
            poDS->GetRMFOffset(poDS->nHeaderOffset, &poDS->nHeaderOffset);
2196
0
        poParentDS->bHeaderDirty = true;
2197
0
        VSIFSeekL(poDS->fp, poDS->nHeaderOffset, SEEK_SET);
2198
0
        poDS->poParentDS = poParentDS;
2199
0
        CPLDebug("RMF",
2200
0
                 "Create overview subfile at " CPL_FRMT_GUIB
2201
0
                 " with size %dx%d, parent overview offset %d",
2202
0
                 poDS->nHeaderOffset, nXSize, nYSize,
2203
0
                 poParentDS->sHeader.nOvrOffset);
2204
0
    }
2205
    /* -------------------------------------------------------------------- */
2206
    /*  Fill the RMFHeader                                                  */
2207
    /* -------------------------------------------------------------------- */
2208
0
    CPLDebug("RMF", "Version %d", poDS->sHeader.iVersion);
2209
2210
0
    poDS->sHeader.iUserID = 0x00;
2211
0
    memset(poDS->sHeader.byName, 0, sizeof(poDS->sHeader.byName));
2212
0
    poDS->sHeader.nBitDepth = GDALGetDataTypeSizeBits(eType) * nBandsIn;
2213
0
    poDS->sHeader.nHeight = nYSize;
2214
0
    poDS->sHeader.nWidth = nXSize;
2215
0
    poDS->sHeader.nTileWidth = nBlockXSize;
2216
0
    poDS->sHeader.nTileHeight = nBlockYSize;
2217
2218
0
    poDS->nXTiles = poDS->sHeader.nXTiles =
2219
0
        DIV_ROUND_UP(nXSize, poDS->sHeader.nTileWidth);
2220
0
    poDS->nYTiles = poDS->sHeader.nYTiles =
2221
0
        DIV_ROUND_UP(nYSize, poDS->sHeader.nTileHeight);
2222
0
    poDS->sHeader.nLastTileHeight = nYSize % poDS->sHeader.nTileHeight;
2223
0
    if (!poDS->sHeader.nLastTileHeight)
2224
0
        poDS->sHeader.nLastTileHeight = poDS->sHeader.nTileHeight;
2225
0
    poDS->sHeader.nLastTileWidth = nXSize % poDS->sHeader.nTileWidth;
2226
0
    if (!poDS->sHeader.nLastTileWidth)
2227
0
        poDS->sHeader.nLastTileWidth = poDS->sHeader.nTileWidth;
2228
2229
    // poDS->sHeader.nROIOffset = 0x00;
2230
    // poDS->sHeader.nROISize = 0x00;
2231
2232
0
    vsi_l_offset nCurPtr = poDS->nHeaderOffset + RMF_HEADER_SIZE;
2233
2234
    // Extended header
2235
0
    poDS->sHeader.nExtHdrOffset = poDS->GetRMFOffset(nCurPtr, &nCurPtr);
2236
0
    poDS->sHeader.nExtHdrSize = RMF_EXT_HEADER_SIZE;
2237
0
    nCurPtr += poDS->sHeader.nExtHdrSize;
2238
2239
    // Color table
2240
0
    if (poDS->eRMFType == RMFT_RSW && nBandsIn == 1)
2241
0
    {
2242
0
        if (poDS->sHeader.nBitDepth > 8)
2243
0
        {
2244
0
            CPLError(CE_Failure, CPLE_AppDefined,
2245
0
                     "Cannot create color table of RSW with nBitDepth = %d. "
2246
0
                     "Retry with MTW ?",
2247
0
                     poDS->sHeader.nBitDepth);
2248
0
            delete poDS;
2249
0
            return nullptr;
2250
0
        }
2251
2252
0
        poDS->sHeader.nClrTblOffset = poDS->GetRMFOffset(nCurPtr, &nCurPtr);
2253
0
        poDS->nColorTableSize = 1 << poDS->sHeader.nBitDepth;
2254
0
        poDS->sHeader.nClrTblSize = poDS->nColorTableSize * 4;
2255
0
        poDS->pabyColorTable =
2256
0
            static_cast<GByte *>(VSI_MALLOC_VERBOSE(poDS->sHeader.nClrTblSize));
2257
0
        if (poDS->pabyColorTable == nullptr)
2258
0
        {
2259
0
            delete poDS;
2260
0
            return nullptr;
2261
0
        }
2262
0
        for (GUInt32 i = 0; i < poDS->nColorTableSize; i++)
2263
0
        {
2264
0
            poDS->pabyColorTable[i * 4 + 0] = static_cast<GByte>(i);
2265
0
            poDS->pabyColorTable[i * 4 + 1] = static_cast<GByte>(i);
2266
0
            poDS->pabyColorTable[i * 4 + 2] = static_cast<GByte>(i);
2267
0
            poDS->pabyColorTable[i * 4 + 3] = 0;
2268
0
        }
2269
0
        nCurPtr += poDS->sHeader.nClrTblSize;
2270
0
    }
2271
0
    else
2272
0
    {
2273
0
        poDS->sHeader.nClrTblOffset = 0x00;
2274
0
        poDS->sHeader.nClrTblSize = 0x00;
2275
0
    }
2276
2277
    // Add room for ROI (frame)
2278
0
    poDS->sHeader.nROIOffset = poDS->GetRMFOffset(nCurPtr, &nCurPtr);
2279
0
    poDS->sHeader.nROISize = 0x00;
2280
0
    nCurPtr +=
2281
0
        sizeof(RSWFrame) +
2282
0
        sizeof(RSWFrameCoord) *
2283
0
            nMaxFramePointCount;  // Allocate nMaxFramePointCount coordinates for frame
2284
2285
    // Add blocks flags
2286
0
    poDS->sHeader.nFlagsTblOffset = poDS->GetRMFOffset(nCurPtr, &nCurPtr);
2287
0
    poDS->sHeader.nFlagsTblSize =
2288
0
        sizeof(GByte) * poDS->sHeader.nXTiles * poDS->sHeader.nYTiles;
2289
0
    nCurPtr += poDS->sHeader.nFlagsTblSize;
2290
2291
    // Blocks table
2292
0
    poDS->sHeader.nTileTblOffset = poDS->GetRMFOffset(nCurPtr, &nCurPtr);
2293
0
    poDS->sHeader.nTileTblSize =
2294
0
        2 * sizeof(GUInt32) * poDS->sHeader.nXTiles * poDS->sHeader.nYTiles;
2295
0
    poDS->paiTiles =
2296
0
        reinterpret_cast<GUInt32 *>(CPLCalloc(poDS->sHeader.nTileTblSize, 1));
2297
    // nCurPtr += poDS->sHeader.nTileTblSize;
2298
0
    const GUInt32 nTileSize = poDS->sHeader.nTileWidth *
2299
0
                              poDS->sHeader.nTileHeight *
2300
0
                              GDALGetDataTypeSizeBytes(eType);
2301
0
    poDS->sHeader.nSize =
2302
0
        poDS->paiTiles[poDS->sHeader.nTileTblSize / 4 - 2] + nTileSize;
2303
2304
    // Elevation units
2305
0
    poDS->sHeader.iElevationUnit = RMFStrToUnitType(poDS->pszUnitType);
2306
2307
0
    poDS->sHeader.iMapType = -1;
2308
0
    poDS->sHeader.iProjection = -1;
2309
0
    poDS->sHeader.iEPSGCode = -1;
2310
0
    poDS->sHeader.dfScale = dfScale;
2311
0
    poDS->sHeader.dfResolution = dfResolution;
2312
0
    poDS->sHeader.dfPixelSize = dfPixelSize;
2313
0
    poDS->sHeader.iMaskType = 0;
2314
0
    poDS->sHeader.iMaskStep = 0;
2315
0
    poDS->sHeader.iFrameFlag = 1;  // 1 - Frame not using
2316
    // poDS->sHeader.nFlagsTblOffset = 0x00;
2317
    // poDS->sHeader.nFlagsTblSize = 0x00;
2318
0
    poDS->sHeader.nFileSize0 = 0x00;
2319
0
    poDS->sHeader.nFileSize1 = 0x00;
2320
0
    poDS->sHeader.iUnknown = 0;
2321
0
    poDS->sHeader.iGeorefFlag = 0;
2322
0
    poDS->sHeader.iInverse = 0;
2323
0
    poDS->sHeader.iJpegQuality = 0;
2324
0
    memset(poDS->sHeader.abyInvisibleColors, 0,
2325
0
           sizeof(poDS->sHeader.abyInvisibleColors));
2326
0
    poDS->sHeader.iElevationType = 0;
2327
2328
0
    poDS->nRasterXSize = nXSize;
2329
0
    poDS->nRasterYSize = nYSize;
2330
0
    poDS->eAccess = GA_Update;
2331
0
    poDS->nBands = nBandsIn;
2332
2333
0
    if (poParentDS == nullptr)
2334
0
    {
2335
0
        poDS->sHeader.adfElevMinMax[0] = 0.0;
2336
0
        poDS->sHeader.adfElevMinMax[1] = 0.0;
2337
0
        poDS->sHeader.dfNoData = 0.0;
2338
0
        poDS->sHeader.iCompression =
2339
0
            GetCompressionType(CSLFetchNameValue(papszParamList, "COMPRESS"));
2340
0
        if (CE_None != poDS->InitCompressorData(papszParamList))
2341
0
        {
2342
0
            delete poDS;
2343
0
            return nullptr;
2344
0
        }
2345
2346
0
        if (poDS->sHeader.iCompression == RMF_COMPRESSION_JPEG)
2347
0
        {
2348
0
            const char *pszJpegQuality =
2349
0
                CSLFetchNameValue(papszParamList, "JPEG_QUALITY");
2350
0
            if (pszJpegQuality == nullptr)
2351
0
            {
2352
0
                poDS->sHeader.iJpegQuality = 75;
2353
0
            }
2354
0
            else
2355
0
            {
2356
0
                int iJpegQuality = atoi(pszJpegQuality);
2357
0
                if (iJpegQuality < 10 || iJpegQuality > 100)
2358
0
                {
2359
0
                    CPLError(CE_Failure, CPLE_IllegalArg,
2360
0
                             "JPEG_QUALITY=%s is not a legal value in the "
2361
0
                             "range 10-100.\n"
2362
0
                             "Defaulting to 75",
2363
0
                             pszJpegQuality);
2364
0
                    iJpegQuality = 75;
2365
0
                }
2366
0
                poDS->sHeader.iJpegQuality = static_cast<GByte>(iJpegQuality);
2367
0
            }
2368
0
        }
2369
2370
0
        if (CE_None != poDS->SetupCompression(eType, pszFilename))
2371
0
        {
2372
0
            delete poDS;
2373
0
            return nullptr;
2374
0
        }
2375
0
    }
2376
0
    else
2377
0
    {
2378
0
        poDS->sHeader.adfElevMinMax[0] = poParentDS->sHeader.adfElevMinMax[0];
2379
0
        poDS->sHeader.adfElevMinMax[1] = poParentDS->sHeader.adfElevMinMax[1];
2380
0
        poDS->sHeader.dfNoData = poParentDS->sHeader.dfNoData;
2381
0
        poDS->sHeader.iCompression = poParentDS->sHeader.iCompression;
2382
0
        poDS->sHeader.iJpegQuality = poParentDS->sHeader.iJpegQuality;
2383
0
        poDS->Decompress = poParentDS->Decompress;
2384
0
        poDS->Compress = poParentDS->Compress;
2385
0
        poDS->poCompressData = poParentDS->poCompressData;
2386
0
    }
2387
2388
0
    if (nBandsIn > 1)
2389
0
    {
2390
0
        poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
2391
0
    }
2392
2393
0
    poDS->WriteHeader();
2394
2395
    /* -------------------------------------------------------------------- */
2396
    /*      Create band information objects.                                */
2397
    /* -------------------------------------------------------------------- */
2398
0
    for (int iBand = 1; iBand <= poDS->nBands; iBand++)
2399
0
        poDS->SetBand(iBand, new RMFRasterBand(poDS, iBand, eType));
2400
2401
0
    poDS->SetupNBits();
2402
2403
0
    return GDALDataset::FromHandle(poDS);
2404
0
}
2405
2406
// GIS Panorama 11 was introduced new format for huge files (greater than 3 Gb)
2407
vsi_l_offset RMFDataset::GetFileOffset(GUInt32 iRMFOffset) const
2408
546
{
2409
546
    if (sHeader.iVersion >= RMF_VERSION_HUGE)
2410
7
    {
2411
7
        return (static_cast<vsi_l_offset>(iRMFOffset)) * RMF_HUGE_OFFSET_FACTOR;
2412
7
    }
2413
2414
539
    return static_cast<vsi_l_offset>(iRMFOffset);
2415
546
}
2416
2417
GUInt32 RMFDataset::GetRMFOffset(vsi_l_offset nFileOffset,
2418
                                 vsi_l_offset *pnNewFileOffset) const
2419
0
{
2420
0
    if (sHeader.iVersion >= RMF_VERSION_HUGE)
2421
0
    {
2422
        // Round offset to next RMF_HUGE_OFFSET_FACTOR
2423
0
        const GUInt32 iRMFOffset =
2424
0
            static_cast<GUInt32>((nFileOffset + (RMF_HUGE_OFFSET_FACTOR - 1)) /
2425
0
                                 RMF_HUGE_OFFSET_FACTOR);
2426
0
        if (pnNewFileOffset != nullptr)
2427
0
        {
2428
0
            *pnNewFileOffset = GetFileOffset(iRMFOffset);
2429
0
        }
2430
0
        return iRMFOffset;
2431
0
    }
2432
2433
0
    if (pnNewFileOffset != nullptr)
2434
0
    {
2435
0
        *pnNewFileOffset = nFileOffset;
2436
0
    }
2437
0
    return static_cast<GUInt32>(nFileOffset);
2438
0
}
2439
2440
RMFDataset *RMFDataset::OpenOverview(RMFDataset *poParent,
2441
                                     GDALOpenInfo *poOpenInfo)
2442
158
{
2443
158
    if (sHeader.nOvrOffset == 0)
2444
4
    {
2445
4
        return nullptr;
2446
4
    }
2447
2448
154
    if (poParent == nullptr)
2449
0
    {
2450
0
        return nullptr;
2451
0
    }
2452
2453
154
    vsi_l_offset nSubOffset = GetFileOffset(sHeader.nOvrOffset);
2454
2455
154
    CPLDebug("RMF",
2456
154
             "Try to open overview subfile at " CPL_FRMT_GUIB " for '%s'",
2457
154
             nSubOffset, poOpenInfo->pszFilename);
2458
2459
154
    if (!poParent->poOvrDatasets.empty())
2460
0
    {
2461
0
        if (poParent->GetFileOffset(poParent->sHeader.nOvrOffset) == nSubOffset)
2462
0
        {
2463
0
            CPLError(CE_Warning, CPLE_IllegalArg,
2464
0
                     "Recursive subdataset list is detected. "
2465
0
                     "Overview open failed.");
2466
0
            return nullptr;
2467
0
        }
2468
2469
0
        for (size_t n = 0; n != poParent->poOvrDatasets.size() - 1; ++n)
2470
0
        {
2471
0
            RMFDataset *poOvr(poParent->poOvrDatasets[n]);
2472
2473
0
            if (poOvr == nullptr)
2474
0
                continue;
2475
0
            if (poOvr->GetFileOffset(poOvr->sHeader.nOvrOffset) == nSubOffset)
2476
0
            {
2477
0
                CPLError(CE_Warning, CPLE_IllegalArg,
2478
0
                         "Recursive subdataset list is detected. "
2479
0
                         "Overview open failed.");
2480
0
                return nullptr;
2481
0
            }
2482
0
        }
2483
0
    }
2484
2485
154
    size_t nHeaderSize(RMF_HEADER_SIZE);
2486
154
    GByte *pabyNewHeader;
2487
154
    pabyNewHeader = static_cast<GByte *>(
2488
154
        CPLRealloc(poOpenInfo->pabyHeader, nHeaderSize + 1));
2489
154
    if (pabyNewHeader == nullptr)
2490
0
    {
2491
0
        CPLError(CE_Warning, CPLE_OutOfMemory,
2492
0
                 "Can't allocate buffer for overview header");
2493
0
        return nullptr;
2494
0
    }
2495
2496
154
    poOpenInfo->pabyHeader = pabyNewHeader;
2497
154
    memset(poOpenInfo->pabyHeader, 0, nHeaderSize + 1);
2498
154
    VSIFSeekL(fp, nSubOffset, SEEK_SET);
2499
154
    poOpenInfo->nHeaderBytes =
2500
154
        static_cast<int>(VSIFReadL(poOpenInfo->pabyHeader, 1, nHeaderSize, fp));
2501
2502
154
    return Open(poOpenInfo, poParent, nSubOffset);
2503
154
}
2504
2505
CPLErr RMFDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
2506
                                   const int *panOverviewList, int nBandsIn,
2507
                                   const int *panBandList,
2508
                                   GDALProgressFunc pfnProgress,
2509
                                   void *pProgressData,
2510
                                   CSLConstList papszOptions)
2511
0
{
2512
0
    bool bUseGenericHandling = false;
2513
2514
0
    if (GetAccess() != GA_Update)
2515
0
    {
2516
0
        CPLDebug("RMF", "File open for read-only accessing, "
2517
0
                        "creating overviews externally.");
2518
2519
0
        bUseGenericHandling = true;
2520
0
    }
2521
2522
0
    if (bUseGenericHandling)
2523
0
    {
2524
0
        if (!poOvrDatasets.empty())
2525
0
        {
2526
0
            CPLError(CE_Failure, CPLE_NotSupported,
2527
0
                     "Cannot add external overviews when there are already "
2528
0
                     "internal overviews");
2529
0
            return CE_Failure;
2530
0
        }
2531
2532
0
        return GDALDataset::IBuildOverviews(
2533
0
            pszResampling, nOverviews, panOverviewList, nBandsIn, panBandList,
2534
0
            pfnProgress, pProgressData, papszOptions);
2535
0
    }
2536
2537
0
    if (nBandsIn != GetRasterCount())
2538
0
    {
2539
0
        CPLError(CE_Failure, CPLE_NotSupported,
2540
0
                 "Generation of overviews in RMF is only "
2541
0
                 "supported when operating on all bands.  "
2542
0
                 "Operation failed.");
2543
0
        return CE_Failure;
2544
0
    }
2545
2546
0
    if (nOverviews == 0)
2547
0
    {
2548
0
        if (poOvrDatasets.empty())
2549
0
        {
2550
0
            return GDALDataset::IBuildOverviews(
2551
0
                pszResampling, nOverviews, panOverviewList, nBandsIn,
2552
0
                panBandList, pfnProgress, pProgressData, papszOptions);
2553
0
        }
2554
0
        return CleanOverviews();
2555
0
    }
2556
2557
    // First destroy old overviews
2558
0
    if (CE_None != CleanOverviews())
2559
0
    {
2560
0
        return CE_Failure;
2561
0
    }
2562
2563
0
    CPLDebug("RMF", "Build overviews on dataset %d x %d size", GetRasterXSize(),
2564
0
             GetRasterYSize());
2565
2566
0
    GDALDataType eMainType = GetRasterBand(1)->GetRasterDataType();
2567
0
    RMFDataset *poParent = this;
2568
0
    double prevOvLevel = 1.0;
2569
0
    for (int n = 0; n != nOverviews; ++n)
2570
0
    {
2571
0
        int nOvLevel = panOverviewList[n];
2572
0
        const int nOXSize = DIV_ROUND_UP(GetRasterXSize(), nOvLevel);
2573
0
        const int nOYSize = DIV_ROUND_UP(GetRasterYSize(), nOvLevel);
2574
0
        CPLDebug("RMF", "\tCreate overview #%d size %d x %d", nOvLevel, nOXSize,
2575
0
                 nOYSize);
2576
2577
0
        RMFDataset *poOvrDataset;
2578
0
        poOvrDataset = static_cast<RMFDataset *>(RMFDataset::Create(
2579
0
            nullptr, nOXSize, nOYSize, GetRasterCount(), eMainType, nullptr,
2580
0
            poParent, nOvLevel / prevOvLevel));
2581
2582
0
        if (poOvrDataset == nullptr)
2583
0
        {
2584
0
            CPLError(CE_Failure, CPLE_AppDefined,
2585
0
                     "Can't create overview dataset #%d size %d x %d", nOvLevel,
2586
0
                     nOXSize, nOYSize);
2587
0
            return CE_Failure;
2588
0
        }
2589
2590
0
        prevOvLevel = nOvLevel;
2591
0
        poParent = poOvrDataset;
2592
0
        poOvrDatasets.push_back(poOvrDataset);
2593
0
    }
2594
2595
0
    GDALRasterBand ***papapoOverviewBands =
2596
0
        static_cast<GDALRasterBand ***>(CPLCalloc(sizeof(void *), nBandsIn));
2597
0
    GDALRasterBand **papoBandList =
2598
0
        static_cast<GDALRasterBand **>(CPLCalloc(sizeof(void *), nBandsIn));
2599
2600
0
    for (int iBand = 0; iBand < nBandsIn; ++iBand)
2601
0
    {
2602
0
        GDALRasterBand *poBand = GetRasterBand(panBandList[iBand]);
2603
2604
0
        papoBandList[iBand] = poBand;
2605
0
        papapoOverviewBands[iBand] = static_cast<GDALRasterBand **>(
2606
0
            CPLCalloc(sizeof(void *), poBand->GetOverviewCount()));
2607
2608
0
        for (int i = 0; i < nOverviews; ++i)
2609
0
        {
2610
0
            papapoOverviewBands[iBand][i] = poBand->GetOverview(i);
2611
0
        }
2612
0
    }
2613
#ifdef DEBUG
2614
    for (int iBand = 0; iBand < nBandsIn; ++iBand)
2615
    {
2616
        CPLDebug("RMF", "Try to create overview for #%d size %d x %d",
2617
                 iBand + 1, papoBandList[iBand]->GetXSize(),
2618
                 papoBandList[iBand]->GetYSize());
2619
        for (int i = 0; i < nOverviews; ++i)
2620
        {
2621
            CPLDebug("RMF", "\t%d x %d",
2622
                     papapoOverviewBands[iBand][i]->GetXSize(),
2623
                     papapoOverviewBands[iBand][i]->GetYSize());
2624
        }
2625
    }
2626
#endif  // DEBUG
2627
0
    CPLErr res;
2628
0
    res = GDALRegenerateOverviewsMultiBand(
2629
0
        nBandsIn, papoBandList, nOverviews, papapoOverviewBands, pszResampling,
2630
0
        pfnProgress, pProgressData, papszOptions);
2631
2632
0
    for (int iBand = 0; iBand < nBandsIn; ++iBand)
2633
0
    {
2634
0
        CPLFree(papapoOverviewBands[iBand]);
2635
0
    }
2636
2637
0
    CPLFree(papapoOverviewBands);
2638
0
    CPLFree(papoBandList);
2639
2640
0
    return res;
2641
0
}
2642
2643
CPLErr RMFDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
2644
                             int nXSize, int nYSize, void *pData, int nBufXSize,
2645
                             int nBufYSize, GDALDataType eBufType,
2646
                             int nBandCount, BANDMAP_TYPE panBandMap,
2647
                             GSpacing nPixelSpace, GSpacing nLineSpace,
2648
                             GSpacing nBandSpace,
2649
                             GDALRasterIOExtraArg *psExtraArg)
2650
0
{
2651
#ifdef DEBUG
2652
    CPLDebug("RMF", "Dataset %p, %s %d %d %d %d, %d %d", this,
2653
             (eRWFlag == GF_Read ? "Read" : "Write"), nXOff, nYOff, nXSize,
2654
             nYSize, nBufXSize, nBufYSize);
2655
#endif  // DEBUG
2656
0
    if (eRWFlag == GF_Read && poCompressData != nullptr &&
2657
0
        poCompressData->oThreadPool.GetThreadCount() > 0)
2658
0
    {
2659
0
        poCompressData->oThreadPool.WaitCompletion();
2660
0
    }
2661
2662
0
    return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
2663
0
                                  nBufXSize, nBufYSize, eBufType, nBandCount,
2664
0
                                  panBandMap, nPixelSpace, nLineSpace,
2665
0
                                  nBandSpace, psExtraArg);
2666
0
}
2667
2668
vsi_l_offset RMFDataset::GetLastOffset() const
2669
0
{
2670
0
    vsi_l_offset nLastTileOff = 0;
2671
0
    GUInt32 nTiles(sHeader.nTileTblSize / sizeof(GUInt32));
2672
2673
0
    for (GUInt32 n = 0; n < nTiles; n += 2)
2674
0
    {
2675
0
        vsi_l_offset nTileOffset = GetFileOffset(paiTiles[n]);
2676
0
        GUInt32 nTileBytes = paiTiles[n + 1];
2677
0
        nLastTileOff = std::max(nLastTileOff, nTileOffset + nTileBytes);
2678
0
    }
2679
2680
0
    nLastTileOff = std::max(nLastTileOff, GetFileOffset(sHeader.nROIOffset) +
2681
0
                                              sHeader.nROISize);
2682
0
    nLastTileOff = std::max(nLastTileOff, GetFileOffset(sHeader.nClrTblOffset) +
2683
0
                                              sHeader.nClrTblSize);
2684
0
    nLastTileOff =
2685
0
        std::max(nLastTileOff,
2686
0
                 GetFileOffset(sHeader.nTileTblOffset) + sHeader.nTileTblSize);
2687
0
    nLastTileOff =
2688
0
        std::max(nLastTileOff, GetFileOffset(sHeader.nFlagsTblOffset) +
2689
0
                                   sHeader.nFlagsTblSize);
2690
0
    nLastTileOff = std::max(nLastTileOff, GetFileOffset(sHeader.nExtHdrOffset) +
2691
0
                                              sHeader.nExtHdrSize);
2692
0
    return nLastTileOff;
2693
0
}
2694
2695
CPLErr RMFDataset::CleanOverviews()
2696
0
{
2697
0
    if (sHeader.nOvrOffset == 0)
2698
0
    {
2699
0
        return CE_None;
2700
0
    }
2701
2702
0
    if (GetAccess() != GA_Update)
2703
0
    {
2704
0
        CPLError(CE_Failure, CPLE_NotSupported,
2705
0
                 "File open for read-only accessing, "
2706
0
                 "overviews cleanup failed.");
2707
0
        return CE_Failure;
2708
0
    }
2709
2710
0
    if (poParentDS != nullptr)
2711
0
    {
2712
0
        CPLError(CE_Failure, CPLE_NotSupported,
2713
0
                 "Overviews cleanup for non-root dataset is not possible.");
2714
0
        return CE_Failure;
2715
0
    }
2716
2717
0
    for (size_t n = 0; n != poOvrDatasets.size(); ++n)
2718
0
    {
2719
0
        GDALClose(poOvrDatasets[n]);
2720
0
    }
2721
0
    poOvrDatasets.clear();
2722
2723
0
    vsi_l_offset nLastTileOff = GetLastOffset();
2724
2725
0
    if (0 != VSIFSeekL(fp, 0, SEEK_END))
2726
0
    {
2727
0
        CPLError(CE_Failure, CPLE_FileIO,
2728
0
                 "Failed to seek to end of file, "
2729
0
                 "overviews cleanup failed.");
2730
0
    }
2731
2732
0
    vsi_l_offset nFileSize = VSIFTellL(fp);
2733
0
    if (nFileSize < nLastTileOff)
2734
0
    {
2735
0
        CPLError(CE_Failure, CPLE_FileIO,
2736
0
                 "Invalid file offset, "
2737
0
                 "overviews cleanup failed.");
2738
0
        return CE_Failure;
2739
0
    }
2740
2741
0
    CPLDebug("RMF", "Truncate to " CPL_FRMT_GUIB, nLastTileOff);
2742
0
    CPLDebug("RMF", "File size:  " CPL_FRMT_GUIB, nFileSize);
2743
2744
0
    if (0 != VSIFTruncateL(fp, nLastTileOff))
2745
0
    {
2746
0
        CPLError(CE_Failure, CPLE_FileIO,
2747
0
                 "Failed to truncate file, "
2748
0
                 "overviews cleanup failed.");
2749
0
        return CE_Failure;
2750
0
    }
2751
2752
0
    sHeader.nOvrOffset = 0;
2753
0
    bHeaderDirty = true;
2754
2755
0
    return CE_None;
2756
0
}
2757
2758
/************************************************************************/
2759
/*                         GetCompressionType()                         */
2760
/************************************************************************/
2761
2762
GByte RMFDataset::GetCompressionType(const char *pszCompressName)
2763
0
{
2764
0
    if (pszCompressName == nullptr || EQUAL(pszCompressName, "NONE"))
2765
0
    {
2766
0
        return RMF_COMPRESSION_NONE;
2767
0
    }
2768
0
    else if (EQUAL(pszCompressName, "LZW"))
2769
0
    {
2770
0
        return RMF_COMPRESSION_LZW;
2771
0
    }
2772
0
    else if (EQUAL(pszCompressName, "JPEG"))
2773
0
    {
2774
0
        return RMF_COMPRESSION_JPEG;
2775
0
    }
2776
0
    else if (EQUAL(pszCompressName, "RMF_DEM"))
2777
0
    {
2778
0
        return RMF_COMPRESSION_DEM;
2779
0
    }
2780
2781
0
    CPLError(CE_Failure, CPLE_AppDefined,
2782
0
             "RMF: Unknown compression scheme <%s>.\n"
2783
0
             "Defaults to NONE compression.",
2784
0
             pszCompressName);
2785
0
    return RMF_COMPRESSION_NONE;
2786
0
}
2787
2788
/************************************************************************/
2789
/*                        SetupCompression()                            */
2790
/************************************************************************/
2791
2792
int RMFDataset::SetupCompression(GDALDataType eType, const char *pszFilename)
2793
159
{
2794
    /* -------------------------------------------------------------------- */
2795
    /*  XXX: The DEM compression method seems to be only applicable         */
2796
    /*  to Int32 data.                                                      */
2797
    /* -------------------------------------------------------------------- */
2798
159
    if (sHeader.iCompression == RMF_COMPRESSION_NONE)
2799
56
    {
2800
56
        Decompress = nullptr;
2801
56
        Compress = nullptr;
2802
56
    }
2803
103
    else if (sHeader.iCompression == RMF_COMPRESSION_LZW)
2804
102
    {
2805
102
        Decompress = &LZWDecompress;
2806
102
        Compress = &LZWCompress;
2807
102
        SetMetadataItem("COMPRESSION", "LZW", "IMAGE_STRUCTURE");
2808
102
    }
2809
1
    else if (sHeader.iCompression == RMF_COMPRESSION_JPEG)
2810
0
    {
2811
0
        if (eType != GDT_Byte || nBands != RMF_JPEG_BAND_COUNT ||
2812
0
            sHeader.nBitDepth != 24)
2813
0
        {
2814
0
            CPLError(CE_Failure, CPLE_AppDefined,
2815
0
                     "RMF support only 24 bpp JPEG compressed files.");
2816
0
            return CE_Failure;
2817
0
        }
2818
0
#ifdef HAVE_LIBJPEG
2819
0
        CPLString oBuf;
2820
0
        oBuf.Printf("%d", sHeader.iJpegQuality);
2821
0
        Decompress = &JPEGDecompress;
2822
0
        Compress = &JPEGCompress;
2823
0
        SetMetadataItem("JPEG_QUALITY", oBuf.c_str(), "IMAGE_STRUCTURE");
2824
0
        SetMetadataItem("COMPRESSION", "JPEG", "IMAGE_STRUCTURE");
2825
#else   // HAVE_LIBJPEG
2826
        CPLError(CE_Failure, CPLE_AppDefined,
2827
                 "JPEG codec is needed to open <%s>.\n"
2828
                 "Please rebuild GDAL with libjpeg support.",
2829
                 pszFilename);
2830
        return CE_Failure;
2831
#endif  // HAVE_LIBJPEG
2832
0
    }
2833
1
    else if (sHeader.iCompression == RMF_COMPRESSION_DEM &&
2834
1
             eType == GDT_Int32 && nBands == RMF_DEM_BAND_COUNT)
2835
0
    {
2836
0
        Decompress = &DEMDecompress;
2837
0
        Compress = &DEMCompress;
2838
0
        SetMetadataItem("COMPRESSION", "RMF_DEM", "IMAGE_STRUCTURE");
2839
0
    }
2840
1
    else
2841
1
    {
2842
1
        CPLError(CE_Failure, CPLE_AppDefined,
2843
1
                 "Unknown compression #%d at file <%s>.", sHeader.iCompression,
2844
1
                 pszFilename);
2845
1
        return CE_Failure;
2846
1
    }
2847
2848
158
    return CE_None;
2849
159
}
2850
2851
void RMFDataset::WriteTileJobFunc(void *pData)
2852
0
{
2853
0
    RMFCompressionJob *psJob = static_cast<RMFCompressionJob *>(pData);
2854
0
    RMFDataset *poDS = psJob->poDS;
2855
2856
0
    GByte *pabyTileData;
2857
0
    size_t nTileSize;
2858
2859
0
    if (poDS->Compress)
2860
0
    {
2861
        // RMF doesn't store compressed tiles with size greater than 80% of
2862
        // uncompressed size
2863
0
        GUInt32 nMaxCompressedTileSize =
2864
0
            static_cast<GUInt32>((psJob->nUncompressedBytes * 8) / 10);
2865
0
        size_t nCompressedBytes =
2866
0
            poDS->Compress(psJob->pabyUncompressedData,
2867
0
                           static_cast<GUInt32>(psJob->nUncompressedBytes),
2868
0
                           psJob->pabyCompressedData, nMaxCompressedTileSize,
2869
0
                           psJob->nXSize, psJob->nYSize, poDS);
2870
0
        if (nCompressedBytes == 0)
2871
0
        {
2872
0
            pabyTileData = psJob->pabyUncompressedData;
2873
0
            nTileSize = psJob->nUncompressedBytes;
2874
0
        }
2875
0
        else
2876
0
        {
2877
0
            pabyTileData = psJob->pabyCompressedData;
2878
0
            nTileSize = nCompressedBytes;
2879
0
        }
2880
0
    }
2881
0
    else
2882
0
    {
2883
0
        pabyTileData = psJob->pabyUncompressedData;
2884
0
        nTileSize = psJob->nUncompressedBytes;
2885
0
    }
2886
2887
0
    {
2888
0
        CPLMutexHolder oHolder(poDS->poCompressData->hWriteTileMutex);
2889
0
        psJob->eResult = poDS->WriteRawTile(
2890
0
            psJob->nBlockXOff, psJob->nBlockYOff, pabyTileData, nTileSize);
2891
0
    }
2892
0
    if (poDS->poCompressData->oThreadPool.GetThreadCount() > 0)
2893
0
    {
2894
0
        CPLMutexHolder oHolder(poDS->poCompressData->hReadyJobMutex);
2895
0
        poDS->poCompressData->asReadyJobs.push_back(psJob);
2896
0
    }
2897
0
}
2898
2899
CPLErr RMFDataset::InitCompressorData(char **papszParamList)
2900
0
{
2901
0
    const char *pszNumThreads =
2902
0
        CSLFetchNameValue(papszParamList, "NUM_THREADS");
2903
0
    if (pszNumThreads == nullptr)
2904
0
        pszNumThreads = CPLGetConfigOption("GDAL_NUM_THREADS", nullptr);
2905
2906
0
    int nThreads = 0;
2907
0
    if (pszNumThreads != nullptr)
2908
0
    {
2909
0
        nThreads = EQUAL(pszNumThreads, "ALL_CPUS") ? CPLGetNumCPUs()
2910
0
                                                    : atoi(pszNumThreads);
2911
0
    }
2912
2913
0
    if (nThreads < 0)
2914
0
    {
2915
0
        nThreads = 0;
2916
0
    }
2917
0
    if (nThreads > 1024)
2918
0
    {
2919
0
        nThreads = 1024;
2920
0
    }
2921
2922
0
    poCompressData = std::make_shared<RMFCompressData>();
2923
0
    if (nThreads > 0)
2924
0
    {
2925
0
        if (!poCompressData->oThreadPool.Setup(nThreads, nullptr, nullptr))
2926
0
        {
2927
0
            CPLError(CE_Failure, CPLE_AppDefined,
2928
0
                     "Can't setup %d compressor threads", nThreads);
2929
0
            return CE_Failure;
2930
0
        }
2931
0
    }
2932
2933
0
    poCompressData->asJobs.resize(nThreads + 1);
2934
2935
0
    size_t nMaxTileBytes =
2936
0
        sHeader.nTileWidth * sHeader.nTileHeight * sHeader.nBitDepth / 8;
2937
0
    size_t nCompressBufferSize =
2938
0
        2 * nMaxTileBytes * poCompressData->asJobs.size();
2939
0
    poCompressData->pabyBuffers =
2940
0
        static_cast<GByte *>(VSIMalloc(nCompressBufferSize));
2941
2942
0
    CPLDebug("RMF", "Setup %d compressor threads and allocate %lu bytes buffer",
2943
0
             nThreads, static_cast<unsigned long>(nCompressBufferSize));
2944
0
    if (poCompressData->pabyBuffers == nullptr)
2945
0
    {
2946
0
        CPLError(CE_Failure, CPLE_OutOfMemory,
2947
0
                 "Can't allocate compress buffer of size %lu.",
2948
0
                 static_cast<unsigned long>(nCompressBufferSize));
2949
0
        return CE_Failure;
2950
0
    }
2951
2952
0
    for (size_t i = 0; i != poCompressData->asJobs.size(); ++i)
2953
0
    {
2954
0
        RMFCompressionJob &sJob(poCompressData->asJobs[i]);
2955
0
        sJob.pabyCompressedData =
2956
0
            poCompressData->pabyBuffers + 2 * i * nMaxTileBytes;
2957
0
        sJob.pabyUncompressedData = sJob.pabyCompressedData + nMaxTileBytes;
2958
0
        poCompressData->asReadyJobs.push_back(&sJob);
2959
0
    }
2960
2961
0
    if (nThreads > 0)
2962
0
    {
2963
0
        poCompressData->hReadyJobMutex = CPLCreateMutex();
2964
0
        CPLReleaseMutex(poCompressData->hReadyJobMutex);
2965
0
        poCompressData->hWriteTileMutex = CPLCreateMutex();
2966
0
        CPLReleaseMutex(poCompressData->hWriteTileMutex);
2967
0
    }
2968
2969
0
    return CE_None;
2970
0
}
2971
2972
CPLErr RMFDataset::WriteTile(int nBlockXOff, int nBlockYOff, GByte *pabyData,
2973
                             size_t nBytes, GUInt32 nRawXSize,
2974
                             GUInt32 nRawYSize)
2975
0
{
2976
0
    RMFCompressionJob *poJob = nullptr;
2977
0
    if (poCompressData == nullptr)
2978
0
    {
2979
0
        CPLError(CE_Failure, CPLE_AppDefined, "RMF: Compress data is null");
2980
0
        return CE_Failure;
2981
0
    }
2982
2983
0
    if (poCompressData->oThreadPool.GetThreadCount() > 0)
2984
0
    {
2985
0
        size_t nJobs(poCompressData->asJobs.size());
2986
2987
0
        poCompressData->oThreadPool.WaitCompletion(static_cast<int>(nJobs - 1));
2988
2989
0
        CPLMutexHolder oHolder(poCompressData->hReadyJobMutex);
2990
0
        CPLAssert(!poCompressData->asReadyJobs.empty());
2991
0
        poJob = poCompressData->asReadyJobs.front();
2992
0
        poCompressData->asReadyJobs.pop_front();
2993
0
    }
2994
0
    else
2995
0
    {
2996
0
        poJob = poCompressData->asReadyJobs.front();
2997
0
    }
2998
2999
0
    if (poJob->eResult != CE_None)
3000
0
    {
3001
        // One of the previous jobs is not done.
3002
        // Detailed debug message is already emitted from WriteRawTile
3003
0
        return poJob->eResult;
3004
0
    }
3005
0
    poJob->poDS = this;
3006
0
    poJob->eResult = CE_Failure;
3007
0
    poJob->nBlockXOff = nBlockXOff;
3008
0
    poJob->nBlockYOff = nBlockYOff;
3009
0
    poJob->nUncompressedBytes = nBytes;
3010
0
    poJob->nXSize = nRawXSize;
3011
0
    poJob->nYSize = nRawYSize;
3012
3013
0
    memcpy(poJob->pabyUncompressedData, pabyData, nBytes);
3014
3015
0
    if (poCompressData->oThreadPool.GetThreadCount() > 0)
3016
0
    {
3017
0
        if (!poCompressData->oThreadPool.SubmitJob(WriteTileJobFunc, poJob))
3018
0
        {
3019
0
            CPLError(CE_Failure, CPLE_NotSupported,
3020
0
                     "Can't submit job to thread pool.");
3021
0
            return CE_Failure;
3022
0
        }
3023
0
    }
3024
0
    else
3025
0
    {
3026
0
        WriteTileJobFunc(poJob);
3027
0
        if (poJob->eResult != CE_None)
3028
0
        {
3029
0
            return poJob->eResult;
3030
0
        }
3031
0
    }
3032
3033
0
    return CE_None;
3034
0
}
3035
3036
CPLErr RMFDataset::WriteRawTile(int nBlockXOff, int nBlockYOff, GByte *pabyData,
3037
                                size_t nTileBytes)
3038
0
{
3039
0
    CPLAssert(nBlockXOff >= 0 && nBlockYOff >= 0 && pabyData != nullptr &&
3040
0
              nTileBytes > 0);
3041
3042
0
    const GUInt32 nTile = nBlockYOff * nXTiles + nBlockXOff;
3043
3044
0
    vsi_l_offset nTileOffset = GetFileOffset(paiTiles[2 * nTile]);
3045
0
    size_t nTileSize = static_cast<size_t>(paiTiles[2 * nTile + 1]);
3046
3047
0
    if (nTileOffset && nTileSize <= nTileBytes)
3048
0
    {
3049
0
        if (VSIFSeekL(fp, nTileOffset, SEEK_SET) < 0)
3050
0
        {
3051
0
            CPLError(
3052
0
                CE_Failure, CPLE_FileIO,
3053
0
                "Can't seek to offset %ld in output file to write data.\n%s",
3054
0
                static_cast<long>(nTileOffset), VSIStrerror(errno));
3055
0
            return CE_Failure;
3056
0
        }
3057
0
    }
3058
0
    else
3059
0
    {
3060
0
        if (VSIFSeekL(fp, 0, SEEK_END) < 0)
3061
0
        {
3062
0
            CPLError(
3063
0
                CE_Failure, CPLE_FileIO,
3064
0
                "Can't seek to offset %ld in output file to write data.\n%s",
3065
0
                static_cast<long>(nTileOffset), VSIStrerror(errno));
3066
0
            return CE_Failure;
3067
0
        }
3068
0
        nTileOffset = VSIFTellL(fp);
3069
0
        vsi_l_offset nNewTileOffset = 0;
3070
0
        paiTiles[2 * nTile] = GetRMFOffset(nTileOffset, &nNewTileOffset);
3071
3072
0
        if (nTileOffset != nNewTileOffset)
3073
0
        {
3074
0
            if (VSIFSeekL(fp, nNewTileOffset, SEEK_SET) < 0)
3075
0
            {
3076
0
                CPLError(CE_Failure, CPLE_FileIO,
3077
0
                         "Can't seek to offset %ld in output file to "
3078
0
                         "write data.\n%s",
3079
0
                         static_cast<long>(nNewTileOffset), VSIStrerror(errno));
3080
0
                return CE_Failure;
3081
0
            }
3082
0
        }
3083
0
        bHeaderDirty = true;
3084
0
    }
3085
3086
#ifdef CPL_MSB
3087
    // Compressed tiles are already with proper byte order
3088
    if (eRMFType == RMFT_MTW && sHeader.iCompression == RMF_COMPRESSION_NONE)
3089
    {
3090
        // Byte swap can be done in place
3091
        if (sHeader.nBitDepth == 16)
3092
        {
3093
            for (size_t i = 0; i < nTileBytes; i += 2)
3094
                CPL_SWAP16PTR(pabyData + i);
3095
        }
3096
        else if (sHeader.nBitDepth == 32)
3097
        {
3098
            for (size_t i = 0; i < nTileBytes; i += 4)
3099
                CPL_SWAP32PTR(pabyData + i);
3100
        }
3101
        else if (sHeader.nBitDepth == 64)
3102
        {
3103
            for (size_t i = 0; i < nTileBytes; i += 8)
3104
                CPL_SWAPDOUBLE(pabyData + i);
3105
        }
3106
    }
3107
#endif
3108
3109
0
    bool bOk = (VSIFWriteL(pabyData, 1, nTileBytes, fp) == nTileBytes);
3110
3111
0
    if (!bOk)
3112
0
    {
3113
0
        CPLError(CE_Failure, CPLE_FileIO,
3114
0
                 "Can't write tile with X offset %d and Y offset %d.\n%s",
3115
0
                 nBlockXOff, nBlockYOff, VSIStrerror(errno));
3116
0
        return CE_Failure;
3117
0
    }
3118
3119
0
    paiTiles[2 * nTile + 1] = static_cast<GUInt32>(nTileBytes);
3120
0
    bHeaderDirty = true;
3121
3122
0
    return CE_None;
3123
0
}
3124
3125
CPLErr RMFDataset::ReadTile(int nBlockXOff, int nBlockYOff, GByte *pabyData,
3126
                            size_t nRawBytes, GUInt32 nRawXSize,
3127
                            GUInt32 nRawYSize, bool &bNullTile)
3128
0
{
3129
0
    bNullTile = false;
3130
3131
0
    const GUInt32 nTile = nBlockYOff * nXTiles + nBlockXOff;
3132
0
    if (2 * nTile + 1 >= sHeader.nTileTblSize / sizeof(GUInt32))
3133
0
    {
3134
0
        return CE_Failure;
3135
0
    }
3136
0
    vsi_l_offset nTileOffset = GetFileOffset(paiTiles[2 * nTile]);
3137
0
    GUInt32 nTileBytes = paiTiles[2 * nTile + 1];
3138
    // RMF doesn't store compressed tiles with size greater than 80% of
3139
    // uncompressed size. But just in case, select twice as many.
3140
0
    GUInt32 nMaxTileBytes =
3141
0
        2 * sHeader.nTileWidth * sHeader.nTileHeight * sHeader.nBitDepth / 8;
3142
3143
0
    if (nTileBytes >= nMaxTileBytes)
3144
0
    {
3145
0
        CPLError(CE_Failure, CPLE_AppDefined,
3146
0
                 "Invalid tile size %lu at offset %ld. Must be less than %lu",
3147
0
                 static_cast<unsigned long>(nTileBytes),
3148
0
                 static_cast<long>(nTileOffset),
3149
0
                 static_cast<unsigned long>(nMaxTileBytes));
3150
0
        return CE_Failure;
3151
0
    }
3152
3153
0
    if (nTileOffset == 0)
3154
0
    {
3155
0
        bNullTile = true;
3156
0
        return CE_None;
3157
0
    }
3158
3159
#ifdef DEBUG
3160
    CPLDebug("RMF", "Read RawSize [%d, %d], nTileBytes %d, nRawBytes %d",
3161
             nRawXSize, nRawYSize, static_cast<int>(nTileBytes),
3162
             static_cast<int>(nRawBytes));
3163
#endif  // DEBUG
3164
3165
0
    if (VSIFSeekL(fp, nTileOffset, SEEK_SET) < 0)
3166
0
    {
3167
        // XXX: We will not report error here, because file just may be
3168
        // in update state and data for this block will be available later
3169
0
        if (eAccess == GA_Update)
3170
0
            return CE_None;
3171
3172
0
        CPLError(CE_Failure, CPLE_FileIO,
3173
0
                 "Can't seek to offset %ld in input file to read data.\n%s",
3174
0
                 static_cast<long>(nTileOffset), VSIStrerror(errno));
3175
0
        return CE_Failure;
3176
0
    }
3177
3178
0
    if (Decompress == nullptr || nTileBytes == nRawBytes)
3179
0
    {
3180
0
        if (nTileBytes != nRawBytes)
3181
0
        {
3182
0
            CPLError(CE_Failure, CPLE_AppDefined,
3183
0
                     "RMF: Invalid tile size %lu, expected %lu",
3184
0
                     static_cast<unsigned long>(nTileBytes),
3185
0
                     static_cast<unsigned long>(nRawBytes));
3186
0
            return CE_Failure;
3187
0
        }
3188
3189
0
        if (VSIFReadL(pabyData, 1, nRawBytes, fp) < nRawBytes)
3190
0
        {
3191
0
            CPLError(CE_Failure, CPLE_FileIO,
3192
0
                     "RMF: Can't read at offset %lu from input file.\n%s",
3193
0
                     static_cast<unsigned long>(nTileOffset),
3194
0
                     VSIStrerror(errno));
3195
0
            return CE_Failure;
3196
0
        }
3197
3198
#ifdef CPL_MSB
3199
        if (eRMFType == RMFT_MTW)
3200
        {
3201
            if (sHeader.nBitDepth == 16)
3202
            {
3203
                for (GUInt32 i = 0; i < nRawBytes; i += 2)
3204
                    CPL_SWAP16PTR(pabyData + i);
3205
            }
3206
            else if (sHeader.nBitDepth == 32)
3207
            {
3208
                for (GUInt32 i = 0; i < nRawBytes; i += 4)
3209
                    CPL_SWAP32PTR(pabyData + i);
3210
            }
3211
            else if (sHeader.nBitDepth == 64)
3212
            {
3213
                for (GUInt32 i = 0; i < nRawBytes; i += 8)
3214
                    CPL_SWAPDOUBLE(pabyData + i);
3215
            }
3216
        }
3217
#endif
3218
0
        return CE_None;
3219
0
    }
3220
3221
0
    if (pabyDecompressBuffer == nullptr)
3222
0
    {
3223
0
        pabyDecompressBuffer =
3224
0
            static_cast<GByte *>(VSIMalloc(std::max(1U, nMaxTileBytes)));
3225
0
        if (!pabyDecompressBuffer)
3226
0
        {
3227
0
            CPLError(CE_Failure, CPLE_OutOfMemory,
3228
0
                     "Can't allocate decompress buffer of size %lu.\n%s",
3229
0
                     static_cast<unsigned long>(nMaxTileBytes),
3230
0
                     VSIStrerror(errno));
3231
0
            return CE_Failure;
3232
0
        }
3233
0
    }
3234
3235
0
    if (VSIFReadL(pabyDecompressBuffer, 1, nTileBytes, fp) < nTileBytes)
3236
0
    {
3237
0
        CPLError(CE_Failure, CPLE_FileIO,
3238
0
                 "RMF: Can't read at offset %lu from input file.\n%s",
3239
0
                 static_cast<unsigned long>(nTileOffset), VSIStrerror(errno));
3240
0
        return CE_Failure;
3241
0
    }
3242
3243
0
    size_t nDecompressedSize =
3244
0
        Decompress(pabyDecompressBuffer, nTileBytes, pabyData,
3245
0
                   static_cast<GUInt32>(nRawBytes), nRawXSize, nRawYSize);
3246
3247
0
    if (nDecompressedSize != static_cast<size_t>(nRawBytes))
3248
0
    {
3249
0
        CPLError(CE_Failure, CPLE_FileIO,
3250
0
                 "Can't decompress tile xOff %d yOff %d. "
3251
0
                 "Raw tile size is %lu but decompressed is %lu. "
3252
0
                 "Compressed tile size is %lu",
3253
0
                 nBlockXOff, nBlockYOff, static_cast<unsigned long>(nRawBytes),
3254
0
                 static_cast<unsigned long>(nDecompressedSize),
3255
0
                 static_cast<unsigned long>(nTileBytes));
3256
0
        return CE_Failure;
3257
0
    }
3258
    // We don't need to swap bytes here,
3259
    // because decompressed data is in proper byte order
3260
0
    return CE_None;
3261
0
}
3262
3263
void RMFDataset::SetupNBits()
3264
158
{
3265
158
    int nBitDepth = 0;
3266
158
    if (sHeader.nBitDepth < 8 && nBands == 1)
3267
0
    {
3268
0
        nBitDepth = static_cast<int>(sHeader.nBitDepth);
3269
0
    }
3270
158
    else if (sHeader.nBitDepth == 16 && nBands == 3 && eRMFType == RMFT_RSW)
3271
158
    {
3272
158
        nBitDepth = 5;
3273
158
    }
3274
3275
158
    if (nBitDepth > 0)
3276
158
    {
3277
158
        char szNBits[32] = {};
3278
158
        snprintf(szNBits, sizeof(szNBits), "%d", nBitDepth);
3279
632
        for (int iBand = 1; iBand <= nBands; iBand++)
3280
474
        {
3281
474
            GetRasterBand(iBand)->SetMetadataItem("NBITS", szNBits,
3282
474
                                                  "IMAGE_STRUCTURE");
3283
474
        }
3284
158
    }
3285
158
}
3286
3287
/************************************************************************/
3288
/*                        GDALRegister_RMF()                            */
3289
/************************************************************************/
3290
3291
void GDALRegister_RMF()
3292
3293
2
{
3294
2
    if (GDALGetDriverByName("RMF") != nullptr)
3295
0
        return;
3296
3297
2
    GDALDriver *poDriver = new GDALDriver();
3298
3299
2
    poDriver->SetDescription("RMF");
3300
2
    poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
3301
2
    poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "Raster Matrix Format");
3302
2
    poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/rmf.html");
3303
2
    poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "rsw");
3304
2
    poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES,
3305
2
                              "Byte Int16 Int32 Float64");
3306
2
    poDriver->SetMetadataItem(
3307
2
        GDAL_DMD_CREATIONOPTIONLIST,
3308
2
        "<CreationOptionList>"
3309
2
        "   <Option name='MTW' type='boolean' description='Create MTW DEM "
3310
2
        "matrix'/>"
3311
2
        "   <Option name='BLOCKXSIZE' type='int' description='Tile Width'/>"
3312
2
        "   <Option name='BLOCKYSIZE' type='int' description='Tile Height'/>"
3313
2
        "   <Option name='RMFHUGE' type='string-select' description='Creation "
3314
2
        "of huge RMF file (Supported by GIS Panorama since v11)'>"
3315
2
        "     <Value>NO</Value>"
3316
2
        "     <Value>YES</Value>"
3317
2
        "     <Value>IF_SAFER</Value>"
3318
2
        "   </Option>"
3319
2
        "   <Option name='COMPRESS' type='string-select' default='NONE'>"
3320
2
        "     <Value>NONE</Value>"
3321
2
        "     <Value>LZW</Value>"
3322
2
        "     <Value>JPEG</Value>"
3323
2
        "     <Value>RMF_DEM</Value>"
3324
2
        "   </Option>"
3325
2
        "   <Option name='JPEG_QUALITY' type='int' description='JPEG quality "
3326
2
        "1-100' default='75'/>"
3327
2
        "   <Option name='NUM_THREADS' type='string' description='Number of "
3328
2
        "worker threads for compression. Can be set to ALL_CPUS' default='1'/>"
3329
2
        "</CreationOptionList>");
3330
2
    poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
3331
3332
2
    poDriver->pfnIdentify = RMFDataset::Identify;
3333
2
    poDriver->pfnOpen = RMFDataset::Open;
3334
2
    poDriver->pfnCreate = RMFDataset::Create;
3335
2
    poDriver->SetMetadataItem(
3336
2
        GDAL_DMD_OPENOPTIONLIST,
3337
2
        "<OpenOptionList>"
3338
2
        "  <Option name='RMF_SET_VERTCS' type='string' description='Layers "
3339
2
        "spatial reference will include vertical coordinate system description "
3340
2
        "if exist' default='NO'/>"
3341
2
        "</OpenOptionList>");
3342
3343
2
    GetGDALDriverManager()->RegisterDriver(poDriver);
3344
2
}
3345
3346
/************************************************************************/
3347
/*                            RMFCompressData                           */
3348
/************************************************************************/
3349
3350
0
RMFCompressData::RMFCompressData() : pabyBuffers(nullptr)
3351
0
{
3352
0
}
3353
3354
RMFCompressData::~RMFCompressData()
3355
0
{
3356
0
    if (pabyBuffers != nullptr)
3357
0
    {
3358
0
        VSIFree(pabyBuffers);
3359
0
    }
3360
3361
0
    if (hWriteTileMutex != nullptr)
3362
0
    {
3363
0
        CPLDestroyMutex(hWriteTileMutex);
3364
0
    }
3365
3366
0
    if (hReadyJobMutex != nullptr)
3367
0
    {
3368
0
        CPLDestroyMutex(hReadyJobMutex);
3369
0
    }
3370
0
}
3371
3372
GDALSuggestedBlockAccessPattern
3373
RMFRasterBand::GetSuggestedBlockAccessPattern() const
3374
0
{
3375
0
    return GSBAP_RANDOM;
3376
0
}
3377
3378
CPLErr RMFDataset::SetMetadataItem(const char *pszName, const char *pszValue,
3379
                                   const char *pszDomain)
3380
1.13k
{
3381
1.13k
    if (GetAccess() == GA_Update)
3382
0
    {
3383
0
        CPLDebug("RMF", "SetMetadataItem: %s=%s", pszName, pszValue);
3384
0
        if (EQUAL(pszName, MD_NAME_KEY))
3385
0
        {
3386
0
            memcpy(sHeader.byName, pszValue,
3387
0
                   CPLStrnlen(pszValue, RMF_NAME_SIZE));
3388
0
            bHeaderDirty = true;
3389
0
        }
3390
0
        else if (EQUAL(pszName, MD_SCALE_KEY) && CPLStrnlen(pszValue, 10) > 4)
3391
0
        {
3392
0
            sHeader.dfScale = atof(pszValue + 4);
3393
0
            sHeader.dfResolution = sHeader.dfScale / sHeader.dfPixelSize;
3394
0
            bHeaderDirty = true;
3395
0
        }
3396
0
        else if (EQUAL(pszName, MD_FRAME_KEY))
3397
0
        {
3398
0
            bHeaderDirty = true;
3399
0
        }
3400
0
    }
3401
1.13k
    return GDALDataset::SetMetadataItem(pszName, pszValue, pszDomain);
3402
1.13k
}
3403
3404
CPLErr RMFDataset::SetMetadata(char **papszMetadata, const char *pszDomain)
3405
0
{
3406
0
    if (GetAccess() == GA_Update)
3407
0
    {
3408
0
        auto pszName = CSLFetchNameValue(papszMetadata, MD_NAME_KEY);
3409
0
        if (pszName != nullptr)
3410
0
        {
3411
0
            memcpy(sHeader.byName, pszName, CPLStrnlen(pszName, RMF_NAME_SIZE));
3412
0
            bHeaderDirty = true;
3413
3414
0
            CPLDebug("RMF", "SetMetadata: %s", pszName);
3415
0
        }
3416
0
        auto pszScale = CSLFetchNameValue(papszMetadata, MD_SCALE_KEY);
3417
0
        if (pszScale != nullptr && CPLStrnlen(pszScale, 10) > 4)
3418
0
        {
3419
0
            sHeader.dfScale = atof(pszScale + 4);
3420
0
            sHeader.dfResolution = sHeader.dfScale / sHeader.dfPixelSize;
3421
0
            bHeaderDirty = true;
3422
3423
0
            CPLDebug("RMF", "SetMetadata: %s", pszScale);
3424
0
        }
3425
0
        auto pszFrame = CSLFetchNameValue(papszMetadata, MD_FRAME_KEY);
3426
0
        if (pszFrame != nullptr)
3427
0
        {
3428
0
            bHeaderDirty = true;
3429
3430
0
            CPLDebug("RMF", "SetMetadata: %s", pszFrame);
3431
0
        }
3432
0
    }
3433
0
    return GDALDataset::SetMetadata(papszMetadata, pszDomain);
3434
0
}