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