Pixmap XCreateBitmapFromData( Display *display, Drawable d, _Xconst char *data, unsigned int width, unsigned int height) { XImage *ximage; GC gc; Pixmap pix; pix = Tk_GetPixmap(display, d, (int) width, (int) height, 1); gc = XCreateGC(display, pix, 0, NULL); if (gc == NULL) { return None; } ximage = XCreateImage(display, NULL, 1, XYBitmap, 0, (char*) data, width, height, 8, (width + 7) / 8); ximage->bitmap_bit_order = LSBFirst; _XInitImageFuncPtrs(ximage); TkPutImage(NULL, 0, display, pix, gc, ximage, 0, 0, 0, 0, width, height); ximage->data = NULL; XDestroyImage(ximage); XFreeGC(display, gc); return pix; }
static void LTSetupImageCache(void) { int i; XImage *Image; LTImageValue ImageValue; /* * The image cache associates an image name with an XImage pointer and * hot spot coordinates. Using the generic hash tables for this makes * this really easy. */ ImageCache = _LTHashTableCreate(NULL, NULL, LTHASH_ID_STRING); /* * Now we put the default images into the cache. I'm not using * _XmCreateImage() here as this insists on a display pointer. Instead * I'm using a self-made XCreateImage() so I don't need a display * pointer at all. */ for (i = 0; DefaultImages[i].ImageName; i++) { Image = (XImage *)XtCalloc(1, (unsigned)sizeof(XImage)); Image->width = 16; Image->height = 16; Image->format = XYBitmap; Image->red_mask = 0; Image->green_mask = 0; Image->blue_mask = 0; Image->xoffset = 0; Image->bitmap_pad = 8; Image->depth = 1; Image->data = (char *)DefaultImages[i].ImageBits; Image->bytes_per_line = (16 + 7) >> 3; Image->bits_per_pixel = 1; Image->byte_order = LSBFirst; Image->bitmap_unit = 8; Image->bitmap_bit_order = LSBFirst; _XInitImageFuncPtrs(Image); ImageValue = (LTImageValue) XtMalloc(sizeof(LTImageValueRec)); ImageValue->image = Image; ImageValue->hot_x = 0; ImageValue->hot_y = 0; ImageValue->undeletable = True; _LTHashTableAddItem(ImageCache, (LTHashItemID) DefaultImages[i].ImageName, (LTHashItemValue) ImageValue); } }
int appImgMakeImage( AppDrawingData * add, APP_IMAGE ** pPimage, int toWide, int toHigh, AppColors * ac, const AppBitmapImage * abi ) { Display * display= add->addDisplay; int screen= add->addScreen; int depth= DefaultDepth( display, screen ); const BitmapDescription * bdIn= &(abi->abiBitmap); const unsigned char * bufferIn= abi->abiBuffer; BitmapDescription bdOut; APP_IMAGE * xim; Visual * vis; int pad= 8; int col; unsigned int one= 1; int bitmapUnit= 0; int swapBitmapBytes= 0; int swapBitmapBits= 0; const int dither= 1; vis= DefaultVisual( display, screen ); bmInitDescription ( &bdOut ); bdOut.bdPixelsWide= toWide; bdOut.bdPixelsHigh= toHigh; bdOut.bdHasAlpha= 0; bdOut.bdXResolution= 1; bdOut.bdYResolution= 1; bdOut.bdUnit= BMunPIXEL; if ( ac->acVisualClass == TrueColor || ac->acVisualClass == DirectColor ) { bdOut.bdBytesPerRow= toWide; if ( depth > 8 ) { bdOut.bdBytesPerRow *= 2; } if ( depth > 16 ) { bdOut.bdBytesPerRow *= 2; } xim= XCreateImage( display, vis, depth, ZPixmap, 0, (char *)0, toWide, toHigh, pad, 0 ); if ( ! xim ) { LDEB(xim); return -1; } bdOut.bdBytesPerRow= xim->bytes_per_line; bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow; bdOut.bdBitsPerSample= depth/ 3; bdOut.bdSamplesPerPixel= 3; bdOut.bdBitsPerPixel= xim->bits_per_pixel; bdOut.bdColorEncoding= BMcoRGB; bdOut.bdColorCount= 0; } else{ switch( depth ) { case 1: xim= XCreateImage( display, vis, depth, XYPixmap, 0, (char *)0, toWide, toHigh, pad, 0 ); if ( ! xim ) { LDEB(xim); return -1; } if ( xim->bitmap_unit > 32 ) { LDEB(xim->bitmap_unit); return -1; } bitmapUnit= xim->bitmap_unit; if ( xim->byte_order == MSBFirst ) { if ( *((unsigned char *)&one) ) { swapBitmapBytes= 1; } else{ swapBitmapBytes= 0; } } else{ if ( *((unsigned char *)&one) ) { swapBitmapBytes= 0; } else{ swapBitmapBytes= 1; } } /* ? */ if ( xim->bitmap_bit_order == MSBFirst ) { swapBitmapBits= 0; } else{ swapBitmapBits= 1; } bdOut.bdBytesPerRow= xim->bytes_per_line; bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow; bdOut.bdBitsPerSample= 1; bdOut.bdSamplesPerPixel= 1; bdOut.bdBitsPerPixel= 1; bdOut.bdColorEncoding= BMcoBLACKWHITE; bdOut.bdColorCount= 0; break; case 8: xim= XCreateImage( display, vis, depth, ZPixmap, 0, (char *)0, toWide, toHigh, pad, 0 ); if ( ! xim ) { LDEB(xim); return -1; } bdOut.bdBytesPerRow= xim->bytes_per_line; bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow; bdOut.bdBitsPerSample= 8; bdOut.bdSamplesPerPixel= 3; bdOut.bdBitsPerPixel= 8; bdOut.bdColorEncoding= BMcoRGB8PALETTE; bdOut.bdColorCount= 0; break; case 16: bdOut.bdBytesPerRow= 4* ( ( 2* toWide+ 3 )/ 4 ); bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow; xim= XCreateImage( display, vis, depth, ZPixmap, 0, (char *)0, toWide, toHigh, pad, 0 ); if ( ! xim ) { LDEB(xim); return -1; } bdOut.bdBytesPerRow= xim->bytes_per_line; bdOut.bdBufferLength= toHigh* bdOut.bdBytesPerRow; bdOut.bdBitsPerSample= 8; bdOut.bdSamplesPerPixel= 3; bdOut.bdBitsPerPixel= 16; bdOut.bdColorEncoding= BMcoRGB8PALETTE; bdOut.bdColorCount= 0; break; case 32: case 24: default: LDEB(depth); return -1; } } /* 2 */ for ( col= 0; col < 64; col++ ) { int r, g, b; XColor xc; r= ( col & 0x30 ) << 2; g= ( col & 0x0c ) << 4; b= ( col & 0x03 ) << 6; if ( appColorRgb( &xc, ac, r, g, b ) ) { LDEB(col); return -1; } } if ( *((unsigned char *)&one) ) { if ( xim->byte_order != LSBFirst ) { xim->byte_order= LSBFirst; _XInitImageFuncPtrs( xim ); } } else{ if ( xim->byte_order != MSBFirst ) { xim->byte_order= MSBFirst; _XInitImageFuncPtrs( xim ); } } xim->data= malloc( bdOut.bdBufferLength ); if ( ! xim->data ) { LXDEB(bdOut.bdBufferLength,xim->data); XDestroyImage( xim ); return -1; } if ( bmFillImage( &(ac->acAllocator), bitmapUnit, swapBitmapBytes, swapBitmapBits, dither, (unsigned char *)xim->data, bufferIn, &bdOut, bdIn ) ) { LDEB(1); # ifdef USE_MECHECK if ( xim->data ) { free( xim->data ); xim->data= (char *)0; } # endif XDestroyImage( xim ); return -1; } *pPimage= xim; return 0; }