Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: Multi-resolution Seamless Image Database (MrSID)
4 : * Purpose: Read/write LizardTech's MrSID file format - Version 4+ SDK.
5 : * Author: Andrey Kiselev, dron@ak4719.spb.edu
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2003, Andrey Kiselev <dron@ak4719.spb.edu>
9 : * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #define NO_DELETE
15 :
16 : #include "cpl_string.h"
17 : #include "gdal_frmts.h"
18 : #include "gdaljp2abstractdataset.h"
19 : #include "gdaljp2metadata.h"
20 : #include "ogr_spatialref.h"
21 : #include <string>
22 :
23 : #include "mrsiddrivercore.h"
24 :
25 : #include <geo_normalize.h>
26 : #include <geovalues.h>
27 :
28 : CPL_C_START
29 : double GTIFAngleToDD(double dfAngle, int nUOMAngle);
30 : void CPL_DLL LibgeotiffOneTimeInit();
31 : CPL_C_END
32 :
33 : #include "mrsiddataset_headers_include.h"
34 :
35 : #ifdef MRSID_POST5
36 : #define MRSID_HAVE_GETWKT
37 : #endif
38 :
39 : /* getTotalBandData is deprecated by getBandData, at least starting with 8.5 */
40 : #if defined(LTI_SDK_MAJOR) && \
41 : (LTI_SDK_MAJOR > 8 || (LTI_SDK_MAJOR >= 8 && LTI_SDK_MINOR >= 5))
42 : #define myGetTotalBandData getBandData
43 : #else
44 : #define myGetTotalBandData getTotalBandData
45 : #endif
46 :
47 : #include "mrsidstream.h"
48 :
49 : using namespace LizardTech;
50 :
51 : /* -------------------------------------------------------------------- */
52 : /* Various wrapper templates used to force new/delete to happen */
53 : /* in the same heap. See bug 1213 and MSDN knowledge base */
54 : /* article 122675. */
55 : /* -------------------------------------------------------------------- */
56 :
57 : template <class T> class LTIDLLPixel : public T
58 : {
59 : public:
60 62 : LTIDLLPixel(LTIColorSpace colorSpace, lt_uint16 numBands,
61 : LTIDataType dataType)
62 62 : : T(colorSpace, numBands, dataType)
63 : {
64 62 : }
65 :
66 124 : virtual ~LTIDLLPixel()
67 : {
68 124 : }
69 : };
70 :
71 : template <class T> class LTIDLLReader : public T
72 : {
73 : public:
74 : explicit LTIDLLReader(const LTFileSpec &fileSpec, bool useWorldFile = false)
75 : : T(fileSpec, useWorldFile)
76 : {
77 : }
78 :
79 : explicit LTIDLLReader(LTIOStreamInf &oStream, bool useWorldFile = false)
80 : : T(oStream, useWorldFile)
81 : {
82 : }
83 :
84 : explicit LTIDLLReader(LTIOStreamInf *poStream,
85 : LTIOStreamInf *poWorldFile = nullptr)
86 : : T(poStream, poWorldFile)
87 : {
88 : }
89 :
90 : virtual ~LTIDLLReader()
91 : {
92 : }
93 : };
94 :
95 : template <class T> class LTIDLLNavigator : public T
96 : {
97 : public:
98 62 : explicit LTIDLLNavigator(const LTIImage &image) : T(image)
99 : {
100 62 : }
101 :
102 124 : virtual ~LTIDLLNavigator()
103 : {
104 124 : }
105 : };
106 :
107 : template <class T> class LTIDLLBuffer : public T
108 : {
109 : public:
110 5 : LTIDLLBuffer(const LTIPixel &pixelProps, lt_uint32 totalNumCols,
111 : lt_uint32 totalNumRows, void **data)
112 5 : : T(pixelProps, totalNumCols, totalNumRows, data)
113 : {
114 5 : }
115 :
116 10 : virtual ~LTIDLLBuffer()
117 : {
118 10 : }
119 : };
120 :
121 : template <class T> class LTIDLLCopy : public T
122 : {
123 : public:
124 12 : explicit LTIDLLCopy(const T &original) : T(original)
125 : {
126 12 : }
127 :
128 24 : virtual ~LTIDLLCopy()
129 : {
130 24 : }
131 : };
132 :
133 : template <class T> class LTIDLLWriter : public T
134 : {
135 : public:
136 : explicit LTIDLLWriter(LTIImageStage *image) : T(image)
137 : {
138 : }
139 :
140 : virtual ~LTIDLLWriter()
141 : {
142 : }
143 : };
144 :
145 : template <class T> class LTIDLLDefault : public T
146 : {
147 : public:
148 : LTIDLLDefault() : T()
149 : {
150 : }
151 :
152 : virtual ~LTIDLLDefault()
153 : {
154 : }
155 : };
156 :
157 : /* -------------------------------------------------------------------- */
158 : /* Interface to MrSID SDK progress reporting. */
159 : /* -------------------------------------------------------------------- */
160 :
161 : class MrSIDProgress : public LTIProgressDelegate
162 : {
163 : public:
164 : MrSIDProgress(GDALProgressFunc f, void *arg) : m_f(f), m_arg(arg)
165 : {
166 : }
167 :
168 : virtual ~MrSIDProgress()
169 : {
170 : }
171 :
172 : virtual LT_STATUS setProgressStatus(float fraction) override
173 : {
174 : if (!m_f)
175 : return LT_STS_BadContext;
176 : if (!m_f(fraction, nullptr, m_arg))
177 : return LT_STS_Failure;
178 : return LT_STS_Success;
179 : }
180 :
181 : private:
182 : GDALProgressFunc m_f;
183 : void *m_arg;
184 : };
185 :
186 : /************************************************************************/
187 : /* ==================================================================== */
188 : /* MrSIDDataset */
189 : /* ==================================================================== */
190 : /************************************************************************/
191 :
192 : class MrSIDDataset final : public GDALJP2AbstractDataset
193 : {
194 : friend class MrSIDRasterBand;
195 :
196 : LTIOStreamInf *poStream;
197 : LTIOFileStream oLTIStream;
198 : LTIVSIStream oVSIStream;
199 :
200 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 7
201 : LTIImageFilter *poImageReader;
202 : #else
203 : LTIImageReader *poImageReader;
204 : #endif
205 :
206 : #ifdef MRSID_ESDK
207 : LTIGeoFileImageWriter *poImageWriter;
208 : #endif
209 :
210 : LTIDLLNavigator<LTINavigator> *poLTINav;
211 : LTIDLLCopy<LTIMetadataDatabase> *poMetadata;
212 : const LTIPixel *poNDPixel;
213 :
214 : LTIDLLBuffer<LTISceneBuffer> *poBuffer;
215 : int nBlockXSize;
216 : int nBlockYSize;
217 : int bPrevBlockRead;
218 : int nPrevBlockXOff, nPrevBlockYOff;
219 :
220 : LTIDataType eSampleType;
221 : GDALDataType eDataType;
222 : LTIColorSpace eColorSpace;
223 :
224 : double dfCurrentMag;
225 :
226 : GTIFDefn *psDefn;
227 :
228 : MrSIDDataset *poParentDS;
229 : int bIsOverview;
230 : int nOverviewCount;
231 : MrSIDDataset **papoOverviewDS;
232 :
233 : CPLString osMETFilename;
234 :
235 : CPLErr OpenZoomLevel(lt_int32 iZoom);
236 : int GetMetadataElement(const char *, void *, int = 0);
237 : void FetchProjParams();
238 : void GetGTIFDefn();
239 : char *GetOGISDefn(GTIFDefn *);
240 :
241 : int m_nInRasterIO = 0; // Prevent infinite recursion in IRasterIO()
242 :
243 : virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
244 : GDALDataType, int, BANDMAP_TYPE,
245 : GSpacing nPixelSpace, GSpacing nLineSpace,
246 : GSpacing nBandSpace,
247 : GDALRasterIOExtraArg *psExtraArg) override;
248 :
249 : protected:
250 : virtual int CloseDependentDatasets() override;
251 :
252 : virtual CPLErr IBuildOverviews(const char *, int, const int *, int,
253 : const int *, GDALProgressFunc, void *,
254 : CSLConstList papszOptions) override;
255 :
256 : public:
257 : explicit MrSIDDataset(int bIsJPEG2000);
258 : ~MrSIDDataset();
259 :
260 : static GDALDataset *Open(GDALOpenInfo *poOpenInfo, int bIsJP2);
261 :
262 : virtual char **GetFileList() override;
263 :
264 : #ifdef MRSID_ESDK
265 : static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
266 : int nBands, GDALDataType eType,
267 : char **papszParamList);
268 : #endif
269 : };
270 :
271 : /************************************************************************/
272 : /* ==================================================================== */
273 : /* MrSIDRasterBand */
274 : /* ==================================================================== */
275 : /************************************************************************/
276 :
277 : class MrSIDRasterBand final : public GDALPamRasterBand
278 : {
279 : friend class MrSIDDataset;
280 :
281 : LTIPixel *poPixel;
282 :
283 : int nBlockSize;
284 :
285 : int bNoDataSet;
286 : double dfNoDataValue;
287 :
288 : MrSIDDataset *poGDS;
289 :
290 : GDALColorInterp eBandInterp;
291 :
292 : public:
293 : MrSIDRasterBand(MrSIDDataset *, int);
294 : ~MrSIDRasterBand();
295 :
296 : virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
297 : GDALDataType, GSpacing nPixelSpace,
298 : GSpacing nLineSpace,
299 : GDALRasterIOExtraArg *psExtraArg) override;
300 :
301 : virtual CPLErr IReadBlock(int, int, void *) override;
302 : virtual GDALColorInterp GetColorInterpretation() override;
303 : CPLErr SetColorInterpretation(GDALColorInterp eNewInterp) override;
304 : virtual double GetNoDataValue(int *) override;
305 : virtual int GetOverviewCount() override;
306 : virtual GDALRasterBand *GetOverview(int) override;
307 :
308 : virtual CPLErr GetStatistics(int bApproxOK, int bForce, double *pdfMin,
309 : double *pdfMax, double *pdfMean,
310 : double *pdfStdDev) override;
311 :
312 : #ifdef MRSID_ESDK
313 : virtual CPLErr IWriteBlock(int, int, void *) override;
314 : #endif
315 : };
316 :
317 : /************************************************************************/
318 : /* MrSIDRasterBand() */
319 : /************************************************************************/
320 :
321 62 : MrSIDRasterBand::MrSIDRasterBand(MrSIDDataset *poDSIn, int nBandIn)
322 : {
323 62 : this->poDS = poDSIn;
324 62 : poGDS = poDSIn;
325 62 : this->nBand = nBandIn;
326 62 : this->eDataType = poDSIn->eDataType;
327 :
328 : /* -------------------------------------------------------------------- */
329 : /* Set the block sizes and buffer parameters. */
330 : /* -------------------------------------------------------------------- */
331 62 : nBlockXSize = poDSIn->nBlockXSize;
332 62 : nBlockYSize = poDSIn->nBlockYSize;
333 : // #ifdef notdef
334 62 : if (poDS->GetRasterXSize() > 2048)
335 0 : nBlockXSize = 1024;
336 62 : if (poDS->GetRasterYSize() > 128)
337 36 : nBlockYSize = 128;
338 : else
339 26 : nBlockYSize = poDS->GetRasterYSize();
340 : // #endif
341 :
342 62 : nBlockSize = nBlockXSize * nBlockYSize;
343 62 : poPixel = new LTIDLLPixel<LTIPixel>(poDSIn->eColorSpace,
344 62 : static_cast<lt_uint16>(poDSIn->nBands),
345 62 : poDSIn->eSampleType);
346 :
347 : /* -------------------------------------------------------------------- */
348 : /* Set NoData values. */
349 : /* */
350 : /* This logic is disabled for now since the MrSID nodata */
351 : /* semantics are different than GDAL. In MrSID all bands must */
352 : /* match the nodata value for that band in order for the pixel */
353 : /* to be considered nodata, otherwise all values are valid. */
354 : /* -------------------------------------------------------------------- */
355 : #ifdef notdef
356 : if (poDS->poNDPixel)
357 : {
358 : switch (poDS->eSampleType)
359 : {
360 : case LTI_DATATYPE_UINT8:
361 : case LTI_DATATYPE_SINT8:
362 : dfNoDataValue =
363 : (double)poDS->poNDPixel->getSampleValueUint8(nBand - 1);
364 : break;
365 : case LTI_DATATYPE_UINT16:
366 : dfNoDataValue =
367 : (double)poDS->poNDPixel->getSampleValueUint16(nBand - 1);
368 : break;
369 : case LTI_DATATYPE_FLOAT32:
370 : dfNoDataValue =
371 : poDS->poNDPixel->getSampleValueFloat32(nBand - 1);
372 : break;
373 : case LTI_DATATYPE_SINT16:
374 : dfNoDataValue =
375 : (double)*(GInt16 *)poDS->poNDPixel->getSampleValueAddr(
376 : nBand - 1);
377 : break;
378 : case LTI_DATATYPE_UINT32:
379 : dfNoDataValue =
380 : (double)*(GUInt32 *)poDS->poNDPixel->getSampleValueAddr(
381 : nBand - 1);
382 : break;
383 : case LTI_DATATYPE_SINT32:
384 : dfNoDataValue =
385 : (double)*(GInt32 *)poDS->poNDPixel->getSampleValueAddr(
386 : nBand - 1);
387 : break;
388 : case LTI_DATATYPE_FLOAT64:
389 : dfNoDataValue =
390 : *(double *)poDS->poNDPixel->getSampleValueAddr(nBand - 1);
391 : break;
392 :
393 : case LTI_DATATYPE_INVALID:
394 : CPLAssert(false);
395 : break;
396 : }
397 : bNoDataSet = TRUE;
398 : }
399 : else
400 : #endif
401 : {
402 62 : dfNoDataValue = 0.0;
403 62 : bNoDataSet = FALSE;
404 : }
405 :
406 62 : switch (poGDS->eColorSpace)
407 : {
408 0 : case LTI_COLORSPACE_RGB:
409 0 : if (nBand == 1)
410 0 : eBandInterp = GCI_RedBand;
411 0 : else if (nBand == 2)
412 0 : eBandInterp = GCI_GreenBand;
413 0 : else if (nBand == 3)
414 0 : eBandInterp = GCI_BlueBand;
415 : else
416 0 : eBandInterp = GCI_Undefined;
417 0 : break;
418 :
419 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
420 0 : case LTI_COLORSPACE_RGBA:
421 0 : if (nBand == 1)
422 0 : eBandInterp = GCI_RedBand;
423 0 : else if (nBand == 2)
424 0 : eBandInterp = GCI_GreenBand;
425 0 : else if (nBand == 3)
426 0 : eBandInterp = GCI_BlueBand;
427 0 : else if (nBand == 4)
428 0 : eBandInterp = GCI_AlphaBand;
429 : else
430 0 : eBandInterp = GCI_Undefined;
431 0 : break;
432 : #endif
433 :
434 0 : case LTI_COLORSPACE_CMYK:
435 0 : if (nBand == 1)
436 0 : eBandInterp = GCI_CyanBand;
437 0 : else if (nBand == 2)
438 0 : eBandInterp = GCI_MagentaBand;
439 0 : else if (nBand == 3)
440 0 : eBandInterp = GCI_YellowBand;
441 0 : else if (nBand == 4)
442 0 : eBandInterp = GCI_BlackBand;
443 : else
444 0 : eBandInterp = GCI_Undefined;
445 0 : break;
446 :
447 62 : case LTI_COLORSPACE_GRAYSCALE:
448 62 : eBandInterp = GCI_GrayIndex;
449 62 : break;
450 :
451 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
452 0 : case LTI_COLORSPACE_GRAYSCALEA:
453 0 : if (nBand == 1)
454 0 : eBandInterp = GCI_GrayIndex;
455 0 : else if (nBand == 2)
456 0 : eBandInterp = GCI_AlphaBand;
457 : else
458 0 : eBandInterp = GCI_Undefined;
459 0 : break;
460 :
461 0 : case LTI_COLORSPACE_GRAYSCALEA_PM:
462 0 : if (nBand == 1)
463 0 : eBandInterp = GCI_GrayIndex;
464 0 : else if (nBand == 2)
465 0 : eBandInterp = GCI_AlphaBand;
466 : else
467 0 : eBandInterp = GCI_Undefined;
468 0 : break;
469 : #endif
470 :
471 0 : default:
472 0 : eBandInterp = GCI_Undefined;
473 0 : break;
474 : }
475 62 : }
476 :
477 : /************************************************************************/
478 : /* ~MrSIDRasterBand() */
479 : /************************************************************************/
480 :
481 124 : MrSIDRasterBand::~MrSIDRasterBand()
482 : {
483 62 : if (poPixel)
484 62 : delete poPixel;
485 124 : }
486 :
487 : /************************************************************************/
488 : /* IReadBlock() */
489 : /************************************************************************/
490 :
491 13 : CPLErr MrSIDRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
492 : {
493 : #ifdef MRSID_ESDK
494 : if (poGDS->eAccess == GA_Update)
495 : {
496 : CPLDebug("MrSID",
497 : "IReadBlock() - DSDK - read on updatable file fails.");
498 : memset(pImage, 0,
499 : static_cast<size_t>(nBlockSize) *
500 : GDALGetDataTypeSizeBytes(eDataType));
501 : return CE_None;
502 : }
503 : #endif /* MRSID_ESDK */
504 :
505 13 : CPLDebug("MrSID", "IReadBlock(%d,%d)", nBlockXOff, nBlockYOff);
506 :
507 13 : if (!poGDS->bPrevBlockRead || poGDS->nPrevBlockXOff != nBlockXOff ||
508 8 : poGDS->nPrevBlockYOff != nBlockYOff)
509 : {
510 13 : GInt32 nLine = nBlockYOff * nBlockYSize;
511 13 : GInt32 nCol = nBlockXOff * nBlockXSize;
512 :
513 : // XXX: The scene, passed to LTIImageStage::read() call must be
514 : // inside the image boundaries. So we should detect the last strip and
515 : // form the scene properly.
516 13 : CPLDebug("MrSID", "IReadBlock - read() %dx%d block at %d,%d.",
517 : nBlockXSize, nBlockYSize, nCol, nLine);
518 :
519 13 : if (!LT_SUCCESS(poGDS->poLTINav->setSceneAsULWH(
520 : nCol, nLine,
521 : (nCol + nBlockXSize > poGDS->GetRasterXSize())
522 : ? (poGDS->GetRasterXSize() - nCol)
523 : : nBlockXSize,
524 : (nLine + nBlockYSize > poGDS->GetRasterYSize())
525 : ? (poGDS->GetRasterYSize() - nLine)
526 : : nBlockYSize,
527 : poGDS->dfCurrentMag)))
528 : {
529 0 : CPLError(
530 : CE_Failure, CPLE_AppDefined,
531 : "MrSIDRasterBand::IReadBlock(): Failed to set scene position.");
532 0 : return CE_Failure;
533 : }
534 :
535 13 : if (!poGDS->poBuffer)
536 : {
537 5 : poGDS->poBuffer = new LTIDLLBuffer<LTISceneBuffer>(
538 5 : *poPixel, nBlockXSize, nBlockYSize, nullptr);
539 : // poGDS->poBuffer =
540 : // new LTISceneBuffer( *poPixel, nBlockXSize,
541 : // nBlockYSize, nullptr );
542 : }
543 :
544 13 : if (!LT_SUCCESS(poGDS->poImageReader->read(poGDS->poLTINav->getScene(),
545 : *poGDS->poBuffer)))
546 : {
547 0 : CPLError(CE_Failure, CPLE_AppDefined,
548 : "MrSIDRasterBand::IReadBlock(): Failed to load image.");
549 0 : return CE_Failure;
550 : }
551 :
552 13 : poGDS->bPrevBlockRead = TRUE;
553 13 : poGDS->nPrevBlockXOff = nBlockXOff;
554 13 : poGDS->nPrevBlockYOff = nBlockYOff;
555 : }
556 :
557 26 : memcpy(
558 : pImage,
559 26 : poGDS->poBuffer->myGetTotalBandData(static_cast<lt_uint16>(nBand - 1)),
560 13 : nBlockSize * GDALGetDataTypeSizeBytes(poGDS->eDataType));
561 :
562 13 : return CE_None;
563 : }
564 :
565 : #ifdef MRSID_ESDK
566 :
567 : /************************************************************************/
568 : /* IWriteBlock() */
569 : /************************************************************************/
570 :
571 : CPLErr MrSIDRasterBand::IWriteBlock(int nBlockXOff, int nBlockYOff,
572 : void *pImage)
573 : {
574 : CPLAssert(poGDS != nullptr && nBlockXOff >= 0 && nBlockYOff >= 0 &&
575 : pImage != nullptr);
576 :
577 : #ifdef DEBUG
578 : CPLDebug("MrSID", "IWriteBlock(): nBlockXOff=%d, nBlockYOff=%d", nBlockXOff,
579 : nBlockYOff);
580 : #endif
581 :
582 : LTIScene oScene(nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
583 : nBlockXSize, nBlockYSize, 1.0);
584 : LTISceneBuffer oSceneBuf(*poPixel, poGDS->nBlockXSize, poGDS->nBlockYSize,
585 : &pImage);
586 :
587 : if (!LT_SUCCESS(poGDS->poImageWriter->writeBegin(oScene)))
588 : {
589 : CPLError(CE_Failure, CPLE_AppDefined,
590 : "MrSIDRasterBand::IWriteBlock(): writeBegin failed.");
591 : return CE_Failure;
592 : }
593 :
594 : if (!LT_SUCCESS(poGDS->poImageWriter->writeStrip(oSceneBuf, oScene)))
595 : {
596 : CPLError(CE_Failure, CPLE_AppDefined,
597 : "MrSIDRasterBand::IWriteBlock(): writeStrip failed.");
598 : return CE_Failure;
599 : }
600 :
601 : if (!LT_SUCCESS(poGDS->poImageWriter->writeEnd()))
602 : {
603 : CPLError(CE_Failure, CPLE_AppDefined,
604 : "MrSIDRasterBand::IWriteBlock(): writeEnd failed.");
605 : return CE_Failure;
606 : }
607 :
608 : return CE_None;
609 : }
610 :
611 : #endif /* MRSID_ESDK */
612 :
613 : /************************************************************************/
614 : /* IRasterIO() */
615 : /************************************************************************/
616 :
617 10 : CPLErr MrSIDRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
618 : int nXSize, int nYSize, void *pData,
619 : int nBufXSize, int nBufYSize,
620 : GDALDataType eBufType, GSpacing nPixelSpace,
621 : GSpacing nLineSpace,
622 : GDALRasterIOExtraArg *psExtraArg)
623 :
624 : {
625 : /* -------------------------------------------------------------------- */
626 : /* Fallback to default implementation if the whole scanline */
627 : /* without subsampling requested. */
628 : /* -------------------------------------------------------------------- */
629 10 : if (nXSize == poGDS->GetRasterXSize() && nXSize == nBufXSize &&
630 : nYSize == nBufYSize)
631 : {
632 10 : return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
633 : pData, nBufXSize, nBufYSize, eBufType,
634 10 : nPixelSpace, nLineSpace, psExtraArg);
635 : }
636 :
637 : /* -------------------------------------------------------------------- */
638 : /* Handle via the dataset level IRasterIO() */
639 : /* -------------------------------------------------------------------- */
640 0 : return poGDS->IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
641 : nBufXSize, nBufYSize, eBufType, 1, &nBand,
642 0 : nPixelSpace, nLineSpace, 0, psExtraArg);
643 : }
644 :
645 : /************************************************************************/
646 : /* GetColorInterpretation() */
647 : /************************************************************************/
648 :
649 0 : GDALColorInterp MrSIDRasterBand::GetColorInterpretation()
650 :
651 : {
652 0 : return eBandInterp;
653 : }
654 :
655 : /************************************************************************/
656 : /* SetColorInterpretation() */
657 : /* */
658 : /* This would normally just be used by folks using the MrSID code */
659 : /* to read JP2 streams in other formats (such as NITF) and */
660 : /* providing their own color interpretation regardless of what */
661 : /* MrSID might think the stream itself says. */
662 : /************************************************************************/
663 :
664 0 : CPLErr MrSIDRasterBand::SetColorInterpretation(GDALColorInterp eNewInterp)
665 :
666 : {
667 0 : eBandInterp = eNewInterp;
668 :
669 0 : return CE_None;
670 : }
671 :
672 : /************************************************************************/
673 : /* GetStatistics() */
674 : /* */
675 : /* We override this method so that we can force generation of */
676 : /* statistics if approx ok is true since we know that a small */
677 : /* overview is always available, and that computing statistics */
678 : /* from it is very fast. */
679 : /************************************************************************/
680 :
681 7 : CPLErr MrSIDRasterBand::GetStatistics(int bApproxOK, int bForce, double *pdfMin,
682 : double *pdfMax, double *pdfMean,
683 : double *pdfStdDev)
684 :
685 : {
686 7 : if (bApproxOK)
687 4 : bForce = TRUE;
688 :
689 7 : return GDALPamRasterBand::GetStatistics(bApproxOK, bForce, pdfMin, pdfMax,
690 7 : pdfMean, pdfStdDev);
691 : }
692 :
693 : /************************************************************************/
694 : /* GetNoDataValue() */
695 : /************************************************************************/
696 :
697 11 : double MrSIDRasterBand::GetNoDataValue(int *pbSuccess)
698 :
699 : {
700 11 : if (bNoDataSet)
701 : {
702 0 : if (pbSuccess)
703 0 : *pbSuccess = bNoDataSet;
704 :
705 0 : return dfNoDataValue;
706 : }
707 :
708 11 : return GDALPamRasterBand::GetNoDataValue(pbSuccess);
709 : }
710 :
711 : /************************************************************************/
712 : /* GetOverviewCount() */
713 : /************************************************************************/
714 :
715 14 : int MrSIDRasterBand::GetOverviewCount()
716 :
717 : {
718 14 : return poGDS->nOverviewCount;
719 : }
720 :
721 : /************************************************************************/
722 : /* GetOverview() */
723 : /************************************************************************/
724 :
725 10 : GDALRasterBand *MrSIDRasterBand::GetOverview(int i)
726 :
727 : {
728 10 : if (i < 0 || i >= poGDS->nOverviewCount)
729 0 : return nullptr;
730 : else
731 10 : return poGDS->papoOverviewDS[i]->GetRasterBand(nBand);
732 : }
733 :
734 : /************************************************************************/
735 : /* MrSIDDataset() */
736 : /************************************************************************/
737 :
738 62 : MrSIDDataset::MrSIDDataset(int bIsJPEG2000)
739 : : nBlockXSize(0), nBlockYSize(0), eSampleType(LTI_DATATYPE_UINT8),
740 62 : eDataType(GDT_Byte), eColorSpace(LTI_COLORSPACE_INVALID)
741 : {
742 62 : poStream = nullptr;
743 62 : poImageReader = nullptr;
744 : #ifdef MRSID_ESDK
745 : poImageWriter = nullptr;
746 : #endif
747 62 : poLTINav = nullptr;
748 62 : poMetadata = nullptr;
749 62 : poNDPixel = nullptr;
750 62 : nBands = 0;
751 :
752 62 : poBuffer = nullptr;
753 62 : bPrevBlockRead = FALSE;
754 62 : nPrevBlockXOff = 0;
755 62 : nPrevBlockYOff = 0;
756 :
757 62 : psDefn = nullptr;
758 :
759 62 : dfCurrentMag = 1.0;
760 62 : bIsOverview = FALSE;
761 62 : poParentDS = this;
762 62 : nOverviewCount = 0;
763 62 : papoOverviewDS = nullptr;
764 :
765 62 : poDriver =
766 62 : (GDALDriver *)GDALGetDriverByName(bIsJPEG2000 ? "JP2MrSID" : "MrSID");
767 62 : }
768 :
769 : /************************************************************************/
770 : /* ~MrSIDDataset() */
771 : /************************************************************************/
772 :
773 124 : MrSIDDataset::~MrSIDDataset()
774 : {
775 62 : MrSIDDataset::FlushCache(true);
776 :
777 : #ifdef MRSID_ESDK
778 : if (poImageWriter)
779 : delete poImageWriter;
780 : #endif
781 :
782 62 : if (poBuffer)
783 5 : delete poBuffer;
784 62 : if (poMetadata)
785 12 : delete poMetadata;
786 62 : if (poLTINav)
787 62 : delete poLTINav;
788 62 : if (poImageReader && !bIsOverview)
789 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 7
790 : {
791 12 : poImageReader->release();
792 12 : poImageReader = nullptr;
793 : }
794 : #else
795 : delete poImageReader;
796 : #endif
797 : // points to another member, don't delete
798 62 : poStream = nullptr;
799 :
800 62 : if (psDefn)
801 12 : delete psDefn;
802 62 : MrSIDDataset::CloseDependentDatasets();
803 124 : }
804 :
805 : /************************************************************************/
806 : /* CloseDependentDatasets() */
807 : /************************************************************************/
808 :
809 64 : int MrSIDDataset::CloseDependentDatasets()
810 : {
811 64 : int bRet = GDALPamDataset::CloseDependentDatasets();
812 :
813 64 : if (papoOverviewDS)
814 : {
815 62 : for (int i = 0; i < nOverviewCount; i++)
816 50 : delete papoOverviewDS[i];
817 12 : CPLFree(papoOverviewDS);
818 12 : papoOverviewDS = nullptr;
819 12 : bRet = TRUE;
820 : }
821 64 : return bRet;
822 : }
823 :
824 : /************************************************************************/
825 : /* IRasterIO() */
826 : /************************************************************************/
827 :
828 1 : CPLErr MrSIDDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
829 : int nXSize, int nYSize, void *pData,
830 : int nBufXSize, int nBufYSize,
831 : GDALDataType eBufType, int nBandCount,
832 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
833 : GSpacing nLineSpace, GSpacing nBandSpace,
834 : GDALRasterIOExtraArg *psExtraArg)
835 :
836 : {
837 : /* -------------------------------------------------------------------- */
838 : /* We need various criteria to skip out to block based methods. */
839 : /* -------------------------------------------------------------------- */
840 1 : int bUseBlockedIO = bForceCachedIO;
841 :
842 1 : if (nYSize == 1 || nXSize * ((double)nYSize) < 100.0)
843 0 : bUseBlockedIO = TRUE;
844 :
845 1 : if (nBufYSize == 1 || nBufXSize * ((double)nBufYSize) < 100.0)
846 0 : bUseBlockedIO = TRUE;
847 :
848 1 : if (CPLTestBool(CPLGetConfigOption("GDAL_ONE_BIG_READ", "NO")))
849 0 : bUseBlockedIO = FALSE;
850 :
851 1 : if (bUseBlockedIO && !m_nInRasterIO)
852 : {
853 0 : ++m_nInRasterIO;
854 0 : const CPLErr eErr = GDALDataset::BlockBasedRasterIO(
855 : eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
856 : eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace,
857 : nBandSpace, psExtraArg);
858 0 : --m_nInRasterIO;
859 0 : return eErr;
860 : }
861 :
862 1 : CPLDebug("MrSID", "RasterIO() - using optimized dataset level IO.");
863 :
864 : /* -------------------------------------------------------------------- */
865 : /* What is our requested window relative to the base dataset. */
866 : /* We want to operate from here on as if we were operating on */
867 : /* the full res band. */
868 : /* -------------------------------------------------------------------- */
869 1 : int nZoomMag = (int)((1 / dfCurrentMag) * 1.0000001);
870 :
871 1 : nXOff *= nZoomMag;
872 1 : nYOff *= nZoomMag;
873 1 : nXSize *= nZoomMag;
874 1 : nYSize *= nZoomMag;
875 :
876 : /* -------------------------------------------------------------------- */
877 : /* We need to figure out the best zoom level to use for this */
878 : /* request. We apply a small fudge factor to make sure that */
879 : /* request just very, very slightly larger than a zoom level do */
880 : /* not force us to the next level. */
881 : /* -------------------------------------------------------------------- */
882 1 : int iOverview = 0;
883 1 : double dfZoomMag =
884 1 : MIN((nXSize / (double)nBufXSize), (nYSize / (double)nBufYSize));
885 :
886 5 : for (nZoomMag = 1; nZoomMag * 2 < (dfZoomMag + 0.1) &&
887 5 : iOverview < poParentDS->nOverviewCount;
888 4 : nZoomMag *= 2, iOverview++)
889 : {
890 : }
891 :
892 : /* -------------------------------------------------------------------- */
893 : /* Work out the size of the temporary buffer and allocate it. */
894 : /* The temporary buffer will generally be at a moderately */
895 : /* higher resolution than the buffer of data requested. */
896 : /* -------------------------------------------------------------------- */
897 : int nTmpPixelSize;
898 2 : LTIPixel oPixel(eColorSpace, static_cast<lt_uint16>(nBands), eSampleType);
899 :
900 : LT_STATUS eLTStatus;
901 : unsigned int maxWidth;
902 : unsigned int maxHeight;
903 :
904 : eLTStatus =
905 1 : poImageReader->getDimsAtMag(1.0 / nZoomMag, maxWidth, maxHeight);
906 :
907 1 : if (!LT_SUCCESS(eLTStatus))
908 : {
909 0 : CPLError(CE_Failure, CPLE_AppDefined,
910 : "MrSIDDataset::IRasterIO(): Failed to get zoomed image "
911 : "dimensions.\n%s",
912 : getLastStatusString(eLTStatus));
913 0 : return CE_Failure;
914 : }
915 :
916 : int maxWidthAtL0 =
917 1 : bIsOverview ? poParentDS->GetRasterXSize() : this->GetRasterXSize();
918 : int maxHeightAtL0 =
919 1 : bIsOverview ? poParentDS->GetRasterYSize() : this->GetRasterYSize();
920 :
921 1 : int sceneUlXOff = nXOff / nZoomMag;
922 1 : int sceneUlYOff = nYOff / nZoomMag;
923 1 : int sceneWidth =
924 1 : (int)(nXSize * (double)maxWidth / (double)maxWidthAtL0 + 0.99);
925 1 : int sceneHeight =
926 1 : (int)(nYSize * (double)maxHeight / (double)maxHeightAtL0 + 0.99);
927 :
928 1 : if ((sceneUlXOff + sceneWidth) > (int)maxWidth)
929 0 : sceneWidth = maxWidth - sceneUlXOff;
930 :
931 1 : if ((sceneUlYOff + sceneHeight) > (int)maxHeight)
932 0 : sceneHeight = maxHeight - sceneUlYOff;
933 :
934 2 : LTISceneBuffer oLTIBuffer(oPixel, sceneWidth, sceneHeight, nullptr);
935 :
936 1 : nTmpPixelSize = GDALGetDataTypeSizeBytes(eDataType);
937 :
938 : /* -------------------------------------------------------------------- */
939 : /* Create navigator, and move to the requested scene area. */
940 : /* -------------------------------------------------------------------- */
941 2 : LTINavigator oNav(*poImageReader);
942 :
943 1 : if (!LT_SUCCESS(oNav.setSceneAsULWH(sceneUlXOff, sceneUlYOff, sceneWidth,
944 : sceneHeight, 1.0 / nZoomMag)))
945 : {
946 0 : CPLError(CE_Failure, CPLE_AppDefined,
947 : "MrSIDDataset::IRasterIO(): Failed to set scene position.");
948 :
949 0 : return CE_Failure;
950 : }
951 :
952 1 : CPLDebug("MrSID",
953 : "Dataset:IRasterIO(%d,%d %dx%d -> %dx%d -> %dx%d, zoom=%d)", nXOff,
954 : nYOff, nXSize, nYSize, sceneWidth, sceneHeight, nBufXSize,
955 : nBufYSize, nZoomMag);
956 :
957 1 : if (!oNav.isSceneValid())
958 0 : CPLDebug("MrSID", "LTINavigator in invalid state.");
959 :
960 : /* -------------------------------------------------------------------- */
961 : /* Read into the buffer. */
962 : /* -------------------------------------------------------------------- */
963 :
964 1 : eLTStatus = poImageReader->read(oNav.getScene(), oLTIBuffer);
965 1 : if (!LT_SUCCESS(eLTStatus))
966 : {
967 0 : CPLError(CE_Failure, CPLE_AppDefined,
968 : "MrSIDRasterBand::IRasterIO(): Failed to load image.\n%s",
969 : getLastStatusString(eLTStatus));
970 0 : return CE_Failure;
971 : }
972 :
973 : /* -------------------------------------------------------------------- */
974 : /* If we are pulling the data at a matching resolution, try to */
975 : /* do a more direct copy without subsampling. */
976 : /* -------------------------------------------------------------------- */
977 : int iBufLine, iBufPixel;
978 :
979 1 : if (nBufXSize == sceneWidth && nBufYSize == sceneHeight)
980 : {
981 0 : for (int iBand = 0; iBand < nBandCount; iBand++)
982 : {
983 0 : GByte *pabySrcBand = (GByte *)oLTIBuffer.myGetTotalBandData(
984 0 : static_cast<lt_uint16>(panBandMap[iBand] - 1));
985 :
986 0 : for (int iLine = 0; iLine < nBufYSize; iLine++)
987 : {
988 0 : GDALCopyWords(
989 0 : pabySrcBand + iLine * nTmpPixelSize * sceneWidth, eDataType,
990 : nTmpPixelSize,
991 0 : ((GByte *)pData) + iLine * nLineSpace + iBand * nBandSpace,
992 : eBufType, static_cast<int>(nPixelSpace), nBufXSize);
993 : }
994 0 : }
995 : }
996 :
997 : /* -------------------------------------------------------------------- */
998 : /* Manually resample to our target buffer. */
999 : /* -------------------------------------------------------------------- */
1000 : else
1001 : {
1002 11 : for (iBufLine = 0; iBufLine < nBufYSize; iBufLine++)
1003 : {
1004 10 : int iTmpLine =
1005 10 : (int)floor(((iBufLine + 0.5) / nBufYSize) * sceneHeight);
1006 :
1007 110 : for (iBufPixel = 0; iBufPixel < nBufXSize; iBufPixel++)
1008 : {
1009 100 : int iTmpPixel =
1010 100 : (int)floor(((iBufPixel + 0.5) / nBufXSize) * sceneWidth);
1011 :
1012 200 : for (int iBand = 0; iBand < nBandCount; iBand++)
1013 : {
1014 : GByte *pabySrc, *pabyDst;
1015 :
1016 100 : pabyDst = ((GByte *)pData) + nPixelSpace * iBufPixel +
1017 100 : nLineSpace * iBufLine + nBandSpace * iBand;
1018 :
1019 200 : pabySrc = (GByte *)oLTIBuffer.myGetTotalBandData(
1020 100 : static_cast<lt_uint16>(panBandMap[iBand] - 1));
1021 100 : pabySrc +=
1022 100 : (iTmpLine * sceneWidth + iTmpPixel) * nTmpPixelSize;
1023 :
1024 100 : if (eDataType == eBufType)
1025 100 : memcpy(pabyDst, pabySrc, nTmpPixelSize);
1026 : else
1027 0 : GDALCopyWords(pabySrc, eDataType, 0, pabyDst, eBufType,
1028 : 0, 1);
1029 : }
1030 : }
1031 : }
1032 : }
1033 :
1034 1 : return CE_None;
1035 : }
1036 :
1037 : /************************************************************************/
1038 : /* IBuildOverviews() */
1039 : /************************************************************************/
1040 :
1041 0 : CPLErr MrSIDDataset::IBuildOverviews(const char *, int, const int *, int,
1042 : const int *, GDALProgressFunc, void *,
1043 : CSLConstList)
1044 : {
1045 0 : CPLError(CE_Warning, CPLE_AppDefined,
1046 : "MrSID overviews are built-in, so building external "
1047 : "overviews is unnecessary. Ignoring.\n");
1048 :
1049 0 : return CE_None;
1050 : }
1051 :
1052 : /************************************************************************/
1053 : /* SerializeMetadataRec() */
1054 : /************************************************************************/
1055 :
1056 372 : static CPLString SerializeMetadataRec(const LTIMetadataRecord *poMetadataRec)
1057 : {
1058 372 : GUInt32 iNumDims = 0;
1059 372 : const GUInt32 *paiDims = nullptr;
1060 372 : const void *pData = poMetadataRec->getArrayData(iNumDims, paiDims);
1061 744 : CPLString osMetadata;
1062 372 : GUInt32 k = 0;
1063 :
1064 744 : for (GUInt32 i = 0; paiDims != nullptr && i < iNumDims; i++)
1065 : {
1066 : // stops on large binary data
1067 372 : if (poMetadataRec->getDataType() == LTI_METADATA_DATATYPE_UINT8 &&
1068 0 : paiDims[i] > 1024)
1069 0 : return CPLString();
1070 :
1071 964 : for (GUInt32 j = 0; j < paiDims[i]; j++)
1072 : {
1073 1184 : CPLString osTemp;
1074 :
1075 592 : switch (poMetadataRec->getDataType())
1076 : {
1077 0 : case LTI_METADATA_DATATYPE_UINT8:
1078 : case LTI_METADATA_DATATYPE_SINT8:
1079 0 : osTemp.Printf("%d", ((GByte *)pData)[k++]);
1080 0 : break;
1081 110 : case LTI_METADATA_DATATYPE_UINT16:
1082 110 : osTemp.Printf("%u", ((GUInt16 *)pData)[k++]);
1083 110 : break;
1084 0 : case LTI_METADATA_DATATYPE_SINT16:
1085 0 : osTemp.Printf("%d", ((GInt16 *)pData)[k++]);
1086 0 : break;
1087 20 : case LTI_METADATA_DATATYPE_UINT32:
1088 20 : osTemp.Printf("%u", ((GUInt32 *)pData)[k++]);
1089 20 : break;
1090 30 : case LTI_METADATA_DATATYPE_SINT32:
1091 30 : osTemp.Printf("%d", ((GInt32 *)pData)[k++]);
1092 30 : break;
1093 30 : case LTI_METADATA_DATATYPE_FLOAT32:
1094 30 : osTemp.Printf("%f", ((float *)pData)[k++]);
1095 30 : break;
1096 282 : case LTI_METADATA_DATATYPE_FLOAT64:
1097 282 : osTemp.Printf("%f", ((double *)pData)[k++]);
1098 282 : break;
1099 120 : case LTI_METADATA_DATATYPE_ASCII:
1100 120 : osTemp = ((const char **)pData)[k++];
1101 120 : break;
1102 0 : default:
1103 0 : break;
1104 : }
1105 :
1106 592 : if (!osMetadata.empty())
1107 220 : osMetadata += ',';
1108 592 : osMetadata += osTemp;
1109 : }
1110 : }
1111 :
1112 372 : return osMetadata;
1113 : }
1114 :
1115 : /************************************************************************/
1116 : /* GetMetadataElement() */
1117 : /************************************************************************/
1118 :
1119 288 : int MrSIDDataset::GetMetadataElement(const char *pszKey, void *pValue,
1120 : int iLength)
1121 : {
1122 288 : if (!poMetadata->has(pszKey))
1123 168 : return FALSE;
1124 :
1125 120 : const LTIMetadataRecord *poMetadataRec = nullptr;
1126 120 : poMetadata->get(pszKey, poMetadataRec);
1127 :
1128 120 : if (poMetadataRec == nullptr || !poMetadataRec->isScalar())
1129 40 : return FALSE;
1130 :
1131 : // XXX: return FALSE if we have more than one element in metadata record
1132 : int iSize;
1133 80 : switch (poMetadataRec->getDataType())
1134 : {
1135 0 : case LTI_METADATA_DATATYPE_UINT8:
1136 : case LTI_METADATA_DATATYPE_SINT8:
1137 0 : iSize = 1;
1138 0 : break;
1139 70 : case LTI_METADATA_DATATYPE_UINT16:
1140 : case LTI_METADATA_DATATYPE_SINT16:
1141 70 : iSize = 2;
1142 70 : break;
1143 0 : case LTI_METADATA_DATATYPE_UINT32:
1144 : case LTI_METADATA_DATATYPE_SINT32:
1145 : case LTI_METADATA_DATATYPE_FLOAT32:
1146 0 : iSize = 4;
1147 0 : break;
1148 0 : case LTI_METADATA_DATATYPE_FLOAT64:
1149 0 : iSize = 8;
1150 0 : break;
1151 10 : case LTI_METADATA_DATATYPE_ASCII:
1152 10 : iSize = iLength;
1153 10 : break;
1154 0 : default:
1155 0 : iSize = 0;
1156 0 : break;
1157 : }
1158 :
1159 80 : if (poMetadataRec->getDataType() == LTI_METADATA_DATATYPE_ASCII)
1160 : {
1161 20 : strncpy((char *)pValue,
1162 10 : ((const char **)poMetadataRec->getScalarData())[0], iSize);
1163 10 : ((char *)pValue)[iSize - 1] = '\0';
1164 : }
1165 : else
1166 70 : memcpy(pValue, poMetadataRec->getScalarData(), iSize);
1167 :
1168 80 : return TRUE;
1169 : }
1170 :
1171 : /************************************************************************/
1172 : /* GetFileList() */
1173 : /************************************************************************/
1174 :
1175 3 : char **MrSIDDataset::GetFileList()
1176 : {
1177 3 : char **papszFileList = GDALPamDataset::GetFileList();
1178 :
1179 3 : if (!osMETFilename.empty())
1180 0 : papszFileList = CSLAddString(papszFileList, osMETFilename.c_str());
1181 :
1182 3 : return papszFileList;
1183 : }
1184 :
1185 : /************************************************************************/
1186 : /* OpenZoomLevel() */
1187 : /************************************************************************/
1188 :
1189 62 : CPLErr MrSIDDataset::OpenZoomLevel(lt_int32 iZoom)
1190 : {
1191 : /* -------------------------------------------------------------------- */
1192 : /* Get image geometry. */
1193 : /* -------------------------------------------------------------------- */
1194 62 : if (iZoom != 0)
1195 : {
1196 : lt_uint32 iWidth, iHeight;
1197 50 : dfCurrentMag = LTIUtils::levelToMag(iZoom);
1198 : auto eLTStatus =
1199 50 : poImageReader->getDimsAtMag(dfCurrentMag, iWidth, iHeight);
1200 50 : if (!LT_SUCCESS(eLTStatus))
1201 : {
1202 0 : CPLDebug("MrSID", "Cannot open zoom level %d", iZoom);
1203 0 : return CE_Failure;
1204 : }
1205 50 : nRasterXSize = iWidth;
1206 50 : nRasterYSize = iHeight;
1207 : }
1208 : else
1209 : {
1210 12 : dfCurrentMag = 1.0;
1211 12 : nRasterXSize = poImageReader->getWidth();
1212 12 : nRasterYSize = poImageReader->getHeight();
1213 : }
1214 :
1215 62 : nBands = poImageReader->getNumBands();
1216 62 : nBlockXSize = nRasterXSize;
1217 62 : nBlockYSize = poImageReader->getStripHeight();
1218 :
1219 62 : CPLDebug("MrSID", "Opened zoom level %d with size %dx%d.", iZoom,
1220 : nRasterXSize, nRasterYSize);
1221 :
1222 : try
1223 : {
1224 62 : poLTINav = new LTIDLLNavigator<LTINavigator>(*poImageReader);
1225 : }
1226 0 : catch (...)
1227 : {
1228 0 : CPLError(CE_Failure, CPLE_AppDefined,
1229 : "MrSIDDataset::OpenZoomLevel(): "
1230 : "Failed to create LTINavigator object.");
1231 0 : return CE_Failure;
1232 : }
1233 :
1234 : /* -------------------------------------------------------------------- */
1235 : /* Handle sample type and color space. */
1236 : /* -------------------------------------------------------------------- */
1237 62 : eColorSpace = poImageReader->getColorSpace();
1238 62 : eSampleType = poImageReader->getDataType();
1239 62 : switch (eSampleType)
1240 : {
1241 0 : case LTI_DATATYPE_UINT16:
1242 0 : eDataType = GDT_UInt16;
1243 0 : break;
1244 0 : case LTI_DATATYPE_SINT16:
1245 0 : eDataType = GDT_Int16;
1246 0 : break;
1247 0 : case LTI_DATATYPE_UINT32:
1248 0 : eDataType = GDT_UInt32;
1249 0 : break;
1250 0 : case LTI_DATATYPE_SINT32:
1251 0 : eDataType = GDT_Int32;
1252 0 : break;
1253 0 : case LTI_DATATYPE_FLOAT32:
1254 0 : eDataType = GDT_Float32;
1255 0 : break;
1256 0 : case LTI_DATATYPE_FLOAT64:
1257 0 : eDataType = GDT_Float64;
1258 0 : break;
1259 62 : case LTI_DATATYPE_UINT8:
1260 : case LTI_DATATYPE_SINT8:
1261 : default:
1262 62 : eDataType = GDT_Byte;
1263 62 : break;
1264 : }
1265 :
1266 : /* -------------------------------------------------------------------- */
1267 : /* Read georeferencing. */
1268 : /* -------------------------------------------------------------------- */
1269 62 : if (!poImageReader->isGeoCoordImplicit())
1270 : {
1271 62 : const LTIGeoCoord &oGeo = poImageReader->getGeoCoord();
1272 62 : oGeo.get(m_gt[0], m_gt[3], m_gt[1], m_gt[5], m_gt[2], m_gt[4]);
1273 :
1274 62 : m_gt[0] = m_gt[0] - m_gt[1] / 2;
1275 62 : m_gt[3] = m_gt[3] - m_gt[5] / 2;
1276 62 : bGeoTransformValid = TRUE;
1277 : }
1278 0 : else if (iZoom == 0)
1279 : {
1280 0 : bGeoTransformValid =
1281 0 : GDALReadWorldFile(GetDescription(), nullptr, m_gt.data()) ||
1282 0 : GDALReadWorldFile(GetDescription(), ".wld", m_gt.data());
1283 : }
1284 :
1285 : /* -------------------------------------------------------------------- */
1286 : /* Read wkt. */
1287 : /* -------------------------------------------------------------------- */
1288 : #ifdef MRSID_HAVE_GETWKT
1289 62 : if (!poImageReader->isGeoCoordImplicit())
1290 : {
1291 62 : const LTIGeoCoord &oGeo = poImageReader->getGeoCoord();
1292 :
1293 62 : if (oGeo.getWKT())
1294 : {
1295 : /* Workaround probable issue with GeoDSK 7 on 64bit Linux */
1296 114 : if (!(m_oSRS.IsEmpty() && !m_oSRS.IsLocal() &&
1297 52 : STARTS_WITH_CI(oGeo.getWKT(), "LOCAL_CS")))
1298 : {
1299 62 : m_oSRS.importFromWkt(oGeo.getWKT());
1300 : }
1301 : }
1302 : }
1303 : #endif // HAVE_MRSID_GETWKT
1304 :
1305 : /* -------------------------------------------------------------------- */
1306 : /* Special case for https://zulu.ssc.nasa.gov/mrsid/mrsid.pl */
1307 : /* where LandSat .SID are accompanied by a .met file with the */
1308 : /* projection */
1309 : /* -------------------------------------------------------------------- */
1310 62 : if (iZoom == 0 && m_oSRS.IsEmpty() &&
1311 62 : EQUAL(CPLGetExtensionSafe(GetDescription()).c_str(), "sid"))
1312 : {
1313 : const std::string l_osMETFilename =
1314 0 : CPLResetExtensionSafe(GetDescription(), "met");
1315 0 : VSILFILE *fp = VSIFOpenL(l_osMETFilename.c_str(), "rb");
1316 0 : if (fp)
1317 : {
1318 0 : const char *pszLine = nullptr;
1319 0 : int nCountLine = 0;
1320 0 : int nUTMZone = 0;
1321 0 : int bWGS84 = FALSE;
1322 0 : int bUnitsMeter = FALSE;
1323 0 : while ((pszLine = CPLReadLine2L(fp, 200, nullptr)) != nullptr &&
1324 : nCountLine < 1000)
1325 : {
1326 0 : ++nCountLine;
1327 0 : if (nCountLine == 1 && strcmp(pszLine, "::MetadataFile") != 0)
1328 0 : break;
1329 0 : if (STARTS_WITH_CI(pszLine, "Projection UTM "))
1330 0 : nUTMZone = atoi(pszLine + 15);
1331 0 : else if (EQUAL(pszLine, "Datum WGS84"))
1332 0 : bWGS84 = TRUE;
1333 0 : else if (EQUAL(pszLine, "Units Meters"))
1334 0 : bUnitsMeter = TRUE;
1335 : }
1336 0 : VSIFCloseL(fp);
1337 :
1338 : /* Images in southern hemisphere have negative northings in the */
1339 : /* .sdw file. A bit weird, but anyway we must use the northern */
1340 : /* UTM SRS for consistency */
1341 0 : if (nUTMZone >= 1 && nUTMZone <= 60 && bWGS84 && bUnitsMeter)
1342 : {
1343 0 : osMETFilename = l_osMETFilename;
1344 :
1345 0 : m_oSRS.importFromEPSG(32600 + nUTMZone);
1346 0 : m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1347 : }
1348 : }
1349 : }
1350 :
1351 : /* -------------------------------------------------------------------- */
1352 : /* Read NoData value. */
1353 : /* -------------------------------------------------------------------- */
1354 62 : poNDPixel = poImageReader->getNoDataPixel();
1355 :
1356 : /* -------------------------------------------------------------------- */
1357 : /* Create band information objects. */
1358 : /* -------------------------------------------------------------------- */
1359 : int iBand;
1360 :
1361 124 : for (iBand = 1; iBand <= nBands; iBand++)
1362 62 : SetBand(iBand, new MrSIDRasterBand(this, iBand));
1363 :
1364 62 : return CE_None;
1365 : }
1366 :
1367 : /************************************************************************/
1368 : /* MrSIDOpen() */
1369 : /* */
1370 : /* Open method that only supports MrSID files. */
1371 : /************************************************************************/
1372 :
1373 12 : static GDALDataset *MrSIDOpen(GDALOpenInfo *poOpenInfo)
1374 : {
1375 12 : if (!MrSIDIdentify(poOpenInfo))
1376 0 : return nullptr;
1377 :
1378 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
1379 : lt_uint8 gen;
1380 : bool raster;
1381 24 : LT_STATUS eStat = MrSIDImageReaderInterface::getMrSIDGeneration(
1382 12 : poOpenInfo->pabyHeader, gen, raster);
1383 12 : if (!LT_SUCCESS(eStat) || !raster)
1384 0 : return nullptr;
1385 : #endif
1386 :
1387 12 : return MrSIDDataset::Open(poOpenInfo, FALSE);
1388 : }
1389 :
1390 : #ifdef MRSID_J2K
1391 :
1392 : /************************************************************************/
1393 : /* JP2Open() */
1394 : /* */
1395 : /* Open method that only supports JPEG2000 files. */
1396 : /************************************************************************/
1397 :
1398 : static GDALDataset *JP2Open(GDALOpenInfo *poOpenInfo)
1399 : {
1400 : if (!MrSIDJP2Identify(poOpenInfo))
1401 : return nullptr;
1402 :
1403 : return MrSIDDataset::Open(poOpenInfo, TRUE);
1404 : }
1405 :
1406 : #endif // MRSID_J2K
1407 :
1408 : /************************************************************************/
1409 : /* Open() */
1410 : /************************************************************************/
1411 :
1412 12 : GDALDataset *MrSIDDataset::Open(GDALOpenInfo *poOpenInfo, int bIsJP2)
1413 : {
1414 12 : if (poOpenInfo->fpL)
1415 : {
1416 12 : VSIFCloseL(poOpenInfo->fpL);
1417 12 : poOpenInfo->fpL = nullptr;
1418 : }
1419 :
1420 : /* -------------------------------------------------------------------- */
1421 : /* Make sure we have hooked CSV lookup for GDAL_DATA. */
1422 : /* -------------------------------------------------------------------- */
1423 12 : LibgeotiffOneTimeInit();
1424 :
1425 : /* -------------------------------------------------------------------- */
1426 : /* Create a corresponding GDALDataset. */
1427 : /* -------------------------------------------------------------------- */
1428 : LT_STATUS eStat;
1429 :
1430 12 : MrSIDDataset *poDS = new MrSIDDataset(bIsJP2);
1431 :
1432 : // try the LTIOFileStream first, since it uses filesystem caching
1433 12 : eStat = poDS->oLTIStream.initialize(poOpenInfo->pszFilename, "rb");
1434 12 : if (LT_SUCCESS(eStat))
1435 : {
1436 12 : eStat = poDS->oLTIStream.open();
1437 12 : if (LT_SUCCESS(eStat))
1438 9 : poDS->poStream = &(poDS->oLTIStream);
1439 : }
1440 :
1441 : // fall back on VSI for non-files
1442 12 : if (!LT_SUCCESS(eStat) || !poDS->poStream)
1443 : {
1444 3 : eStat = poDS->oVSIStream.initialize(poOpenInfo->pszFilename, "rb");
1445 3 : if (!LT_SUCCESS(eStat))
1446 : {
1447 0 : CPLError(CE_Failure, CPLE_AppDefined,
1448 : "LTIVSIStream::initialize(): "
1449 : "failed to open file \"%s\".\n%s",
1450 : poOpenInfo->pszFilename, getLastStatusString(eStat));
1451 0 : delete poDS;
1452 0 : return nullptr;
1453 : }
1454 :
1455 3 : eStat = poDS->oVSIStream.open();
1456 3 : if (!LT_SUCCESS(eStat))
1457 : {
1458 0 : CPLError(CE_Failure, CPLE_AppDefined,
1459 : "LTIVSIStream::open(): "
1460 : "failed to open file \"%s\".\n%s",
1461 : poOpenInfo->pszFilename, getLastStatusString(eStat));
1462 0 : delete poDS;
1463 0 : return nullptr;
1464 : }
1465 :
1466 3 : poDS->poStream = &(poDS->oVSIStream);
1467 : }
1468 :
1469 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 7
1470 :
1471 : #ifdef MRSID_J2K
1472 : if (bIsJP2)
1473 : {
1474 : J2KImageReader *reader = J2KImageReader::create();
1475 : eStat = reader->initialize(*(poDS->poStream));
1476 : poDS->poImageReader = reader;
1477 : }
1478 : else
1479 : #endif /* MRSID_J2K */
1480 : {
1481 12 : MrSIDImageReader *reader = MrSIDImageReader::create();
1482 12 : eStat = reader->initialize(poDS->poStream, nullptr);
1483 12 : poDS->poImageReader = reader;
1484 : }
1485 :
1486 : #else /* LTI_SDK_MAJOR < 7 */
1487 :
1488 : #ifdef MRSID_J2K
1489 : if (bIsJP2)
1490 : {
1491 : poDS->poImageReader =
1492 : new LTIDLLReader<J2KImageReader>(*(poDS->poStream), true);
1493 : eStat = poDS->poImageReader->initialize();
1494 : }
1495 : else
1496 : #endif /* MRSID_J2K */
1497 : {
1498 : poDS->poImageReader =
1499 : new LTIDLLReader<MrSIDImageReader>(poDS->poStream, nullptr);
1500 : eStat = poDS->poImageReader->initialize();
1501 : }
1502 :
1503 : #endif /* LTI_SDK_MAJOR >= 7 */
1504 :
1505 12 : if (!LT_SUCCESS(eStat))
1506 : {
1507 0 : CPLError(CE_Failure, CPLE_AppDefined,
1508 : "LTIImageReader::initialize(): "
1509 : "failed to initialize reader from the stream \"%s\".\n%s",
1510 : poOpenInfo->pszFilename, getLastStatusString(eStat));
1511 0 : delete poDS;
1512 0 : return nullptr;
1513 : }
1514 :
1515 : /* -------------------------------------------------------------------- */
1516 : /* Read metadata. */
1517 : /* -------------------------------------------------------------------- */
1518 12 : poDS->poMetadata =
1519 12 : new LTIDLLCopy<LTIMetadataDatabase>(poDS->poImageReader->getMetadata());
1520 12 : const GUInt32 iNumRecs = poDS->poMetadata->getIndexCount();
1521 :
1522 384 : for (GUInt32 i = 0; i < iNumRecs; i++)
1523 : {
1524 372 : const LTIMetadataRecord *poMetadataRec = nullptr;
1525 372 : if (LT_SUCCESS(poDS->poMetadata->getDataByIndex(i, poMetadataRec)))
1526 : {
1527 744 : const auto osElement = SerializeMetadataRec(poMetadataRec);
1528 372 : char *pszKey = CPLStrdup(poMetadataRec->getTagName());
1529 372 : char *pszTemp = pszKey;
1530 :
1531 : // GDAL metadata keys should not contain ':' and '=' characters.
1532 : // We will replace them with '_'.
1533 11612 : do
1534 : {
1535 11984 : if (*pszTemp == ':' || *pszTemp == '=')
1536 1044 : *pszTemp = '_';
1537 11984 : } while (*++pszTemp);
1538 :
1539 372 : poDS->SetMetadataItem(pszKey, osElement.c_str());
1540 :
1541 372 : CPLFree(pszKey);
1542 : }
1543 : }
1544 :
1545 : /* -------------------------------------------------------------------- */
1546 : /* Add MrSID version. */
1547 : /* -------------------------------------------------------------------- */
1548 : #ifdef MRSID_J2K
1549 : if (!bIsJP2)
1550 : #endif
1551 : {
1552 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
1553 : lt_uint8 gen;
1554 : bool raster;
1555 12 : MrSIDImageReaderInterface::getMrSIDGeneration(poOpenInfo->pabyHeader,
1556 : gen, raster);
1557 12 : poDS->SetMetadataItem(
1558 : "VERSION",
1559 24 : CPLString().Printf("MG%d%s", gen, raster ? "" : " LiDAR"));
1560 : #else
1561 : lt_uint8 major;
1562 : lt_uint8 minor;
1563 : char letter;
1564 : MrSIDImageReader *poMrSIDImageReader =
1565 : static_cast<MrSIDImageReader *>(poDS->poImageReader);
1566 : poMrSIDImageReader->getVersion(major, minor, minor, letter);
1567 : if (major < 2)
1568 : major = 2;
1569 : poDS->SetMetadataItem("VERSION", CPLString().Printf("MG%d", major));
1570 : #endif
1571 : }
1572 :
1573 12 : poDS->GetGTIFDefn();
1574 :
1575 : /* -------------------------------------------------------------------- */
1576 : /* Get number of resolution levels (we will use them as overviews).*/
1577 : /* -------------------------------------------------------------------- */
1578 : #ifdef MRSID_J2K
1579 : if (bIsJP2)
1580 : poDS->nOverviewCount =
1581 : static_cast<J2KImageReader *>(poDS->poImageReader)->getNumLevels();
1582 : else
1583 : #endif
1584 12 : poDS->nOverviewCount =
1585 12 : static_cast<MrSIDImageReader *>(poDS->poImageReader)
1586 12 : ->getNumLevels();
1587 :
1588 12 : if (poDS->nOverviewCount > 0)
1589 : {
1590 : lt_int32 i;
1591 :
1592 12 : poDS->papoOverviewDS =
1593 12 : (MrSIDDataset **)CPLMalloc(poDS->nOverviewCount * (sizeof(void *)));
1594 :
1595 62 : for (i = 0; i < poDS->nOverviewCount; i++)
1596 : {
1597 50 : poDS->papoOverviewDS[i] = new MrSIDDataset(bIsJP2);
1598 50 : poDS->papoOverviewDS[i]->poImageReader = poDS->poImageReader;
1599 50 : poDS->papoOverviewDS[i]->bIsOverview = TRUE;
1600 50 : poDS->papoOverviewDS[i]->poParentDS = poDS;
1601 50 : if (poDS->papoOverviewDS[i]->OpenZoomLevel(i + 1) != CE_None)
1602 : {
1603 0 : delete poDS->papoOverviewDS[i];
1604 0 : poDS->nOverviewCount = i;
1605 0 : break;
1606 : }
1607 : }
1608 : }
1609 :
1610 : /* -------------------------------------------------------------------- */
1611 : /* Create object for the whole image. */
1612 : /* -------------------------------------------------------------------- */
1613 12 : poDS->SetDescription(poOpenInfo->pszFilename);
1614 12 : if (poDS->OpenZoomLevel(0) != CE_None)
1615 : {
1616 0 : delete poDS;
1617 0 : return nullptr;
1618 : }
1619 :
1620 12 : CPLDebug("MrSID", "Opened image: width %d, height %d, bands %d",
1621 : poDS->nRasterXSize, poDS->nRasterYSize, poDS->nBands);
1622 :
1623 12 : if (poDS->nBands > 1)
1624 0 : poDS->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
1625 :
1626 12 : if (bIsJP2)
1627 : {
1628 0 : poDS->LoadJP2Metadata(poOpenInfo);
1629 : }
1630 :
1631 : /* -------------------------------------------------------------------- */
1632 : /* Initialize any PAM information. */
1633 : /* -------------------------------------------------------------------- */
1634 12 : poDS->TryLoadXML();
1635 :
1636 : /* -------------------------------------------------------------------- */
1637 : /* Initialize the overview manager for mask band support. */
1638 : /* -------------------------------------------------------------------- */
1639 12 : poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename);
1640 :
1641 12 : return poDS;
1642 : }
1643 :
1644 : /************************************************************************/
1645 : /* EPSGProjMethodToCTProjMethod() */
1646 : /* */
1647 : /* Convert between the EPSG enumeration for projection methods, */
1648 : /* and the GeoTIFF CT codes. */
1649 : /* Explicitly copied from geo_normalize.c of the GeoTIFF package */
1650 : /************************************************************************/
1651 :
1652 0 : static int EPSGProjMethodToCTProjMethod(int nEPSG)
1653 :
1654 : {
1655 : /* see trf_method.csv for list of EPSG codes */
1656 :
1657 0 : switch (nEPSG)
1658 : {
1659 0 : case 9801:
1660 0 : return CT_LambertConfConic_1SP;
1661 :
1662 0 : case 9802:
1663 0 : return CT_LambertConfConic_2SP;
1664 :
1665 0 : case 9803:
1666 0 : return CT_LambertConfConic_2SP; // Belgian variant not supported.
1667 :
1668 0 : case 9804:
1669 0 : return CT_Mercator; // 1SP and 2SP not differentiated.
1670 :
1671 0 : case 9805:
1672 0 : return CT_Mercator; // 1SP and 2SP not differentiated.
1673 :
1674 0 : case 9806:
1675 0 : return CT_CassiniSoldner;
1676 :
1677 0 : case 9807:
1678 0 : return CT_TransverseMercator;
1679 :
1680 0 : case 9808:
1681 0 : return CT_TransvMercator_SouthOriented;
1682 :
1683 0 : case 9809:
1684 0 : return CT_ObliqueStereographic;
1685 :
1686 0 : case 9810:
1687 0 : return CT_PolarStereographic;
1688 :
1689 0 : case 9811:
1690 0 : return CT_NewZealandMapGrid;
1691 :
1692 0 : case 9812:
1693 0 : return CT_ObliqueMercator; // Is hotine actually different?
1694 :
1695 0 : case 9813:
1696 0 : return CT_ObliqueMercator_Laborde;
1697 :
1698 0 : case 9814:
1699 0 : return CT_ObliqueMercator_Rosenmund; // Swiss.
1700 :
1701 0 : case 9815:
1702 0 : return CT_ObliqueMercator;
1703 :
1704 0 : case 9816: /* tunesia mining grid has no counterpart */
1705 0 : return KvUserDefined;
1706 : }
1707 :
1708 0 : return KvUserDefined;
1709 : }
1710 :
1711 : /* EPSG Codes for projection parameters. Unfortunately, these bear no
1712 : relationship to the GeoTIFF codes even though the names are so similar. */
1713 :
1714 : #define EPSGNatOriginLat 8801
1715 : #define EPSGNatOriginLong 8802
1716 : #define EPSGNatOriginScaleFactor 8805
1717 : #define EPSGFalseEasting 8806
1718 : #define EPSGFalseNorthing 8807
1719 : #define EPSGProjCenterLat 8811
1720 : #define EPSGProjCenterLong 8812
1721 : #define EPSGAzimuth 8813
1722 : #define EPSGAngleRectifiedToSkewedGrid 8814
1723 : #define EPSGInitialLineScaleFactor 8815
1724 : #define EPSGProjCenterEasting 8816
1725 : #define EPSGProjCenterNorthing 8817
1726 : #define EPSGPseudoStdParallelLat 8818
1727 : #define EPSGPseudoStdParallelScaleFactor 8819
1728 : #define EPSGFalseOriginLat 8821
1729 : #define EPSGFalseOriginLong 8822
1730 : #define EPSGStdParallel1Lat 8823
1731 : #define EPSGStdParallel2Lat 8824
1732 : #define EPSGFalseOriginEasting 8826
1733 : #define EPSGFalseOriginNorthing 8827
1734 : #define EPSGSphericalOriginLat 8828
1735 : #define EPSGSphericalOriginLong 8829
1736 : #define EPSGInitialLongitude 8830
1737 : #define EPSGZoneWidth 8831
1738 :
1739 : /************************************************************************/
1740 : /* SetGTParamIds() */
1741 : /* */
1742 : /* This is hardcoded logic to set the GeoTIFF parameter */
1743 : /* identifiers for all the EPSG supported projections. As the */
1744 : /* trf_method.csv table grows with new projections, this code */
1745 : /* will need to be updated. */
1746 : /* Explicitly copied from geo_normalize.c of the GeoTIFF package. */
1747 : /************************************************************************/
1748 :
1749 0 : static int SetGTParamIds(int nCTProjection, int *panProjParamId,
1750 : int *panEPSGCodes)
1751 :
1752 : {
1753 : int anWorkingDummy[7];
1754 :
1755 0 : if (panEPSGCodes == nullptr)
1756 0 : panEPSGCodes = anWorkingDummy;
1757 0 : if (panProjParamId == nullptr)
1758 0 : panProjParamId = anWorkingDummy;
1759 :
1760 0 : memset(panEPSGCodes, 0, sizeof(int) * 7);
1761 :
1762 : /* psDefn->nParms = 7; */
1763 :
1764 0 : switch (nCTProjection)
1765 : {
1766 0 : case CT_CassiniSoldner:
1767 : case CT_NewZealandMapGrid:
1768 0 : panProjParamId[0] = ProjNatOriginLatGeoKey;
1769 0 : panProjParamId[1] = ProjNatOriginLongGeoKey;
1770 0 : panProjParamId[5] = ProjFalseEastingGeoKey;
1771 0 : panProjParamId[6] = ProjFalseNorthingGeoKey;
1772 :
1773 0 : panEPSGCodes[0] = EPSGNatOriginLat;
1774 0 : panEPSGCodes[1] = EPSGNatOriginLong;
1775 0 : panEPSGCodes[5] = EPSGFalseEasting;
1776 0 : panEPSGCodes[6] = EPSGFalseNorthing;
1777 0 : return TRUE;
1778 :
1779 0 : case CT_ObliqueMercator:
1780 0 : panProjParamId[0] = ProjCenterLatGeoKey;
1781 0 : panProjParamId[1] = ProjCenterLongGeoKey;
1782 0 : panProjParamId[2] = ProjAzimuthAngleGeoKey;
1783 0 : panProjParamId[3] = ProjRectifiedGridAngleGeoKey;
1784 0 : panProjParamId[4] = ProjScaleAtCenterGeoKey;
1785 0 : panProjParamId[5] = ProjFalseEastingGeoKey;
1786 0 : panProjParamId[6] = ProjFalseNorthingGeoKey;
1787 :
1788 0 : panEPSGCodes[0] = EPSGProjCenterLat;
1789 0 : panEPSGCodes[1] = EPSGProjCenterLong;
1790 0 : panEPSGCodes[2] = EPSGAzimuth;
1791 0 : panEPSGCodes[3] = EPSGAngleRectifiedToSkewedGrid;
1792 0 : panEPSGCodes[4] = EPSGInitialLineScaleFactor;
1793 0 : panEPSGCodes[5] = EPSGProjCenterEasting;
1794 0 : panEPSGCodes[6] = EPSGProjCenterNorthing;
1795 0 : return TRUE;
1796 :
1797 0 : case CT_ObliqueMercator_Laborde:
1798 0 : panProjParamId[0] = ProjCenterLatGeoKey;
1799 0 : panProjParamId[1] = ProjCenterLongGeoKey;
1800 0 : panProjParamId[2] = ProjAzimuthAngleGeoKey;
1801 0 : panProjParamId[4] = ProjScaleAtCenterGeoKey;
1802 0 : panProjParamId[5] = ProjFalseEastingGeoKey;
1803 0 : panProjParamId[6] = ProjFalseNorthingGeoKey;
1804 :
1805 0 : panEPSGCodes[0] = EPSGProjCenterLat;
1806 0 : panEPSGCodes[1] = EPSGProjCenterLong;
1807 0 : panEPSGCodes[2] = EPSGAzimuth;
1808 0 : panEPSGCodes[4] = EPSGInitialLineScaleFactor;
1809 0 : panEPSGCodes[5] = EPSGProjCenterEasting;
1810 0 : panEPSGCodes[6] = EPSGProjCenterNorthing;
1811 0 : return TRUE;
1812 :
1813 0 : case CT_LambertConfConic_1SP:
1814 : case CT_Mercator:
1815 : case CT_ObliqueStereographic:
1816 : case CT_PolarStereographic:
1817 : case CT_TransverseMercator:
1818 : case CT_TransvMercator_SouthOriented:
1819 0 : panProjParamId[0] = ProjNatOriginLatGeoKey;
1820 0 : panProjParamId[1] = ProjNatOriginLongGeoKey;
1821 0 : panProjParamId[4] = ProjScaleAtNatOriginGeoKey;
1822 0 : panProjParamId[5] = ProjFalseEastingGeoKey;
1823 0 : panProjParamId[6] = ProjFalseNorthingGeoKey;
1824 :
1825 0 : panEPSGCodes[0] = EPSGNatOriginLat;
1826 0 : panEPSGCodes[1] = EPSGNatOriginLong;
1827 0 : panEPSGCodes[4] = EPSGNatOriginScaleFactor;
1828 0 : panEPSGCodes[5] = EPSGFalseEasting;
1829 0 : panEPSGCodes[6] = EPSGFalseNorthing;
1830 0 : return TRUE;
1831 :
1832 0 : case CT_LambertConfConic_2SP:
1833 0 : panProjParamId[0] = ProjFalseOriginLatGeoKey;
1834 0 : panProjParamId[1] = ProjFalseOriginLongGeoKey;
1835 0 : panProjParamId[2] = ProjStdParallel1GeoKey;
1836 0 : panProjParamId[3] = ProjStdParallel2GeoKey;
1837 0 : panProjParamId[5] = ProjFalseEastingGeoKey;
1838 0 : panProjParamId[6] = ProjFalseNorthingGeoKey;
1839 :
1840 0 : panEPSGCodes[0] = EPSGFalseOriginLat;
1841 0 : panEPSGCodes[1] = EPSGFalseOriginLong;
1842 0 : panEPSGCodes[2] = EPSGStdParallel1Lat;
1843 0 : panEPSGCodes[3] = EPSGStdParallel2Lat;
1844 0 : panEPSGCodes[5] = EPSGFalseOriginEasting;
1845 0 : panEPSGCodes[6] = EPSGFalseOriginNorthing;
1846 0 : return TRUE;
1847 :
1848 0 : case CT_SwissObliqueCylindrical:
1849 0 : panProjParamId[0] = ProjCenterLatGeoKey;
1850 0 : panProjParamId[1] = ProjCenterLongGeoKey;
1851 0 : panProjParamId[5] = ProjFalseEastingGeoKey;
1852 0 : panProjParamId[6] = ProjFalseNorthingGeoKey;
1853 :
1854 : /* EPSG codes? */
1855 0 : return TRUE;
1856 :
1857 0 : default:
1858 0 : return FALSE;
1859 : }
1860 : }
1861 :
1862 : static const char *const papszDatumEquiv[] = {
1863 : "Militar_Geographische_Institut",
1864 : "Militar_Geographische_Institute",
1865 : "World_Geodetic_System_1984",
1866 : "WGS_1984",
1867 : "WGS_72_Transit_Broadcast_Ephemeris",
1868 : "WGS_1972_Transit_Broadcast_Ephemeris",
1869 : "World_Geodetic_System_1972",
1870 : "WGS_1972",
1871 : "European_Terrestrial_Reference_System_89",
1872 : "European_Reference_System_1989",
1873 : nullptr};
1874 :
1875 : /************************************************************************/
1876 : /* WKTMassageDatum() */
1877 : /* */
1878 : /* Massage an EPSG datum name into WMT format. Also transform */
1879 : /* specific exception cases into WKT versions. */
1880 : /* Explicitly copied from the gt_wkt_srs.cpp. */
1881 : /************************************************************************/
1882 :
1883 10 : static void WKTMassageDatum(char **ppszDatum)
1884 :
1885 : {
1886 : int i, j;
1887 10 : char *pszDatum = *ppszDatum;
1888 :
1889 10 : if (pszDatum[0] == '\0')
1890 0 : return;
1891 :
1892 : /* -------------------------------------------------------------------- */
1893 : /* Translate non-alphanumeric values to underscores. */
1894 : /* -------------------------------------------------------------------- */
1895 260 : for (i = 0; pszDatum[i] != '\0'; i++)
1896 : {
1897 250 : if (!(pszDatum[i] >= 'A' && pszDatum[i] <= 'Z') &&
1898 220 : !(pszDatum[i] >= 'a' && pszDatum[i] <= 'z') &&
1899 70 : !(pszDatum[i] >= '0' && pszDatum[i] <= '9'))
1900 : {
1901 30 : pszDatum[i] = '_';
1902 : }
1903 : }
1904 :
1905 : /* -------------------------------------------------------------------- */
1906 : /* Remove repeated and trailing underscores. */
1907 : /* -------------------------------------------------------------------- */
1908 250 : for (i = 1, j = 0; pszDatum[i] != '\0'; i++)
1909 : {
1910 240 : if (pszDatum[j] == '_' && pszDatum[i] == '_')
1911 0 : continue;
1912 :
1913 240 : pszDatum[++j] = pszDatum[i];
1914 : }
1915 10 : if (pszDatum[j] == '_')
1916 0 : pszDatum[j] = '\0';
1917 : else
1918 10 : pszDatum[j + 1] = '\0';
1919 :
1920 : /* -------------------------------------------------------------------- */
1921 : /* Search for datum equivalences. Specific massaged names get */
1922 : /* mapped to OpenGIS specified names. */
1923 : /* -------------------------------------------------------------------- */
1924 60 : for (i = 0; papszDatumEquiv[i] != nullptr; i += 2)
1925 : {
1926 50 : if (EQUAL(*ppszDatum, papszDatumEquiv[i]))
1927 : {
1928 0 : CPLFree(*ppszDatum);
1929 0 : *ppszDatum = CPLStrdup(papszDatumEquiv[i + 1]);
1930 0 : return;
1931 : }
1932 : }
1933 : }
1934 :
1935 : /************************************************************************/
1936 : /* FetchProjParams() */
1937 : /* */
1938 : /* Fetch the projection parameters for a particular projection */
1939 : /* from MrSID metadata, and fill the GTIFDefn structure out */
1940 : /* with them. */
1941 : /* Copied from geo_normalize.c of the GeoTIFF package. */
1942 : /************************************************************************/
1943 :
1944 10 : void MrSIDDataset::FetchProjParams()
1945 : {
1946 10 : double dfNatOriginLong = 0.0, dfNatOriginLat = 0.0, dfRectGridAngle = 0.0;
1947 10 : double dfFalseEasting = 0.0, dfFalseNorthing = 0.0, dfNatOriginScale = 1.0;
1948 10 : double dfStdParallel1 = 0.0, dfStdParallel2 = 0.0, dfAzimuth = 0.0;
1949 :
1950 : /* -------------------------------------------------------------------- */
1951 : /* Get the false easting, and northing if available. */
1952 : /* -------------------------------------------------------------------- */
1953 10 : if (!GetMetadataElement("GEOTIFF_NUM::3082::ProjFalseEastingGeoKey",
1954 20 : &dfFalseEasting) &&
1955 10 : !GetMetadataElement("GEOTIFF_NUM::3090:ProjCenterEastingGeoKey",
1956 : &dfFalseEasting))
1957 10 : dfFalseEasting = 0.0;
1958 :
1959 10 : if (!GetMetadataElement("GEOTIFF_NUM::3083::ProjFalseNorthingGeoKey",
1960 20 : &dfFalseNorthing) &&
1961 10 : !GetMetadataElement("GEOTIFF_NUM::3091::ProjCenterNorthingGeoKey",
1962 : &dfFalseNorthing))
1963 10 : dfFalseNorthing = 0.0;
1964 :
1965 10 : switch (psDefn->CTProjection)
1966 : {
1967 : /* --------------------------------------------------------------------
1968 : */
1969 0 : case CT_Stereographic:
1970 : /* --------------------------------------------------------------------
1971 : */
1972 0 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
1973 0 : &dfNatOriginLong) == 0 &&
1974 0 : GetMetadataElement(
1975 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
1976 0 : &dfNatOriginLong) == 0 &&
1977 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
1978 : &dfNatOriginLong) == 0)
1979 0 : dfNatOriginLong = 0.0;
1980 :
1981 0 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
1982 0 : &dfNatOriginLat) == 0 &&
1983 0 : GetMetadataElement(
1984 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
1985 0 : &dfNatOriginLat) == 0 &&
1986 0 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
1987 : &dfNatOriginLat) == 0)
1988 0 : dfNatOriginLat = 0.0;
1989 :
1990 0 : if (GetMetadataElement(
1991 : "GEOTIFF_NUM::3092::ProjScaleAtNatOriginGeoKey",
1992 0 : &dfNatOriginScale) == 0)
1993 0 : dfNatOriginScale = 1.0;
1994 :
1995 : /* notdef: should transform to decimal degrees at this point */
1996 :
1997 0 : psDefn->ProjParm[0] = dfNatOriginLat;
1998 0 : psDefn->ProjParmId[0] = ProjCenterLatGeoKey;
1999 0 : psDefn->ProjParm[1] = dfNatOriginLong;
2000 0 : psDefn->ProjParmId[1] = ProjCenterLongGeoKey;
2001 0 : psDefn->ProjParm[4] = dfNatOriginScale;
2002 0 : psDefn->ProjParmId[4] = ProjScaleAtNatOriginGeoKey;
2003 0 : psDefn->ProjParm[5] = dfFalseEasting;
2004 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2005 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2006 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2007 :
2008 0 : psDefn->nParms = 7;
2009 0 : break;
2010 :
2011 : /* --------------------------------------------------------------------
2012 : */
2013 10 : case CT_LambertConfConic_1SP:
2014 : case CT_Mercator:
2015 : case CT_ObliqueStereographic:
2016 : case CT_TransverseMercator:
2017 : case CT_TransvMercator_SouthOriented:
2018 : /* --------------------------------------------------------------------
2019 : */
2020 10 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2021 10 : &dfNatOriginLong) == 0 &&
2022 10 : GetMetadataElement(
2023 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2024 20 : &dfNatOriginLong) == 0 &&
2025 10 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2026 : &dfNatOriginLong) == 0)
2027 10 : dfNatOriginLong = 0.0;
2028 :
2029 10 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
2030 10 : &dfNatOriginLat) == 0 &&
2031 10 : GetMetadataElement(
2032 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
2033 20 : &dfNatOriginLat) == 0 &&
2034 10 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
2035 : &dfNatOriginLat) == 0)
2036 10 : dfNatOriginLat = 0.0;
2037 :
2038 10 : if (GetMetadataElement(
2039 : "GEOTIFF_NUM::3092::ProjScaleAtNatOriginGeoKey",
2040 10 : &dfNatOriginScale) == 0)
2041 10 : dfNatOriginScale = 1.0;
2042 :
2043 : /* notdef: should transform to decimal degrees at this point */
2044 :
2045 10 : psDefn->ProjParm[0] = dfNatOriginLat;
2046 10 : psDefn->ProjParmId[0] = ProjNatOriginLatGeoKey;
2047 10 : psDefn->ProjParm[1] = dfNatOriginLong;
2048 10 : psDefn->ProjParmId[1] = ProjNatOriginLongGeoKey;
2049 10 : psDefn->ProjParm[4] = dfNatOriginScale;
2050 10 : psDefn->ProjParmId[4] = ProjScaleAtNatOriginGeoKey;
2051 10 : psDefn->ProjParm[5] = dfFalseEasting;
2052 10 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2053 10 : psDefn->ProjParm[6] = dfFalseNorthing;
2054 10 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2055 :
2056 10 : psDefn->nParms = 7;
2057 10 : break;
2058 :
2059 : /* --------------------------------------------------------------------
2060 : */
2061 0 : case CT_ObliqueMercator: /* hotine */
2062 : /* --------------------------------------------------------------------
2063 : */
2064 0 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2065 0 : &dfNatOriginLong) == 0 &&
2066 0 : GetMetadataElement(
2067 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2068 0 : &dfNatOriginLong) == 0 &&
2069 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2070 : &dfNatOriginLong) == 0)
2071 0 : dfNatOriginLong = 0.0;
2072 :
2073 0 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
2074 0 : &dfNatOriginLat) == 0 &&
2075 0 : GetMetadataElement(
2076 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
2077 0 : &dfNatOriginLat) == 0 &&
2078 0 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
2079 : &dfNatOriginLat) == 0)
2080 0 : dfNatOriginLat = 0.0;
2081 :
2082 0 : if (GetMetadataElement("GEOTIFF_NUM::3094::ProjAzimuthAngleGeoKey",
2083 0 : &dfAzimuth) == 0)
2084 0 : dfAzimuth = 0.0;
2085 :
2086 0 : if (GetMetadataElement(
2087 : "GEOTIFF_NUM::3096::ProjRectifiedGridAngleGeoKey",
2088 0 : &dfRectGridAngle) == 0)
2089 0 : dfRectGridAngle = 90.0;
2090 :
2091 0 : if (GetMetadataElement(
2092 : "GEOTIFF_NUM::3092::ProjScaleAtNatOriginGeoKey",
2093 0 : &dfNatOriginScale) == 0 &&
2094 0 : GetMetadataElement("GEOTIFF_NUM::3093::ProjScaleAtCenterGeoKey",
2095 : &dfNatOriginScale) == 0)
2096 0 : dfNatOriginScale = 1.0;
2097 :
2098 : /* notdef: should transform to decimal degrees at this point */
2099 :
2100 0 : psDefn->ProjParm[0] = dfNatOriginLat;
2101 0 : psDefn->ProjParmId[0] = ProjCenterLatGeoKey;
2102 0 : psDefn->ProjParm[1] = dfNatOriginLong;
2103 0 : psDefn->ProjParmId[1] = ProjCenterLongGeoKey;
2104 0 : psDefn->ProjParm[2] = dfAzimuth;
2105 0 : psDefn->ProjParmId[2] = ProjAzimuthAngleGeoKey;
2106 0 : psDefn->ProjParm[3] = dfRectGridAngle;
2107 0 : psDefn->ProjParmId[3] = ProjRectifiedGridAngleGeoKey;
2108 0 : psDefn->ProjParm[4] = dfNatOriginScale;
2109 0 : psDefn->ProjParmId[4] = ProjScaleAtCenterGeoKey;
2110 0 : psDefn->ProjParm[5] = dfFalseEasting;
2111 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2112 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2113 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2114 :
2115 0 : psDefn->nParms = 7;
2116 0 : break;
2117 :
2118 : /* --------------------------------------------------------------------
2119 : */
2120 0 : case CT_CassiniSoldner:
2121 : case CT_Polyconic:
2122 : /* --------------------------------------------------------------------
2123 : */
2124 0 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2125 0 : &dfNatOriginLong) == 0 &&
2126 0 : GetMetadataElement(
2127 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2128 0 : &dfNatOriginLong) == 0 &&
2129 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2130 : &dfNatOriginLong) == 0)
2131 0 : dfNatOriginLong = 0.0;
2132 :
2133 0 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
2134 0 : &dfNatOriginLat) == 0 &&
2135 0 : GetMetadataElement(
2136 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
2137 0 : &dfNatOriginLat) == 0 &&
2138 0 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
2139 : &dfNatOriginLat) == 0)
2140 0 : dfNatOriginLat = 0.0;
2141 :
2142 0 : if (GetMetadataElement(
2143 : "GEOTIFF_NUM::3092::ProjScaleAtNatOriginGeoKey",
2144 0 : &dfNatOriginScale) == 0 &&
2145 0 : GetMetadataElement("GEOTIFF_NUM::3093::ProjScaleAtCenterGeoKey",
2146 : &dfNatOriginScale) == 0)
2147 0 : dfNatOriginScale = 1.0;
2148 :
2149 : /* notdef: should transform to decimal degrees at this point */
2150 :
2151 0 : psDefn->ProjParm[0] = dfNatOriginLat;
2152 0 : psDefn->ProjParmId[0] = ProjNatOriginLatGeoKey;
2153 0 : psDefn->ProjParm[1] = dfNatOriginLong;
2154 0 : psDefn->ProjParmId[1] = ProjNatOriginLongGeoKey;
2155 0 : psDefn->ProjParm[4] = dfNatOriginScale;
2156 0 : psDefn->ProjParmId[4] = ProjScaleAtNatOriginGeoKey;
2157 0 : psDefn->ProjParm[5] = dfFalseEasting;
2158 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2159 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2160 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2161 :
2162 0 : psDefn->nParms = 7;
2163 0 : break;
2164 :
2165 : /* --------------------------------------------------------------------
2166 : */
2167 0 : case CT_AzimuthalEquidistant:
2168 : case CT_MillerCylindrical:
2169 : case CT_Equirectangular:
2170 : case CT_Gnomonic:
2171 : case CT_LambertAzimEqualArea:
2172 : case CT_Orthographic:
2173 : /* --------------------------------------------------------------------
2174 : */
2175 0 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2176 0 : &dfNatOriginLong) == 0 &&
2177 0 : GetMetadataElement(
2178 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2179 0 : &dfNatOriginLong) == 0 &&
2180 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2181 : &dfNatOriginLong) == 0)
2182 0 : dfNatOriginLong = 0.0;
2183 :
2184 0 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
2185 0 : &dfNatOriginLat) == 0 &&
2186 0 : GetMetadataElement(
2187 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
2188 0 : &dfNatOriginLat) == 0 &&
2189 0 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
2190 : &dfNatOriginLat) == 0)
2191 0 : dfNatOriginLat = 0.0;
2192 :
2193 : /* notdef: should transform to decimal degrees at this point */
2194 :
2195 0 : psDefn->ProjParm[0] = dfNatOriginLat;
2196 0 : psDefn->ProjParmId[0] = ProjCenterLatGeoKey;
2197 0 : psDefn->ProjParm[1] = dfNatOriginLong;
2198 0 : psDefn->ProjParmId[1] = ProjCenterLongGeoKey;
2199 0 : psDefn->ProjParm[5] = dfFalseEasting;
2200 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2201 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2202 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2203 :
2204 0 : psDefn->nParms = 7;
2205 0 : break;
2206 :
2207 : /* --------------------------------------------------------------------
2208 : */
2209 0 : case CT_Robinson:
2210 : case CT_Sinusoidal:
2211 : case CT_VanDerGrinten:
2212 : /* --------------------------------------------------------------------
2213 : */
2214 0 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2215 0 : &dfNatOriginLong) == 0 &&
2216 0 : GetMetadataElement(
2217 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2218 0 : &dfNatOriginLong) == 0 &&
2219 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2220 : &dfNatOriginLong) == 0)
2221 0 : dfNatOriginLong = 0.0;
2222 :
2223 : /* notdef: should transform to decimal degrees at this point */
2224 :
2225 0 : psDefn->ProjParm[1] = dfNatOriginLong;
2226 0 : psDefn->ProjParmId[1] = ProjCenterLongGeoKey;
2227 0 : psDefn->ProjParm[5] = dfFalseEasting;
2228 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2229 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2230 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2231 :
2232 0 : psDefn->nParms = 7;
2233 0 : break;
2234 :
2235 : /* --------------------------------------------------------------------
2236 : */
2237 0 : case CT_PolarStereographic:
2238 : /* --------------------------------------------------------------------
2239 : */
2240 0 : if (GetMetadataElement(
2241 : "GEOTIFF_NUM::3095::ProjStraightVertPoleLongGeoKey",
2242 0 : &dfNatOriginLong) == 0 &&
2243 0 : GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2244 0 : &dfNatOriginLong) == 0 &&
2245 0 : GetMetadataElement(
2246 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2247 0 : &dfNatOriginLong) == 0 &&
2248 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2249 : &dfNatOriginLong) == 0)
2250 0 : dfNatOriginLong = 0.0;
2251 :
2252 0 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
2253 0 : &dfNatOriginLat) == 0 &&
2254 0 : GetMetadataElement(
2255 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
2256 0 : &dfNatOriginLat) == 0 &&
2257 0 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
2258 : &dfNatOriginLat) == 0)
2259 0 : dfNatOriginLat = 0.0;
2260 :
2261 0 : if (GetMetadataElement(
2262 : "GEOTIFF_NUM::3092::ProjScaleAtNatOriginGeoKey",
2263 0 : &dfNatOriginScale) == 0 &&
2264 0 : GetMetadataElement("GEOTIFF_NUM::3093::ProjScaleAtCenterGeoKey",
2265 : &dfNatOriginScale) == 0)
2266 0 : dfNatOriginScale = 1.0;
2267 :
2268 : /* notdef: should transform to decimal degrees at this point */
2269 :
2270 0 : psDefn->ProjParm[0] = dfNatOriginLat;
2271 0 : psDefn->ProjParmId[0] = ProjNatOriginLatGeoKey;
2272 0 : psDefn->ProjParm[1] = dfNatOriginLong;
2273 0 : psDefn->ProjParmId[1] = ProjStraightVertPoleLongGeoKey;
2274 0 : psDefn->ProjParm[4] = dfNatOriginScale;
2275 0 : psDefn->ProjParmId[4] = ProjScaleAtNatOriginGeoKey;
2276 0 : psDefn->ProjParm[5] = dfFalseEasting;
2277 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2278 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2279 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2280 :
2281 0 : psDefn->nParms = 7;
2282 0 : break;
2283 :
2284 : /* --------------------------------------------------------------------
2285 : */
2286 0 : case CT_LambertConfConic_2SP:
2287 : /* --------------------------------------------------------------------
2288 : */
2289 0 : if (GetMetadataElement("GEOTIFF_NUM::3078::ProjStdParallel1GeoKey",
2290 0 : &dfStdParallel1) == 0)
2291 0 : dfStdParallel1 = 0.0;
2292 :
2293 0 : if (GetMetadataElement("GEOTIFF_NUM::3079::ProjStdParallel2GeoKey",
2294 0 : &dfStdParallel2) == 0)
2295 0 : dfStdParallel1 = 0.0;
2296 :
2297 0 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2298 0 : &dfNatOriginLong) == 0 &&
2299 0 : GetMetadataElement(
2300 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2301 0 : &dfNatOriginLong) == 0 &&
2302 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2303 : &dfNatOriginLong) == 0)
2304 0 : dfNatOriginLong = 0.0;
2305 :
2306 0 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
2307 0 : &dfNatOriginLat) == 0 &&
2308 0 : GetMetadataElement(
2309 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
2310 0 : &dfNatOriginLat) == 0 &&
2311 0 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
2312 : &dfNatOriginLat) == 0)
2313 0 : dfNatOriginLat = 0.0;
2314 :
2315 : /* notdef: should transform to decimal degrees at this point */
2316 :
2317 0 : psDefn->ProjParm[0] = dfNatOriginLat;
2318 0 : psDefn->ProjParmId[0] = ProjFalseOriginLatGeoKey;
2319 0 : psDefn->ProjParm[1] = dfNatOriginLong;
2320 0 : psDefn->ProjParmId[1] = ProjFalseOriginLongGeoKey;
2321 0 : psDefn->ProjParm[2] = dfStdParallel1;
2322 0 : psDefn->ProjParmId[2] = ProjStdParallel1GeoKey;
2323 0 : psDefn->ProjParm[3] = dfStdParallel2;
2324 0 : psDefn->ProjParmId[3] = ProjStdParallel2GeoKey;
2325 0 : psDefn->ProjParm[5] = dfFalseEasting;
2326 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2327 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2328 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2329 :
2330 0 : psDefn->nParms = 7;
2331 0 : break;
2332 :
2333 : /* --------------------------------------------------------------------
2334 : */
2335 0 : case CT_AlbersEqualArea:
2336 : case CT_EquidistantConic:
2337 : /* --------------------------------------------------------------------
2338 : */
2339 0 : if (GetMetadataElement("GEOTIFF_NUM::3078::ProjStdParallel1GeoKey",
2340 0 : &dfStdParallel1) == 0)
2341 0 : dfStdParallel1 = 0.0;
2342 :
2343 0 : if (GetMetadataElement("GEOTIFF_NUM::3079::ProjStdParallel2GeoKey",
2344 0 : &dfStdParallel2) == 0)
2345 0 : dfStdParallel1 = 0.0;
2346 :
2347 0 : if (GetMetadataElement("GEOTIFF_NUM::3080::ProjNatOriginLongGeoKey",
2348 0 : &dfNatOriginLong) == 0 &&
2349 0 : GetMetadataElement(
2350 : "GEOTIFF_NUM::3084::ProjFalseOriginLongGeoKey",
2351 0 : &dfNatOriginLong) == 0 &&
2352 0 : GetMetadataElement("GEOTIFF_NUM::3088::ProjCenterLongGeoKey",
2353 : &dfNatOriginLong) == 0)
2354 0 : dfNatOriginLong = 0.0;
2355 :
2356 0 : if (GetMetadataElement("GEOTIFF_NUM::3081::ProjNatOriginLatGeoKey",
2357 0 : &dfNatOriginLat) == 0 &&
2358 0 : GetMetadataElement(
2359 : "GEOTIFF_NUM::3085::ProjFalseOriginLatGeoKey",
2360 0 : &dfNatOriginLat) == 0 &&
2361 0 : GetMetadataElement("GEOTIFF_NUM::3089::ProjCenterLatGeoKey",
2362 : &dfNatOriginLat) == 0)
2363 0 : dfNatOriginLat = 0.0;
2364 :
2365 : /* notdef: should transform to decimal degrees at this point */
2366 :
2367 0 : psDefn->ProjParm[0] = dfStdParallel1;
2368 0 : psDefn->ProjParmId[0] = ProjStdParallel1GeoKey;
2369 0 : psDefn->ProjParm[1] = dfStdParallel2;
2370 0 : psDefn->ProjParmId[1] = ProjStdParallel2GeoKey;
2371 0 : psDefn->ProjParm[2] = dfNatOriginLat;
2372 0 : psDefn->ProjParmId[2] = ProjNatOriginLatGeoKey;
2373 0 : psDefn->ProjParm[3] = dfNatOriginLong;
2374 0 : psDefn->ProjParmId[3] = ProjNatOriginLongGeoKey;
2375 0 : psDefn->ProjParm[5] = dfFalseEasting;
2376 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2377 0 : psDefn->ProjParm[6] = dfFalseNorthing;
2378 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2379 :
2380 0 : psDefn->nParms = 7;
2381 0 : break;
2382 : }
2383 10 : }
2384 :
2385 : /************************************************************************/
2386 : /* GetGTIFDefn() */
2387 : /* This function borrowed from the GTIFGetDefn() function. */
2388 : /* See geo_normalize.c from the GeoTIFF package. */
2389 : /************************************************************************/
2390 :
2391 12 : void MrSIDDataset::GetGTIFDefn()
2392 : {
2393 : double dfInvFlattening;
2394 :
2395 : /* -------------------------------------------------------------------- */
2396 : /* Make sure we have hooked CSV lookup for GDAL_DATA. */
2397 : /* -------------------------------------------------------------------- */
2398 12 : LibgeotiffOneTimeInit();
2399 :
2400 : /* -------------------------------------------------------------------- */
2401 : /* Initially we default all the information we can. */
2402 : /* -------------------------------------------------------------------- */
2403 12 : psDefn = new (GTIFDefn);
2404 12 : psDefn->Model = KvUserDefined;
2405 12 : psDefn->PCS = KvUserDefined;
2406 12 : psDefn->GCS = KvUserDefined;
2407 12 : psDefn->UOMLength = KvUserDefined;
2408 12 : psDefn->UOMLengthInMeters = 1.0;
2409 12 : psDefn->UOMAngle = KvUserDefined;
2410 12 : psDefn->UOMAngleInDegrees = 1.0;
2411 12 : psDefn->Datum = KvUserDefined;
2412 12 : psDefn->Ellipsoid = KvUserDefined;
2413 12 : psDefn->SemiMajor = 0.0;
2414 12 : psDefn->SemiMinor = 0.0;
2415 12 : psDefn->PM = KvUserDefined;
2416 12 : psDefn->PMLongToGreenwich = 0.0;
2417 :
2418 12 : psDefn->ProjCode = KvUserDefined;
2419 12 : psDefn->Projection = KvUserDefined;
2420 12 : psDefn->CTProjection = KvUserDefined;
2421 :
2422 12 : psDefn->nParms = 0;
2423 132 : for (int i = 0; i < MAX_GTIF_PROJPARMS; i++)
2424 : {
2425 120 : psDefn->ProjParm[i] = 0.0;
2426 120 : psDefn->ProjParmId[i] = 0;
2427 : }
2428 :
2429 12 : psDefn->MapSys = KvUserDefined;
2430 12 : psDefn->Zone = 0;
2431 :
2432 : /* -------------------------------------------------------------------- */
2433 : /* Try to get the overall model type. */
2434 : /* -------------------------------------------------------------------- */
2435 12 : GetMetadataElement("GEOTIFF_NUM::1024::GTModelTypeGeoKey",
2436 12 : &(psDefn->Model));
2437 :
2438 : /* -------------------------------------------------------------------- */
2439 : /* Try to get a PCS. */
2440 : /* -------------------------------------------------------------------- */
2441 36 : if (GetMetadataElement("GEOTIFF_NUM::3072::ProjectedCSTypeGeoKey",
2442 22 : &(psDefn->PCS)) &&
2443 10 : psDefn->PCS != KvUserDefined)
2444 : {
2445 : /*
2446 : * Translate this into useful information.
2447 : */
2448 0 : GTIFGetPCSInfo(psDefn->PCS, nullptr, &(psDefn->ProjCode),
2449 0 : &(psDefn->UOMLength), &(psDefn->GCS));
2450 : }
2451 :
2452 : /* -------------------------------------------------------------------- */
2453 : /* If we have the PCS code, but didn't find it in the CSV files */
2454 : /* (likely because we can't find them) we will try some ``jiffy */
2455 : /* rules'' for UTM and state plane. */
2456 : /* -------------------------------------------------------------------- */
2457 12 : if (psDefn->PCS != KvUserDefined && psDefn->ProjCode == KvUserDefined)
2458 : {
2459 : int nMapSys, nZone;
2460 0 : int nGCS = psDefn->GCS;
2461 :
2462 0 : nMapSys = GTIFPCSToMapSys(psDefn->PCS, &nGCS, &nZone);
2463 0 : if (nMapSys != KvUserDefined)
2464 : {
2465 0 : psDefn->ProjCode = (short)GTIFMapSysToProj(nMapSys, nZone);
2466 0 : psDefn->GCS = (short)nGCS;
2467 : }
2468 : }
2469 :
2470 : /* -------------------------------------------------------------------- */
2471 : /* If the Proj_ code is specified directly, use that. */
2472 : /* -------------------------------------------------------------------- */
2473 12 : if (psDefn->ProjCode == KvUserDefined)
2474 12 : GetMetadataElement("GEOTIFF_NUM::3074::ProjectionGeoKey",
2475 12 : &(psDefn->ProjCode));
2476 :
2477 12 : if (psDefn->ProjCode != KvUserDefined)
2478 : {
2479 : /*
2480 : * We have an underlying projection transformation value. Look
2481 : * this up. For a PCS of ``WGS 84 / UTM 11'' the transformation
2482 : * would be Transverse Mercator, with a particular set of options.
2483 : * The nProjTRFCode itself would correspond to the name
2484 : * ``UTM zone 11N'', and doesn't include datum info.
2485 : */
2486 0 : GTIFGetProjTRFInfo(psDefn->ProjCode, nullptr, &(psDefn->Projection),
2487 0 : psDefn->ProjParm);
2488 :
2489 : /*
2490 : * Set the GeoTIFF identity of the parameters.
2491 : */
2492 0 : psDefn->CTProjection =
2493 0 : (short)EPSGProjMethodToCTProjMethod(psDefn->Projection);
2494 :
2495 0 : SetGTParamIds(psDefn->CTProjection, psDefn->ProjParmId, nullptr);
2496 0 : psDefn->nParms = 7;
2497 : }
2498 :
2499 : /* -------------------------------------------------------------------- */
2500 : /* Try to get a GCS. If found, it will override any implied by */
2501 : /* the PCS. */
2502 : /* -------------------------------------------------------------------- */
2503 12 : GetMetadataElement("GEOTIFF_NUM::2048::GeographicTypeGeoKey",
2504 12 : &(psDefn->GCS));
2505 :
2506 : /* -------------------------------------------------------------------- */
2507 : /* Derive the datum, and prime meridian from the GCS. */
2508 : /* -------------------------------------------------------------------- */
2509 12 : if (psDefn->GCS != KvUserDefined)
2510 : {
2511 10 : GTIFGetGCSInfo(psDefn->GCS, nullptr, &(psDefn->Datum), &(psDefn->PM),
2512 10 : &(psDefn->UOMAngle));
2513 : }
2514 :
2515 : /* -------------------------------------------------------------------- */
2516 : /* Handle the GCS angular units. GeogAngularUnitsGeoKey */
2517 : /* overrides the GCS or PCS setting. */
2518 : /* -------------------------------------------------------------------- */
2519 12 : GetMetadataElement("GEOTIFF_NUM::2054::GeogAngularUnitsGeoKey",
2520 12 : &(psDefn->UOMAngle));
2521 12 : if (psDefn->UOMAngle != KvUserDefined)
2522 : {
2523 10 : GTIFGetUOMAngleInfo(psDefn->UOMAngle, nullptr,
2524 10 : &(psDefn->UOMAngleInDegrees));
2525 : }
2526 :
2527 : /* -------------------------------------------------------------------- */
2528 : /* Check for a datum setting, and then use the datum to derive */
2529 : /* an ellipsoid. */
2530 : /* -------------------------------------------------------------------- */
2531 12 : GetMetadataElement("GEOTIFF_NUM::2050::GeogGeodeticDatumGeoKey",
2532 12 : &(psDefn->Datum));
2533 :
2534 12 : if (psDefn->Datum != KvUserDefined)
2535 : {
2536 10 : GTIFGetDatumInfo(psDefn->Datum, nullptr, &(psDefn->Ellipsoid));
2537 : }
2538 :
2539 : /* -------------------------------------------------------------------- */
2540 : /* Check for an explicit ellipsoid. Use the ellipsoid to */
2541 : /* derive the ellipsoid characteristics, if possible. */
2542 : /* -------------------------------------------------------------------- */
2543 12 : GetMetadataElement("GEOTIFF_NUM::2056::GeogEllipsoidGeoKey",
2544 12 : &(psDefn->Ellipsoid));
2545 :
2546 12 : if (psDefn->Ellipsoid != KvUserDefined)
2547 : {
2548 10 : GTIFGetEllipsoidInfo(psDefn->Ellipsoid, nullptr, &(psDefn->SemiMajor),
2549 10 : &(psDefn->SemiMinor));
2550 : }
2551 :
2552 : /* -------------------------------------------------------------------- */
2553 : /* Check for overridden ellipsoid parameters. It would be nice */
2554 : /* to warn if they conflict with provided information, but for */
2555 : /* now we just override. */
2556 : /* -------------------------------------------------------------------- */
2557 12 : GetMetadataElement("GEOTIFF_NUM::2057::GeogSemiMajorAxisGeoKey",
2558 12 : &(psDefn->SemiMajor));
2559 12 : GetMetadataElement("GEOTIFF_NUM::2058::GeogSemiMinorAxisGeoKey",
2560 12 : &(psDefn->SemiMinor));
2561 :
2562 12 : if (GetMetadataElement("GEOTIFF_NUM::2059::GeogInvFlatteningGeoKey",
2563 12 : &dfInvFlattening) == 1)
2564 : {
2565 0 : if (dfInvFlattening != 0.0)
2566 0 : psDefn->SemiMinor = OSRCalcSemiMinorFromInvFlattening(
2567 0 : psDefn->SemiMajor, dfInvFlattening);
2568 : }
2569 :
2570 : /* -------------------------------------------------------------------- */
2571 : /* Get the prime meridian info. */
2572 : /* -------------------------------------------------------------------- */
2573 12 : GetMetadataElement("GEOTIFF_NUM::2051::GeogPrimeMeridianGeoKey",
2574 12 : &(psDefn->PM));
2575 :
2576 12 : if (psDefn->PM != KvUserDefined)
2577 : {
2578 10 : GTIFGetPMInfo(psDefn->PM, nullptr, &(psDefn->PMLongToGreenwich));
2579 : }
2580 : else
2581 : {
2582 2 : GetMetadataElement("GEOTIFF_NUM::2061::GeogPrimeMeridianLongGeoKey",
2583 2 : &(psDefn->PMLongToGreenwich));
2584 :
2585 4 : psDefn->PMLongToGreenwich =
2586 2 : GTIFAngleToDD(psDefn->PMLongToGreenwich, psDefn->UOMAngle);
2587 : }
2588 :
2589 : /* -------------------------------------------------------------------- */
2590 : /* Have the projection units of measure been overridden? We */
2591 : /* should likely be doing something about angular units too, */
2592 : /* but these are very rarely not decimal degrees for actual */
2593 : /* file coordinates. */
2594 : /* -------------------------------------------------------------------- */
2595 12 : GetMetadataElement("GEOTIFF_NUM::3076::ProjLinearUnitsGeoKey",
2596 12 : &(psDefn->UOMLength));
2597 :
2598 12 : if (psDefn->UOMLength != KvUserDefined)
2599 : {
2600 10 : GTIFGetUOMLengthInfo(psDefn->UOMLength, nullptr,
2601 10 : &(psDefn->UOMLengthInMeters));
2602 : }
2603 :
2604 : /* -------------------------------------------------------------------- */
2605 : /* Handle a variety of user defined transform types. */
2606 : /* -------------------------------------------------------------------- */
2607 24 : if (GetMetadataElement("GEOTIFF_NUM::3075::ProjCoordTransGeoKey",
2608 12 : &(psDefn->CTProjection)))
2609 : {
2610 10 : FetchProjParams();
2611 : }
2612 :
2613 : /* -------------------------------------------------------------------- */
2614 : /* Try to set the zoned map system information. */
2615 : /* -------------------------------------------------------------------- */
2616 12 : psDefn->MapSys = GTIFProjToMapSys(psDefn->ProjCode, &(psDefn->Zone));
2617 :
2618 : /* -------------------------------------------------------------------- */
2619 : /* If this is UTM, and we were unable to extract the projection */
2620 : /* parameters from the CSV file, just set them directly now, */
2621 : /* since it is pretty easy, and a common case. */
2622 : /* -------------------------------------------------------------------- */
2623 12 : if ((psDefn->MapSys == MapSys_UTM_North ||
2624 12 : psDefn->MapSys == MapSys_UTM_South) &&
2625 0 : psDefn->CTProjection == KvUserDefined)
2626 : {
2627 0 : psDefn->CTProjection = CT_TransverseMercator;
2628 0 : psDefn->nParms = 7;
2629 0 : psDefn->ProjParmId[0] = ProjNatOriginLatGeoKey;
2630 0 : psDefn->ProjParm[0] = 0.0;
2631 :
2632 0 : psDefn->ProjParmId[1] = ProjNatOriginLongGeoKey;
2633 0 : psDefn->ProjParm[1] = psDefn->Zone * 6 - 183.0;
2634 :
2635 0 : psDefn->ProjParmId[4] = ProjScaleAtNatOriginGeoKey;
2636 0 : psDefn->ProjParm[4] = 0.9996;
2637 :
2638 0 : psDefn->ProjParmId[5] = ProjFalseEastingGeoKey;
2639 0 : psDefn->ProjParm[5] = 500000.0;
2640 :
2641 0 : psDefn->ProjParmId[6] = ProjFalseNorthingGeoKey;
2642 :
2643 0 : if (psDefn->MapSys == MapSys_UTM_North)
2644 0 : psDefn->ProjParm[6] = 0.0;
2645 : else
2646 0 : psDefn->ProjParm[6] = 10000000.0;
2647 : }
2648 :
2649 12 : char *pszProjection = GetOGISDefn(psDefn);
2650 12 : if (pszProjection)
2651 : {
2652 12 : m_oSRS.importFromWkt(pszProjection);
2653 12 : m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
2654 : }
2655 12 : CPLFree(pszProjection);
2656 12 : }
2657 :
2658 : /************************************************************************/
2659 : /* GTIFToCPLRecyleString() */
2660 : /* */
2661 : /* This changes a string from the libgeotiff heap to the GDAL */
2662 : /* heap. */
2663 : /************************************************************************/
2664 :
2665 50 : static void GTIFToCPLRecycleString(char **ppszTarget)
2666 :
2667 : {
2668 50 : if (*ppszTarget == nullptr)
2669 0 : return;
2670 :
2671 50 : char *pszTempString = CPLStrdup(*ppszTarget);
2672 50 : GTIFFreeMemory(*ppszTarget);
2673 50 : *ppszTarget = pszTempString;
2674 : }
2675 :
2676 : /************************************************************************/
2677 : /* GetOGISDefn() */
2678 : /* Copied from the gt_wkt_srs.cpp. */
2679 : /************************************************************************/
2680 :
2681 12 : char *MrSIDDataset::GetOGISDefn(GTIFDefn *psDefnIn)
2682 : {
2683 24 : OGRSpatialReference oSRS;
2684 :
2685 12 : if (psDefnIn->Model != ModelTypeProjected &&
2686 2 : psDefnIn->Model != ModelTypeGeographic)
2687 2 : return CPLStrdup("");
2688 :
2689 : /* -------------------------------------------------------------------- */
2690 : /* If this is a projected SRS we set the PROJCS keyword first */
2691 : /* to ensure that the GEOGCS will be a child. */
2692 : /* -------------------------------------------------------------------- */
2693 10 : if (psDefnIn->Model == ModelTypeProjected)
2694 : {
2695 10 : int bPCSNameSet = FALSE;
2696 :
2697 10 : if (psDefnIn->PCS != KvUserDefined)
2698 : {
2699 0 : char *pszPCSName = nullptr;
2700 :
2701 0 : if (GTIFGetPCSInfo(psDefnIn->PCS, &pszPCSName, nullptr, nullptr,
2702 0 : nullptr))
2703 0 : bPCSNameSet = TRUE;
2704 :
2705 0 : oSRS.SetNode("PROJCS", bPCSNameSet ? pszPCSName : "unnamed");
2706 0 : if (bPCSNameSet)
2707 0 : GTIFFreeMemory(pszPCSName);
2708 :
2709 0 : oSRS.SetAuthority("PROJCS", "EPSG", psDefnIn->PCS);
2710 : }
2711 : else
2712 : {
2713 : char szPCSName[200];
2714 10 : strcpy(szPCSName, "unnamed");
2715 10 : if (GetMetadataElement("GEOTIFF_NUM::1026::GTCitationGeoKey",
2716 10 : szPCSName, sizeof(szPCSName)))
2717 10 : oSRS.SetNode("PROJCS", szPCSName);
2718 : }
2719 : }
2720 :
2721 : /* ==================================================================== */
2722 : /* Setup the GeogCS */
2723 : /* ==================================================================== */
2724 10 : char *pszGeogName = nullptr;
2725 10 : char *pszDatumName = nullptr;
2726 10 : char *pszPMName = nullptr;
2727 10 : char *pszSpheroidName = nullptr;
2728 10 : char *pszAngularUnits = nullptr;
2729 : double dfInvFlattening, dfSemiMajor;
2730 : char szGCSName[200];
2731 :
2732 10 : if (GetMetadataElement("GEOTIFF_NUM::2049::GeogCitationGeoKey", szGCSName,
2733 10 : sizeof(szGCSName)))
2734 0 : pszGeogName = CPLStrdup(szGCSName);
2735 : else
2736 : {
2737 10 : GTIFGetGCSInfo(psDefnIn->GCS, &pszGeogName, nullptr, nullptr, nullptr);
2738 10 : GTIFToCPLRecycleString(&pszGeogName);
2739 : }
2740 10 : GTIFGetDatumInfo(psDefnIn->Datum, &pszDatumName, nullptr);
2741 10 : GTIFToCPLRecycleString(&pszDatumName);
2742 10 : GTIFGetPMInfo(psDefnIn->PM, &pszPMName, nullptr);
2743 10 : GTIFToCPLRecycleString(&pszPMName);
2744 10 : GTIFGetEllipsoidInfo(psDefnIn->Ellipsoid, &pszSpheroidName, nullptr,
2745 : nullptr);
2746 10 : GTIFToCPLRecycleString(&pszSpheroidName);
2747 :
2748 10 : GTIFGetUOMAngleInfo(psDefnIn->UOMAngle, &pszAngularUnits, nullptr);
2749 10 : GTIFToCPLRecycleString(&pszAngularUnits);
2750 10 : if (pszAngularUnits == nullptr)
2751 0 : pszAngularUnits = CPLStrdup("unknown");
2752 :
2753 10 : if (pszDatumName != nullptr)
2754 10 : WKTMassageDatum(&pszDatumName);
2755 :
2756 10 : dfSemiMajor = psDefnIn->SemiMajor;
2757 10 : if (psDefnIn->SemiMajor == 0.0)
2758 : {
2759 0 : CPLFree(pszSpheroidName);
2760 0 : pszSpheroidName = CPLStrdup("unretrievable - using WGS84");
2761 0 : dfSemiMajor = SRS_WGS84_SEMIMAJOR;
2762 0 : dfInvFlattening = SRS_WGS84_INVFLATTENING;
2763 : }
2764 : else
2765 : dfInvFlattening =
2766 10 : OSRCalcInvFlattening(psDefnIn->SemiMajor, psDefnIn->SemiMinor);
2767 :
2768 10 : oSRS.SetGeogCS(pszGeogName, pszDatumName, pszSpheroidName, dfSemiMajor,
2769 : dfInvFlattening, pszPMName,
2770 10 : psDefnIn->PMLongToGreenwich / psDefnIn->UOMAngleInDegrees,
2771 : pszAngularUnits,
2772 10 : psDefnIn->UOMAngleInDegrees * 0.0174532925199433);
2773 :
2774 10 : if (psDefnIn->GCS != KvUserDefined)
2775 10 : oSRS.SetAuthority("GEOGCS", "EPSG", psDefnIn->GCS);
2776 :
2777 10 : if (psDefnIn->Datum != KvUserDefined)
2778 10 : oSRS.SetAuthority("DATUM", "EPSG", psDefnIn->Datum);
2779 :
2780 10 : if (psDefnIn->Ellipsoid != KvUserDefined)
2781 10 : oSRS.SetAuthority("SPHEROID", "EPSG", psDefnIn->Ellipsoid);
2782 :
2783 10 : CPLFree(pszGeogName);
2784 10 : CPLFree(pszDatumName);
2785 10 : CPLFree(pszPMName);
2786 10 : CPLFree(pszSpheroidName);
2787 10 : CPLFree(pszAngularUnits);
2788 :
2789 : /* ==================================================================== */
2790 : /* Handle projection parameters. */
2791 : /* ==================================================================== */
2792 10 : if (psDefnIn->Model == ModelTypeProjected)
2793 : {
2794 : /* --------------------------------------------------------------------
2795 : */
2796 : /* Make a local copy of params, and convert back into the */
2797 : /* angular units of the GEOGCS and the linear units of the */
2798 : /* projection. */
2799 : /* --------------------------------------------------------------------
2800 : */
2801 : double adfParam[10];
2802 : int i;
2803 :
2804 80 : for (i = 0; i < MIN(10, psDefnIn->nParms); i++)
2805 70 : adfParam[i] = psDefnIn->ProjParm[i];
2806 40 : for (; i < 10; i++)
2807 30 : adfParam[i] = 0;
2808 :
2809 10 : adfParam[0] /= psDefnIn->UOMAngleInDegrees;
2810 10 : adfParam[1] /= psDefnIn->UOMAngleInDegrees;
2811 10 : adfParam[2] /= psDefnIn->UOMAngleInDegrees;
2812 10 : adfParam[3] /= psDefnIn->UOMAngleInDegrees;
2813 :
2814 10 : adfParam[5] /= psDefnIn->UOMLengthInMeters;
2815 10 : adfParam[6] /= psDefnIn->UOMLengthInMeters;
2816 :
2817 : /* --------------------------------------------------------------------
2818 : */
2819 : /* Translation the fundamental projection. */
2820 : /* --------------------------------------------------------------------
2821 : */
2822 10 : switch (psDefnIn->CTProjection)
2823 : {
2824 0 : case CT_TransverseMercator:
2825 0 : oSRS.SetTM(adfParam[0], adfParam[1], adfParam[4], adfParam[5],
2826 : adfParam[6]);
2827 0 : break;
2828 :
2829 0 : case CT_TransvMercator_SouthOriented:
2830 0 : oSRS.SetTMSO(adfParam[0], adfParam[1], adfParam[4], adfParam[5],
2831 : adfParam[6]);
2832 0 : break;
2833 :
2834 10 : case CT_Mercator:
2835 10 : oSRS.SetMercator(adfParam[0], adfParam[1], adfParam[4],
2836 : adfParam[5], adfParam[6]);
2837 10 : break;
2838 :
2839 0 : case CT_ObliqueStereographic:
2840 0 : oSRS.SetOS(adfParam[0], adfParam[1], adfParam[4], adfParam[5],
2841 : adfParam[6]);
2842 0 : break;
2843 :
2844 0 : case CT_Stereographic:
2845 0 : oSRS.SetOS(adfParam[0], adfParam[1], adfParam[4], adfParam[5],
2846 : adfParam[6]);
2847 0 : break;
2848 :
2849 0 : case CT_ObliqueMercator: /* hotine */
2850 0 : oSRS.SetHOM(adfParam[0], adfParam[1], adfParam[2], adfParam[3],
2851 : adfParam[4], adfParam[5], adfParam[6]);
2852 0 : break;
2853 :
2854 0 : case CT_EquidistantConic:
2855 0 : oSRS.SetEC(adfParam[0], adfParam[1], adfParam[2], adfParam[3],
2856 : adfParam[5], adfParam[6]);
2857 0 : break;
2858 :
2859 0 : case CT_CassiniSoldner:
2860 0 : oSRS.SetCS(adfParam[0], adfParam[1], adfParam[5], adfParam[6]);
2861 0 : break;
2862 :
2863 0 : case CT_Polyconic:
2864 0 : oSRS.SetPolyconic(adfParam[0], adfParam[1], adfParam[5],
2865 : adfParam[6]);
2866 0 : break;
2867 :
2868 0 : case CT_AzimuthalEquidistant:
2869 0 : oSRS.SetAE(adfParam[0], adfParam[1], adfParam[5], adfParam[6]);
2870 0 : break;
2871 :
2872 0 : case CT_MillerCylindrical:
2873 0 : oSRS.SetMC(adfParam[0], adfParam[1], adfParam[5], adfParam[6]);
2874 0 : break;
2875 :
2876 0 : case CT_Equirectangular:
2877 0 : oSRS.SetEquirectangular(adfParam[0], adfParam[1], adfParam[5],
2878 : adfParam[6]);
2879 0 : break;
2880 :
2881 0 : case CT_Gnomonic:
2882 0 : oSRS.SetGnomonic(adfParam[0], adfParam[1], adfParam[5],
2883 : adfParam[6]);
2884 0 : break;
2885 :
2886 0 : case CT_LambertAzimEqualArea:
2887 0 : oSRS.SetLAEA(adfParam[0], adfParam[1], adfParam[5],
2888 : adfParam[6]);
2889 0 : break;
2890 :
2891 0 : case CT_Orthographic:
2892 0 : oSRS.SetOrthographic(adfParam[0], adfParam[1], adfParam[5],
2893 : adfParam[6]);
2894 0 : break;
2895 :
2896 0 : case CT_Robinson:
2897 0 : oSRS.SetRobinson(adfParam[1], adfParam[5], adfParam[6]);
2898 0 : break;
2899 :
2900 0 : case CT_Sinusoidal:
2901 0 : oSRS.SetSinusoidal(adfParam[1], adfParam[5], adfParam[6]);
2902 0 : break;
2903 :
2904 0 : case CT_VanDerGrinten:
2905 0 : oSRS.SetVDG(adfParam[1], adfParam[5], adfParam[6]);
2906 0 : break;
2907 :
2908 0 : case CT_PolarStereographic:
2909 0 : oSRS.SetPS(adfParam[0], adfParam[1], adfParam[4], adfParam[5],
2910 : adfParam[6]);
2911 0 : break;
2912 :
2913 0 : case CT_LambertConfConic_2SP:
2914 0 : oSRS.SetLCC(adfParam[2], adfParam[3], adfParam[0], adfParam[1],
2915 : adfParam[5], adfParam[6]);
2916 0 : break;
2917 :
2918 0 : case CT_LambertConfConic_1SP:
2919 0 : oSRS.SetLCC1SP(adfParam[0], adfParam[1], adfParam[4],
2920 : adfParam[5], adfParam[6]);
2921 0 : break;
2922 :
2923 0 : case CT_AlbersEqualArea:
2924 0 : oSRS.SetACEA(adfParam[0], adfParam[1], adfParam[2], adfParam[3],
2925 : adfParam[5], adfParam[6]);
2926 0 : break;
2927 :
2928 0 : case CT_NewZealandMapGrid:
2929 0 : oSRS.SetNZMG(adfParam[0], adfParam[1], adfParam[5],
2930 : adfParam[6]);
2931 0 : break;
2932 : }
2933 :
2934 : /* --------------------------------------------------------------------
2935 : */
2936 : /* Set projection units. */
2937 : /* --------------------------------------------------------------------
2938 : */
2939 10 : char *pszUnitsName = nullptr;
2940 :
2941 10 : GTIFGetUOMLengthInfo(psDefnIn->UOMLength, &pszUnitsName, nullptr);
2942 :
2943 10 : if (pszUnitsName != nullptr && psDefnIn->UOMLength != KvUserDefined)
2944 : {
2945 10 : oSRS.SetLinearUnits(pszUnitsName, psDefnIn->UOMLengthInMeters);
2946 10 : oSRS.SetAuthority("PROJCS|UNIT", "EPSG", psDefnIn->UOMLength);
2947 : }
2948 : else
2949 0 : oSRS.SetLinearUnits("unknown", psDefnIn->UOMLengthInMeters);
2950 :
2951 10 : GTIFFreeMemory(pszUnitsName);
2952 : }
2953 :
2954 : /* -------------------------------------------------------------------- */
2955 : /* Return the WKT serialization of the object. */
2956 : /* -------------------------------------------------------------------- */
2957 :
2958 10 : char *pszWKT = nullptr;
2959 10 : if (oSRS.exportToWkt(&pszWKT) == OGRERR_NONE)
2960 10 : return pszWKT;
2961 : else
2962 : {
2963 0 : CPLFree(pszWKT);
2964 0 : return nullptr;
2965 : }
2966 : }
2967 :
2968 : #ifdef MRSID_ESDK
2969 :
2970 : /************************************************************************/
2971 : /* ==================================================================== */
2972 : /* MrSIDDummyImageReader */
2973 : /* */
2974 : /* This is a helper class to wrap GDAL calls in MrSID interface. */
2975 : /* ==================================================================== */
2976 : /************************************************************************/
2977 :
2978 : class MrSIDDummyImageReader : public LTIImageReader
2979 : {
2980 : public:
2981 : MrSIDDummyImageReader(GDALDataset *poSrcDS);
2982 : ~MrSIDDummyImageReader();
2983 : LT_STATUS initialize();
2984 :
2985 : lt_int64 getPhysicalFileSize(void) const
2986 : {
2987 : return 0;
2988 : };
2989 :
2990 : private:
2991 : GDALDataset *poDS;
2992 : GDALDataType eDataType;
2993 : LTIDataType eSampleType;
2994 : const LTIPixel *poPixel;
2995 :
2996 : GDALGeoTransform m_gt{};
2997 :
2998 : virtual LT_STATUS decodeStrip(LTISceneBuffer &stripBuffer,
2999 : const LTIScene &stripScene);
3000 :
3001 : virtual LT_STATUS decodeBegin(const LTIScene &)
3002 : {
3003 : return LT_STS_Success;
3004 : };
3005 :
3006 : virtual LT_STATUS decodeEnd()
3007 : {
3008 : return LT_STS_Success;
3009 : };
3010 : };
3011 :
3012 : /************************************************************************/
3013 : /* MrSIDDummyImageReader() */
3014 : /************************************************************************/
3015 :
3016 : MrSIDDummyImageReader::MrSIDDummyImageReader(GDALDataset *poSrcDS)
3017 : : LTIImageReader(), poDS(poSrcDS)
3018 : {
3019 : poPixel = nullptr;
3020 : }
3021 :
3022 : /************************************************************************/
3023 : /* ~MrSIDDummyImageReader() */
3024 : /************************************************************************/
3025 :
3026 : MrSIDDummyImageReader::~MrSIDDummyImageReader()
3027 : {
3028 : if (poPixel)
3029 : delete poPixel;
3030 : }
3031 :
3032 : /************************************************************************/
3033 : /* initialize() */
3034 : /************************************************************************/
3035 :
3036 : LT_STATUS MrSIDDummyImageReader::initialize()
3037 : {
3038 : LT_STATUS eStat = LT_STS_Uninit;
3039 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 6
3040 : if (!LT_SUCCESS(eStat = LTIImageReader::init()))
3041 : return eStat;
3042 : #else
3043 : if (!LT_SUCCESS(eStat = LTIImageReader::initialize()))
3044 : return eStat;
3045 : #endif
3046 :
3047 : lt_uint16 nBands = (lt_uint16)poDS->GetRasterCount();
3048 : LTIColorSpace eColorSpace = LTI_COLORSPACE_RGB;
3049 : switch (nBands)
3050 : {
3051 : case 1:
3052 : eColorSpace = LTI_COLORSPACE_GRAYSCALE;
3053 : break;
3054 : case 3:
3055 : eColorSpace = LTI_COLORSPACE_RGB;
3056 : break;
3057 : default:
3058 : eColorSpace = LTI_COLORSPACE_MULTISPECTRAL;
3059 : break;
3060 : }
3061 :
3062 : eDataType = poDS->GetRasterBand(1)->GetRasterDataType();
3063 : switch (eDataType)
3064 : {
3065 : case GDT_UInt16:
3066 : eSampleType = LTI_DATATYPE_UINT16;
3067 : break;
3068 : case GDT_Int16:
3069 : eSampleType = LTI_DATATYPE_SINT16;
3070 : break;
3071 : case GDT_UInt32:
3072 : eSampleType = LTI_DATATYPE_UINT32;
3073 : break;
3074 : case GDT_Int32:
3075 : eSampleType = LTI_DATATYPE_SINT32;
3076 : break;
3077 : case GDT_Float32:
3078 : eSampleType = LTI_DATATYPE_FLOAT32;
3079 : break;
3080 : case GDT_Float64:
3081 : eSampleType = LTI_DATATYPE_FLOAT64;
3082 : break;
3083 : case GDT_Byte:
3084 : default:
3085 : eSampleType = LTI_DATATYPE_UINT8;
3086 : break;
3087 : }
3088 :
3089 : poPixel = new LTIDLLPixel<LTIPixel>(eColorSpace, nBands, eSampleType);
3090 : if (!LT_SUCCESS(setPixelProps(*poPixel)))
3091 : return LT_STS_Failure;
3092 :
3093 : if (!LT_SUCCESS(
3094 : setDimensions(poDS->GetRasterXSize(), poDS->GetRasterYSize())))
3095 : return LT_STS_Failure;
3096 :
3097 : if (poDS->GetGeoTransform(m_gt) == CE_None)
3098 : {
3099 : #ifdef MRSID_SDK_40
3100 : LTIGeoCoord oGeo(m_gt[0] + m_gt[1] / 2, m_gt[3] + m_gt[5] / 2, m_gt[1],
3101 : m_gt[5], m_gt[2], m_gt[4], nullptr,
3102 : poDS->GetProjectionRef());
3103 : #else
3104 : LTIGeoCoord oGeo(m_gt[0] + m_gt[1] / 2, m_gt[3] + m_gt[5] / 2, m_gt[1],
3105 : m_gt[5], m_gt[2], m_gt[4], poDS->GetProjectionRef());
3106 : #endif
3107 : if (!LT_SUCCESS(setGeoCoord(oGeo)))
3108 : return LT_STS_Failure;
3109 : }
3110 :
3111 : /*int bSuccess;
3112 : double dfNoDataValue = poDS->GetNoDataValue( &bSuccess );
3113 : if ( bSuccess )
3114 : {
3115 : LTIPixel oNoDataPixel( *poPixel );
3116 : lt_uint16 iBand;
3117 :
3118 : for (iBand = 0; iBand < (lt_uint16)poDS->GetRasterCount(); iBand++)
3119 : oNoDataPixel.setSampleValueFloat32( iBand, dfNoDataValue );
3120 : if ( !LT_SUCCESS(setNoDataPixel( &oNoDataPixel )) )
3121 : return LT_STS_Failure;
3122 : }*/
3123 :
3124 : setDefaultDynamicRange();
3125 : #if !defined(LTI_SDK_MAJOR) || LTI_SDK_MAJOR < 8
3126 : setClassicalMetadata();
3127 : #endif
3128 :
3129 : return LT_STS_Success;
3130 : }
3131 :
3132 : /************************************************************************/
3133 : /* decodeStrip() */
3134 : /************************************************************************/
3135 :
3136 : LT_STATUS MrSIDDummyImageReader::decodeStrip(LTISceneBuffer &stripData,
3137 : const LTIScene &stripScene)
3138 :
3139 : {
3140 : const lt_int32 nXOff = stripScene.getUpperLeftCol();
3141 : const lt_int32 nYOff = stripScene.getUpperLeftRow();
3142 : const lt_int32 nBufXSize = stripScene.getNumCols();
3143 : const lt_int32 nBufYSize = stripScene.getNumRows();
3144 : const lt_int32 nDataBufXSize = stripData.getTotalNumCols();
3145 : const lt_int32 nDataBufYSize = stripData.getTotalNumRows();
3146 : const lt_uint16 nBands = poPixel->getNumBands();
3147 :
3148 : void *pData =
3149 : CPLMalloc(nDataBufXSize * nDataBufYSize * poPixel->getNumBytes());
3150 : if (!pData)
3151 : {
3152 : CPLError(CE_Failure, CPLE_AppDefined,
3153 : "MrSIDDummyImageReader::decodeStrip(): "
3154 : "Cannot allocate enough space for scene buffer");
3155 : return LT_STS_Failure;
3156 : }
3157 :
3158 : poDS->RasterIO(GF_Read, nXOff, nYOff, nBufXSize, nBufYSize, pData,
3159 : nBufXSize, nBufYSize, eDataType, nBands, nullptr, 0, 0, 0,
3160 : nullptr);
3161 :
3162 : stripData.importDataBSQ(pData);
3163 : CPLFree(pData);
3164 : return LT_STS_Success;
3165 : }
3166 :
3167 : /************************************************************************/
3168 : /* MrSIDCreateCopy() */
3169 : /************************************************************************/
3170 :
3171 : static GDALDataset *MrSIDCreateCopy(const char *pszFilename,
3172 : GDALDataset *poSrcDS, int bStrict,
3173 : char **papszOptions,
3174 : GDALProgressFunc pfnProgress,
3175 : void *pProgressData)
3176 :
3177 : {
3178 : const char *pszVersion = CSLFetchNameValue(papszOptions, "VERSION");
3179 : #ifdef MRSID_HAVE_MG4WRITE
3180 : int iVersion = pszVersion ? atoi(pszVersion) : 4;
3181 : #else
3182 : int iVersion = pszVersion ? atoi(pszVersion) : 3;
3183 : #endif
3184 : LT_STATUS eStat = LT_STS_Uninit;
3185 :
3186 : #ifdef DEBUG
3187 : bool bMeter = false;
3188 : #else
3189 : bool bMeter = true;
3190 : #endif
3191 :
3192 : if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
3193 : {
3194 : CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
3195 : "MrSID driver ignores color table. "
3196 : "The source raster band will be considered as grey level.\n"
3197 : "Consider using color table expansion (-expand option in "
3198 : "gdal_translate)\n");
3199 : if (bStrict)
3200 : return nullptr;
3201 : }
3202 :
3203 : MrSIDProgress oProgressDelegate(pfnProgress, pProgressData);
3204 : if (LT_FAILURE(eStat = oProgressDelegate.setProgressStatus(0)))
3205 : {
3206 : CPLError(CE_Failure, CPLE_AppDefined,
3207 : "MrSIDProgress.setProgressStatus failed.\n%s",
3208 : getLastStatusString(eStat));
3209 : return nullptr;
3210 : }
3211 :
3212 : // Create the file.
3213 : MrSIDDummyImageReader oImageReader(poSrcDS);
3214 : if (LT_FAILURE(eStat = oImageReader.initialize()))
3215 : {
3216 : CPLError(CE_Failure, CPLE_AppDefined,
3217 : "MrSIDDummyImageReader.Initialize failed.\n%s",
3218 : getLastStatusString(eStat));
3219 : return nullptr;
3220 : }
3221 :
3222 : LTIGeoFileImageWriter *poImageWriter = nullptr;
3223 : switch (iVersion)
3224 : {
3225 : case 2:
3226 : {
3227 : // Output Mrsid Version 2 file.
3228 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
3229 : LTIDLLDefault<MG2ImageWriter> *poMG2ImageWriter;
3230 : poMG2ImageWriter = new LTIDLLDefault<MG2ImageWriter>;
3231 : eStat = poMG2ImageWriter->initialize(&oImageReader);
3232 : #else
3233 : LTIDLLWriter<MG2ImageWriter> *poMG2ImageWriter;
3234 : poMG2ImageWriter = new LTIDLLWriter<MG2ImageWriter>(&oImageReader);
3235 : eStat = poMG2ImageWriter->initialize();
3236 : #endif
3237 : if (LT_FAILURE(eStat))
3238 : {
3239 : delete poMG2ImageWriter;
3240 : CPLError(CE_Failure, CPLE_AppDefined,
3241 : "MG2ImageWriter.initialize() failed.\n%s",
3242 : getLastStatusString(eStat));
3243 : return nullptr;
3244 : }
3245 :
3246 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
3247 : eStat = poMG2ImageWriter->setEncodingApplication(
3248 : "MrSID Driver", GDALVersionInfo("--version"));
3249 : if (LT_FAILURE(eStat))
3250 : {
3251 : delete poMG2ImageWriter;
3252 : CPLError(CE_Failure, CPLE_AppDefined,
3253 : "MG2ImageWriter.setEncodingApplication() failed.\n%s",
3254 : getLastStatusString(eStat));
3255 : return nullptr;
3256 : }
3257 : #endif
3258 :
3259 : poMG2ImageWriter->setUsageMeterEnabled(bMeter);
3260 :
3261 : poMG2ImageWriter->params().setBlockSize(
3262 : poMG2ImageWriter->params().getBlockSize());
3263 :
3264 : // check for compression option
3265 : const char *pszValue =
3266 : CSLFetchNameValue(papszOptions, "COMPRESSION");
3267 : if (pszValue != nullptr)
3268 : poMG2ImageWriter->params().setCompressionRatio(
3269 : (float)CPLAtof(pszValue));
3270 :
3271 : poImageWriter = poMG2ImageWriter;
3272 :
3273 : break;
3274 : }
3275 : case 3:
3276 : {
3277 : // Output Mrsid Version 3 file.
3278 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
3279 : LTIDLLDefault<MG3ImageWriter> *poMG3ImageWriter;
3280 : poMG3ImageWriter = new LTIDLLDefault<MG3ImageWriter>;
3281 : eStat = poMG3ImageWriter->initialize(&oImageReader);
3282 : #else
3283 : LTIDLLWriter<MG3ImageWriter> *poMG3ImageWriter;
3284 : poMG3ImageWriter = new LTIDLLWriter<MG3ImageWriter>(&oImageReader);
3285 : eStat = poMG3ImageWriter->initialize();
3286 : #endif
3287 : if (LT_FAILURE(eStat))
3288 : {
3289 : delete poMG3ImageWriter;
3290 : CPLError(CE_Failure, CPLE_AppDefined,
3291 : "MG3ImageWriter.initialize() failed.\n%s",
3292 : getLastStatusString(eStat));
3293 : return nullptr;
3294 : }
3295 :
3296 : #if defined(LTI_SDK_MAJOR) && LTI_SDK_MAJOR >= 8
3297 : eStat = poMG3ImageWriter->setEncodingApplication(
3298 : "MrSID Driver", GDALVersionInfo("--version"));
3299 : if (LT_FAILURE(eStat))
3300 : {
3301 : delete poMG3ImageWriter;
3302 : CPLError(CE_Failure, CPLE_AppDefined,
3303 : "MG3ImageWriter.setEncodingApplication() failed.\n%s",
3304 : getLastStatusString(eStat));
3305 : return nullptr;
3306 : }
3307 : #endif
3308 :
3309 : // usage meter should only be disabled for debugging
3310 : poMG3ImageWriter->setUsageMeterEnabled(bMeter);
3311 :
3312 : #if !defined(LTI_SDK_MAJOR) || LTI_SDK_MAJOR < 8
3313 : // Set 64-bit Interface for large files.
3314 : poMG3ImageWriter->setFileStream64(true);
3315 : #endif
3316 :
3317 : // set 2 pass optimizer option
3318 : if (CSLFetchNameValue(papszOptions, "TWOPASS") != nullptr)
3319 : poMG3ImageWriter->params().setTwoPassOptimizer(true);
3320 :
3321 : // set filesize in KB
3322 : const char *pszValue = CSLFetchNameValue(papszOptions, "FILESIZE");
3323 : if (pszValue != nullptr)
3324 : poMG3ImageWriter->params().setTargetFilesize(atoi(pszValue));
3325 :
3326 : poImageWriter = poMG3ImageWriter;
3327 :
3328 : break;
3329 : }
3330 : #ifdef MRSID_HAVE_MG4WRITE
3331 : case 4:
3332 : {
3333 : // Output Mrsid Version 4 file.
3334 : LTIDLLDefault<MG4ImageWriter> *poMG4ImageWriter;
3335 : poMG4ImageWriter = new LTIDLLDefault<MG4ImageWriter>;
3336 : eStat =
3337 : poMG4ImageWriter->initialize(&oImageReader, nullptr, nullptr);
3338 : if (LT_FAILURE(eStat))
3339 : {
3340 : delete poMG4ImageWriter;
3341 : CPLError(CE_Failure, CPLE_AppDefined,
3342 : "MG3ImageWriter.initialize() failed.\n%s",
3343 : getLastStatusString(eStat));
3344 : return nullptr;
3345 : }
3346 :
3347 : eStat = poMG4ImageWriter->setEncodingApplication(
3348 : "MrSID Driver", GDALVersionInfo("--version"));
3349 : if (LT_FAILURE(eStat))
3350 : {
3351 : delete poMG4ImageWriter;
3352 : CPLError(CE_Failure, CPLE_AppDefined,
3353 : "MG3ImageWriter.setEncodingApplication() failed.\n%s",
3354 : getLastStatusString(eStat));
3355 : return nullptr;
3356 : }
3357 :
3358 : // usage meter should only be disabled for debugging
3359 : poMG4ImageWriter->setUsageMeterEnabled(bMeter);
3360 :
3361 : // set 2 pass optimizer option
3362 : if (CSLFetchNameValue(papszOptions, "TWOPASS") != nullptr)
3363 : poMG4ImageWriter->params().setTwoPassOptimizer(true);
3364 :
3365 : // set filesize in KB
3366 : const char *pszValue = CSLFetchNameValue(papszOptions, "FILESIZE");
3367 : if (pszValue != nullptr)
3368 : poMG4ImageWriter->params().setTargetFilesize(atoi(pszValue));
3369 :
3370 : poImageWriter = poMG4ImageWriter;
3371 :
3372 : break;
3373 : }
3374 : #endif /* MRSID_HAVE_MG4WRITE */
3375 : default:
3376 : CPLError(CE_Failure, CPLE_AppDefined,
3377 : "Invalid MrSID generation specified (VERSION=%s).",
3378 : pszVersion);
3379 : return nullptr;
3380 : }
3381 :
3382 : // set output filename
3383 : poImageWriter->setOutputFileSpec(pszFilename);
3384 :
3385 : // set progress delegate
3386 : poImageWriter->setProgressDelegate(&oProgressDelegate);
3387 :
3388 : // set defaults
3389 : poImageWriter->setStripHeight(poImageWriter->getStripHeight());
3390 :
3391 : // set MrSID world file
3392 : if (CSLFetchNameValue(papszOptions, "WORLDFILE") != nullptr)
3393 : poImageWriter->setWorldFileSupport(true);
3394 :
3395 : // write the scene
3396 : int nXSize = poSrcDS->GetRasterXSize();
3397 : int nYSize = poSrcDS->GetRasterYSize();
3398 : const LTIScene oScene(0, 0, nXSize, nYSize, 1.0);
3399 : if (LT_FAILURE(eStat = poImageWriter->write(oScene)))
3400 : {
3401 : delete poImageWriter;
3402 : CPLError(CE_Failure, CPLE_AppDefined,
3403 : "MG2ImageWriter.write() failed.\n%s",
3404 : getLastStatusString(eStat));
3405 : return nullptr;
3406 : }
3407 :
3408 : delete poImageWriter;
3409 : /* -------------------------------------------------------------------- */
3410 : /* Re-open dataset, and copy any auxiliary pam information. */
3411 : /* -------------------------------------------------------------------- */
3412 : GDALPamDataset *poDS = (GDALPamDataset *)GDALOpen(pszFilename, GA_ReadOnly);
3413 :
3414 : if (poDS)
3415 : poDS->CloneInfo(poSrcDS, GCIF_PAM_DEFAULT);
3416 :
3417 : return poDS;
3418 : }
3419 :
3420 : #ifdef MRSID_J2K
3421 : /************************************************************************/
3422 : /* JP2CreateCopy() */
3423 : /************************************************************************/
3424 :
3425 : static GDALDataset *JP2CreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
3426 : int bStrict, char **papszOptions,
3427 : GDALProgressFunc pfnProgress,
3428 : void *pProgressData)
3429 :
3430 : {
3431 : #ifdef DEBUG
3432 : bool bMeter = false;
3433 : #else
3434 : bool bMeter = true;
3435 : #endif
3436 :
3437 : int nXSize = poSrcDS->GetRasterXSize();
3438 : int nYSize = poSrcDS->GetRasterYSize();
3439 : LT_STATUS eStat;
3440 :
3441 : if (poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr)
3442 : {
3443 : CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
3444 : "MrSID driver ignores color table. "
3445 : "The source raster band will be considered as grey level.\n"
3446 : "Consider using color table expansion (-expand option in "
3447 : "gdal_translate)\n");
3448 : if (bStrict)
3449 : return nullptr;
3450 : }
3451 :
3452 : MrSIDProgress oProgressDelegate(pfnProgress, pProgressData);
3453 : if (LT_FAILURE(eStat = oProgressDelegate.setProgressStatus(0)))
3454 : {
3455 : CPLError(CE_Failure, CPLE_AppDefined,
3456 : "MrSIDProgress.setProgressStatus failed.\n%s",
3457 : getLastStatusString(eStat));
3458 : return nullptr;
3459 : }
3460 :
3461 : // Create the file.
3462 : MrSIDDummyImageReader oImageReader(poSrcDS);
3463 : eStat = oImageReader.initialize();
3464 : if (eStat != LT_STS_Success)
3465 : {
3466 : CPLError(CE_Failure, CPLE_AppDefined,
3467 : "MrSIDDummyImageReader.Initialize failed.\n%s",
3468 : getLastStatusString(eStat));
3469 : return nullptr;
3470 : }
3471 :
3472 : #if !defined(MRSID_POST5)
3473 : J2KImageWriter oImageWriter(&oImageReader);
3474 : eStat = oImageWriter.initialize();
3475 : #elif !defined(LTI_SDK_MAJOR) || LTI_SDK_MAJOR < 8
3476 : JP2WriterManager oImageWriter(&oImageReader);
3477 : eStat = oImageWriter.initialize();
3478 : #else
3479 : JP2WriterManager oImageWriter;
3480 : eStat = oImageWriter.initialize(&oImageReader);
3481 : #endif
3482 : if (eStat != LT_STS_Success)
3483 : {
3484 : CPLError(CE_Failure, CPLE_AppDefined,
3485 : "J2KImageWriter.Initialize failed.\n%s",
3486 : getLastStatusString(eStat));
3487 : return nullptr;
3488 : }
3489 :
3490 : #if !defined(LTI_SDK_MAJOR) || LTI_SDK_MAJOR < 8
3491 : // Set 64-bit Interface for large files.
3492 : oImageWriter.setFileStream64(true);
3493 : #endif
3494 :
3495 : oImageWriter.setUsageMeterEnabled(bMeter);
3496 :
3497 : // set output filename
3498 : oImageWriter.setOutputFileSpec(pszFilename);
3499 :
3500 : // set progress delegate
3501 : oImageWriter.setProgressDelegate(&oProgressDelegate);
3502 :
3503 : // Set defaults
3504 : // oImageWriter.setStripHeight(oImageWriter.getStripHeight());
3505 :
3506 : // set MrSID world file
3507 : if (CSLFetchNameValue(papszOptions, "WORLDFILE") != nullptr)
3508 : oImageWriter.setWorldFileSupport(true);
3509 :
3510 : // check for compression option
3511 : const char *pszValue = CSLFetchNameValue(papszOptions, "COMPRESSION");
3512 : if (pszValue != nullptr)
3513 : oImageWriter.params().setCompressionRatio((float)CPLAtof(pszValue));
3514 :
3515 : pszValue = CSLFetchNameValue(papszOptions, "XMLPROFILE");
3516 : if (pszValue != nullptr)
3517 : {
3518 : LTFileSpec xmlprofile(pszValue);
3519 : eStat = oImageWriter.params().readProfile(xmlprofile);
3520 : if (eStat != LT_STS_Success)
3521 : {
3522 : CPLError(CE_Failure, CPLE_AppDefined,
3523 : "JPCWriterParams.readProfile failed.\n%s",
3524 : getLastStatusString(eStat));
3525 : return nullptr;
3526 : }
3527 : }
3528 :
3529 : // write the scene
3530 : const LTIScene oScene(0, 0, nXSize, nYSize, 1.0);
3531 : eStat = oImageWriter.write(oScene);
3532 : if (eStat != LT_STS_Success)
3533 : {
3534 : CPLError(CE_Failure, CPLE_AppDefined,
3535 : "J2KImageWriter.write() failed.\n%s",
3536 : getLastStatusString(eStat));
3537 : return nullptr;
3538 : }
3539 :
3540 : /* -------------------------------------------------------------------- */
3541 : /* Re-open dataset, and copy any auxiliary pam information. */
3542 : /* -------------------------------------------------------------------- */
3543 : GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
3544 : GDALPamDataset *poDS = (GDALPamDataset *)JP2Open(&oOpenInfo);
3545 :
3546 : if (poDS)
3547 : poDS->CloneInfo(poSrcDS, GCIF_PAM_DEFAULT);
3548 :
3549 : return poDS;
3550 : }
3551 : #endif /* MRSID_J2K */
3552 : #endif /* MRSID_ESDK */
3553 :
3554 : /************************************************************************/
3555 : /* GDALRegister_MrSID() */
3556 : /************************************************************************/
3557 :
3558 1935 : void GDALRegister_MrSID()
3559 :
3560 : {
3561 1935 : if (!GDAL_CHECK_VERSION("MrSID driver"))
3562 0 : return;
3563 :
3564 : /* -------------------------------------------------------------------- */
3565 : /* MrSID driver. */
3566 : /* -------------------------------------------------------------------- */
3567 1935 : if (GDALGetDriverByName(MRSID_DRIVER_NAME) != nullptr)
3568 282 : return;
3569 :
3570 1653 : GDALDriver *poDriver = new GDALDriver();
3571 1653 : MrSIDDriverSetCommonMetadata(poDriver);
3572 : #ifdef MRSID_ESDK
3573 : poDriver->pfnCreateCopy = MrSIDCreateCopy;
3574 : #endif
3575 1653 : poDriver->pfnOpen = MrSIDOpen;
3576 :
3577 1653 : GetGDALDriverManager()->RegisterDriver(poDriver);
3578 :
3579 : /* -------------------------------------------------------------------- */
3580 : /* JP2MRSID driver. */
3581 : /* -------------------------------------------------------------------- */
3582 : #ifdef MRSID_J2K
3583 : poDriver = new GDALDriver();
3584 : JP2MrSIDDriverSetCommonMetadata(poDriver);
3585 : #ifdef MRSID_ESDK
3586 : poDriver->pfnCreateCopy = JP2CreateCopy;
3587 : #endif
3588 : poDriver->pfnOpen = JP2Open;
3589 :
3590 : GetGDALDriverManager()->RegisterDriver(poDriver);
3591 : #endif /* def MRSID_J2K */
3592 : }
3593 :
3594 : #if defined(MRSID_USE_TIFFSYMS_WORKAROUND)
3595 : extern "C"
3596 : {
3597 :
3598 : /* This is not pretty but I am not sure how else to get the plugin to build
3599 : * against the ESDK. ESDK symbol dependencies bring in __TIFFmemcpy and
3600 : * __gtiff_size, which are not exported from gdal.dll. Rather than link
3601 : * these symbols from the ESDK distribution of GDAL, or link in the entire
3602 : * gdal.lib statically, it seemed safer and smaller to bring in just the
3603 : * objects that wouldsatisfy these symbols from the enclosing GDAL build.
3604 : * However, doing so pulls in a few more dependencies. /Gy and /OPT:REF did
3605 : * not seem to help things, so I have implemented no-op versions of these
3606 : * symbols since they do not actually get called. If the MrSID ESDK ever
3607 : * comes to require the actual versions of these functions, we'll hope
3608 : * duplicate symbol errors will bring attention back to this problem.
3609 : */
3610 : void TIFFClientOpen()
3611 : {
3612 : }
3613 :
3614 : void TIFFError()
3615 : {
3616 : }
3617 :
3618 : void TIFFGetField()
3619 : {
3620 : }
3621 :
3622 : void TIFFSetField()
3623 : {
3624 : }
3625 : }
3626 : #endif
|