1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 
42 #ifndef __GRFMT_GDAL_HPP__
43 #define __GRFMT_GDAL_HPP__
44 
45 /// OpenCV FMT Base Type
46 #include "grfmt_base.hpp"
47 
48 /// Macro to make sure we specified GDAL in CMake
49 #ifdef HAVE_GDAL
50 
51 /// C++ Libraries
52 #include <iostream>
53 
54 /// Geospatial Data Abstraction Library
55 #include <cpl_conv.h>
56 #include <gdal_priv.h>
57 #include <gdal.h>
58 
59 
60 /// Start of CV Namespace
61 namespace cv {
62 
63 /**
64  * Convert GDAL Pixel Range to OpenCV Pixel Range
65 */
66 double range_cast( const GDALDataType& gdalType,
67                    const int& cvDepth,
68                    const double& value );
69 
70 /**
71  * Convert GDAL Palette Interpretation to OpenCV Pixel Type
72 */
73 int gdalPaletteInterpretation2OpenCV( GDALPaletteInterp const& paletteInterp,
74                                       GDALDataType const& gdalType );
75 
76 /**
77  * Convert a GDAL Raster Type to OpenCV Type
78 */
79 int gdal2opencv( const GDALDataType& gdalType, const int& channels );
80 
81 /**
82  * Write an image to pixel
83 */
84 void write_pixel( const double& pixelValue,
85                   GDALDataType const& gdalType,
86                   const int& gdalChannels,
87                   Mat& image,
88                   const int& row,
89                   const int& col,
90                   const int& channel );
91 
92 /**
93  * Write a color table pixel to the image
94 */
95 void write_ctable_pixel( const double& pixelValue,
96                          const GDALDataType& gdalType,
97                          const GDALColorTable* gdalColorTable,
98                          Mat& image,
99                          const int& y,
100                          const int& x,
101                          const int& c );
102 
103 /**
104  * Loader for GDAL
105 */
106 class GdalDecoder CV_FINAL : public BaseImageDecoder{
107 
108     public:
109 
110         /**
111          * Default Constructor
112         */
113         GdalDecoder();
114 
115         /**
116          * Destructor
117         */
118         ~GdalDecoder() CV_OVERRIDE;
119 
120         /**
121          * Read image data
122         */
123         bool readData( Mat& img ) CV_OVERRIDE;
124 
125         /**
126          * Read the image header
127         */
128         bool readHeader() CV_OVERRIDE;
129 
130         /**
131          * Close the module
132         */
133         void close();
134 
135         /**
136          * Create a new decoder
137         */
138         ImageDecoder newDecoder() const CV_OVERRIDE;
139 
140         /**
141          * Test the file signature
142          *
143          * In general, this should be avoided as the user should specifically request GDAL.
144          * The reason is that GDAL tends to overlap with other image formats and it is probably
145          * safer to use other formats first.
146         */
147         virtual bool checkSignature( const String& signature ) const CV_OVERRIDE;
148 
149     protected:
150 
151         /// GDAL Dataset
152         GDALDataset* m_dataset;
153 
154         /// GDAL Driver
155         GDALDriver* m_driver;
156 
157         /// Check if we are reading from a color table
158         bool hasColorTable;
159 
160 }; /// End of GdalDecoder Class
161 
162 } /// End of Namespace cv
163 
164 #endif/*HAVE_GDAL*/
165 
166 #endif/*__GRFMT_GDAL_HPP__*/
167