1 : /******************************************************************************
2 : * $Id: ceosdataset.cpp 20504 2010-09-02 02:40:49Z warmerdam $
3 : *
4 : * Project: CEOS Translator
5 : * Purpose: GDALDataset driver for CEOS translator.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "ceosopen.h"
31 : #include "gdal_pam.h"
32 :
33 : CPL_CVSID("$Id: ceosdataset.cpp 20504 2010-09-02 02:40:49Z warmerdam $");
34 :
35 : CPL_C_START
36 : void GDALRegister_CEOS(void);
37 : CPL_C_END
38 :
39 : /************************************************************************/
40 : /* ==================================================================== */
41 : /* CEOSDataset */
42 : /* ==================================================================== */
43 : /************************************************************************/
44 :
45 : class CEOSRasterBand;
46 :
47 : class CEOSDataset : public GDALPamDataset
48 : {
49 : friend class CEOSRasterBand;
50 :
51 : CEOSImage *psCEOS;
52 :
53 : public:
54 : CEOSDataset();
55 : ~CEOSDataset();
56 : static GDALDataset *Open( GDALOpenInfo * );
57 : };
58 :
59 : /************************************************************************/
60 : /* ==================================================================== */
61 : /* CEOSRasterBand */
62 : /* ==================================================================== */
63 : /************************************************************************/
64 :
65 : class CEOSRasterBand : public GDALPamRasterBand
66 4 : {
67 : friend class CEOSDataset;
68 :
69 : public:
70 :
71 : CEOSRasterBand( CEOSDataset *, int );
72 :
73 : virtual CPLErr IReadBlock( int, int, void * );
74 : };
75 :
76 :
77 : /************************************************************************/
78 : /* CEOSRasterBand() */
79 : /************************************************************************/
80 :
81 4 : CEOSRasterBand::CEOSRasterBand( CEOSDataset *poDS, int nBand )
82 :
83 : {
84 4 : this->poDS = poDS;
85 4 : this->nBand = nBand;
86 :
87 4 : eDataType = GDT_Byte;
88 :
89 4 : nBlockXSize = poDS->GetRasterXSize();
90 4 : nBlockYSize = 1;
91 4 : }
92 :
93 : /************************************************************************/
94 : /* IReadBlock() */
95 : /************************************************************************/
96 :
97 3 : CPLErr CEOSRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff,
98 : void * pImage )
99 :
100 : {
101 3 : CEOSDataset *poCEOS_DS = (CEOSDataset *) poDS;
102 :
103 3 : CPLAssert( nBlockXOff == 0 );
104 :
105 3 : return( CEOSReadScanline(poCEOS_DS->psCEOS, nBand, nBlockYOff+1, pImage) );
106 : }
107 :
108 : /************************************************************************/
109 : /* ==================================================================== */
110 : /* CEOSDataset */
111 : /* ==================================================================== */
112 : /************************************************************************/
113 :
114 : /************************************************************************/
115 : /* CEOSDataset() */
116 : /************************************************************************/
117 :
118 1 : CEOSDataset::CEOSDataset()
119 :
120 : {
121 1 : psCEOS = NULL;
122 1 : }
123 :
124 : /************************************************************************/
125 : /* ~CEOSDataset() */
126 : /************************************************************************/
127 :
128 1 : CEOSDataset::~CEOSDataset()
129 :
130 : {
131 1 : FlushCache();
132 1 : if( psCEOS )
133 1 : CEOSClose( psCEOS );
134 1 : }
135 :
136 : /************************************************************************/
137 : /* Open() */
138 : /************************************************************************/
139 :
140 12899 : GDALDataset *CEOSDataset::Open( GDALOpenInfo * poOpenInfo )
141 :
142 : {
143 : CEOSImage *psCEOS;
144 : int i;
145 :
146 : /* -------------------------------------------------------------------- */
147 : /* Before trying CEOSOpen() we first verify that the first */
148 : /* record is in fact a CEOS file descriptor record. */
149 : /* -------------------------------------------------------------------- */
150 12899 : if( poOpenInfo->nHeaderBytes < 100 )
151 11053 : return NULL;
152 :
153 1849 : if( poOpenInfo->pabyHeader[4] != 0x3f
154 1 : || poOpenInfo->pabyHeader[5] != 0xc0
155 1 : || poOpenInfo->pabyHeader[6] != 0x12
156 1 : || poOpenInfo->pabyHeader[7] != 0x12 )
157 1845 : return NULL;
158 :
159 : /* -------------------------------------------------------------------- */
160 : /* Try opening the dataset. */
161 : /* -------------------------------------------------------------------- */
162 1 : psCEOS = CEOSOpen( poOpenInfo->pszFilename, "rb" );
163 :
164 1 : if( psCEOS == NULL )
165 0 : return( NULL );
166 :
167 1 : if( psCEOS->nBitsPerPixel != 8 )
168 : {
169 : CPLError( CE_Failure, CPLE_NotSupported,
170 : "The CEOS driver cannot handle nBitsPerPixel = %d",
171 0 : psCEOS->nBitsPerPixel );
172 0 : CEOSClose(psCEOS);
173 0 : return NULL;
174 : }
175 :
176 1 : if( !GDALCheckDatasetDimensions(psCEOS->nPixels, psCEOS->nBands) ||
177 : !GDALCheckBandCount(psCEOS->nBands, FALSE) )
178 : {
179 0 : CEOSClose( psCEOS );
180 0 : return NULL;
181 : }
182 :
183 : /* -------------------------------------------------------------------- */
184 : /* Confirm the requested access is supported. */
185 : /* -------------------------------------------------------------------- */
186 1 : if( poOpenInfo->eAccess == GA_Update )
187 : {
188 0 : CEOSClose(psCEOS);
189 : CPLError( CE_Failure, CPLE_NotSupported,
190 : "The CEOS driver does not support update access to existing"
191 0 : " datasets.\n" );
192 0 : return NULL;
193 : }
194 : /* -------------------------------------------------------------------- */
195 : /* Create a corresponding GDALDataset. */
196 : /* -------------------------------------------------------------------- */
197 : CEOSDataset *poDS;
198 :
199 1 : poDS = new CEOSDataset();
200 :
201 1 : poDS->psCEOS = psCEOS;
202 :
203 : /* -------------------------------------------------------------------- */
204 : /* Capture some information from the file that is of interest. */
205 : /* -------------------------------------------------------------------- */
206 1 : poDS->nRasterXSize = psCEOS->nPixels;
207 1 : poDS->nRasterYSize = psCEOS->nLines;
208 :
209 : /* -------------------------------------------------------------------- */
210 : /* Create band information objects. */
211 : /* -------------------------------------------------------------------- */
212 1 : poDS->nBands = psCEOS->nBands;;
213 :
214 10 : for( i = 0; i < poDS->nBands; i++ )
215 4 : poDS->SetBand( i+1, new CEOSRasterBand( poDS, i+1 ) );
216 :
217 : /* -------------------------------------------------------------------- */
218 : /* Initialize any PAM information. */
219 : /* -------------------------------------------------------------------- */
220 1 : poDS->SetDescription( poOpenInfo->pszFilename );
221 1 : poDS->TryLoadXML();
222 :
223 : /* -------------------------------------------------------------------- */
224 : /* Check for overviews. */
225 : /* -------------------------------------------------------------------- */
226 1 : poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename );
227 :
228 1 : return( poDS );
229 : }
230 :
231 : /************************************************************************/
232 : /* GDALRegister_GTiff() */
233 : /************************************************************************/
234 :
235 558 : void GDALRegister_CEOS()
236 :
237 : {
238 : GDALDriver *poDriver;
239 :
240 558 : if( GDALGetDriverByName( "CEOS" ) == NULL )
241 : {
242 537 : poDriver = new GDALDriver();
243 :
244 537 : poDriver->SetDescription( "CEOS" );
245 : poDriver->SetMetadataItem( GDAL_DMD_LONGNAME,
246 537 : "CEOS Image" );
247 : poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC,
248 537 : "frmt_various.html#CEOS" );
249 537 : poDriver->SetMetadataItem( GDAL_DCAP_VIRTUALIO, "YES" );
250 :
251 537 : poDriver->pfnOpen = CEOSDataset::Open;
252 :
253 537 : GetGDALDriverManager()->RegisterDriver( poDriver );
254 : }
255 558 : }
|