[Qt-interest] Painting on QImage vs. QPixmap

Brad Howes howes at ll.mit.edu
Tue Mar 24 17:41:42 CET 2009


I wrote a simple application to measure whether QPainting on a QImage  
was faster than doing so on a QPixmap. Interestingly, it depends on  
what operations you are doing. The source (scooby.tgz) is available at

     http://idisk.mac.com/bradhowes-Public?view=web

At least on my MacBook Pro running Qt 4.5 it is measurably slower to  
do indexing into a QImage (via setPixel) and then convert the QImage  
into a QPixmap for displaying vs. using QPainter to draw directly into  
a QPixmap via drawPoints (all the same color). However, if I request  
that each pixel get its own color, then QImage wins.

I'd be curious if anyone can speed these cases up (the code is  
*really* simple). Here are the two drawing routines:

void
MainWindow::doImageUpdate()
{
     bool colorPerPixel = ui_.colorPerPixel->isChecked();
     QRgb color( qrand() );
     size_t count = ui_.pixelCount->value();
     while ( count-- ) {
	image_.setPixel( QPoint( x_++, y_ ), color );
	if ( colorPerPixel )
	    color = QRgb( qrand() );

	if ( x_ == ui_.image->width() ) {
	    x_ = 0;
	    ++y_;
	}
	if ( y_ == ui_.image->height() ) {
	    y_ = 0;
	}
     }

     pixmap_ = QPixmap::fromImage( image_ );
     ui_.image->setPixmap( pixmap_ );
}

void
MainWindow::doPixmapUpdate()
{
     QPainter painter;
     painter.begin( &pixmap_ );
     if ( ui_.colorPerPixel->isChecked() ) {
	size_t count = ui_.pixelCount->value();
	while ( count-- ) {
	    QRgb color( qrand() );
	    painter.setPen( color );
	    painter.drawPoint( QPoint( x_++, y_ ) );
	    if ( x_ == ui_.image->width() ) {
		x_ = 0;
		++y_;
	    }
	    if ( y_ == ui_.image->height() ) {
		y_ = 0;
	    }
	}
     }
     else {
	std::vector<QPoint> points( ui_.pixelCount->value() );
	for ( size_t index = 0; index < points.size(); ++index ) {
	    points[ index ] = QPoint( x_++, y_ );
	    if ( x_ == ui_.image->width() ) {
		x_ = 0;
		++y_;
	    }
	    if ( y_ == ui_.image->height() ) {
		y_ = 0;
	    }
	}

	QRgb color( qrand() );
	painter.setPen( color );
	painter.drawPoints( &points[ 0 ], points.size() );
	painter.end();
     }

     ui_.image->setPixmap( pixmap_ );
}



Brad

-- 
Brad Howes
Group 42
MIT Lincoln Laboratory • 244 Wood St. • Lexington, MA 02173
Phone: 781.981.5292 • Fax: 781.981.3495 • Secretary: 781.981.7420








More information about the Qt-interest-old mailing list