Beispiel #1
1
void* icvVideoRender(void* data)
{
    int cameraid = (int)data;
    CvVideoCamera *const camera = &(cameras[cameraid]);
    Display* display;
    int screen_num;
    GC gc;
    char* display_name = NULL;
    Window window = camera->window;
    XWindowAttributes windowattr;
    Visual* visual;
    int windowdepth;
    XImage* image;
    XShmSegmentInfo xshmseg;
    int width  = (camera->renderwidth>0)?camera->renderwidth:camera->videopp.width;
    int height = camera->renderheight?camera->renderheight:camera->videopp.height;
    int picturedepth = camera->videopp.picture.depth;
    int pixelsize;
    XGCValues values;
    IplImage* iplimage;
    time_t start, now;
    int frames = 0;
    float rate = 0;
    Status XShm;
    uchar* imgdata = NULL;
    uchar* tmpbuff = NULL;

    pthread_mutex_lock(&(camera->capturestatemutex));
    if(camera->capturestate != CAPTURING)
    {
        pthread_mutex_unlock(&(camera->capturestatemutex));
        pthread_exit( NULL );
    }
    camera->renderstate=1;
    pthread_cond_signal(&(camera->capturestatecond));
    pthread_mutex_unlock(&(camera->capturestatemutex));
    XInitThreads();

    if ( (display=XOpenDisplay(display_name)) == NULL )

    {
        fprintf( stderr, "cvVideo: cannot connect to X server %s\n",
            XDisplayName(display_name));
        pthread_exit( NULL );
    }

    screen_num = DefaultScreen(display);

    if (XGetWindowAttributes(display, window,
        &windowattr) == 0)
    {
        fprintf(stderr, "icvVideoRender: failed to get window attributes.\n" );
        pthread_exit(NULL);
    }

    if(windowattr.map_state == IsUnmapped)
    {
        fprintf(stderr, "icvVideoRender: window is not mapped \n" );
        pthread_exit(NULL);
    }

    windowdepth = windowattr.depth;
    visual      = windowattr.visual;

    pixelsize = icvVideoWindowPixelsize(windowdepth);

    XShm = XShmQueryExtension(display);
    if(XShm)
    {
        image = XShmCreateImage(display, visual, windowdepth, ZPixmap, NULL,
            &xshmseg, width, height );

        assert(image);

        xshmseg.shmid = shmget (IPC_PRIVATE,
            width*height*pixelsize, IPC_CREAT|0777);

        assert(xshmseg.shmid != -1);
        xshmseg.shmaddr = image->data=(char*)shmat (xshmseg.shmid, 0, 0) ;

        xshmseg.readOnly = False;

        XShmAttach (display, &xshmseg);
    }
    else
    {
        imgdata = (uchar*)malloc(width*height*icvVideoWindowPixelsize(windowdepth)) ;
        image = XCreateImage(display, visual, windowdepth, ZPixmap, 0,
            (char*)imgdata, width,
            height, 8,
            icvVideoWindowPixelsize(windowdepth)
            *width);

        assert(image);
        XInitImage(image);
    }

    gc = XCreateGC(display,window,0, &values );
#ifdef RENDER_FRAMERATE
    start = time(NULL);
#endif

    pthread_mutex_lock(&(camera->capturestatemutex));
    while((camera->capturestate == CAPTURING) && (camera->rendered))
    {
        pthread_mutex_unlock(&(camera->capturestatemutex));
        pthread_mutex_lock(&(camera->updatedmutex));
        while(camera->updated == 0)
            pthread_cond_wait(&(camera->updatedcond), &(camera->updatedmutex));
        camera->updated = 0;
        pthread_mutex_unlock(&(camera->updatedmutex));
        if(cvcamGetProperty(cameraid, "raw_image",&iplimage ))
        {
            assert(image->data);
            if(camera->callback != NULL)
                camera->callback(iplimage);
            if((width==camera->videopp.width)&&
               (height==camera->videopp.height))
            {
                icvvConvert(width, height, width*picturedepth/8, picturedepth,
                            iplimage->imageData, width*pixelsize, pixelsize*8, image->data
                    );
                cvReleaseImage(&iplimage);
            }
            else
            {
                tmpbuff = (uchar*)malloc(camera->videopp.width*camera->videopp.height*
                                         pixelsize) ;
                
                icvvConvert(camera->videopp.width, camera->videopp.height,
                            camera->videopp.width*picturedepth/8, picturedepth,
                            iplimage->imageData, camera->videopp.width*pixelsize,
                            pixelsize*8, (char*)tmpbuff);
                cvReleaseImage(&iplimage);
                
                icvvResizeImage(camera->videopp.width,
                                camera->videopp.height,
                                (camera->videopp.width)*pixelsize, (char*)tmpbuff,
                                width, height,width*pixelsize, image->data, pixelsize*8);
                
                free(tmpbuff);
                
            }
            
            //fprintf(stdout, "cvVideoRendering:image converted!!!!\n");
            
            if(XShm)
            {
                XShmPutImage(display, window, gc,
                             image,0,0,0,0, width,
                             height, False);
            }
            else
            {
                XPutImage(display, window, gc,
                          image,0,0,0,0, width,
                          height);
            }
            
            XSync(display, False);
#ifdef RENDER_FRAMERATE            
            now = time(NULL);
            frames++;
            if (now-start)
                rate = frames/(float)(now-start);
            if((frames%30) == 0)
                fprintf(stdout, "camera %d fps = %f\n", cameraid, rate);
#endif
        }//if(cvcamGetProperty(CAMERA, "raw_image",&image ))

        // stop here if we're paused
        pthread_mutex_lock(&(camera->pausemutex));
        pthread_mutex_unlock(&(camera->pausemutex));
        pthread_mutex_lock(&(camera->capturestatemutex));
    }//while (camera->state == CAPTURING && camera->rendered)
    pthread_mutex_unlock(&(camera->capturestatemutex));

    pthread_mutex_lock(&(camera->capturestatemutex));
#if 0
    if(camera->state != CAPTURING) {
        // we ended because the camera is not capturing anymore
        while (camera->capturestate != FINISHED )
        {
            pthread_cond_wait(&(camera->capturestatecond),&(camera->capturestatemutex));
        }
    }
#endif
    camera->renderstate=0;
    pthread_cond_signal(&(camera->capturestatecond));
    pthread_mutex_unlock(&(camera->capturestatemutex));

    XShmDetach (display, &xshmseg);
    XDestroyImage (image);
    XFreeGC(display,gc);
    shmdt (xshmseg.shmaddr);
    shmctl (xshmseg.shmid, IPC_RMID, 0);
    if(imgdata)
        free(imgdata);  
    pthread_exit(NULL);
}
Beispiel #2
0
Picture PreviewCursor::createPicture(const XcursorImage *image) const
{
    Display *dpy = QPaintDevice::x11AppDisplay();

    XImage ximage;
    ximage.width = image->width;
    ximage.height = image->height;
    ximage.xoffset = 0;
    ximage.format = ZPixmap;
    ximage.data = reinterpret_cast< char * >(image->pixels);
    ximage.byte_order = ImageByteOrder(dpy);
    ximage.bitmap_unit = 32;
    ximage.bitmap_bit_order = ximage.byte_order;
    ximage.bitmap_pad = 32;
    ximage.depth = 32;
    ximage.bits_per_pixel = 32;
    ximage.bytes_per_line = image->width * 4;
    ximage.red_mask = 0x00ff0000;
    ximage.green_mask = 0x0000ff00;
    ximage.blue_mask = 0x000000ff;
    ximage.obdata = 0;

    XInitImage(&ximage);

    Pixmap pix = XCreatePixmap(dpy, DefaultRootWindow(dpy), ximage.width, ximage.height, 32);
    GC gc = XCreateGC(dpy, pix, 0, NULL);
    XPutImage(dpy, pix, gc, &ximage, 0, 0, 0, 0, ximage.width, ximage.height);
    XFreeGC(dpy, gc);

    XRenderPictFormat *fmt = XRenderFindStandardFormat(dpy, PictStandardARGB32);
    Picture pict = XRenderCreatePicture(dpy, pix, fmt, 0, NULL);
    XFreePixmap(dpy, pix);

    return pict;
}
Beispiel #3
0
void xf_Glyph_New(rdpContext* context, rdpGlyph* glyph)
{
	int scanline;
	XImage* image;
	xfGlyph* xf_glyph;

	xf_glyph = (xfGlyph*) glyph;
	xfContext* xfc = (xfContext*) context;

	xf_lock_x11(xfc, FALSE);

	scanline = (glyph->cx + 7) / 8;

	xf_glyph->pixmap = XCreatePixmap(xfc->display, xfc->drawing, glyph->cx, glyph->cy, 1);

	image = XCreateImage(xfc->display, xfc->visual, 1,
			ZPixmap, 0, (char*) glyph->aj, glyph->cx, glyph->cy, 8, scanline);

	image->byte_order = MSBFirst;
	image->bitmap_bit_order = MSBFirst;

	XInitImage(image);
	XPutImage(xfc->display, xf_glyph->pixmap, xfc->gc_mono, image, 0, 0, 0, 0, glyph->cx, glyph->cy);
	XFree(image);

	xf_unlock_x11(xfc, FALSE);
}
static Pixmap
xCreateBitmapFromBitCell(Display *display, Drawable d, char *data,
		unsigned int width, unsigned int height)
{
	XImage ximage;
	GC gc;
	Pixmap pix;

	pix = XCreatePixmap(display, d, width, height, 1);
	if (!pix)
	  return(0);
	gc = XCreateGC(display, pix, (unsigned long)0, (XGCValues *)0);
	memset(&ximage, 0, sizeof(ximage));
	ximage.height = height;
	ximage.width = width;
	ximage.depth = 1;
	ximage.xoffset = 0;
	ximage.format = ZPixmap;
	ximage.data = data;
	ximage.byte_order = MSBFirst;
	ximage.bitmap_unit = 8;
	ximage.bitmap_bit_order = MSBFirst;
	ximage.bitmap_pad = 8;
	ximage.bytes_per_line = (width+7)/8;
	XInitImage(&ximage);

	XPutImage(display, pix, gc, &ximage, 0, 0, 0, 0, width, height);
	XFreeGC(display, gc);
	return(pix);
}
Beispiel #5
0
void rf_Glyph_New(rdpContext* context, rdpGlyph* glyph)
{
#ifdef RF_GLYPH
	int scanline;
	XImage* image;
	rfContext* rfi;
	rfGlyph* rf_glyph;

	rf_glyph = (rfGlyph*) glyph;
	rfi = (rfContext*) context;

	scanline = (glyph->cx + 7) / 8;

	rf_glyph->pixmap = XCreatePixmap(rfi->display, rfi->drawing, glyph->cx, glyph->cy, 1);

	image = XCreateImage(rfi->display, rfi->visual, 1,
			ZPixmap, 0, (char*) glyph->aj, glyph->cx, glyph->cy, 8, scanline);

	image->byte_order = MSBFirst;
	image->bitmap_bit_order = MSBFirst;

	XInitImage(image);
	XPutImage(rfi->display, rf_glyph->pixmap, rfi->gc_mono, image, 0, 0, 0, 0, glyph->cx, glyph->cy);
	XFree(image);
#endif
}
Beispiel #6
0
void SetSurface(SystemDraw& w, int x, int y, int cx, int cy, const RGBA *pixels)
{
	GuiLock __;
	Pixmap pixmap = XCreatePixmap(Xdisplay, Xroot, cx, cy, 24);
	XPicture picture = XRenderCreatePicture(
		Xdisplay, pixmap,
	    XRenderFindStandardFormat(Xdisplay, PictStandardRGB24),
	    0, 0
	);
	XImage ximg;
	sInitXImage(ximg, Size(cx, cy));
	ximg.bitmap_pad = 32;
	ximg.bytes_per_line = 4 * cx;
	ximg.bits_per_pixel = 32;
	ximg.blue_mask = 0x00ff0000;
	ximg.green_mask = 0x0000ff00;
	ximg.red_mask = 0x000000ff;
	ximg.bitmap_unit = 32;
	ximg.depth = 24;
	ximg.data = (char *)pixels;
	XInitImage(&ximg);
	GC gc = XCreateGC(Xdisplay, pixmap, 0, 0);
	XPutImage(Xdisplay, pixmap, gc, &ximg, 0, 0, 0, 0, cx, cy);
	XFreeGC(Xdisplay, gc);
	XFreePixmap(Xdisplay, pixmap);
	XRenderComposite(Xdisplay, PictOpOver,
	                 picture, 0, XftDrawPicture(w.GetXftDraw()),
	                 0, 0, 0, 0, x, y, cx, cy);
	XRenderFreePicture(Xdisplay, picture);
}
Beispiel #7
0
ibool 	MGLAPI XWIN8_initDriver(void *data,MGLDC *dc,int driverId,int modeId,ulong hwnd,
								 int virtualX,int virtualY,int numBuffers,ibool stereo,int refreshRate)
