00001 #include "image.h" 00002 00003 #include "gdalfrmt.h" 00004 #include "hdfeosfrmt.h" 00005 #include "l7a_geotiff.h" 00006 #include "l7a_nlaps.h" 00007 #include "l7a_fast.h" 00008 #include "nlapsfrmt.h" 00009 00010 #include "TraceLog.h" 00011 00012 std::string Image::MFF_NAMES[] = { 00013 "Landsat 7 Geotiff", 00014 "Landsat 7 NLAPS", 00015 "Landsat 7 FastL7A" 00016 }; 00017 00018 int Image::NUM_MFF = 3; 00019 00020 00021 Image::Image( ImageFormat *imgf ) : 00022 mImgFrmt( imgf ) { 00023 std::vector<ImageFormat::BandInf> bandset = imgf->getBandInfSet(); 00024 00025 mWidth = 0; 00026 mHeight = 0; 00027 00028 // GET THE LARGEST DIMENSIONS FOR BOTH X & Y 00029 for ( unsigned int i=0; i < bandset.size(); i++ ) { 00030 if ( bandset[i].width > mWidth ) 00031 mWidth = bandset[i].width; 00032 if ( bandset[i].height > mHeight ) 00033 mHeight = bandset[i].height; 00034 } 00035 00036 mState.mRed = -1; 00037 mState.mGreen = -1; 00038 mState.mBlue = -1; 00039 mState.mColor = false; 00040 00041 mMemFrmt = NULL; 00042 } 00043 00044 Image::~Image( ) { 00045 if ( mImgFrmt ) 00046 delete mImgFrmt; 00047 00048 if ( mMemFrmt ) 00049 delete mMemFrmt; 00050 } 00051 00052 Image* Image::openImage( std::string fname, MultiFileType type ) { 00053 Image *retimg = NULL; 00054 switch ( type ) { 00055 case LSAT7_GEOTIFF: 00056 { 00057 L7A_Geotiff *l7a_g = new L7A_Geotiff; 00058 if ( l7a_g->open(fname ) ) 00059 retimg = new Image( l7a_g ); 00060 else { 00061 delete l7a_g; 00062 return NULL; 00063 } 00064 } 00065 break; 00066 00067 case LSAT7_NLAPS: 00068 { 00069 L7A_Nlaps *l7a_n = new L7A_Nlaps; 00070 if ( l7a_n->open(fname ) ) 00071 retimg = new Image( l7a_n ); 00072 else { 00073 delete l7a_n; 00074 return NULL; 00075 } 00076 } 00077 break; 00078 00079 case LSAT7_FASTL7A: 00080 return NULL; 00081 break; 00082 00083 default: 00084 break; 00085 } 00086 00087 GDALFrmt *gdal = new GDALFrmt; 00088 if ( !retimg && gdal->open( fname ) ) 00089 retimg = new Image( gdal ); 00090 else 00091 delete gdal; 00092 00093 HdfEosFrmt *hdfeos = new HdfEosFrmt; 00094 if ( !retimg && hdfeos->open( fname ) ) 00095 retimg = new Image( hdfeos ); 00096 else 00097 delete hdfeos; 00098 00099 NlapsFrmt *nlaps = new NlapsFrmt; 00100 if ( !retimg && nlaps->open( fname ) ) 00101 retimg = new Image( nlaps ); 00102 else 00103 delete nlaps; 00104 00105 if ( retimg ) { 00106 ImageState state; 00107 state.mFileName = fname; 00108 state.mType = type; 00109 retimg->setImageState( state ); 00110 return retimg; 00111 } 00112 return NULL; 00113 } 00114 00115 QImage* Image::getRect( 00116 int x1, 00117 int y1, 00118 int w1, 00119 int h1, 00120 int w2, 00121 int h2, 00122 ImageFormat::ScaleType st ) { 00123 double b1xs, b2xs, b3xs; // X SCALE FOR ALL BANDS 00124 double b1ys, b2ys, b3ys; // Y SCALE FOR ALL BANDS 00125 void *data[3]; // DATA PTR FOR RAW DATA 00126 QImage *retimg; // QIMAGE PTR FOR RETURN 00127 int imgw, imgh; // BAND WIDTH/HEIGHT 00128 00129 if ( mState.mRed == -1 ) 00130 return NULL; 00131 00132 std::vector<ImageFormat::BandInf> bandset = // BAND INFO FOR IMAGE 00133 mImgFrmt->getBandInfSet(); 00134 00135 if ( w2 == 0 ) 00136 w2 = w1; 00137 if ( h2 == 0 ) 00138 h2 = h1; 00139 00140 imgw = width(); 00141 imgh = height(); 00142 00143 // COMPARE BAND DIMS TO REQUESTED AND GET NEEDED DATA DIMS FOR ALL BANDS 00144 b1xs = (double)bandset[mState.mRed].width / (double)imgw; 00145 b2xs = (double)bandset[mState.mGreen].width / (double)imgw; 00146 b3xs = (double)bandset[mState.mBlue].width / (double)imgw; 00147 b1ys = (double)bandset[mState.mRed].height / (double)imgh; 00148 b2ys = (double)bandset[mState.mGreen].height / (double)imgh; 00149 b3ys = (double)bandset[mState.mBlue].height / (double)imgh; 00150 00151 // GET THE DATA FROM THE FORMAT 00152 data[0] = mImgFrmt->getBand( mState.mRed, 00153 (int)(x1*b1xs), 00154 (int)(y1*b1ys), 00155 (int)(w1*b1xs), 00156 (int)(h1*b1ys), 00157 w2, 00158 h2, 00159 st ); 00160 if ( mState.mRed == mState.mGreen ) 00161 data[1] = data[0]; 00162 else 00163 data[1] = mImgFrmt->getBand( mState.mGreen, 00164 (int)(x1*b2xs), 00165 (int)(y1*b2ys), 00166 (int)(w1*b2xs), 00167 (int)(h1*b2ys), 00168 w2, 00169 h2, 00170 st ); 00171 00172 if ( mState.mRed == mState.mBlue ) 00173 data[2] = data[0]; 00174 else if ( mState.mGreen == mState.mBlue ) 00175 data[2] = data[1]; 00176 else 00177 data[2] = mImgFrmt->getBand( mState.mBlue, 00178 (int)(x1*b3xs), 00179 (int)(y1*b3ys), 00180 (int)(w1*b3xs), 00181 (int)(h1*b3ys), 00182 w2, 00183 h2, 00184 st ); 00185 00186 if ( data[0] == NULL || data[1] == NULL || data[2] == NULL ) 00187 return new QImage( w2, h2, 32 ); 00188 00189 // SCALE DATA TO UNSIGNED BYTE FOR IMAGE 00190 for ( int i=0; i < 3; i++ ) { 00191 int sz = w2 * h2; 00192 int iband=0; 00193 if ( i == 0 ) 00194 iband = mState.mRed; 00195 else if ( i == 1 ) 00196 iband = mState.mGreen; 00197 else 00198 iband = mState.mBlue; 00199 00200 double max = bandset[iband].max; 00201 double min = bandset[iband].min; 00202 00203 if ( i == 1 && mState.mGreen == mState.mRed ) { 00204 data[1] = data[0]; 00205 continue; 00206 } 00207 00208 if ( i == 2 && (mState.mRed == mState.mBlue || mState.mGreen == mState.mBlue) ) { 00209 data[2] = data[0]; 00210 continue; 00211 } 00212 00213 ImageFormat::DataType dt = (ImageFormat::DataType)0; 00214 if ( i == 0 ) 00215 dt = bandset[mState.mRed].type; 00216 if ( i == 1 ) 00217 dt = bandset[mState.mGreen].type; 00218 if ( i == 2 ) 00219 dt = bandset[mState.mBlue].type; 00220 00221 switch ( dt ) { 00222 case ImageFormat::INT8: 00223 min = -127; 00224 max = 128; 00225 data[i] = scaleToUC( (char*)data[i], min, max, sz ); 00226 break; 00227 00228 case ImageFormat::UINT8: 00229 break; 00230 00231 case ImageFormat::INT16: 00232 data[i] = scaleToUC( (short*)data[i], min, max, sz ); 00233 break; 00234 00235 case ImageFormat::UINT16: 00236 data[i] = scaleToUC( (unsigned short*)data[i], min, max, sz ); 00237 break; 00238 00239 default: 00240 break; 00241 } 00242 } 00243 00244 // RESIZE(GROW) SMALLER DATA 00245 00246 // CONSTRUCT QIMAGE 00247 retimg = new QImage( w2, h2, 32 ); 00248 makeRGBA( retimg->bits(), 00249 (unsigned char*)data[0], 00250 (unsigned char*)data[1], 00251 (unsigned char*)data[2], 00252 w2, 00253 h2 ); 00254 00255 delete [] (char*)data[0]; 00256 if ( !(mState.mRed == mState.mGreen) ) 00257 delete [] (char*)data[1]; 00258 00259 if ( !(mState.mRed == mState.mBlue || mState.mGreen == mState.mBlue) ) 00260 delete [] (char*)data[2]; 00261 00262 return retimg; 00263 } 00264 00265 int Image::width( ) { 00266 int w = 0; 00267 std::vector<ImageFormat::BandInf> bandset = // BAND INFO FOR IMAGE 00268 mImgFrmt->getBandInfSet(); 00269 00270 if ( mState.mRed >= 0 && mState.mRed < (int)bandset.size() ) 00271 w = bandset[mState.mRed].width; 00272 if ( mState.mGreen >= 0 && mState.mGreen < (int)bandset.size() ) 00273 w = MAX( bandset[mState.mGreen].width, w ); 00274 if ( mState.mBlue >= 0 && mState.mBlue < (int)bandset.size() ) 00275 w = MAX( bandset[mState.mBlue].width, w ); 00276 00277 return w; 00278 } 00279 00280 int Image::height( ) { 00281 int h = 0; 00282 std::vector<ImageFormat::BandInf> bandset = // BAND INFO FOR IMAGE 00283 mImgFrmt->getBandInfSet(); 00284 00285 if ( mState.mRed >= 0 && mState.mRed < (int)bandset.size() ) 00286 h = bandset[mState.mRed].height; 00287 if ( mState.mGreen >= 0 && mState.mGreen < (int)bandset.size() ) 00288 h = MAX( bandset[mState.mGreen].height, h ); 00289 if ( mState.mBlue >= 0 && mState.mBlue < (int)bandset.size() ) 00290 h = MAX( bandset[mState.mBlue].height, h ); 00291 00292 return h; 00293 } 00294 00295 int Image::width( int bind ) { 00296 int w = 0; 00297 std::vector<ImageFormat::BandInf> bandset = // BAND INFO FOR IMAGE 00298 mImgFrmt->getBandInfSet(); 00299 00300 if ( bind >= 0 && bind < (int)bandset.size() ) 00301 w = bandset[bind].width; 00302 00303 return w; 00304 } 00305 00306 int Image::height( int bind ) { 00307 int h = 0; 00308 std::vector<ImageFormat::BandInf> bandset = // BAND INFO FOR IMAGE 00309 mImgFrmt->getBandInfSet(); 00310 00311 if ( bind >= 0 && bind < (int)bandset.size() ) 00312 h = bandset[bind].height; 00313 00314 return h; 00315 } 00316 00317 std::vector<ImageFormat::BandInf>& 00318 Image::getBandInfSet( ) const { 00319 return mImgFrmt->getBandInfSet(); 00320 } 00321 00322 std::string Image::getName( ) const { 00323 return mImgFrmt->getName(); 00324 } 00325 00326 template<class DType> 00327 unsigned char* Image::scaleToUC( DType *data, 00328 double min, 00329 double max, 00330 int len ) { 00331 unsigned char *retdata = new unsigned char[ len ]; 00332 00333 min *= -1; 00334 for ( int i=0; i < len; i++ ) 00335 retdata[i] = (unsigned char)((data[i] + min) * 255.0 / (max+min)); 00336 00337 delete [] data; 00338 return retdata; 00339 } 00340 00341 void Image::makeRGBA( unsigned char* imbits, 00342 unsigned char* rData, 00343 unsigned char* gData, 00344 unsigned char* bData, 00345 int newWidth, 00346 int newHeight ) { 00347 int ibdata; 00348 int iidata; 00349 for ( int y=0; y < newHeight; y++ ) { 00350 for ( int x=0; x < newWidth; x++ ) { 00351 ibdata = newWidth * y + x; 00352 iidata = (newWidth * y * 4) + x * 4; 00353 00354 imbits[iidata++] = bData[ibdata]; 00355 imbits[iidata++] = gData[ibdata]; 00356 imbits[iidata++] = rData[ibdata]; 00357 imbits[iidata] = 0xff; 00358 } 00359 } 00360 } 00361 00362 void Image::getLL( double &x, double &y ) { 00363 mImgFrmt->getLL( mState.mRed, x, y ); 00364 } 00365 00366 void Image::getXY( double &x, double &y ) { 00367 mImgFrmt->getXY( mState.mRed, x, y ); 00368 } 00369 00370 void Image::setMemBuf( bool state ) { 00371 if ( state ) { 00372 if ( mMemFrmt ) 00373 return; 00374 00375 mMemFrmt = new InMemFormat( mImgFrmt, &mState ); 00376 mImgFrmt = mMemFrmt; 00377 } else { 00378 if ( !mMemFrmt ) 00379 return; 00380 00381 mImgFrmt = mMemFrmt->getFormat(); 00382 delete mMemFrmt; 00383 mMemFrmt = NULL; 00384 } 00385 } 00386 00387 00388 00389
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |