⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ximutil.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $XConsortium: XImUtil.c,v 11.51 91/07/23 12:02:13 rws Exp $ *//* Copyright    Massachusetts Institute of Technology    1986	*//*Permission to use, copy, modify, distribute, and sell this software and itsdocumentation for any purpose is hereby granted without fee, provided thatthe above copyright notice appear in all copies and that both thatcopyright notice and this permission notice appear in supportingdocumentation, and that the name of M.I.T. not be used in advertising orpublicity pertaining to distribution of the software without specific,written prior permission.  M.I.T. makes no representations about thesuitability of this software for any purpose.  It is provided "as is"without express or implied warranty.*/#include <X11/Xlibint.h>#include <X11/Xutil.h>#include <stdio.h>#if __STDC__#define Const const#else#define Const /**/#endifstatic unsigned char Const _lomask[0x09] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };static unsigned char Const _himask[0x09] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 };/* These two convenience routines return the scanline_pad and bits_per_pixel 	associated with a specific depth of ZPixmap format image for a 	display. */ _XGetScanlinePad(dpy, depth) Display *dpy; int depth; { 	register ScreenFormat *fmt = dpy->pixmap_format; 	register int i;  	for (i = dpy->nformats + 1; --i; ++fmt) 		if (fmt->depth == depth) 			return(fmt->scanline_pad);  	return(dpy->bitmap_pad); }  _XGetBitsPerPixel(dpy, depth) Display *dpy; int depth; { 	register ScreenFormat *fmt = dpy->pixmap_format; 	register int i;  	for (i = dpy->nformats + 1; --i; ++fmt) 		if (fmt->depth == depth) 			return(fmt->bits_per_pixel);  	return(depth); } /* * This module provides rudimentary manipulation routines for image data * structures.  The functions provided are: * *	XCreateImage	Creates a default XImage data structure *	_XDestroyImage	Deletes an XImage data structure *	_XGetPixel	Reads a pixel from an image data structure *	_XGetPixel32	Reads a pixel from a 32-bit Z image data structure *	_XGetPixel16	Reads a pixel from a 16-bit Z image data structure *	_XGetPixel8	Reads a pixel from an 8-bit Z image data structure *	_XGetPixel1	Reads a pixel from an 1-bit image data structure *	_XPutPixel	Writes a pixel into an image data structure *	_XPutPixel32	Writes a pixel into a 32-bit Z image data structure *	_XPutPixel16	Writes a pixel into a 16-bit Z image data structure *	_XPutPixel8	Writes a pixel into an 8-bit Z image data structure *	_XPutPixel1	Writes a pixel into an 1-bit image data structure *	_XSubImage	Clones a new (sub)image from an existing one *	_XSetImage	Writes an image data pattern into another image *	_XAddPixel	Adds a constant value to every pixel in an image * * The logic contained in these routines makes several assumptions about * the image data structures, and at least for current implementations * these assumptions are believed to be true.  They are:  * *	For all formats, bits_per_pixel is less than or equal to 32. *	For XY formats, bitmap_unit is always less than or equal to bitmap_pad. *	For XY formats, bitmap_unit is 8, 16, or 32 bits. *	For Z format, bits_per_pixel is 1, 4, 8, 16, 24, or 32 bits. */static _xynormalizeimagebits (bp, img)    register unsigned char *bp;    register XImage *img;{	register unsigned char c;	if (img->byte_order != img->bitmap_bit_order) {	    switch (img->bitmap_unit) {		case 16:		    c = *bp;		    *bp = *(bp + 1);		    *(bp + 1) = c;		    break;		case 32:		    c = *(bp + 3);		    *(bp + 3) = *bp;		    *bp = c;		    c = *(bp + 2);		    *(bp + 2) = *(bp + 1);		    *(bp + 1) = c;		    break;	    }	}	if (img->bitmap_bit_order == MSBFirst)	    _XReverse_Bytes (bp, img->bitmap_unit >> 3);}static _znormalizeimagebits (bp, img)    register unsigned char *bp;    register XImage *img;{	register unsigned char c;	switch (img->bits_per_pixel) {	    case 4:		*bp = ((*bp >> 4) & 0xF) | ((*bp << 4) & ~0xF);		break;	    case 16:		c = *bp;		*bp = *(bp + 1);		*(bp + 1) = c;		break;	    case 24:		c = *(bp + 2);		*(bp + 2) = *bp;		*bp = c;		break;	    case 32:		c = *(bp + 3);		*(bp + 3) = *bp;		*bp = c;		c = *(bp + 2);		*(bp + 2) = *(bp + 1);		*(bp + 1) = c;		break;	}}static _putbits (src, dstoffset, numbits, dst)    register char *src;	/* address of source bit string */    int dstoffset;	/* bit offset into destination; range is 0-31 */    register int numbits;/* number of bits to copy to destination */    register char *dst;	/* address of destination bit string */{	register unsigned char chlo, chhi;	int hibits;	dst = dst + (dstoffset >> 3);	dstoffset = dstoffset & 7;	hibits = 8 - dstoffset;	chlo = *dst & _lomask[dstoffset];	for (;;) {	    chhi = (*src << dstoffset) & _himask[dstoffset];	    if (numbits <= hibits) {		chhi = chhi & _lomask[dstoffset + numbits];		*dst = (*dst & _himask[dstoffset + numbits]) | chlo | chhi;		break;	    }	    *dst = chhi | chlo;	    dst++;	    numbits = numbits - hibits;	    chlo = (unsigned char) (*src & _himask[hibits]) >> hibits;	    src++;	    if (numbits <= dstoffset) {		chlo = chlo & _lomask[numbits];		*dst = (*dst & _himask[numbits]) | chlo;		break;	    }	    numbits = numbits - dstoffset;	}	}/* * Macros *  * The ROUNDUP macro rounds up a quantity to the specified boundary, * then truncates to bytes. * * The XYNORMALIZE macro determines whether XY format data requires  * normalization and calls a routine to do so if needed. The logic in * this module is designed for LSBFirst byte and bit order, so  * normalization is done as required to present the data in this order. * * The ZNORMALIZE macro performs byte and nibble order normalization if  * required for Z format data. * * The XYINDEX macro computes the index to the starting byte (char) boundary * for a bitmap_unit containing a pixel with coordinates x and y for image * data in XY format. *  * The ZINDEX macro computes the index to the starting byte (char) boundary  * for a pixel with coordinates x and y for image data in ZPixmap format. *  */#define ROUNDUP(nbytes, pad) ((((nbytes) + ((pad)-1)) / (pad)) * ((pad)>>3))#define XYNORMALIZE(bp, img) \    if ((img->byte_order == MSBFirst) || (img->bitmap_bit_order == MSBFirst)) \	_xynormalizeimagebits((unsigned char *)(bp), img)#define ZNORMALIZE(bp, img) \    if (img->byte_order == MSBFirst) \	_znormalizeimagebits((unsigned char *)(bp), img)#define XYINDEX(x, y, img) \    ((y) * img->bytes_per_line) + \    (((x) + img->xoffset) / img->bitmap_unit) * (img->bitmap_unit >> 3)#define ZINDEX(x, y, img) ((y) * img->bytes_per_line) + \    (((x) * img->bits_per_pixel) >> 3)/* * CreateImage *  * Allocates the memory necessary for an XImage data structure.  * Initializes the structure with "default" values and returns XImage.  *  */XImage *XCreateImage (dpy, visual, depth, format, offset, data, width, height,    xpad, image_bytes_per_line)    register Display *dpy;    register Visual *visual;    unsigned int depth;    int format;    int offset; /*How many pixels from the start of the data does the		picture to be transmitted start?*/    char *data;    unsigned int width;    unsigned int height;    int xpad;	    int image_bytes_per_line; 		/*How many bytes between a pixel on one line and the pixel with		  the same X coordinate on the next line? 0 means		  XCreateImage can calculate it.*/{	register XImage *image;	int bits_per_pixel = 1;	if ((image = (XImage *) Xcalloc(1, (unsigned) sizeof(XImage))) == NULL)	    return (XImage *) NULL;	image->width = width;	image->height = height;	image->format = format;	image->byte_order = dpy->byte_order;	image->bitmap_unit = dpy->bitmap_unit;	image->bitmap_bit_order = dpy->bitmap_bit_order;	if (visual != NULL) {		image->red_mask = visual->red_mask;		image->green_mask = visual->green_mask;		image->blue_mask = visual->blue_mask;	}	else {		image->red_mask = image->green_mask = image->blue_mask = 0;	}	if (format == ZPixmap) 	{	    bits_per_pixel = _XGetBitsPerPixel(dpy, (int) depth);	}	image->xoffset = offset;	image->bitmap_pad = xpad;	image->depth = depth; 	image->data = data;	/*	 * compute per line accelerator.	 */	if (image_bytes_per_line == 0)	{	if (format == ZPixmap)	    image->bytes_per_line = 	       ROUNDUP((bits_per_pixel * width), image->bitmap_pad);	else	    image->bytes_per_line =	        ROUNDUP((width + offset), image->bitmap_pad);	}	else image->bytes_per_line = image_bytes_per_line;	image->bits_per_pixel = bits_per_pixel;	image->obdata = NULL;	_XInitImageFuncPtrs (image);	return image;}/* * _DestroyImage * 	 * Deallocates the memory associated with the ximage data structure.  * this version handles the case of the image data being malloc'd * entirely by the library. */static int _XDestroyImage (ximage)    XImage *ximage;{	if (ximage->data != NULL) Xfree((char *)ximage->data);	if (ximage->obdata != NULL) Xfree((char *)ximage->obdata);	Xfree((char *)ximage);	return 1;}/* * GetPixel *  * Returns the specified pixel.  The X and Y coordinates are relative to  * the origin (upper left [0,0]) of the image.  The pixel value is returned * in normalized format, i.e. the LSB of the long is the LSB of the pixel. * The algorithm used is: * *	copy the source bitmap_unit or Zpixel into temp *	normalize temp if needed *	extract the pixel bits into return value * */static unsigned long Const low_bits_table[] = {    0x00000000, 0x00000001, 0x00000003, 0x00000007,    0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,    0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,    0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,    0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,    0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,    0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,    0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,    0xffffffff};static unsigned long _XGetPixel (ximage, x, y)    register XImage *ximage;    int x;    int y;{	unsigned long pixel, px;	register char *src;	register char *dst;	register int i, j;	int bits, nbytes;	long plane;     	if (ximage->depth == 1) {		src = &ximage->data[XYINDEX(x, y, ximage)];		dst = (char *)&pixel;		pixel = 0;		for (i = ximage->bitmap_unit >> 3; --i >= 0; ) *dst++ = *src++;		XYNORMALIZE(&pixel, ximage);          	bits = (x + ximage->xoffset) % ximage->bitmap_unit;		pixel = ((((char *)&pixel)[bits>>3])>>(bits&7)) & 1;	} else if (ximage->format == XYPixmap) {		pixel = 0;		plane = 0;		nbytes = ximage->bitmap_unit >> 3;		for (i = ximage->depth; --i >= 0; ) {		    src = &ximage->data[XYINDEX(x, y, ximage)+ plane];		    dst = (char *)&px;		    px = 0;		    for (j = nbytes; --j >= 0; ) *dst++ = *src++;		    XYNORMALIZE(&px, ximage);		    bits = (x + ximage->xoffset) % ximage->bitmap_unit;		    pixel = (pixel << 1) |			    (((((char *)&px)[bits>>3])>>(bits&7)) & 1);		    plane = plane + (ximage->bytes_per_line * ximage->height);		}	} else if (ximage->format == ZPixmap) {		src = &ximage->data[ZINDEX(x, y, ximage)];		dst = (char *)&px;		px = 0;		for (i = (ximage->bits_per_pixel + 7) >> 3; --i >= 0; )		    *dst++ = *src++;				ZNORMALIZE(&px, ximage);		pixel = 0;		for (i=sizeof(unsigned long); --i >= 0; )		    pixel = (pixel << 8) | ((unsigned char *)&px)[i];		if (ximage->bits_per_pixel == 4) {		    if (x & 1)			pixel >>= 4;		    else			pixel &= 0xf;		}	} else {		return 0; /* bad image */	}	if (ximage->bits_per_pixel == ximage->depth)	  return pixel;	else	  return (pixel & low_bits_table[ximage->depth]);}#ifndef WORD64static unsigned long byteorderpixel = MSBFirst << 24;#endifstatic unsigned long _XGetPixel32 (ximage, x, y)    register XImage *ximage;    int x;    int y;{	register unsigned char *addr;	unsigned long pixel;	if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 32)) {	    addr = &((unsigned char *)ximage->data)			[y * ximage->bytes_per_line + (x << 2)];#ifndef WORD64	    if (*((char *)&byteorderpixel) == ximage->byte_order)		pixel = *((unsigned long *)addr);	    else#endif	    if (ximage->byte_order == MSBFirst)		pixel = ((unsigned long)addr[0] << 24 |			 (unsigned long)addr[1] << 16 |			 (unsigned long)addr[2] << 8 |			 addr[3]);	    else		pixel = ((unsigned long)addr[3] << 24 |			 (unsigned long)addr[2] << 16 |			 (unsigned long)addr[1] << 8 |			 addr[0]);	    if (ximage->depth != 32)		pixel &= low_bits_table[ximage->depth];	    return pixel;	} else {	    _XInitImageFuncPtrs(ximage);	    return XGetPixel(ximage, x, y);	}}static unsigned long _XGetPixel16 (ximage, x, y)    register XImage *ximage;    int x;    int y;{	register unsigned char *addr;	unsigned long pixel;	if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 16)) {	    addr = &((unsigned char *)ximage->data)			[y * ximage->bytes_per_line + (x << 1)];	    if (ximage->byte_order == MSBFirst)		pixel = addr[0] << 8 | addr[1];	    else		pixel = addr[1] << 8 | addr[0];	    if (ximage->depth != 16)		pixel &= low_bits_table[ximage->depth];	    return pixel;	} else {	    _XInitImageFuncPtrs(ximage);	    return XGetPixel(ximage, x, y);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -