Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XGetImage question

11 views
Skip to first unread message

David Lin

unread,
Sep 2, 1995, 3:00:00 AM9/2/95
to
Hi X-programmers,

I have some problem with the following sequence of function calls.
Most of the times, it'll work but occasionally, I get BadMatch error.

Display *dpy = XtDisplay(_widget);
Window win = XtWindow(_widget);
XtVaGetValues(_widget, XmNwidth, &width, XmNheight, &height, NULL);
_ximage = XGetImage(dpy, win, 0,0, width, height, AllPlanes, ZPixmap);


I get the following error:
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 73 (X_GetImage)
Serial number of failed request: 1569
Current serial number in output stream: 1569

Am I doing anything wrong? The problem is that the error does not occur
all the time, so I'm kinda stumped on the cause of the problem.

Thanks in advance for any help or suggestion.
Dave

--
-------------------------------------------------------------------------------
David C Lin dl...@cs.stanford.edu http://www-cs-students.stanford.edu/~dlin
-------------------------------------------------------------------------------

Markku Savela

unread,
Sep 2, 1995, 3:00:00 AM9/2/95
to
In article <428b6q$h...@Radon.Stanford.EDU> dl...@eye.Stanford.EDU (David Lin) writes:

> _ximage = XGetImage(dpy, win, 0,0, width, height, AllPlanes, ZPixmap);


>I get the following error:
>X Error of failed request: BadMatch (invalid parameter attributes)

Perhaps a part of the window is outside the screen? A couple of months
back I tried to get XImage from random Xt Widget and found that the
problem is not necessarily simple one. In the end I got something that
worked and posted the results to the newsgroup.

Perhaps the attached code gives some ideas about the complexities (no
guarantees on code quality, this snipped was just a test code I used,
it is not a "product" :-). In your case you would just do

_ximage = GetWidetImage(_widget);

or if you want a PBM file out, you might use

fp = open("image.pbm", "wb");
WriteWidgetPBM(_widget, fp);
fclose(fp);


------------------------------------- snip ---------------------------
/*
** getimage.c
**
** Contains a function which attempts to retrieve an XImage of a
** widget window. -- Markku Savela
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/IntrinsicP.h>

static void WaitGraphics(w, client_data, event, continue_to_dispatch)
Widget w;
XtPointer client_data;
XEvent *event;
Boolean *continue_to_dispatch;
{
static XExposeEvent init_expose;

Drawable *drawable = (Drawable *)client_data;
Window window;
XExposeEvent expose;
Region region;
XRectangle rect;

if (event->type==NoExpose && event->xnoexpose.drawable == *drawable)
{
*drawable = 0;
*continue_to_dispatch = False;
return;
}
else if (event->type != GraphicsExpose ||
event->xgraphicsexpose.drawable != *drawable)
return;
/*
** Getting Graphics Expose event. Do the "dirty" and fake
** an expose event (this may break something...)
*/
window = w->core.window;
expose = init_expose;
expose.type = Expose;
expose.serial = event->xgraphicsexpose.serial;
expose.send_event = event->xgraphicsexpose.send_event;
expose.display = event->xgraphicsexpose.display;
expose.window = *drawable;
rect.x = expose.x = event->xgraphicsexpose.x;
rect.y = expose.y = event->xgraphicsexpose.y;
rect.width = expose.width = event->xgraphicsexpose.width;
rect.height = expose.height = event->xgraphicsexpose.height;
expose.count = event->xgraphicsexpose.count;
w->core.window = *drawable;
region = XCreateRegion();
XUnionRectWithRegion(&rect, region, region);
if (w->core.widget_class->core_class.expose)
(*w->core.widget_class->core_class.expose)
(w, (XEvent *)&expose, region);
XDestroyRegion(region);
w->core.window = window;
/*
*/
if (event->xgraphicsexpose.count == 0)
*drawable = 0;
*continue_to_dispatch = False;
}

