Coverage Report

Created: 2025-06-09 07:43

/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
170
{
69
170
    switch (iElevationUnit)
70
170
    {
71
1
        case 0:
72
1
            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
167
        case 3:
78
167
            return CPLStrdup(RMF_UnitsMM);
79
2
        default:
80
2
            return CPLStrdup(RMF_UnitsEmpty);
81
170
    }
82
170
}
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
374
    : nLastTileWidth(poDSIn->GetRasterXSize() % poDSIn->sHeader.nTileWidth),
122
374
      nLastTileHeight(poDSIn->GetRasterYSize() % poDSIn->sHeader.nTileHeight),
123
374
      nDataSize(GDALGetDataTypeSizeBytes(eType))
124
374
{
125
374
    poDS = poDSIn;
126
374
    nBand = nBandIn;
127
128
374
    eDataType = eType;
129
374
    nBlockXSize = poDSIn->sHeader.nTileWidth;
130
374
    nBlockYSize = poDSIn->sHeader.nTileHeight;
131
374
    nBlockSize = nBlockXSize * nBlockYSize;
132
374
    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
374
}
143
144
/************************************************************************/
145
/*                           ~RMFRasterBand()                           */
146
/************************************************************************/
147
148
RMFRasterBand::~RMFRasterBand()
149
374
{
150
374
}
151
152
/************************************************************************/
153
/*                             IReadBlock()                             */
154
/************************************************************************/
155
156
CPLErr RMFRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
157
825
{
158
825
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
159
160
825
    CPLAssert(poGDS != nullptr && nBlockXOff >= 0 && nBlockYOff >= 0 &&
161
825
              pImage != nullptr);
162
163
825
    memset(pImage, 0, nBlockBytes);
164
165
825
    GUInt32 nRawXSize = nBlockXSize;
166
825
    GUInt32 nRawYSize = nBlockYSize;
167
168
825
    if (nLastTileWidth &&
169
825
        static_cast<GUInt32>(nBlockXOff) == poGDS->nXTiles - 1)
170
300
        nRawXSize = nLastTileWidth;
171
172
825
    if (nLastTileHeight &&
173
825
        static_cast<GUInt32>(nBlockYOff) == poGDS->nYTiles - 1)
174
159
        nRawYSize = nLastTileHeight;
175
176
825
    GUInt32 nRawBytes = nRawXSize * nRawYSize * poGDS->sHeader.nBitDepth / 8;
177
178
    // Direct read optimization
179
825
    if (poGDS->nBands == 1 && poGDS->sHeader.nBitDepth >= 8 &&
180
825
        nRawXSize == static_cast<GUInt32>(nBlockXSize) &&
181
825
        nRawYSize == static_cast<GUInt32>(nBlockYSize))
182
187
    {
183
187
        bool bNullTile = false;
184
187
        if (CE_None != poGDS->ReadTile(nBlockXOff, nBlockYOff,
185
187
                                       reinterpret_cast<GByte *>(pImage),
186
187
                                       nRawBytes, nRawXSize, nRawYSize,
187
187
                                       bNullTile))
188
20
        {
189
20
            CPLError(CE_Failure, CPLE_FileIO,
190
20
                     "Failed to read tile xOff %d yOff %d", nBlockXOff,
191
20
                     nBlockYOff);
192
20
            return CE_Failure;
193
20
        }
194
167
        if (bNullTile)
195
6
        {
196
6
            const int nChunkSize =
197
6
                std::max(1, GDALGetDataTypeSizeBytes(eDataType));
198
6
            const GPtrDiff_t nWords =
199
6
                static_cast<GPtrDiff_t>(nBlockXSize) * nBlockYSize;
200
6
            GDALCopyWords64(&poGDS->sHeader.dfNoData, GDT_Float64, 0, pImage,
201
6
                            eDataType, nChunkSize, nWords);
202
6
        }
203
167
        return CE_None;
204
187
    }
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
638
    if (poGDS->pabyCurrentTile == nullptr ||
210
638
        poGDS->nCurrentTileXOff != nBlockXOff ||
211
638
        poGDS->nCurrentTileYOff != nBlockYOff ||
212
638
        poGDS->nCurrentTileBytes != nRawBytes)
213
638
    {
214
638
        if (poGDS->pabyCurrentTile == nullptr)
215
247
        {
216
247
            GUInt32 nMaxTileBytes = poGDS->sHeader.nTileWidth *
217
247
                                    poGDS->sHeader.nTileHeight *
218
247
                                    poGDS->sHeader.nBitDepth / 8;
219
247
            poGDS->pabyCurrentTile = reinterpret_cast<GByte *>(
220
247
                VSIMalloc(std::max(1U, nMaxTileBytes)));
221
247
            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
247
        }
231
232
638
        poGDS->nCurrentTileXOff = nBlockXOff;
233
638
        poGDS->nCurrentTileYOff = nBlockYOff;
234
638
        poGDS->nCurrentTileBytes = nRawBytes;
235
236
638
        if (CE_None != poGDS->ReadTile(nBlockXOff, nBlockYOff,
237
638
                                       poGDS->pabyCurrentTile, nRawBytes,
238
638
                                       nRawXSize, nRawYSize,
239
638
                                       poGDS->bCurrentTileIsNull))
240
280
        {
241
280
            CPLError(CE_Failure, CPLE_FileIO,
242
280
                     "Failed to read tile xOff %d yOff %d", nBlockXOff,
243
280
                     nBlockYOff);
244
280
            poGDS->nCurrentTileBytes = 0;
245
280
            return CE_Failure;
246
280
        }
247
638
    }
248
249
    /* -------------------------------------------------------------------- */
250
    /*  Deinterleave pixels from input buffer.                              */
251
    /* -------------------------------------------------------------------- */
252
253
358
    if (poGDS->bCurrentTileIsNull)
254
74
    {
255
74
        const int nChunkSize = std::max(1, GDALGetDataTypeSizeBytes(eDataType));
256
74
        const GPtrDiff_t nWords =
257
74
            static_cast<GPtrDiff_t>(nBlockXSize) * nBlockYSize;
258
74
        GDALCopyWords64(&poGDS->sHeader.dfNoData, GDT_Float64, 0, pImage,
259
74
                        eDataType, nChunkSize, nWords);
260
74
        return CE_None;
261
74
    }
262
284
    else if ((poGDS->eRMFType == RMFT_RSW &&
263
284
              (poGDS->sHeader.nBitDepth == 8 ||
264
143
               poGDS->sHeader.nBitDepth == 24 ||
265
143
               poGDS->sHeader.nBitDepth == 32)) ||
266
284
             (poGDS->eRMFType == RMFT_MTW))
267
282
    {
268
282
        const size_t nTilePixelSize = poGDS->sHeader.nBitDepth / 8;
269
282
        const size_t nTileLineSize = nTilePixelSize * nRawXSize;
270
282
        const size_t nBlockLineSize =
271
282
            static_cast<size_t>(nDataSize) * nBlockXSize;
272
282
        int iDstBand = (poGDS->nBands - nBand);
273
46.6k
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
274
46.3k
        {
275
46.3k
            GByte *pabySrc;
276
46.3k
            GByte *pabyDst;
277
46.3k
            pabySrc = poGDS->pabyCurrentTile + iLine * nTileLineSize +
278
46.3k
                      iDstBand * nDataSize;
279
46.3k
            pabyDst =
280
46.3k
                reinterpret_cast<GByte *>(pImage) + iLine * nBlockLineSize;
281
46.3k
            GDALCopyWords(pabySrc, eDataType, static_cast<int>(nTilePixelSize),
282
46.3k
                          pabyDst, eDataType, static_cast<int>(nDataSize),
283
46.3k
                          nRawXSize);
284
46.3k
        }
285
282
        return CE_None;
286
282
    }
287
2
    else if (poGDS->eRMFType == RMFT_RSW && poGDS->sHeader.nBitDepth == 16 &&
288
2
             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
2
    else if (poGDS->eRMFType == RMFT_RSW && poGDS->nBands == 1 &&
328
2
             poGDS->sHeader.nBitDepth == 4)
329
1
    {
330
1
        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
1
        const size_t nTilePixelBits = poGDS->sHeader.nBitDepth;
339
1
        const size_t nTileLineSize = nTilePixelBits * nRawXSize / 8;
340
1
        const size_t nBlockLineSize =
341
1
            static_cast<size_t>(nDataSize) * nBlockXSize;
342
343
232
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
344
231
        {
345
231
            GByte *pabySrc;
346
231
            GByte *pabyDst;
347
231
            pabySrc = poGDS->pabyCurrentTile + iLine * nTileLineSize;
348
231
            pabyDst =
349
231
                reinterpret_cast<GByte *>(pImage) + iLine * nBlockLineSize;
350
56.1k
            for (GUInt32 i = 0; i < nRawXSize; ++i)
351
55.9k
            {
352
55.9k
                if (i & 0x01)
353
27.9k
                    pabyDst[i] = (*pabySrc++ & 0xF0) >> 4;
354
27.9k
                else
355
27.9k
                    pabyDst[i] = *pabySrc & 0x0F;
356
55.9k
            }
357
231
        }
358
1
        return CE_None;
359
1
    }
360
1
    else if (poGDS->eRMFType == RMFT_RSW && poGDS->nBands == 1 &&
361
1
             poGDS->sHeader.nBitDepth == 1)
362
1
    {
363
1
        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
1
        const size_t nTilePixelBits = poGDS->sHeader.nBitDepth;
372
1
        const size_t nTileLineSize = nTilePixelBits * nRawXSize / 8;
373
1
        const size_t nBlockLineSize =
374
1
            static_cast<size_t>(nDataSize) * nBlockXSize;
375
376
232
        for (GUInt32 iLine = 0; iLine != nRawYSize; ++iLine)
377
231
        {
378
231
            GByte *pabySrc;
379
231
            GByte *pabyDst;
380
231
            pabySrc = poGDS->pabyCurrentTile + iLine * nTileLineSize;
381
231
            pabyDst =
382
231
                reinterpret_cast<GByte *>(pImage) + iLine * nBlockLineSize;
383
384
57.5k
            for (GUInt32 i = 0; i < nRawXSize; ++i)
385
57.2k
            {
386
57.2k
                switch (i & 0x7)
387
57.2k
                {
388
7.16k
                    case 0:
389
7.16k
                        pabyDst[i] = (*pabySrc & 0x80) >> 7;
390
7.16k
                        break;
391
7.16k
                    case 1:
392
7.16k
                        pabyDst[i] = (*pabySrc & 0x40) >> 6;
393
7.16k
                        break;
394
7.16k
                    case 2:
395
7.16k
                        pabyDst[i] = (*pabySrc & 0x20) >> 5;
396
7.16k
                        break;
397
7.16k
                    case 3:
398
7.16k
                        pabyDst[i] = (*pabySrc & 0x10) >> 4;
399
7.16k
                        break;
400
7.16k
                    case 4:
401
7.16k
                        pabyDst[i] = (*pabySrc & 0x08) >> 3;
402
7.16k
                        break;
403
7.16k
                    case 5:
404
7.16k
                        pabyDst[i] = (*pabySrc & 0x04) >> 2;
405
7.16k
                        break;
406
7.16k
                    case 6:
407
7.16k
                        pabyDst[i] = (*pabySrc & 0x02) >> 1;
408
7.16k
                        break;
409
7.16k
                    case 7:
410
7.16k
                        pabyDst[i] = *pabySrc++ & 0x01;
411
7.16k
                        break;
412
0
                    default:
413
0
                        break;
414
57.2k
                }
415
57.2k
            }
416
231
        }
417
1
        return CE_None;
418
1
    }
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
358
}
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
1.08k
{
545
1.08k
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
546
547
1.08k
    if (pbSuccess)
548
813
        *pbSuccess = TRUE;
549
550
1.08k
    return poGDS->sHeader.dfNoData;
551
1.08k
}
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
271
{
570
271
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
571
572
271
    return poGDS->pszUnitType;
573
271
}
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
271
{
656
271
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
657
271
    if (poGDS->poOvrDatasets.empty())
658
261
        return GDALRasterBand::GetOverviewCount();
659
10
    else
660
10
        return static_cast<int>(poGDS->poOvrDatasets.size());
661
271
}
662
663
GDALRasterBand *RMFRasterBand::GetOverview(int i)
664
22
{
665
22
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
666
22
    size_t n = static_cast<size_t>(i);
667
22
    if (poGDS->poOvrDatasets.empty())
668
0
        return GDALRasterBand::GetOverview(i);
669
22
    else
670
22
        return poGDS->poOvrDatasets[n]->GetRasterBand(nBand);
671
22
}
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
459
{
680
459
    RMFDataset *poGDS = cpl::down_cast<RMFDataset *>(poDS);
681
682
459
    if (eRWFlag == GF_Read && poGDS->poCompressData != nullptr &&
683
459
        poGDS->poCompressData->oThreadPool.GetThreadCount() > 0)
684
0
    {
685
0
        poGDS->poCompressData->oThreadPool.WaitCompletion();
686
0
    }
687
688
459
    return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
689
459
                                     pData, nBufXSize, nBufYSize, eType,
690
459
                                     nPixelSpace, nLineSpace, psExtraArg);
691
459
}
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
299
RMFDataset::RMFDataset() : pszUnitType(CPLStrdup(RMF_UnitsEmpty))
730
299
{
731
299
    m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
732
299
    nBands = 0;
733
299
    memset(&sHeader, 0, sizeof(sHeader));
734
299
    memset(&sExtHeader, 0, sizeof(sExtHeader));
735
299
}
736
737
/************************************************************************/
738
/*                            ~RMFDataset()                             */
739
/************************************************************************/
740
741
RMFDataset::~RMFDataset()
742
299
{
743
299
    RMFDataset::FlushCache(true);
744
321
    for (size_t n = 0; n != poOvrDatasets.size(); ++n)
745
22
    {
746
22
        poOvrDatasets[n]->RMFDataset::FlushCache(true);
747
22
    }
748
749
299
    VSIFree(paiTiles);
750
299
    VSIFree(pabyDecompressBuffer);
751
299
    VSIFree(pabyCurrentTile);
752
299
    CPLFree(pszUnitType);
753
299
    CPLFree(pabyColorTable);
754
299
    if (poColorTable != nullptr)
755
84
        delete poColorTable;
756
757
321
    for (size_t n = 0; n != poOvrDatasets.size(); ++n)
758
22
    {
759
22
        GDALClose(poOvrDatasets[n]);
760
22
    }
761
762
299
    if (fp != nullptr && poParentDS == nullptr)
763
273
    {
764
273
        VSIFCloseL(fp);
765
273
    }
766
299
}
767
768
/************************************************************************/
769
/*                          GetGeoTransform()                           */
770
/************************************************************************/
771
772
CPLErr RMFDataset::GetGeoTransform(double *padfTransform)
773
271
{
774
271
    memcpy(padfTransform, adfGeoTransform.data(), sizeof(adfGeoTransform));
775
776
271
    if (sHeader.iGeorefFlag)
777
175
        return CE_None;
778
779
96
    return CE_Failure;
780
271
}
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
271
{
808
271
    return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
809
271
}
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
321
{
1165
321
    CPLErr eErr = GDALDataset::FlushCache(bAtClosing);
1166
1167
321
    if (poCompressData != nullptr &&
1168
321
        poCompressData->oThreadPool.GetThreadCount() > 0)
1169
0
    {
1170
0
        poCompressData->oThreadPool.WaitCompletion();
1171
0
    }
1172
1173
321
    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
321
    if (bHeaderDirty && WriteHeader() != CE_None)
1187
0
        eErr = CE_Failure;
1188
321
    return eErr;
1189
321
}
1190
1191
/************************************************************************/
1192
/*                              Identify()                              */
1193
/************************************************************************/
1194
1195
int RMFDataset::Identify(GDALOpenInfo *poOpenInfo)
1196
1197
191k
{
1198
191k
    if (poOpenInfo->pabyHeader == nullptr)
1199
182k
        return FALSE;
1200
1201
8.52k
    if (memcmp(poOpenInfo->pabyHeader, RMF_SigRSW, sizeof(RMF_SigRSW)) != 0 &&
1202
8.52k
        memcmp(poOpenInfo->pabyHeader, RMF_SigRSW_BE, sizeof(RMF_SigRSW_BE)) !=
1203
8.30k
            0 &&
1204
8.52k
        memcmp(poOpenInfo->pabyHeader, RMF_SigMTW, sizeof(RMF_SigMTW)) != 0)
1205
7.95k
        return FALSE;
1206
1207
572
    return TRUE;
1208
8.52k
}
1209
1210
/************************************************************************/
1211
/*                                Open()                                */
1212
/************************************************************************/
1213
1214
GDALDataset *RMFDataset::Open(GDALOpenInfo *poOpenInfo)
1215
273
{
1216
273
    auto poDS = Open(poOpenInfo, nullptr, 0);
1217
273
    if (poDS == nullptr)
1218
2
    {
1219
2
        return nullptr;
1220
2
    }
1221
1222
271
    RMFDataset *poCurrentLayer = poDS;
1223
271
    RMFDataset *poParent = poCurrentLayer;
1224
271
    const int nMaxPossibleOvCount = 64;
1225
1226
293
    for (int iOv = 0; iOv < nMaxPossibleOvCount && poCurrentLayer != nullptr;
1227
271
         ++iOv)
1228
293
    {
1229
293
        poCurrentLayer = poCurrentLayer->OpenOverview(poParent, poOpenInfo);
1230
293
        if (poCurrentLayer == nullptr)
1231
271
            break;
1232
22
        poParent->poOvrDatasets.push_back(poCurrentLayer);
1233
22
    }
1234
1235
271
    return poDS;
1236
273
}
1237
1238
RMFDataset *RMFDataset::Open(GDALOpenInfo *poOpenInfo, RMFDataset *poParentDS,
1239
                             vsi_l_offset nNextHeaderOffset)
