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