Skip to content

Commit 4435c4f

Browse files
committedMar 26, 2013
UI to control brightness and contrast values in raster properties,
attempt to fix contrast calculation
1 parent 8bf6df2 commit 4435c4f

File tree

4 files changed

+132
-11
lines changed

4 files changed

+132
-11
lines changed
 

‎src/app/qgsrasterlayerproperties.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
8282

8383
connect( sliderTransparency, SIGNAL( valueChanged( int ) ), this, SLOT( sliderTransparency_valueChanged( int ) ) );
8484

85+
// brightness/contrast controls
86+
connect( mSliderBrightness, SIGNAL( valueChanged( int ) ), mBrightnessSpinBox, SLOT( setValue( int ) ) );
87+
connect( mBrightnessSpinBox, SIGNAL( valueChanged( int ) ), mSliderBrightness, SLOT( setValue( int ) ) );
88+
89+
connect( mSliderContrast, SIGNAL( valueChanged( int ) ), mContrastSpinBox, SLOT( setValue( int ) ) );
90+
connect( mContrastSpinBox, SIGNAL( valueChanged( int ) ), mSliderContrast, SLOT( setValue( int ) ) );
91+
8592
// enable or disable Build Pyramids button depending on selection in pyramid list
8693
connect( lbxPyramidResolutions, SIGNAL( itemSelectionChanged() ), this, SLOT( toggleBuildPyramidsButton() ) );
8794

@@ -527,6 +534,19 @@ void QgsRasterLayerProperties::sync()
527534

528535
QgsDebugMsg( "populate transparency tab" );
529536

537+
/*
538+
* Style tab (brightness and contrast)
539+
*/
540+
541+
QgsBrightnessContrastFilter* brightnessFilter = mRasterLayer->brightnessFilter();
542+
if ( brightnessFilter )
543+
{
544+
mSliderBrightness->setValue( brightnessFilter->brightness() );
545+
mSliderContrast->setValue( brightnessFilter->contrast() );
546+
}
547+
548+
//set the transparency slider
549+
530550
/*
531551
* Transparent Pixel Tab
532552
*/
@@ -672,6 +692,9 @@ void QgsRasterLayerProperties::apply()
672692
//set whether the layer histogram should be inverted
673693
//mRasterLayer->setInvertHistogram( cboxInvertColorMap->isChecked() );
674694

695+
mRasterLayer->brightnessFilter()->setBrightness( mSliderBrightness->value() );
696+
mRasterLayer->brightnessFilter()->setContrast( mSliderContrast->value() );
697+
675698
QgsDebugMsg( "processing transparency tab" );
676699
/*
677700
* Transparent Pixel Tab

‎src/core/raster/qgsbrightnesscontrastfilter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsrasterdataprovider.h"
1919
#include "qgsbrightnesscontrastfilter.h"
2020

21+
#include <qmath.h>
2122
#include <QDomDocument>
2223
#include <QDomElement>
2324

@@ -145,9 +146,9 @@ QgsRasterBlock * QgsBrightnessContrastFilter::block( int bandNo, QgsRectangle c
145146
// adjust image
146147
QRgb myNoDataColor = qRgba( 0, 0, 0, 0 );
147148
QRgb myColor;
148-
int r, g, b;
149149

150-
double f = ( 259 * ( mContrast + 255 ) ) / ( 255 * ( 259 - mContrast ) );
150+
int r, g, b;
151+
double f = qPow(( mContrast + 100 ) / 100.0, 2 );
151152

152153
for ( size_t i = 0; i < ( size_t )width*height; i++ )
153154
{
@@ -158,9 +159,9 @@ QgsRasterBlock * QgsBrightnessContrastFilter::block( int bandNo, QgsRectangle c
158159
}
159160

160161
myColor = inputBlock->color( i );
161-
r = qBound( 0, qRound( f * ( qBound( 0, qRed( myColor ) + mBrightness, 255 ) - 128 ) + 128 ), 255 );
162-
g = qBound( 0, qRound( f * ( qBound( 0, qGreen( myColor ) + mBrightness, 255 ) - 128 ) + 128 ), 255 );
163-
b = qBound( 0, qRound( f * ( qBound( 0, qBlue( myColor ) + mBrightness, 255 ) - 128 ) + 128 ), 255 );
162+
r = qBound( 0, ( int )((((( qRed( myColor ) / 255.0 ) - 0.5 ) * f ) + 0.5 ) * 255 ) + mBrightness, 255 );
163+
g = qBound( 0, ( int )((((( qGreen( myColor ) / 255.0 ) - 0.5 ) * f ) + 0.5 ) * 255 ) + mBrightness, 255 );
164+
b = qBound( 0, ( int )((((( qBlue( myColor ) / 255.0 ) - 0.5 ) * f ) + 0.5 ) * 255 ) + mBrightness, 255 );
164165

165166
outputBlock->setColor( i, qRgb( r, g, b ) );
166167
}

‎src/core/raster/qgsbrightnesscontrastfilter.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CORE_EXPORT QgsBrightnessContrastFilter : public QgsRasterInterface
4545
void setBrightness( int brightness ) { mBrightness = qBound( -255, brightness, 255 ); }
4646
int brightness() const { return mBrightness; }
4747

48-
void setContrast( int contrast ) { mContrast = qBound( -255, contrast, 255 ); }
48+
void setContrast( int contrast ) { mContrast = qBound( -100, contrast, 100 ); }
4949
int contrast() const { return mContrast; }
5050

5151
void writeXML( QDomDocument& doc, QDomElement& parentElem );
@@ -54,11 +54,11 @@ class CORE_EXPORT QgsBrightnessContrastFilter : public QgsRasterInterface
5454
void readXML( const QDomElement& filterElem );
5555

5656
private:
57-
/**Current brightness coefficient value. Default: 0. Range: -255...255*/
57+
/** Current brightness coefficient value. Default: 0. Range: -255...255 */
5858
int mBrightness;
5959

