From: stv Date: Wed, 30 Aug 2006 09:01:13 +0000 (+0000) Subject: Functions for image manipulation were added. X-Git-Tag: LAST_STABLE_VERSION_21_09_2006_ON_3_2_0~19 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d07093f077ce5dd55301ce977b67dbb63c287775;p=modules%2Fgui.git Functions for image manipulation were added. --- diff --git a/src/Qtx/Qtx.cxx b/src/Qtx/Qtx.cxx index 14704e550..6e2a31648 100755 --- a/src/Qtx/Qtx.cxx +++ b/src/Qtx/Qtx.cxx @@ -22,9 +22,11 @@ #include "Qtx.h" #include +#include #include #include #include +#include #include #include #include @@ -556,3 +558,144 @@ void Qtx::scaleColors( const int num, QValueList& lst ) for ( int i = 0; i < num; i++ ) lst.append( scaleColor( i, 0, num - 1 ) ); } + +/*! + Name: grayscale [static public] + Desc: Convert color image to grayscale image. +*/ +QImage Qtx::grayscale( const QImage& img ) +{ + QImage res = img; + + int colNum = res.numColors(); + if ( colNum ) + { + for ( int i = 0; i < colNum; i++ ) + res.setColor( i, qGray( res.color( i ) ) ); + } + else + { + for ( int y = 0; y < res.height(); y++ ) + { + for ( int x = 0; x < res.width(); x++ ) + { + QRgb pix = res.pixel( x, y ); + res.setPixel( x, y, qRgba( qGray( pix ), qGray( pix ), qGray( pix ), qAlpha( pix ) ) ); + } + } + } + + return res; +} + +/*! + Name: grayscale [static public] + Desc: Convert color pixmap to grayscale pixmap. +*/ +QPixmap Qtx::grayscale( const QPixmap& pix ) +{ + QPixmap res; + res.convertFromImage( grayscale( pix.convertToImage() ) ); + return res; +} + +/*! + Name: transparentImage [static public] + Desc: Create transparent image with specified width \aw, height \ah and color depth \ad. +*/ +QImage Qtx::transparentImage( const int w, const int h, const int d ) +{ + QImage img; + if ( img.create( w, h, d < 0 ? QPixmap::defaultDepth() : d ) ) + { + img.setAlphaBuffer( true ); + for ( int i = 0; i < img.height(); i++ ) + for ( int j = 0; j < img.width(); j++ ) + img.setPixel( j, i, qRgba( 0, 0, 0, 0 ) ); + } + return img; +} + +/*! + Name: transparentPixmap [static public] + Desc: Create transparent pixmap with specified width \aw, height \ah and color depth \ad. +*/ +QPixmap Qtx::transparentPixmap( const int w, const int h, const int d ) +{ + QPixmap pix; + QImage img = transparentImage( w, h, d ); + if ( !img.isNull() ) + pix.convertFromImage( img ); + return pix; +} + +/*! + Name: composite [static public] + Desc: Create composite pixmap. Pixmap 'pix' draws over pixmap 'dest' with coordinates + specified relative upper left corner of 'dest'. If 'dest' not given then new empty + pixmap with appropriate size created. +*/ +QPixmap Qtx::composite( const QPixmap& pix, const int x, const int y, const QPixmap& dest ) +{ + if ( pix.isNull() ) + return dest; + + int width = QMAX( pix.width() + x, dest.width() ); + int height = QMAX( pix.height() + y, dest.height() ); + + QPixmap res( width, height ); + QImage img = transparentImage( width, height, 32 ); + + QPainter p; + p.begin( &res ); + p.fillRect( 0, 0, width, height, QBrush( white ) ); + + if ( !dest.isNull() ) + { + p.drawPixmap( 0, 0, dest ); + QImage temp = dest.convertToImage(); + for ( int i = 0; i < temp.width() && i < img.width(); i++ ) + { + for ( int j = 0; j < temp.height() && j < img.height(); j++ ) + { + if ( temp.hasAlphaBuffer() ) + img.setPixel( i, j, temp.pixel( i, j ) ); + else + { + QRgb p = temp.pixel( i, j ); + img.setPixel( i, j, qRgba( qRed( p ), qGreen( p ), qBlue( p ), 255 ) ); + } + } + } + } + + p.drawPixmap( x, y, pix ); + QImage temp = pix.convertToImage(); + for ( int c = x; c < temp.width() + x && c < img.width(); c++ ) + { + for ( int r = y; r < temp.height() + y && r < img.height(); r++ ) + { + if ( qAlpha( temp.pixel( c - x, r - y ) ) > 0 ) + img.setPixel( c, r, temp.pixel( c - x, r - y ) ); + } + } + + p.end(); + + for ( int ai = 0; ai < img.width(); ai++ ) + { + for ( int aj = 0; aj < img.height(); aj++ ) + { + if ( qAlpha( img.pixel( ai, aj ) ) < 1 ) + img.setPixel( ai, aj, qRgba( 255, 255, 255, 255 ) ); + else + img.setPixel( ai, aj, qRgba( 0, 0, 0, 0 ) ); + } + } + + QBitmap bmp( width, height ); + bmp.convertFromImage( img, Qt::ColorMode_Mask | Qt::ThresholdDither ); + res.setMask( bmp ); + + return res; +} diff --git a/src/Qtx/Qtx.h b/src/Qtx/Qtx.h index 1fef63f04..0d5cb3a40 100755 --- a/src/Qtx/Qtx.h +++ b/src/Qtx/Qtx.h @@ -68,6 +68,9 @@ #endif #endif +#include +#include + class QObject; class QString; class QWidget; @@ -131,6 +134,12 @@ public: static QColor scaleColor( const int, const int, const int ); static void scaleColors( const int, QValueList& ); + + static QImage grayscale( const QImage& ); + static QPixmap grayscale( const QPixmap& ); + static QImage transparentImage( const int, const int, const int = -1 ); + static QPixmap transparentPixmap( const int, const int, const int = -1 ); + static QPixmap composite( const QPixmap&, const int, const int, const QPixmap& = QPixmap() ); }; #endif