File: jpipkakdataset.h

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (360 lines) | stat: -rw-r--r-- 10,952 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/******************************************************************************
 * $Id$
 *
 * Project:  jpip read driver
 * Purpose:  GDAL bindings for JPIP.
 * Author:   Norman Barker, ITT VIS, norman.barker@gmail.com
 *
 ******************************************************************************
 * ITT Visual Information Systems grants you use of this code, under the
following license:
 *
 * Copyright (c) 2000-2007, ITT Visual Information Solutions
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
**/

#include "gdal_pam.h"
#include "gdaljp2metadata.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_http.h"
#include "cpl_vsi.h"
#include "cpl_multiproc.h"

#include "jpipkak_headers.h"

#include <time.h>

#if KDU_MAJOR_VERSION > 7 || (KDU_MAJOR_VERSION == 7 && KDU_MINOR_VERSION >= 5)
using namespace kdu_core;
using namespace kdu_supp;
#endif

static void JPIPWorkerFunc(void *);

/************************************************************************/
/* ==================================================================== */
/*                           JPIPDataSegment                            */
/* ==================================================================== */
/************************************************************************/
class JPIPDataSegment
{
  private:
    long nId;
    long nAux;
    long nClassId;
    long nCodestream;
    long nOffset;
    long nLen;
    GByte *pabyData;
    int bIsFinal;
    int bIsEOR;

  public:
    long GetId() const
    {
        return nId;
    }
    long GetAux() const
    {
        return nAux;
    }
    long GetClassId() const
    {
        return nClassId;
    }
    long GetCodestreamIdx() const
    {
        return nCodestream;
    }
    long GetOffset() const
    {
        return nOffset;
    }
    long GetLen() const
    {
        return nLen;
    }
    GByte *GetData()
    {
        return pabyData;
    }
    int IsFinal() const
    {
        return bIsFinal;
    }
    int IsEOR() const
    {
        return bIsEOR;
    }

    void SetId(long nIdIn)
    {
        this->nId = nIdIn;
    }
    void SetAux(long nAuxIn)
    {
        this->nAux = nAuxIn;
    }
    void SetClassId(long nClassIdIn)
    {
        this->nClassId = nClassIdIn;
    }
    void SetCodestreamIdx(long nCodestreamIn)
    {
        this->nCodestream = nCodestreamIn;
    }
    void SetOffset(long nOffsetIn)
    {
        this->nOffset = nOffsetIn;
    }
    void SetLen(long nLenIn)
    {
        this->nLen = nLenIn;
    }
    void SetData(GByte *pabyDataIn)
    {
        this->pabyData = pabyDataIn;
    }
    void SetFinal(int bIsFinalIn)
    {
        this->bIsFinal = bIsFinalIn;
    }
    void SetEOR(int bIsEORIn)
    {
        this->bIsEOR = bIsEORIn;
    }
    JPIPDataSegment();
    ~JPIPDataSegment();
};

/************************************************************************/
/* ==================================================================== */
/*                           JPIPKAKDataset                             */
/* ==================================================================== */
/************************************************************************/

class JPIPKAKDataset final : public GDALPamDataset
{
  private:
    int bNeedReinitialize = FALSE;
    CPLString osRequestUrl;
    char *pszPath = nullptr;
    char *pszCid = nullptr;
    OGRSpatialReference m_oSRS{};

    int nPos = 0;
    int nVBASLen = 0;
    int nVBASFirstByte = 0;
    int nClassId = 0;
    int nQualityLayers = 0;
    int nResLevels = 0;
    int nComps = 0;
    int nBitDepth = 0;
    int bYCC = 0;
    GDALDataType eDT = GDT_Unknown;

    int nCodestream = 0;
    long nDatabins = 0;

    double adfGeoTransform[6];

    int bWindowDone = FALSE;
    int bGeoTransformValid = FALSE;

    int nGCPCount = 0;
    GDAL_GCP *pasGCPList = nullptr;

    // kakadu
    kdu_codestream *poCodestream = nullptr;
    kdu_region_decompressor *poDecompressor = nullptr;
    kdu_cache *poCache = nullptr;

    long ReadVBAS(GByte *pabyData, int nLen);
    JPIPDataSegment *ReadSegment(GByte *pabyData, int nLen, int &bError);
    int Initialize(const char *url, int bReinitializing);
    void Deinitialize();
    static int KakaduClassId(int nClassId);

    CPLMutex *pGlobalMutex = nullptr;

    // support two communication threads to the server, a main and an overview
    // thread
    volatile int bHighThreadRunning = 0;
    volatile int bLowThreadRunning = 0;
    volatile int bHighThreadFinished = 0;
    volatile int bLowThreadFinished = 0;

    // transmission counts
    volatile long nHighThreadByteCount = 0;
    volatile long nLowThreadByteCount = 0;

    static void KakaduInitialize();