1240
403
{
1241
403
    if (!Identify(poOpenInfo) ||
1242
403
        (poParentDS == nullptr && poOpenInfo->fpL == nullptr))
1243
104
        return nullptr;
1244
1245
    /* -------------------------------------------------------------------- */
1246
    /*  Create a corresponding GDALDataset.                                 */
1247
    /* -------------------------------------------------------------------- */
1248
299
    RMFDataset *poDS = new RMFDataset();
1249
1250
299
    if (poParentDS == nullptr)
1251
273
    {
1252
273
        poDS->fp = poOpenInfo->fpL;
1253
273
        poOpenInfo->fpL = nullptr;
1254
273
        poDS->nHeaderOffset = 0;
1255
273
        poDS->poParentDS = nullptr;
1256
273
    }
1257
26
    else
1258
26
    {
1259
26
        poDS->fp = poParentDS->fp;
1260
26
        poDS->poParentDS = poParentDS;
1261
26
        poDS->nHeaderOffset = nNextHeaderOffset;
1262
26
    }
1263
299
    poDS->eAccess = poOpenInfo->eAccess;
1264
1265
299
#define RMF_READ_SHORT(ptr, value, offset)                                     \
1266
299
    do                                                                         \
1267
299
    {                                                                          \
1268
299
        memcpy(&(value), reinterpret_cast<GInt16 *>((ptr) + (offset)),         \
1269
299
               sizeof(GInt16));                                                \
1270
299
        if (poDS->bBigEndian)                                                  \
1271
299
        {                                                                      \
1272
299
            CPL_MSBPTR16(&(value));                                            \
1273
299
        }                                                                      \
1274
299
        else                                                                   \
1275
299
        {                                                                      \
1276
299
            CPL_LSBPTR16(&(value));                                            \
1277
299
        }                                                                      \
1278
299
    } while (false);
1279
1280
299
#define RMF_READ_ULONG(ptr, value, offset)                                     \
1281
10.4k
    do                                                                         \
1282
10.4k
    {                                                                          \
1283
10.4k
        memcpy(&(value), reinterpret_cast<GUInt32 *>((ptr) + (offset)),        \
1284
10.4k
               sizeof(GUInt32));                                               \
1285
10.4k
        if (poDS->bBigEndian)                                                  \
1286
10.4k
        {                                                                      \
1287
631
            CPL_MSBPTR32(&(value));                                            \
1288
631
        }                                                                      \
1289
10.4k
        else                                                                   \
1290
10.4k
        {                                                                      \
1291
9.79k
            CPL_LSBPTR32(&(value));                                            \
1292
9.79k
        }                                                                      \
1293
10.4k
    } while (false);
1294
1295
2.65k
#define RMF_READ_LONG(ptr, value, offset) RMF_READ_ULONG(ptr, value, offset)
1296
1297
299
#define RMF_READ_DOUBLE(ptr, value, offset)                                    \
1298
3.58k
    do                                                                         \
1299
3.58k
    {                                                                          \
1300
3.58k
        memcpy(&(value), reinterpret_cast<double *>((ptr) + (offset)),         \
1301
3.58k
               sizeof(double));                                                \
1302
3.58k
        if (poDS->bBigEndian)                                                  \
1303
3.58k
        {                                                                      \
1304
24
            CPL_MSBPTR64(&(value));                                            \
1305
24
        }                                                                      \
1306
3.58k
        else                                                                   \
1307
3.58k
        {                                                                      \
1308
3.56k
            CPL_LSBPTR64(&(value));                                            \
1309
3.56k
        }                                                                      \
1310
3.58k
    } while (false);
1311
1312
    /* -------------------------------------------------------------------- */
1313
    /*  Read the main header.                                               */
1314
    /* -------------------------------------------------------------------- */
1315
1316
299
    {
1317
299
        GByte abyHeader[RMF_HEADER_SIZE] = {};
1318
1319
299
        VSIFSeekL(poDS->fp, nNextHeaderOffset, SEEK_SET);
1320
299
        if (VSIFReadL(abyHeader, 1, sizeof(abyHeader), poDS->fp) !=
1321
299
            sizeof(abyHeader))
1322
0
        {
1323
0
            delete poDS;
1324
0
            return nullptr;
1325
0
        }
1326
1327
299
        if (memcmp(abyHeader, RMF_SigMTW, sizeof(RMF_SigMTW)) == 0)
1328
172
        {
1329
172
            poDS->eRMFType = RMFT_MTW;
1330
172
        }
1331
127
        else if (memcmp(abyHeader, RMF_SigRSW_BE, sizeof(RMF_SigRSW_BE)) == 0)
1332
2
        {
1333
2
            poDS->eRMFType = RMFT_RSW;
1334
2
            poDS->bBigEndian = true;
1335
2
        }
1336
125
        else
1337
125
        {
1338
125
            poDS->eRMFType = RMFT_RSW;
1339
125
        }
1340
1341
299
        memcpy(poDS->sHeader.bySignature, abyHeader, RMF_SIGNATURE_SIZE);
1342
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.iVersion, 4);
1343
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nSize, 8);
1344
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nOvrOffset, 12);
1345
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.iUserID, 16);
1346
299
        memcpy(poDS->sHeader.byName, abyHeader + 20,
1347
299
               sizeof(poDS->sHeader.byName));
