[comp.windows.x] XGetImage

A_LEAO%INESCN.RCCN.PT@MITVMA.MIT.EDU (Ana Leao) (12/29/89)

   I am developing a graphical editor that must display a bitmap file
received from a scanner and, after an editing session, store it back
in a file.
   I am having trouble using XPutImage() and XGetImage(), as those
functions sometimes cause an I/O error (broken pipe) that kills my
program. The crashes seem to occur randomly. The size of my images
is about 100K, but I also got crashes with 15K images.
   I work on a Sun3/50, diskless, running X11R3.
   From the Unix Network Communications Interface (sockets) I used
both the AF_INET family (TCP/IP) and the AF_UNIX family.
   I enclose a minimal program that exemplifies my problem. It
contains a loop that calls XGetImage() a thousand times, in order
to make a crash highly probable. However, sometimes those thousand
times aren't enough to cause one.
   I would appreciate any suggestion.
/*-------------------------------------------------------*/
/* Author: Ana Maria de Sa' Carneiro Leao, Inesc Norte
 * Filename: test.c
 * File Version: 1.00
 * File History: 19/12/89
 */

#include <stdio.h>
#include <X11/Xlib.h>

main(argc, argv)
int argc;
char **argv;
{
  Display *display;
  int screen;
  Window canvas;
  Pixmap bitmap;
  XImage *image;
  FILE *out;
  char *filename = "image";
  int format = XYPixmap, i, size;
  unsigned long planes = AllPlanes;
  unsigned int width = 1500, height = 1500,
               depth = 1, buffer_size;

  if ((display = XOpenDisplay(0)) == (Display *)0) {
    printf("Cannot open display: %s\n", getenv("DISPLAY"));
    exit(-1);
    }
  screen = DefaultScreen(display);
  canvas = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0,
             width, height, 0,
             WhitePixel(display, screen),
             BlackPixel(display, screen));
  XMapWindow(display, canvas);
  bitmap = XCreatePixmap(display, canvas, width, height, depth);
  for(i = 0; i < 1000 ; i++){
    if(!(image = XGetImage (display, bitmap,
                0, 0, width, height, planes, format))){
      printf ("Unable to get image.");
      exit(1);
      }
    XDestroyImage(image);
    if (!(i % 10)){
      printf("%d\t", i);
      fflush(stdout);
      }
    }
  if(!(image = XGetImage (display, bitmap,
                0, 0, width, height, planes, format))){
    printf ("Unable to get image.");
    exit(1);
    }
  if ((out = fopen(filename, "w")) == NULL){
    XDestroyImage(image);
    printf("Can't open output file.\n");
    exit(1);
    }
  buffer_size = image->bytes_per_line * image->height * image->depth;
  if((size = fwrite(image->data, 1, (int)buffer_size, out)) != (int)buffer_size)
    printf("Error writing image.\n");
  XDestroyImage(image);
  fclose(out);
}