/****************************************************************************
*
* Function:		WDCI8_initDriver
* Parameters:	dc	- Device context.
* Returns:		True if the device was correctly initialised.
*
* Description:	Initialises the device driver, and starts the specified
*				graphics mode. This is also where we fill in all of the
*				vectors in the device context to initialise our device
*				context properly.
*
****************************************************************************/
{
	Screen *scr;
	Window wnd;
	char *buf;
	XSetWindowAttributes xswa;

	dc->mi = XWIN8_modes[modeId - grSVGA_640x480x256];
	dc->deviceType = MGL_WINDOWED_DEVICE;

	g_state.d.hardwareCursor = false;

	dc->v = &g_state;
	dc->wm.xwindc.dpy = globalDisplay;
	dc->wm.xwindc.scr = scr = DefaultScreenOfDisplay(globalDisplay);
	dc->wm.xwindc.gc  = DefaultGCOfScreen(scr);

	xswa.background_pixel = BlackPixel(globalDisplay,XScreenNumberOfScreen(scr));
	xswa.backing_store = Always;

	dc->wm.xwindc.wnd = wnd = XCreateWindow(globalDisplay, RootWindowOfScreen(scr), 0,0, dc->mi.xRes+1, dc->mi.yRes+1,
											0, CopyFromParent, CopyFromParent, CopyFromParent,
											CWBackPixel | CWBackingStore, &xswa
											);
	buf = MGL_malloc((dc->mi.xRes+1) * (dc->mi.yRes+1));
	dc->wm.xwindc.img = XCreateImage(globalDisplay, DefaultVisualOfScreen(scr), 
									 8, ZPixmap, 0, NULL, dc->mi.xRes+1, dc->mi.yRes+1, 8, 0);
	dc->wm.xwindc.img->data = buf;
	XInitImage(dc->wm.xwindc.img);

	// Sets up the private colormap
	dc->wm.xwindc.hpal = xswa.colormap = XCreateColormap(globalDisplay, wnd, DefaultVisualOfScreen(scr), AllocAll);
	XChangeWindowAttributes(globalDisplay, wnd, CWColormap, &xswa);

	XMapRaised(globalDisplay, wnd);
	XClearWindow(globalDisplay, wnd);
	XStoreName(globalDisplay, wnd, "MGL [Unix/X11 8 bit Display]");

	XWIN_initInternal(dc);
	dc->r.realizePalette = XWIN8_realizePalette;
	dc->r.getDefaultPalette = XWIN8_getDefaultPalette;
	dc->r.putImage = XWIN8_putImage;
	return true;
}
Beispiel #8
0
/* Apply the cursor to the Chromium OS X11 server.
 * Adapted from the XcursorImageLoadCursor implementation in libXcursor,
 * copyright 2002 Keith Packard.
 */
