Seite 1 von 1

Schnelles Pixelweises ausgeben

Verfasst: 12. September 2007 16:35
von toscen
Hallo,

ich muss (simuliere ein vga device dass über die svgalib angesprochen wird) pixelweise in ein bild schreiben und dieses in echtzeit anzeigen lassen.

Hierzu verwende ich ein QPixmap, auf welches ich mit QPainter.setPen() und dann QPainter.drawPoint() pixel ausgeben.
Anzeigen lass ich mir das in einem QLabel, wekches ich dann immer wieder mit der funktion setPixmap auf das aktuelle Pixmap updaten muss.

Jedoch sind das eine Menge Pixel die da geschrieben werden müssen und das ganze ist schon sehr langsam. Daher suche ich nach einer Möglichkeit dies schneller zu machen.

QImage soll ja schneller sein. Aber wie Zeige ich dies in Echtzeit an?

Vielleicht kennt auch jmd noch eine andere Möglichkeit.


Gruß
Thomas

Verfasst: 12. September 2007 16:47
von CaptnChaos
ich habe da mal ein tool geschrieben, was das mit qimage macht. hoffe das hilft dir.

rgbscanner.h

Code: Alles auswählen

#ifndef RGBScanner_H_
#define RGBScanner_H_
#include <QtGui>
class RGBScanner : public QWidget
{
      Q_OBJECT
      
public:
       RGBScanner(QWidget *parent = 0);
	   void Stage1();
	   void Stage2();
	   void Stage3();
	   void Stage4();
	   void Stage5();
	   void ScanImage();
	   void paintEvent(QPaintEvent *e);
private:
	int scanPos;
	int scanMode;
	QTimer *scanTimer;
	QImage scanImg;
	int iWidth;
	QPixmap dPix;
};
#endif
rgbscanner.cpp

Code: Alles auswählen

#include "rgbscanner.h"
RGBScanner::RGBScanner(QWidget *parent)
   : QWidget(parent)
{
    scanPos = 0;
	scanMode = 1;
    scanTimer = new QTimer();
	scanImg.load("Scan.png");
	scanTimer->start(0);
	connect(scanTimer, SIGNAL(timeout()), this, SLOT(repaint()));
	iWidth = scanImg.width();
	setFixedSize(scanImg.width(), scanImg.height());
}

