static XImage * create_fallback (Display *dpy, Visual *visual, unsigned int depth, int format, XShmSegmentInfo *shm_info, unsigned int width, unsigned int height) { XImage *image = XCreateImage (dpy, visual, depth, format, 0, NULL, width, height, BitmapPad(dpy), 0); shm_info->shmid = -1; if (!image) { print_error (ENOMEM); } else { /* Sometimes the XImage data needs to be aligned, such as for SIMD (SSE2 in Fireworkx), or multithreading (AnalogTV). */ int error = thread_malloc ((void **)&image->data, dpy, image->height * image->bytes_per_line); if (error) { print_error (error); XDestroyImage (image); image = NULL; } else { memset (image->data, 0, image->height * image->bytes_per_line); } } return image; }
XImage *xNewImage (Display *dpy, unsigned long pmin, unsigned long pmax, int width, int height, unsigned char *bytes) /***************************************************************************** make a new image of pixels from bytes ****************************************************************************** Input: dpy display pointer pmin minimum pixel value (corresponding to byte=0) pmax maximum pixel value (corresponding to byte=255) width number of bytes in x dimension height number of bytes in y dimension bytes unsigned bytes to be mapped to an image ****************************************************************************** Author: Dave Hale, Colorado School of Mines, 06/08/90 *****************************************************************************/ { int scr=DefaultScreen(dpy); int i,j,k,line,iline,jline,widthpad; float base,scale; unsigned char map[256]; unsigned char *data; /* build map for translating bytes to pixels */ base = pmin+0.499; scale = (pmax-pmin)/255.0; for (i=0; i<=255; ++i) map[i] = base+i*scale; /* allocate memory for image data */ widthpad = (1+(width-1)/(BitmapPad(dpy)/8))*BitmapPad(dpy)/8; data = ealloc1(widthpad*height,sizeof(unsigned char)); /* translate bytes to pixels, padding scanlines as necessary */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) data[j] = map[bytes[i]]; for (j=jline+width,k=width; k<widthpad; ++j,++k) data[j] = data[jline+width-1]; } /* create and return image structure */ return XCreateImage(dpy,DefaultVisual(dpy,scr), DefaultDepth(dpy,scr),ZPixmap, 0,data,widthpad,height,BitmapPad(dpy),widthpad); }
/* * create or update the XImage so that it has the given width and height */ int ImageDisplay::update(int width, int height) { if (xImage_) { // reuse old XImage if it has the same dimensions if (xImage_->width == width && xImage_->height == height) { return TCL_OK; } destroyXImage(); xImage_ = NULL; } if (useXShm_) { // try to create a shared memory XImage if (updateShm(width, height) == TCL_OK) { usingXShm_ = 1; return TCL_OK; } usingXShm_ = 0; #ifdef DEBUG if (verbose_) cout << "Couldn't create X shared memory: reverting to standard X Image\n"; #endif } // fallback: create a normal XImage xImage_ = XCreateImage(display_, visual_, depth_, ZPixmap, 0, (char *) NULL, width, height, BitmapPad(display_), 0); // now allocate the image data (which must use the appropriate padding). xImage_->data = (char *)malloc(xImage_->bytes_per_line * height); if (xImage_->data == NULL) { #ifdef DEBUG if (verbose_) cout << "out of memory for XImage\n"; #endif XDestroyImage(xImage_); return error("not enough memory for an image this size"); } #ifdef XXXDEBUG if (verbose_) cout << "Not Sharing memory\n"; #endif return TCL_OK; }
/*- * initialise_image * * Initialise the image for drawing to * * - swirl is the swirl data */ static Bool initialise_image(Display * dpy, swirlstruct *sp) { unsigned int pad; int bytes_per_line; int image_depth = sp->rdepth; int data_depth = image_depth; unsigned char *image; /* image data */ /* On SGIs at least, using an XImage of depth 24 on a Visual of depth 24 * requires the XImage data to use 32 bits per pixel. I don't understand * how one is supposed to determine this -- maybe XListPixmapFormats? * But on systems that don't work this way, allocating 32 bpp instead of * 24 will be wasteful but non-fatal. -- jwz, 16-May-97. */ if (data_depth >= 24 && data_depth < 32) data_depth = 32; /* get the bitmap pad */ pad = BitmapPad(dpy); /* destroy the old image (destroy XImage and data) */ if (sp->ximage != NULL) (void) XDestroyImage(sp->ximage); /* how many bytes per line? (bits rounded up to pad) */ bytes_per_line = ((sp->width * data_depth + pad - 1) / pad) * (pad / 8); /* allocate space for the image */ if ((image = (unsigned char *) calloc(bytes_per_line * sp->height, 1)) == NULL) { return False; } /* create an ximage with this */ if ((sp->ximage = XCreateImage(dpy, sp->visual, image_depth, ZPixmap, 0, (char *) image, sp->width, sp->height, pad, bytes_per_line)) == None) { free(image); return False; } return True; }
void InitOutput(ScreenInfo *screenInfo, int argc, char *argv[]) { int i, j; xnestOpenDisplay(argc, argv); screenInfo->imageByteOrder = ImageByteOrder(xnestDisplay); screenInfo->bitmapScanlineUnit = BitmapUnit(xnestDisplay); screenInfo->bitmapScanlinePad = BitmapPad(xnestDisplay); screenInfo->bitmapBitOrder = BitmapBitOrder(xnestDisplay); screenInfo->numPixmapFormats = 0; for (i = 0; i < xnestNumPixmapFormats; i++) for (j = 0; j < xnestNumDepths; j++) if ((xnestPixmapFormats[i].depth == 1) || (xnestPixmapFormats[i].depth == xnestDepths[j])) { screenInfo->formats[screenInfo->numPixmapFormats].depth = xnestPixmapFormats[i].depth; screenInfo->formats[screenInfo->numPixmapFormats].bitsPerPixel = xnestPixmapFormats[i].bits_per_pixel; screenInfo->formats[screenInfo->numPixmapFormats].scanlinePad = xnestPixmapFormats[i].scanline_pad; screenInfo->numPixmapFormats++; break; } xnestWindowPrivateIndex = AllocateWindowPrivateIndex(); xnestGCPrivateIndex = AllocateGCPrivateIndex(); xnestFontPrivateIndex = AllocateFontPrivateIndex(); if (!xnestNumScreens) xnestNumScreens = 1; for (i = 0; i < xnestNumScreens; i++) AddScreen(xnestOpenScreen, argc, argv); xnestNumScreens = screenInfo->numScreens; xnestDoFullGeneration = xnestFullGeneration; }
/** Transfer \a pBits image to back-end server associated with \a * pDrawable's screen. If primitive subdivision optimization is * enabled, then only transfer the sections of \a pBits that are * visible (i.e., not-clipped) to the back-end server. */ void dmxPutImage(DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y, int w, int h, int leftPad, int format, char *pBits) { DMXScreenInfo *dmxScreen = &dmxScreens[pDrawable->pScreen->myNum]; dmxGCPrivPtr pGCPriv = DMX_GET_GC_PRIV(pGC); XImage *img; if (DMX_GCOPS_OFFSCREEN(pDrawable)) return; img = XCreateImage(dmxScreen->beDisplay, dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].visual, depth, format, leftPad, pBits, w, h, BitmapPad(dmxScreen->beDisplay), (format == ZPixmap) ? PixmapBytePad(w, depth) : BitmapBytePad(w+leftPad)); if (img) { Drawable draw; DMX_GCOPS_SET_DRAWABLE(pDrawable, draw); if (dmxSubdividePrimitives && pGC->pCompositeClip) { RegionPtr pSubImages; RegionPtr pClip; BoxRec box; BoxPtr pBox; int nBox; box.x1 = x; box.y1 = y; box.x2 = x + w; box.y2 = y + h; pSubImages = RegionCreate(&box, 1); pClip = RegionCreate(NullBox, 1); RegionCopy(pClip, pGC->pCompositeClip); RegionTranslate(pClip, -pDrawable->x, -pDrawable->y); RegionIntersect(pSubImages, pSubImages, pClip); nBox = RegionNumRects(pSubImages); pBox = RegionRects(pSubImages); while (nBox--) { XPutImage(dmxScreen->beDisplay, draw, pGCPriv->gc, img, pBox->x1 - box.x1, pBox->y1 - box.y1, pBox->x1, pBox->y1, pBox->x2 - pBox->x1, pBox->y2 - pBox->y1); pBox++; } RegionDestroy(pClip); RegionDestroy(pSubImages); } else { XPutImage(dmxScreen->beDisplay, draw, pGCPriv->gc, img, 0, 0, x, y, w, h); } XFree(img); /* Use XFree instead of XDestroyImage * because pBits is passed in from the * caller. */ dmxSync(dmxScreen, FALSE); } else { /* Error -- this should not happen! */ } }
Bool xnestRealizeCursor(DeviceIntPtr pDev, ScreenPtr pScreen, CursorPtr pCursor) { XImage *ximage; Pixmap source, mask; XColor fg_color, bg_color; unsigned long valuemask; XGCValues values; valuemask = GCFunction | GCPlaneMask | GCForeground | GCBackground | GCClipMask; values.function = GXcopy; values.plane_mask = AllPlanes; values.foreground = 1L; values.background = 0L; values.clip_mask = None; XChangeGC(xnestDisplay, xnestBitmapGC, valuemask, &values); source = XCreatePixmap(xnestDisplay, xnestDefaultWindows[pScreen->myNum], pCursor->bits->width, pCursor->bits->height, 1); mask = XCreatePixmap(xnestDisplay, xnestDefaultWindows[pScreen->myNum], pCursor->bits->width, pCursor->bits->height, 1); ximage = XCreateImage(xnestDisplay, xnestDefaultVisual(pScreen), 1, XYBitmap, 0, (char *)pCursor->bits->source, pCursor->bits->width, pCursor->bits->height, BitmapPad(xnestDisplay), 0); XPutImage(xnestDisplay, source, xnestBitmapGC, ximage, 0, 0, 0, 0, pCursor->bits->width, pCursor->bits->height); XFree(ximage); ximage = XCreateImage(xnestDisplay, xnestDefaultVisual(pScreen), 1, XYBitmap, 0, (char *)pCursor->bits->mask, pCursor->bits->width, pCursor->bits->height, BitmapPad(xnestDisplay), 0); XPutImage(xnestDisplay, mask, xnestBitmapGC, ximage, 0, 0, 0, 0, pCursor->bits->width, pCursor->bits->height); XFree(ximage); fg_color.red = pCursor->foreRed; fg_color.green = pCursor->foreGreen; fg_color.blue = pCursor->foreBlue; bg_color.red = pCursor->backRed; bg_color.green = pCursor->backGreen; bg_color.blue = pCursor->backBlue; xnestSetCursorPriv(pCursor, pScreen, malloc(sizeof(xnestPrivCursor))); xnestCursor(pCursor, pScreen) = XCreatePixmapCursor(xnestDisplay, source, mask, &fg_color, &bg_color, pCursor->bits->xhot, pCursor->bits->yhot); XFreePixmap(xnestDisplay, source); XFreePixmap(xnestDisplay, mask); return True; }
static void drawimage (Display *dpy, Drawable dbl, Region region, FGC fgc, Model *m, float fmin, float fmax, int style) { int scr=-1; int x,y,width,height; int i,j,k,line,iline,jline,widthpad; unsigned long pmin,pmax,p; float fx,fy,s,base,scale; Tri *t=NULL; TriAttributes *ta; XRectangle rect; XImage *image=NULL; int bitmap_pad=0; int nbpr=0; #if 0 /* OLD VERSION . See JG fix below */ unsigned char *data=NULL; scr=DefaultScreen(dpy); /* Kludge to fix problem with XCreateImage introduced in */ /* Xorg 7.0 update for security */ if (BitmapPad(dpy)>16) { bitmap_pad = 16; } else if (BitmapPad(dpy) < 16) { bitmap_pad = 8; } /* determine smallest box enclosing region */ XClipBox(region,&rect); x = rect.x; y = rect.y; width = rect.width; height = rect.height; if (width==0 || height==0) return; /* allocate memory for image data */ widthpad = (1+(width-1)/bitmap_pad)*bitmap_pad; nbpr = widthpad-1; data = alloc1(widthpad*height,sizeof(unsigned char)); if (data==NULL) err("width,widthpad,height = %d %d %d", width,widthpad,height); warn("nbpr = %d widthpad = %d height = %d bitmap_pad = %d ", nbpr,widthpad,height,bitmap_pad); /* determine min and max pixels from standard colormap */ pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(dpy); /* determine base and scale factor */ scale = (fmax!=fmin) ? ((float) (pmax-pmin))/(fmax-fmin) : 0.0; base = ((float) pmin)-fmin*scale; /* loop over scan lines */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; /* loop over pixels in scan line */ for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { /* determine float x and y coordinates */ if (style==XtcwpNORMAL) { fx = MapX(fgc,x+k); fy = MapY(fgc,y+line); } else { fx = MapY(fgc,y+line); fy = MapX(fgc,x+k); } /* determine sloth */ t = insideTriInModel(m,t,fx,fy); ta = (TriAttributes*)t->fa; s = ta->s00+fy*ta->dsdx+fx*ta->dsdz; /* convert to pixel and put in image */ p = (unsigned long) (base+s*scale); if (p<pmin) p = pmin; if (p>pmax) p = pmax; data[j] = (unsigned char) p; } for (j=jline+width,k=width; k<widthpad; ++j,++k) data[j] = data[jline+width-1]; } /* create, put, and destroy image */ image = XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int)ZPixmap, (int) 0, (char*)data, (unsigned int) widthpad, (unsigned int) height, (int) bitmap_pad, (int) nbpr); #else char *data=NULL; char noCmap; scr=DefaultScreen(dpy); /* JG: get bitmap_pad from X */ bitmap_pad = BitmapPad(dpy); /* determine smallest box enclosing region */ XClipBox(region,&rect); x = rect.x; y = rect.y; width = rect.width; height = rect.height; if (width==0 || height==0) return; /* allocate memory for image data */ widthpad = (1+(width-1)/bitmap_pad)*bitmap_pad; nbpr = widthpad-1; /* create image & determine alloc size from X */ image = XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int)ZPixmap, (int) 0, NULL , (unsigned int) widthpad, (unsigned int) height, (int) bitmap_pad, 0); /* JG XCreateImage(....,0) gets X to compute the size it needs. Then we alloc. */ image->data = (char *)calloc(image->bytes_per_line, image->height); if (image->data==NULL) err("width,widthpad,height = %d %d %d", width,widthpad,height); /* warn("nbpr = %d widthpad = %d height = %d bitmap_pad = %d ", nbpr,widthpad,height,bitmap_pad); */ /* determine min and max pixels from standard colormap */ pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(dpy); /* JGHACK ... When colormap fails, we get pmax=pmin=0 */ noCmap = (pmax==0 && pmin==0) ? 1:0; if (noCmap) { pmax = 255; warn("No colormap found...."); } /* ...JGHACK */ /* determine base and scale factor */ scale = (fmax!=fmin) ? ((float) (pmax-pmin))/(fmax-fmin) : 0.0; base = ((float) pmin)-fmin*scale; data = (char *)image->data ; /* loop over scan lines */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; /* loop over pixels in scan line */ for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { /* determine float x and y coordinates */ if (style==XtcwpNORMAL) { fx = MapX(fgc,x+k); fy = MapY(fgc,y+line); } else { fx = MapY(fgc,y+line); fy = MapX(fgc,x+k); } /* determine sloth */ t = insideTriInModel(m,t,fx,fy); ta = (TriAttributes*)t->fa; s = ta->s00+fy*ta->dsdx+fx*ta->dsdz; /* convert to pixel and put in image */ p = (unsigned long) (base+s*scale); if (p<pmin) p = pmin; if (p>pmax) p = pmax; if (noCmap) { /* JG. Can't get colormap. Might as well write RGB pixels as grayscale */ XPutPixel(image,k,line, p | (p<<8)| (p<<16)); } else { /* original */ /* data[j] = (unsigned char) p; */ XPutPixel(image,k,line,p); } } /* original. Not sure this is needed JG */ /* for (j=jline+width,k=width; k<widthpad; ++j,++k) data[j] = data[jline+width-1]; */ } #endif XPutImage(dpy,dbl,fgc->gc,image,0,0,x,y,image->width,image->height); /* free(data); */ XDestroyImage(image); }
int main(int argc, char **argv) { Atom atomWMDeleteWindow; int screenNumber; Screen *screen; Window window; XWindowAttributes windowAttributes; Colormap colormap; PaletteInfo paletteInfo; Image image; XImage *xImage; int x, y; int captureFrame; XEvent event; bool sizeChanged; // ProgramExit initialization display = NULL; v4l = -1; captureBuf = NULL; on_exit(ProgramExit, NULL); // Get command line options magnification = 1; if (argc > 1) { magnification = atoi(argv[1]); } // end if magnification = max(1, magnification); printf("Magnification is %i\n", magnification); // Open display if ((display = XOpenDisplay(NULL)) == NULL) { // NULL for DISPLAY printf("Error: XOpenDisplay() failed\n"); exit(1); } // end if screenNumber = DefaultScreen(display); screen = XScreenOfDisplay(display, screenNumber); // Obtain WM protocols atom for ClientMessage exit event if ((atomWMDeleteWindow = XInternAtom(display, AtomWMDeleteWindowName, True)) == None) { printf("Error: %s atom does not exist\n", AtomWMDeleteWindowName); exit(1); } // end if // Create window, inheriting depth and visual from root window window = XCreateSimpleWindow( display, RootWindowOfScreen(screen), 0, // x 0, // y 640, // width 480, // height 0, // border width BlackPixelOfScreen(screen), // border BlackPixelOfScreen(screen) // background ); XStoreName(display, window, "V4L RGB Test"); XGetWindowAttributes(display, window, &windowAttributes); if (((windowAttributes.depth == 8) && (windowAttributes.visual->c_class != PseudoColor)) || ((windowAttributes.depth > 8) && (windowAttributes.visual->c_class != TrueColor))) { printf("Error: Visual not supported\n"); exit(1); } // end if // Create PseudoColor HI240 colormap, if needed if (windowAttributes.depth == 8) { colormap = XCreateColormap(display, window, windowAttributes.visual, AllocAll); paletteInfo.display = display; paletteInfo.colormap = colormap; Hi240BuildPalette((ulong) 0x10000, (Hi240StorePaletteEntry *) StoreColormapEntry, &paletteInfo); XSetWindowColormap(display, window, colormap); } // end if // Create image if (image.Create( display, window, // Defines visual, depth MaxImageWidth, MaxImageHeight, True // MITSHM ) < 0) { printf("Error: image.Create() failed\n"); exit(1); } // end if image.Clear(); #if (1) printf("\nDisplay:\n"); printf("Image byte order = %s\n", ByteOrderName(ImageByteOrder(display))); printf("Bitmap unit = %i\n", BitmapUnit(display)); printf("Bitmap bit order = %s\n", ByteOrderName(BitmapBitOrder(display))); printf("Bitmap pad = %i\n", BitmapPad(display)); printf("\nWindow:\n"); printf("Depth = %i\n", windowAttributes.depth); printf("Visual ID = 0x%02x\n", windowAttributes.visual->visualid); printf("Visual class = %s\n", VisualClassName(windowAttributes.visual->c_class)); printf("Red mask = 0x%08lx\n", windowAttributes.visual->red_mask); printf("Green mask = 0x%08lx\n", windowAttributes.visual->green_mask); printf("Blue mask = 0x%08lx\n", windowAttributes.visual->blue_mask); printf("Bits per R/G/B = %i\n", windowAttributes.visual->bits_per_rgb); // log2 # colors xImage = image.X(); printf("\nImage:\n"); printf("Image byte order = %s\n", ByteOrderName(xImage->byte_order)); printf("Bitmap unit = %i\n", xImage->bitmap_unit); printf("Bitmap bit order = %s\n", ByteOrderName(xImage->bitmap_bit_order)); printf("Bitmap pad = %i\n", xImage->bitmap_pad); printf("Depth = %i\n", xImage->depth); printf("Red mask = 0x%08lx\n", xImage->red_mask); printf("Green mask = 0x%08lx\n", xImage->green_mask); printf("Blue mask = 0x%08lx\n", xImage->blue_mask); printf("Bits per pixel = %i\n", xImage->bits_per_pixel); // ZPixmap printf("Bytes per line = %i\n", xImage->bytes_per_line); printf("IsShared = %s\n", image.IsShared() ? "True" : "False"); printf("HasSharedPixmap = %s\n", image.HasSharedPixmap() ? "True" : "False"); #endif // V4L stuff if ((v4l = open(BigPictureDevice, O_RDWR)) < 0) { printf("Error: Can't open %s: %s\n", BigPictureDevice, strerror(errno)); exit(1); } // end if if (V4LMGetMMInfo(v4l, &v4lMMInfo) < 0) { printf("Error: V4LMGetMMInfo: %s\n", strerror(errno)); exit(1); } // end if #if (0) printf("Capture buffer size = %i\n", v4lMMInfo.size); printf("Capture buffer frames = %i\n", v4lMMInfo.frames); #endif if (v4lMMInfo.frames < 2) { printf("Error: V4LMGetMMInfo: frames < 2\n"); exit(1); } // end if if ((captureBuf = (bits8 *) mmap(0, v4lMMInfo.size, PROT_READ | PROT_WRITE, MAP_SHARED, v4l, 0)) == MAP_FAILED) { printf("Error: mmap(): %s\n", strerror(errno)); exit(1); } // end if if (V4LSetSource(v4l, BigPictureCompositeSource, VIDEO_MODE_NTSC) < 0) { printf("Error: V4LSetSource: %s\n", strerror(errno)); exit(1); } // end if if (V4LGetCaps(v4l, &v4lCaps) < 0) { printf("Error: V4LGetCaps: %s\n", strerror(errno)); exit(1); } // end if // Select V4L RGB capture format to exactly match image/visual (no LUTs!) if ((captureFormat = XImageCaptureFormat(image.X())) < 0) { printf("Error: No match for visual/image\n"); exit(1); } // end if // Initialize capture size based on window size windowWidth = windowAttributes.width; windowHeight = windowAttributes.height;; WindowResize(v4l, windowWidth, windowHeight, magnification); // Does V4LMSetFormat(). // Initialize picture attributes to mid-range V4LSetBrightness(v4l, 65535 / 2); V4LSetContrast(v4l, 65535 / 2); V4LSetSaturation(v4l, 65535 / 2); V4LSetHue(v4l, 65535 / 2); // Ready to start: Display window, select events, and initiate capture sequence XMapRaised(display, window); XSetWMProtocols(display, window, &atomWMDeleteWindow, 1); XSelectInput(display, window, StructureNotifyMask | ExposureMask); captureFrame = 0; if (V4LMCapture(v4l, captureFrame) < 0) { printf("Error: V4LMCapture: %s\n", strerror(errno)); exit(1); } // end if while (1) { if (XPending(display) > 0) { XNextEvent(display, &event); switch (event.type) { case ClientMessage: // From WM if (event.xclient.data.l[0] == atomWMDeleteWindow) { exit(0); } // end if break; case ConfigureNotify: sizeChanged = false; if (event.xconfigure.width != windowWidth) { sizeChanged = true; windowWidth = event.xconfigure.width; } // end if if (event.xconfigure.height != windowHeight) { sizeChanged = true; windowHeight = event.xconfigure.height; } // end if if (sizeChanged) { image.Clear(); XClearWindow(display, window); WindowResize(v4l, windowWidth, windowHeight, magnification); } // end if break; case Expose: if (event.xexpose.count == 0) { Put(window, image); } // end if break; } // end switch } else { // Wait for this frame if (V4LMSync(v4l, captureFrame) < 0) { printf("Error: V4LMSync: %s\n", strerror(errno)); exit(1); } // end if // Start capture for next frame if (V4LMCapture(v4l, 1 - captureFrame) < 0) { printf("Error: V4LMCapture: %s\n", strerror(errno)); exit(1); } // end if Draw(image, captureBuf + v4lMMInfo.offsets[captureFrame], magnification); Put(window, image); captureFrame = 1 - captureFrame; // 0<->1 } // endif } // end while printf("Error: Fell out of event loop!\n"); exit(1); } // end main
XImage *xNewImage (Display *dpy /* display pointer */, unsigned long pmin /* minimum pixel value (corresponding to byte=0) */, unsigned long pmax /* maximum pixel value (corresponding to byte=255) */, int width /* number of bytes in x dimension */, int height /* number of bytes in y dimension */, float blank /* portion for blanking (0 to 1) */, unsigned char *bytes /* unsigned bytes to be mapped to an image */) /*< make a new image of pixels from bytes >*/ /****************************************************************************** Author: Dave Hale, Colorado School of Mines, 06/08/90 *****************************************************************************/ { int scr=DefaultScreen(dpy); int i,j,k,line,iline,jline,widthpad; float base,scale; unsigned long map[256],bkgnd; unsigned char *data; int byte_perpixel; unsigned int depth; XImage *xim; xim=(XImage *) NULL; depth=(unsigned int)DefaultDepth(dpy,scr); byte_perpixel=4; if(depth<=8) byte_perpixel=1; else if(depth<=16) byte_perpixel=2; /* else if(depth<=24) byte_perpixel=3;*/ /* build map for translating bytes to pixels */ base = ((double) pmin)+0.499; scale = ((double) (pmax-pmin))/255.0; for (i=0; i<=255; ++i){ map[i] = base+i*scale; } /* blanking */ bkgnd = (unsigned long) WhitePixel(dpy,scr); j = SF_MAX(0,SF_MIN(256,(int)(256*blank))); for (i = 0; i < j; i++) map[255-i] = bkgnd; /* allocate memory for image data */ widthpad = (1+(width-1)/(BitmapPad(dpy)/8))*BitmapPad(dpy)/8; data = (unsigned char*) sf_alloc(widthpad*height,byte_perpixel); xim=XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int) ZPixmap, (int) 0, (char *) data, (unsigned int) widthpad, (unsigned int) height, /* (int) BitmapPad(dpy), (int) widthpad*byte_perpixel */ 8,0); byte_perpixel=xim->bits_per_pixel/8; /* fprintf(stderr,"\nbyte_perpixel = %d, depth= %d\n", byte_perpixel,depth); */ /* translate bytes to pixels, padding scanlines as necessary */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { if(byte_perpixel==1) ((unsigned char *)data)[j] =(unsigned char)map[bytes[i]]; if(byte_perpixel==2) { int edn=xim->byte_order; if(edn==LSBFirst){ ((unsigned char *)data)[j*2+0] =(unsigned char)(truecolor_pixel[bytes[i]]); ((unsigned char *)data)[j*2+1] =(unsigned char)(truecolor_pixel[bytes[i]]>>8); }else{ ((unsigned char *)data)[j*2+0] =(unsigned char)(truecolor_pixel[bytes[i]]>>24); ((unsigned char *)data)[j*2+1] =(unsigned char)(truecolor_pixel[bytes[i]]>>16); } /*((unsigned short *)data)[j] =(unsigned short)(truecolor_pixel[bytes[i]]);*/ }
int XBitmapPad(Display *dpy) { return (BitmapPad(dpy)); }
static XImage *makeImage (Display *dpy, int width, int height, int nx, int ny, int interp, unsigned char *bytes) /***************************************************************************** Makes image from bytes ordered as left-to-right, top-to-bottom scanlines. *****************************************************************************/ { int widthpad; unsigned char *pixels; unsigned char *data; Colormap wcmap; XColor color; int i,j,k; int scr; int depth; unsigned long truecolor_pixel[256]; XImage *xim; int byte_perpixel; int line, iline,jline; int ih,half; Screen *scr1; # define RGB_BLACK {0x00, 0x00, 0x00} # define RGB_WHITE {0xff, 0xff, 0xff} # define RGB_GRAY {0x80, 0x80, 0x80} float c_rgb [3][3] = { RGB_BLACK, RGB_GRAY, RGB_WHITE }; half=128; byte_perpixel=4; scr1=XDefaultScreenOfDisplay(dpy); xim=(XImage *) NULL; scr=DefaultScreen(dpy); wcmap=DefaultColormapOfScreen(scr1); /* Build the 1st ramp */ for (ih = 0; ih < 128; ++ih) { color.red = c_rgb[0][0] + (c_rgb[1][0] - c_rgb[0][0]) * ((float) ih)/((float) half); color.green = c_rgb[0][1] + (c_rgb[1][1] - c_rgb[0][1]) * ((float) ih)/((float) half); color.blue = c_rgb[0][2] + (c_rgb[1][2] - c_rgb[0][2]) * ((float) ih)/((float) half); color.red *= 257.0; color.green *= 257.0; color.blue *= 257.0; color.flags = DoRed|DoGreen|DoBlue; XAllocColor(dpy,wcmap,&color); truecolor_pixel[ih]=(unsigned long )color.pixel; } /* Build the 2nd ramp */ for (ih=128; ih<256; ++ih) { color.red = c_rgb[1][0] + (c_rgb[2][0] - c_rgb[1][0]) * ((float) (ih-half))/((float) half); color.green = c_rgb[1][1] + (c_rgb[2][1] - c_rgb[1][1]) * ((float) (ih-half))/((float) half); color.blue = c_rgb[1][2] + (c_rgb[2][2] - c_rgb[1][2]) * ((float) (ih-half))/((float) half); color.red *= 257.0; color.green *= 257.0; color.blue *= 257.0; color.flags = DoRed|DoGreen|DoBlue; XAllocColor(dpy,wcmap,&color); truecolor_pixel[ih]=(unsigned long )color.pixel; } depth=(unsigned int)DefaultDepth(dpy,scr); if(depth<=8) byte_perpixel=1; else if(depth<=16) byte_perpixel=2; /* determine scanline padding and allocate memory for pixels */ widthpad = (1+(width-1)/(BitmapPad(dpy)/8))*BitmapPad(dpy)/8; pixels = ealloc1(widthpad*height,sizeof(unsigned char)); /* bilinearly interpolate bytes to pixels */ if (interp) intl2b(nx,1.0,0.0,ny,1.0,0.0,bytes, width,(float)(nx-1)/(float)(width-1),0.0, height,(float)(ny-1)/(float)(height-1),0.0,pixels); else intn2b(nx,1.0,0.0,ny,1.0,0.0,bytes, width,(float)(nx-1)/(float)(width-1),0.0, height,(float)(ny-1)/(float)(height-1),0.0,pixels); data = ealloc1(widthpad*height,byte_perpixel); xim=XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int) ZPixmap, (int) 0, (char *) data, (unsigned int) widthpad, (unsigned int) height, (int) BitmapPad(dpy), (int) widthpad*byte_perpixel); byte_perpixel=xim->bits_per_pixel/8; /* fprintf(stderr,"\nbyte_perpixel = %d, depth= %d\n", byte_perpixel,depth); */ /* translate bytes to pixels, padding scanlines as necessary */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { if(byte_perpixel==1) ((unsigned char *)data)[j] =(unsigned char)pixels[i]; if(byte_perpixel==2) { int edn=xim->byte_order; if(edn==LSBFirst){ ((unsigned char *)data)[j*2+0] =(unsigned char)(truecolor_pixel[pixels[i]]); ((unsigned char *)data)[j*2+1] =(unsigned char)(truecolor_pixel[pixels[i]]>>8); }else{ ((unsigned char *)data)[j*2+0] =(unsigned char)(truecolor_pixel[pixels[i]]>>24); ((unsigned char *)data)[j*2+1] =(unsigned char)(truecolor_pixel[pixels[i]]>>16); } /*((unsigned short *)data)[j] =(unsigned short)(truecolor_pixel[pixels[i]]);*/ /*((unsigned short *)data)[j] =(unsigned short)(truecolor_pixel[pixels[i]]);*/ }
int init_view_struct( ThreeDViewStruct *view_struct) { XWindowAttributes win_att; Display *dpy = XtDisplay(view_struct->canvas); Window win = XtWindow(view_struct->canvas); unsigned int widthp, heightp; Dimension win_width, win_height; WlzThreeDViewStruct *wlzViewStr = view_struct->wlzViewStr; float minz, maxz, z; Widget widget; WlzErrorNum errNum=WLZ_ERR_NONE; if( XGetWindowAttributes(dpy, win, &win_att) == 0 ) return( 1 ); /* initialise the 3D view structure */ errNum = WlzInit3DViewStruct(wlzViewStr, globals.obj); if( errNum != WLZ_ERR_NONE ){ MAPaintReportWlzError(globals.topl, "init_view_struct", errNum); return 1; } /* set the limits set the slider values here */ minz = wlzViewStr->minvals.vtZ; maxz = wlzViewStr->maxvals.vtZ; if( wlzViewStr->dist < wlzViewStr->minvals.vtZ ) wlzViewStr->dist = wlzViewStr->minvals.vtZ; if( wlzViewStr->dist > wlzViewStr->maxvals.vtZ ) wlzViewStr->dist = wlzViewStr->maxvals.vtZ; z = wlzViewStr->dist; HGU_XmSetSliderRange( view_struct->slider, minz, maxz ); HGU_XmSetSliderValue( view_struct->slider, z ); /* set the zeta slider */ if((widget = XtNameToWidget(view_struct->dialog, "*.zeta_slider"))){ HGU_XmSetSliderValue(widget, view_struct->wlzViewStr->zeta * 180.0 / WLZ_M_PI); } /* set up the ximage - note the internal ximage is 8 bit but we must honour the bitmap-pad for the display */ widthp = wlzViewStr->maxvals.vtX - wlzViewStr->minvals.vtX + 1; heightp = wlzViewStr->maxvals.vtY - wlzViewStr->minvals.vtY + 1; view_struct->ximage = XCreateImage(dpy, win_att.visual, 8, ZPixmap, 0, NULL, widthp, heightp, BitmapPad(dpy), 0); if(view_struct->ximage == NULL) { MAPaintReportWlzError(globals.topl, "init_Inview_struct", WLZ_ERR_PARAM_DATA); return 1; } /* resize the canvas */ win_width = widthp * wlzViewStr->scale; win_height = heightp * wlzViewStr->scale; XtVaSetValues(view_struct->canvas, XmNwidth, win_width, XmNheight, win_height, NULL); view_struct->display_list = 0; return( 0 ); }
main() { Display *dpy; Window root,win; Colormap cmap; XStandardColormap scmap; XColor color,junk; XImage *image; XEvent event; GC gc; int scr,i; unsigned long black,white,pmin,pmax; char *data; /* connect to X server */ dpy = XOpenDisplay(NULL); if ((dpy=XOpenDisplay(NULL))==NULL) { fprintf(stderr,"Cannot open display!\n"); exit(-1); } scr = DefaultScreen(dpy); root = RootWindow(dpy,scr); black = BlackPixel(dpy,scr); white = WhitePixel(dpy,scr); /* create and map window */ win = XCreateSimpleWindow(dpy,root,X,Y,WIDTH,HEIGHT,4,black,white); cmap = XtcwpCreateGrayColormap(dpy,win); XSetWindowColormap(dpy,win,cmap); XMapWindow(dpy,win); /* determine range of contiguous pixels from standard colormap */ if (!XtcwpCreateRGBDefaultMap(dpy,&scmap)) { fprintf(stderr,"Cannot create standard colormap!\n"); exit(-1); } pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(dpy); /* create image */ data = (char*)malloc(WIDTH*HEIGHT); for (i=0; i<WIDTH*HEIGHT; ++i) data[i] = pmin+(pmax-pmin)*(i%WIDTH)/WIDTH; image = XCreateImage(dpy,DefaultVisual(dpy,scr), DefaultDepth(dpy,scr),ZPixmap, 0,data,WIDTH,HEIGHT,BitmapPad(dpy),WIDTH); gc = XCreateGC(dpy,win,0,NULL); XAllocNamedColor(dpy,cmap,"red",&color,&junk); XSetForeground(dpy,gc,color.pixel); /* set event mask */ XSelectInput(dpy,win,ExposureMask); /* loop forever */ XPutImage(dpy,win,gc,image,0,0,0,0,WIDTH,HEIGHT); while(True) { XNextEvent(dpy,&event); while (XCheckTypedEvent(dpy,Expose,&event)); XPutImage(dpy,win,gc,image,0,0,0,0,WIDTH,HEIGHT); XDrawLine(dpy,win,gc,0,0,WIDTH,HEIGHT); } /* close display */ XCloseDisplay(dpy); }