1348
299
        poDS->sHeader.byName[sizeof(poDS->sHeader.byName) - 1] = '\0';
1349
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nBitDepth, 52);
1350
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nHeight, 56);
1351
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nWidth, 60);
1352
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nXTiles, 64);
1353
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nYTiles, 68);
1354
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileHeight, 72);
1355
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileWidth, 76);
1356
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nLastTileHeight, 80);
1357
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nLastTileWidth, 84);
1358
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nROIOffset, 88);
1359
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nROISize, 92);
1360
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nClrTblOffset, 96);
1361
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nClrTblSize, 100);
1362
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileTblOffset, 104);
1363
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nTileTblSize, 108);
1364
299
        RMF_READ_LONG(abyHeader, poDS->sHeader.iMapType, 124);
1365
299
        RMF_READ_LONG(abyHeader, poDS->sHeader.iProjection, 128);
1366
299
        RMF_READ_LONG(abyHeader, poDS->sHeader.iEPSGCode, 132);
1367
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfScale, 136);
1368
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfResolution, 144);
1369
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfPixelSize, 152);
1370
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfLLY, 160);
1371
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfLLX, 168);
1372
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfStdP1, 176);
1373
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfStdP2, 184);
1374
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfCenterLong, 192);
1375
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfCenterLat, 200);
1376
299
        poDS->sHeader.iCompression = *(abyHeader + 208);
