Line data Source code
1 : /* 2 : * keadataset.h 3 : * 4 : * Created by Pete Bunting on 01/08/2012. 5 : * Copyright 2012 LibKEA. All rights reserved. 6 : * 7 : * This file is part of LibKEA. 8 : * 9 : * SPDX-License-Identifier: MIT 10 : * 11 : */ 12 : 13 : #ifndef KEADATASET_H 14 : #define KEADATASET_H 15 : 16 : #include "gdal_priv.h" 17 : #include "cpl_multiproc.h" 18 : #include "libkea_headers.h" 19 : 20 : class LockedRefCount; 21 : 22 : // class that implements a GDAL dataset 23 : class KEADataset final : public GDALDataset 24 : { 25 : static H5::H5File *CreateLL(const char *pszFilename, int nXSize, int nYSize, 26 : int nBands, GDALDataType eType, 27 : char **papszParamList); 28 : 29 : public: 30 : // constructor/destructor 31 : KEADataset(H5::H5File *keaImgH5File, GDALAccess eAccess); 32 : ~KEADataset() override; 33 : 34 : // static methods that handle open and creation 35 : // the driver class has pointers to these 36 : static GDALDataset *Open(GDALOpenInfo *); 37 : static int Identify(GDALOpenInfo *poOpenInfo); 38 : static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize, 39 : int nBands, GDALDataType eType, 40 : char **papszParamList); 41 : static GDALDataset *CreateCopy(const char *pszFilename, GDALDataset *pSrcDs, 42 : int bStrict, char **papszParamList, 43 : GDALProgressFunc pfnProgress, 44 : void *pProgressData); 45 : 46 : // virtual methods for dealing with transform and projection 47 : CPLErr GetGeoTransform(GDALGeoTransform >) const override; 48 : const OGRSpatialReference *GetSpatialRef() const override; 49 : 50 : CPLErr SetGeoTransform(const GDALGeoTransform >) override; 51 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override; 52 : 53 : // method to get a pointer to the imageio class 54 : void *GetInternalHandle(const char *) override; 55 : 56 : // virtual methods for dealing with metadata 57 : CPLErr SetMetadataItem(const char *pszName, const char *pszValue, 58 : const char *pszDomain = "") override; 59 : const char *GetMetadataItem(const char *pszName, 60 : const char *pszDomain = "") override; 61 : 62 : char **GetMetadata(const char *pszDomain = "") override; 63 : CPLErr SetMetadata(char **papszMetadata, 64 : const char *pszDomain = "") override; 65 : 66 : // virtual method for adding new image bands 67 : CPLErr AddBand(GDALDataType eType, char **papszOptions = nullptr) override; 68 : 69 : // GCPs 70 : int GetGCPCount() override; 71 : const OGRSpatialReference *GetGCPSpatialRef() const override; 72 : const GDAL_GCP *GetGCPs() override; 73 : CPLErr SetGCPs(int nGCPCount, const GDAL_GCP *pasGCPList, 74 : const OGRSpatialReference *poSRS) override; 75 : 76 : protected: 77 : // this method builds overviews for the specified bands. 78 : CPLErr IBuildOverviews(const char *pszResampling, int nOverviews, 79 : const int *panOverviewList, int nListBands, 80 : const int *panBandList, GDALProgressFunc pfnProgress, 81 : void *pProgressData, 82 : CSLConstList papszOptions) override; 83 : 84 : // internal method to update m_papszMetadataList 85 : void UpdateMetadataList(); 86 : 87 : void DestroyGCPs(); 88 : 89 : private: 90 : // pointer to KEAImageIO class and the refcount for it 91 : kealib::KEAImageIO *m_pImageIO; 92 : LockedRefCount *m_pRefcount; 93 : char **m_papszMetadataList; // CSLStringList for metadata 94 : GDAL_GCP *m_pGCPs; 95 : mutable OGRSpatialReference m_oGCPSRS{}; 96 : mutable CPLMutex *m_hMutex; 97 : mutable OGRSpatialReference m_oSRS{}; 98 : }; 99 : 100 : // conversion functions 101 : GDALDataType KEA_to_GDAL_Type(kealib::KEADataType ekeaType); 102 : kealib::KEADataType GDAL_to_KEA_Type(GDALDataType egdalType); 103 : 104 : // For unloading the VFL 105 : void KEADatasetDriverUnload(GDALDriver *); 106 : 107 : // A thresafe reference count. Used to manage shared pointer to 108 : // the kealib::KEAImageIO instance between bands and dataset. 109 : class LockedRefCount 110 : { 111 : private: 112 : int m_nRefCount; 113 : CPLMutex *m_hMutex; 114 : 115 : CPL_DISALLOW_COPY_ASSIGN(LockedRefCount) 116 : 117 : public: 118 278 : explicit LockedRefCount(int initCount = 1) 119 278 : { 120 278 : m_nRefCount = initCount; 121 278 : m_hMutex = CPLCreateMutex(); 122 278 : CPLReleaseMutex(m_hMutex); 123 278 : } 124 : 125 278 : ~LockedRefCount() 126 278 : { 127 278 : CPLDestroyMutex(m_hMutex); 128 278 : m_hMutex = nullptr; 129 278 : } 130 : 131 408 : void IncRef() 132 : { 133 408 : CPLMutexHolderD(&m_hMutex); 134 408 : m_nRefCount++; 135 408 : } 136 : 137 : // returns true if reference count now 0 138 686 : bool DecRef() 139 : { 140 686 : CPLMutexHolderD(&m_hMutex); 141 686 : m_nRefCount--; 142 1372 : return m_nRefCount <= 0; 143 : } 144 : }; 145 : 146 : #endif // KEADATASET_H