LCOV - code coverage report
Current view: top level - frmts/mrf - marfa.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 156 177 88.1 %
Date: 2024-05-04 12:52:34 Functions: 59 68 86.8 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2002-2012, California Institute of Technology.
       3             :  * All rights reserved.  Based on Government Sponsored Research under contracts
       4             :  * NAS7-1407 and/or NAS7-03001.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are met:
       8             :  *   1. Redistributions of source code must retain the above copyright notice,
       9             :  * this list of conditions and the following disclaimer.
      10             :  *   2. Redistributions in binary form must reproduce the above copyright
      11             :  * notice, this list of conditions and the following disclaimer in the
      12             :  * documentation and/or other materials provided with the distribution.
      13             :  *   3. Neither the name of the California Institute of Technology (Caltech),
      14             :  * its operating division the Jet Propulsion Laboratory (JPL), the National
      15             :  * Aeronautics and Space Administration (NASA), nor the names of its
      16             :  * contributors may be used to endorse or promote products derived from this
      17             :  * software without specific prior written permission.
      18             :  *
      19             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      20             :  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      21             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      22             :  * ARE DISCLAIMED. IN NO EVENT SHALL THE CALIFORNIA INSTITUTE OF TECHNOLOGY BE
      23             :  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      24             :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      25             :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      26             :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      27             :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      28             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      29             :  * POSSIBILITY OF SUCH DAMAGE.
      30             :  *
      31             :  * Copyright 2014-2021 Esri
      32             :  *
      33             :  * Licensed under the Apache License, Version 2.0 (the "License");
      34             :  * you may not use this file except in compliance with the License.
      35             :  * You may obtain a copy of the License at
      36             :  *
      37             :  * http://www.apache.org/licenses/LICENSE-2.0
      38             :  *
      39             :  * Unless required by applicable law or agreed to in writing, software
      40             :  * distributed under the License is distributed on an "AS IS" BASIS,
      41             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      42             :  * See the License for the specific language governing permissions and
      43             :  * limitations under the License.
      44             :  */
      45             : 
      46             : /******************************************************************************
      47             :  *
      48             :  * Project:  Meta Raster Format
      49             :  * Purpose:  MRF structures
      50             :  * Author:   Lucian Plesea
      51             :  *
      52             :  ******************************************************************************
      53             :  *
      54             :  *
      55             :  *
      56             :  ****************************************************************************/
      57             : 
      58             : #ifndef GDAL_FRMTS_MRF_MARFA_H_INCLUDED
      59             : #define GDAL_FRMTS_MRF_MARFA_H_INCLUDED
      60             : 
      61             : #include "gdal_pam.h"
      62             : #include "ogr_srs_api.h"
      63             : #include "ogr_spatialref.h"
      64             : 
      65             : #include <limits>
      66             : // For printing values
      67             : #include <ostream>
      68             : #include <iostream>
      69             : #include <sstream>
      70             : #include <chrono>
      71             : #if defined(ZSTD_SUPPORT)
      72             : #include <zstd.h>
      73             : #endif
      74             : 
      75             : #define NAMESPACE_MRF_START                                                    \
      76             :     namespace GDAL_MRF                                                         \
      77             :     {
      78             : #define NAMESPACE_MRF_END }
      79             : #define USING_NAMESPACE_MRF using namespace GDAL_MRF;
      80             : 
      81             : NAMESPACE_MRF_START
      82             : 
      83             : // ZLIB Bit flag fields
      84             : // 0:3 - level, 4 - GZip, 5 RAW zlib, 6:9 strategy
      85             : #define ZFLAG_LMASK 0xF
      86             : // GZ and RAW are mutually exclusive, GZ has higher priority
      87             : // If neither is set, use zlib stream format
      88             : #define ZFLAG_GZ 0x10
      89             : #define ZFLAG_RAW 0x20
      90             : 
      91             : // Mask for zlib strategy, valid values are 0 to 4 shifted six bits, see zlib
      92             : // for meaning Can use one of: Z_DEFAULT : whatever zlib decides Z_FILTERED :
      93             : // optimized for delta encoding Z_HUFFMAN_ONLY : Only huffman encoding
      94             : // (adaptive) Z_RLE : Only next character matches Z_FIXED : Static huffman,
      95             : // faster decoder
      96             : //
      97             : #define ZFLAG_SMASK 0x1c0
      98             : 
      99             : #define PADDING_BYTES 3
     100             : 
     101             : // Force LERC to be included, normally off, detected in the makefile
     102             : // #define LERC
     103             : 
     104             : // These are a pain to maintain in sync.  They should be replaced with
     105             : // C++11 uniform initializers.  The externs reside in util.cpp
     106             : enum ILCompression
     107             : {
     108             : #ifdef HAVE_PNG
     109             :     IL_PNG,
     110             :     IL_PPNG,
     111             : #endif
     112             : #ifdef HAVE_JPEG
     113             :     IL_JPEG,
     114             : #endif
     115             : #if defined(HAVE_PNG) && defined(HAVE_JPEG)
     116             :     IL_JPNG,
     117             : #endif
     118             :     IL_NONE,
     119             :     IL_ZLIB,
     120             :     IL_TIF,
     121             : #if defined(LERC)
     122             :     IL_LERC,
     123             : #endif
     124             : #if defined(ZSTD_SUPPORT)
     125             :     IL_ZSTD,
     126             : #endif
     127             : #if defined(QB3_SUPPORT)
     128             :     IL_QB3,
     129             : #endif
     130             :     IL_ERR_COMP
     131             : };
     132             : 
     133             : // Sequential is not supported by GDAL
     134             : enum ILOrder
     135             : {
     136             :     IL_Interleaved = 0,
     137             :     IL_Separate,
     138             :     IL_Sequential,
     139             :     IL_ERR_ORD
     140             : };
     141             : 
     142             : extern char const *const *ILComp_Name;
     143             : extern char const *const *ILComp_Ext;
     144             : extern char const *const *ILOrder_Name;
     145             : 
     146             : class MRFDataset;
     147             : class MRFRasterBand;
     148             : 
     149             : typedef struct
     150             : {
     151             :     char *buffer;
     152             :     size_t size;
     153             : } buf_mgr;
     154             : 
     155             : // A tile index record, 16 bytes, big endian
     156             : typedef struct
     157             : {
     158             :     GIntBig offset;
     159             :     GIntBig size;
     160             : } ILIdx;
     161             : 
     162             : // Size of an image, also used as a tile or pixel location
     163             : struct ILSize
     164             : {
     165             :     GInt32 x, y, z, c;
     166             :     GIntBig l;  // Dual use, sometimes it holds the number of pages
     167             : 
     168       17151 :     explicit ILSize(const int x_ = -1, const int y_ = -1, const int z_ = -1,
     169             :                     const int c_ = -1, const int l_ = -1)
     170       17151 :         : x(x_), y(y_), z(z_), c(c_), l(l_)
     171             :     {
     172       17151 :     }
     173             : 
     174             :     bool operator==(const ILSize &other) const
     175             :     {
     176             :         return ((x == other.x) && (y == other.y) && (z == other.z) &&
     177             :                 (c == other.c) && (l == other.l));
     178             :     }
     179             : 
     180             :     bool operator!=(const ILSize &other) const
     181             :     {
     182             :         return !(*this == other);
     183             :     }
     184             : };
     185             : 
     186             : std::ostream &operator<<(std::ostream &out, const ILSize &sz);
     187             : std::ostream &operator<<(std::ostream &out, const ILIdx &t);
     188             : 
     189             : bool is_Endianness_Dependent(GDALDataType dt, ILCompression comp);
     190             : 
     191             : // Debugging support
     192             : // #define PPMW
     193             : #ifdef PPMW
     194             : void ppmWrite(const char *fname, const char *data, const ILSize &sz);
     195             : #endif
     196             : 
     197             : /**
     198             :  * Collects information pertaining to a single raster
     199             :  * This structure is being shallow copied, no pointers allowed
     200             :  *
     201             :  */
     202             : 
     203             : typedef struct ILImage
     204             : {
     205             :     ILImage();
     206             :     GIntBig dataoffset;
     207             :     GIntBig idxoffset;
     208             :     GInt32 quality;
     209             :     GInt32 pageSizeBytes;
     210             :     ILSize size;
     211             :     ILSize pagesize;
     212             :     ILSize pagecount;
     213             :     ILCompression comp;
     214             :     ILOrder order;
     215             :     bool nbo;
     216             :     int hasNoData;
     217             :     double NoDataValue;
     218             :     CPLString datfname;
     219             :     CPLString idxfname;
     220             :     GDALDataType dt;
     221             :     GDALColorInterp ci;
     222             : } ILImage;
     223             : 
     224             : // Declarations of utility functions
     225             : 
     226             : /**
     227             :  *
     228             :  *\brief  Converters between endianness
     229             :  *  Call netXX() to guarantee big endian
     230             :  *
     231             :  */
     232       56696 : static inline unsigned short int swab16(const unsigned short int val)
     233             : {
     234       56696 :     return (val << 8) | (val >> 8);
     235             : }
     236             : 
     237       28348 : static inline unsigned int swab32(unsigned int val)
     238             : {
     239       28348 :     return (unsigned int)(swab16((unsigned short int)val)) << 16 |
     240       28348 :            swab16((unsigned short int)(val >> 16));
     241             : }
     242             : 
     243       14174 : static inline unsigned long long int swab64(const unsigned long long int val)
     244             : {
     245       14174 :     return (unsigned long long int)(swab32((unsigned int)val)) << 32 |
     246       14174 :            swab32((unsigned int)(val >> 32));
     247             : }
     248             : 
     249             : // NET_ORDER is true if machine is BE, false otherwise
     250             : // Call netxx() if network (big) order is needed
     251             : 
     252             : #ifdef CPL_MSB
     253             : #define NET_ORDER true
     254             : 
     255             : // These could be macros, but for the side effects related to type
     256             : static inline unsigned short net16(const unsigned short x)
     257             : {
     258             :     return (x);
     259             : }
     260             : 
     261             : static inline unsigned int net32(const unsigned int x)
     262             : {
     263             :     return (x);
     264             : }
     265             : 
     266             : static inline unsigned long long net64(const unsigned long long x)
     267             : {
     268             :     return (x);
     269             : }
     270             : 
     271             : #else
     272             : #define NET_ORDER false
     273             : #define net16(x) swab16(x)
     274             : #define net32(x) swab32(x)
     275             : #define net64(x) swab64(x)
     276             : #endif
     277             : 
     278             : // Count the values in a buffer that match a specific value
     279          16 : template <typename T> static int MatchCount(T *buff, int sz, T val)
     280             : {
     281          16 :     int ncount = 0;
     282      529904 :     for (int i = 0; i < sz; i++)
     283      529888 :         if (buff[i] == val)
     284      129822 :             ncount++;
     285          16 :     return ncount;
     286             : }
     287             : 
     288             : const char *CompName(ILCompression comp);
     289             : const char *OrderName(ILOrder val);
     290             : ILCompression CompToken(const char *, ILCompression def = IL_ERR_COMP);
     291             : ILOrder OrderToken(const char *, ILOrder def = IL_ERR_ORD);
     292             : CPLString getFname(CPLXMLNode *, const char *, const CPLString &, const char *);
     293             : CPLString getFname(const CPLString &, const char *);
     294             : double getXMLNum(CPLXMLNode *, const char *, double);
     295             : GIntBig IdxOffset(const ILSize &, const ILImage &);
     296             : double logbase(double val, double base);
     297             : int IsPower(double value, double base);
     298             : CPLXMLNode *SearchXMLSiblings(CPLXMLNode *psRoot, const char *pszElement);
     299             : CPLString PrintDouble(double d, const char *frmt = "%12.8f");
     300             : void XMLSetAttributeVal(CPLXMLNode *parent, const char *pszName,
     301             :                         const char *pszValue);
     302             : void XMLSetAttributeVal(CPLXMLNode *parent, const char *pszName,
     303             :                         const double val, const char *frmt = "%12.8f");
     304             : CPLXMLNode *XMLSetAttributeVal(CPLXMLNode *parent, const char *pszName,
     305             :                                const ILSize &sz, const char *frmt = nullptr);
     306             : void XMLSetAttributeVal(CPLXMLNode *parent, const char *pszName,
     307             :                         std::vector<double> const &values);
     308             : 
     309             : GIntBig IdxSize(const ILImage &full, const int scale = 0);
     310             : int CheckFileSize(const char *fname, GIntBig sz, GDALAccess eAccess);
     311             : 
     312             : // Number of pages of size psz needed to hold n elements
     313        6610 : static inline int pcount(const int n, const int sz)
     314             : {
     315        6610 :     return 1 + (n - 1) / sz;
     316             : }
     317             : 
     318             : // Returns a pagecount per dimension, .l will have the total number
     319             : // or -1 in case of error
     320        1596 : static inline const ILSize pcount(const ILSize &size, const ILSize &psz)
     321             : {
     322        1596 :     ILSize pcnt;
     323        1596 :     pcnt.x = pcount(size.x, psz.x);
     324        1596 :     pcnt.y = pcount(size.y, psz.y);
     325        1596 :     pcnt.z = pcount(size.z, psz.z);
     326        1596 :     pcnt.c = pcount(size.c, psz.c);
     327        1596 :     auto xy = static_cast<GIntBig>(pcnt.x) * pcnt.y;
     328        1596 :     auto zc = static_cast<GIntBig>(pcnt.z) * pcnt.c;
     329        1596 :     if (zc != 0 && xy > std::numeric_limits<GIntBig>::max() / zc)
     330             :     {
     331           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     332             :                  "Integer overflow in page count computation");
     333           0 :         pcnt.l = -1;
     334           0 :         return pcnt;
     335             :     }
     336        1596 :     pcnt.l = xy * zc;
     337        1596 :     return pcnt;
     338             : }
     339             : 
     340             : // Wrapper around the VISFile, remembers how the file was opened
     341             : typedef struct
     342             : {
     343             :     VSILFILE *FP;
     344             :     GDALRWFlag acc;
     345             : } VF;
     346             : 
     347             : // Offset of index, pos is in pages
     348             : GIntBig IdxOffset(const ILSize &pos, const ILImage &img);
     349             : 
     350             : enum
     351             : {
     352             :     SAMPLING_ERR,
     353             :     SAMPLING_Avg,
     354             :     SAMPLING_Near
     355             : };
     356             : 
     357             : MRFRasterBand *newMRFRasterBand(MRFDataset *, const ILImage &, int,
     358             :                                 int level = 0);
     359             : 
     360             : class MRFDataset final : public GDALPamDataset
     361             : {
     362             :     friend class MRFRasterBand;
     363             :     friend MRFRasterBand *newMRFRasterBand(MRFDataset *, const ILImage &, int,
     364             :                                            int level);
     365             : 
     366             :   public:
     367             :     MRFDataset();
     368             :     virtual ~MRFDataset();
     369             : 
     370             :     static GDALDataset *Open(GDALOpenInfo *);
     371             : 
     372             :     static GDALDataset *CreateCopy(const char *pszFilename,
     373             :                                    GDALDataset *poSrcDS, int bStrict,
     374             :                                    char **papszOptions,
     375             :                                    GDALProgressFunc pfnProgress,
     376             :                                    void *pProgressData);
     377             : 
     378             :     static GDALDataset *Create(const char *pszName, int nXSize, int nYSize,
     379             :                                int nBands, GDALDataType eType,
     380             :                                char **papszOptions);
     381             : 
     382             :     // Stub for delete, GDAL should only overwrite the XML
     383         106 :     static CPLErr Delete(const char *)
     384             :     {
     385         106 :         return CE_None;
     386             :     }
     387             : 
     388         427 :     const OGRSpatialReference *GetSpatialRef() const override
     389             :     {
     390         427 :         return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
     391             :     }
     392             : 
     393          10 :     CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override
     394             :     {
     395          10 :         m_oSRS.Clear();
     396          10 :         if (poSRS)
     397          10 :             m_oSRS = *poSRS;
     398          10 :         return CE_None;
     399             :     }
     400             : 
     401          31 :     virtual CPLString const &GetPhotometricInterpretation()
     402             :     {
     403          31 :         return photometric;
     404             :     }
     405             : 
     406           5 :     virtual CPLErr SetPhotometricInterpretation(const char *photo)
     407             :     {
     408           5 :         photometric = photo;
     409           5 :         return CE_None;
     410             :     }
     411             : 
     412             :     virtual CPLErr GetGeoTransform(double *gt) override;
     413             :     virtual CPLErr SetGeoTransform(double *gt) override;
     414             : 
     415             :     virtual char **GetFileList() override;
     416             : 
     417           2 :     void SetColorTable(GDALColorTable *pct)
     418             :     {
     419           2 :         poColorTable = pct;
     420           2 :     }
     421             : 
     422         405 :     const GDALColorTable *GetColorTable()
     423             :     {
     424         405 :         return poColorTable;
     425             :     }
     426             : 
     427             :     void SetNoDataValue(const char *);
     428             :     void SetMinValue(const char *);
     429             :     void SetMaxValue(const char *);
     430             :     CPLErr SetVersion(int version);
     431             : 
     432        1937 :     const CPLString GetFname()
     433             :     {
     434        1937 :         return fname;
     435             :     }
     436             : 
     437             :     // Patches a region of all the next overview, argument counts are in blocks
     438             :     // Exported for mrf_insert utility
     439             :     virtual CPL_DLL CPLErr PatchOverview(int BlockX, int BlockY, int Width,
     440             :                                          int Height, int srcLevel = 0,
     441             :                                          int recursive = false,
     442             :                                          int sampling_mode = SAMPLING_Avg);
     443             : 
     444             :     // Creates an XML tree from the current MRF.  If written to a file it
     445             :     // becomes an MRF
     446             :     CPLXMLNode *BuildConfig();
     447             : 
     448         231 :     void SetPBufferSize(unsigned int sz)
     449             :     {
     450         231 :         pbsize = sz;
     451         231 :     }
     452             : 
     453        5486 :     unsigned int GetPBufferSize()
     454             :     {
     455        5486 :         return pbsize;
     456             :     }
     457             : 
     458             :   protected:
     459             :     // False if it failed
     460             :     int Crystalize();
     461             : 
     462             :     CPLErr LevelInit(const int l);
     463             : 
     464             :     // Reads the XML metadata and returns the XML
     465             :     CPLXMLNode *ReadConfig() const;
     466             : 
     467             :     // Apply create options to the current dataset
     468             :     void ProcessCreateOptions(char **papszOptions);
     469             : 
     470             :     // Called once before the parsing of the XML, should just capture the
     471             :     // options in dataset variables
     472             :     void ProcessOpenOptions(char **papszOptions);
     473             : 
     474             :     // Writes the XML tree as MRF.  It does not check the content
     475             :     int WriteConfig(CPLXMLNode *);
     476             : 
     477             :     // Initializes the dataset from an MRF metadata XML
     478             :     // Options should be papszOpenOptions, but the dataset already has a member
     479             :     // with that name
     480             :     CPLErr Initialize(CPLXMLNode *);
     481             : 
     482             :     bool IsSingleTile();
     483             : 
     484             :     // Add uniform scale overlays, returns the new size of the index file
     485             :     GIntBig AddOverviews(int scale);
     486             : 
     487             :     // Late allocation buffer
     488             :     bool SetPBuffer(unsigned int sz);
     489             : 
     490        5096 :     void *GetPBuffer()
     491             :     {
     492        5096 :         if (!pbuffer && pbsize)
     493          45 :             SetPBuffer(pbsize);
     494        5096 :         return pbuffer;
     495             :     }
     496             : 
     497             :     virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
     498             :                              GDALDataType, int, int *, GSpacing, GSpacing,
     499             :                              GSpacing, GDALRasterIOExtraArg *) override;
     500             : 
     501             :     virtual CPLErr IBuildOverviews(const char *, int, const int *, int,
     502             :                                    const int *, GDALProgressFunc, void *,
     503             :                                    CSLConstList papszOptions) override;
     504             : 
     505             :     virtual int CloseDependentDatasets() override;
     506             : 
     507             :     // Write a tile, the infooffset is the relative position in the index file
     508             :     virtual CPLErr WriteTile(void *buff, GUIntBig infooffset,
     509             :                              GUIntBig size = 0);
     510             : 
     511             :     // Custom CopyWholeRaster for Zen JPEG
     512             :     CPLErr ZenCopy(GDALDataset *poSrc, GDALProgressFunc pfnProgress,
     513             :                    void *pProgressData);
     514             : 
     515             :     // For versioned MRFs, add a version
     516             :     CPLErr AddVersion();
     517             : 
     518             :     // Read the index record itself
     519             :     CPLErr ReadTileIdx(ILIdx &tinfo, const ILSize &pos, const ILImage &img,
     520             :                        const GIntBig bias = 0);
     521             : 
     522             :     VSILFILE *IdxFP();
     523             :     VSILFILE *DataFP();
     524             : 
     525           4 :     GDALRWFlag IdxMode()
     526             :     {
     527           4 :         if (!ifp.FP)
     528           0 :             IdxFP();
     529           4 :         return ifp.acc;
     530             :     }
     531             : 
     532           1 :     GDALRWFlag DataMode()
     533             :     {
     534           1 :         if (!dfp.FP)
     535           1 :             DataFP();
     536           1 :         return dfp.acc;
     537             :     }
     538             : 
     539             :     GDALDataset *GetSrcDS();
     540             : 
     541             :     /*
     542             :      *  There are two images defined to allow for morphing on use, in the future
     543             :      *  For example storing a multispectral image and opening it as RGB
     544             :      *
     545             :      *  Support for this feature is not yet implemented.
     546             :      *
     547             :      */
     548             : 
     549             :     // What the image is on disk
     550             :     ILImage full;
     551             :     // How we use it currently
     552             :     ILImage current;
     553             :     // The third dimension slice in use
     554             :     int zslice;
     555             : 
     556             :     // MRF file name
     557             :     CPLString fname;
     558             : 
     559             :     // The source to be cached in this MRF
     560             :     CPLString source;
     561             :     GIntBig idxSize;  // The size of each version index, or the size of the
     562             :                       // cloned index
     563             : 
     564             :     int clonedSource;  // Is it a cloned source
     565             :     int nocopy;        // Set when initializing a caching MRF
     566             :     int bypass_cache;  // Do we alter disk cache
     567             :     int mp_safe;       // Not thread safe, only multiple writers
     568             :     int hasVersions;   // Does it support versions
     569             :     int verCount;      // The last version
     570             :     int bCrystalized;  // Unset only during the create process
     571             :     int spacing;       // How many spare bytes before each tile data
     572             :     int no_errors;     // Ignore read errors
     573             :     int missing;       // set if no_errors is set and data is missing
     574             : 
     575             :     // Freeform sticky dataset options, as a list of key-value pairs
     576             :     CPLStringList optlist;
     577             : 
     578             :     // If caching data, the parent dataset
     579             :     GDALDataset *poSrcDS;
     580             : 
     581             :     // Level picked, or -1 for native
     582             :     int level;
     583             : 
     584             :     // Child dataset, if picking a specific level
     585             :     MRFDataset *cds;
     586             :     // A small int actually due to GDAL limitations
     587             :     double scale;
     588             : 
     589             :     // A place to keep an uncompressed block, to keep from allocating it all the
     590             :     // time
     591             :     void *pbuffer;
     592             :     unsigned int pbsize;
     593             :     ILSize tile;  // ID of tile present in buffer
     594             :     GIntBig
     595             :         bdirty;  // Holds bits, to be used in pixel interleaved (up to 64 bands)
     596             : 
     597             :     // GeoTransform support
     598             :     double GeoTransform[6];
     599             :     int bGeoTransformValid;
     600             : 
     601             :     // CRS
     602             :     OGRSpatialReference m_oSRS{};
     603             : 
     604             :     // Photometric interpretation
     605             :     CPLString photometric;
     606             : 
     607             :     GDALColorTable *poColorTable;
     608             :     int Quality;
     609             : 
     610             :     VF dfp;  // Data file handle
     611             :     VF ifp;  // Index file handle
     612             : 
     613             :     // statistical values
     614             :     std::vector<double> vNoData, vMin, vMax;
     615             :     // Sticky context for zstd compress and decompress
     616             :     void *pzscctx, *pzsdctx;
     617             : #if defined(ZSTD_SUPPORT)
     618          11 :     ZSTD_CCtx *getzsc()
     619             :     {
     620          11 :         if (!pzscctx)
     621           9 :             pzscctx = ZSTD_createCCtx();
     622          11 :         return static_cast<ZSTD_CCtx *>(pzscctx);
     623             :     }
     624             : 
     625           9 :     ZSTD_DCtx *getzsd()
     626             :     {
     627           9 :         if (!pzsdctx)
     628           9 :             pzsdctx = ZSTD_createDCtx();
     629           9 :         return static_cast<ZSTD_DCtx *>(pzsdctx);
     630             :     }
     631             : #endif
     632             :     // Time duration spend for decompression and compression
     633             :     std::chrono::nanoseconds read_timer, write_timer;
     634             : };
     635             : 
     636             : class MRFRasterBand CPL_NON_FINAL : public GDALPamRasterBand
     637             : {
     638             :     friend class MRFDataset;
     639             : 
     640             :   public:
     641             :     MRFRasterBand(MRFDataset *, const ILImage &, int, int);
     642             :     virtual ~MRFRasterBand();
     643             :     virtual CPLErr IReadBlock(int xblk, int yblk, void *buffer) override;
     644             :     virtual CPLErr IWriteBlock(int xblk, int yblk, void *buffer) override;
     645             : 
     646             :     // Check that the respective block has data, without reading it
     647             :     virtual bool TestBlock(int xblk, int yblk);
     648             : 
     649          11 :     virtual GDALColorTable *GetColorTable() override
     650             :     {
     651          11 :         return poMRFDS->poColorTable;
     652             :     }
     653             : 
     654         405 :     CPLErr SetColorInterpretation(GDALColorInterp ci) override
     655             :     {
     656         405 :         img.ci = ci;
     657         405 :         return CE_None;
     658             :     }
     659             : 
     660         136 :     virtual GDALColorInterp GetColorInterpretation() override
     661             :     {
     662         136 :         return img.ci;
     663             :     }
     664             : 
     665             :     // Get works within MRF or with PAM
     666             :     virtual double GetNoDataValue(int *) override;
     667             :     virtual CPLErr SetNoDataValue(double) override;
     668             : 
     669             :     // These get set with SetStatistics
     670             :     virtual double GetMinimum(int *) override;
     671             :     virtual double GetMaximum(int *) override;
     672             : 
     673             :     // MRF specific, fetch is from a remote source
     674             :     CPLErr FetchBlock(int xblk, int yblk, void *buffer = nullptr);
     675             :     // Fetch a block from a cloned MRF
     676             :     CPLErr FetchClonedBlock(int xblk, int yblk, void *buffer = nullptr);
     677             : 
     678             :     // Block not stored on disk
     679             :     CPLErr FillBlock(void *buffer);
     680             : 
     681             :     // Same, for interleaved bands, current band goes in buffer
     682             :     CPLErr FillBlock(int xblk, int yblk, void *buffer);
     683             : 
     684             :     // de-interlace a buffer in pixel blocks
     685             :     CPLErr ReadInterleavedBlock(int xblk, int yblk, void *buffer);
     686             : 
     687             :     const char *GetOptionValue(const char *opt, const char *def) const;
     688             : 
     689         477 :     void SetAccess(GDALAccess eA)
     690             :     {
     691         477 :         eAccess = eA;
     692         477 :     }
     693             : 
     694          21 :     void SetDeflate(int v)
     695             :     {
     696          21 :         dodeflate = (v != 0);
     697          21 :     }
     698             : 
     699          39 :     void SetZstd(int v)
     700             :     {
     701          39 :         dozstd = (v != 0);
     702          39 :     }
     703             : 
     704             :   protected:
     705             :     // Pointer to the GDALMRFDataset
     706             :     MRFDataset *poMRFDS;
     707             :     // Deflate page requested, named to avoid conflict with libz deflate()
     708             :     int dodeflate;
     709             :     int deflate_flags;
     710             :     int dozstd;
     711             :     int zstd_level;
     712             :     // Level count of this band
     713             :     GInt32 m_l;
     714             :     // The info about the current image, to enable R-sets
     715             :     ILImage img;
     716             :     std::vector<MRFRasterBand *> overviews;
     717             : 
     718             :     VSILFILE *IdxFP()
     719             :     {
     720             :         return poMRFDS->IdxFP();
     721             :     }
     722             : 
     723           4 :     GDALRWFlag IdxMode()
     724             :     {
     725           4 :         return poMRFDS->IdxMode();
     726             :     }
     727             : 
     728        1871 :     VSILFILE *DataFP()
     729             :     {
     730        1871 :         return poMRFDS->DataFP();
     731             :     }
     732             : 
     733           1 :     GDALRWFlag DataMode()
     734             :     {
     735           1 :         return poMRFDS->DataMode();
     736             :     }
     737             : 
     738             :     // How many bytes are in a band block (not a page, a single band block)
     739             :     // Easiest is to calculate it from the pageSizeBytes
     740         159 :     GUInt32 blockSizeBytes()
     741             :     {
     742         159 :         return poMRFDS->current.pageSizeBytes / poMRFDS->current.pagesize.c;
     743             :     }
     744             : 
     745        2894 :     const CPLStringList &GetOptlist() const
     746             :     {
     747        2894 :         return poMRFDS->optlist;
     748             :     }
     749             : 
     750             :     // Compression and decompression functions.  To be overwritten by specific
     751             :     // implementations
     752             :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) = 0;
     753             :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) = 0;
     754             : 
     755             :     // Read the index record itself, can be overwritten
     756             :     //    virtual CPLErr ReadTileIdx(const ILSize &, ILIdx &, GIntBig bias = 0);
     757             : 
     758          61 :     static GIntBig bandbit(int b)
     759             :     {
     760          61 :         return ((GIntBig)1) << b;
     761             :     }
     762             : 
     763          11 :     GIntBig bandbit()
     764             :     {
     765          11 :         return bandbit(nBand - 1);
     766             :     }
     767             : 
     768          22 :     GIntBig AllBandMask()
     769             :     {
     770          22 :         return bandbit(poMRFDS->nBands) - 1;
     771             :     }
     772             : 
     773             :     // Overview Support
     774             :     // Inherited from GDALRasterBand
     775             :     // These are called only in the base level RasterBand
     776             :     virtual int GetOverviewCount() override;
     777             :     virtual GDALRasterBand *GetOverview(int n) override;
     778             : 
     779          72 :     void AddOverview(MRFRasterBand *b)
     780             :     {
     781          72 :         overviews.push_back(b);
     782          72 :     }
     783             : };
     784             : 
     785             : /**
     786             :  * Each type of compression needs to define at least two methods, a compress and
     787             :  * a decompress, which take as arguments a dest and a source buffer, plus an
     788             :  * image structure that holds the information about the compression type.
     789             :  * Filtering is needed, probably in the form of pack and unpack functions
     790             :  *
     791             :  */
     792             : 
     793             : class PNG_Codec
     794             : {
     795             :   public:
     796         155 :     explicit PNG_Codec(const ILImage &image)
     797         155 :         : img(image), PNGColors(nullptr), PNGAlpha(nullptr), PalSize(0),
     798         155 :           TransSize(0), deflate_flags(0)
     799             :     {
     800         155 :     }
     801             : 
     802         155 :     virtual ~PNG_Codec()
     803         155 :     {
     804         155 :         CPLFree(PNGColors);
     805         155 :         CPLFree(PNGAlpha);
     806         155 :     }
     807             : 
     808             :     CPLErr CompressPNG(buf_mgr &dst, const buf_mgr &src);
     809             :     static CPLErr DecompressPNG(buf_mgr &dst, buf_mgr &src);
     810             : 
     811             :     const ILImage img;
     812             : 
     813             :     void *PNGColors;
     814             :     void *PNGAlpha;
     815             :     int PalSize, TransSize, deflate_flags;
     816             : 
     817             :   private:
     818             :     // not implemented. but suppress MSVC warning about 'assignment operator
     819             :     // could not be generated'
     820             :     PNG_Codec &operator=(const PNG_Codec &src);
     821             : };
     822             : 
     823             : class PNG_Band final : public MRFRasterBand
     824             : {
     825             :     friend class MRFDataset;
     826             : 
     827             :   public:
     828             :     PNG_Band(MRFDataset *pDS, const ILImage &image, int b, int level);
     829             : 
     830             :   protected:
     831             :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) override;
     832             :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) override;
     833             : 
     834             :     PNG_Codec codec;
     835             : };
     836             : 
     837             : /*
     838             :  * The JPEG Codec can be used outside of the JPEG_Band
     839             :  */
     840             : 
     841             : class JPEG_Codec
     842             : {
     843             :   public:
     844          49 :     explicit JPEG_Codec(const ILImage &image)
     845          49 :         : img(image), sameres(FALSE), rgb(FALSE), optimize(false), JFIF(false)
     846             :     {
     847          49 :     }
     848             : 
     849             :     CPLErr CompressJPEG(buf_mgr &dst, buf_mgr &src);
     850             :     CPLErr DecompressJPEG(buf_mgr &dst, const buf_mgr &src);
     851             : 
     852             :     // Returns true for both JPEG and JPEG-XL (brunsli)
     853             :     static bool IsJPEG(const buf_mgr &src);
     854             : 
     855             : #if defined(JPEG12_SUPPORTED)  // Internal only
     856             :     CPLErr CompressJPEG12(buf_mgr &dst, buf_mgr &src);
     857             :     CPLErr DecompressJPEG12(buf_mgr &dst, const buf_mgr &src);
     858             : #endif
     859             : 
     860             :     const ILImage img;
     861             : 
     862             :     // JPEG specific flags
     863             :     bool sameres;   // No color space subsample
     864             :     bool rgb;       // No conversion to YCbCr
     865             :     bool optimize;  // Optimize Huffman tables
     866             :     bool JFIF;      // Write JFIF only
     867             : 
     868             :   private:
     869             :     // not implemented. but suppress MSVC warning about 'assignment operator
     870             :     // could not be generated'
     871             :     JPEG_Codec &operator=(const JPEG_Codec &src);
     872             : };
     873             : 
     874             : class JPEG_Band final : public MRFRasterBand
     875             : {
     876             :     friend class MRFDataset;
     877             : 
     878             :   public:
     879             :     JPEG_Band(MRFDataset *pDS, const ILImage &image, int b, int level);
     880             : 
     881          98 :     virtual ~JPEG_Band()
     882          49 :     {
     883          98 :     }
     884             : 
     885             :   protected:
     886             :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) override;
     887             :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) override;
     888             : 
     889             :     JPEG_Codec codec;
     890             : };
     891             : 
     892             : // A 2 or 4 band, with JPEG and/or PNG page encoding, optimized for size
     893             : class JPNG_Band final : public MRFRasterBand
     894             : {
     895             :     friend class MRFDataset;
     896             : 
     897             :   public:
     898             :     JPNG_Band(MRFDataset *pDS, const ILImage &image, int b, int level);
     899             :     virtual ~JPNG_Band();
     900             : 
     901             :   protected:
     902             :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) override;
     903             :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) override;
     904             : 
     905             :     CPLErr CompressJPNG(buf_mgr &dst, buf_mgr &src);
     906             :     CPLErr DecompressJPNG(buf_mgr &dst, buf_mgr &src);
     907             :     bool rgb, sameres, optimize, JFIF;
     908             : };
     909             : 
     910             : class Raw_Band final : public MRFRasterBand
     911             : {
     912             :     friend class MRFDataset;
     913             : 
     914             :   public:
     915         197 :     Raw_Band(MRFDataset *pDS, const ILImage &image, int b, int level)
     916         197 :         : MRFRasterBand(pDS, image, b, int(level))
     917             :     {
     918         197 :     }
     919             : 
     920         394 :     virtual ~Raw_Band()
     921         197 :     {
     922         394 :     }
     923             : 
     924             :   protected:
     925         426 :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) override
     926             :     {
     927         426 :         if (src.size > dst.size)
     928           0 :             return CE_Failure;
     929         426 :         memcpy(dst.buffer, src.buffer, src.size);
     930         426 :         dst.size = src.size;
     931         426 :         return CE_None;
     932             :     }
     933             : 
     934         212 :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) override
     935             :     {
     936         212 :         return Decompress(dst, src);
     937             :     }
     938             : };
     939             : 
     940             : class TIF_Band final : public MRFRasterBand
     941             : {
     942             :     friend class MRFDataset;
     943             : 
     944             :   public:
     945             :     TIF_Band(MRFDataset *pDS, const ILImage &image, int b, int level);
     946             :     virtual ~TIF_Band();
     947             : 
     948             :   protected:
     949             :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) override;
     950             :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) override;
     951             : 
     952             :     // Create options for TIF pages
     953             :     char **papszOptions;
     954             : };
     955             : 
     956             : #if defined(LERC)
     957             : class LERC_Band final : public MRFRasterBand
     958             : {
     959             :     friend class MRFDataset;
     960             : 
     961             :   public:
     962             :     LERC_Band(MRFDataset *pDS, const ILImage &image, int b, int level);
     963             :     virtual ~LERC_Band();
     964             : 
     965             :   protected:
     966             :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) override;
     967             :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) override;
     968             :     double precision;
     969             :     // L1 or L2
     970             :     int version;
     971             :     // L2 version
     972             :     int l2ver;
     973             :     // Build a MRF header for a single LERC tile
     974             :     static CPLXMLNode *GetMRFConfig(GDALOpenInfo *poOpenInfo);
     975             : 
     976             :   private:
     977        1079 :     static bool IsLerc1(const char *s)
     978             :     {
     979             :         static const char L1sig[] = "CntZImage ";
     980        1079 :         return !strncmp(s, L1sig, sizeof(L1sig) - 1);
     981             :     }
     982             : 
     983        1614 :     static bool IsLerc2(const char *s)
     984             :     {
     985             :         static const char L2sig[] = "Lerc2 ";
     986        1614 :         return !strncmp(s, L2sig, sizeof(L2sig) - 1);
     987             :     }
     988             : };
     989             : #endif
     990             : 
     991             : #if defined(QB3_SUPPORT)
     992             : class QB3_Band final : public MRFRasterBand
     993             : {
     994             :     friend class MRFDataset;
     995             : 
     996             :   public:
     997             :     QB3_Band(MRFDataset *pDS, const ILImage &image, int b, int level);
     998             : 
     999          84 :     virtual ~QB3_Band()
    1000          42 :     {
    1001          84 :     }
    1002             : 
    1003             :   protected:
    1004             :     virtual CPLErr Decompress(buf_mgr &dst, buf_mgr &src) override;
    1005             :     virtual CPLErr Compress(buf_mgr &dst, buf_mgr &src) override;
    1006             : };
    1007             : #endif
    1008             : 
    1009             : /*\brief band for level mrf
    1010             :  *
    1011             :  * Stand alone definition of a derived band, used in access to a specific level
    1012             :  * in an MRF
    1013             :  *
    1014             :  */
    1015             : class MRFLRasterBand final : public GDALPamRasterBand
    1016             : {
    1017             :   public:
    1018           1 :     explicit MRFLRasterBand(MRFRasterBand *b)
    1019           1 :     {
    1020           1 :         pBand = b;
    1021           1 :         eDataType = b->GetRasterDataType();
    1022           1 :         b->GetBlockSize(&nBlockXSize, &nBlockYSize);
    1023           1 :         eAccess = b->GetAccess();
    1024           1 :         nRasterXSize = b->GetXSize();
    1025           1 :         nRasterYSize = b->GetYSize();
    1026           1 :     }
    1027             : 
    1028           1 :     virtual CPLErr IReadBlock(int xblk, int yblk, void *buffer) override
    1029             :     {
    1030           1 :         return pBand->IReadBlock(xblk, yblk, buffer);
    1031             :     }
    1032             : 
    1033           0 :     virtual CPLErr IWriteBlock(int xblk, int yblk, void *buffer) override
    1034             :     {
    1035           0 :         return pBand->IWriteBlock(xblk, yblk, buffer);
    1036             :     }
    1037             : 
    1038           0 :     virtual GDALColorTable *GetColorTable() override
    1039             :     {
    1040           0 :         return pBand->GetColorTable();
    1041             :     }
    1042             : 
    1043           0 :     virtual GDALColorInterp GetColorInterpretation() override
    1044             :     {
    1045           0 :         return pBand->GetColorInterpretation();
    1046             :     }
    1047             : 
    1048           0 :     virtual double GetNoDataValue(int *pbSuccess) override
    1049             :     {
    1050           0 :         return pBand->GetNoDataValue(pbSuccess);
    1051             :     }
    1052             : 
    1053           0 :     virtual double GetMinimum(int *b) override
    1054             :     {
    1055           0 :         return pBand->GetMinimum(b);
    1056             :     }
    1057             : 
    1058           0 :     virtual double GetMaximum(int *b) override
    1059             :     {
    1060           0 :         return pBand->GetMaximum(b);
    1061             :     }
    1062             : 
    1063             :   protected:
    1064           0 :     virtual int GetOverviewCount() override
    1065             :     {
    1066           0 :         return 0;
    1067             :     }
    1068             : 
    1069           0 :     virtual GDALRasterBand *GetOverview(int) override
    1070             :     {
    1071           0 :         return nullptr;
    1072             :     }
    1073             : 
    1074             :     MRFRasterBand *pBand;
    1075             : };
    1076             : 
    1077             : NAMESPACE_MRF_END
    1078             : 
    1079             : #endif  // GDAL_FRMTS_MRF_MARFA_H_INCLUDED

Generated by: LCOV version 1.14