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 : * 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 : #pragma once
33 :
34 : #include "cpl_atomic_ops.h"
35 : #include "cpl_multiproc.h"
36 : #include "cpl_string.h"
37 : #include "cpl_worker_thread_pool.h"
38 : #include "gdaljp2abstractdataset.h"
39 : #include "gdaljp2metadata.h"
40 :
41 : typedef int JP2_COLOR_SPACE;
42 : typedef int JP2_PROG_ORDER;
43 :
44 : #define JP2_LRCP 0
45 : #define JP2_RLCP 1
46 : #define JP2_RPCL 2
47 : #define JP2_PCRL 3
48 : #define JP2_CPRL 4
49 :
50 : enum JP2_ENUM
51 : {
52 : JP2_CLRSPC_UNKNOWN,
53 : JP2_CLRSPC_SRGB,
54 : JP2_CLRSPC_GRAY,
55 : JP2_CLRSPC_SYCC,
56 : JP2_CODEC_J2K,
57 : JP2_CODEC_JP2
58 : };
59 :
60 : typedef struct
61 : {
62 : VSILFILE *fp_;
63 : vsi_l_offset nBaseOffset;
64 : } JP2File;
65 :
66 : /************************************************************************/
67 : /* ==================================================================== */
68 : /* JP2DatasetBase */
69 : /* ==================================================================== */
70 : /************************************************************************/
71 :
72 : struct JP2DatasetBase
73 : {
74 1600 : int GetNumThreads()
75 : {
76 1600 : if (nThreads >= 1)
77 417 : return nThreads;
78 :
79 : const char *pszThreads =
80 1183 : CPLGetConfigOption("GDAL_NUM_THREADS", "ALL_CPUS");
81 1183 : if (EQUAL(pszThreads, "ALL_CPUS"))
82 1183 : nThreads = CPLGetNumCPUs();
83 : else
84 0 : nThreads = atoi(pszThreads);
85 1183 : if (nThreads > 128)
86 0 : nThreads = 128;
87 1183 : if (nThreads <= 0)
88 0 : nThreads = 1;
89 1183 : return nThreads;
90 : }
91 :
92 : std::string m_osFilename;
93 : VSILFILE *fp_ = nullptr; /* Large FILE API */
94 : vsi_l_offset nCodeStreamStart = 0;
95 : vsi_l_offset nCodeStreamLength = 0;
96 :
97 : int nRedIndex = 0;
98 : int nGreenIndex = 1;
99 : int nBlueIndex = 2;
100 : int nAlphaIndex = -1;
101 :
102 : int bIs420 = FALSE;
103 :
104 : int nParentXSize = 0;
105 : int nParentYSize = 0;
106 : int iLevel = 0;
107 : int nOverviewCount = 0;
108 :
109 : int bEnoughMemoryToLoadOtherBands = TRUE;
110 : int bRewrite = FALSE;
111 : int bHasGeoreferencingAtOpening = FALSE;
112 :
113 : int nThreads = -1;
114 : bool bUseSetDecodeArea = false;
115 : bool bSingleTiled = false;
116 : int m_nBlocksToLoad = 0;
117 : int m_nX0 = 0;
118 : int m_nY0 = 0;
119 : uint32_t m_nTileWidth = 0;
120 : uint32_t m_nTileHeight = 0;
121 : };
122 :
123 : /************************************************************************/
124 : /* ==================================================================== */
125 : /* JP2OPJLikeDataset */
126 : /* ==================================================================== */
127 : /************************************************************************/
128 : template <typename CODEC, typename BASE> class JP2OPJLikeRasterBand;
129 :
130 : template <typename CODEC, typename BASE>
131 : class JP2OPJLikeDataset final : public GDALJP2AbstractDataset, public BASE
132 : {
133 : friend class JP2OPJLikeRasterBand<CODEC, BASE>;
134 : JP2OPJLikeDataset **papoOverviewDS = nullptr;
135 :
136 : protected:
137 : virtual int CloseDependentDatasets() override;
138 : virtual VSILFILE *GetFileHandle() override;
139 : CPLErr Close() override;
140 :
141 : public:
142 : JP2OPJLikeDataset();
143 : virtual ~JP2OPJLikeDataset();
144 :
145 : static int Identify(GDALOpenInfo *poOpenInfo);
146 : static GDALDataset *Open(GDALOpenInfo *);
147 : static GDALDataset *CreateCopy(const char *pszFilename,
148 : GDALDataset *poSrcDS, int bStrict,
149 : char **papszOptions,
150 : GDALProgressFunc pfnProgress,
151 : void *pProgressData);
152 :
153 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
154 :
155 : virtual CPLErr SetGeoTransform(double *) override;
156 :
157 : CPLErr SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
158 : const OGRSpatialReference *poSRS) override;
159 :
160 : virtual CPLErr SetMetadata(char **papszMetadata,
161 : const char *pszDomain = "") override;
162 : virtual CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
163 : const char *pszDomain = "") override;
164 :
165 : virtual CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
166 : int nXSize, int nYSize, void *pData, int nBufXSize,
167 : int nBufYSize, GDALDataType eBufType,
168 : int nBandCount, int *panBandMap,
169 : GSpacing nPixelSpace, GSpacing nLineSpace,
170 : GSpacing nBandSpace,
171 : GDALRasterIOExtraArg *psExtraArg) override;
172 :
173 : virtual GIntBig GetEstimatedRAMUsage() override;
174 :
175 : CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
176 : const int *panOverviewList, int nListBands,
177 : const int *panBandList, GDALProgressFunc pfnProgress,
178 : void *pProgressData,
179 : CSLConstList papszOptions) override;
180 :
181 : static bool WriteBox(VSILFILE *fp, GDALJP2Box *poBox);
182 : static bool WriteGDALMetadataBox(VSILFILE *fp, GDALDataset *poSrcDS,
183 : char **papszOptions);
184 : static bool WriteXMLBoxes(VSILFILE *fp, GDALDataset *poSrcDS);
185 : static bool WriteXMPBox(VSILFILE *fp, GDALDataset *poSrcDS);
186 : static bool WriteIPRBox(VSILFILE *fp, GDALDataset *poSrcDS);
187 :
188 : CPLErr ReadBlock(int nBand, VSILFILE *fp, int nBlockXOff, int nBlockYOff,
189 : void *pImage, int nBandCount, int *panBandMap);
190 :
191 : int PreloadBlocks(JP2OPJLikeRasterBand<CODEC, BASE> *poBand, int nXOff,
192 : int nYOff, int nXSize, int nYSize, int nBandCount,
193 : int *panBandMap);
194 :
195 : static void ReadBlockInThread(void *userdata);
196 : };
197 :
198 : /************************************************************************/
199 : /* ==================================================================== */
200 : /* JP2OPJLikeRasterBand */
201 : /* ==================================================================== */
202 : /************************************************************************/
203 :
204 : template <typename CODEC, typename BASE>
205 : class JP2OPJLikeRasterBand final : public GDALPamRasterBand
206 : {
207 : friend class JP2OPJLikeDataset<CODEC, BASE>;
208 : int bPromoteTo8Bit;
209 : GDALColorTable *poCT;
210 :
211 : public:
212 : JP2OPJLikeRasterBand(JP2OPJLikeDataset<CODEC, BASE> *poDSIn, int nBandIn,
213 : GDALDataType eDataTypeIn, int nBits,
214 : int bPromoteTo8BitIn, int nBlockXSizeIn,
215 : int nBlockYSizeIn);
216 : virtual ~JP2OPJLikeRasterBand();
217 :
218 : virtual CPLErr IReadBlock(int, int, void *) override;
219 : virtual CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
220 : int nXSize, int nYSize, void *pData, int nBufXSize,
221 : int nBufYSize, GDALDataType eBufType,
222 : GSpacing nPixelSpace, GSpacing nLineSpace,
223 : GDALRasterIOExtraArg *psExtraArg) override;
224 :
225 : virtual GDALColorInterp GetColorInterpretation() override;
226 : virtual GDALColorTable *GetColorTable() override;
227 :
228 : virtual int GetOverviewCount() override;
229 : virtual GDALRasterBand *GetOverview(int iOvrLevel) override;
230 :
231 : virtual int HasArbitraryOverviews() override;
232 : };
233 :
234 : #ifdef unused
235 : void GDALRegisterJP2();
236 : #endif
|