static void apply_cursor(Display* d, Window w, XFixesCursorImage *image) {
    static Cursor cur_cursor = 0;
    XImage ximage;
    Pixmap pixmap;
    Picture picture;
    GC gc;
    XRenderPictFormat *format;
    Cursor cursor;

    /* Unset the current cursor if no image is passed. */
    if (!image) {
        if (cur_cursor) {
            XUndefineCursor(d, w);
            XFreeCursor(d, cur_cursor);
            cur_cursor = 0;
        }
        return;
    }

    ximage.width = image->width;
    ximage.height = image->height;
    ximage.xoffset = 0;
    ximage.format = ZPixmap;
    ximage.data = (char *) image->pixels;
    ximage.byte_order = LSBFirst;
    ximage.bitmap_unit = 32;
    ximage.bitmap_bit_order = ximage.byte_order;
    ximage.bitmap_pad = 32;
    ximage.depth = 32;
    ximage.bits_per_pixel = 32;
    ximage.bytes_per_line = image->width * 4;
    ximage.red_mask = 0xff0000;
    ximage.green_mask = 0x00ff00;
    ximage.blue_mask = 0x0000ff;
    ximage.obdata = 0;
    if (!XInitImage(&ximage)) {
        puts("failed to init image");
        return;
    }
    pixmap = XCreatePixmap(d, w, image->width, image->height, 32);
    gc = XCreateGC(d, pixmap, 0, 0);
    XPutImage(d, pixmap, gc, &ximage, 0, 0, 0, 0, image->width, image->height);
    XFreeGC(d, gc);
    format = XRenderFindStandardFormat(d, PictStandardARGB32);
    picture = XRenderCreatePicture(d, pixmap, format, 0, 0);
    XFreePixmap(d, pixmap);
    cursor = XRenderCreateCursor(d, picture, image->xhot, image->yhot);
    XRenderFreePicture(d, picture);
    XDefineCursor(d, w, cursor);
    XFlush(d);
    if (cur_cursor)
        XFreeCursor(d, cur_cursor);
    cur_cursor = cursor;
}
void SWRenderDisplayWindowProvider::draw_image(const Rect &dest, const PixelBuffer &image, const Rect &src)
{
	XImage ximage;
	memset(&ximage, 0, sizeof(ximage));

	ximage.width = image.get_width();
	ximage.height = image.get_height();
	ximage.format = ZPixmap;
	ximage.data = (char *) image.get_data();
	ximage.byte_order = LSBFirst;

	int image_pitch = image.get_pitch();
	int image_bpp = image.get_bytes_per_pixel();

	if (image_bpp == 1)
	{
		ximage.bitmap_unit = 8;
	}
	else if (image_bpp == 2)
	{
		ximage.bitmap_unit = 16;
	}
	else if (image_bpp == 3)
	{
		ximage.bitmap_unit = 24;
	}
	else	ximage.bitmap_unit = 32;

	ximage.bitmap_pad = ximage.bitmap_unit;

	ximage.bitmap_bit_order = LSBFirst;
	ximage.depth = 24;
	ximage.bytes_per_line = image_pitch;

	ximage.bits_per_pixel = image_bpp * 8;

	ximage.red_mask = 0x00ff0000;
	ximage.green_mask = 0x0000ff00;
	ximage.blue_mask = 0x000000ff;

	if (!XInitImage(&ximage))
	{
		throw Exception("Cannot initialise image");
	}

	GC xgc = XCreateGC(window.get_display(), window.get_window(), 0, NULL); 

	XPutImage(window.get_display(), window.get_window(), xgc, &ximage, src.left, src.top, dest.left, dest.top, src.get_width(), src.get_height());
	XFreeGC(window.get_display(), xgc);
}
Beispiel #10
0
int video_display(PLOT *plot)
{
	struct videodata *plotdata = (struct videodata *)plot->plotdata;
	int status = SUCCESS, tstatus;
	GROUP *group = plot->group;

	/*
	** Load the video data, but only if we need to...
	*/
	if (group->viddirty == 1)
	{
		if (load_viddata(&group->vidfp, group->loadedfilename, group->filename, group->entry, 
			&plotdata->nframes, &plotdata->width, &plotdata->height, &plotdata->ncomps,
			&plotdata->microsecs_per_frame,
			&group->ntime) == 0)
		{
			if (plotdata->framedata)
			{
				free(plotdata->framedata);
				plotdata->framedata = (char *)calloc(plotdata->height * plotdata->width, plotdata->ncomps);
			}
			group->viddirty = 0;
			plotdata->image->width = plotdata->width;
			plotdata->image->height = plotdata->height;
			plotdata->image->xoffset = 0;
			plotdata->image->format = ZPixmap; /* XYBitmap, XYPixmap, or ZPixmap */
			plotdata->image->data = NULL;
			plotdata->image->byte_order = LSBFirst;
			plotdata->image->bitmap_unit = 8;
			plotdata->image->bitmap_bit_order = LSBFirst;
			plotdata->image->bitmap_pad = 8;
			plotdata->image->depth = plotdata->ncomps;
			plotdata->image->bytes_per_line = plotdata->width * plotdata->ncomps;
			plotdata->image->bits_per_pixel = plotdata->ncomps * 8;
			plotdata->image->red_mask = 0;
			plotdata->image->green_mask = 0;
			plotdata->image->blue_mask = 0;
			XInitImage(plotdata->image);
			plotdata->image->data = plotdata->framedata;
		}
		else status = ERROR;
	}

	/*
	** Update the plot
	*/
	tstatus = video_set(plot);
	return (status == SUCCESS) ? (tstatus) : (status);
}
Beispiel #11
0
struct pipe_screen*
vl_screen_create(Display *display, int screen)
{
   struct xsp_pipe_winsys *xsp_winsys;

   assert(display);

   xsp_winsys = CALLOC_STRUCT(xsp_pipe_winsys);
   if (!xsp_winsys)
      return NULL;

   xsp_winsys->base.buffer_create = xsp_buffer_create;
   xsp_winsys->base.user_buffer_create = xsp_user_buffer_create;
   xsp_winsys->base.buffer_map = xsp_buffer_map;
   xsp_winsys->base.buffer_unmap = xsp_buffer_unmap;
   xsp_winsys->base.buffer_destroy = xsp_buffer_destroy;
   xsp_winsys->base.surface_buffer_create = xsp_surface_buffer_create;
   xsp_winsys->base.fence_reference = xsp_fence_reference;
   xsp_winsys->base.fence_signalled = xsp_fence_signalled;
   xsp_winsys->base.fence_finish = xsp_fence_finish;
   xsp_winsys->base.flush_frontbuffer = xsp_flush_frontbuffer;
   xsp_winsys->base.get_name = xsp_get_name;
   xsp_winsys->base.destroy = xsp_destroy;
   xsp_winsys->display = display;
   xsp_winsys->screen = screen;
   xsp_winsys->fbimage = XCreateImage
   (
      display,
      XDefaultVisual(display, screen),
      XDefaultDepth(display, screen),
      ZPixmap,
      0,
      NULL,
      0, /* Don't know the width and height until flush_frontbuffer */
      0,
      32,
      0
   );

   if (!xsp_winsys->fbimage) {
      FREE(xsp_winsys);
      return NULL;
   }

   XInitImage(xsp_winsys->fbimage);

   return softpipe_create_screen(&xsp_winsys->base);
}
Beispiel #12
0
Pixmap xf_glyph_new(xfInfo* xfi, int width, int height, uint8* data)
{
	int scanline;
	Pixmap bitmap;
	XImage* image;

	scanline = (width + 7) / 8;
	bitmap = XCreatePixmap(xfi->display, xfi->drawable, width, height, 1);
	image = XCreateImage(xfi->display, xfi->visual, 1,
			ZPixmap, 0, (char*) data, width, height, 8, scanline);
	image->byte_order = MSBFirst;
	image->bitmap_bit_order = MSBFirst;
	XInitImage(image);
	XPutImage(xfi->display, bitmap, xfi->gc_mono, image, 0, 0, 0, 0, width, height);
	XFree(image);
	return bitmap;
}
Beispiel #13
0
static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) {
    sk_bzero(&image, sizeof(image));

    int bitsPerPixel = bitmap.bytesPerPixel() * 8;
    image.width = bitmap.width();
    image.height = bitmap.height();
    image.format = ZPixmap;
    image.data = (char*) bitmap.getPixels();
    image.byte_order = LSBFirst;
    image.bitmap_unit = bitsPerPixel;
    image.bitmap_bit_order = LSBFirst;
    image.bitmap_pad = bitsPerPixel;
    image.depth = 24;
    image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
    image.bits_per_pixel = bitsPerPixel;
    return XInitImage(&image);
}
Beispiel #14
0
XImage *create_image_from_buffer (Display *dis, int screen, int *width, int *height) {
	int magiclength = strlen(artnavsegda);
	char magic[sizeof(artnavsegda)];
	fgets(magic,magiclength+1,stdin);
	if (strcmp(artnavsegda,magic)!=0)
	{
		printf("incorrect format\n");
		exit(1);
	}
	*width = fgetc(stdin);
	*height = fgetc(stdin);
	XImage *img = NULL;
	Visual *vis;
	double rRatio;
	double gRatio;
	double bRatio;
	int outIndex = 0;	
	int i;
	int numBufBytes = (3 * (*width * *height));
	vis = DefaultVisual (dis, screen);
	rRatio = vis->red_mask / 255.0;
	gRatio = vis->green_mask / 255.0;
	bRatio = vis->blue_mask / 255.0;
	size_t numNewBufBytes = (4 * (*width * *height));
	u_int32_t *newBuf = malloc (numNewBufBytes);
	for (i = 0; i < numBufBytes; ++i)
	{
		unsigned int r, g, b;
		r = (fgetc(stdin) * rRatio);
		++i;
		g = (fgetc(stdin) * gRatio);
		++i;
		b = (fgetc(stdin) * bRatio);
		//r &= vis->red_mask;
		//g &= vis->green_mask;
		//b &= vis->blue_mask;
		newBuf[outIndex] = r | g | b;
		++outIndex;
	}		
	img = XCreateImage (dis,CopyFromParent, 24,ZPixmap, 0,(char *) newBuf,*width, *height,32, 0);
	XInitImage (img);
	img->byte_order = LSBFirst;
	img->bitmap_bit_order = MSBFirst;
	return img;
}		
Beispiel #15
0
void MGLAPI XWIN8_putImage(MGLDC *dc,int left,int top,int right,
	int bottom,int dstLeft,int dstTop,int op,void *surface,
	int bytesPerLine,MGLDC *src)
