LCOV - code coverage report
Current view: top level - frmts/mrsid - mrsiddataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 571 1224 46.7 %
Date: 2024-11-21 22:18:42 Functions: 38 43 88.4 %

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

Generated by: LCOV version 1.14