1 : /******************************************************************************
2 : * $Id: ceosdataset.cpp 17664 2009-09-21 21:16:45Z rouault $
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 17664 2009-09-21 21:16:45Z rouault $");
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 8 : {
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 : 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 2 : CEOSDataset::~CEOSDataset()
129 :
130 : {
131 1 : FlushCache();
132 1 : if( psCEOS )
133 1 : CEOSClose( psCEOS );
134 2 : }
135 :
136 : /************************************************************************/
137 : /* Open() */
138 : /************************************************************************/
139 :
140 9943 : 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 9943 : if( poOpenInfo->nHeaderBytes < 100 )
151 8764 : return NULL;
152 :
153 1182 : if( poOpenInfo->pabyHeader[4] != 0x3f
154 1 : || poOpenInfo->pabyHeader[5] != 0xc0
155 1 : || poOpenInfo->pabyHeader[6] != 0x12
156 1 : || poOpenInfo->pabyHeader[7] != 0x12 )
157 1178 : 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 : /* -------------------------------------------------------------------- */
168 : /* Confirm the requested access is supported. */
169 : /* -------------------------------------------------------------------- */
170 1 : if( poOpenInfo->eAccess == GA_Update )
171 : {
172 0 : CEOSClose(psCEOS);
173 : CPLError( CE_Failure, CPLE_NotSupported,
174 : "The CEOS driver does not support update access to existing"
175 0 : " datasets.\n" );
176 0 : return NULL;
177 : }
178 : /* -------------------------------------------------------------------- */
179 : /* Create a corresponding GDALDataset. */
180 : /* -------------------------------------------------------------------- */
181 : CEOSDataset *poDS;
182 :
183 1 : poDS = new CEOSDataset();
184 :
185 1 : poDS->psCEOS = psCEOS;
186 :
187 : /* -------------------------------------------------------------------- */
188 : /* Capture some information from the file that is of interest. */
189 : /* -------------------------------------------------------------------- */
190 1 : poDS->nRasterXSize = psCEOS->nPixels;
191 1 : poDS->nRasterYSize = psCEOS->nLines;
192 :
193 : /* -------------------------------------------------------------------- */
194 : /* Create band information objects. */
195 : /* -------------------------------------------------------------------- */
196 1 : poDS->nBands = psCEOS->nBands;;
197 :
198 10 : for( i = 0; i < poDS->nBands; i++ )
199 4 : poDS->SetBand( i+1, new CEOSRasterBand( poDS, i+1 ) );
200 :
201 : /* -------------------------------------------------------------------- */
202 : /* Initialize any PAM information. */
203 : /* -------------------------------------------------------------------- */
204 1 : poDS->SetDescription( poOpenInfo->pszFilename );
205 1 : poDS->TryLoadXML();
206 :
207 : /* -------------------------------------------------------------------- */
208 : /* Check for overviews. */
209 : /* -------------------------------------------------------------------- */
210 1 : poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename );
211 :
212 1 : return( poDS );
213 : }
214 :
215 : /************************************************************************/
216 : /* GDALRegister_GTiff() */
217 : /************************************************************************/
218 :
219 338 : void GDALRegister_CEOS()
220 :
221 : {
222 : GDALDriver *poDriver;
223 :
224 338 : if( GDALGetDriverByName( "CEOS" ) == NULL )
225 : {
226 336 : poDriver = new GDALDriver();
227 :
228 336 : poDriver->SetDescription( "CEOS" );
229 : poDriver->SetMetadataItem( GDAL_DMD_LONGNAME,
230 336 : "CEOS Image" );
231 : poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC,
232 336 : "frmt_various.html#CEOS" );
233 :
234 336 : poDriver->pfnOpen = CEOSDataset::Open;
235 :
236 336 : GetGDALDriverManager()->RegisterDriver( poDriver );
237 : }
238 338 : }
239 :
|