1 /******************************************************************************
2  * $Id: envidataset.h 2783cb874e95c7d69dc42f3680a2829d4a276dea 2021-08-26 09:26:51 +0200 Even Rouault $
3  *
4  * Project:  ENVI .hdr Driver
5  * Purpose:  Implementation of ENVI .hdr labelled raw raster support.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  * Maintainer: Chris Padwick (cpadwick at ittvis.com)
8  *
9  ******************************************************************************
10  * Copyright (c) 2002, Frank Warmerdam
11  * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef GDAL_FRMTS_RAW_ENVIDATASET_H_INCLUDED
33 #define GDAL_FRMTS_RAW_ENVIDATASET_H_INCLUDED
34 
35 #include "cpl_port.h"
36 #include "rawdataset.h"
37 
38 #include <climits>
39 #include <cmath>
40 #include <cstdlib>
41 #include <cstring>
42 #if HAVE_FCNTL_H
43 #  include <fcntl.h>
44 #endif
45 
46 #include <algorithm>
47 #include <limits>
48 #include <string>
49 
50 #include "cpl_conv.h"
51 #include "cpl_error.h"
52 #include "cpl_string.h"
53 #include "cpl_vsi.h"
54 #include "gdal.h"
55 #include "gdal_frmts.h"
56 #include "gdal_priv.h"
57 #include "ogr_core.h"
58 #include "ogr_spatialref.h"
59 #include "ogr_srs_api.h"
60 
61 class ENVIRasterBand;
62 
63 class ENVIDataset final: public RawDataset
64 {
65     friend class ENVIRasterBand;
66 
67     VSILFILE   *fpImage;  // Image data file.
68     VSILFILE   *fp;  // Header file
69     char       *pszHDRFilename;
70 
71     bool        bFoundMapinfo;
72     bool        bHeaderDirty;
73     bool        bFillFile;
74 
75     double      adfGeoTransform[6];
76 
77     char       *pszProjection;
78 
79     CPLStringList m_aosHeader{};
80 
81     CPLString   osStaFilename{};
82 
83     std::vector<GDAL_GCP> m_asGCPs{};
84 
85     bool        ReadHeader( VSILFILE * );
86     bool        ProcessMapinfo( const char * );
87     void        ProcessRPCinfo( const char *, int, int);
88     void        ProcessGeoPoints( const char* );
89     void        ProcessStatsFile();
90     static int         byteSwapInt(int);
91     static float       byteSwapFloat(float);
92     static double      byteSwapDouble(double);
93     static void        SetENVIDatum( OGRSpatialReference *, const char * );
94     static void        SetENVIEllipse( OGRSpatialReference *, char ** );
95     void        WriteProjectionInfo();
96     bool        ParseRpcCoeffsMetaDataString( const char *psName,
97                                               char *papszVal[], int& idx );
98     bool        WriteRpcInfo();
99     bool        WritePseudoGcpInfo();
100 
SetFillFile()101     void        SetFillFile() { bFillFile = true; }
102 
103     char        **SplitList( const char * );
104 
105     enum Interleave { BSQ, BIL, BIP } interleave;
106     static int GetEnviType(GDALDataType eType);
107 
108     CPL_DISALLOW_COPY_ASSIGN(ENVIDataset)
109 
110   public:
111     ENVIDataset();
112     ~ENVIDataset() override;
113 
114     void    FlushCache() override;
115     CPLErr  GetGeoTransform( double *padfTransform ) override;
116     CPLErr  SetGeoTransform( double * ) override;
117     const char *_GetProjectionRef() override;
118     CPLErr  _SetProjection( const char * ) override;
GetSpatialRef()119     const OGRSpatialReference* GetSpatialRef() const override {
120         return GetSpatialRefFromOldGetProjectionRef();
121     }
SetSpatialRef(const OGRSpatialReference * poSRS)122     CPLErr SetSpatialRef(const OGRSpatialReference* poSRS) override {
123         return OldSetProjectionFromSetSpatialRef(poSRS);
124     }
125     char  **GetFileList() override;
126 
127     void SetDescription( const char * ) override;
128 
129     CPLErr SetMetadata( char **papszMetadata,
130                         const char *pszDomain = "" ) override;
131     CPLErr SetMetadataItem( const char *pszName,
132                             const char *pszValue,
133                             const char *pszDomain = "" ) override;
134     CPLErr SetGCPs( int nGCPCount, const GDAL_GCP *pasGCPList,
135                     const OGRSpatialReference* poSRS ) override;
136     int    GetGCPCount() override;
137     const GDAL_GCP *GetGCPs() override;
138 
139     bool GetRawBinaryLayout(GDALDataset::RawBinaryLayout&) override;
140 
141     static GDALDataset *Open( GDALOpenInfo * );
142     static ENVIDataset *Open( GDALOpenInfo *, bool bFileSizeCheck );
143     static GDALDataset *Create( const char *pszFilename,
144                                 int nXSize, int nYSize, int nBands,
145                                 GDALDataType eType, char ** papszOptions );
146 };
147 
148 /************************************************************************/
149 /* ==================================================================== */
150 /*                            ENVIRasterBand                            */
151 /* ==================================================================== */
152 /************************************************************************/
153 
154 class ENVIRasterBand final: public RawRasterBand
155 {
156     CPL_DISALLOW_COPY_ASSIGN(ENVIRasterBand)
157 
158   public:
159     ENVIRasterBand( GDALDataset *poDSIn, int nBandIn, VSILFILE *fpRawIn,
160                     vsi_l_offset nImgOffsetIn, int nPixelOffsetIn,
161                     int nLineOffsetIn, GDALDataType eDataTypeIn,
162                     int bNativeOrderIn );
~ENVIRasterBand()163     ~ENVIRasterBand() override {}
164 
165     void SetDescription( const char * ) override;
166     CPLErr SetNoDataValue( double ) override;
167 
168     CPLErr SetCategoryNames( char ** ) override;
169 };
170 
171 #endif  // GDAL_FRMTS_RAW_ENVIDATASET_H_INCLUDED
172