void RGBScanner::ScanImage()
{
	
}
void RGBScanner::paintEvent(QPaintEvent *e)
{
	if(scanMode == 6)
		scanMode = 1;
	if(scanPos == scanImg.height()+1)
	{
		scanPos = 0;
		++scanMode;
	}
	switch(scanMode)
	{
		case 1:
			scanTimer->start(10);
			dPix = QPixmap(scanImg.width(), scanImg.height());
			dPix.fill();
			Stage1();
		break;
		case 2:
			dPix = QPixmap(scanImg.width(), scanImg.height());
			dPix.fill();
			Stage2();
		break;
		case 3:
			dPix = QPixmap(scanImg.width(), scanImg.height());
			dPix.fill();
			Stage3();
		break;
		case 4:
			dPix = QPixmap(scanImg.width(), scanImg.height());
			dPix.fill();
			Stage4();
		break;
		case 5:
			scanTimer->start(30);
			Stage5();
		break;
	}
	QPainter p(this);
	p.fillRect(rect(), dPix);
	p.end();
	++scanPos;
}
void RGBScanner::Stage1()
{
	QPainter p(&dPix);
	QImage sImg = scanImg;
	for(int i = 0; i < scanPos; ++i)
		for(int j = 0; j < iWidth; ++j)
		{
			QRgb rgb = qRgba(qRed(scanImg.pixel(j,i)),0,0,qAlpha(scanImg.pixel(j,i)));
			sImg.setPixel(j,i, rgb);
		}
				
		p.drawPixmap(QRect(0, 0, iWidth, scanPos), 
					 QPixmap::fromImage(sImg), 
					 QRect(0, 0, iWidth, scanPos));
		for(int j = 0; j < iWidth; ++j)
		{
			
			QColor fc = QColor(qRed(scanImg.pixel(j,scanPos)), 0, 0,qAlpha(scanImg.pixel(j,scanPos)));
			p.fillRect(j,scanPos,10,10,fc);
		}
		p.setPen(QPen(Qt::green, 1));
		p.drawLine(QPoint(0, scanPos+9), QPoint(iWidth, scanPos+9));
		p.drawLine(QPoint(0, scanPos+10), QPoint(iWidth, scanPos+10));
	
	p.end();
}
void RGBScanner::Stage2()
{
	
	QPainter p(&dPix);
	QImage sImg = scanImg;
	for(int i = 0; i < scanPos; ++i)
		for(int j = 0; j < iWidth; ++j)
		{
			QRgb rgb = qRgba(0,qGreen(scanImg.pixel(j,i)),0,qAlpha(scanImg.pixel(j,i)));
			sImg.setPixel(j,i, rgb);
		}
				
		p.drawPixmap(QRect(0, 0, iWidth, scanPos), 
					 QPixmap::fromImage(sImg), 
					 QRect(0, 0, iWidth, scanPos));
		for(int j = 0; j < iWidth; ++j)
		{
			
			QColor fc = QColor(0, qGreen(scanImg.pixel(j,scanPos)), 0,qAlpha(scanImg.pixel(j,scanPos)));
			p.fillRect(j,scanPos,10,10,fc);
		}
		p.setPen(QPen(Qt::red, 1));
		p.drawLine(QPoint(0, scanPos+9), QPoint(iWidth, scanPos+9));
		p.drawLine(QPoint(0, scanPos+10), QPoint(iWidth, scanPos+10));
	p.end();
}
void RGBScanner::Stage3()
{
	QPainter p(&dPix);
	QImage sImg = scanImg;
	for(int i = 0; i < scanPos; ++i)
		for(int j = 0; j < iWidth; ++j)
		{
			QRgb rgb = qRgba(0,0,qBlue(scanImg.pixel(j,i)),qAlpha(scanImg.pixel(j,i)));
			sImg.setPixel(j,i, rgb);
		}
				
		p.drawPixmap(QRect(0, 0, iWidth, scanPos), 
					 QPixmap::fromImage(sImg), 
					 QRect(0, 0, iWidth, scanPos));
		for(int j = 0; j < iWidth; ++j)
		{
			
			QColor fc = QColor(0, 
							   0,
							   qBlue(scanImg.pixel(j,scanPos)),qAlpha(scanImg.pixel(j,scanPos)));
			p.fillRect(j,scanPos,10,10,fc);
		}
		p.setPen(QPen(Qt::yellow, 1));
		p.drawLine(QPoint(0, scanPos+9), QPoint(iWidth, scanPos+9));
		p.drawLine(QPoint(0, scanPos+10), QPoint(iWidth, scanPos+10));
	
	p.end();
}
void RGBScanner::Stage4()
{
	QPainter p(&dPix);
	p.drawPixmap(QRect(0, 0, iWidth, scanPos), 
					 QPixmap::fromImage(scanImg.rgbSwapped()), 
					 QRect(0, 0, iWidth, scanPos));
		for(int j = 0; j < iWidth; ++j)
		{
			
			QColor fc = QColor(qBlue(scanImg.pixel(j,scanPos)), 
							   qGreen(scanImg.pixel(j,scanPos)),
							   qRed(scanImg.pixel(j,scanPos)),qAlpha(scanImg.pixel(j,scanPos)));
			p.fillRect(j,scanPos,10,10,fc);
		}
		p.setPen(QPen(Qt::green, 1));
		p.drawLine(QPoint(0, scanPos+9), QPoint(iWidth, scanPos+9));
		p.setPen(QPen(Qt::red, 1));
		p.drawLine(QPoint(0, scanPos+10), QPoint(iWidth, scanPos+10));
		p.setPen(QPen(Qt::yellow, 1));
		p.drawLine(QPoint(0, scanPos+11), QPoint(iWidth, scanPos+11));
	
	p.end();
}
void RGBScanner::Stage5()
{
	QPainter p(&dPix);
	int hor = (scanImg.height()/iWidth)+1;
	
	p.fillRect(rect(), QPixmap::fromImage(scanImg));
	
	
		p.setPen(QPen(Qt::green, 1));
		p.drawLine(QPointF(scanPos, 0), QPointF(scanPos, scanImg.height()));
		p.setPen(QPen(Qt::red, 1));
		p.drawLine(QPointF(iWidth-scanPos, 0), QPointF(iWidth-scanPos, scanImg.height()));
		p.setPen(QPen(Qt::yellow, 1));
		p.drawLine(QPointF(0, scanPos*hor), QPointF(iWidth, scanPos*hor));	
	    p.setPen(QPen(Qt::blue, 1));
		p.drawLine(QPointF(0, scanImg.height()-scanPos*hor), QPointF(iWidth, scanImg.height()-scanPos*hor));		
	
	p.end();
}		

Verfasst: 14. September 2007 11:47
von toscen
Danke...

hat mir weitergeholfen.. habe es nun so ähnlich gelöst.. ist nun wesentlich schneller...


gruß
Thomas