simple question about XImage & XPutPixel - Xwindows

This is a discussion on simple question about XImage & XPutPixel - Xwindows ; Hello everyone I have just written a simple app under linux to display images using xlib. It's all working fine but there's a point I'd like to clarify. If you create an array of rgb pixels (unsigned char *rgbImage = ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: simple question about XImage & XPutPixel

  1. simple question about XImage & XPutPixel

    Hello everyone

    I have just written a simple app under linux to display images using
    xlib. It's all working fine but there's a point I'd like to clarify.
    If you create an array of rgb pixels (unsigned char *rgbImage = new
    unsigned char[width * height * 3]) & have an ximage data pointing to
    it (ximg->data = rgbImage) it seems possible to directly alter the
    content of rgbImage before doing an XPutImage instead of enconding the
    rgb values into a unsigned long & use XPutPixel. I also noticed than
    when you do a XDestroyImage, the function deletes the content of
    data... so i had to do something like that...
    ximg->data = NULL;
    delete [] rgbImage;
    XDestroyImage(ximg);

    so my question why providing a XPutPixel function if we can simply
    change the image by changing the values of ximg->data ? Am I doing
    something wrong ? For example when I create an XImage does the
    function actually creathe the memory for the image internally that I
    can't change with XPutPixel or is what I am doing the right thing to
    do... (allocate some memory, fill with the rgb values properly
    enconded and get ximg->data to point to this array ?).

    Thanks for your help -coralie


  2. Re: simple question about XImage & XPutPixel

    On 2007-10-11, mast4as wrote:

    > I have just written a simple app under linux to display images using
    > xlib. It's all working fine but there's a point I'd like to clarify.
    > If you create an array of rgb pixels (unsigned char *rgbImage = new
    > unsigned char[width * height * 3]) & have an ximage data pointing to
    > it (ximg->data = rgbImage) it seems possible to directly alter the
    > content of rgbImage before doing an XPutImage instead of enconding the
    > rgb values into a unsigned long & use XPutPixel.


    It's more complicated than that, but that's the general idea.

    > so my question why providing a XPutPixel function if we can simply
    > change the image by changing the values of ximg->data ?


    Because changing the value of a pixel in an XImage is actually
    fairly involved :

    - there may be padding between the lines (scanline_pad),

    - pixels may be subject to alignment (bits_per_pixel, which is
    frequently 32 on 24-bit displays),

    - the place of the red, green and blue bits within
    bits_per_pixel is not fixed (red_mask, green_mask, blue_mask),

    - there may be more than one pixel per byte (if bits_per_pixel <
    8, check bitmap_bit_order),

    - if the endianness of the machine your program runs on differs
    from the endianness of the X server, pixel values cannot be
    memcpy()'d onto an XImage buffer.

    XPutPixel() is not the fastest way to change pixels but it's
    good to have. If performance is not critical, you can use it and
    save yourself quite a bit of coding and testing.

    People who do direct access on XImage.data often don't get all
    the cases right and as a result, their code won't work on all
    displays.

    --
    André Majorel
    (Counterfeit: myt@hedonism.com dawav@moyer.com)
    There is always someone somewhere who needs a good laugh.

  3. Re: simple question about XImage & XPutPixel

    > People who do direct access on XImage.data often don't get all
    > the cases right and as a result, their code won't work on all
    > displays.


    Makes sense. Writing the code on both an fairly old powerbook & a
    linux box I indeed had to deal with endianess. And the man
    documentation about XPutImage & XDestroyImage is fairly clear about
    the fact that it is reponsible for releasing the memory of the
    XImage's data.
    Thank you -


+ Reply to Thread