PDA

View Full Version : need some clarification on QImage and QRgb usage



kona
3rd September 2008, 22:02
i'm taking a few lines of code i've found on the internet and am trying to write the c++ equivalent, however, the documentation on some of these classes/functions is often vague or not as comprehensive as i would like. also, i don't have qt set up to run/test any of this myself as i only needed to figure out these 3 things.

here are three code samples i'm working with and my questions for each one:



QImage image2 = image1->scaled(image1->size()/2);

Question 1:
does 'QImage image2' malloc memory and then contain a scaled version of 'image1', OR is 'image1' actually being scaled itself and 'image2' is just a pointer to 'image1'?



for(y=0;y<height;y++){
QRgb* image2 = (QRgb*)image1->scanLine(y);
//...

Question 2:
Same thing here. Is 'QRgb* image2' allocating memory to store the scanline of 'image1'?



QVector<QRgb> colortable;
image1->setColorTable(colortable);

Question 3:
what exactly is setColorTable doing/creating? it seems to be some sort of array/list of unique colors found in 'image1' - sort of like a palette, but that's all i could guess. if so, is this list sorted in any fashion, or is there anything else that might be important to know about it?

thanks in advance for your help!!!

jacek
4th September 2008, 00:06
does 'QImage image2' malloc memory and then contain a scaled version of 'image1', OR is 'image1' actually being scaled itself and 'image2' is just a pointer to 'image1'?
The docs say:
Returns a copy of the image scaled to a rectangle defined by the given size according to the given aspectRatioMode and transformMode.
Also if you check the declaration, you will see that this method is marked as "const", so it can't modify the object it is being invoked on.

If you add these two together, you will know that image1 remains unchanged and image2 is a scaled copy of image1.



Same thing here. Is 'QRgb* image2' allocating memory to store the scanline of 'image1'?
First of all QRgb * is a pointer, so it can't perform any actions when it is created --- it doesn't have a constructor.

The docs say:
Returns a pointer to the pixel data at the scanline with index i. The first scanline is at index 0.
So you get a pointer that points to internal QImage data. No copy is created.



what exactly is setColorTable doing/creating? it seems to be some sort of array/list of unique colors found in 'image1' - sort of like a palette, but that's all i could guess. if so, is this list sorted in any fashion, or is there anything else that might be important to know about it?
If the image is in an indexed format, the colors are stored in color table and image data (the one you can access with scanLine()) doesn't contain QRgb values, but indices.

For example if scanLine(x)[y] is 0, it means that given pixel has the same color as the first color in the color table. If scanLine(x)[y] is 42, it means that the color of the pixel is color(42) (or colorTable()[42]).

If you change a color at given index in color table, all pixels that refer to that index will change their colors.

Remember that his is only valid for indexed images.

kona
4th September 2008, 00:17
thanks for clearing all that up! guess i could've figured out the first two had i given it more thought.

one followup question, though. when you say 'indexed images', do you simply mean, 8-bit, as opposed to 16, or 32 bits per channel?

thanks again!!!

jacek
4th September 2008, 00:40
one followup question, though. when you say 'indexed images', do you simply mean, 8-bit, as opposed to 16, or 32 bits per channel?
The only 8-bit format supported by Qt is also the only indexed format. So in Qt yes, but you can't say "simply" in general as you can imagine other indexed formats or non-indexed 8-bit format.

You can check if the image is indexed using QImage::format().

kona
6th September 2008, 01:42
ok, final question to make sure i understand all this color table stuff.



QVector<QRgb> colortable;
colortable.append(qRgb(0,0,0));
colortable.append(qRgb(255,255,255));
myimage.setColorTable(colortable);


does this give me a two-color image, and if so, how does it determine which values to set to 0,0,0, and which to set to 255,255,255?

thought i understood it, but maybe i don't :/

jacek
6th September 2008, 14:20
If pixelIndex() returns 0 the pixel will use (0,0,0) color, if it returns 1 --- (255,255,255).

If you have an image in 8-bit indexed format. scanLine() will return you a pointer to an array where each byte is either zero and one, depending on the color of the pixel.