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 : CPLErr Close(GDALProgressFunc, void *) override = 0;
44 :
45 : public:
46 : RawDataset();
47 : ~RawDataset() override = 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 : bool bFlushCacheAtClosingHasRun = false;
115 : bool bTruncatedFileAllowed = false;
116 :
117 : GDALColorTable *poCT{};
118 : GDALColorInterp eInterp = GCI_Undefined;
119 :
120 : char **papszCategoryNames{};
121 :
122 : int bOwnsFP{};
123 :
124 : int Seek(vsi_l_offset, int);
125 : size_t Read(void *, size_t, size_t);
126 : size_t Write(void *, size_t, size_t);
127 :
128 : CPLErr AccessBlock(vsi_l_offset nBlockOff, size_t nBlockSize, void *pData,
129 : size_t nValues);
130 : int IsSignificantNumberOfLinesLoaded(int nLineOff, int nLines);
131 : void Initialize();
132 :
133 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
134 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
135 : GDALRasterIOExtraArg *psExtraArg) override;
136 :
137 : int CanUseDirectIO(int nXOff, int nYOff, int nXSize, int nYSize,
138 : GDALDataType eBufType, GDALRasterIOExtraArg *psExtraArg);
139 :
140 : public:
141 : enum class OwnFP
142 : {
143 : NO,
144 : YES
145 : };
146 :
147 : // IsValid() should be called afterwards
148 : RawRasterBand(GDALDataset *poDS, int nBand, VSILFILE *fpRaw,
149 : vsi_l_offset nImgOffset, int nPixelOffset, int nLineOffset,
150 : GDALDataType eDataType, int bNativeOrder, OwnFP bOwnsFP);
151 :
152 : // IsValid() should be called afterwards
153 : RawRasterBand(GDALDataset *poDS, int nBand, VSILFILE *fpRaw,
154 : vsi_l_offset nImgOffset, int nPixelOffset, int nLineOffset,
155 : GDALDataType eDataType, ByteOrder eByteOrder, OwnFP bOwnsFP);
156 :
157 : // IsValid() should be called afterwards
158 : RawRasterBand(VSILFILE *fpRaw, vsi_l_offset nImgOffset, int nPixelOffset,
159 : int nLineOffset, GDALDataType eDataType, int bNativeOrder,
160 : int nXSize, int nYSize, OwnFP bOwnsFP);
161 :
162 : // IsValid() should be called afterwards
163 : RawRasterBand(VSILFILE *fpRaw, vsi_l_offset nImgOffset, int nPixelOffset,
164 : int nLineOffset, GDALDataType eDataType, ByteOrder eByteOrder,
165 : int nXSize, int nYSize, OwnFP bOwnsFP);
166 :
167 : // Returns nullptr in case of error
168 : static std::unique_ptr<RawRasterBand>
169 : Create(GDALDataset *poDS, int nBand, VSILFILE *fpRaw,
170 : vsi_l_offset nImgOffset, int nPixelOffset, int nLineOffset,
171 : GDALDataType eDataType, ByteOrder eByteOrder, OwnFP bOwnsFP);
172 :
173 : // Returns nullptr in case of error
174 : static std::unique_ptr<RawRasterBand>
175 : Create(VSILFILE *fpRaw, vsi_l_offset nImgOffset, int nPixelOffset,
176 : int nLineOffset, GDALDataType eDataType, ByteOrder eByteOrder,
177 : int nXSize, int nYSize, OwnFP bOwnsFP);
178 :
179 : ~RawRasterBand() override;
180 :
181 3648 : bool IsValid() const
182 : {
183 3648 : return pLineStart != nullptr;
184 : }
185 :
186 : CPLErr IReadBlock(int, int, void *) override;
187 : CPLErr IWriteBlock(int, int, void *) override;
188 :
189 : GDALColorTable *GetColorTable() override;
190 : GDALColorInterp GetColorInterpretation() override;
191 : CPLErr SetColorTable(GDALColorTable *) override;
192 : CPLErr SetColorInterpretation(GDALColorInterp) override;
193 :
194 : char **GetCategoryNames() override;
195 : CPLErr SetCategoryNames(char **) override;
196 :
197 : CPLErr FlushCache(bool bAtClosing) override;
198 :
199 : CPLVirtualMem *GetVirtualMemAuto(GDALRWFlag eRWFlag, int *pnPixelSpace,
200 : GIntBig *pnLineSpace,
201 : char **papszOptions) override;
202 :
203 : CPLErr AccessLine(int iLine);
204 :
205 : void SetAccess(GDALAccess eAccess);
206 :
207 : // this is deprecated.
208 : void StoreNoDataValue(double);
209 :
210 : // Query methods for internal data.
211 7 : vsi_l_offset GetImgOffset() const
212 : {
213 7 : return nImgOffset;
214 : }
215 :
216 7 : int GetPixelOffset() const
217 : {
218 7 : return nPixelOffset;
219 : }
220 :
221 7 : int GetLineOffset() const
222 : {
223 7 : return nLineOffset;
224 : }
225 :
226 7 : ByteOrder GetByteOrder() const
227 : {
228 7 : return eByteOrder;
229 : }
230 :
231 27 : VSILFILE *GetFPL() const
232 : {
233 27 : return fpRawL;
234 : }
235 :
236 : int GetOwnsFP() const
237 : {
238 : return bOwnsFP;
239 : }
240 :
241 316 : void SetTruncatedFileAllowed(bool allowed)
242 : {
243 316 : bTruncatedFileAllowed = allowed;
244 316 : }
245 :
246 : private:
247 : CPL_DISALLOW_COPY_ASSIGN(RawRasterBand)
248 :
249 : bool NeedsByteOrderChange() const;
250 : void DoByteSwap(void *pBuffer, size_t nValues, int nByteSkip,
251 : bool bDiskToCPU) const;
252 : bool IsBIP() const;
253 : vsi_l_offset ComputeFileOffset(int iLine) const;
254 : bool FlushCurrentLine(bool bNeedUsableBufferAfter);
255 : CPLErr BIPWriteBlock(int nBlockYOff, int nCallingBand, const void *pImage);
256 : };
257 :
258 : #ifdef GDAL_COMPILATION
259 :
260 : bool CPL_DLL RAWDatasetCheckMemoryUsage(int nXSize, int nYSize, int nBands,
261 : int nDTSize, int nPixelOffset,
262 : int nLineOffset,
263 : vsi_l_offset nHeaderSize,
264 : vsi_l_offset nBandOffset, VSILFILE *fp);
265 :
266 : #endif
267 :
268 : #endif // GDAL_FRMTS_RAW_RAWDATASET_H_INCLUDED
|