/tests/src/core/testcontrastenhancements.cpp

https://github.com/ricardogsilva/Quantum-GIS · C++ · 107 lines · 61 code · 10 blank · 36 comment · 7 complexity · d4dd632aa8a9a3ac425142e0590d8799 MD5 · raw file

  1. /***************************************************************************
  2. testcontrastenhancements.cpp
  3. --------------------------------------
  4. Date : Frida Nov 23 2007
  5. Copyright : (C) 2007 by Tim Sutton
  6. Email : tim@linfiniti.com
  7. ***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include "qgstest.h"
  16. #include <QObject>
  17. #include <QApplication>
  18. #include <QDesktopServices>
  19. //qgis includes...
  20. #include <qgsrasterlayer.h>
  21. #include <qgscliptominmaxenhancement.h>
  22. #include <qgscontrastenhancement.h>
  23. #include <qgslinearminmaxenhancement.h>
  24. #include <qgslinearminmaxenhancementwithclip.h>
  25. /**
  26. * \ingroup UnitTests
  27. * This is a unit test for the ContrastEnhancements contrast enhancement classes.
  28. */
  29. class TestContrastEnhancements: public QObject
  30. {
  31. Q_OBJECT
  32. private slots:
  33. void initTestCase();// will be called before the first testfunction is executed.
  34. void cleanupTestCase();// will be called after the last testfunction was executed.
  35. void init() {} // will be called before each testfunction is executed.
  36. void cleanup() {} // will be called after every testfunction.
  37. void clipMinMaxEnhancementTest();
  38. void linearMinMaxEnhancementWithClipTest();
  39. void linearMinMaxEnhancementTest();
  40. private:
  41. QString mReport;
  42. };
  43. //runs before all tests
  44. void TestContrastEnhancements::initTestCase()
  45. {
  46. mReport += QLatin1String( "<h1>Raster Contrast Enhancement Tests</h1>\n" );
  47. }
  48. //runs after all tests
  49. void TestContrastEnhancements::cleanupTestCase()
  50. {
  51. const QString myReportFile = QDir::tempPath() + "/qgistest.html";
  52. QFile myFile( myReportFile );
  53. if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
  54. {
  55. QTextStream myQTextStream( &myFile );
  56. myQTextStream << mReport;
  57. myFile.close();
  58. //QDesktopServices::openUrl( "file:///" + myReportFile );
  59. }
  60. }
  61. void TestContrastEnhancements::clipMinMaxEnhancementTest()
  62. {
  63. //Clips 0 < x < 10, 240 < X < 256
  64. //Stretch no stretch is applied
  65. QgsClipToMinMaxEnhancement myEnhancement( Qgis::DataType::Byte, 10.0, 240.0 );
  66. // Original pixel value 0.0 Should be out of range thus clipped
  67. QVERIFY( !myEnhancement.isValueInDisplayableRange( 0.0 ) );
  68. //Original pixel value of 10.0 should be scaled to 10.0
  69. QVERIFY( 10.0 == myEnhancement.enhance( 10.0 ) );
  70. //Original pixel value of 240 should be scaled to 240
  71. QVERIFY( 240.0 == myEnhancement.enhance( 240.0 ) );
  72. }
  73. void TestContrastEnhancements::linearMinMaxEnhancementWithClipTest()
  74. {
  75. //First clips 0 < x < 10, 240 < X < 256
  76. //Then stretch 10 = 0, 240 = 255 linearly distribute values 10 -> 240 between 0 -> 255
  77. QgsLinearMinMaxEnhancementWithClip myEnhancement( Qgis::DataType::Byte, 10.0, 240.0 );
  78. // Original pixel value 0.0 Should be out of range thus clipped
  79. QVERIFY( !myEnhancement.isValueInDisplayableRange( 0.0 ) );
  80. //Original pixel value of 10.0 should be scaled to 0.0
  81. QVERIFY( 0.0 == myEnhancement.enhance( 10.0 ) );
  82. //Original pixel value of 240 should be scaled to 255
  83. QVERIFY( 255.0 == myEnhancement.enhance( 240.0 ) );
  84. }
  85. void TestContrastEnhancements::linearMinMaxEnhancementTest()
  86. {
  87. //Stretch 10 = 0, 240 = 255 linearly distribute values 10 -> 240 between 0 -> 255
  88. QgsLinearMinMaxEnhancement myEnhancement( Qgis::DataType::Byte, 10.0, 240.0 );
  89. //0 should be scaled to 10 and not clipped
  90. QVERIFY( myEnhancement.isValueInDisplayableRange( 0.0 ) );
  91. //Original pixel value of 10.0 should be scaled to 0.0
  92. QVERIFY( 0.0 == myEnhancement.enhance( 10.0 ) );
  93. //Original pixel value of 240 should be scaled to 255
  94. QVERIFY( 255.0 == myEnhancement.enhance( 240.0 ) );
  95. }
  96. QGSTEST_MAIN( TestContrastEnhancements )
  97. #include "testcontrastenhancements.moc"