  public:
    JPIPKAKDataset();
    virtual ~JPIPKAKDataset();

    // progressive methods
    virtual GDALAsyncReader *
    BeginAsyncReader(int xOff, int yOff, int xSize, int ySize, void *pBuf,
                     int bufXSize, int bufYSize, GDALDataType bufType,
                     int nBandCount, int *bandMap, int nPixelSpace,
                     int nLineSpace, int nBandSpace,
                     char **papszOptions) override;

    virtual void EndAsyncReader(GDALAsyncReader *) override;
    int GetNQualityLayers() const
    {
        return nQualityLayers;
    }
    int GetNResolutionLevels() const
    {
        return nResLevels;
    }
    int GetNComponents() const
    {
        return nComps;
    }

    int ReadFromInput(GByte *pabyData, int nLen, int &bError);

    int TestUseBlockIO(int nXOff, int nYOff, int nXSize, int nYSize,
                       int nBufXSize, int nBufYSize, GDALDataType eDataType,
                       int nBandCount, const int *panBandList) const;

    // gdaldataset methods
    virtual CPLErr GetGeoTransform(double *) override;
    const OGRSpatialReference *GetSpatialRef() const override;
    virtual int GetGCPCount() override;
    const OGRSpatialReference *GetGCPSpatialRef() const override;
    virtual const GDAL_GCP *GetGCPs() override;
    virtual CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
                             int nXSize, int nYSize, void *pData, int nBufXSize,
                             int nBufYSize, GDALDataType eBufType,
                             int nBandCount, int *panBandMap,
                             GSpacing nPixelSpace, GSpacing nLineSpace,
                             GSpacing nBandSpace,
                             GDALRasterIOExtraArg *psExtraArg) override;

    static GDALDataset *Open(GDALOpenInfo *);
    static const GByte JPIP_EOR_IMAGE_DONE = 1;
    static const GByte JPIP_EOR_WINDOW_DONE = 2;
    static const GByte MAIN_HEADER_DATA_BIN_CLASS = 6;
    static const GByte META_DATA_BIN_CLASS = 8;
    static const GByte PRECINCT_DATA_BIN_CLASS = 0;
    static const GByte TILE_HEADER_DATA_BIN_CLASS = 2;
    static const GByte TILE_DATA_BIN_CLASS = 4;

    friend class JPIPKAKAsyncReader;
    friend class JPIPKAKRasterBand;
    friend void JPIPWorkerFunc(void *);
};

/************************************************************************/
/* ==================================================================== */
/*                            JPIPKAKRasterBand                          */
/* ==================================================================== */
/************************************************************************/

class JPIPKAKRasterBand final : public GDALPamRasterBand
{
    friend class JPIPKAKDataset;

    JPIPKAKDataset *poBaseDS;

    int nDiscardLevels;

    kdu_dims band_dims;

    int nOverviewCount;
    JPIPKAKRasterBand **papoOverviewBand;

    kdu_codestream *oCodeStream;

    GDALColorTable oCT;
    GDALColorInterp eInterp;

  public:
    JPIPKAKRasterBand(int, int, kdu_codestream *, int, JPIPKAKDataset *);
    virtual ~JPIPKAKRasterBand();

    virtual CPLErr IReadBlock(int, int, void *) override;
    virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
                             GDALDataType, GSpacing nPixelSpace,
                             GSpacing nLineSpace,
                             GDALRasterIOExtraArg *psExtraArg) override;

    virtual int GetOverviewCount() override;
    virtual GDALRasterBand *GetOverview(int) override;
};

/************************************************************************/
/* ==================================================================== */
/*                           JPIPKAKAsyncReader                         */
/* ==================================================================== */
/************************************************************************/

class JPIPKAKAsyncReader final : public GDALAsyncReader
{
  private:
    void *pAppBuf;
    int nAppPixelSpace, nAppLineSpace, nAppBandSpace;

    int nDataRead;
    int nLevel;
    int nQualityLayers;
    int bHighPriority;
    int bComplete;
    kdu_channel_mapping channels;
    kdu_coords exp_numerator, exp_denominator;

    kdu_dims rr_win;  // user requested window expressed on reduced res level

    void Start();
    void Stop();

  public:
    JPIPKAKAsyncReader();
    virtual ~JPIPKAKAsyncReader();

    virtual GDALAsyncStatusType
    GetNextUpdatedRegion(double timeout, int *pnxbufoff, int *pnybufoff,
                         int *pnxbufsize, int *pnybufsize) override;
    void SetComplete(int bFinished)
    {
        this->bComplete = bFinished;
    }

    friend class JPIPKAKDataset;

    CPLString osErrorMsg;
};

/************************************************************************/
/* ==================================================================== */
/*                           JPIPRequest                                */
/* ==================================================================== */
/************************************************************************/
struct JPIPRequest
{
    int bPriority;
    CPLString osRequest;
    JPIPKAKAsyncReader *poARIO;
};