/*
** GetWidgetImage
*/
XImage *GetWidgetImage(w)
Widget w;
{
XImage *image;
XtAppContext app = XtWidgetToApplicationContext(w);
Display *display = XtDisplay(w);
Window window = XtWindow(w);
Pixmap pixmap;
GC gc;
int width = w->core.width;
int height = w->core.height;
Pixmap waiting;

if (!window)
return NULL;
pixmap = XCreatePixmap(display, window, width, height, w->core.depth);
waiting = pixmap;
gc = XCreateGC(display, window, 0, 0);
XFillRectangle(display, pixmap, gc, 0, 0, width, height);
XtRegisterDrawable(display, pixmap, w);
XtInsertEventHandler(w,NoEventMask, True, WaitGraphics,
(XtPointer)&waiting, XtListHead);
XCopyArea(display, window, pixmap, gc, 0, 0, width, height, 0, 0);
while (waiting)
XtAppProcessEvent(app, XtIMAll);
XtUnregisterDrawable(display, pixmap);
XtRemoveEventHandler
(w, NoEventMask,True, WaitGraphics, (XtPointer)&waiting);
XFreeGC(display, gc);
image = XGetImage
(display, pixmap, 0,0, width, height, AllPlanes, ZPixmap);
XFreePixmap(display, pixmap);
return image;
}


static int highbit(ul)
unsigned long ul;
{
int i;

for (i=31; ((ul&0x80000000) == 0) && i>=0; i--, ul<<=1);

return i;
}

/*
** WriteWidgetPBM
*/
int WriteWidgetPBM(w, fp)
Widget w;
FILE *fp;
{
XWindowAttributes attr;
Display *display = XtDisplay(w);
Window window = XtWindow(w);
XImage *image = GetWidgetImage(w);
XColor *colors;
unsigned long pixel;
int x, y, i;
unsigned long rm, gm, bm;
int rs, gs, bs;

if (!image)
return 0;

XGetWindowAttributes(display, window, &attr);
switch (attr.visual->class)
{
case StaticColor:
case StaticGray:
case GrayScale:
case PseudoColor:
fprintf(fp, "P6\n%d %d\n255\n", image->width, image->height);
colors = (XColor *)XtMalloc
(attr.visual->map_entries * sizeof(XColor));
for (i = 0; i < attr.visual->map_entries; ++i)
colors[i].pixel = i;
XQueryColors(display, attr.colormap,
colors, attr.visual->map_entries);
for (i = 0; i < attr.visual->map_entries; ++i)
{
colors[i].red = colors[i].red / 257;
colors[i].green = colors[i].green / 257;
colors[i].blue = colors[i].blue / 257;
}
for (y = 0; y < image->height; ++y)
for (x = 0; x < image->width; ++x)
{
pixel = XGetPixel(image, x, y);
fputc(colors[pixel].red, fp);
fputc(colors[pixel].green, fp);
fputc(colors[pixel].blue, fp);
}
XtFree((char *)colors);
break;
case DirectColor:
case TrueColor:
fprintf(fp, "P6\n%d %d\n255\n", image->width, image->height);

rm = attr.visual->red_mask;
gm = attr.visual->green_mask;
bm = attr.visual->blue_mask;

rs = highbit(rm) - 7;
gs = highbit(gm) - 7;
bs = highbit(bm) - 7;
rm = rs > 0 ? (rm >> rs) : (rm << -rs);
gm = gs > 0 ? (gm >> gs) : (gm << -gs);
bm = bs > 0 ? (bm >> bs) : (bm << -bs);

for (y = 0; y < image->height; ++y)
for (x = 0; x < image->width; ++x)
{
int r, g, b;

pixel = XGetPixel(image, x, y);

r = rs > 0 ? pixel >> rs : pixel << -rs;
g = gs > 0 ? pixel >> gs : pixel << -gs;
b = bs > 0 ? pixel >> bs : pixel << -bs;

fputc(r & rm, fp);
fputc(g & gm, fp);
fputc(b & bm, fp);
}
break;
}
XDestroyImage(image);
return 1;
}

--
Markku Savela (m...@hemuli.tte.vtt.fi), Technical Research Centre of Finland
Multimedia Systems, P.O.Box 1203,FIN-02044 VTT,http://www.vtt.fi/tte/staff/msa/

0 new messages