1 /******************************************************************************
2  * $Id: rawdataset.h 11d5fd1088af3c1c129433e30914108ebbc82750 2021-04-06 21:39:37 +0200 Even Rouault $
3  *
4  * Project:  Raw Translator
5  * Purpose:  Implementation of RawDataset class.  Intended to be subclassed
6  *           by other raw formats.
7  * Author:   Frank Warmerdam, warmerdam@pobox.com
8  *
9  ******************************************************************************
10  * Copyright (c) 1999, Frank Warmerdam
11  * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef GDAL_FRMTS_RAW_RAWDATASET_H_INCLUDED
33 #define GDAL_FRMTS_RAW_RAWDATASET_H_INCLUDED
34 
35 #include "gdal_pam.h"
36 
37 /************************************************************************/
38 /* ==================================================================== */
39 /*                              RawDataset                              */
40 /* ==================================================================== */
41 /************************************************************************/
42 
43 class RawRasterBand;
44 
45 /**
46  * \brief Abstract Base Class dedicated to define new raw dataset types.
47  */
48 class CPL_DLL RawDataset : public GDALPamDataset
49 {
50     friend class RawRasterBand;
51 
52   protected:
53     CPLErr IRasterIO( GDALRWFlag, int, int, int, int,
54                       void *, int, int, GDALDataType,
55                       int, int *,
56                       GSpacing nPixelSpace, GSpacing nLineSpace,
57                       GSpacing nBandSpace,
58                       GDALRasterIOExtraArg* psExtraArg ) override;
59   public:
60                  RawDataset();
61          virtual ~RawDataset() = 0;
62 
63     bool GetRawBinaryLayout(GDALDataset::RawBinaryLayout&) override;
64 
65   private:
66     CPL_DISALLOW_COPY_ASSIGN(RawDataset)
67 };
68 
69 /************************************************************************/
70 /* ==================================================================== */
71 /*                            RawRasterBand                             */
72 /* ==================================================================== */
73 /************************************************************************/
74 
75 /**
76  * \brief Abstract Base Class dedicated to define raw datasets.
77  * \note It is not defined an Abstract Base Class, but it is advised to
78  * consider it as such and not use it directly in client's code.
79  */
80 class CPL_DLL RawRasterBand : public GDALPamRasterBand
81 {
82 public:
83 
84     enum class ByteOrder
85     {
86         ORDER_LITTLE_ENDIAN,
87         ORDER_BIG_ENDIAN,
88         ORDER_VAX // only valid for Float32, Float64, CFloat32 and CFloat64
89     };
90 
91 protected:
92     friend class RawDataset;
93 
94     static constexpr int NO_SCANLINE_LOADED = -1;
95 
96     VSILFILE   *fpRawL{};
97 
98     vsi_l_offset nImgOffset{};
99     int         nPixelOffset{};
100     int         nLineOffset{};
101     int         nLineSize{};
102     ByteOrder   eByteOrder{};
103 
104     int         nLoadedScanline = NO_SCANLINE_LOADED;
105     void        *pLineBuffer{};
106     void        *pLineStart{};
107     bool        bNeedFileFlush = false;
108     bool        bLoadedScanlineDirty = false; // true when the buffer has
109                                               // modified content that needs to
110                                               // be pushed to disk
111 
112     GDALColorTable *poCT{};
113     GDALColorInterp eInterp = GCI_Undefined;
114 
115     char           **papszCategoryNames{};
116 
117     int         bOwnsFP{};
118 
119     int         Seek( vsi_l_offset, int );
120     size_t      Read( void *, size_t, size_t );
121     size_t      Write( void *, size_t, size_t );
122 
123     CPLErr      AccessBlock( vsi_l_offset nBlockOff, size_t nBlockSize,
124                              void * pData );
125     int         IsSignificantNumberOfLinesLoaded( int nLineOff, int nLines );
126     void        Initialize();
127 
128     CPLErr IRasterIO( GDALRWFlag, int, int, int, int,
129                       void *, int, int, GDALDataType,
130                       GSpacing nPixelSpace, GSpacing nLineSpace,
131                       GDALRasterIOExtraArg* psExtraArg ) override;
132 
133     int         CanUseDirectIO(int nXOff, int nYOff, int nXSize, int nYSize,
134                                GDALDataType eBufType,
135                                GDALRasterIOExtraArg* psExtraArg);
136 
137 public:
138 
139     enum class OwnFP
140     {
141         NO,
142         YES
143     };
144 
145                  RawRasterBand( GDALDataset *poDS, int nBand, VSILFILE* fpRaw,
146                                 vsi_l_offset nImgOffset, int nPixelOffset,
147                                 int nLineOffset,
148                                 GDALDataType eDataType, int bNativeOrder,
149                                 OwnFP bOwnsFP );
150 
151                  RawRasterBand( GDALDataset *poDS, int nBand, VSILFILE* fpRaw,
152                                 vsi_l_offset nImgOffset, int nPixelOffset,
153                                 int nLineOffset,
154                                 GDALDataType eDataType,
155                                 ByteOrder eByteOrder,
156                                 OwnFP bOwnsFP );
157 
158                  RawRasterBand( VSILFILE* fpRaw,
159                                 vsi_l_offset nImgOffset, int nPixelOffset,
160                                 int nLineOffset,
161                                 GDALDataType eDataType, int bNativeOrder,
162                                 int nXSize, int nYSize,
163                                 OwnFP bOwnsFP );
164 
165                  RawRasterBand( VSILFILE* fpRaw,
166                                 vsi_l_offset nImgOffset, int nPixelOffset,
167                                 int nLineOffset,
168                                 GDALDataType eDataType,
169                                 ByteOrder eByteOrder,
170                                 int nXSize, int nYSize,
171                                 OwnFP bOwnsFP );
172 
173     virtual ~RawRasterBand() /* = 0 */ ;
174 
175     CPLErr  IReadBlock( int, int, void * ) override;
176     CPLErr  IWriteBlock( int, int, void * ) override;
177 
178     GDALColorTable *GetColorTable() override;
179     GDALColorInterp GetColorInterpretation() override;
180     CPLErr SetColorTable( GDALColorTable * ) override;
181     CPLErr SetColorInterpretation( GDALColorInterp ) override;
182 
183     char **GetCategoryNames() override;
184     CPLErr SetCategoryNames( char ** ) override;
185 
186     CPLErr FlushCache() override;
187 
188     CPLVirtualMem *GetVirtualMemAuto( GDALRWFlag eRWFlag,
189                                       int *pnPixelSpace,
190                                       GIntBig *pnLineSpace,
191                                       char **papszOptions ) override;
192 
193     CPLErr          AccessLine( int iLine );
194 
195     void            SetAccess( GDALAccess eAccess );
196 
197     // this is deprecated.
198     void         StoreNoDataValue( double );
199 
200     // Query methods for internal data.
GetImgOffset()201     vsi_l_offset GetImgOffset() const { return nImgOffset; }
GetPixelOffset()202     int          GetPixelOffset() const { return nPixelOffset; }
GetLineOffset()203     int          GetLineOffset() const { return nLineOffset; }
GetByteOrder()204     ByteOrder    GetByteOrder() const { return eByteOrder; }
GetFPL()205     VSILFILE    *GetFPL() const { return fpRawL; }
GetOwnsFP()206     int          GetOwnsFP() const { return bOwnsFP; }
207 
208   private:
209     CPL_DISALLOW_COPY_ASSIGN(RawRasterBand)
210 
211     bool         NeedsByteOrderChange() const;
212     void         DoByteSwap(void* pBuffer, size_t nValues, int nByteSkip, bool bDiskToCPU) const;
213     bool         IsBIP() const;
214     vsi_l_offset ComputeFileOffset(int iLine) const;
215     bool         FlushCurrentLine(bool bNeedUsableBufferAfter);
216     CPLErr       BIPWriteBlock( int nBlockYOff, int nCallingBand, const void* pImage );
217 
218 };
219 
220 #ifdef GDAL_COMPILATION
221 
222 bool RAWDatasetCheckMemoryUsage(int nXSize, int nYSize, int nBands,
223                                 int nDTSize,
224                                 int nPixelOffset,
225                                 int nLineOffset,
226                                 vsi_l_offset nHeaderSize,
227                                 vsi_l_offset nBandOffset,
228                                 VSILFILE* fp);
229 
230 #endif
231 
232 #endif // GDAL_FRMTS_RAW_RAWDATASET_H_INCLUDED
233