Skip to content

Commit d1b7e8b

Browse files
committed
add raster histogram tests
1 parent 29c8ef7 commit d1b7e8b

File tree

22 files changed

+360
-20
lines changed

22 files changed

+360
-20
lines changed

src/gui/raster/qgsrasterhistogramwidget.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,19 +522,21 @@ void QgsRasterHistogramWidget::on_mSaveAsImageButton_clicked()
522522
}
523523
}
524524

525-
void QgsRasterHistogramWidget::histoSaveAsImage( const QString& theFilename )
525+
bool QgsRasterHistogramWidget::histoSaveAsImage( const QString& theFilename,
526+
int width, int height, int quality )
526527
{
527528
// make sure dir. exists
528-
QDir myDir( QFileInfo( theFilename ).dir() );
529+
QFileInfo myInfo( theFilename );
530+
QDir myDir( myInfo.dir() );
529531
if ( ! myDir.exists() )
530532
{
531533
QgsDebugMsg( QString( "Error, directory %1 non-existent (theFilename = %2)" ).arg( myDir.absolutePath() ).arg( theFilename ) );
532-
return;
534+
return false;
533535
}
534536

535537
// prepare the pixmap
536-
QPixmap myPixmap( 600, 600 );
537-
QRect myQRect( 5, 5, 590, 590 ); // leave a 5px border on all sides
538+
QPixmap myPixmap( width, height );
539+
QRect myQRect( 5, 5, width - 10, height - 10 ); // leave a 5px border on all sides
538540
myPixmap.fill( Qt::white ); // Qt::transparent ?
539541

540542
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
@@ -566,10 +568,15 @@ void QgsRasterHistogramWidget::histoSaveAsImage( const QString& theFilename )
566568
#endif
567569

568570
// save pixmap to file
569-
myPixmap.save( theFilename );
571+
myPixmap.save( theFilename, 0, quality );
570572

571-
#if defined(QWT_VERSION) && QWT_VERSION<0x060000
572-
#endif
573+
// should do more error checking
574+
return true;
575+
}
576+
577+
void QgsRasterHistogramWidget::setSelectedBand( int theBandNo )
578+
{
579+
cboHistoBand->setCurrentIndex( theBandNo - 1 );
573580
}
574581

575582
void QgsRasterHistogramWidget::on_cboHistoBand_currentIndexChanged( int index )
@@ -610,16 +617,21 @@ void QgsRasterHistogramWidget::histoActionTriggered( QAction* action )
610617
{
611618
if ( ! action )
612619
return;
620+
histoAction( action->data().toString(), action->isChecked() );
621+
}
613622

614-
// this approach is a bit of a hack, but this way we don't have to define slots for each action
615-
QString actionName = action->data().toString();
623+
void QgsRasterHistogramWidget::histoAction( const QString actionName, bool actionFlag )
624+
{
625+
if ( actionName == "" )
626+
return;
616627

628+
// this approach is a bit of a hack, but this way we don't have to define slots for each action
617629
QgsDebugMsg( QString( "band = %1 action = %2" ).arg( cboHistoBand->currentIndex() + 1 ).arg( actionName ) );
618630

619631
// checkeable actions
620632
if ( actionName == "Show markers" )
621633
{
622-
mHistoShowMarkers = action->isChecked();
634+
mHistoShowMarkers = actionFlag;
623635
updateHistoMarkers();
624636
return;
625637
}
@@ -643,7 +655,7 @@ void QgsRasterHistogramWidget::histoActionTriggered( QAction* action )
643655
}
644656
else if ( actionName == "Load apply all" )
645657
{
646-
mHistoLoadApplyAll = action->isChecked();
658+
mHistoLoadApplyAll = actionFlag;
647659
return;
648660
}
649661
// Load actions

src/gui/raster/qgsrasterhistogramwidget.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class QgsRasterHistogramWidget : public QWidget, private Ui::QgsRasterHistogramW
4343
~QgsRasterHistogramWidget();
4444

4545
/** Save the histogram as an image to disk */
46-
void histoSaveAsImage( const QString& theFilename );
46+
bool histoSaveAsImage( const QString& theFilename, int width = 600, int height = 600, int quality = -1 );
4747

