|
| 1 | +/*************************************************************************** |
| 2 | + testqgsimageoperations.cpp |
| 3 | + -------------------------- |
| 4 | + begin : January 2015 |
| 5 | + copyright : (C) 2015 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsimageoperation.h" |
| 19 | +#include "qgsvectorcolorrampv2.h" |
| 20 | +#include <QObject> |
| 21 | +#include <QtTest/QtTest> |
| 22 | +#include "qgsrenderchecker.h" |
| 23 | + |
| 24 | +class TestQgsImageOperation : public QObject |
| 25 | +{ |
| 26 | + Q_OBJECT |
| 27 | + |
| 28 | + private slots: |
| 29 | + void initTestCase();// will be called before the first testfunction is executed. |
| 30 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 31 | + void init();// will be called before each testfunction is executed. |
| 32 | + void cleanup();// will be called after every testfunction. |
| 33 | + void smallImageOp(); //test operation on small image (single threaded op) |
| 34 | + |
| 35 | + //grayscale |
| 36 | + void grayscaleLightness(); |
| 37 | + void grayscaleLuminosity(); |
| 38 | + void grayscaleAverage(); |
| 39 | + |
| 40 | + //brightness/contrast |
| 41 | + void brightnessContrastNoChange(); |
| 42 | + void increaseBrightness(); |
| 43 | + void decreaseBrightness(); |
| 44 | + void increaseContrast(); |
| 45 | + void decreaseContrast(); |
| 46 | + |
| 47 | + //hue/saturation |
| 48 | + void hueSaturationNoChange(); |
| 49 | + void increaseSaturation(); |
| 50 | + void decreaseSaturation(); |
| 51 | + void colorizeFull(); |
| 52 | + void colorizePartial(); |
| 53 | + |
| 54 | + //multiply opacity |
| 55 | + void opacityNoChange(); |
| 56 | + void opacityIncrease(); |
| 57 | + void opacityDecrease(); |
| 58 | + |
| 59 | + //overlay color |
| 60 | + void overlayColor(); |
| 61 | + |
| 62 | + //distance transform |
| 63 | + void distanceTransformMaxDist(); |
| 64 | + void distanceTransformSetSpread(); |
| 65 | + void distanceTransformInterior(); |
| 66 | + |
| 67 | + //stack blur |
| 68 | + void stackBlur(); |
| 69 | + void alphaOnlyBlur(); |
| 70 | + |
| 71 | + //flip |
| 72 | + void flipHorizontal(); |
| 73 | + void flipVertical(); |
| 74 | + |
| 75 | + private: |
| 76 | + |
| 77 | + QString mReport; |
| 78 | + QString mSampleImage; |
| 79 | + |
| 80 | + bool imageCheck( QString testName , QImage &image, int mismatchCount ); |
| 81 | + |
| 82 | +}; |
| 83 | + |
| 84 | +void TestQgsImageOperation::initTestCase() |
| 85 | +{ |
| 86 | + mReport += "<h1>Image Operation Tests</h1>\n"; |
| 87 | + mSampleImage = QString( TEST_DATA_DIR ) + QDir::separator() + "sample_image.png"; |
| 88 | +} |
| 89 | + |
| 90 | +void TestQgsImageOperation::cleanupTestCase() |
| 91 | +{ |
| 92 | + QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html"; |
| 93 | + QFile myFile( myReportFile ); |
| 94 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 95 | + { |
| 96 | + QTextStream myQTextStream( &myFile ); |
| 97 | + myQTextStream << mReport; |
| 98 | + myFile.close(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void TestQgsImageOperation::init() |
| 103 | +{ |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +void TestQgsImageOperation::cleanup() |
| 108 | +{ |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +void TestQgsImageOperation::smallImageOp() |
| 113 | +{ |
| 114 | + QImage image( QString( TEST_DATA_DIR ) + QDir::separator() + "small_sample_image.png" ); |
| 115 | + QgsImageOperation::convertToGrayscale( image, QgsImageOperation::GrayscaleLightness ); |
| 116 | + |
| 117 | + bool result = imageCheck( QString( "imageop_smallimage" ), image, 0 ); |
| 118 | + QVERIFY( result ); |
| 119 | +} |
| 120 | + |
| 121 | +void TestQgsImageOperation::grayscaleLightness() |
| 122 | +{ |
| 123 | + QImage image( mSampleImage ); |
| 124 | + QgsImageOperation::convertToGrayscale( image, QgsImageOperation::GrayscaleLightness ); |
| 125 | + |
| 126 | + bool result = imageCheck( QString( "imageop_graylightness" ), image, 0 ); |
| 127 | + QVERIFY( result ); |
| 128 | +} |
| 129 | + |
| 130 | +void TestQgsImageOperation::grayscaleLuminosity() |
| 131 | +{ |
| 132 | + QImage image( mSampleImage ); |
| 133 | + QgsImageOperation::convertToGrayscale( image, QgsImageOperation::GrayscaleLuminosity ); |
| 134 | + |
| 135 | + bool result = imageCheck( QString( "imageop_grayluminosity" ), image, 0 ); |
| 136 | + QVERIFY( result ); |
| 137 | +} |
| 138 | + |
| 139 | +void TestQgsImageOperation::grayscaleAverage() |
| 140 | +{ |
| 141 | + QImage image( mSampleImage ); |
| 142 | + QgsImageOperation::convertToGrayscale( image, QgsImageOperation::GrayscaleAverage ); |
| 143 | + |
| 144 | + bool result = imageCheck( QString( "imageop_grayaverage" ), image, 0 ); |
| 145 | + QVERIFY( result ); |
| 146 | +} |
| 147 | + |
| 148 | +void TestQgsImageOperation::brightnessContrastNoChange() |
| 149 | +{ |
| 150 | + QImage image( mSampleImage ); |
| 151 | + QgsImageOperation::adjustBrightnessContrast( image, 0, 1.0 ); |
| 152 | + |
| 153 | + bool result = imageCheck( QString( "imageop_bcnochange" ), image, 0 ); |
| 154 | + QVERIFY( result ); |
| 155 | +} |
| 156 | + |
| 157 | +void TestQgsImageOperation::increaseBrightness() |
| 158 | +{ |
| 159 | + QImage image( mSampleImage ); |
| 160 | + QgsImageOperation::adjustBrightnessContrast( image, 50, 1.0 ); |
| 161 | + |
| 162 | + bool result = imageCheck( QString( "imageop_increasebright" ), image, 0 ); |
| 163 | + QVERIFY( result ); |
| 164 | +} |
| 165 | + |
| 166 | +void TestQgsImageOperation::decreaseBrightness() |
| 167 | +{ |
| 168 | + QImage image( mSampleImage ); |
| 169 | + QgsImageOperation::adjustBrightnessContrast( image, -50, 1.0 ); |
| 170 | + |
| 171 | + bool result = imageCheck( QString( "imageop_decreasebright" ), image, 0 ); |
| 172 | + QVERIFY( result ); |
| 173 | +} |
| 174 | + |
| 175 | +void TestQgsImageOperation::increaseContrast() |
| 176 | +{ |
| 177 | + QImage image( mSampleImage ); |
| 178 | + QgsImageOperation::adjustBrightnessContrast( image, 0, 30.0 ); |
| 179 | + |
| 180 | + bool result = imageCheck( QString( "imageop_increasecontrast" ), image, 0 ); |
| 181 | + QVERIFY( result ); |
| 182 | +} |
| 183 | + |
| 184 | +void TestQgsImageOperation::decreaseContrast() |
| 185 | +{ |
| 186 | + QImage image( mSampleImage ); |
| 187 | + QgsImageOperation::adjustBrightnessContrast( image, 0, 0.1 ); |
| 188 | + |
| 189 | + bool result = imageCheck( QString( "imageop_decreasecontrast" ), image, 0 ); |
| 190 | + QVERIFY( result ); |
| 191 | +} |
| 192 | + |
| 193 | +void TestQgsImageOperation::hueSaturationNoChange() |
| 194 | +{ |
| 195 | + QImage image( mSampleImage ); |
| 196 | + QgsImageOperation::adjustHueSaturation( image, 1.0 ); |
| 197 | + |
| 198 | + bool result = imageCheck( QString( "imageop_satnochange" ), image, 0 ); |
| 199 | + QVERIFY( result ); |
| 200 | +} |
| 201 | + |
| 202 | +void TestQgsImageOperation::increaseSaturation() |
| 203 | +{ |
| 204 | + QImage image( mSampleImage ); |
| 205 | + QgsImageOperation::adjustHueSaturation( image, 5.0 ); |
| 206 | + |
| 207 | + bool result = imageCheck( QString( "imageop_increasesat" ), image, 0 ); |
| 208 | + QVERIFY( result ); |
| 209 | +} |
| 210 | + |
| 211 | +void TestQgsImageOperation::decreaseSaturation() |
| 212 | +{ |
| 213 | + QImage image( mSampleImage ); |
| 214 | + QgsImageOperation::adjustHueSaturation( image, 0.5 ); |
| 215 | + |
| 216 | + bool result = imageCheck( QString( "imageop_decreasesat" ), image, 0 ); |
| 217 | + QVERIFY( result ); |
| 218 | +} |
| 219 | + |
| 220 | +void TestQgsImageOperation::colorizeFull() |
| 221 | +{ |
| 222 | + QImage image( mSampleImage ); |
| 223 | + QgsImageOperation::adjustHueSaturation( image, 1.0, QColor( 255, 255, 0 ), 1.0 ); |
| 224 | + |
| 225 | + bool result = imageCheck( QString( "imageop_colorizefull" ), image, 0 ); |
| 226 | + QVERIFY( result ); |
| 227 | +} |
| 228 | + |
| 229 | +void TestQgsImageOperation::colorizePartial() |
| 230 | +{ |
| 231 | + QImage image( mSampleImage ); |
| 232 | + QgsImageOperation::adjustHueSaturation( image, 1.0, QColor( 255, 255, 0 ), 0.5 ); |
| 233 | + |
| 234 | + bool result = imageCheck( QString( "imageop_colorizepartial" ), image, 0 ); |
| 235 | + QVERIFY( result ); |
| 236 | +} |
| 237 | + |
| 238 | +void TestQgsImageOperation::opacityNoChange() |
| 239 | +{ |
| 240 | + QImage image( mSampleImage ); |
| 241 | + QgsImageOperation::multiplyOpacity( image, 1.0 ); |
| 242 | + |
| 243 | + bool result = imageCheck( QString( "imageop_opacitynochange" ), image, 0 ); |
| 244 | + QVERIFY( result ); |
| 245 | +} |
| 246 | + |
| 247 | +void TestQgsImageOperation::opacityIncrease() |
| 248 | +{ |
| 249 | + QImage image( mSampleImage ); |
| 250 | + QgsImageOperation::multiplyOpacity( image, 2.0 ); |
| 251 | + |
| 252 | + bool result = imageCheck( QString( "imageop_opacityincrease" ), image, 0 ); |
| 253 | + QVERIFY( result ); |
| 254 | +} |
| 255 | + |
| 256 | +void TestQgsImageOperation::opacityDecrease() |
| 257 | +{ |
| 258 | + QImage image( mSampleImage ); |
| 259 | + QgsImageOperation::multiplyOpacity( image, 0.5 ); |
| 260 | + |
| 261 | + bool result = imageCheck( QString( "imageop_opacitydecrease" ), image, 0 ); |
| 262 | + QVERIFY( result ); |
| 263 | +} |
| 264 | + |
| 265 | +void TestQgsImageOperation::overlayColor() |
| 266 | +{ |
| 267 | + QImage image( mSampleImage ); |
| 268 | + QgsImageOperation::overlayColor( image, QColor( 0, 255, 255 ) ); |
| 269 | + |
| 270 | + bool result = imageCheck( QString( "imageop_overlaycolor" ), image, 0 ); |
| 271 | + QVERIFY( result ); |
| 272 | +} |
| 273 | + |
| 274 | +void TestQgsImageOperation::distanceTransformMaxDist() |
| 275 | +{ |
| 276 | + QImage image( mSampleImage ); |
| 277 | + QgsVectorGradientColorRampV2 ramp; |
| 278 | + QgsImageOperation::DistanceTransformProperties props; |
| 279 | + props.useMaxDistance = true; |
| 280 | + props.ramp = &ramp; |
| 281 | + props.shadeExterior = true; |
| 282 | + |
| 283 | + QgsImageOperation::distanceTransform( image, props ); |
| 284 | + |
| 285 | + bool result = imageCheck( QString( "imageop_dt_max" ), image, 0 ); |
| 286 | + QVERIFY( result ); |
| 287 | +} |
| 288 | + |
| 289 | +void TestQgsImageOperation::distanceTransformSetSpread() |
| 290 | +{ |
| 291 | + QImage image( mSampleImage ); |
| 292 | + QgsVectorGradientColorRampV2 ramp; |
| 293 | + QgsImageOperation::DistanceTransformProperties props; |
| 294 | + props.useMaxDistance = false; |
| 295 | + props.spread = 10; |
| 296 | + props.ramp = &ramp; |
| 297 | + props.shadeExterior = true; |
| 298 | + |
| 299 | + QgsImageOperation::distanceTransform( image, props ); |
| 300 | + |
| 301 | + bool result = imageCheck( QString( "imageop_dt_spread" ), image, 0 ); |
| 302 | + QVERIFY( result ); |
| 303 | +} |
| 304 | + |
| 305 | +void TestQgsImageOperation::distanceTransformInterior() |
| 306 | +{ |
| 307 | + QImage image( mSampleImage ); |
| 308 | + QgsVectorGradientColorRampV2 ramp; |
| 309 | + QgsImageOperation::DistanceTransformProperties props; |
| 310 | + props.useMaxDistance = true; |
| 311 | + props.ramp = &ramp; |
| 312 | + props.shadeExterior = false; |
| 313 | + |
| 314 | + QgsImageOperation::distanceTransform( image, props ); |
| 315 | + |
| 316 | + bool result = imageCheck( QString( "imageop_dt_interior" ), image, 0 ); |
| 317 | + QVERIFY( result ); |
| 318 | +} |
| 319 | + |
| 320 | +void TestQgsImageOperation::stackBlur() |
| 321 | +{ |
| 322 | + QImage image( mSampleImage ); |
| 323 | + QgsImageOperation::stackBlur( image, 10 ); |
| 324 | + |
| 325 | + bool result = imageCheck( QString( "imageop_stackblur" ), image, 0 ); |
| 326 | + QVERIFY( result ); |
| 327 | +} |
| 328 | + |
| 329 | +void TestQgsImageOperation::alphaOnlyBlur() |
| 330 | +{ |
| 331 | + QImage image( QString( TEST_DATA_DIR ) + QDir::separator() + "small_sample_image.png" ); |
| 332 | + QgsImageOperation::stackBlur( image, 10, true ); |
| 333 | + |
| 334 | + bool result = imageCheck( QString( "imageop_stackblur_alphaonly" ), image, 0 ); |
| 335 | + QVERIFY( result ); |
| 336 | +} |
| 337 | + |
| 338 | +void TestQgsImageOperation::flipHorizontal() |
| 339 | +{ |
| 340 | + QImage image( mSampleImage ); |
| 341 | + QgsImageOperation::flipImage( image, QgsImageOperation::FlipHorizontal ); |
| 342 | + |
| 343 | + bool result = imageCheck( QString( "imageop_fliphoz" ), image, 0 ); |
| 344 | + QVERIFY( result ); |
| 345 | +} |
| 346 | + |
| 347 | +void TestQgsImageOperation::flipVertical() |
| 348 | +{ |
| 349 | + QImage image( mSampleImage ); |
| 350 | + QgsImageOperation::flipImage( image, QgsImageOperation::FlipVertical ); |
| 351 | + |
| 352 | + bool result = imageCheck( QString( "imageop_flipvert" ), image, 0 ); |
| 353 | + QVERIFY( result ); |
| 354 | +} |
| 355 | + |
| 356 | +// |
| 357 | +// Private helper functions not called directly by CTest |
| 358 | +// |
| 359 | + |
| 360 | +bool TestQgsImageOperation::imageCheck( QString testName, QImage &image, int mismatchCount ) |
| 361 | +{ |
| 362 | + //draw background |
| 363 | + QImage imageWithBackground( image.width(), image.height(), QImage::Format_RGB32 ); |
| 364 | + QgsRenderChecker::drawBackground( &imageWithBackground ); |
| 365 | + QPainter painter( &imageWithBackground ); |
| 366 | + painter.drawImage( 0, 0, image ); |
| 367 | + painter.end(); |
| 368 | + |
| 369 | + mReport += "<h2>" + testName + "</h2>\n"; |
| 370 | + QString tempDir = QDir::tempPath() + QDir::separator(); |
| 371 | + QString fileName = tempDir + testName + ".png"; |
| 372 | + imageWithBackground.save( fileName, "PNG" ); |
| 373 | + QgsRenderChecker checker; |
| 374 | + checker.setControlName( "expected_" + testName ); |
| 375 | + checker.setRenderedImage( fileName ); |
| 376 | + checker.setColorTolerance( 1 ); |
| 377 | + bool resultFlag = checker.compareImages( testName, mismatchCount ); |
| 378 | + mReport += checker.report(); |
| 379 | + return resultFlag; |
| 380 | +} |
| 381 | + |
| 382 | +QTEST_MAIN( TestQgsImageOperation ) |
| 383 | +#include "testqgsimageoperation.moc" |
0 commit comments