Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: JPEG2000 driver based on OpenJPEG or Grok library
4 : * Purpose: JPEG2000 driver based on OpenJPEG or Grok library
5 : * Authors: Even Rouault, <even dot rouault at spatialys dot com>
6 : * Aaron Boxer, <boxerab at protonmail dot com>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2010-2014, Even Rouault <even dot rouault at spatialys dot com>
10 : * Copyright (c) 2015, European Union (European Environment Agency)
11 : * Copyright (c) 2023, Grok Image Compression Inc.
12 : *
13 : * SPDX-License-Identifier: MIT
14 : ****************************************************************************/
15 :
16 : #pragma once
17 :
18 : #include "cpl_atomic_ops.h"
19 : #include "cpl_multiproc.h"
20 : #include "cpl_string.h"
21 : #include "cpl_worker_thread_pool.h"
22 : #include "gdal_thread_pool.h"
23 : #include "gdaljp2abstractdataset.h"
24 : #include "gdaljp2metadata.h"
25 :
26 : typedef int JP2_COLOR_SPACE;
27 : typedef int JP2_PROG_ORDER;
28 :
29 : #define JP2_LRCP 0
30 : #define JP2_RLCP 1
31 : #define JP2_RPCL 2
32 : #define JP2_PCRL 3
33 : #define JP2_CPRL 4
34 :
35 : enum JP2_ENUM
36 : {
37 : JP2_CLRSPC_UNKNOWN,
38 : JP2_CLRSPC_SRGB,
39 : JP2_CLRSPC_GRAY,
40 : JP2_CLRSPC_SYCC,
41 : JP2_CODEC_J2K,
42 : JP2_CODEC_JP2
43 : };
44 :
45 : typedef struct
46 : {
47 : VSILFILE *fp_;
48 : vsi_l_offset nBaseOffset;
49 : } JP2File;
50 :
51 : /************************************************************************/
52 : /* ==================================================================== */
53 : /* JP2DatasetBase */
54 : /* ==================================================================== */
55 : /************************************************************************/
56 :
57 : struct JP2DatasetBase
58 : {
59 1659 : int GetNumThreads()
60 : {
61 1659 : if (nThreads < 0)
62 1213 : nThreads = GDALGetNumThreads(GDAL_DEFAULT_MAX_THREAD_COUNT,
63 : /* bDefaultAllCPUs = */ true);
64 1659 : return nThreads;
65 : }
66 :
67 : std::string m_osFilename{};
68 : VSILFILE *fp_ = nullptr; /* Large FILE API */
69 : vsi_l_offset nCodeStreamStart = 0;
70 : vsi_l_offset nCodeStreamLength = 0;
71 :
72 : int nRedIndex = 0;
73 : int nGreenIndex = 1;
74 : int nBlueIndex = 2;
75 : int nAlphaIndex = -1;
76 : bool bHas1BitAlpha = false;
77 :
78 : int bIs420 = FALSE;
79 :
80 : int nParentXSize = 0;
81 : int nParentYSize = 0;
82 : int iLevel = 0;
83 : int nOverviewCount = 0;
84 :
85 : int bEnoughMemoryToLoadOtherBands = TRUE;
86 : int bRewrite = FALSE;
87 : int bHasGeoreferencingAtOpening = FALSE;
88 :
89 : int nThreads = -1;
90 : bool bUseSetDecodeArea = false;
91 : bool bSingleTiled = false;
92 : int m_nBlocksToLoad = 0;
93 : int m_nX0 = 0;
94 : int m_nY0 = 0;
95 : uint32_t m_nTileWidth = 0;
96 : uint32_t m_nTileHeight = 0;
97 :
98 : protected:
99 1905 : ~JP2DatasetBase() = default;
100 : };
101 :
102 : /************************************************************************/
103 : /* ==================================================================== */
104 : /* JP2OPJLikeDataset */
105 : /* ==================================================================== */
106 : /************************************************************************/
107 : template <typename CODEC, typename BASE> class JP2OPJLikeRasterBand;
108 :
109 : template <typename CODEC, typename BASE>
110 : class JP2OPJLikeDataset final : public GDALJP2AbstractDataset, public BASE
111 : {
112 : friend class JP2OPJLikeRasterBand<CODEC, BASE>;
113 : JP2OPJLikeDataset **papoOverviewDS = nullptr;
114 :
115 : JP2OPJLikeDataset(const JP2OPJLikeDataset &) = delete;
116 : JP2OPJLikeDataset &operator=(const JP2OPJLikeDataset &) = delete;
117 :
118 : protected:
119 : int CloseDependentDatasets() override;
120 : VSILFILE *GetFileHandle() override;
121 : CPLErr Close(GDALProgressFunc = nullptr, void * = nullptr) override;
122 :
123 : public:
124 : JP2OPJLikeDataset();
125 : ~JP2OPJLikeDataset() override;
126 :
127 : static int Identify(GDALOpenInfo *poOpenInfo);
128 : static GDALDataset *Open(GDALOpenInfo *);
129 : static GDALDataset *CreateCopy(const char *pszFilename,
130 : GDALDataset *poSrcDS, int bStrict,
131 : CSLConstList papszOptions,
132 : GDALProgressFunc pfnProgress,
133 : void *pProgressData);
134 :
135 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
136 :
137 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
138 :
139 : CPLErr SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
140 : const OGRSpatialReference *poSRS) override;
141 :
142 : CPLErr SetMetadata(CSLConstList papszMetadata,
143 : const char *pszDomain = "") override;
144 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
145 : const char *pszDomain = "") override;
146 :
147 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
148 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
149 : GDALDataType eBufType, int nBandCount,
150 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
151 : GSpacing nLineSpace, GSpacing nBandSpace,
152 : GDALRasterIOExtraArg *psExtraArg) override;
153 :
154 : virtual CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
155 : int nBufXSize, int nBufYSize, GDALDataType eDT,
156 : int nBandCount, int *panBandList,
157 : CSLConstList papszOptions) override;
158 :
159 : GIntBig GetEstimatedRAMUsage() override;
160 :
161 : CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
162 : const int *panOverviewList, int nListBands,
163 : const int *panBandList, GDALProgressFunc pfnProgress,
164 : void *pProgressData,
165 : CSLConstList papszOptions) override;
166 :
167 : static bool WriteBox(VSILFILE *fp, GDALJP2Box *poBox);
168 : static bool WriteGDALMetadataBox(VSILFILE *fp, GDALDataset *poSrcDS,
169 : CSLConstList papszOptions);
170 : static bool WriteXMLBoxes(VSILFILE *fp, GDALDataset *poSrcDS);
171 : static bool WriteXMPBox(VSILFILE *fp, GDALDataset *poSrcDS);
172 : static bool WriteIPRBox(VSILFILE *fp, GDALDataset *poSrcDS);
173 :
174 : CPLErr ReadBlock(int nBand, VSILFILE *fp, int nBlockXOff, int nBlockYOff,
175 : void *pImage, int nBandCount, const int *panBandMap);
176 :
177 : int PreloadBlocks(JP2OPJLikeRasterBand<CODEC, BASE> *poBand, int nXOff,
178 : int nYOff, int nXSize, int nYSize, int nBandCount,
179 : const int *panBandMap);
180 :
181 : static void ReadBlockInThread(void *userdata);
182 :
183 : static vsi_l_offset JP2FindCodeStream(VSILFILE *fp, vsi_l_offset *pnLength);
184 : };
185 :
186 : /************************************************************************/
187 : /* ==================================================================== */
188 : /* JP2OPJLikeRasterBand */
189 : /* ==================================================================== */
190 : /************************************************************************/
191 :
192 : template <typename CODEC, typename BASE>
193 : class JP2OPJLikeRasterBand final : public GDALPamRasterBand
194 : {
195 : friend class JP2OPJLikeDataset<CODEC, BASE>;
196 : int bPromoteTo8Bit = false;
197 : GDALColorTable *poCT = nullptr;
198 :
199 : JP2OPJLikeRasterBand(const JP2OPJLikeRasterBand &) = delete;
200 : JP2OPJLikeRasterBand &operator=(const JP2OPJLikeRasterBand &) = delete;
201 :
202 : public:
203 : JP2OPJLikeRasterBand(JP2OPJLikeDataset<CODEC, BASE> *poDSIn, int nBandIn,
204 : GDALDataType eDataTypeIn, int nBits,
205 : int bPromoteTo8BitIn, int nBlockXSizeIn,
206 : int nBlockYSizeIn);
207 : ~JP2OPJLikeRasterBand() override;
208 :
209 : CPLErr IReadBlock(int, int, void *) override;
210 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
211 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
212 : GDALDataType eBufType, GSpacing nPixelSpace,
213 : GSpacing nLineSpace,
214 : GDALRasterIOExtraArg *psExtraArg) override;
215 :
216 : GDALColorInterp GetColorInterpretation() override;
217 : GDALColorTable *GetColorTable() override;
218 :
219 : int GetOverviewCount() override;
220 : GDALRasterBand *GetOverview(int iOvrLevel) override;
221 :
222 : int HasArbitraryOverviews() override;
223 :
224 : bool MayMultiBlockReadingBeMultiThreaded() const override;
225 : };
226 :
227 : #ifdef unused
228 : void GDALRegisterJP2();
229 : #endif
|