4848
/** Set the renderer widget (or just its name if there is no widget) */
4949
void setRendererWidget( const QString& name, QgsRasterRendererWidget* rendererWidget = NULL );
@@ -54,6 +54,12 @@ class QgsRasterHistogramWidget : public QWidget, private Ui::QgsRasterHistogramW
5454
/** \brief Compute the histogram on demand. */
5555
bool computeHistogram( bool forceComputeFlag );
5656

57+
/** Apply a histoActionTriggered() event. */
58+
void histoAction( const QString actionName, bool actionFlag = true );
59+
60+
/** Apply a histoActionTriggered() event. */
61+
void setSelectedBand( int index );
62+
5763
public slots:
5864
/** \brief slot executed when user wishes to refresh raster histogramwidget */
5965
void refreshHistogram();

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,10 @@ void QgsGdalProvider::populateHistogram( int theBandNo, QgsRasterBandStats & the
14981498
double dfHalfBucket = 0;
14991499
eErr = GDALGetRasterStatistics( myGdalBand, TRUE, TRUE, &myMinVal, &myMaxVal, NULL, NULL );
15001500
if ( eErr != CE_None )
1501+
{
1502+
delete [] myHistogramArray;
15011503
return;
1504+
}
15021505
dfHalfBucket = ( myMaxVal - myMinVal ) / ( 2 * theBinCount );
15031506
myMinVal -= dfHalfBucket;
15041507
myMaxVal += dfHalfBucket;
@@ -1509,7 +1512,10 @@ void QgsGdalProvider::populateHistogram( int theBandNo, QgsRasterBandStats & the
15091512
theIgnoreOutOfRangeFlag, theHistogramEstimatedFlag, progressCallback,
15101513
&myProg ); //this is the arg for our custom gdal progress callback
15111514
if ( myError != CE_None )
1515+
{
1516+
delete [] myHistogramArray;
15121517
return;
1518+
}
15131519

15141520
#endif
15151521

@@ -1526,6 +1532,8 @@ void QgsGdalProvider::populateHistogram( int theBandNo, QgsRasterBandStats & the
15261532
// QgsDebugMsg( "Added " + QString::number( myHistogramArray[myBin] ) + " to histogram vector" );
15271533
}
15281534
}
1535+
delete [] myHistogramArray;
1536+
15291537