60-
/**Current contrast coefficient value. Default: 0. Range: -255...255*/
61-
int mContrast;
60+
/** Current contrast coefficient value. Default: 0. Range: -100...100 */
61+
double mContrast;
6262
};
6363

6464
#endif // QGSBRIGHTNESSCONTRASTFILTER_H

‎src/ui/qgsrasterlayerpropertiesbase.ui

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<item row="0" column="0" colspan="4">
7373
<widget class="QTabWidget" name="tabBar">
7474
<property name="currentIndex">
75-
<number>2</number>
75+
<number>0</number>
7676
</property>
7777
<property name="iconSize">
7878
<size>
@@ -248,6 +248,103 @@
248248
</layout>
249249
</widget>
250250
</item>
251+
<item>
252+
<widget class="QgsCollapsibleGroupBox" name="mBrightessContrastGroupBox">
253+
<property name="title">
254+
<string>Brightness and Contrast</string>
255+
</property>
256+
<property name="collapsed" stdset="0">
257+
<bool>false</bool>
258+
</property>
259+
<property name="saveCollapsedState" stdset="0">
260+
<bool>true</bool>
261+
</property>
262+
<layout class="QHBoxLayout" name="horizontalLayout_11">
263+
<item>
264+
<widget class="QLabel" name="label_2">
265+
<property name="text">
266+
<string>Brightness</string>
267+
</property>
268+
</widget>
269+
</item>
270+
<item>
271+
<widget class="QSlider" name="mSliderBrightness">
272+
<property name="minimum">
273+
<number>-255</number>
274+
</property>
275+
<property name="maximum">
276+
<number>255</number>
277+
</property>
278+
<property name="orientation">
279+
<enum>Qt::Horizontal</enum>
280+
</property>
281+
<property name="tickPosition">
282+
<enum>QSlider::NoTicks</enum>
283+
</property>
284+
<property name="tickInterval">
285+
<number>0</number>
286+
</property>
287+
</widget>
288+
</item>
289+
<item>
290+
<widget class="QSpinBox" name="mBrightnessSpinBox">
291+
<property name="minimum">
292+
<number>-255</number>
293+
</property>
294+
<property name="maximum">
295+
<number>255</number>
296+
</property>
297+
</widget>
298+
</item>
299+
<item>
300+
<spacer name="horizontalSpacer_4">
301+
<property name="orientation">
302+
<enum>Qt::Horizontal</enum>
303+
</property>
304+
<property name="sizeHint" stdset="0">
305+
<size>
306+
<width>40</width>
307+
<height>20</height>
308+
</size>
309+
</property>
310+
</spacer>
311+
</item>
312+
<item>
313+
<widget class="QLabel" name="label_5">
314+
<property name="text">
315+
<string>Contrast</string>
316+
</property>
317+
</widget>
318+
</item>
319+
<item>
320+
<widget class="QSlider" name="mSliderContrast">
321+
<property name="minimum">
322+
<number>-100</number>
323+
</property>
324+
<property name="maximum">
325+
<number>100</number>
326+
</property>
327+
<property name="singleStep">
328+
<number>1</number>
329+
</property>
330+
<property name="orientation">
331+
<enum>Qt::Horizontal</enum>
332+
</property>
333+
</widget>
334+
</item>
335+
<item>
336+
<widget class="QSpinBox" name="mContrastSpinBox">
337+
<property name="minimum">
338+
<number>-100</number>
339+
</property>
340+
<property name="maximum">
341+
<number>100</number>
342+
</property>
343+
</widget>
344+
</item>
345+
</layout>
346+
</widget>
347+
</item>
251348
<item>
252349
<spacer name="verticalSpacer">
253350
<property name="orientation">
@@ -1183,7 +1280,7 @@ p, li { white-space: pre-wrap; }
11831280
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
11841281
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
11851282
p, li { white-space: pre-wrap; }
1186-
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Lucida Grande'; font-size:13pt; font-weight:400; font-style:normal;&quot;&gt;
1283+
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Droid Sans'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
11871284
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'Cantarell'; font-size:11pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
11881285
</property>
11891286
</widget>

0 commit comments

Comments
 (0)
Please sign in to comment.