1377
299
        poDS->sHeader.iMaskType = *(abyHeader + 209);
1378
299
        poDS->sHeader.iMaskStep = *(abyHeader + 210);
1379
299
        poDS->sHeader.iFrameFlag = *(abyHeader + 211);
1380
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFlagsTblOffset, 212);
1381
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFlagsTblSize, 216);
1382
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFileSize0, 220);
1383
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nFileSize1, 224);
1384
299
        poDS->sHeader.iUnknown = *(abyHeader + 228);
1385
299
        poDS->sHeader.iGeorefFlag = *(abyHeader + 244);
1386
299
        poDS->sHeader.iInverse = *(abyHeader + 245);
1387
299
        poDS->sHeader.iJpegQuality = *(abyHeader + 246);
1388
299
        memcpy(poDS->sHeader.abyInvisibleColors, abyHeader + 248,
1389
299
               sizeof(poDS->sHeader.abyInvisibleColors));
1390
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.adfElevMinMax[0], 280);
1391
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.adfElevMinMax[1], 288);
1392
299
        RMF_READ_DOUBLE(abyHeader, poDS->sHeader.dfNoData, 296);
1393
1394
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.iElevationUnit, 304);
1395
299
        poDS->sHeader.iElevationType = *(abyHeader + 308);
1396
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nExtHdrOffset, 312);
1397
299
        RMF_READ_ULONG(abyHeader, poDS->sHeader.nExtHdrSize, 316);
1398
299
        poDS->SetMetadataItem(MD_SCALE_KEY,
1399
299
                              CPLSPrintf("1 : %u", int(poDS->sHeader.dfScale)));
1400
299
        poDS->SetMetadataItem(MD_NAME_KEY,
1401
299
                              CPLSPrintf("%s", poDS->sHeader.byName));
1402
299
        poDS->SetMetadataItem(MD_VERSION_KEY,
1403
299
                              CPLSPrintf("%d", poDS->sHeader.iVersion));
1404
299
        poDS->SetMetadataItem(MD_MATH_BASE_MAP_TYPE_KEY,
1405
299
                              CPLSPrintf("%d", poDS->sHeader.iMapType));
1406
299
        poDS->SetMetadataItem(MD_MATH_BASE_PROJECTION_KEY,
1407
299
                              CPLSPrintf("%d", poDS->sHeader.iProjection));
1408
299
    }
1409
1410
299
    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
298
    bool bInvalidTileSize;
1418
298
    try
1419
298
    {
1420
298
        uint64_t nMaxTileBits =
1421
298
            (CPLSM(static_cast<uint64_t>(2)) *
1422
298
             CPLSM(static_cast<uint64_t>(poDS->sHeader.nTileWidth)) *
1423
298
             CPLSM(static_cast<uint64_t>(poDS->sHeader.nTileHeight)) *
1424
298
             CPLSM(static_cast<uint64_t>(poDS->sHeader.nBitDepth)))
1425
298
                .v();
1426
298
        bInvalidTileSize =
1427
298
            (nMaxTileBits >
1428
298
             static_cast<uint64_t>(std::numeric_limits<GUInt32>::max()));
1429
298
    }
1430
298
    catch (...)
1431
298
    {
1432
0
        bInvalidTileSize = true;
1433
0
    }
1434
298
    if (bInvalidTileSize)
1435
0
    {
1436
0
        CPLError(CE_Warning, CPLE_IllegalArg,
1437
0
                 "Invalid tile size. Width %lu, height %lu, bit depth %lu.",
1438
0
                 static_cast<unsigned long>(poDS->sHeader.nTileWidth),
1439
0
                 static_cast<unsigned long>(poDS->sHeader.nTileHeight),
1440
0
                 static_cast<unsigned long>(poDS->sHeader.nBitDepth));
1441
0
        delete poDS;
1442
0
        return nullptr;
1443
0
    }
1444
1445
298
    if (poDS->sHeader.nLastTileWidth > poDS->sHeader.nTileWidth ||
1446
298
        poDS->sHeader.nLastTileHeight > poDS->sHeader.nTileHeight)
1447
0
    {
1448
0
        CPLError(CE_Warning, CPLE_IllegalArg,
1449
0
                 "Invalid last tile size %lu x %lu. "
1450
0
                 "It can't be greater than %lu x %lu.",
1451
0
                 static_cast<unsigned long>(poDS->sHeader.nLastTileWidth),
1452
0
                 static_cast<unsigned long>(poDS->sHeader.nLastTileHeight),
1453
0
                 static_cast<unsigned long>(poDS->sHeader.nTileWidth),
1454
0
                 static_cast<unsigned long>(poDS->sHeader.nTileHeight));
1455
0
        delete poDS;
1456
0
        return nullptr;
1457
0
    }
1458
1459
298
    if (poParentDS != nullptr)
1460
25
    {
1461
25
        if (0 != memcmp(poDS->sHeader.bySignature,
1462
25
                        poParentDS->sHeader.bySignature, RMF_SIGNATURE_SIZE))
1463
1
        {
1464
1
            CPLError(CE_Warning, CPLE_IllegalArg,
1465
1
                     "Invalid subheader signature.");
1466
1
            delete poDS;
1467
1
            return nullptr;
1468
1
        }
1469
25
    }
1470
1471
    /* -------------------------------------------------------------------- */
1472
    /*  Read the extended header.                                           */
1473
    /* -------------------------------------------------------------------- */