15301538
}
15311539
QgsDebugMsg( ">>>>> Histogram vector now contains " + QString::number( theBandStats.histogramVector->size() ) +

tests/src/core/qgsrenderchecker.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include "qgsrenderchecker.h"
1717

18-
#include <QDir>
1918
#include <QColor>
2019
#include <QPainter>
2120
#include <QImage>
@@ -33,16 +32,17 @@ QgsRenderChecker::QgsRenderChecker( ) :
3332
mMatchTarget( 0 ),
3433
mElapsedTime( 0 ),
3534
mElapsedTimeTarget( 0 ),
36-
mpMapRenderer( NULL )
35+
mpMapRenderer( NULL ),
36+
mControlPathPrefix( "" )
3737
{
3838

3939
}
4040

4141
QString QgsRenderChecker::controlImagePath() const
4242
{
4343
QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
44-
QString myControlImageDir = myDataDir + QDir::separator() + "control_images"
45-
+ QDir::separator() ;
44+
QString myControlImageDir = myDataDir + QDir::separator() + "control_images" +
45+
QDir::separator() + mControlPathPrefix;
4646
return myControlImageDir;
4747
}
4848

@@ -167,7 +167,8 @@ bool QgsRenderChecker::runTest( QString theTestName,
167167

168168

169169
bool QgsRenderChecker::compareImages( QString theTestName,
170-
unsigned int theMismatchCount )
170+
unsigned int theMismatchCount,
171+
QString theRenderedImageFile )
171172
{
172173
if ( mExpectedImageFile.isEmpty() )
173174
{
@@ -178,6 +179,10 @@ bool QgsRenderChecker::compareImages( QString theTestName,
178179
"Image File not set.</td></tr></table>\n";
179180
return false;
180181
}
182+
if ( ! theRenderedImageFile.isEmpty() )
183+
{
184+
mRenderedImageFile = theRenderedImageFile;
185+
}
181186
if ( mRenderedImageFile.isEmpty() )
182187
{
183188
qDebug( "QgsRenderChecker::runTest failed - Rendered Image File not set." );
@@ -327,7 +332,7 @@ bool QgsRenderChecker::compareImages( QString theTestName,
327332
"\" type=\"text/text\">" + myMessage +
328333
" If you feel the difference image should be considered an anomaly "
329334
"you can do something like this\n"
330-
"cp " + myDiffImageFile + "../tests/testdata/control_images/" + theTestName +
335+
"cp " + myDiffImageFile + " ../tests/testdata/control_images/" + theTestName +
331336
"/<imagename>.png"
332337
"</DartMeasurement>";
333338
qDebug() << myMeasureMessage;

tests/src/core/qgsrenderchecker.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef QGSRENDERCHECKER_H
1717
#define QGSRENDERCHECKER_H
1818

19+
#include <QDir>
1920
#include <QString>
2021
#include <qgsmaprenderer.h>
2122
class QImage;
@@ -52,6 +53,10 @@ class QgsRenderChecker
5253
* controlImagePath + '/' + mControlName + '/' + mControlName + '.png'
5354
*/
5455
void setControlName( const QString theName );
56+
/** Prefix where the control images are kept.
57+
* This will be appended to controlImagePath
58+
*/
59+
void setControlPathPrefix( const QString theName ) { mControlPathPrefix = theName + QDir::separator(); }
5560
/** Get an md5 hash that uniquely identifies an image */
5661
QString imageToHash( QString theImageFile );
5762

@@ -79,7 +84,7 @@ class QgsRenderChecker
7984
* for that by providing a tolerance.
8085
* @note: make sure to call setExpectedImage and setRenderedImage first.
8186
*/
82-
bool compareImages( QString theTestName, unsigned int theMismatchCount = 0 );
87+
bool compareImages( QString theTestName, unsigned int theMismatchCount = 0, QString theRenderedImageFile = "" );
8388
/** Get a list of all teh anomalies. An anomaly is a rendered difference
8489
* file where there is some red pixel content (indicating a render check
8590
* mismatch), but where the output was still acceptible. If the render
@@ -100,6 +105,7 @@ class QgsRenderChecker
100105
int mElapsedTime;
101106
int mElapsedTimeTarget;
102107
QgsMapRenderer * mpMapRenderer;
108+
QString mControlPathPrefix;
103109

104110
}; // class QgsRenderChecker
105111

tests/src/gui/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
1111
${CMAKE_CURRENT_SOURCE_DIR}/../core #for render checker class
1212
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/gui
1313
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/gui/symbology-ng
14+
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/gui/raster
1415
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core
1516
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core/raster
1617
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core/symbology-ng
1718
${QT_INCLUDE_DIR}
1819
${GDAL_INCLUDE_DIR}
1920
${PROJ_INCLUDE_DIR}
2021
${GEOS_INCLUDE_DIR}
22+
${QWT_INCLUDE_DIR}
2123
)
2224

2325
#############################################################
@@ -37,6 +39,10 @@ IF (WIN32)
3739
SET(PLATFORM_LIBRARIES wsock32)
3840
ENDIF (WIN32)
3941

42+
IF(WIN32)
43+
ADD_DEFINITIONS(-DQWT_DLL)
44+
ENDIF(WIN32)
45+
4046
# Since the tests are not actually installed, but rather
4147
# run directly from the build/src/tests dir we need to
4248
# ensure the omg libs can be found.
@@ -93,6 +99,7 @@ MACRO (ADD_QGIS_TEST testname testsrc)
9399
${PROJ_LIBRARY}
94100
${GEOS_LIBRARY}
95101
${GDAL_LIBRARY}
102+
${QWT_LIBRARY}
96103
qgis_core
97104
qgis_gui)
98105
ADD_TEST(qgis_${testname} ${CMAKE_CURRENT_BINARY_DIR}/../../../output/bin/qgis_${testname})
@@ -110,4 +117,5 @@ ADD_QGIS_TEST(zoomtest testqgsmaptoolzoom.cpp)
110117
#QT4_WRAP_CPP(rendererv2gui_MOC_SRCS ${rendererv2gui_HDRS})
111118
#ADD_EXECUTABLE(qgis_rendererv2gui ${rendererv2gui_SRCS} ${rendererv2gui_MOC_SRCS})
112119

120+
ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
113121

0 commit comments

Comments
 (0)