Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: Raw Translator
4 : * Purpose: Implementation of RawDataset class. Intended to be subclassed
5 : * by other raw formats.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
10 : * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #ifndef GDAL_FRMTS_RAW_RAWDATASET_H_INCLUDED
16 : #define GDAL_FRMTS_RAW_RAWDATASET_H_INCLUDED
17 :
18 : #include "gdal_pam.h"
19 :
20 : #include <atomic>
21 : #include <utility>
22 :
23 : /************************************************************************/
24 : /* ==================================================================== */
25 : /* RawDataset */
26 : /* ==================================================================== */
27 : /************************************************************************/
28 :
29 : class RawRasterBand;
30 :
31 : /**
32 : * \brief Abstract Base Class dedicated to define new raw dataset types.
33 : */
34 : class CPL_DLL RawDataset : public GDALPamDataset
35 : {
36 : friend class RawRasterBand;
37 :
38 : protected:
39 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
40 : GDALDataType, int, BANDMAP_TYPE, GSpacing nPixelSpace,
41 : GSpacing nLineSpace, GSpacing nBandSpace,
42 : GDALRasterIOExtraArg *psExtraArg) override;
43 : virtual CPLErr Close() override = 0;
44 :
45 : public:
46 : RawDataset();
47 : virtual ~RawDataset() = 0;
48 :
49 : enum class Interleave
50 : {
51 : BSQ,
52 : BIL,
53 : BIP,
54 : };
55 :
56 : bool GetRawBinaryLayout(GDALDataset::RawBinaryLayout &) override;
57 : void ClearCachedConfigOption(void);
58 :
59 : private:
60 : CPL_DISALLOW_COPY_ASSIGN(RawDataset)
61 : protected:
62 : std::atomic<int> cachedCPLOneBigReadOption = {
63 : 0}; // [0-7] bits are "valid", [8-15] bits are "value"
64 : };
65 :
66 : /************************************************************************/
67 : /* ==================================================================== */
68 : /* RawRasterBand */
69 : /* ==================================================================== */
70 : /************************************************************************/
71 :
72 : /**
73 : * \brief Abstract Base Class dedicated to define raw datasets.
74 : * \note It is not defined an Abstract Base Class, but it is advised to
75 : * consider it as such and not use it directly in client's code.
76 : */
77 : class CPL_DLL RawRasterBand : public GDALPamRasterBand
78 : {
79 : public:
80 : enum class ByteOrder
81 : {
82 : ORDER_LITTLE_ENDIAN,
83 : ORDER_BIG_ENDIAN,
84 : ORDER_VAX // only valid for Float32, Float64, CFloat32 and CFloat64
85 : };
86 :
87 : #ifdef CPL_LSB
88 : static constexpr ByteOrder NATIVE_BYTE_ORDER =
89 : ByteOrder::ORDER_LITTLE_ENDIAN;
90 : #else
91 : static constexpr ByteOrder NATIVE_BYTE_ORDER = ByteOrder::ORDER_BIG_ENDIAN;
92 : #endif
93 :
94 : protected:
95 : friend class RawDataset;
96 :
97 : static constexpr int NO_SCANLINE_LOADED = -1;
98 :
99 : VSILFILE *fpRawL{};
100 :
101 : vsi_l_offset nImgOffset{};
102 : int nPixelOffset{};
103 : int nLineOffset{};
104 : int nLineSize{};
105 : ByteOrder eByteOrder{};
106 :
107 : int nLoadedScanline = NO_SCANLINE_LOADED;
108 : void *pLineBuffer{};
109 : void *pLineStart{};
110 : bool bNeedFileFlush = false;
111 : bool bLoadedScanlineDirty = false; // true when the buffer has
112 : // modified content that needs to
113 : // be pushed to disk
114 :
115 : GDALColorTable *poCT{};
116 : GDALColorInterp eInterp = GCI_Undefined;
117 :
118 : char **papszCategoryNames{};
119 :
120 : int bOwnsFP{};
121 :
122 : int Seek(vsi_l_offset, int);
123 : size_t Read(void *, size_t, size_t);
124 : size_t Write(void *, size_t, size_t);
125 :
126 : CPLErr AccessBlock(vsi_l_offset nBlockOff, size_t nBlockSize, void *pData,
127 : size_t nValues);
128 : int IsSignificantNumberOfLinesLoaded(int nLineOff, int nLines);
129 : void Initialize();
130 :
131 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
132 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
133 : GDALRasterIOExtraArg *psExtraArg) override;
134 :
135 : int CanUseDirectIO(int nXOff, int nYOff, int nXSize, int nYSize,
136 : GDALDataType eBufType, GDALRasterIOExtraArg *psExtraArg);
137 :
138 : public:
139 : enum class OwnFP
140 : {
141 : NO,
142 : YES
143 : };
144 :
145 : // IsValid() should be called afterwards
146 : RawRasterBand(GDALDataset *poDS, int nBand, VSILFILE *fpRaw,
147 : vsi_l_offset nImgOffset, int nPixelOffset, int nLineOffset,
148 : GDALDataType eDataType, int bNativeOrder, OwnFP bOwnsFP);
149 :
150 : // IsValid() should be called afterwards
151 : RawRasterBand(GDALDataset *poDS, int nBand, VSILFILE *fpRaw,
152 : vsi_l_offset nImgOffset, int nPixelOffset, int nLineOffset,
153 : GDALDataType eDataType, ByteOrder eByteOrder, OwnFP bOwnsFP);
154 :
155 : // IsValid() should be called afterwards
156 : RawRasterBand(VSILFILE *fpRaw, vsi_l_offset nImgOffset, int nPixelOffset,
157 : int nLineOffset, GDALDataType eDataType, int bNativeOrder,
158 : int nXSize, int nYSize, OwnFP bOwnsFP);
159 :
160 : // IsValid() should be called afterwards
161 : RawRasterBand(VSILFILE *fpRaw, vsi_l_offset nImgOffset, int nPixelOffset,
162 : int nLineOffset, GDALDataType eDataType, ByteOrder eByteOrder,
163 : int nXSize, int nYSize, OwnFP bOwnsFP);
164 :
165 : // Returns nullptr in case of error
166 : static std::unique_ptr<RawRasterBand>
167 : Create(GDALDataset *poDS, int nBand, VSILFILE *fpRaw,
168 : vsi_l_offset nImgOffset, int nPixelOffset, int nLineOffset,
169 : GDALDataType eDataType, ByteOrder eByteOrder, OwnFP bOwnsFP);
170 :
171 : // Returns nullptr in case of error
172 : static std::unique_ptr<RawRasterBand>
173 : Create(VSILFILE *fpRaw, vsi_l_offset nImgOffset, int nPixelOffset,
174 : int nLineOffset, GDALDataType eDataType, ByteOrder eByteOrder,
175 : int nXSize, int nYSize, OwnFP bOwnsFP);
176 :
177 : virtual ~RawRasterBand() /* = 0 */;
178 :
179 3609 : bool IsValid() const
180 : {
181 3609 : return pLineStart != nullptr;
182 : }
183 :
184 : CPLErr IReadBlock(int, int, void *) override;
185 : CPLErr IWriteBlock(int, int, void *) override;
186 :
187 : GDALColorTable *GetColorTable() override;
188 : GDALColorInterp GetColorInterpretation() override;
189 : CPLErr SetColorTable(GDALColorTable *) override;
190 : CPLErr SetColorInterpretation(GDALColorInterp) override;
191 :
192 : char **GetCategoryNames() override;
193 : CPLErr SetCategoryNames(char **) override;
194 :
195 : CPLErr FlushCache(bool bAtClosing) override;
196 :
197 : CPLVirtualMem *GetVirtualMemAuto(GDALRWFlag eRWFlag, int *pnPixelSpace,
198 : GIntBig *pnLineSpace,
199 : char **papszOptions) override;
200 :
201 : CPLErr AccessLine(int iLine);
202 :
203 : void SetAccess(GDALAccess eAccess);
204 :
205 : // this is deprecated.
206 : void StoreNoDataValue(double);
207 :
208 : // Query methods for internal data.
209 7 : vsi_l_offset GetImgOffset() const
210 : {
211 7 : return nImgOffset;
212 : }
213 :
214 7 : int GetPixelOffset() const
215 : {
216 7 : return nPixelOffset;
217 : }
218 :
219 7 : int GetLineOffset() const
220 : {
221 7 : return nLineOffset;
222 : }
223 :
224 7 : ByteOrder GetByteOrder() const
225 : {
226 7 : return eByteOrder;
227 : }
228 :
229 24 : VSILFILE *GetFPL() const
230 : {
231 24 : return fpRawL;
232 : }
233 :
234 : int GetOwnsFP() const
235 : {
236 : return bOwnsFP;
237 : }
238 :
239 : private:
240 : CPL_DISALLOW_COPY_ASSIGN(RawRasterBand)
241 :
242 : bool NeedsByteOrderChange() const;
243 : void DoByteSwap(void *pBuffer, size_t nValues, int nByteSkip,
244 : bool bDiskToCPU) const;
245 : bool IsBIP() const;
246 : vsi_l_offset ComputeFileOffset(int iLine) const;
247 : bool FlushCurrentLine(bool bNeedUsableBufferAfter);
248 : CPLErr BIPWriteBlock(int nBlockYOff, int nCallingBand, const void *pImage);
249 : };
250 :
251 : #ifdef GDAL_COMPILATION
252 :
253 : bool CPL_DLL RAWDatasetCheckMemoryUsage(int nXSize, int nYSize, int nBands,
254 : int nDTSize, int nPixelOffset,
255 : int nLineOffset,
256 : vsi_l_offset nHeaderSize,
257 : vsi_l_offset nBandOffset, VSILFILE *fp);
258 :
259 : #endif
260 :
261 : #endif // GDAL_FRMTS_RAW_RAWDATASET_H_INCLUDED
|