/****************************************************************************
*
* Function:		XWIN8_putImage
* Parameters:	dc		- Device context
*
* Description:	Blt's a system memory buffer DC of the same pixel depth
*				as the display device context to the display DC.
*
****************************************************************************/
{
	MGL_HDC	gc = dc->wm.xwindc.gc;
	Window wnd = dc->wm.xwindc.wnd;
	Display *dpy = dc->wm.xwindc.dpy;
	xwindc_vars *v = &dc->wm.xwindc;
	
	//	XImage	*im = src->wm.memdc.img;
	XImage *im; // = dc->wm.xwindc.img;
	int func;

	switch(op){
	case MGL_REPLACE_MODE:
		func = GXcopy;
		break;
	case MGL_AND_MODE:
		func = GXand;
		break;
	case MGL_OR_MODE:
		func = GXor;
		break;
	case MGL_XOR_MODE:
		func = GXxor;
		break;
	}
	XSetFunction(dpy,gc,func);
	//	memcpy(im->data, surface, (src->mi.xRes+1) * (src->mi.yRes+1));
	//	memcpy(im->data, surface, (right-left+1) * (bottom-top+1));
	im = XCreateImage(dpy, DefaultVisualOfScreen(v->scr), 8, ZPixmap, 0, surface, right-left, bottom-top, 8, 0);
	XInitImage(im);
	XPutImage(dpy, wnd, gc, im, 0,0, dstLeft, dstTop, right-left,bottom-top);
	XDestroyImage(im);
}
Beispiel #16
0
void mainloop(StarfishRef tex)
{
  char *buf;
  int bpl;
  int n_pmf;
  int i;
  XPixmapFormatValues * pmf;   
     
  pmf = XListPixmapFormats (display, &n_pmf);
  if (pmf)
  {
    for (i = 0; i < n_pmf; i++)
    {
      if (pmf[i].depth == depth)
      {
        int pad, pad_bytes;
        bpp = pmf[i].bits_per_pixel;
	bpl = width * bpp / 8;
	pad = pmf[i].scanline_pad;
	pad_bytes = pad / 8;
	/* make bpl a whole multiple of pad/8 */
	bpl = (bpl + pad_bytes - 1) & ~(pad_bytes - 1);
        buf=malloc(height*bpl);
        image=XCreateImage(display,DefaultVisual(display,screen), depth,
       	                   ZPixmap,0,buf,width,height,pad,bpl);
        if(!image)
        {
          puts("starfish: XCreateImage failed");
          return;
        }
	break;
      }
    }
    XFree ((char *) pmf);
  }    

  if (! XInitImage(image))
    return;
  fillimage(tex);
  XSetWindowBackgroundImage(display, rootwin, image);
  XDestroyImage(image);
}
Beispiel #17
0
XImage *create_image_from_buffer (Display *dis, int screen, u_char *buf, int width, int height) {
	XImage *img = NULL;
	Visual *vis;
	double rRatio;
	double gRatio;
	double bRatio;
	int outIndex = 0;	
	int i;
	int numBufBytes = (3 * (width * height));
		
	vis = DefaultVisual (dis, screen);

	rRatio = vis->red_mask / 255.0;
	gRatio = vis->green_mask / 255.0;
	bRatio = vis->blue_mask / 255.0;
		
	size_t numNewBufBytes = (4 * (width * height));
	u_int32_t *newBuf = malloc (numNewBufBytes);
	
	for (i = 0; i < numBufBytes; ++i)
	{
		unsigned int r, g, b;
		r = (buf[i] * rRatio);
		++i;
		g = (buf[i] * gRatio);
		++i;
		b = (buf[i] * bRatio);
		r &= vis->red_mask;
		g &= vis->green_mask;
		b &= vis->blue_mask;
		newBuf[outIndex] = r | g | b;
		++outIndex;
	}		
		
	img = XCreateImage (dis,CopyFromParent, 24,ZPixmap, 0,(char *) newBuf,width, height,32, 0);
	//img = XCreateImage (dis,CopyFromParent, 24,ZPixmap, 0,(char *) buf,width, height,32, 0);
	XInitImage (img);
	img->byte_order = LSBFirst;
	img->bitmap_bit_order = MSBFirst;

	return img;
}		
Beispiel #18
0
void SetSurface(SystemDraw& w, const Rect& dest, const RGBA *pixels, Size srcsz, Point srcoff)
{
	GuiLock __;
	XImage ximg;
	sInitXImage(ximg, srcsz);
	ximg.bitmap_pad = 32;
	ximg.bytes_per_line = sizeof(RGBA) * srcsz.cx;
	ximg.bits_per_pixel = 32;
	ximg.blue_mask = 0x00ff0000;
	ximg.green_mask = 0x0000ff00;
	ximg.red_mask = 0x000000ff;
	ximg.bitmap_unit = 32;
	ximg.depth = 24;
	ximg.data = (char *)pixels;
	XInitImage(&ximg);
	Drawable dw = w.GetDrawable();
	GC gc = XCreateGC(Xdisplay, dw, 0, 0);
	Point p = dest.TopLeft() + w.GetOffset();
	XPutImage(Xdisplay, dw, gc, &ximg, srcoff.x, srcoff.y,
	          p.x, p.y, dest.GetWidth(), dest.GetHeight());
	XFreeGC(Xdisplay, gc);
}
Beispiel #19
0
void *SystemDraw::CursorX11(const Image& img)
{
	GuiLock __;
	int q = (int64)img.GetAuxData() - 1;
	if(q >= 0)
		return (void *)XCreateFontCursor(Xdisplay, q);
	int len = img.GetLength();
	Size sz = img.GetSize();
	Pixmap pixmap = XCreatePixmap(Xdisplay, Xroot, sz.cx, sz.cy, 32);
	XPicture picture = XRenderCreatePicture(
		Xdisplay, pixmap,
	    XRenderFindStandardFormat(Xdisplay, PictStandardARGB32),
	    0, 0
	);
	XImage ximg;
	sInitXImage(ximg, sz);
	ximg.bitmap_pad = 32;
	ximg.bytes_per_line = 4 * sz.cx;
	ximg.bits_per_pixel = 32;
	ximg.blue_mask = 0x00ff0000;
	ximg.green_mask = 0x0000ff00;
	ximg.red_mask = 0x000000ff;
	Buffer<RGBA> pma;
	pma.Alloc(len);
	memcpy(pma, ~img, len * sizeof(RGBA));
	ximg.bitmap_unit = 32;
	ximg.depth = 32;
	ximg.data = (char *)~pma;
	XInitImage(&ximg);
	GC gc = XCreateGC(Xdisplay, pixmap, 0, 0);
	XPutImage(Xdisplay, pixmap, gc, &ximg, 0, 0, 0, 0, sz.cx, sz.cy);
	XFreeGC(Xdisplay, gc);
	XFreePixmap(Xdisplay, pixmap);
	Point p = img.GetHotSpot();
	Cursor c = XRenderCreateCursor(Xdisplay, picture, p.x, p.y);
	XRenderFreePicture(Xdisplay, picture);
	return (void *)c;
}
int main(int argc, char *argv[])
{

    program_info programInfo = GetProgramInfoFromArgs(argc, argv);

    int winWidth = programInfo.winWidth;
    int winHeight = programInfo.winHeight;
    int bytesPerPixel = 4;

    // We allocate the image buffer
    SetupGraphicsBuffer(&gBuffer, winWidth, winHeight, bytesPerPixel);

    xBuffer.width = winWidth;
    xBuffer.height = winHeight;
    xBuffer.data = malloc(winWidth * winHeight * bytesPerPixel);

    // We start the image thread
    pthread_t thread;
    programInfo.buffer = &gBuffer;
    int result = pthread_create(&thread, 0, ImageThreadFunction, (void *)(&programInfo));
    if (result)
    {
        fprintf(stderr, "Error with pthread_create. Code: %d\n", result);
        exit(EXIT_FAILURE);
    }

    // We open the display
    Display *display = XOpenDisplay(0);
    Window window = XCreateSimpleWindow(display, DefaultRootWindow(display),
            0, 0, winWidth, winHeight, 0,
            0xFFFFFFFF, 0);
    GC gc = XCreateGC(display, window, 0, 0);


    XMapWindow(display, window);

    int screen = DefaultScreen(display);
    Visual *visual = DefaultVisual(display, screen);
    int depth = DefaultDepth(display, screen);
    XImage *image = XCreateImage(display,
            visual,
            depth,
            ZPixmap,
            0,
            (char *)xBuffer.data,
            winWidth,
            winHeight,
            32,
            0);
    XInitImage(image);


    int xMasks = ExposureMask | KeyPressMask;
    XSelectInput(display, window, xMasks);


    // Nasty hack
    Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
    /* XSetWMProtocols(display, window, &wmDeleteMessage, 1); */

    int loopRunning = 1;
    while(loopRunning)
    {
        XEvent event;
        /* XNextEvent(display, &event); */

        // We flush the events queue
        while (XCheckMaskEvent(display, xMasks, &event))
        {
            switch (event.type)
            {
                case Expose:
                    TransferBuffer(&gBuffer, &xBuffer);
                    XPutImage(display, window, gc, image,
                            0, 0, 0, 0,
                            winWidth, winHeight);
                    XFlush(display);
                    break;
                    // Hack for the window close eventExposureMask
                    // TODO(Cristian): See how to do this correctly
                case ClientMessage:
                    if (event.xclient.data.l[0] == wmDeleteMessage)
                    {
                        loopRunning = 0;
                    }
                    break;
            }
        }

        // We sleep for a while
        usleep(100 * 1000);

        TransferBuffer(&gBuffer, &xBuffer);
        XPutImage(display, window, gc, image,
                0, 0, 0, 0,
                winWidth, winHeight);
        XFlush(display);

    }
}
Beispiel #21
0
bool CWinSystemX11::CreateIconPixmap()
{
  int depth;
  XImage *img = NULL;
  Visual *vis;
  XWindowAttributes wndattribs;
  XVisualInfo visInfo;
  double rRatio;
  double gRatio;
  double bRatio;
  int outIndex = 0;
  unsigned int i,j;
  unsigned char *buf;
  uint32_t *newBuf = 0;
  size_t numNewBufBytes;

  // Get visual Info
  XGetWindowAttributes(m_dpy, m_glWindow, &wndattribs);
  visInfo.visualid = wndattribs.visual->visualid;
  int nvisuals = 0;
  XVisualInfo* visuals = XGetVisualInfo(m_dpy, VisualIDMask, &visInfo, &nvisuals);
  if (nvisuals != 1)
  {
    CLog::Log(LOGERROR, "CWinSystemX11::CreateIconPixmap - could not find visual");
    return false;
  }
  visInfo = visuals[0];
  XFree(visuals);

  depth = visInfo.depth;
  vis = visInfo.visual;

  if (depth < 15)
  {
    CLog::Log(LOGERROR, "CWinSystemX11::CreateIconPixmap - no suitable depth");
    return false;
  }

  rRatio = vis->red_mask / 255.0;
  gRatio = vis->green_mask / 255.0;
  bRatio = vis->blue_mask / 255.0;

  CBaseTexture *iconTexture = CBaseTexture::LoadFromFile("special://xbmc/media/icon256x256.png");

  if (!iconTexture)
    return false;

  buf = iconTexture->GetPixels();

  if (depth>=24)
    numNewBufBytes = (4 * (iconTexture->GetWidth() * iconTexture->GetHeight()));
  else
    numNewBufBytes = (2 * (iconTexture->GetWidth() * iconTexture->GetHeight()));

  newBuf = (uint32_t*)malloc(numNewBufBytes);
  if (!newBuf)
  {
    CLog::Log(LOGERROR, "CWinSystemX11::CreateIconPixmap - malloc failed");
    return false;
  }

  for (i=0; i<iconTexture->GetHeight();++i)
  {
    for (j=0; j<iconTexture->GetWidth();++j)
    {
      unsigned int pos = i*iconTexture->GetPitch()+j*4;
      unsigned int r, g, b;
      r = (buf[pos+2] * rRatio);
      g = (buf[pos+1] * gRatio);
      b = (buf[pos+0] * bRatio);
      r &= vis->red_mask;
      g &= vis->green_mask;
      b &= vis->blue_mask;
      newBuf[outIndex] = r | g | b;
      ++outIndex;
    }
  }
  img = XCreateImage(m_dpy, vis, depth,ZPixmap, 0, (char *)newBuf,
                     iconTexture->GetWidth(), iconTexture->GetHeight(),
                     (depth>=24)?32:16, 0);
  if (!img)
  {
    CLog::Log(LOGERROR, "CWinSystemX11::CreateIconPixmap - could not create image");
    free(newBuf);
    return false;
  }
  if (!XInitImage(img))
  {
    CLog::Log(LOGERROR, "CWinSystemX11::CreateIconPixmap - init image failed");
    XDestroyImage(img);
    return false;
  }

  // set byte order
  union
  {
    char c[sizeof(short)];
    short s;
  } order;
  order.s = 1;
  if ((1 == order.c[0]))
  {
    img->byte_order = LSBFirst;
  }
  else
  {
    img->byte_order = MSBFirst;
  }

  // create icon pixmap from image
  m_icon = XCreatePixmap(m_dpy, m_glWindow, img->width, img->height, depth);
  GC gc = XCreateGC(m_dpy, m_glWindow, 0, NULL);
  XPutImage(m_dpy, m_icon, gc, img, 0, 0, 0, 0, img->width, img->height);
  XFreeGC(m_dpy, gc);
  XDestroyImage(img); // this also frees newBuf

  delete iconTexture;

  return true;
}
Beispiel #22
0
int main(int argc, char **argv)
{
	int x, y;

	Window win1;
	GC gc1;
	XEvent event;
	XImage *ximg1;

	int flag;
	struct IplImage *prev1, *curr1, *motion;
	struct IplDev *dev1;

	time_t start, end;
	int f;

	f = 0;
	start = 0;
	end = 0;
	
	x = 0;
	y = 0;
	initX();
	getXinfo();
	win1 = openWindow(500, 500, 640, 480, 0, &gc1);
	XNextEvent(theDisplay, &event);
	ximg1 = XGetImage(theDisplay, win1, x, y, 640, 480, AllPlanes, ZPixmap);
	if(XInitImage(ximg1) == 0) {
		fprintf(stderr,"error: XInitImage\n");
		return 1;
	}

	printf("first xgetimage done\n");

	if ((dev1 = ipl_opendev(0, IPL_RGB_MODE)) == NULL) {
		printf("error while creating device 0\n");
		return 1;
	}

	if (ipl_setparams(dev1, 320, 240, IPL_FORCE_SCALE_ON) < 0) {
		fprintf(stderr, "error on changing cam params\n");
		free(dev1);
		return 1;
	}
	if ((prev1 = ipl_getframe(dev1)) == NULL) {
		printf("error capturing prev1\n");
		return 1;
	}


	flag = 1;
	start = time(NULL);
	
	while (flag) {
		f++;	
		end = time(NULL);
		if (end - start >= 1.0) {
			printf("fps = %i\n", f);
			f = 0;
			start = end;
		}
	
		if ((curr1 = ipl_getframe(dev1)) == NULL) {
			printf("error capturing curr1\n");
			return 1;
		}
		motion = framescompare(curr1, prev1);
		ipl_scaleimg(&motion, 640, 480);
		camera_shoot(motion, ximg1, gc1, win1, x, y);
		ipl_freeimg(&prev1);
		prev1 = curr1;


		ipl_freeimg(&motion);
		XCheckTypedWindowEvent(theDisplay, win1, ButtonPress, &event);
		if (event.xbutton.button == 1)
			flag = 0;
	}

	ipl_freeimg(&curr1);
	XDestroyImage(ximg1);
	XDestroyWindow(theDisplay, win1);

	quitX();


	return 0;
}
Beispiel #23
0
void HoImage::init(int w, int h, void *rgbData) {
    this->w = w;
    this->h = h;

#if hplatform==XW
    handle = XCreatePixmap(
                 HoMainApp->dpy,
                 DefaultRootWindow(HoMainApp->dpy),
                 w, h,
                 DefaultDepth(HoMainApp->dpy, DefaultScreen(HoMainApp->dpy))
             );
    int depth;
    XImage *img = NULL;
    int outIndex = 0;
    int i;
    int numBufBytes = (3 * (w * h));
    depth = DefaultDepth(HoMainApp->dpy, DefaultScreen(HoMainApp->dpy));
    size_t numNewBufBytes = (4 * (w * h));
    unsigned *newBuf = (unsigned *) malloc(numNewBufBytes);
    for (i = 0; i < numBufBytes;) {
        newBuf[outIndex] = ((unsigned char *) rgbData)[i++] << 16;
        newBuf[outIndex] |= ((unsigned char *) rgbData)[i++] << 8;
        newBuf[outIndex] |= ((unsigned char *) rgbData)[i++];
        ++outIndex;
    }

    img = XCreateImage (HoMainApp->dpy,
                        CopyFromParent, depth,
                        ZPixmap, 0,
                        (char *) newBuf,
                        w, h,
                        32, 0
                       );


    XInitImage (img);
    if (htons(1) != 1) {
        img->byte_order = LSBFirst;
    } else {
        img->byte_order = MSBFirst;
    }

    /*The bitmap_bit_order doesn't matter with ZPixmap images.*/
    img->bitmap_bit_order = MSBFirst;

    GC dc = XCreateGC(HoMainApp->dpy, handle, 0, NULL);
    XPutImage(HoMainApp->dpy, handle, dc, img, 0, 0, 0, 0, w, h);
    XFreeGC(HoMainApp->dpy, dc);
    XFlush(HoMainApp->dpy);
    XDestroyImage(img);

#elif hplatform==MW

    BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO));
    bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi->bmiHeader.biWidth = w;
    bmi->bmiHeader.biHeight = h;
    bmi->bmiHeader.biPlanes = 1;
    bmi->bmiHeader.biBitCount = 32;
    bmi->bmiHeader.biCompression = BI_RGB;
    bmi->bmiHeader.biSizeImage = 0;
    bmi->bmiHeader.biXPelsPerMeter = 0;
    bmi->bmiHeader.biYPelsPerMeter = 0;
    bmi->bmiHeader.biClrUsed = 0;
    bmi->bmiHeader.biClrImportant = 0;
    void *data;
    unsigned char *bdata;

    handle = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &data, NULL, NULL);
    bdata = (unsigned char *) data;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            bdata[(i * w + j) * 4 + 2] = ((unsigned char *) rgbData)[((h - i - 1) * w + j) * 3];
            bdata[(i * w + j) * 4 + 1] = ((unsigned char *) rgbData)[((h - i - 1) * w + j) * 3 + 1];
            bdata[(i * w + j) * 4] = ((unsigned char *) rgbData)[((h - i - 1) * w + j) * 3 + 2];
        }
    }
