#include "Qtx.h"
#include <qdir.h>
+#include <qimage.h>
+#include <qcolor.h>
#include <qstring.h>
#include <qwidget.h>
#include <qlayout.h>
+#include <qbitmap.h>
+#include <qpainter.h>
#include <qtoolbar.h>
#include <qgroupbox.h>
#include <qfileinfo.h>
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: 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( width, height, 32 );
+ 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 ) );
+
+ 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;
+}
QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const
{
- QString fname = fileName( sect, prefix, name );
- bool toCache = resMgr() ? resMgr()->isPixmapCached() : false;
QPixmap p;
- if( toCache && myPixmapCache.contains( fname ) )
+ QString fname = fileName( sect, prefix, name );
+ bool toCache = resMgr() && resMgr()->isPixmapCached();
+ if ( toCache && myPixmapCache.contains( fname ) )
p = myPixmapCache[fname];
else
{
p.load( fname );
- if( toCache )
- ( ( QMap<QString,QPixmap>& )myPixmapCache ).insert( fname, p );
+ if ( toCache )
+ {
+ Resources* that = (Resources*)this;
+ that->myPixmapCache.insert( fname, p );
+ }
}
return p;
}