00001
00002
00003
00004
00005
00006
00007
00008 #include "TeTestsBase.hpp"
00009
00010 #include <terralib/kernel/TeDecoderSmartMem.h>
00011 #include <terralib/kernel/TeDecoderMemory.h>
00012 #include <terralib/kernel/TeDecoderMemoryMap.h>
00013
00014 #ifdef ENABLE_PAMDECODER_TEST
00015 #include <terralib/kernel/TeDecoderPAM.h>
00016 #endif
00017
00018 #include <terralib/kernel/TeException.h>
00019 #include <terralib/kernel/TeUtils.h>
00020 #include <terralib/kernel/TeInitRasterDecoders.h>
00021 #include <terralib/kernel/TeAgnostic.h>
00022 #include <terralib/kernel/TeProgress.h>
00023 #include <terralib/kernel/TeStdIOProgress.h>
00024
00025 #include <stdlib.h>
00026
00027
00028 void DecoderSmartMem_test()
00029 {
00030
00031
00032
00033 const int columns = 100;
00034 const int lines = 100;
00035 const unsigned int datasize = sizeof(unsigned long int);
00036 const unsigned long int curr_free_ram = (unsigned long int)
00037 ( 0.05 * (double)MIN( ( TeGetTotalVirtualMemory() / 2.0 ) -
00038 TeGetUsedVirtualMemory(), TeGetTotalPhysicalMemory() ) );
00039 const int bands = ( ( 2 * curr_free_ram ) /
00040 ( lines * columns * datasize ) ) + 1;
00041
00042 TeRasterParams params;
00043 params.setNLinesNColumns( lines, columns );
00044 params.nBands( bands );
00045 params.setDataType( TeUNSIGNEDLONG, -1 );
00046 params.setDummy( 0, -1 );
00047 params.decoderIdentifier_ = "SMARTMEM";
00048
00049 TeRasterParams::DecoderParamT decParm;
00050 decParm.first = "MaxMemPercentUsage";
00051 decParm.second = "5";
00052 params.decoderParams_.push_back( decParm );
00053
00054 TeSharedPtr< TeDecoderSmartMem > decoder( (TeDecoderSmartMem*)
00055 TeDecoderFactory::make( params ) );
00056 if( ! decoder.isActive() ) {
00057 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder instantiation error",
00058 false ) );
00059 }
00060
00061 decoder->init();
00062
00063 if( decoder->params().status_ != TeRasterParams::TeReadyToRead ) {
00064 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder initialization error",
00065 false ) );
00066 }
00067
00068 double value_in_double = 0;
00069 double value_out_double = 0;
00070 int band = 0;
00071 int line = 0;
00072 int col = 0;
00073
00074
00075
00076 for( band = 0 ; band < bands ; ++band ) {
00077 for( line = 0 ; line < lines ; ++line ) {
00078 for( col = 0 ; col < columns ; ++col ) {
00079 if( ! decoder->setElement( col, line, value_in_double, band ) ) {
00080 throw( TeException( UNKNOWN_ERROR_TYPE , "setElement error", false ) );
00081 }
00082
00083 value_out_double = 0;
00084
00085 if( ! decoder->getElement( col, line, value_out_double, band ) ) {
00086 throw( TeException( UNKNOWN_ERROR_TYPE , "getElement error",
00087 false ) );
00088 }
00089
00090 TEAGN_CHECK_EPS( value_out_double, value_in_double, 0.0,
00091 "Invalid value read band=" + Te2String( band ) + " line=" +
00092 Te2String( line ) + " col=" + Te2String( col ) )
00093
00094 value_in_double += 1.0;
00095 }
00096 }
00097 }
00098
00099 value_in_double = 0;
00100 value_out_double = 0;
00101
00102 for( band = 0 ; band < bands ; ++band ) {
00103 for( line = 0 ; line < lines ; ++line ) {
00104 for( col = 0 ; col < columns ; ++col ) {
00105 if( ! decoder->getElement( col, line, value_out_double, band ) ) {
00106 throw( TeException( UNKNOWN_ERROR_TYPE , "getElement error",
00107 false ) );
00108 }
00109
00110 TEAGN_CHECK_EPS( value_out_double, value_in_double, 0.0,
00111 "Invalid value read band=" + Te2String( band ) + " line=" +
00112 Te2String( line ) + " col=" + Te2String( col ) )
00113
00114 value_in_double += 1.0;
00115 }
00116 }
00117 }
00118
00119
00120
00121 unsigned int value_in_uint = 0;
00122 unsigned int value_out_uint = 0;
00123
00124 for( band = 0 ; band < bands ; ++band )
00125 {
00126 for( line = 0 ; line < lines ; ++line )
00127 {
00128 for( col = 0 ; col < columns ; ++col )
00129 {
00130 decoder->setValue( col, line, value_in_uint, band );
00131
00132 value_in_uint += 1.0;
00133 }
00134 }
00135 }
00136
00137 value_in_uint = 0;
00138 value_out_uint = 0;
00139 unsigned int* linePtr = 0;
00140
00141 for( band = 0 ; band < bands ; ++band )
00142 {
00143 for( line = 0 ; line < lines ; ++line )
00144 {
00145 linePtr = (unsigned int*)decoder->getScanLine( line, band );
00146 TEAGN_TRUE_OR_THROW( linePtr, "Invalid line pointer" );
00147
00148 for( col = 0 ; col < columns ; ++col )
00149 {
00150 decoder->getValue( col, line, value_out_uint, band );
00151
00152 TEAGN_CHECK_EPS( value_out_uint, value_in_uint, 0.0,
00153 "Invalid value read band=" + Te2String( band ) + " line=" +
00154 Te2String( line ) + " col=" + Te2String( col ) )
00155
00156 TEAGN_CHECK_EPS( value_out_uint, linePtr[ col ], 0.0,
00157 "Invalid value read band=" + Te2String( band ) + " line=" +
00158 Te2String( line ) + " col=" + Te2String( col ) )
00159
00160 value_in_uint += 1.0;
00161 }
00162 }
00163 }
00164 }
00165
00166
00167 void DecoderMemory_test()
00168 {
00169 const unsigned int columns = 2000;
00170 const unsigned int lines = 2000;
00171 const unsigned int bands = 10;
00172
00173 TeRasterParams params;
00174 params.setNLinesNColumns( lines, columns );
00175 params.nBands( bands );
00176 params.setDataType( TeDOUBLE, -1 );
00177 params.setDummy( 0, -1 );
00178 params.decoderIdentifier_ = "MEM";
00179
00180 TeSharedPtr< TeDecoder > decoder( TeDecoderFactory::make( params ) );
00181 if( ! decoder.isActive() ) {
00182 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder instantiation error",
00183 false ) );
00184 }
00185
00186 decoder->init();
00187
00188 if( decoder->params().status_ != TeRasterParams::TeReadyToWrite ) {
00189 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder initialization error",
00190 false ) );
00191 }
00192
00193 double value_in = 0;
00194 unsigned int band = 0;
00195 unsigned int line = 0;
00196 unsigned int col = 0;
00197
00198 for( band = 0 ; band < bands ; ++band ) {
00199 for( line = 0 ; line < lines ; ++line ) {
00200 for( col = 0 ; col < columns ; ++col ) {
00201 if( ! decoder->setElement( col, line, value_in, band ) ) {
00202 throw( TeException( UNKNOWN_ERROR_TYPE , "setElement error", false ) );
00203 }
00204
00205 value_in += 1.0;
00206 }
00207 }
00208 }
00209
00210 value_in = 0;
00211 double value_out = 0;
00212
00213 for( band = 0 ; band < bands ; ++band ) {
00214 for( line = 0 ; line < lines ; ++line ) {
00215 for( col = 0 ; col < columns ; ++col ) {
00216 if( ! decoder->getElement( col, line, value_out, band ) ) {
00217 throw( TeException( UNKNOWN_ERROR_TYPE , "getElement error",
00218 false ) );
00219 }
00220
00221 if( value_out != value_in ) {
00222 throw( TeException( UNKNOWN_ERROR_TYPE , "invalid value read",
00223 false ) );
00224 }
00225
00226 value_in += 1.0;
00227 }
00228 }
00229 }
00230 }
00231
00232
00233 void TeDecoderTIFF_test()
00234 {
00235 const unsigned int columns = 5;
00236 const unsigned int lines = 5;
00237 const unsigned int bands = 2;
00238
00239 {
00240 TeRasterParams params;
00241 params.nBands( bands );
00242 params.setDataType( TeUNSIGNEDCHAR, -1 );
00243 params.setDummy( 0, -1 );
00244 params.fileName_ = "TeDecoderTIF_test.tif";
00245 params.mode_ = 'c';
00246 params.decoderIdentifier_ = "TIF";
00247 params.setNLinesNColumns( lines, columns );
00248
00249 TeSharedPtr< TeDecoder > decoder( TeDecoderFactory::make( params ) );
00250 if( ! decoder.isActive() ) {
00251 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder instantiation error",
00252 false ) );
00253 }
00254
00255 decoder->init();
00256
00257 if( decoder->params().status_ != TeRasterParams::TeReadyToWrite ) {
00258 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder initialization error",
00259 false ) );
00260 }
00261
00262 double value_out = 0;
00263 unsigned int band = 0;
00264 unsigned int line = 0;
00265 unsigned int col = 0;
00266
00267 for( band = 0 ; band < bands ; ++band ) {
00268 for( line = 0 ; line < lines ; ++line ) {
00269 for( col = 0 ; col < columns ; ++col ) {
00270 if( ! decoder->setElement( col, line, value_out, band ) ) {
00271 throw( TeException( UNKNOWN_ERROR_TYPE ,
00272 "setElement error at col=" +
00273 Te2String( col ) + " line=" + Te2String( line ),
00274 false ) );
00275 }
00276
00277 value_out += 1.0;
00278 }
00279 }
00280 }
00281
00282 decoder.reset();
00283 }
00284
00285 {
00286 TeRasterParams params;
00287 params.fileName_ = "TeDecoderTIF_test.tif";
00288 params.mode_ = 'r';
00289 params.decoderIdentifier_ = "TIF";
00290
00291 TeSharedPtr< TeDecoder > decoder( TeDecoderFactory::make( params ) );
00292 if( ! decoder.isActive() ) {
00293 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder instantiation error",
00294 false ) );
00295 }
00296
00297 decoder->init();
00298
00299 if( decoder->params().status_ != TeRasterParams::TeReadyToRead ) {
00300 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder initialization error",
00301 false ) );
00302 }
00303 if( decoder->params().ncols_ != (int)columns ) {
00304 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid columsn number " +
00305 Te2String( decoder->params().ncols_ ),
00306 false ) );
00307 }
00308 if( decoder->params().nlines_ != (int)lines ) {
00309 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid lines number " +
00310 Te2String( decoder->params().nlines_ ),
00311 false ) );
00312 }
00313 if( decoder->params().nBands() != (int)bands ) {
00314 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid channels number " +
00315 Te2String( decoder->params().nBands() ),
00316 false ) );
00317 }
00318 if( decoder->params().dataType_[ 0 ] != TeUNSIGNEDCHAR ) {
00319 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid data type",
00320 false ) );
00321 }
00322
00323 double value_in = 0;
00324 double value_counter = 0;
00325 unsigned int band = 0;
00326 unsigned int line = 0;
00327 unsigned int col = 0;
00328
00329 for( band = 0 ; band < bands ; ++band ) {
00330 for( line = 0 ; line < lines ; ++line ) {
00331 for( col = 0 ; col < columns ; ++col ) {
00332 if( ! decoder->getElement( col, line, value_in, band ) ) {
00333 throw( TeException( UNKNOWN_ERROR_TYPE , "getElement error",
00334 false ) );
00335 }
00336
00337 if( value_counter != value_in ) {
00338 throw( TeException( UNKNOWN_ERROR_TYPE , "invalid read value"
00339 " col=" + Te2String( col ) + " line=" + Te2String( line ) +
00340 " value_in=" + Te2String( value_in ) +
00341 " value_counter=" + Te2String( value_counter ),
00342 false ) );
00343 }
00344
00345 value_counter += 1.0;
00346 }
00347 }
00348 }
00349
00350 decoder.reset();
00351 }
00352 }
00353
00354 #ifdef ENABLE_PAMDECODER_TEST
00355 void TeDecoderPAM_test()
00356 {
00357 const unsigned int columns = 10;
00358 const unsigned int lines = 5;
00359 const unsigned int bands = 3;
00360
00361 {
00362 TeRasterParams params;
00363 params.setNLinesNColumns( lines, columns );
00364 params.nBands( bands );
00365 params.setDataType( TeUNSIGNEDCHAR, -1 );
00366 params.setDummy( 0, -1 );
00367 params.fileName_ = "TeDecoderPAM_test.ppm";
00368 params.mode_ = 'c';
00369 params.decoderIdentifier_ = "PAM";
00370
00371 TeSharedPtr< TeDecoder > decoder( TeDecoderFactory::make( params ) );
00372 if( ! decoder.isActive() ) {
00373 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder instantiation error",
00374 false ) );
00375 }
00376
00377 decoder->init();
00378
00379 if( decoder->params().status_ != TeRasterParams::TeReadyToWrite ) {
00380 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder initialization error",
00381 false ) );
00382 }
00383
00384 double value_in = 0;
00385 double value_out = 0;
00386 unsigned int band = 0;
00387 unsigned int line = 0;
00388 unsigned int col = 0;
00389
00390 for( band = 0 ; band < bands ; ++band ) {
00391 for( line = 0 ; line < lines ; ++line ) {
00392 for( col = 0 ; col < columns ; ++col ) {
00393 if( ! decoder->setElement( col, line, value_out, band ) ) {
00394 throw( TeException( UNKNOWN_ERROR_TYPE ,
00395 "setElement error at col=" +
00396 Te2String( col ) + " line=" + Te2String( line ),
00397 false ) );
00398 }
00399 if( ! decoder->getElement( col, line, value_in, band ) ) {
00400 throw( TeException( UNKNOWN_ERROR_TYPE ,
00401 "getElement error at col=" +
00402 Te2String( col ) + " line=" + Te2String( line ),
00403 false ) );
00404 }
00405 if( value_in != value_out ) {
00406 throw( TeException( UNKNOWN_ERROR_TYPE ,
00407 "invalid stored value at col=" +
00408 Te2String( col ) + " line=" + Te2String( line ) +
00409 " value_in=" + Te2String( value_in ) +
00410 " value_out=" + Te2String( value_out ),
00411 false ) );
00412 }
00413
00414 value_out += 1.0;
00415 }
00416 }
00417 }
00418
00419 decoder.reset();
00420 }
00421
00422 {
00423 TeRasterParams params;
00424 params.fileName_ = "TeDecoderPAM_test.ppm";
00425 params.mode_ = 'r';
00426 params.decoderIdentifier_ = "PAM";
00427
00428 TeSharedPtr< TeDecoder > decoder( TeDecoderFactory::make( params ) );
00429 if( ! decoder.isActive() ) {
00430 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder instantiation error",
00431 false ) );
00432 }
00433
00434 decoder->init();
00435
00436 if( decoder->params().status_ != TeRasterParams::TeReadyToRead ) {
00437 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder initialization error",
00438 false ) );
00439 }
00440 if( decoder->params().ncols_ != (int)columns ) {
00441 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid columsn number " +
00442 Te2String( decoder->params().ncols_ ),
00443 false ) );
00444 }
00445 if( decoder->params().nlines_ != (int)lines ) {
00446 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid lines number " +
00447 Te2String( decoder->params().nlines_ ),
00448 false ) );
00449 }
00450 if( decoder->params().nBands() != 3 ) {
00451 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid channels number " +
00452 Te2String( decoder->params().nBands() ),
00453 false ) );
00454 }
00455 if( decoder->params().dataType_[ 0 ] != TeUNSIGNEDCHAR ) {
00456 throw( TeException( UNKNOWN_ERROR_TYPE , "Invalid data type",
00457 false ) );
00458 }
00459
00460 double value_in = 0;
00461 double value_counter = 0;
00462 unsigned int band = 0;
00463 unsigned int line = 0;
00464 unsigned int col = 0;
00465
00466 for( band = 0 ; band < bands ; ++band ) {
00467 for( line = 0 ; line < lines ; ++line ) {
00468 for( col = 0 ; col < columns ; ++col ) {
00469 if( ! decoder->getElement( col, line, value_in, band ) ) {
00470 throw( TeException( UNKNOWN_ERROR_TYPE , "getElement error",
00471 false ) );
00472 }
00473
00474 if( value_counter != value_in ) {
00475 throw( TeException( UNKNOWN_ERROR_TYPE , "invalid read value"
00476 " col=" + Te2String( col ) + " line=" + Te2String( line ) +
00477 " value_in=" + Te2String( value_in ) +
00478 " value_counter=" + Te2String( value_counter ),
00479 false ) );
00480 }
00481
00482 value_counter += 1.0;
00483 }
00484 }
00485 }
00486
00487 decoder.reset();
00488 }
00489 }
00490 #endif
00491
00492 void DecoderMemoryMap_test()
00493 {
00494 const unsigned int columns = 100;
00495 const unsigned int lines = 100;
00496 const unsigned int bands = 2;
00497
00498 TeRasterParams params;
00499 params.setNLinesNColumns( lines, columns );
00500 params.nBands( bands );
00501 params.setDataType( TeINTEGER, -1 );
00502 params.setDummy( 0, -1 );
00503 params.decoderIdentifier_ = "MEMMAP";
00504 params.mode_ = 'c';
00505 params.fileName_ = TETESTSBINPATH "/DecoderMemoryMap_test.bin";
00506
00507 TeSharedPtr< TeDecoder > decoder( TeDecoderFactory::make( params ) );
00508 if( ! decoder.isActive() ) {
00509 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder instantiation error",
00510 false ) );
00511 }
00512
00513 decoder->init();
00514
00515 if( decoder->params().status_ != TeRasterParams::TeReadyToWrite ) {
00516 throw( TeException( UNKNOWN_ERROR_TYPE , "Decoder initialization error",
00517 false ) );
00518 }
00519
00520 double value_in = 0;
00521 unsigned int band = 0;
00522 unsigned int line = 0;
00523 unsigned int col = 0;
00524
00525 for( band = 0 ; band < bands ; ++band ) {
00526 for( line = 0 ; line < lines ; ++line ) {
00527 for( col = 0 ; col < columns ; ++col ) {
00528 if( ! decoder->setElement( col, line, value_in, band ) ) {
00529 throw( TeException( UNKNOWN_ERROR_TYPE , "setElement error", false ) );
00530 }
00531
00532 value_in += 1.0;
00533 }
00534 }
00535 }
00536
00537 value_in = 0;
00538 double value_out = 0;
00539
00540 for( band = 0 ; band < bands ; ++band ) {
00541 for( line = 0 ; line < lines ; ++line ) {
00542 for( col = 0 ; col < columns ; ++col ) {
00543 if( ! decoder->getElement( col, line, value_out, band ) ) {
00544 throw( TeException( UNKNOWN_ERROR_TYPE , "getElement error",
00545 false ) );
00546 }
00547
00548 if( value_out != value_in ) {
00549 throw( TeException( UNKNOWN_ERROR_TYPE , "invalid value read",
00550 false ) );
00551 }
00552
00553 value_in += 1.0;
00554 }
00555 }
00556 }
00557 }
00558
00559
00560 int main()
00561 {
00562 std::cout << std::endl << "Test started." << std::endl;
00563
00564 TEAGN_DEBUG_MODE_CHECK;
00565
00566 TeStdIOProgress pi;
00567 TeProgress::setProgressInterf( dynamic_cast< TeProgressBase* >( &pi ) );
00568
00569 TeInitRasterDecoders();
00570
00571 DecoderMemoryMap_test();
00572 DecoderSmartMem_test();
00573 DecoderMemory_test();
00574 TeDecoderTIFF_test();
00575
00576 #ifdef ENABLE_PAMDECODER_TEST
00577 TeDecoderPAM_test();
00578 #endif
00579
00580 std::cout << std::endl << "Test OK" << std::endl;
00581 return EXIT_SUCCESS;
00582 }