[comp.windows.x] XGetImage, BadMatch

hajadi@goofy.csd.sgi.com (Ivan Hajadi) (04/17/91)

I copied the reflect_window program from Vol 1 O`Reilly book, and
tried to run it.  I keep getting BadMatch error.  Anyone knows
what's wrong with this program ?

Configuration : Sun3/60, 4.1.1, X11R4.

Thanks.

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  73 (X_GetImage)
  Minor opcode of failed request:  0
  Resource id in failed request:  0x600003
  Serial number of failed request:  10
  Current serial number in output stream:  10


-------- code --------------
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>

Display *disp;
int screen;

XPoint points[] =
{
  {10,10},
  {400,10},
  {400,400}
};

main()
{
        Window win1, win2;
        XSetWindowAttributes attr;
        unsigned long attrmask;

        unsigned long gcmask;
        XGCValues gcvalues;
        GC gc;

        if (!(disp = XOpenDisplay(NULL))) {
                printf("Can't open display.\n");
                exit(-1);
        }
        screen = DefaultScreen(disp);

        attrmask = CWBackPixel | CWBorderPixel;
        attr.background_pixel = WhitePixel(disp, screen);
        attr.border_pixel     = BlackPixel(disp, screen);

        win1 = XCreateWindow(disp,
                RootWindow(disp, screen),       /* parent window */
                100,100,                        /* x,y location  */
                500,500,                        /* width, height */
                3,                              /* border width  */
                CopyFromParent,                 /* window depth  */
                InputOutput,                    /* window class  */
                CopyFromParent,                 /* visual type   */
                attrmask,                       /* attribute mask */
                &attr                           /* window attributes */
                );

        win2 = XCreateWindow(disp,
                RootWindow(disp, screen),       /* parent window */
                100,100,                        /* x,y location  */
                500,500,                        /* width, height */
                3,                              /* border width  */
                CopyFromParent,                 /* window depth  */
                InputOutput,                    /* window class  */
                CopyFromParent,                 /* visual type   */
                attrmask,                       /* attribute mask */
                &attr                           /* window attributes */
                );

        gcmask = GCBackground | GCForeground;
        gcvalues.foreground = BlackPixel(disp, screen);
        gcvalues.background = WhitePixel(disp, screen);

        gc = XCreateGC(disp, win1, gcmask, &gcvalues);

        XMapWindow(disp, win1);
        XMapWindow(disp, win2);
        XFillPolygon(disp, win1, gc, points, 3, Convex, CoordModeOrigin);
        XFlush(disp);

        reflect_window(win1, win2, gc, 500, 500);


        XFlush(disp);

        printf("\nHit Enter to quit.");
        getchar();                              /* hit return to exit */

}


int reflect_window(window, newwindow, gc, w,h)
Window window;
Window newwindow;
GC gc;
unsigned int w;
unsigned int h;
{
        XImage *xi;
        unsigned long pixval1, pixval2;
        int y;
        int left_x, right_x;

        xi = XGetImage(disp, window, 0,0, w,h, AllPlanes, XYPixmap);
        printf("calculating....");

        for (left_x=0; left_x<w/2; left_x++) {
                for (y=0; y<h; y++) {
                        pixval1 = XGetPixel(xi, left_x, y);
                        right_x = w - left_x;
                        if (left_x != right_x) {
                                pixval2 = XGetPixel(xi, right_x, y);
                                XPutPixel(xi, left_x, y, pixval2);
                        }
                        XPutPixel(xi, right_x, y, pixval1);
                }
        }

        printf("putting image....");
        XPutImage(disp, newwindow, gc, xi, 0,0,0,0, w,h);
}

--

    Ivan M. Hajadi,                      Widget - any small, unspecified
    hajadi@csd.sgi.com                            gadget or device (Webster)

klee@wsl.dec.com (Ken Lee) (04/18/91)

In article <1991Apr17.022705.14758@odin.corp.sgi.com>, hajadi@goofy.csd.sgi.com (Ivan Hajadi) writes:
|> I copied the reflect_window program from Vol 1 O`Reilly book, and
|> tried to run it.  I keep getting BadMatch error.  Anyone knows
|> what's wrong with this program ?

This code commits the most basic error of X programming.  It creates
and maps a window then tries to use it (XGetImage) without waiting for
an expose or map event.  Since mapping is done by the window manager,
it is sometimes delayed.  The Xlib manual says you will get a BadMatch
if you do an XGetImage on an unmapped window, which is probably your problem.

-- 
Ken Lee
DEC Western Software Laboratory, Palo Alto, Calif.
Internet: klee@wsl.dec.com
uucp: uunet!decwrl!klee

bni@modulex.dk (Bent Nielsen) (04/18/91)

hajadi@goofy.csd.sgi.com (Ivan Hajadi) writes:
>I copied the reflect_window program from Vol 1 O`Reilly book, and
>tried to run it.  I keep getting BadMatch error.  Anyone knows
>what's wrong with this program ?

According to the manual you will get BadMatch when the window isn't fully
visible. I think you win2 is overlapping win1.

>-------- code --------------
stuff deleted

--
Bent Nielsen		<bni@modulex.dk>
A/S MODULEX		Phone:    +45 44 53 30 11
Lyskaer 15		Telefax:  +45 44 53 30 74
DK-2730 Herlev
Denmark

mouse@lightning.mcrcim.mcgill.EDU (der Mouse) (05/02/91)

> i also seem to get BadMatch errors if part of the image i'm trying to
> get is off the screen.  works fine if all of it is on screen.

> u might have to check for the window's width and height then.

The Xlib document says:

	If the drawable is a pixmap, the given rectangle must be wholly
	contained within the pixmap, or a BadMatch error results.  If
	the drawable is a window, the window must be viewable, and it
	must be the case that if there were no inferiors or overlapping
	windows, the specified rectangle of the window would be fully
	visible on the screen and wholly contained within the outside
	edges of the window, or a BadMatch error results.

There's more, but that's all that talks about BadMatch errors.  The
protocol document contains similar language.

So yes, if the window is partly off-screen, you can't XGetImage the
off-screen portion, and this is not a bug.  Note that you can use
XGetImage to fetch portions that are on-screen but behind other
windows, but unless the server is maintaining backing-store for the
window, the bits you get are undefined and typically will be those of
the obscuring window.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu