Results 1 to 6 of 6

Thread: need some clarification on QImage and QRgb usage

  1. #1
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    1

    Default need some clarification on QImage and QRgb usage

    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:

    Qt Code:
    1. QImage image2 = image1->scaled(image1->size()/2);
    To copy to clipboard, switch view to plain text mode 
    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'?

    Qt Code:
    1. for(y=0;y<height;y++){
    2. QRgb* image2 = (QRgb*)image1->scanLine(y);
    3. //...
    To copy to clipboard, switch view to plain text mode 
    Question 2:
    Same thing here. Is 'QRgb* image2' allocating memory to store the scanline of 'image1'?

    Qt Code:
    1. QVector<QRgb> colortable;
    2. image1->setColorTable(colortable);
    To copy to clipboard, switch view to plain text mode 
    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!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need some clarification on QImage and QRgb usage

    Quote Originally Posted by kona View Post
    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.


    Quote Originally Posted by kona View Post
    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.


    Quote Originally Posted by kona View Post
    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.

  3. The following user says thank you to jacek for this useful post:

    kona (4th September 2008)

  4. #3
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    1

    Default Re: need some clarification on QImage and QRgb usage

    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!!!

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need some clarification on QImage and QRgb usage

    Quote Originally Posted by kona View Post
    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().

  6. #5
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    1

    Default Re: need some clarification on QImage and QRgb usage

    ok, final question to make sure i understand all this color table stuff.

    Qt Code:
    1. QVector<QRgb> colortable;
    2. colortable.append(qRgb(0,0,0));
    3. colortable.append(qRgb(255,255,255));
    4. myimage.setColorTable(colortable);
    To copy to clipboard, switch view to plain text mode 

    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 :/

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need some clarification on QImage and QRgb usage

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.