Displaying an image - Xwindows

This is a discussion on Displaying an image - Xwindows ; I've been having a horrible time trying to blit a image to the screen. Maybe someone can help me out.... #define WIDTH 800 #define HEIGHT 800 int image[WIDTH][HEIGHT]; // Fill in the image with data here w = XCreateSimpleWindow(display, DefaultRootWindow(display), ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Displaying an image

  1. Displaying an image

    I've been having a horrible time trying to blit a image to the screen.
    Maybe someone can help me out....

    #define WIDTH 800
    #define HEIGHT 800
    int image[WIDTH][HEIGHT];
    // Fill in the image with data here

    w = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0,
    WIDTH, HEIGHT, 0, blackColor, blackColor);
    XMapWindow(display, w);
    gc = XCreateGC(display, w, 0, NULL);
    XSync(display,False);
    img = XCreateImage(display, DefaultVisual(display,
    DefaultScreen(display)), 32, XYPixmap, 0, (char*)image,
    WIDTH, HEIGHT, 32, 0);
    XInitImage(img);
    // Always gives an error at this!!!
    XPutImage (display, w, gc, img, 0, 0, 0, 0, WIDTH, HEIGHT);

    Does anyone have some example code which will blit an image to an X
    window???


  2. Re: Displaying an image


    "speedplane" wrote in message
    news:1146536459.836246.160210@g10g2000cwb.googlegr oups.com...
    > I've been having a horrible time trying to blit a image to the screen.
    > Maybe someone can help me out....
    >
    > #define WIDTH 800
    > #define HEIGHT 800
    > int image[WIDTH][HEIGHT];
    > // Fill in the image with data here
    >
    > w = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0,
    > WIDTH, HEIGHT, 0, blackColor, blackColor);
    > XMapWindow(display, w);
    > gc = XCreateGC(display, w, 0, NULL);
    > XSync(display,False);
    > img = XCreateImage(display, DefaultVisual(display,
    > DefaultScreen(display)), 32, XYPixmap, 0, (char*)image,
    > WIDTH, HEIGHT, 32, 0);


    Bad things are going to happen above. You are casting an int[][] to a char*,
    which is clearly incorrect.

    > XInitImage(img);
    > // Always gives an error at this!!!


    You neglected to tell us what the error message says

    > XPutImage (display, w, gc, img, 0, 0, 0, 0, WIDTH, HEIGHT);
    >
    > Does anyone have some example code which will blit an image to an X
    > window???
    >


    A better way:
    XImage *image;
    unsigned int width = WIDTH;
    unsigned int height = HEIGHT;
    /* Create and map the window */

    /* Then copy its content into an XImage */
    image = XGetImage( display, w, 0, 0, width, height, AllPlanes, XYPixmap );

    /* fill the image here, using XPutPixel() */

    /* then blit the image back to the screen */
    XPutImage( display, w, gc, xi, 0, 0, 0, 0, width, height );

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project





  3. Re: Displaying an image

    On Tue, 02 May 2006 14:27:13 +0000, Fred Kleinschmidt wrote:

    >
    > "speedplane" wrote in message
    > news:1146536459.836246.160210@g10g2000cwb.googlegr oups.com...
    >> I've been having a horrible time trying to blit a image to the screen.
    >> Maybe someone can help me out....
    >>
    >> #define WIDTH 800
    >> #define HEIGHT 800
    >> int image[WIDTH][HEIGHT];
    >> // Fill in the image with data here
    >>
    >> w = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0,
    >> WIDTH, HEIGHT, 0, blackColor, blackColor);
    >> XMapWindow(display, w);
    >> gc = XCreateGC(display, w, 0, NULL);
    >> XSync(display,False);
    >> img = XCreateImage(display, DefaultVisual(display,
    >> DefaultScreen(display)), 32, XYPixmap, 0, (char*)image,
    >> WIDTH, HEIGHT, 32, 0);

    >
    > Bad things are going to happen above. You are casting an int[][] to a char*,
    > which is clearly incorrect.


    Not necessarily in this case. The declaration int image[WIDTH][HEIGHT]
    will generate a contiguous region of memory, unlike other languages such
    as Java.

    >
    >> XInitImage(img);
    >> // Always gives an error at this!!!

    >
    > You neglected to tell us what the error message says


    XInitImage() is an unnecessary call here; he's already created a
    dynamic XImage using XCreateImage(). XInitImage() is properly used to
    create an image from a blank, uninitialized block of memory (of
    sizeof(XImage)). Something along the lines of

    XImage tmp; XInitImage(&tmp);

    and not needed after XCreateImage().

    >
    >> XPutImage (display, w, gc, img, 0, 0, 0, 0, WIDTH, HEIGHT);
    >>
    >> Does anyone have some example code which will blit an image to an X
    >> window???
    >>

    >
    > A better way:
    > XImage *image;
    > unsigned int width = WIDTH;
    > unsigned int height = HEIGHT;
    > /* Create and map the window */
    >
    > /* Then copy its content into an XImage */
    > image = XGetImage( display, w, 0, 0, width, height, AllPlanes, XYPixmap );
    >
    > /* fill the image here, using XPutPixel() */
    >
    > /* then blit the image back to the screen */
    > XPutImage( display, w, gc, xi, 0, 0, 0, 0, width, height );


    Slow but reasonably portable. Its main problem is the discarding of the
    image after fetching what is essentially a screengrab.

    --
    #191, ewill3@earthlink.net
    It's still legal to go .sigless.


+ Reply to Thread