PDA

View Full Version : QImage & Pixel manipulation



lycos
29th June 2013, 18:20
Hi guys,

I'm italian and my english is not so good so...I'll try as possible in a very schematic way!

1. What I want

I have an atoms square box L*L (physics simulation) and each of them have two possible spin values (binary information, very simple). I want to display this box to an image (atom ---> pixel, spin value ----->pixel color ).
So, I initialize my box and display it to my MainWindow. But during the simulation, in each step of my loop I select a spin (a pixel) and I flip it (flip pixel's color).
It's all! (simply)

2. What I've done...

The first algorithm works fine (I can view my image in a correct way)....




/* Previously, I declared

QImage *reticolo_image=new QImage(L,L,QImage::Format_RGB32);
QPixmap *pix=new QPixmap;
pix->fromImage(*reticolo_image); */


void MainWindow::inizializzaImage(int *reticolo)

/* reticolo is a Pointer to array representing my box..I don't use a matrix
representation, it's the same. */
{
QRgb downValue, upValue;
downValue=qRgb(122,163,39); //green
upValue=qRgb(237,187,51); //orange

int t;
for(t=0;t<L*L;t++)
{
if (t==0)
{
if (reticolo[t]==1) reticolo_image->setPixel(0,0,upValue);
if (reticolo[t]==-1) reticolo_image->setPixel(0,0,downValue);
}
else
{
if (reticolo[t]==1) reticolo_image->setPixel(t%L,(int)(t/L),upValue); // t%L=y t/L=x in matricial representation
if (reticolo[t]==-1) reticolo_image->setPixel(t%L,(int)(t/L),downValue);
}
}

reticolo_image->scaled(QSize(270, 270));
QPixmap *pixtmp=new QPixmap(270,270);
pixtmp->fromImage(*reticolo_image);
pix=pixtmp;
ui->labelImage->setPixmap(*pix);

ui->labelImage->show();

}


The problem is that I can't upload or view any pixel color change on the screen. I thing that this is a correct (and simple) way to approach this question, instead using QPainter and similar.
I've simply changed a pixel color with setPixel without using tableColor because I selected RGB32 format.




void MainWindow::changeSpin(int spin, int value)
{
int x,y, L;
QRgb downValue, upValue;
downValue=qRgb(122,163,39); //green
upValue=qRgb(237,187,51); //orange

L=ui->sL->value();
x=spin%L;
y=(int)(spin/L);
if (value==1) reticolo_image->setPixel(x,y,downValue);
if (value==-1) reticolo_image->setPixel(x,y,upValue);


ui->labelImage->setPixmap(*pix); // I've also tried without replacing these last rows
ui->labelImage->show();
}


PS: the second algorithm is a SLOT called by a SIGNAL emitted in my main simulation function.

I hope someone help me...I know it!

You are a great resource...
Thanks

wysota
29th June 2013, 21:15
You need to convert the changed image to the pixmap again and set it on the label.

lycos
1st July 2013, 13:33
Ok, thanks for the answer!

now I can see changes but... the process is too slow. For each step I have to convert the entire image into a pixmap and not only one pixel.
Is there any way to set my first pixel configuration as background and change only one of them? (I'm talking about computational time...)

Thanks

wysota
1st July 2013, 16:46
You can open a painter on the pixmap and overpaint what you want using QPainter API.