#endif

}
Beispiel #24
0
void X11EmbedContainer::setBackgroundPixmap(QPixmap background)
{
    if (!clientWinId()) {
        return;
    }

    Display *display = QX11Info::display();
    Pixmap bg = XCreatePixmap(display, clientWinId(), width(), height(), d->attr.depth);

    XRenderPictFormat *format = XRenderFindVisualFormat(display, d->attr.visual);
    Picture picture = XRenderCreatePicture(display, bg, format, 0, 0);

    //Prevent updating the background-image if possible. Updating can cause a very annoying flicker due to the XClearArea, and thus has to be kept to a minimum
    QImage image;
    if (background.paintEngine()->type() != QPaintEngine::X11)
      image = background.toImage(); // With the raster graphics system this call just returns the backing image, so the image data isn't copied.
    else
      image = background.copy().toImage(); //With the X11 graphics engine, we have to create a copy first, else we get a crash

    if(d->oldBackgroundImage == image) {
      XFreePixmap(display, bg);
      XRenderFreePicture(display, picture);
      return;
    }
    d->oldBackgroundImage = image;

    if (background.paintEngine()->type() != QPaintEngine::X11) {

        XRenderPictFormat *format = 0;
        int depth = 0;
        int bpp = 0;

        if (image.format() == QImage::Format_ARGB32_Premultiplied) {
            format = XRenderFindStandardFormat(display, PictStandardARGB32);
            depth = 32;
            bpp = 32;
        } else if (image.format() == QImage::Format_RGB32) {
            format = XRenderFindStandardFormat(display, PictStandardRGB24);
            depth = 24;
            bpp = 32;
        } else if (image.format() == QImage::Format_RGB16) {
            bpp = 16;
            depth = 16;

            // Try to find a picture format that matches the image format.
            // The Render spec doesn't require the X server to support 16bpp formats,
            // so this call can fail.
            XRenderPictFormat templ;
            templ.type             = PictTypeDirect;
            templ.direct.alpha     = 0;
            templ.direct.alphaMask = 0;
            templ.depth            = 16;
            templ.direct.red       = 11;
            templ.direct.redMask   = 0x1f;
            templ.direct.green     = 5;
            templ.direct.greenMask = 0x3f;
            templ.direct.blue      = 0;
            templ.direct.blueMask  = 0x1f;
            format = XRenderFindFormat(display, PictFormatType | PictFormatDepth | PictFormatAlpha |
                                       PictFormatAlphaMask | PictFormatRed | PictFormatRedMask |
                                       PictFormatGreen | PictFormatGreenMask | PictFormatBlue |
                                       PictFormatBlueMask, &templ, 0);
        }

        if (format == 0)
        {
            // Convert the image to a standard format.
            if (image.hasAlphaChannel()) {
                image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
                format = XRenderFindStandardFormat(display, PictStandardARGB32);
                depth = 32;
            } else { 
                image = image.convertToFormat(QImage::Format_RGB32);
                format = XRenderFindStandardFormat(display, PictStandardRGB24);
                depth = 24;
            }
            bpp = 32;
        }

        if (image.format() == QImage::Format_RGB32) {
            // Make sure the would be alpha bits are set to 1.
            quint32 * const pixels = (quint32*)(const_cast<const QImage*>(&image)->bits());
            for (int i = 0; i < image.width() * image.height(); i++) {
                pixels[i] |= 0xff000000;
            }
        }

        Q_ASSERT(format != 0);

        // Get the image data into a pixmap
        XImage ximage;
        ximage.width            = image.width(); 
        ximage.height           = image.height();
        ximage.xoffset          = 0; 
        ximage.format           = ZPixmap;
        // This is a hack to prevent the image data from detaching
        ximage.data             = (char*) const_cast<const QImage*>(&image)->bits();
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
        ximage.byte_order       = MSBFirst;
#else
        ximage.byte_order       = LSBFirst;
#endif
        ximage.bitmap_unit      = bpp;
        ximage.bitmap_bit_order = ximage.byte_order;
        ximage.bitmap_pad       = bpp;
        ximage.depth            = depth;
        ximage.bytes_per_line   = image.bytesPerLine();
        ximage.bits_per_pixel   = bpp;
        if (depth > 16) {
            ximage.red_mask     = 0x00ff0000;
            ximage.green_mask   = 0x0000ff00;
            ximage.blue_mask    = 0x000000ff;
        } else {
            // r5g6b5
            ximage.red_mask     = 0xf800;
            ximage.green_mask   = 0x07e0;
            ximage.blue_mask    = 0x001f;
        }
        ximage.obdata           = 0;
        if (XInitImage(&ximage) == 0) {
            XRenderFreePicture(display, picture);
            XFreePixmap(display, bg);
            return;
        }

        Pixmap pm = XCreatePixmap(display, clientWinId(), width(), height(), ximage.depth);
        GC gc = XCreateGC(display, pm, 0, 0);
        XPutImage(display, pm, gc, &ximage, 0, 0, 0, 0, width(), height());
        XFreeGC(display, gc);

        Picture pict = XRenderCreatePicture(display, pm, format, 0, 0);
        XRenderComposite(display, PictOpSrc, pict, None, picture,
                         0, 0, 0, 0, 0, 0, width(), height());
        XRenderFreePicture(display, pict);
        XFreePixmap(display, pm);
    } else {
        XRenderComposite(display, PictOpSrc, background.x11PictureHandle(),
                         None, picture, 0, 0, 0, 0, 0, 0, width(), height());
    }

    XSetWindowBackgroundPixmap(display, clientWinId(), bg);

    XRenderFreePicture(display, picture);
    XFreePixmap(display, bg);

    XClearArea(display, clientWinId(), 0, 0, 0, 0, True);
}
Beispiel #25
0
static void*
display_create (const void* arg)
{
  display_t* display = malloc (sizeof (display_t));

  /* open connection with the server */
  display->display = XOpenDisplay (NULL);
  
  if (display->display == NULL) {
    fprintf (stderr, "Cannot open display\n");
    exit (EXIT_FAILURE);
  }
  
  display->fd = ConnectionNumber (display->display);
  display->screen = DefaultScreen (display->display);
  
  /* create window */
  display->window = XCreateSimpleWindow (display->display,
					 RootWindow (display->display, display->screen),
					 X_OFFSET, Y_OFFSET, WIDTH, HEIGHT, BORDER_WIDTH,
					 BlackPixel (display->display, display->screen),
					 WhitePixel (display->display, display->screen));
  
  // Prosses Window Close Event through event handler so XNextEvent does Not fail
  display->del_window = XInternAtom (display->display, "WM_DELETE_WINDOW", 0);
  XSetWMProtocols (display->display, display->window, &display->del_window, 1);
  
  /* select kind of events we are interested in */
  XSelectInput (display->display, display->window, StructureNotifyMask | ExposureMask | KeyPressMask);
  
  /* map (show) the window */
  XMapWindow (display->display, display->window);
  XFlush (display->display);

  display->data = malloc (WIDTH * HEIGHT * sizeof (rgb_t));

  int depth = DefaultDepth (display->display, display->screen);
  Visual* visual = DefaultVisual (display->display, display->screen);

  /* I assume these.
   * Generlize later.
   */
  assert (depth == 24);
  assert (visual->red_mask == 0x00FF0000);
  assert (visual->green_mask == 0x0000FF00);
  assert (visual->blue_mask == 0x000000FF);

  display->image = XCreateImage (display->display,
				 CopyFromParent,
				 depth,
				 ZPixmap,
				 0,
				 (char *)display->data,
				 WIDTH,
				 HEIGHT,
				 32,
				 0);

  assert (1 == XInitImage (display->image));
  
  return display;
}
Beispiel #26
0
void ImageSysData::Paint(SystemDraw& w, int x, int y, const Rect& src, Color c)
{
	GuiLock __;
	x += w.GetOffset().x;
	y += w.GetOffset().y;
	Size sz = img.GetSize();
	int  len = sz.cx * sz.cy;
	Rect sr = src & sz;
	Size ssz = sr.Size();
	if(sr.IsEmpty())
		return;
	int kind = img.GetKind();
	if(kind == IMAGE_EMPTY)
		return;
	if(kind == IMAGE_OPAQUE && !IsNull(c)) {
		w.DrawRect(x, y, sz.cx, sz.cy, c);
		return;
	}
	if(kind == IMAGE_OPAQUE && paintcount == 0 && sr == Rect(sz)) {
		SetSurface(w, x, y, sz.cx, sz.cy, ~img);
		paintcount++;
		return;
	}
	if(IsNull(c)) {
		if(!picture) {
			bool opaque = kind == IMAGE_OPAQUE;
			Pixmap pixmap = XCreatePixmap(Xdisplay, Xroot, sz.cx, sz.cy, opaque ? 24 : 32);
			picture = XRenderCreatePicture(
				Xdisplay, pixmap,
			    XRenderFindStandardFormat(Xdisplay, opaque ? PictStandardRGB24
			                                               : PictStandardARGB32),
			    0, 0
			);
			XImage ximg;
			sInitXImage(ximg, sz);
			ximg.bitmap_pad = 32;
			ximg.bytes_per_line = 4 * sz.cx;
			ximg.bits_per_pixel = 32;
			ximg.blue_mask = 0x00ff0000;
			ximg.green_mask = 0x0000ff00;
			ximg.red_mask = 0x000000ff;
			ximg.bitmap_unit = 32;
			ximg.data = (char *)~img;
			ximg.depth = opaque ? 24 : 32;
			XInitImage(&ximg);
			GC gc = XCreateGC(Xdisplay, pixmap, 0, 0);
			XPutImage(Xdisplay, pixmap, gc, &ximg, 0, 0, 0, 0, sz.cx, sz.cy);
			XFreeGC(Xdisplay, gc);
			XFreePixmap(Xdisplay, pixmap);
			SysImageRealized(img);
		}
		XRenderComposite(Xdisplay, PictOpOver,
		                 picture, 0, XftDrawPicture(w.GetXftDraw()),
		                 sr.left, sr.top, 0, 0, x, y, ssz.cx, ssz.cy);
	}
	else {
		if(!picture8) {
			Pixmap pixmap = XCreatePixmap(Xdisplay, Xroot, sz.cx, sz.cy, 8);
			picture8 = XRenderCreatePicture(Xdisplay, pixmap,
			                                XRenderFindStandardFormat(Xdisplay, PictStandardA8),
			                                0, 0);
			Buffer<byte> ab(len);
			byte *t = ab;
			const RGBA *s = ~img;
			const RGBA *e = s + len;
			while(s < e)
				*t++ = (s++)->a;
			XImage ximg;
			sInitXImage(ximg, sz);
			ximg.data             = (char *)~ab;
			ximg.bitmap_unit      = 8;
			ximg.bitmap_pad       = 8;
			ximg.depth            = 8;
			ximg.bytes_per_line   = sz.cx;
			ximg.bits_per_pixel   = 8;
			XInitImage(&ximg);
			GC gc = XCreateGC(Xdisplay, pixmap, 0, 0);
			XPutImage(Xdisplay, pixmap, gc, &ximg, 0, 0, 0, 0, sz.cx, sz.cy);
			XFreeGC(Xdisplay, gc);
			XFreePixmap(Xdisplay, pixmap);
		}
		XRenderComposite(Xdisplay, PictOpOver,
		                 sGetSolidFill(c), picture8, XftDrawPicture(w.GetXftDraw()),
		                 sr.left, sr.top, 0, 0, x, y, ssz.cx, ssz.cy);
	}
}
Beispiel #27
0
int 
main(int argc, char *argv[])
{
    Display *dpy;
    int screen;
    register int i;
    XImage in_image_struct;
    XImage *in_image, *out_image;
    XSetWindowAttributes attributes;
    XVisualInfo vinfo, *vinfos;
    long mask;
    register char *buffer;
    unsigned long swaptest = 1;
    int count, stdcnt;
    unsigned buffer_size;
    int win_name_size;
    int ncolors;
    char *file_name = NULL;
    char *win_name;
    Bool inverse = False, rawbits = False, newmap = False;
    Bool onclick = True;
    Bool scale = False;
    int plane = -1;
    char *std = NULL;
    char *vis = NULL;
    char *display_name = NULL;
    char *fgname = NULL;
    char *bgname = NULL;
    char *geom = NULL;
    int gbits = 0;
    XSizeHints hints;
    XTextProperty textprop;
    XClassHint class_hint;
    XColor *colors = NULL, color, igncolor;
    Window image_win;
    Colormap colormap;
    XEvent event;
    register XExposeEvent *expose = (XExposeEvent *)&event;
    GC gc;
    XGCValues gc_val;
    XWDFileHeader header;
    XWDColor xwdcolor;
    FILE *in_file = stdin;
    char *map_name;
    Atom map_prop;
    XStandardColormap *stdmaps, *stdmap = NULL;
    char c;
    int win_width, win_height;

    progname = argv[0];

    for (i = 1; i < argc; i++) {
	if (strcmp(argv[i], "-bg") == 0) {
	    if (++i >= argc) usage();
	    bgname = argv[i];
	    continue;
	}
	if (strcmp(argv[i], "-display") == 0) {
	    if (++i >= argc) usage();
	    display_name = argv[i];
	    continue;
	}
	if (strcmp(argv[i], "-fg") == 0) {
	    if (++i >= argc) usage();
	    fgname = argv[i];
	    continue;
	}
	if (strcmp(argv[i], "-geometry") == 0) {
	    if (++i >= argc) usage();
	    geom = argv[i];
	    continue;
	}
	if (strcmp(argv[i], "-help") == 0) {
	    usage();
	}
	if (strcmp(argv[i], "-in") == 0) {
	    if (++i >= argc) usage();
	    file_name = argv[i];
	    continue;
	}
	if (strcmp(argv[i], "-inverse") == 0) { /* for compatibility */
	    inverse = True;
	    continue;
	}
	if (strcmp(argv[i], "-new") == 0) {
	    newmap = True;
	    if (std) usage();
	    continue;
	}
	if (strcmp(argv[i], "-noclick") == 0) {
	    onclick = False;
	    continue;
	}
	if (strcmp(argv[i], "-plane") == 0) {
	    if (++i >= argc) usage();
	    plane = atoi(argv[i]);
	    continue;
	}
	if (strcmp(argv[i], "-raw") == 0) {
	    rawbits = True;
	    if (std) usage();
	    continue;
	}
	if (strcmp(argv[i], "-rv") == 0) {
	    inverse = True;
	    continue;
	}
	if (strcmp(argv[i], "-scale") == 0) {
	    scale = True;
	    continue;
	}
	if (strcmp(argv[i], "-split") == 0) {
	    split = True;
	    continue;
	}
	if (strcmp(argv[i], "-std") == 0) {
	    if (++i >= argc) usage();
	    std = argv[i];
	    if (newmap || rawbits) usage();
	    continue;
	}
	if (strcmp(argv[i], "-vis") == 0) {
	    if (++i >= argc) usage();
	    vis = argv[i];
	    continue;
	}
	usage();
    }
    
    if (file_name) {
	in_file = fopen(file_name, "rb");
	if (in_file == NULL)
	    Error("Can't open input file as specified.");
    }
#ifdef WIN32
    else
	_setmode(fileno(in_file), _O_BINARY);
#endif
    
    dpy = XOpenDisplay(display_name);
    if (dpy == NULL) {
	fprintf(stderr, "%s:  unable to open display \"%s\"\n",
		progname, XDisplayName(display_name));
	exit(1);
    }
    screen = DefaultScreen(dpy);

    /*
     * Read in header information.
     */
    if(!Read((char *)&header, SIZEOF(XWDheader), 1, in_file))
      Error("Unable to read dump file header.");

    if (*(char *) &swaptest)
	_swaplong((char *) &header, SIZEOF(XWDheader));

    /* check to see if the dump file is in the proper format */
    if (header.file_version != XWD_FILE_VERSION) {
	fprintf(stderr,"xwud: XWD file format version mismatch.");
	Error("exiting.");
    }
    if (header.header_size < SIZEOF(XWDheader)) {
	fprintf(stderr,"xwud: XWD header size is too small.");
	Error("exiting.");
    }

    /* alloc window name */
    win_name_size = (header.header_size - SIZEOF(XWDheader));
    if((win_name = malloc((unsigned) win_name_size + 6)) == NULL)
      Error("Can't malloc window name storage.");
    strcpy(win_name, "xwud: ");

     /* read in window name */
    if(!Read(win_name + 6, sizeof(char), win_name_size, in_file))
      Error("Unable to read window name from dump file.");

    /* initialize the input image */

    in_image = &in_image_struct;
    in_image->depth = header.pixmap_depth;
    in_image->format = header.pixmap_format;
    in_image->xoffset = header.xoffset;
    in_image->data = NULL;
    in_image->width = header.pixmap_width;
    in_image->height = header.pixmap_height;
    in_image->bitmap_pad = header.bitmap_pad;
    in_image->bytes_per_line = header.bytes_per_line;
    in_image->byte_order = header.byte_order;
    in_image->bitmap_unit = header.bitmap_unit;
    in_image->bitmap_bit_order = header.bitmap_bit_order;
    in_image->bits_per_pixel = header.bits_per_pixel;
    in_image->red_mask = header.red_mask;
    in_image->green_mask = header.green_mask;
    in_image->blue_mask = header.blue_mask;
    if (!XInitImage(in_image))
	Error("Invalid input image header data.");

    /* read in the color map buffer */
    if((ncolors = header.ncolors)) {
	colors = (XColor *)malloc((unsigned) ncolors * sizeof(XColor));
	if (!colors)
	    Error("Can't malloc color table");
	for (i = 0; i < ncolors; i++) {
	    if(!Read((char *) &xwdcolor, SIZEOF(XWDColor), 1, in_file))
		Error("Unable to read color map from dump file.");
	    colors[i].pixel = xwdcolor.pixel;
	    colors[i].red = xwdcolor.red;
	    colors[i].green = xwdcolor.green;
	    colors[i].blue = xwdcolor.blue;
	    colors[i].flags = xwdcolor.flags;
	}
	if (*(char *) &swaptest) {
	    for (i = 0; i < ncolors; i++) {
		_swaplong((char *) &colors[i].pixel, sizeof(long));
		_swapshort((char *) &colors[i].red, 3 * sizeof(short));
	    }
	}
    }
    else
	/* no color map exists, turn on the raw option */
	rawbits = True;

    /* alloc the pixel buffer */
    buffer_size = Image_Size(in_image);
    if((buffer = malloc(buffer_size)) == NULL)
      Error("Can't malloc data buffer.");

    /* read in the image data */
    if (!Read(buffer, sizeof(char), (int)buffer_size, in_file))
        Error("Unable to read pixmap from dump file.");

     /* close the input file */
    (void) fclose(in_file);

    if (plane >= in_image->depth)
	Error("plane number exceeds image depth");
    if ((in_image->format == XYPixmap) && (plane >= 0)) {
	buffer += in_image->bytes_per_line * in_image->height *
		  (in_image->depth - (plane + 1));
	in_image->depth = 1;
	ncolors = 0;
    }
    if (in_image->bits_per_pixel == 1 && in_image->depth == 1) {
	in_image->format = XYBitmap;
	newmap = False;
	rawbits = True;
    }
    in_image->data = buffer;

    if (std) {
	map_name = malloc(strlen(std) + 9);
	strcpy(map_name, "RGB_");
	strcat(map_name, std);
	strcat(map_name, "_MAP");
	Latin1Upper(map_name);
	map_prop = XInternAtom(dpy, map_name, True);
	if (!map_prop || !XGetRGBColormaps(dpy, RootWindow(dpy, screen),
					   &stdmaps, &stdcnt, map_prop))
	    Error("specified standard colormap does not exist");
    }
    vinfo.screen = screen;
    mask = VisualScreenMask;
    if (vis)
    {
	char *vt;
	vt = malloc(strlen(vis) + 1);
	strcpy(vt, vis);
	Latin1Upper(vt);
	if (strcmp(vt, "STATICGRAY") == 0) {
	    vinfo.class = StaticGray;
	    mask |= VisualClassMask;
	} else if (strcmp(vt, "GRAYSCALE") == 0) {
Beispiel #28
0
static Image *ReadXWDImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
    char
    *comment = (char *) NULL;

    Image
    *image;

    IndexPacket
    index_val;

    int
    status;

    long
    y;

    register IndexPacket
    *indexes;

    register long
    x;

    register PixelPacket
    *q;

    register unsigned long
    pixel;

    size_t
    count,
    length;

    unsigned long
    lsb_first;

    XColor
    *colors = (XColor *) NULL;

    XImage
    *ximage = (XImage *) NULL;

    XWDFileHeader
    header;

    /*
      Open image file.
    */
    assert(image_info != (const ImageInfo *) NULL);
    assert(image_info->signature == MagickSignature);
    assert(exception != (ExceptionInfo *) NULL);
    assert(exception->signature == MagickSignature);
    image=AllocateImage(image_info);
    status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
    if (status == False)
        ThrowXWDReaderException(FileOpenError,UnableToOpenFile,image);
    /*
      Read in header information.

      XWDFileHeader is defined in /usr/include/X11/XWDFile.h

      All elements are 32-bit unsigned storage but non-mask properties
      in XImage use 32-bit signed values.
    */
    count=ReadBlob(image,sz_XWDheader,(char *) &header);
    if (count != sz_XWDheader)
        ThrowXWDReaderException(CorruptImageError,UnableToReadImageHeader,image);
    /*
      Ensure the header byte-order is most-significant byte first.
    */
    lsb_first=1;
    if (*(char *) &lsb_first)
        MSBOrderLong((unsigned char *) &header,sz_XWDheader);

    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                          "XWDFileHeader:\n"
                          "    header_size      : %u\n"
                          "    file_version     : %u\n"
                          "    pixmap_format    : %s\n"
                          "    pixmap_depth     : %u\n"
                          "    pixmap_width     : %u\n"
                          "    pixmap_height    : %u\n"
                          "    xoffset          : %u\n"
                          "    byte_order       : %s\n"
                          "    bitmap_unit      : %u\n"
                          "    bitmap_bit_order : %s\n"
                          "    bitmap_pad       : %u\n"
                          "    bits_per_pixel   : %u\n"
                          "    bytes_per_line   : %u\n"
                          "    visual_class     : %s\n"
                          "    red_mask         : 0x%06X\n"
                          "    green_mask       : 0x%06X\n"
                          "    blue_mask        : 0x%06X\n"
                          "    bits_per_rgb     : %u\n"
                          "    colormap_entries : %u\n"
                          "    ncolors          : %u\n"
                          "    window_width     : %u\n"
                          "    window_height    : %u\n"
                          "    window_x         : %u\n"
                          "    window_y         : %u\n"
                          "    window_bdrwidth  : %u",
                          (unsigned int) header.header_size,
                          (unsigned int) header.file_version,
                          /* (unsigned int) header.pixmap_format, */
                          (header.pixmap_format == XYBitmap ? "XYBitmap" :
                           (header.pixmap_format == XYPixmap ? "XYPixmap" :
                            (header.pixmap_format == ZPixmap ? "ZPixmap" : "?"))),
                          (unsigned int) header.pixmap_depth,
                          (unsigned int) header.pixmap_width,
                          (unsigned int) header.pixmap_height,
                          (unsigned int) header.xoffset,
                          (header.byte_order == MSBFirst? "MSBFirst" :
                           (header.byte_order == LSBFirst ? "LSBFirst" : "?")),
                          (unsigned int) header.bitmap_unit,
                          (header.bitmap_bit_order == MSBFirst? "MSBFirst" :
                           (header.bitmap_bit_order == LSBFirst ? "LSBFirst" :
                            "?")),
                          (unsigned int) header.bitmap_pad,
                          (unsigned int) header.bits_per_pixel,
                          (unsigned int) header.bytes_per_line,
                          (header.visual_class == StaticGray ? "StaticGray" :
                           (header.visual_class == GrayScale ? "GrayScale" :
                            (header.visual_class == StaticColor ? "StaticColor" :
                             (header.visual_class == PseudoColor ? "PseudoColor" :
                              (header.visual_class == TrueColor ? "TrueColor" :
                               (header.visual_class == DirectColor ?
                                "DirectColor" : "?")))))),
                          (unsigned int) header.red_mask,
                          (unsigned int) header.green_mask,
                          (unsigned int) header.blue_mask,
                          (unsigned int) header.bits_per_rgb,
                          (unsigned int) header.colormap_entries,
                          (unsigned int) header.ncolors,
                          (unsigned int) header.window_width,
                          (unsigned int) header.window_height,
                          (unsigned int) header.window_x,
                          (unsigned int) header.window_y,
                          (unsigned int) header.window_bdrwidth
                         );

    /*
      Check to see if the dump file is in the proper format.
    */
    if (header.file_version != XWD_FILE_VERSION)
        ThrowXWDReaderException(CorruptImageError,InvalidFileFormatVersion,image);
    if (header.header_size < sz_XWDheader)
        ThrowXWDReaderException(CorruptImageError,CorruptImage,image);
    switch (header.visual_class)
    {
    case StaticGray:
    case GrayScale:
    case StaticColor:
    case PseudoColor:
    case TrueColor:
    case DirectColor:
        break;
    default:
    {
        ThrowXWDReaderException(CorruptImageError,CorruptImage,image);
    }
    }
    switch (header.pixmap_format)
    {
    case XYBitmap:
    case XYPixmap:
    case ZPixmap:
        break;
    default:
    {
        ThrowXWDReaderException(CorruptImageError,CorruptImage,image);
    }
    }

    /*
      Retrieve comment (if any)
    */
    length=header.header_size-sz_XWDheader;
    if (length > ((~0UL)/sizeof(*comment)))
        ThrowXWDReaderException(CorruptImageError,ImproperImageHeader,image);
    comment=MagickAllocateMemory(char *,length+1);
    if (comment == (char *) NULL)
        ThrowXWDReaderException(ResourceLimitError,MemoryAllocationFailed,image);
    count=ReadBlob(image,length,comment);
    if (count != length)
        ThrowXWDReaderException(CorruptImageError,UnableToReadWindowNameFromDumpFile,
                                image);
    comment[length]='\0';
    (void) SetImageAttribute(image,"comment",comment);
    MagickFreeMemory(comment);

    /*
      Initialize the X image.
    */
    ximage=MagickAllocateMemory(XImage *,sizeof(XImage));
    if (ximage == (XImage *) NULL)
        ThrowXWDReaderException(ResourceLimitError,MemoryAllocationFailed,image);
    ximage->depth=(int) header.pixmap_depth;
    ximage->format=(int) header.pixmap_format;
    ximage->xoffset=(int) header.xoffset;
    ximage->data=(char *) NULL;
    ximage->width=(int) header.pixmap_width;
    ximage->height=(int) header.pixmap_height;
    ximage->bitmap_pad=(int) header.bitmap_pad;
    ximage->bytes_per_line=(int) header.bytes_per_line;
    ximage->byte_order=(int) header.byte_order;
    ximage->bitmap_unit=(int) header.bitmap_unit;
    ximage->bitmap_bit_order=(int) header.bitmap_bit_order;
    ximage->bits_per_pixel=(int) header.bits_per_pixel;
    ximage->red_mask=header.red_mask;
    ximage->green_mask=header.green_mask;
    ximage->blue_mask=header.blue_mask;
    /*
      XImage uses signed integers rather than unsigned.  Check for
      overflow due to assignment.
    */
    if (ximage->width < 0 ||
            ximage->height < 0 ||
            ximage->format < 0 ||
            ximage->byte_order < 0 ||
            ximage->bitmap_unit < 0 ||
            ximage->bitmap_bit_order < 0 ||
            ximage->bitmap_pad < 0 ||
            ximage->depth < 0 ||
            ximage->bytes_per_line < 0 ||
            ximage->bits_per_pixel < 0)
        ThrowXWDReaderException(CorruptImageError,ImproperImageHeader,image);
    /* Guard against buffer overflow in libX11. */
    if (ximage->bits_per_pixel > 32 || ximage->bitmap_unit > 32)
        ThrowXWDReaderException(CorruptImageError,ImproperImageHeader,image);
    status=XInitImage(ximage);
    if (status == False)
        ThrowXWDReaderException(CorruptImageError,UnrecognizedXWDHeader,image);
    image->columns=ximage->width;
    image->rows=ximage->height;
    if (!image_info->ping)
        if (CheckImagePixelLimits(image, exception) != MagickPass)
            ThrowXWDReaderException(ResourceLimitError,ImagePixelLimitExceeded,image);
    image->depth=8;
    if ((header.ncolors == 0U) ||
            ((ximage->red_mask != 0) ||
             (ximage->green_mask != 0) ||
             (ximage->blue_mask != 0)))
    {
        image->storage_class=DirectClass;
        if (!image_info->ping)
            if ((ximage->red_mask == 0) ||
                    (ximage->green_mask == 0) ||
                    (ximage->blue_mask == 0))
                ThrowXWDReaderException(CorruptImageError,ImproperImageHeader,image);
    }
    else
    {
        image->storage_class=PseudoClass;
    }
    image->colors=header.ncolors;
    if (!image_info->ping)
    {
        /*
          Read colormap.
        */
        colors=(XColor *) NULL;
        if (header.ncolors != 0)
        {
            XWDColor
            color;

            register long
            i;

            length=(size_t) header.ncolors;
            if (length > ((~0UL)/sizeof(*colors)))
                ThrowXWDReaderException(CorruptImageError,ImproperImageHeader,image);
            colors=MagickAllocateArray(XColor *,length,sizeof(XColor));
            if (colors == (XColor *) NULL)
                ThrowXWDReaderException(ResourceLimitError,MemoryAllocationFailed,
                                        image);
            for (i=0; i < (long) header.ncolors; i++)
            {
                count=ReadBlob(image,sz_XWDColor,(char *) &color);
                if (count != sz_XWDColor)
                    ThrowXWDReaderException(CorruptImageError,
                                            UnableToReadColormapFromDumpFile,image);
                colors[i].pixel=color.pixel;
                colors[i].red=color.red;
                colors[i].green=color.green;
                colors[i].blue=color.blue;
                colors[i].flags=color.flags;
            }
            /*
              Ensure the header byte-order is most-significant byte first.
            */
            lsb_first=1;
            if (*(char *) &lsb_first)
                for (i=0; i < (long) header.ncolors; i++)
                {
                    MSBOrderLong((unsigned char *) &colors[i].pixel,
                                 sizeof(unsigned long));
                    MSBOrderShort((unsigned char *) &colors[i].red,
                                  3*sizeof(unsigned short));
                }
        }