1474
1475
297
    if (poDS->sHeader.nExtHdrOffset &&
1476
297
        poDS->sHeader.nExtHdrSize >= RMF_MIN_EXT_HEADER_SIZE)
1477
291
    {
1478
291
        if (poDS->sHeader.nExtHdrSize > RMF_MAX_EXT_HEADER_SIZE)
1479
0
        {
1480
0
            CPLError(CE_Failure, CPLE_FileIO, "RMF File malformed");
1481
0
            delete poDS;
1482
0
            return nullptr;
1483
0
        }
1484
291
        GByte *pabyExtHeader =
1485
291
            reinterpret_cast<GByte *>(CPLCalloc(poDS->sHeader.nExtHdrSize, 1));
1486
291
        if (pabyExtHeader == nullptr)
1487
0
        {
1488
0
            delete poDS;
1489
0
            return nullptr;
1490
0
        }
1491
1492
291
        VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nExtHdrOffset),
1493
291
                  SEEK_SET);
1494
291
        VSIFReadL(pabyExtHeader, 1, poDS->sHeader.nExtHdrSize, poDS->fp);
1495
1496
291
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nEllipsoid, 24);
1497
291
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nVertDatum, 28);
1498
291
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nDatum, 32);
1499
291
        RMF_READ_LONG(pabyExtHeader, poDS->sExtHeader.nZone, 36);
1500
1501
291
        CPLFree(pabyExtHeader);
1502
291
    }
1503
1504
297
    CPLDebug("RMF", "Version %d", poDS->sHeader.iVersion);
1505
1506
297
    constexpr GUInt32 ROI_MAX_SIZE_TO_AVOID_EXCESSIVE_RAM_USAGE =
1507
297
        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
297
    if (poDS->sHeader.nWidth >= INT_MAX || poDS->sHeader.nHeight >= INT_MAX ||
1571
297
        !GDALCheckDatasetDimensions(poDS->sHeader.nWidth,
1572
297
                                    poDS->sHeader.nHeight))
1573
0
    {
1574
0
        delete poDS;
1575
0
        return nullptr;
1576
0
    }
1577
1578
    /* -------------------------------------------------------------------- */
1579
    /*  Read array of blocks offsets/sizes.                                 */
1580
    /* -------------------------------------------------------------------- */
1581
1582
    // To avoid useless excessive memory allocation
1583
297
    if (poDS->sHeader.nTileTblSize > 1000000)
1584
0
    {
1585
0
        VSIFSeekL(poDS->fp, 0, SEEK_END);
1586
0
        vsi_l_offset nFileSize = VSIFTellL(poDS->fp);
1587
0
        if (nFileSize < poDS->sHeader.nTileTblSize)
1588
0
        {
1589
0
            delete poDS;
1590
0
            return nullptr;
1591
0
        }
1592
0
    }
1593
1594
297
    if (VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nTileTblOffset),
1595
297
                  SEEK_SET) < 0)
1596
0
    {
1597
0
        delete poDS;
1598
0
        return nullptr;
1599
0
    }
1600
1601
297
    poDS->paiTiles =
1602
297
        reinterpret_cast<GUInt32 *>(VSIMalloc(poDS->sHeader.nTileTblSize));
1603
297
    if (!poDS->paiTiles)
1604
0
    {
1605
0
        delete poDS;
1606
0
        return nullptr;
1607
0
    }
1608
1609
297
    if (VSIFReadL(poDS->paiTiles, 1, poDS->sHeader.nTileTblSize, poDS->fp) <
1610
297
        poDS->sHeader.nTileTblSize)
1611
2
    {
1612
2
        CPLDebug("RMF", "Can't read tiles offsets/sizes table.");
1613
2
        delete poDS;
1614
2
        return nullptr;
1615
2
    }
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
295
    if (poDS->bBigEndian)
1626
2
    {
1627
10
        for (GUInt32 i = 0; i < poDS->sHeader.nTileTblSize / sizeof(GUInt32);
1628
8
             i++)
1629
8
            CPL_SWAP32PTR(poDS->paiTiles + i);
1630
2
    }
1631
295
#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
295
    GDALDataType eType = GDT_Byte;
1648
1649
295
    poDS->nRasterXSize = poDS->sHeader.nWidth;
1650
295
    poDS->nRasterYSize = poDS->sHeader.nHeight;
1651
1652
295
    if (poDS->eRMFType == RMFT_RSW)
