Coverage Report

Created: 2025-11-15 08:43

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