📄 ximutil.c
字号:
static unsigned long _XGetPixel8 (ximage, x, y) register XImage *ximage; int x; int y;{ unsigned char pixel; if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 8)) { pixel = ((unsigned char *)ximage->data) [y * ximage->bytes_per_line + x]; if (ximage->depth != 8) pixel &= low_bits_table[ximage->depth]; return pixel; } else { _XInitImageFuncPtrs(ximage); return XGetPixel(ximage, x, y); }}static unsigned long _XGetPixel1 (ximage, x, y) register XImage *ximage; int x; int y;{ unsigned char bit; int xoff, yoff; if ((ximage->depth == 1) && (ximage->byte_order == ximage->bitmap_bit_order)) { xoff = x + ximage->xoffset; yoff = y * ximage->bytes_per_line + (xoff >> 3); xoff &= 7; if (ximage->bitmap_bit_order == MSBFirst) bit = 0x80 >> xoff; else bit = 1 << xoff; return (ximage->data[yoff] & bit) ? 1 : 0; } else { _XInitImageFuncPtrs(ximage); return XGetPixel(ximage, x, y); }} /* * PutPixel * * Overwrites the specified pixel. The X and Y coordinates are relative to * the origin (upper left [0,0]) of the image. The input pixel value must be * in normalized format, i.e. the LSB of the long is the LSB of the pixel. * The algorithm used is: * * copy the destination bitmap_unit or Zpixel to temp * normalize temp if needed * copy the pixel bits into the temp * renormalize temp if needed * copy the temp back into the destination image data * */static int _XPutPixel (ximage, x, y, pixel) register XImage *ximage; int x; int y; unsigned long pixel;{ unsigned long px, npixel; register char *src; register char *dst; register int i; int j, nbytes; long plane; if (ximage->depth == 4) pixel &= 0xf; npixel = pixel; for (i=0, px=pixel; i<sizeof(unsigned long); i++, px>>=8) ((unsigned char *)&pixel)[i] = px; if (ximage->depth == 1) { src = &ximage->data[XYINDEX(x, y, ximage)]; dst = (char *)&px; px = 0; nbytes = ximage->bitmap_unit >> 3; for (i = nbytes; --i >= 0; ) *dst++ = *src++; XYNORMALIZE(&px, ximage); i = ((x + ximage->xoffset) % ximage->bitmap_unit); _putbits ((char *)&pixel, i, 1, (char *)&px); XYNORMALIZE(&px, ximage); src = (char *) &px; dst = &ximage->data[XYINDEX(x, y, ximage)]; for (i = nbytes; --i >= 0; ) *dst++ = *src++; } else if (ximage->format == XYPixmap) { plane = (ximage->bytes_per_line * ximage->height) * (ximage->depth - 1); /* do least signif plane 1st */ nbytes = ximage->bitmap_unit >> 3; for (j = ximage->depth; --j >= 0; ) { src = &ximage->data[XYINDEX(x, y, ximage) + plane]; dst = (char *) &px; px = 0; for (i = nbytes; --i >= 0; ) *dst++ = *src++; XYNORMALIZE(&px, ximage); i = ((x + ximage->xoffset) % ximage->bitmap_unit); _putbits ((char *)&pixel, i, 1, (char *)&px); XYNORMALIZE(&px, ximage); src = (char *)&px; dst = &ximage->data[XYINDEX(x, y, ximage) + plane]; for (i = nbytes; --i >= 0; ) *dst++ = *src++; npixel = npixel >> 1; for (i=0, px=npixel; i<sizeof(unsigned long); i++, px>>=8) ((unsigned char *)&pixel)[i] = px; 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; nbytes = (ximage->bits_per_pixel + 7) >> 3; for (i = nbytes; --i >= 0; ) *dst++ = *src++; ZNORMALIZE(&px, ximage); _putbits ((char *)&pixel, (x * ximage->bits_per_pixel) & 7, ximage->bits_per_pixel, (char *)&px); ZNORMALIZE(&px, ximage); src = (char *)&px; dst = &ximage->data[ZINDEX(x, y, ximage)]; for (i = nbytes; --i >= 0; ) *dst++ = *src++; } else { return 0; /* bad image */ } return 1;}static int _XPutPixel32 (ximage, x, y, pixel) register XImage *ximage; int x; int y; unsigned long pixel;{ unsigned char *addr; 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) *((unsigned long *)addr) = pixel; else#endif if (ximage->byte_order == MSBFirst) { addr[0] = pixel >> 24; addr[1] = pixel >> 16; addr[2] = pixel >> 8; addr[3] = pixel; } else { addr[3] = pixel >> 24; addr[2] = pixel >> 16; addr[1] = pixel >> 8; addr[0] = pixel; } return 1; } else { _XInitImageFuncPtrs(ximage); return XPutPixel(ximage, x, y, pixel); }}static int _XPutPixel16 (ximage, x, y, pixel) register XImage *ximage; int x; int y; unsigned long pixel;{ unsigned char *addr; 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) { addr[0] = pixel >> 8; addr[1] = pixel; } else { addr[1] = pixel >> 8; addr[0] = pixel; } return 1; } else { _XInitImageFuncPtrs(ximage); return XPutPixel(ximage, x, y, pixel); }}static int _XPutPixel8 (ximage, x, y, pixel) register XImage *ximage; int x; int y; unsigned long pixel;{ if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 8)) { ximage->data[y * ximage->bytes_per_line + x] = pixel; return 1; } else { _XInitImageFuncPtrs(ximage); return XPutPixel(ximage, x, y, pixel); }}static int _XPutPixel1 (ximage, x, y, pixel) register XImage *ximage; int x; int y; unsigned long pixel;{ unsigned char bit; int xoff, yoff; if ((ximage->depth == 1) && (ximage->byte_order == ximage->bitmap_bit_order)) { xoff = x + ximage->xoffset; yoff = y * ximage->bytes_per_line + (xoff >> 3); xoff &= 7; if (ximage->bitmap_bit_order == MSBFirst) bit = 0x80 >> xoff; else bit = 1 << xoff; if (pixel & 1) ximage->data[yoff] |= bit; else ximage->data[yoff] &= ~bit; return 1; } else { _XInitImageFuncPtrs(ximage); return XPutPixel(ximage, x, y, pixel); }}/* * SubImage * * Creates a new image that is a subsection of an existing one. * Allocates the memory necessary for the new XImage data structure. * Pointer to new image is returned. The algorithm used is repetitive * calls to get and put pixel. * */static XImage *_XSubImage (ximage, x, y, width, height) XImage *ximage; register int x; /* starting x coordinate in existing image */ register int y; /* starting y coordinate in existing image */ unsigned int width; /* width in pixels of new subimage */ unsigned int height;/* height in pixels of new subimage */{ register XImage *subimage; int dsize; register int row, col; register unsigned long pixel; char *data; if ((subimage = (XImage *) Xcalloc (1, sizeof (XImage))) == NULL) return (XImage *) NULL; subimage->width = width; subimage->height = height; subimage->xoffset = 0; subimage->format = ximage->format; subimage->byte_order = ximage->byte_order; subimage->bitmap_unit = ximage->bitmap_unit; subimage->bitmap_bit_order = ximage->bitmap_bit_order; subimage->bitmap_pad = ximage->bitmap_pad; subimage->bits_per_pixel = ximage->bits_per_pixel; subimage->depth = ximage->depth; /* * compute per line accelerator. */ if (subimage->format == ZPixmap) subimage->bytes_per_line = ROUNDUP(subimage->bits_per_pixel * width, subimage->bitmap_pad); else subimage->bytes_per_line = ROUNDUP(width, subimage->bitmap_pad); subimage->obdata = NULL; _XInitImageFuncPtrs (subimage); dsize = subimage->bytes_per_line * height; if (subimage->format == XYPixmap) dsize = dsize * subimage->depth; if (((data = Xcalloc (1, (unsigned) dsize)) == NULL) && (dsize > 0)) { Xfree((char *) subimage); return (XImage *) NULL; } subimage->data = data; /* * Test for cases where the new subimage is larger than the region * that we are copying from the existing data. In those cases, * copy the area of the existing image, and allow the "uncovered" * area of new subimage to remain with zero filled pixels. */ if (height > ximage->height - y ) height = ximage->height - y; if (width > ximage->width - x ) width = ximage->width - x; for (row = y; row < (y + height); row++) { for (col = x; col < (x + width); col++) { pixel = XGetPixel(ximage, col, row); XPutPixel(subimage, (col - x), (row - y), pixel); } } return subimage;}/* * SetImage * * Overwrites a section of one image with all of the data from another. * If the two images are not of the same format (i.e. XYPixmap and ZPixmap), * the image data is converted to the destination format. The following * restrictions apply: * * 1. The depths of the source and destination images must be equal. * * 2. If the height of the source image is too large to fit between * the specified y starting point and the bottom of the image, * then scanlines are truncated on the bottom. * * 3. If the width of the source image is too large to fit between * the specified x starting point and the end of the scanline, * then pixels are truncated on the right. * * The images need not have the same bitmap_bit_order, byte_order, * bitmap_unit, bits_per_pixel, bitmap_pad, or xoffset. * */int _XSetImage (srcimg, dstimg, x, y) XImage *srcimg; register XImage *dstimg; register int x; register int y;{ register unsigned long pixel; register int row, col; int width, height, startrow, startcol; if (x < 0) { startcol = -x; x = 0; } else startcol = 0; if (y < 0) { startrow = -y; y = 0; } else startrow = 0; width = dstimg->width - x; if (srcimg->width < width) width = srcimg->width; height = dstimg->height - y; if (srcimg->height < height) height = srcimg->height; /* this is slow, will do better later */ for (row = startrow; row < height; row++) { for (col = startcol; col < width; col++) { pixel = XGetPixel(srcimg, col, row); XPutPixel(dstimg, x + col, y + row, pixel); } } return 1;}/* * AddPixel * * Adds a constant value to every pixel in a pixmap. * */static _XAddPixel (ximage, value) register XImage *ximage; register long value;{ register int x; register int y; if (!value) return; if (ximage->depth == 1) { /* The only value that we can add here to an XYBitmap * is one. Since 1 + value = ~value for one bit wide * data, we do this quickly by taking the ones complement * of the entire bitmap data (offset and pad included!). * Note that we don't need to be concerned with bit or * byte order at all. */ register unsigned char *dp = (unsigned char *) ximage->data; x = ximage->bytes_per_line * ximage->height; while (--x >= 0) { *dp = ~*dp; dp++; } } else if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 8)) { register unsigned char *dp = (unsigned char *) ximage->data; x = ximage->bytes_per_line * ximage->height; while (--x >= 0) *dp++ += value;#ifndef WORD64 } else if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 16) && (*((char *)&byteorderpixel) == ximage->byte_order)) { register unsigned short *dp = (unsigned short *) ximage->data; x = (ximage->bytes_per_line >> 1) * ximage->height; while (--x >= 0) *dp++ += value; } else if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 32) && (*((char *)&byteorderpixel) == ximage->byte_order)) { register unsigned long *dp = (unsigned long *) ximage->data; x = (ximage->bytes_per_line >> 2) * ximage->height; while (--x >= 0) *dp++ += value;#endif } else { for (y = ximage->height; --y >= 0; ) { for (x = ximage->width; --x >= 0; ) { register unsigned long pixel = XGetPixel(ximage, x, y); pixel = pixel + value; XPutPixel(ximage, x, y, pixel); } } }}/* * This routine initializes the image object function pointers. The * intent is to provide native (i.e. fast) routines for native format images * only using the generic (i.e. slow) routines when fast ones don't exist. * However, with the current rather botched external interface, clients may * have to mung image attributes after the image gets created, so the fast * routines always have to check to make sure the optimization is still * valid, and reinit the functions if not. */_XInitImageFuncPtrs (image) register XImage *image;{ image->f.create_image = XCreateImage; image->f.destroy_image = _XDestroyImage; if ((image->format == ZPixmap) && (image->bits_per_pixel == 8)) { image->f.get_pixel = _XGetPixel8; image->f.put_pixel = _XPutPixel8; } else if ((image->depth == 1) && (image->byte_order == image->bitmap_bit_order)) { image->f.get_pixel = _XGetPixel1; image->f.put_pixel = _XPutPixel1; } else if ((image->format == ZPixmap) && (image->bits_per_pixel == 32)) { image->f.get_pixel = _XGetPixel32; image->f.put_pixel = _XPutPixel32; } else if ((image->format == ZPixmap) && (image->bits_per_pixel == 16)) { image->f.get_pixel = _XGetPixel16; image->f.put_pixel = _XPutPixel16; } else { image->f.get_pixel = _XGetPixel; image->f.put_pixel = _XPutPixel; } image->f.sub_image = _XSubImage;/* image->f.set_image = _XSetImage;*/ image->f.add_pixel = _XAddPixel;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -