| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: jpip read driver |
| 4 |
* Purpose: GDAL bindings for JPIP. |
| 5 |
* Author: Norman Barker, ITT VIS, [email protected] |
| 6 |
* |
| 7 |
****************************************************************************** |
| 8 |
* ITT Visual Information Systems grants you use of this code, under the |
| 9 |
following license: |
| 10 |
* |
| 11 |
* Copyright (c) 2000-2007, ITT Visual Information Solutions |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: MIT |
| 14 |
**/ |
| 15 |
|
| 16 |
#include "gdal_pam.h" |
| 17 |
#include "gdaljp2metadata.h" |
| 18 |
#include "cpl_conv.h" |
| 19 |
#include "cpl_string.h" |
| 20 |
#include "cpl_http.h" |
| 21 |
#include "cpl_vsi.h" |
| 22 |
#include "cpl_multiproc.h" |
| 23 |
|
| 24 |
#include "jpipkak_headers.h" |
| 25 |
|
| 26 |
#include <time.h> |
| 27 |
|
| 28 |
#if KDU_MAJOR_VERSION > 7 || (KDU_MAJOR_VERSION == 7 && KDU_MINOR_VERSION >= 5) |
| 29 |
using namespace kdu_core; |
| 30 |
using namespace kdu_supp; |
| 31 |
#endif |
| 32 |
|
| 33 |
static void JPIPWorkerFunc(void *); |
| 34 |
|
| 35 |
/************************************************************************/ |
| 36 |
/* ==================================================================== */ |
| 37 |
/* JPIPDataSegment */ |
| 38 |
/* ==================================================================== */ |
| 39 |
/************************************************************************/ |
| 40 |
class JPIPDataSegment |
| 41 |
{ |
| 42 |
private: |
| 43 |
long nId; |
| 44 |
long nAux; |
| 45 |
long nClassId; |
| 46 |
long nCodestream; |
| 47 |
long nOffset; |
| 48 |
long nLen; |
| 49 |
GByte *pabyData; |
| 50 |
int bIsFinal; |
| 51 |
int bIsEOR; |
| 52 |
|
| 53 |
public: |
| 54 |
long GetId() const |
| 55 |
{ |
| 56 |
return nId; |
| 57 |
} |
| 58 |
|
| 59 |
long GetAux() const |
| 60 |
{ |
| 61 |
return nAux; |
| 62 |
} |
| 63 |
|
| 64 |
long GetClassId() const |
| 65 |
{ |
| 66 |
return nClassId; |
| 67 |
} |
| 68 |
|
| 69 |
long GetCodestreamIdx() const |
| 70 |
{ |
| 71 |
return nCodestream; |
| 72 |
} |
| 73 |
|
| 74 |
long GetOffset() const |
| 75 |
{ |
| 76 |
return nOffset; |
| 77 |
} |
| 78 |
|
| 79 |
long GetLen() const |
| 80 |
{ |
| 81 |
return nLen; |
| 82 |
} |
| 83 |
|
| 84 |
GByte *GetData() |
| 85 |
{ |
| 86 |
return pabyData; |
| 87 |
} |
| 88 |
|
| 89 |
int IsFinal() const |
| 90 |
{ |
| 91 |
return bIsFinal; |
| 92 |
} |
| 93 |
|
| 94 |
int IsEOR() const |
| 95 |
{ |
| 96 |
return bIsEOR; |
| 97 |
} |
| 98 |
|
| 99 |
void SetId(long nIdIn) |
| 100 |
{ |
| 101 |
this->nId = nIdIn; |
| 102 |
} |
| 103 |
|
| 104 |
void SetAux(long nAuxIn) |
| 105 |
{ |
| 106 |
this->nAux = nAuxIn; |
| 107 |
} |
| 108 |
|
| 109 |
void SetClassId(long nClassIdIn) |
| 110 |
{ |
| 111 |
this->nClassId = nClassIdIn; |
| 112 |
} |
| 113 |
|
| 114 |
void SetCodestreamIdx(long nCodestreamIn) |
| 115 |
{ |
| 116 |
this->nCodestream = nCodestreamIn; |
| 117 |
} |
| 118 |
|
| 119 |
void SetOffset(long nOffsetIn) |
| 120 |
{ |
| 121 |
this->nOffset = nOffsetIn; |
| 122 |
} |
| 123 |
|
| 124 |
void SetLen(long nLenIn) |
| 125 |
{ |
| 126 |
this->nLen = nLenIn; |
| 127 |
} |
| 128 |
|
| 129 |
void SetData(GByte *pabyDataIn) |
| 130 |
{ |
| 131 |
this->pabyData = pabyDataIn; |
| 132 |
} |
| 133 |
|
| 134 |
void SetFinal(int bIsFinalIn) |
| 135 |
{ |
| 136 |
this->bIsFinal = bIsFinalIn; |
| 137 |
} |
| 138 |
|
| 139 |
void SetEOR(int bIsEORIn) |
| 140 |
{ |
| 141 |
this->bIsEOR = bIsEORIn; |
| 142 |
} |
| 143 |
|
| 144 |
JPIPDataSegment(); |
| 145 |
~JPIPDataSegment(); |
| 146 |
}; |
| 147 |
|
| 148 |
/************************************************************************/ |
| 149 |
/* ==================================================================== */ |
| 150 |
/* JPIPKAKDataset */ |
| 151 |
/* ==================================================================== */ |
| 152 |
/************************************************************************/ |
| 153 |
|
| 154 |
class JPIPKAKDataset final : public GDALPamDataset |
| 155 |
{ |
| 156 |
private: |
| 157 |
int bNeedReinitialize = FALSE; |
| 158 |
CPLString osRequestUrl; |
| 159 |
char *pszPath = nullptr; |
| 160 |
char *pszCid = nullptr; |
| 161 |
OGRSpatialReference m_oSRS{}; |
| 162 |
|
| 163 |
int nPos = 0; |
| 164 |
int nVBASLen = 0; |
| 165 |
int nVBASFirstByte = 0; |
| 166 |
int nClassId = 0; |
| 167 |
int nQualityLayers = 0; |
| 168 |
int nResLevels = 0; |
| 169 |
int nComps = 0; |
| 170 |
int nBitDepth = 0; |
| 171 |
int bYCC = 0; |
| 172 |
GDALDataType eDT = GDT_Unknown; |
| 173 |
|
| 174 |
int nCodestream = 0; |
| 175 |
long nDatabins = 0; |
| 176 |
|
| 177 |
GDALGeoTransform m_gt{}; |
| 178 |
|
| 179 |
int bWindowDone = FALSE; |
| 180 |
int bGeoTransformValid = FALSE; |
| 181 |
|
| 182 |
int nGCPCount = 0; |
| 183 |
GDAL_GCP *pasGCPList = nullptr; |
| 184 |
|
| 185 |
// kakadu |
| 186 |
kdu_codestream *poCodestream = nullptr; |
| 187 |
kdu_region_decompressor *poDecompressor = nullptr; |
| 188 |
kdu_cache *poCache = nullptr; |
| 189 |
|
| 190 |
long ReadVBAS(GByte *pabyData, int nLen); |
| 191 |
JPIPDataSegment *ReadSegment(GByte *pabyData, int nLen, int &bError); |
| 192 |
int Initialize(const char *url, int bReinitializing); |
| 193 |
void Deinitialize(); |
| 194 |
static int KakaduClassId(int nClassId); |
| 195 |
|
| 196 |
CPLMutex *pGlobalMutex = nullptr; |
| 197 |
|
| 198 |
// support two communication threads to the server, a main and an overview |
| 199 |
// thread |
| 200 |
volatile int bHighThreadRunning = 0; |
| 201 |
volatile int bLowThreadRunning = 0; |
| 202 |
volatile int bHighThreadFinished = 0; |
| 203 |
volatile int bLowThreadFinished = 0; |
| 204 |
|
| 205 |
// transmission counts |
| 206 |
volatile long nHighThreadByteCount = 0; |
| 207 |
volatile long nLowThreadByteCount = 0; |
| 208 |
|
| 209 |
static void KakaduInitialize(); |
| 210 |
|
| 211 |
public: |
| 212 |
JPIPKAKDataset(); |
| 213 |
~JPIPKAKDataset() override; |
| 214 |
|
| 215 |
// progressive methods |
| 216 |
virtual GDALAsyncReader * |
| 217 |
BeginAsyncReader(int xOff, int yOff, int xSize, int ySize, void *pBuf, |
| 218 |
int bufXSize, int bufYSize, GDALDataType bufType, |
| 219 |
int nBandCount, int *bandMap, int nPixelSpace, |
| 220 |
int nLineSpace, int nBandSpace, |
| 221 |
CSLConstList papszOptions) override; |
| 222 |
|
| 223 |
void EndAsyncReader(GDALAsyncReader *) override; |
| 224 |
|
| 225 |
int GetNQualityLayers() const |
| 226 |
{ |
| 227 |
return nQualityLayers; |
| 228 |
} |
| 229 |
|
| 230 |
int GetNResolutionLevels() const |
| 231 |
{ |
| 232 |
return nResLevels; |
| 233 |
} |
| 234 |
|
| 235 |
int GetNComponents() const |
| 236 |
{ |
| 237 |
return nComps; |
| 238 |
} |
| 239 |
|
| 240 |
int ReadFromInput(GByte *pabyData, int nLen, int &bError); |
| 241 |
|
| 242 |
int TestUseBlockIO(int nXOff, int nYOff, int nXSize, int nYSize, |
| 243 |
int nBufXSize, int nBufYSize, GDALDataType eDataType, |
| 244 |
int nBandCount, const int *panBandList) const; |
| 245 |
|
| 246 |
// gdaldataset methods |
| 247 |
CPLErr GetGeoTransform(GDALGeoTransform >) const override; |
| 248 |
const OGRSpatialReference *GetSpatialRef() const override; |
| 249 |
int GetGCPCount() override; |
| 250 |
const OGRSpatialReference *GetGCPSpatialRef() const override; |
| 251 |
const GDAL_GCP *GetGCPs() override; |
| 252 |
CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, |
| 253 |
int nYSize, void *pData, int nBufXSize, int nBufYSize, |
| 254 |
GDALDataType eBufType, int nBandCount, |
| 255 |
BANDMAP_TYPE panBandMap, GSpacing nPixelSpace, |
| 256 |
GSpacing nLineSpace, GSpacing nBandSpace, |
| 257 |
GDALRasterIOExtraArg *psExtraArg) override; |
| 258 |
|
| 259 |
static GDALDataset *Open(GDALOpenInfo *); |
| 260 |
static const GByte JPIP_EOR_IMAGE_DONE = 1; |
| 261 |
static const GByte JPIP_EOR_WINDOW_DONE = 2; |
| 262 |
static const GByte MAIN_HEADER_DATA_BIN_CLASS = 6; |
| 263 |
static const GByte META_DATA_BIN_CLASS = 8; |
| 264 |
static const GByte PRECINCT_DATA_BIN_CLASS = 0; |
| 265 |
static const GByte TILE_HEADER_DATA_BIN_CLASS = 2; |
| 266 |
static const GByte TILE_DATA_BIN_CLASS = 4; |
| 267 |
|
| 268 |
friend class JPIPKAKAsyncReader; |
| 269 |
friend class JPIPKAKRasterBand; |
| 270 |
friend void JPIPWorkerFunc(void *); |
| 271 |
}; |
| 272 |
|
| 273 |
/************************************************************************/ |
| 274 |
/* ==================================================================== */ |
| 275 |
/* JPIPKAKRasterBand */ |
| 276 |
/* ==================================================================== */ |
| 277 |
/************************************************************************/ |
| 278 |
|
| 279 |
class JPIPKAKRasterBand final : public GDALPamRasterBand |
| 280 |
{ |
| 281 |
friend class JPIPKAKDataset; |
| 282 |
|
| 283 |
JPIPKAKDataset *poBaseDS; |
| 284 |
|
| 285 |
int nDiscardLevels; |
| 286 |
|
| 287 |
kdu_dims band_dims; |
| 288 |
|
| 289 |
int nOverviewCount; |
| 290 |
JPIPKAKRasterBand **papoOverviewBand; |
| 291 |
|
| 292 |
kdu_codestream *oCodeStream; |
| 293 |
|
| 294 |
GDALColorTable oCT; |
| 295 |
GDALColorInterp eInterp; |
| 296 |
|
| 297 |
public: |
| 298 |
JPIPKAKRasterBand(int, int, kdu_codestream *, int, JPIPKAKDataset *); |
| 299 |
~JPIPKAKRasterBand() override; |
| 300 |
|
| 301 |
CPLErr IReadBlock(int, int, void *) override; |
| 302 |
CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int, |
| 303 |
GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace, |
| 304 |
GDALRasterIOExtraArg *psExtraArg) override; |
| 305 |
|
| 306 |
int GetOverviewCount() override; |
| 307 |
GDALRasterBand *GetOverview(int) override; |
| 308 |
}; |
| 309 |
|
| 310 |
/************************************************************************/ |
| 311 |
/* ==================================================================== */ |
| 312 |
/* JPIPKAKAsyncReader */ |
| 313 |
/* ==================================================================== */ |
| 314 |
/************************************************************************/ |
| 315 |
|
| 316 |
class JPIPKAKAsyncReader final : public GDALAsyncReader |
| 317 |
{ |
| 318 |
private: |
| 319 |
void *pAppBuf; |
| 320 |
int nAppPixelSpace, nAppLineSpace, nAppBandSpace; |
| 321 |
|
| 322 |
int nDataRead; |
| 323 |
int nLevel; |
| 324 |
int nQualityLayers; |
| 325 |
int bHighPriority; |
| 326 |
int bComplete; |
| 327 |
kdu_channel_mapping channels; |
| 328 |
kdu_coords exp_numerator, exp_denominator; |
| 329 |
|
| 330 |
kdu_dims rr_win; // user requested window expressed on reduced res level |
| 331 |
|
| 332 |
void Start(); |
| 333 |
void Stop(); |
| 334 |
|
| 335 |
public: |
| 336 |
JPIPKAKAsyncReader(); |
| 337 |
~JPIPKAKAsyncReader() override; |
| 338 |
|
| 339 |
virtual GDALAsyncStatusType |
| 340 |
GetNextUpdatedRegion(double timeout, int *pnxbufoff, int *pnybufoff, |
| 341 |
int *pnxbufsize, int *pnybufsize) override; |
| 342 |
|
| 343 |
void SetComplete(int bFinished) |
| 344 |
{ |
| 345 |
this->bComplete = bFinished; |
| 346 |
} |
| 347 |
|
| 348 |
friend class JPIPKAKDataset; |
| 349 |
|
| 350 |
CPLString osErrorMsg; |
| 351 |
}; |
| 352 |
|
| 353 |
/************************************************************************/ |
| 354 |
/* ==================================================================== */ |
| 355 |
/* JPIPRequest */ |
| 356 |
/* ==================================================================== */ |
| 357 |
/************************************************************************/ |
| 358 |
struct JPIPRequest |
| 359 |
{ |
| 360 |
int bPriority; |
| 361 |
CPLString osRequest; |
| 362 |
JPIPKAKAsyncReader *poARIO; |
| 363 |
}; |