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

📄 images.c

📁 用于移动设备上的java虚拟机源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * @(#)images.c	1.53 02/11/07 @(#) * * Copyright (c) 1999-2002 Sun Microsystems, Inc.  All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */#include <stdio.h>#define NeedFunctionPrototypes 1#include <kni.h>#include <defaultLCDUI.h>#include <nativeGUI.h>#include <imageDecode.h>#include <midpMalloc.h>#include <midpServices.h>#include <images.h>typedef struct _mbs {    Pixmap        pixmap;    XImage        image;    unsigned char *imageMask; /* this serves as a bit mask and as an alpha			       * channel array depending on what kind of			       * information is in the image. these two			       * properties are mutually exclusive so this                               * is ok                               */    jint           width;    jint           height;    jint           mutable;    jbyte          prop;} myBitmapStruct;typedef struct _imgDst {    imageDstData   super;    myBitmapStruct *bitmap;    jint            cmap[256];    unsigned char  tmap[256];    jint            dataSize;    jboolean       mutable;    jboolean       hasColormap;    jboolean       hasTransMap;} _imageDstData, *_imageDstPtr;/* REMIND: this is 3:3:2.  Consider 6x6x6 + 16 grays instead. */#define GETRGBPIXEL(r, g, b) \    rgbTable[((r & 0xe0) >> 0) | ((g & 0xe0) >> 3) | ((b & 0xc0) >> 6)].pixel/*  * getPixelParts scales 16 bit xcolor componets for red, green, blue * to 8 bit values.  Thus the red/green/blue values returned in * xcolor correspond to 8 bit rgb values of the color displayed. */static voidgetPixelParts(long pixel, XColor *xcolor){    XColor *table;    int i;    switch (numColors) {        case 2:         case 4:        case 16:            table = grayTable;            break;        default:            table = rgbTable;            break;    }    for (i = 0; i < numColors; i++) {        if (pixel == table[i].pixel) {            switch (numColors) {                case 2:                    xcolor->red   = grayTable[i].red >> 7;                    xcolor->green = xcolor->red;                    xcolor->blue  = xcolor->red;                    return;                case 4:                    xcolor->red   = grayTable[i].red >> 6;                    xcolor->green = xcolor->red;                    xcolor->blue  = xcolor->red;                    return;                case 16:                    xcolor->red   = grayTable[i].red >> 4;                    xcolor->green = xcolor->red;                    xcolor->blue  = xcolor->red;                    return;                default:                    xcolor->red   = (rgbTable[i].red   >> 8) & 0xe0;                    xcolor->green = (rgbTable[i].green >> 8) & 0xe0;                    xcolor->blue  = (rgbTable[i].blue  >> 8) & 0xc0;                    return;            }        }    }    xcolor->red = xcolor->green = xcolor->blue = 0;}static intgetPixelValue(int r, int g, int b){    switch (numColors) {        case  2: return grayTable[r >> 7].pixel;        case  4: return grayTable[r >> 6].pixel;        case 16: return grayTable[r >> 4].pixel;        default: return GETRGBPIXEL(r, g, b);    }}static intgetColorPixel(int rgb, int gray, int isGray){    int r = (rgb >> 16) & 0xff;    int g = (rgb >> 8)  & 0xff;    int b = (rgb >> 0)  & 0xff;    return GETRGBPIXEL(r, g, b);}static intgetGrayPixel(int rgb, int gray, int isGray){    switch (numColors) {    case  2: return grayTable[gray >> 7].pixel;    case  4: return grayTable[gray >> 6].pixel;    case 16: return grayTable[gray >> 4].pixel;    default:        fprintf(stderr, "Unsupported number of gray levels -- %d\n",                numColors);    }    return 0;}#define NTSC(r, g, b) ((r*78 + g*150 + b*29) >> 8)intLCDUIgetPixel(int rgb, int gray, int isGray) {    return numColors < 256 ? getGrayPixel(rgb, gray, isGray) :                              getColorPixel(rgb, gray, isGray);}intLCDUIgetDisplayColor(int color) {    int r = (color >> 16) & 0xff;    int g = (color >> 8)  & 0xff;    int b = (color >> 0)  & 0xff;    int pix;    pix = getPixelValue(r, g, b);    /* the returned values are X's Xcolor.pixel       stored as BGR */    b = (pix >> 16) & 0xff;    g = (pix >> 8)  & 0xff;    r = (pix >> 0)  & 0xff;     pix = ((r << 16) | (g << 8) | b)          & 0xFFFFFF;    return (pix);}/*  * fill rgbBuffer with pixels in 0xAARRGGBB format  * assume that (*rgbBuffer) can move, as it is on the java heap * * implements this function: *   rgbData[offset + (a - x) + (b - y) * scanlength] = P(a, b) *       for *   x <= a < x + width *   y <= b < y + height */void LCDUIgetRGB(int** rgbBuffer, int offset, int scanLength,	    int x, int y, int width, int height, void *img){    int curX;    int curY;    int curOffset = offset; /* current offset in output array */    int maskOffset;    unsigned char alpha;    /*     * unsigned char red;     * unsigned char green;     * unsigned char blue;      */    int argb;    int _argb;    char alphaFlag = 0;    /* zero if we don't need to process 			      alpha channel values */    XColor color;    Pixmap *pmap;    XImage *image;        myBitmapStruct *p = (myBitmapStruct *)img;    pmap = &p->pixmap;    image = &p->image;    if (img == NULL) return;    if (p->prop == HAS_ALPHA && p->imageMask != NULL)	alphaFlag = 1;    if (p->mutable) {        int w = p->width, h = p->height;        image = XGetImage(display, *pmap, 0, 0, w, h, AllPlanes, ZPixmap);    }    for (curY = y; curY < y + height; curY++) {	for (curX = x; curX < x + width; curX++) {	  _argb	= XGetPixel(image, curX, curY);		color.blue  = (_argb >> 16) & 0xff;		color.green = (_argb >> 8) & 0xff;		color.red   = _argb & 0xff;	    if (alphaFlag) {		maskOffset = curY * p->width + curX;		alpha = (unsigned char) *(p->imageMask + maskOffset);	    } else { 		alpha = 255; /* default fully opaque */	    }	    argb = (alpha << 24) | (color.red << 16) |		(color.green << 8) | color.blue;	    (*rgbBuffer)[curOffset] = argb;	    curOffset++;	}	curOffset += (scanLength - width);    }    if (p->mutable) {        XDestroyImage(image);    }}static voidsetImageColormap(imageDstPtr self, long *map, int length){    _imageDstPtr p = (_imageDstPtr)self;    p->hasColormap = KNI_TRUE;    memcpy(p->cmap, map, length * sizeof(long));}static voidsetImageTransparencyMap(imageDstPtr self, unsigned char *map,                         int length, int palLength){    _imageDstPtr p = (_imageDstPtr)self->ptr;    /* we must have a palette, so assume that everything is opaque */    if (palLength >= 0 ) {         memset(p->tmap, 0xFF, palLength);    }    memcpy(p->tmap, map, length);    p->hasTransMap = KNI_TRUE;}static XPixmapFormatValues *getPixmapFormat(int bpp) {    static XPixmapFormatValues *pixmapFormats = NULL;    static int numFormats = 0;    int i;    if (pixmapFormats == NULL) {	pixmapFormats = XListPixmapFormats(display, &numFormats);    }    if (bpp > 1) {	bpp = DefaultDepthOfScreen(screen);    }    for (i = 0; i < numFormats; ++i) {	if (bpp == pixmapFormats[i].depth) {	    return pixmapFormats + i;	}    }    return NULL;}static voidsetImageSize(imageDstPtr self, int width, int height){    /* REMIND: we're not allowing for transparency here. */    /* REMIND: yes, we are :) */    int x, y;    XImage *img;    _imageDstPtr p = (_imageDstPtr)self;    int depth = DefaultDepthOfScreen(screen);    XPixmapFormatValues *format = getPixmapFormat(depth);    int bytesPerPixel = format->bits_per_pixel >> 3;    int bytesPerLine  = (width*bytesPerPixel + 3) & ~3;    p->dataSize = sizeof(XImage) + sizeof(Pixmap);    if (! p->mutable) {        p->dataSize += bytesPerLine*height;    }    p->bitmap->width = width;    p->bitmap->height = height;    p->bitmap->mutable = p->mutable;    p->bitmap->imageMask = NULL;    p->bitmap->prop = HAS_SOLID;    img = &p->bitmap->image;    if (p->mutable) {        p->bitmap->pixmap = XCreatePixmap(display, paintWindow,                                 width, height,                                  visualDepth);	XSetClipMask(display, gc, None);        XSetForeground(display, gc, getPixelValue(0xff, 0xff, 0xff));        XFillRectangle(display, p->bitmap->pixmap, gc, 0, 0, width, height);        img->width    = width;        img->height   = height;        return;    }    img->width            = width;    img->height           = height;    img->xoffset          = 0;    img->format           = ZPixmap;    img->byte_order       = ImageByteOrder(display);    img->bitmap_unit      = BitmapUnit(display);    img->bitmap_bit_order = BitmapBitOrder(display);    img->depth            = format->depth;    img->bits_per_pixel   = format->bits_per_pixel;    img->bitmap_pad       = format->scanline_pad;    img->bytes_per_line   = bytesPerLine;    img->data             = (char *)midpMalloc(bytesPerLine * height);    if (!XInitImage(img)) {        fprintf(stderr, "XInitImage returned zero\n");        fprintf(stderr, "width            = %d\n", img->width);        fprintf(stderr, "height           = %d\n", img->height);        fprintf(stderr, "xoffset          = %d\n", img->xoffset);        fprintf(stderr, "format           = %d\n", img->format);        fprintf(stderr, "byte_order       = %d\n", img->byte_order);        fprintf(stderr, "bitmap_unit      = %d\n", img->bitmap_unit);        fprintf(stderr, "bitmap_bit_order = %d\n", img->bitmap_bit_order);        fprintf(stderr, "depth            = %d\n", img->depth);        fprintf(stderr, "bits_per_pixel   = %d\n", img->bits_per_pixel);        fprintf(stderr, "bitmap_pad       = %d\n", img->bitmap_pad);        fprintf(stderr, "bytes_per_line   = %d\n", img->bytes_per_line);    }    for (y = 0; y < height; ++y) {        for (x = 0; x < width; ++x) {            XPutPixel(img, x, y, lightPixel.pixel);        }    }    return;}static voidcopyImagePixels(imageDstPtr self, void *sourceData){    int x, y;    _imageDstPtr p = (_imageDstPtr)self;    myBitmapStruct *bitmap = (myBitmapStruct *)sourceData;    Pixmap *source = &bitmap->pixmap;    XImage *srcImg = &bitmap->image;    #if 0    if (*source == None) {        /* we're copying an immutable image! */    } else {        int w = srcImg->width, h = srcImg->height;        srcImg = XGetImage(display, *source, 0, 0, w, h, AllPlanes, ZPixmap);    }#endif    int w = srcImg->width, h = srcImg->height;    if (bitmap->mutable) {        srcImg = XGetImage(display, *source, 0, 0, w, h, AllPlanes, ZPixmap);    }    /* memcpy seems like it would be the right idea, but if srcImg doesn't */    /* exactly match p->image, we would end up with problems.  So we do it */    /* the slow way instead.                                               */    for (y = 0; y < h; ++y) {        for (x = 0; x < w; ++x) {            XPutPixel(&p->bitmap->image, x, y, XGetPixel(srcImg, x, y));        }    }    if (bitmap->mutable) {        XDestroyImage(srcImg);    }#if 0    if (*source != None) {        XDestroyImage(srcImg);    }#endif}static voidcopyPixelsTransform(imageDstPtr self, void *sourceData,		    int nXOriginSrc, int nYOriginSrc,		    int nWidth, int nHeight,		    int transform){  _imageDstPtr destP     = (_imageDstPtr)self;  myBitmapStruct *sourceBitmap = (myBitmapStruct *)sourceData;  /* --- */  int imgLen;  int srcX;  int srcY;  int xStart;  int yStart;  int xIncr;  int yIncr;  int destX;  int destY;  int yCounter;  int xCounter;      int t_width;  int t_height;  /*scan length of the source image*/  int imageWidth;  /*number of rows of the source image*/  int imageHeight;  int srcImgLen;  /* --- */  imageWidth = sourceBitmap->width;  imageHeight = sourceBitmap->height;  destP->bitmap->prop = HAS_ALPHA;  /* --- */

⌨️ 快捷键说明

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