#include <QImage>#include "math.h"template<class T>inline const T& kClamp( const T& x, const T& low, const T& high ){ if ( x < low ) return low; else if ( high < x ) return high; else return x;}inlineint changeBrightness( int value, int brightness ) { return kClamp( value + brightness * 255 / 100, 0, 255 ); }inlineint changeContrast( int value, int contrast ) { return kClamp((( value - 127 ) * contrast / 100 ) + 127, 0, 255 ); }inlineint changeGamma( int value, int gamma ) { return kClamp( int( pow( value / 255.0, 100.0 / gamma ) * 255 ), 0, 255 ); }inlineint changeUsingTable( int value, const int table[] ) { return table[ value ]; }template< int operation( int, int ) >staticQImage changeImage( const QImage& image, int value ) { QImage im = image; im.detach(); if( im.numColors() == 0 ) /* truecolor */ { if( im.format() != QImage::Format_RGB32 ) /* just in case */ im = im.convertToFormat( QImage::Format_RGB32 ); int table[ 256 ]; for( int i = 0; i < 256; ++i ) table[ i ] = operation( i, value ); if( im.hasAlphaChannel() ) { for( int y = 0; y < im.height(); ++y ) { QRgb* line = reinterpret_cast< QRgb* >( im.scanLine( y )); for( int x = 0; x < im.width(); ++x ) line[ x ] = qRgba( changeUsingTable( qRed( line[ x ] ), table ), changeUsingTable( qGreen( line[ x ] ), table ), changeUsingTable( qBlue( line[ x ] ), table ), changeUsingTable( qAlpha( line[ x ] ), table )); } } else { for( int y = 0; y < im.height(); ++y ) { QRgb* line = reinterpret_cast< QRgb* >( im.scanLine( y )); for( int x = 0; x < im.width(); ++x ) line[ x ] = qRgb( changeUsingTable( qRed( line[ x ] ), table ), changeUsingTable( qGreen( line[ x ] ), table ), changeUsingTable( qBlue( line[ x ] ), table )); } } } else { QVector<QRgb> colors = im.colorTable(); for( int i = 0; i < im.numColors(); ++i ) colors[ i ] = qRgb( operation( qRed( colors[ i ] ), value ), operation( qGreen( colors[ i ] ), value ), operation( qBlue( colors[ i ] ), value )); } return im; }// brightness is multiplied by 100 in order to avoid floating point numbersQImage changeBrightness( const QImage& image, int brightness ) { if( brightness == 0 ) // no change return image; return changeImage< changeBrightness >( image, brightness ); }// contrast is multiplied by 100 in order to avoid floating point numbersQImage changeContrast( const QImage& image, int contrast ) { if( contrast == 100 ) // no change return image; return changeImage< changeContrast >( image, contrast ); }// gamma is multiplied by 100 in order to avoid floating point numbersQImage changeGamma( const QImage& image, int gamma ) { if( gamma == 100 ) // no change return image; return changeImage< changeGamma >( image, gamma ); }