You are not logged in.

1

Tuesday, June 28th 2005, 3:25pm

Get and set Pixel in QImage

Hi,I am a newbie in QT.
I am trying to load a PGM image and change its pixels.
FOr this purpose I use QImage and I learned how to load the image
But I can't change the pixels of the image.
Here is a part of my code which I cant make it work


QRgb px = image.pixel(90,90);

image.setPixel( 20,20,px);

I am trying to read the first pixel(90,90) and set its color to the 20,20 pixel but it's not working.

What should I do to make it work ?

Thanks

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Tuesday, June 28th 2005, 3:28pm

RE: Get and set Pixel in QImage

Quoted

Originally posted by spent
I am trying to read the first pixel(90,90) and set its color to the 20,20 pixel but it's not working.

What do you mean by "it's not working"? Do you get any errors?

3

Wednesday, June 29th 2005, 8:58am

No, there are no errors,
the pixel (90,90) has color Blue,the pixel(20,20) is white
The program works without any errors but there is no effect.
I am trying yo get the blue color from the 90,90 pixel and set it to the white pixel, but no effect, The white pixel remain white all the time

dimitri

Professional

  • "dimitri" is male

Posts: 1,311

Occupation: Engineer

  • Send private message

4

Wednesday, June 29th 2005, 9:05am

The problem is that we see how you manipulate the QImage, but we don't see how you verify the manipulation doesn't work.

Maybe the code that verifies whether it worked actually is wrong.

5

Wednesday, June 29th 2005, 9:29am

Now I see that when I get the value with
image.pixel(20,20)); it returns blue color indeed, but on the screen it remains white
I thought that I can't see a single pixel, so I made it a square about 400 pixels. They all return blue color with the pixel function but on the screen I see white,
Do I have to do something like repaining or refreshing the image ?

This post has been edited 1 times, last edit by "spent" (Jun 29th 2005, 9:31am)


jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

6

Wednesday, June 29th 2005, 11:07am

Quoted

Originally posted by spent
Do I have to do something like repaining or refreshing the image ?

Could you post some code? How do you create that QImage object? Where it should be displayed?

7

Thursday, June 30th 2005, 9:41am

Here is the LoadImage function

Quoted


bool ImageViewer::loadImage( const QString& fileName )
{
filename = fileName;
bool ok = FALSE;
if ( !filename.isEmpty() ) {
QApplication::setOverrideCursor( waitCursor ); // this might take time
ok = image.load(filename, 0);
pickx = -1;
clickx = -1;
if ( ok )
ok = reconvertImage();
if ( ok ) {
setCaption( filename ); // set window caption
int w = pm.width();
int h = pm.height();

const int reasonable_width = 128;
if ( w < reasonable_width ) {
// Integer scale up to something reasonable
int multiply = ( reasonable_width + w - 1 ) / w;
w *= multiply;
h *= multiply;
}

h += menubar->heightForWidth(w) + status->height();
resize( w, h ); // we resize to fit image
} else {
pm.resize(0,0); // couldn't load image
update();
}
QApplication::restoreOverrideCursor(); // restore original cursor
}
updateStatus();
setMenuItemFlags();
return ok;
}




This is the code I use the existing project in Examples/showimg to learn and practice on it

dimitri

Professional

  • "dimitri" is male

Posts: 1,311

Occupation: Engineer

  • Send private message

8

Thursday, June 30th 2005, 9:50am

Please use the Insert CODE button instead of the Insert Quote button to post source code, otherwise it's unreadable.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

9

Thursday, June 30th 2005, 12:52pm

Quoted

Originally posted by spent
Here is the LoadImage function
...
This is the code I use the existing project in Examples/showimg to learn and practice on it

Before this image can be shown, it must be converted to a QPixmap. It looks like ImageViewer::setImage does it. You could also try:

Source code

1
2
reconvertImage();
repaint( image.hasAlphaBuffer() );

10

Thursday, June 30th 2005, 6:35pm

It Works !
Thank you all for your help.You have been most helpful to me.