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 1902 : struct JP2DatasetBase
58 : {
59 1657 : int GetNumThreads()
60 : {
61 1657 : if (nThreads < 0)
62 1211 : nThreads = GDALGetNumThreads(GDAL_DEFAULT_MAX_THREAD_COUNT,
63 : /* bDefaultAllCPUs = */ true);
64 1657 : 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 :
77 : int bIs420 = FALSE;
78 :
79 : int nParentXSize = 0;
80 : int nParentYSize = 0;
81 : int iLevel = 0;
82 : int nOverviewCount = 0;
83 :
84 : int bEnoughMemoryToLoadOtherBands = TRUE;
85 : int bRewrite = FALSE;
86 : int bHasGeoreferencingAtOpening = FALSE;
87 :
88 : int nThreads = -1;
89 : bool bUseSetDecodeArea = false;
90 : bool bSingleTiled = false;
91 : int m_nBlocksToLoad = 0;
92 : int m_nX0 = 0;
93 : int m_nY0 = 0;
94 : uint32_t m_nTileWidth = 0;
95 : uint32_t m_nTileHeight = 0;
96 :
97 : virtual ~JP2DatasetBase();
98 : };
99 :
100 : /************************************************************************/
101 : /* ==================================================================== */
102 : /* JP2OPJLikeDataset */
103 : /* ==================================================================== */
104 : /************************************************************************/
105 : template <typename CODEC, typename BASE> class JP2OPJLikeRasterBand;
106 :
107 : template <typename CODEC, typename BASE>
108 : class JP2OPJLikeDataset final : public GDALJP2AbstractDataset, public BASE
109 : {
110 : friend class JP2OPJLikeRasterBand<CODEC, BASE>;
111 : JP2OPJLikeDataset **papoOverviewDS = nullptr;
112 :
113 : JP2OPJLikeDataset(const JP2OPJLikeDataset &) = delete;
114 : JP2OPJLikeDataset &operator=(const JP2OPJLikeDataset &) = delete;
115 :
116 : protected:
117 : int CloseDependentDatasets() override;
118 : VSILFILE *GetFileHandle() override;
119 : CPLErr Close(GDALProgressFunc = nullptr, void * = nullptr) override;
120 :
121 : public:
122 : JP2OPJLikeDataset();
123 : ~JP2OPJLikeDataset() override;
124 :
125 : static int Identify(GDALOpenInfo *poOpenInfo);
126 : static GDALDataset *Open(GDALOpenInfo *);
127 : static GDALDataset *CreateCopy(const char *pszFilename,
128 : GDALDataset *poSrcDS, int bStrict,
129 : CSLConstList papszOptions,
130 : GDALProgressFunc pfnProgress,
131 : void *pProgressData);
132 :
133 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
134 :
135 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
136 :
137 : CPLErr SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
138 : const OGRSpatialReference *poSRS) override;
139 :
140 : CPLErr SetMetadata(CSLConstList papszMetadata,
141 : const char *pszDomain = "") override;
142 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
143 : const char *pszDomain = "") override;
144 :
145 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
146 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
147 : GDALDataType eBufType, int nBandCount,
148 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
149 : GSpacing nLineSpace, GSpacing nBandSpace,
150 : GDALRasterIOExtraArg *psExtraArg) override;
151 :
152 : GIntBig GetEstimatedRAMUsage() override;
153 :
154 : CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
155 : const int *panOverviewList, int nListBands,
156 : const int *panBandList, GDALProgressFunc pfnProgress,
157 : void *pProgressData,
158 : CSLConstList papszOptions) override;
159 :
160 : static bool WriteBox(VSILFILE *fp, GDALJP2Box *poBox);
161 : static bool WriteGDALMetadataBox(VSILFILE *fp, GDALDataset *poSrcDS,
162 : CSLConstList papszOptions);
163 : static bool WriteXMLBoxes(VSILFILE *fp, GDALDataset *poSrcDS);
164 : static bool WriteXMPBox(VSILFILE *fp, GDALDataset *poSrcDS);
165 : static bool WriteIPRBox(VSILFILE *fp, GDALDataset *poSrcDS);
166 :
167 : CPLErr ReadBlock(int nBand, VSILFILE *fp, int nBlockXOff, int nBlockYOff,
168 : void *pImage, int nBandCount, const int *panBandMap);
169 :
170 : int PreloadBlocks(JP2OPJLikeRasterBand<CODEC, BASE> *poBand, int nXOff,
171 : int nYOff, int nXSize, int nYSize, int nBandCount,
172 : const int *panBandMap);
173 :
174 : static void ReadBlockInThread(void *userdata);
175 : };
176 :
177 : /************************************************************************/
178 : /* ==================================================================== */
179 : /* JP2OPJLikeRasterBand */
180 : /* ==================================================================== */
181 : /************************************************************************/
182 :
183 : template <typename CODEC, typename BASE>
184 : class JP2OPJLikeRasterBand final : public GDALPamRasterBand
185 : {
186 : friend class JP2OPJLikeDataset<CODEC, BASE>;
187 : int bPromoteTo8Bit = false;
188 : GDALColorTable *poCT = nullptr;
189 :
190 : JP2OPJLikeRasterBand(const JP2OPJLikeRasterBand &) = delete;
191 : JP2OPJLikeRasterBand &operator=(const JP2OPJLikeRasterBand &) = delete;
192 :
193 : public:
194 : JP2OPJLikeRasterBand(JP2OPJLikeDataset<CODEC, BASE> *poDSIn, int nBandIn,
195 : GDALDataType eDataTypeIn, int nBits,
196 : int bPromoteTo8BitIn, int nBlockXSizeIn,
197 : int nBlockYSizeIn);
198 : ~JP2OPJLikeRasterBand() override;
199 :
200 : CPLErr IReadBlock(int, int, void *) override;
201 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
202 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
203 : GDALDataType eBufType, GSpacing nPixelSpace,
204 : GSpacing nLineSpace,
205 : GDALRasterIOExtraArg *psExtraArg) override;
206 :
207 : GDALColorInterp GetColorInterpretation() override;
208 : GDALColorTable *GetColorTable() override;
209 :
210 : int GetOverviewCount() override;
211 : GDALRasterBand *GetOverview(int iOvrLevel) override;
212 :
213 : int HasArbitraryOverviews() override;
214 :
215 : bool MayMultiBlockReadingBeMultiThreaded() const override;
216 : };
217 :
218 : #ifdef unused
219 : void GDALRegisterJP2();
220 : #endif
|