1653
124
    {
1654
124
        switch (poDS->sHeader.nBitDepth)
1655
124
        {
1656
2
            case 32:
1657
40
            case 24:
1658
40
            case 16:
1659
40
                poDS->nBands = 3;
1660
40
                break;
1661
1
            case 1:
1662
48
            case 4:
1663
84
            case 8:
1664
84
                if (poParentDS != nullptr &&
1665
84
                    poParentDS->poColorTable != nullptr)
1666
18
                {
1667
18
                    poDS->poColorTable = poParentDS->poColorTable->Clone();
1668
18
                }
1669
66
                else
1670
66
                {
1671
                    // Allocate memory for colour table and read it
1672
66
                    poDS->nColorTableSize = 1 << poDS->sHeader.nBitDepth;
1673
66
                    GUInt32 nExpectedColorTableBytes =
1674
66
                        poDS->nColorTableSize * 4;
1675
66
                    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
66
                    poDS->pabyColorTable = reinterpret_cast<GByte *>(
1688
66
                        VSIMalloc(nExpectedColorTableBytes));
1689
66
                    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
66
                    if (VSIFSeekL(
1696
66
                            poDS->fp,
1697
66
                            poDS->GetFileOffset(poDS->sHeader.nClrTblOffset),
1698
66
                            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
66
                    if (VSIFReadL(poDS->pabyColorTable, 1,
1705
66
                                  nExpectedColorTableBytes,
1706
66
                                  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
66
                    poDS->poColorTable = new GDALColorTable();
1714
5.42k
                    for (GUInt32 i = 0; i < poDS->nColorTableSize; i++)
1715
5.36k
                    {
1716
5.36k
                        const GDALColorEntry oEntry = {
1717
5.36k
                            poDS->pabyColorTable[i * 4],      // Red
1718
5.36k
                            poDS->pabyColorTable[i * 4 + 1],  // Green
1719
5.36k
                            poDS->pabyColorTable[i * 4 + 2],  // Blue
1720
5.36k
                            255                               // Alpha
1721
5.36k
                        };
1722
1723
5.36k
                        poDS->poColorTable->SetColorEntry(i, &oEntry);
1724
5.36k
                    }
1725
66
                }
1726
84
                poDS->nBands = 1;
1727
84
                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
124
        }
1735
124
        eType = GDT_Byte;
1736
124
    }
1737
171
    else
1738
171
    {
1739
171
        poDS->nBands = 1;
1740
171
        if (poDS->sHeader.nBitDepth == 8)
1741
0
        {
1742
0
            eType = GDT_Byte;
1743
0
        }
1744
171
        else if (poDS->sHeader.nBitDepth == 16)
1745
0
        {
1746
0
            eType = GDT_Int16;
1747
0
        }
1748
171
        else if (poDS->sHeader.nBitDepth == 32)
1749
171
        {
1750
171
            eType = GDT_Int32;
1751
171
        }
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
171
    }
1764
1765
295
    if (poDS->sHeader.nTileWidth == 0 || poDS->sHeader.nTileWidth > INT_MAX ||
1766
295
        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
295
    const int nDataSize = GDALGetDataTypeSizeBytes(eType);
1775
295
    const int nBlockXSize = static_cast<int>(poDS->sHeader.nTileWidth);
1776
295
    const int nBlockYSize = static_cast<int>(poDS->sHeader.nTileHeight);
1777
295
    if (nDataSize == 0 || nBlockXSize > INT_MAX / nBlockYSize ||
1778
295
        nBlockYSize > INT_MAX / nDataSize ||
1779
295
        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
295
    poDS->nXTiles = DIV_ROUND_UP(poDS->nRasterXSize, nBlockXSize);
1787
295
    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
295
    if (CE_None != poDS->SetupCompression(eType, poOpenInfo->pszFilename))
1798
1
    {
1799
1
        delete poDS;
1800
1
        return nullptr;
1801
1
    }
1802
1803
294
    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
668
    for (int iBand = 1; iBand <= poDS->nBands; iBand++)
1823
374
        poDS->SetBand(iBand, new RMFRasterBand(poDS, iBand, eType));
1824
1825
294
    poDS->SetupNBits();
1826
1827
294
    if (poDS->nBands > 1)
1828
40
    {
1829
40
        poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
1830
40
    }
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
294
    if (poDS->sHeader.iEPSGCode > RMF_EPSG_MIN_CODE ||
1838
294
        poDS->sHeader.iProjection > 0 ||
1839
294
        (poDS->sHeader.dfPixelSize != 0.0 && poDS->sHeader.dfLLX != 0.0 &&
1840
107
         poDS->sHeader.dfLLY != 0.0))
1841
193
    {
1842
193
        GInt32 nProj =
1843
193
            (poDS->sHeader.iProjection) ? poDS->sHeader.iProjection : 1;
1844
193
        double padfPrjParams[8] = {poDS->sHeader.dfStdP1,
1845
193
                                   poDS->sHeader.dfStdP2,
1846
193
                                   poDS->sHeader.dfCenterLat,
1847
193
                                   poDS->sHeader.dfCenterLong,
1848
193
                                   1.0,
1849
193
                                   0.0,
1850
193
                                   0.0,
1851
193
                                   0.0};
1852
1853
        // XXX: Compute zone number for Gauss-Kruger (Transverse Mercator)
1854
        // projection if it is not specified.
1855
193
        if (nProj == 1L && poDS->sHeader.dfCenterLong == 0.0)
1856
10
        {
1857
10
            if (poDS->sExtHeader.nZone == 0)
1858
1
            {
1859
1
                double centerXCoord =
1860
1
                    poDS->sHeader.dfLLX +
1861
1
                    (poDS->nRasterXSize * poDS->sHeader.dfPixelSize / 2.0);
1862
1
                padfPrjParams[7] = floor((centerXCoord - 500000.0) / 1000000.0);
1863
1
            }
1864
9
            else
1865
9
            {
1866
9
                padfPrjParams[7] = poDS->sExtHeader.nZone;
1867
9
            }
1868
10
        }
1869
1870
193
        OGRErr res = OGRERR_FAILURE;
1871
193
        if (nProj >= 0 &&
1872
193
            (poDS->sExtHeader.nDatum >= 0 || poDS->sExtHeader.nEllipsoid >= 0))
1873
193
        {
1874
193
            res = poDS->m_oSRS.importFromPanorama(
1875
193
                nProj, poDS->sExtHeader.nDatum, poDS->sExtHeader.nEllipsoid,
1876
193
                padfPrjParams);
1877
193
        }
1878
1879
193
        if (poDS->sHeader.iEPSGCode > RMF_EPSG_MIN_CODE &&
1880
193
            (OGRERR_NONE != res || poDS->m_oSRS.IsLocal()))
1881
1
        {
1882
1
            res = poDS->m_oSRS.importFromEPSG(poDS->sHeader.iEPSGCode);
1883
1
        }
1884
1885
193
        const char *pszSetVertCS =
1886
193
            CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "RMF_SET_VERTCS",
1887
193
                                 CPLGetConfigOption("RMF_SET_VERTCS", "NO"));
1888
193
        if (CPLTestBool(pszSetVertCS) && res == OGRERR_NONE &&
1889
193
            poDS->sExtHeader.nVertDatum > 0)
1890
0
        {
1891
0
            poDS->m_oSRS.importVertCSFromPanorama(poDS->sExtHeader.nVertDatum);
1892
0
        }
1893
193
    }
1894
1895
    /* -------------------------------------------------------------------- */
1896
    /*  Set up georeferencing.                                              */
1897
    /* -------------------------------------------------------------------- */
1898
294
    if ((poDS->eRMFType == RMFT_RSW && poDS->sHeader.iGeorefFlag) ||
1899
294
        (poDS->eRMFType == RMFT_MTW && poDS->sHeader.dfPixelSize != 0.0))
1900
182
    {
1901
182
        poDS->adfGeoTransform[0] = poDS->sHeader.dfLLX;
1902
182
        poDS->adfGeoTransform[3] =
1903
182
            poDS->sHeader.dfLLY +
1904
182
            poDS->nRasterYSize * poDS->sHeader.dfPixelSize;
1905
182
        poDS->adfGeoTransform[1] = poDS->sHeader.dfPixelSize;
1906
182
        poDS->adfGeoTransform[5] = -poDS->sHeader.dfPixelSize;
1907
182
        poDS->adfGeoTransform[2] = 0.0;
1908
182
        poDS->adfGeoTransform[4] = 0.0;
1909
182
    }
1910
1911
    /* -------------------------------------------------------------------- */
1912
    /*  Set units.                                                          */
1913
    /* -------------------------------------------------------------------- */
1914
1915
294
    if (poDS->eRMFType == RMFT_MTW)
1916
170
    {
1917
170
        CPLFree(poDS->pszUnitType);
1918
170
        poDS->pszUnitType = RMFUnitTypeToStr(poDS->sHeader.iElevationUnit);
1919
170
    }
1920
1921
    /* -------------------------------------------------------------------- */
1922
    /*  Report some other dataset related information.                      */
1923
    /* -------------------------------------------------------------------- */
1924
1925
294
    if (poDS->eRMFType == RMFT_MTW)
1926
170
    {
1927
170
        char szTemp[256] = {};
1928
1929
170
        snprintf(szTemp, sizeof(szTemp), "%g", poDS->sHeader.adfElevMinMax[0]);
1930
170
        poDS->SetMetadataItem("ELEVATION_MINIMUM", szTemp);
1931
1932
170
        snprintf(szTemp, sizeof(szTemp), "%g", poDS->sHeader.adfElevMinMax[1]);
1933
170
        poDS->SetMetadataItem("ELEVATION_MAXIMUM", szTemp);
1934
1935
170
        poDS->SetMetadataItem("ELEVATION_UNITS", poDS->pszUnitType);
1936
1937
170
        snprintf(szTemp, sizeof(szTemp), "%d", poDS->sHeader.iElevationType);
1938
170
        poDS->SetMetadataItem("ELEVATION_TYPE", szTemp);
1939
170
    }
1940
1941
    /* -------------------------------------------------------------------- */
1942
    /*      Check for overviews.                                            */
1943
    /* -------------------------------------------------------------------- */
1944
294
    if (nNextHeaderOffset == 0 && poParentDS == nullptr)
1945
272
    {
1946
272
        poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
1947
272
    }
1948
1949
    /* Set frame */
1950
294
    if (poDS->sHeader.nROIOffset &&
1951
294
        poDS->sHeader.nROISize >= sizeof(RSWFrame) &&
1952
294
        poDS->sHeader.nROISize <= ROI_MAX_SIZE_TO_AVOID_EXCESSIVE_RAM_USAGE)
1953
20
    {
1954
20
        GByte *pabyROI = reinterpret_cast<GByte *>(
1955
20
            VSI_MALLOC_VERBOSE(poDS->sHeader.nROISize));
1956
20
        if (pabyROI == nullptr)
1957
0
        {
1958
0
            delete poDS;
1959
0
            return nullptr;
1960
0
        }
1961
1962
20
        VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nROIOffset),
1963
20
                  SEEK_SET);
1964
20
        if (VSIFReadL(pabyROI, poDS->sHeader.nROISize, 1, poDS->fp) != 1)
1965
1
        {
1966
1
            CPLError(CE_Failure, CPLE_FileIO, "Cannot read ROI");
1967
1
            CPLFree(pabyROI);
1968
1
            delete poDS;
1969
1
            return nullptr;
1970
1
        }
1971
1972
19
        GInt32 nFrameType;
1973
19
        RMF_READ_LONG(pabyROI, nFrameType, 0);
1974
19
        if (nFrameType == nPolygonType)
1975
2
        {
1976
2
            CPLString osWKT = "POLYGON((";
1977
2
            bool bFirst = true;
1978
1979
2
            CPLDebug("RMF", "ROI coordinates:");
1980
            /* coverity[tainted_data] */
1981
2
            for (GUInt32 i = sizeof(RSWFrame);
1982
289
                 i + sizeof(RSWFrameCoord) <= poDS->sHeader.nROISize;
1983
287
                 i += sizeof(RSWFrameCoord))
1984
287
            {
1985
287
                GInt32 nX, nY;
1986
287
                RMF_READ_LONG(pabyROI, nX, i);
1987
287
                RMF_READ_LONG(pabyROI, nY, i + 4);
1988
1989
287
                CPLDebug("RMF", "X: %d, Y: %d", nX, nY);
1990
1991
287
                double dfX = poDS->adfGeoTransform[0] +
1992
287
                             nX * poDS->adfGeoTransform[1] +
1993
287
                             nY * poDS->adfGeoTransform[2];
1994
287
                double dfY = poDS->adfGeoTransform[3] +
1995
287
                             nX * poDS->adfGeoTransform[4] +
1996
287
                             nY * poDS->adfGeoTransform[5];
1997
1998
287
                if (bFirst)
1999
2
                {
2000
2
                    osWKT += CPLSPrintf("%f %f", dfX, dfY);
2001
2
                    bFirst = false;
2002
2
                }
2003
285
                else
2004
285
                {
2005
285
                    osWKT += CPLSPrintf(", %f %f", dfX, dfY);
2006
285
                }
2007
287
            }
2008
2
            osWKT += "))";
2009
2
            CPLDebug("RMF", "Frame WKT: %s", osWKT.c_str());
2010
2
            poDS->SetMetadataItem(MD_FRAME_KEY, osWKT);
2011
2
        }
2012
19
        CPLFree(pabyROI);
2013
19
    }
2014
2015
293
#undef RMF_READ_DOUBLE
2016
293
#undef RMF_READ_LONG
2017
293
#undef RMF_READ_ULONG
2018
2019
293
    if (poDS->sHeader.nFlagsTblOffset && poDS->sHeader.nFlagsTblSize)
2020
176
    {
2021
176
        VSIFSeekL(poDS->fp, poDS->GetFileOffset(poDS->sHeader.nFlagsTblOffset),
2022
176
                  SEEK_SET);
2023
176
        CPLDebug("RMF", "Blocks flags:");
2024
        /* coverity[tainted_data] */
2025
29.4k
        for (GUInt32 i = 0; i < poDS->sHeader.nFlagsTblSize; i += sizeof(GByte))
2026
29.4k
        {
2027
29.4k
            GByte nValue;
2028
29.4k
            if (VSIFReadL(&nValue, 1, sizeof(nValue), poDS->fp) !=
2029
29.4k
                sizeof(nValue))
2030
144
            {
2031
144
                CPLDebug("RMF", "Cannot read Block flag at index %u", i);
2032
144
                break;
2033
144
            }
2034
29.2k
            CPLDebug("RMF", "Block %u -- flag %d", i, nValue);
2035
29.2k
        }
2036
176
    }
2037
293
    return poDS;
2038
294
}
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
1.83k
{
2409
1.83k
    if (sHeader.iVersion >= RMF_VERSION_HUGE)
2410
0
    {
2411
0
        return (static_cast<vsi_l_offset>(iRMFOffset)) * RMF_HUGE_OFFSET_FACTOR;
2412
0
    }
2413
2414
1.83k
    return static_cast<vsi_l_offset>(iRMFOffset);
2415
1.83k
}
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
293
{
2443
293
    if (sHeader.nOvrOffset == 0)
2444
161
    {
2445
161
        return nullptr;
2446
161
    }
2447
2448
132
    if (poParent == nullptr)
2449
0
    {
2450
0
        return nullptr;
2451
0
    }
2452
2453
132
    vsi_l_offset nSubOffset = GetFileOffset(sHeader.nOvrOffset);
2454
2455
132
    CPLDebug("RMF",
2456
132
             "Try to open overview subfile at " CPL_FRMT_GUIB " for '%s'",
2457
132
             nSubOffset, poOpenInfo->pszFilename);
2458
2459
132
    if (!poParent->poOvrDatasets.empty())
2460
17
    {
2461
17
        if (poParent->GetFileOffset(poParent->sHeader.nOvrOffset) == nSubOffset)
2462
1
        {
2463
1
            CPLError(CE_Warning, CPLE_IllegalArg,
2464
1
                     "Recursive subdataset list is detected. "
2465
1
                     "Overview open failed.");
2466
1
            return nullptr;
2467
1
        }
2468
2469
22
        for (size_t n = 0; n != poParent->poOvrDatasets.size() - 1; ++n)
2470
7
        {
2471
7
            RMFDataset *poOvr(poParent->poOvrDatasets[n]);
2472
2473
7
            if (poOvr == nullptr)
2474
0
                continue;
2475
7
            if (poOvr->GetFileOffset(poOvr->sHeader.nOvrOffset) == nSubOffset)
2476
1
            {
2477
1
                CPLError(CE_Warning, CPLE_IllegalArg,
2478
1
                         "Recursive subdataset list is detected. "
2479
1
                         "Overview open failed.");
2480
1
                return nullptr;
2481
1
            }
2482
7
        }
2483
16
    }
2484
2485
130
    size_t nHeaderSize(RMF_HEADER_SIZE);
2486
130
    GByte *pabyNewHeader;
2487
130
    pabyNewHeader = static_cast<GByte *>(
2488
130
        CPLRealloc(poOpenInfo->pabyHeader, nHeaderSize + 1));
2489
130
    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
130
    poOpenInfo->pabyHeader = pabyNewHeader;
2497
130
    memset(poOpenInfo->pabyHeader, 0, nHeaderSize + 1);
2498
130
    VSIFSeekL(fp, nSubOffset, SEEK_SET);
2499
130
    poOpenInfo->nHeaderBytes =
2500
130
        static_cast<int>(VSIFReadL(poOpenInfo->pabyHeader, 1, nHeaderSize, fp));
2501
2502
130
    return Open(poOpenInfo, poParent, nSubOffset);
2503
130
}
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
295
{
2794
    /* -------------------------------------------------------------------- */
2795
    /*  XXX: The DEM compression method seems to be only applicable         */
2796
    /*  to Int32 data.                                                      */
2797
    /* -------------------------------------------------------------------- */
2798
295
    if (sHeader.iCompression == RMF_COMPRESSION_NONE)
2799
23
    {
2800
23
        Decompress = nullptr;
2801
23
        Compress = nullptr;
2802
23
    }
2803
272
    else if (sHeader.iCompression == RMF_COMPRESSION_LZW)
2804
87
    {
2805
87
        Decompress = &LZWDecompress;
2806
87
        Compress = &LZWCompress;
2807
87
        SetMetadataItem("COMPRESSION", "LZW", "IMAGE_STRUCTURE");
2808
87
    }
2809
185
    else if (sHeader.iCompression == RMF_COMPRESSION_JPEG)
2810
17
    {
2811
17
        if (eType != GDT_Byte || nBands != RMF_JPEG_BAND_COUNT ||
2812
17
            sHeader.nBitDepth != 24)
2813
1
        {
2814
1
            CPLError(CE_Failure, CPLE_AppDefined,
2815
1
                     "RMF support only 24 bpp JPEG compressed files.");
2816
1
            return CE_Failure;
2817
1
        }
2818
16
#ifdef HAVE_LIBJPEG
2819
16
        CPLString oBuf;
2820
16
        oBuf.Printf("%d", sHeader.iJpegQuality);
2821
16
        Decompress = &JPEGDecompress;
2822
16
        Compress = &JPEGCompress;
2823
16
        SetMetadataItem("JPEG_QUALITY", oBuf.c_str(), "IMAGE_STRUCTURE");
2824
16
        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
16
    }
2833
168
    else if (sHeader.iCompression == RMF_COMPRESSION_DEM &&
2834
168
             eType == GDT_Int32 && nBands == RMF_DEM_BAND_COUNT)
2835
168
    {
2836
168
        Decompress = &DEMDecompress;
2837
168
        Compress = &DEMCompress;
2838
168
        SetMetadataItem("COMPRESSION", "RMF_DEM", "IMAGE_STRUCTURE");
2839
168
    }
2840
0
    else
2841
0
    {
2842
0
        CPLError(CE_Failure, CPLE_AppDefined,
2843
0
                 "Unknown compression #%d at file <%s>.", sHeader.iCompression,
2844
0
                 pszFilename);
2845
0
        return CE_Failure;
2846
0
    }
2847
2848
294
    return CE_None;
2849
295
}
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
825
{
3129
825
    bNullTile = false;
3130
3131
825
    const GUInt32 nTile = nBlockYOff * nXTiles + nBlockXOff;
3132
825
    if (2 * nTile + 1 >= sHeader.nTileTblSize / sizeof(GUInt32))
3133
1
    {
3134
1
        return CE_Failure;
3135
1
    }
3136
824
    vsi_l_offset nTileOffset = GetFileOffset(paiTiles[2 * nTile]);
3137
824
    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
824
    GUInt32 nMaxTileBytes =
3141
824
        2 * sHeader.nTileWidth * sHeader.nTileHeight * sHeader.nBitDepth / 8;
3142
3143
824
    if (nTileBytes >= nMaxTileBytes)
3144
18
    {
3145
18
        CPLError(CE_Failure, CPLE_AppDefined,
3146
18
                 "Invalid tile size %lu at offset %ld. Must be less than %lu",
3147
18
                 static_cast<unsigned long>(nTileBytes),
3148
18
                 static_cast<long>(nTileOffset),
3149
18
                 static_cast<unsigned long>(nMaxTileBytes));
3150
18
        return CE_Failure;
3151
18
    }
3152
3153
806
    if (nTileOffset == 0)
3154
80
    {
3155
80
        bNullTile = true;
3156
80
        return CE_None;
3157
80
    }
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
726
    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
726
    if (Decompress == nullptr || nTileBytes == nRawBytes)
3179
22
    {
3180
22
        if (nTileBytes != nRawBytes)
3181
6
        {
3182
6
            CPLError(CE_Failure, CPLE_AppDefined,
3183
6
                     "RMF: Invalid tile size %lu, expected %lu",
3184
6
                     static_cast<unsigned long>(nTileBytes),
3185
6
                     static_cast<unsigned long>(nRawBytes));
3186
6
            return CE_Failure;
3187
6
        }
3188
3189
16
        if (VSIFReadL(pabyData, 1, nRawBytes, fp) < nRawBytes)
3190
4
        {
3191
4
            CPLError(CE_Failure, CPLE_FileIO,
3192
4
                     "RMF: Can't read at offset %lu from input file.\n%s",
3193
4
                     static_cast<unsigned long>(nTileOffset),
3194
4
                     VSIStrerror(errno));
3195
4
            return CE_Failure;
3196
4
        }
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
12
        return CE_None;
3219
16
    }
3220
3221
704
    if (pabyDecompressBuffer == nullptr)
3222
241
    {
3223
241
        pabyDecompressBuffer =
3224
241
            static_cast<GByte *>(VSIMalloc(std::max(1U, nMaxTileBytes)));
3225
241
        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
241
    }
3234
3235
704
    if (VSIFReadL(pabyDecompressBuffer, 1, nTileBytes, fp) < nTileBytes)
3236
144
    {
3237
144
        CPLError(CE_Failure, CPLE_FileIO,
3238
144
                 "RMF: Can't read at offset %lu from input file.\n%s",
3239
144
                 static_cast<unsigned long>(nTileOffset), VSIStrerror(errno));
3240
144
        return CE_Failure;
3241
144
    }
3242
3243
560
    size_t nDecompressedSize =
3244
560
        Decompress(pabyDecompressBuffer, nTileBytes, pabyData,
3245
560
                   static_cast<GUInt32>(nRawBytes), nRawXSize, nRawYSize);
3246
3247
560
    if (nDecompressedSize != static_cast<size_t>(nRawBytes))
3248
127
    {
3249
127
        CPLError(CE_Failure, CPLE_FileIO,
3250
127
                 "Can't decompress tile xOff %d yOff %d. "
3251
127
                 "Raw tile size is %lu but decompressed is %lu. "
3252
127
                 "Compressed tile size is %lu",
3253
127
                 nBlockXOff, nBlockYOff, static_cast<unsigned long>(nRawBytes),
3254
127
                 static_cast<unsigned long>(nDecompressedSize),
3255
127
                 static_cast<unsigned long>(nTileBytes));
3256
127
        return CE_Failure;
3257
127
    }
3258
    // We don't need to swap bytes here,
3259
    // because decompressed data is in proper byte order
3260
433
    return CE_None;
3261
560
}
3262
3263
void RMFDataset::SetupNBits()
3264
294
{
3265
294
    int nBitDepth = 0;
3266
294
    if (sHeader.nBitDepth < 8 && nBands == 1)
3267
48
    {
3268
48
        nBitDepth = static_cast<int>(sHeader.nBitDepth);
3269
48
    }
3270
246
    else if (sHeader.nBitDepth == 16 && nBands == 3 && eRMFType == RMFT_RSW)
3271
0
    {
3272
0
        nBitDepth = 5;
3273
0
    }
3274
3275
294
    if (nBitDepth > 0)
3276
48
    {
3277
48
        char szNBits[32] = {};
3278
48
        snprintf(szNBits, sizeof(szNBits), "%d", nBitDepth);
3279
96
        for (int iBand = 1; iBand <= nBands; iBand++)
3280
48
        {
3281
48
            GetRasterBand(iBand)->SetMetadataItem("NBITS", szNBits,
3282
48
                                                  "IMAGE_STRUCTURE");
3283
48
        }
3284
48
    }
3285
294
}
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
2.50k
{
3381
2.50k
    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
2.50k
    return GDALDataset::SetMetadataItem(pszName, pszValue, pszDomain);
3402
2.50k
}
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
}