gxj_image.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 567 行 · 第 1/2 页

C
567
字号
 * @param clip                pointer to structure holding the dest clip *                              [x, y, width, height] * @param x_dest              x-coordinate in the destination * @param y_dest              y-coordinate in the destination * @param width               width of the region * @param height              height of the region * @param x_src               x-coord of the region * @param y_src               y-coord of the region * @param transform           transform to be applied to the region */voidcopy_imageregion(gxj_screen_buffer* src, gxj_screen_buffer* dest, const jshort *clip,		jint x_dest, jint y_dest,                jint width, jint height, jint x_src, jint y_src,                jint transform) {    int clipX1 = clip[0];    int clipY1 = clip[1];    int clipX2 = clip[2];    int clipY2 = clip[3];    int diff;    gxj_screen_buffer newSrc;    /*     * Don't let a bad clip origin into the clip code or the may be     * over or under writes of the destination buffer.     */    if (clipX1 < 0) {        clipX1 = 0;    }    if (clipY1 < 0) {        clipY1 = 0;    }    diff = clipX2 - dest->width;    if (diff > 0) {        clipX2 -= diff;    }    diff = clipY2 - dest->height;    if (diff > 0) {        clipY2 -= diff;    }    if (clipX1 >= clipX2 || clipY1 >= clipY2) {        /* Nothing to do. */        return;    }    /*     * Don't let any bad source numbers into the transform or copy,     * clip any pixels outside of the source buffer to prevent over or     * under reading the source buffer.     */    if (x_src < 0) {        width += x_src;        x_src = 0;    }    if (y_src < 0) {        height += y_src;        y_src = 0;    }    diff = (x_src + width) - src->width;    if (diff > 0) {        width -= diff;    }    diff = (y_src + height) - src->height;    if (diff > 0) {        height -= diff;    }    if (width <= 0 || height <= 0) {        /* Nothing to do. */        return;    }    /*     * check if the source and destination are the same image,     * or a transform is needed     */    newSrc.pixelData = NULL;    newSrc.alphaData = NULL;    if (dest == src || transform != 0) {        /*         * create a new image that is a copy of the region with transform         * applied         */        newSrc.pixelData =            (gxj_pixel_type *)midpMalloc(width * height * sizeof (gxj_pixel_type));        if (newSrc.pixelData == NULL) {            REPORT_ERROR(LC_LOWUI, "Out of memory error, copyImageRegion (pixelData)\n");             return ;         }        if (src->alphaData != NULL) {            newSrc.alphaData =                (gxj_alpha_type *)midpMalloc(width * height * sizeof (gxj_alpha_type));            if (newSrc.alphaData == NULL) {                midpFree(newSrc.pixelData);                REPORT_ERROR(LC_LOWUI, "Out of memory error, copyImageRegion (Alpha)\n");                return ;            }        }                create_transformed_imageregion(src, &newSrc, x_src, y_src, width,				       height, transform);                /* set the new image as the source */        src = &newSrc;        x_src = 0;        y_src = 0;        if (transform & TRANSFORM_INVERTED_AXES) {            // exchange the width and height            width = src->width;            height = src->height;        }    }    /* Apply the clip region to the destination region */    diff = clipX1 - x_dest;    if (diff > 0) {        x_src += diff;        width -= diff;        x_dest = clipX1;    }    diff = clipY1 - y_dest;    if (diff > 0) {        y_src += diff;        height -= diff;        y_dest = clipY1;    }    diff = (x_dest + width) - clipX2;    if (diff > 0) {        width -= diff;    }    diff = (y_dest + height) - clipY2;    if (diff > 0) {        height -= diff;    }    if (width > 0) {        int rowsCopied;        gxj_pixel_type* pDest = dest->pixelData + (y_dest * dest->width) + x_dest;        gxj_pixel_type* pSrc = src->pixelData + (y_src * src->width) + x_src;        gxj_pixel_type* limit;        int destWidthDiff = dest->width - width;        int srcWidthDiff = src->width - width;        int r1, g1, b1, a2, a3, r2, b2, g2;        if (src->alphaData != NULL) {            unsigned char *pSrcAlpha = src->alphaData + (y_src * src->width) + x_src;            /* copy the source to the destination */            for (rowsCopied = 0; rowsCopied < height; rowsCopied++) {                for (limit = pDest + width; pDest < limit; pDest++, pSrc++, pSrcAlpha++) {                    if ((*pSrcAlpha) == 0xFF) {                        CHECK_PTR_CLIP(dest, pDest);                        *pDest = *pSrc;                    }                    else if (*pSrcAlpha > 0x3) {                        r1 = (*pSrc >> 11);                        g1 = ((*pSrc >> 5) & 0x3F);                        b1 = (*pSrc & 0x1F);                        r2 = (*pDest >> 11);                        g2 = ((*pDest >> 5) & 0x3F);                        b2 = (*pDest & 0x1F);                        a2 = *pSrcAlpha >> 2;                        a3 = *pSrcAlpha >> 3;                        r1 = (r1 * a3 + r2 * (31 - a3)) >> 5;                        g1 = (g1 * a2 + g2 * (63 - a2)) >> 6;                        b1 = (b1 * a3 + b2 * (31 - a3)) >> 5;                        *pDest = (gxj_pixel_type)((r1 << 11) | (g1 << 5) | (b1));                    }                }                pDest += destWidthDiff;                pSrc += srcWidthDiff;                pSrcAlpha += srcWidthDiff;            }        } else {            /* copy the source to the destination */            for (rowsCopied = 0; rowsCopied < height; rowsCopied++) {                for (limit = pDest + width; pDest < limit; pDest++, pSrc++) {                    CHECK_PTR_CLIP(dest, pDest);                    *pDest = *pSrc;                }                pDest += destWidthDiff;                pSrc += srcWidthDiff;            }        }    }    if (newSrc.pixelData != NULL) {        midpFree(newSrc.pixelData);    }    if (newSrc.alphaData != NULL) {        midpFree(newSrc.alphaData);    }}/** * Draws the specified image at the given coordinates. * * <p>If the source image contains transparent pixels, the corresponding * pixels in the destination image must be left untouched.  If the source * image contains partially transparent pixels, a compositing operation * must be performed with the destination pixels, leaving all pixels of * the destination image fully opaque.</p> * * @param srcImageDataPtr the source image to be rendered * @param dstMutableImageDataPtr the mutable target image to be rendered to * @param clip the clip of the target image * @param x the x coordinate of the anchor point * @param y the y coordinate of the anchor point */void gx_render_image(const java_imagedata * srcImageDataPtr,		      const java_imagedata * dstMutableImageDataPtr,		      const jshort * clip,		      jint x, jint y) {  gxj_screen_buffer srcSBuf;  gxj_screen_buffer dstSBuf;  gxj_screen_buffer * psrcSBuf =    gxj_get_image_screen_buffer_impl(srcImageDataPtr, &srcSBuf, NULL);  gxj_screen_buffer * pdstSBuf =    getScreenBuffer(gxj_get_image_screen_buffer_impl(dstMutableImageDataPtr, &dstSBuf, NULL));  draw_image(psrcSBuf, pdstSBuf, clip, x, y);}/** * Renders the given region of the source image onto the destination image * at the given coordinates. * * @param srcImageDataPtr the source image to be rendered * @param dstMutableImageDataPtr the mutable destination image to be rendered to * @param x_src The x coordinate of the upper-left corner of the *              source region * @param y_src The y coordinate of the upper-left corner of the *              source region * @param width The width of the source region * @param height The height of the source region * @param x_dest The x coordinate of the upper-left corner of the destination region * @param y_dest The y coordinate of the upper-left corner of the destination region * @param transform The transform to apply to the selected region. */extern void gx_render_imageregion(const java_imagedata * srcImageDataPtr,				  const java_imagedata * dstMutableImageDataPtr,				  const jshort * clip,				  jint x_src, jint y_src,				  jint width, jint height,				   jint x_dest, jint y_dest,				   jint transform) {    gxj_screen_buffer srcSBuf;    gxj_screen_buffer dstSBuf;    gxj_screen_buffer * psrcSBuf =      gxj_get_image_screen_buffer_impl(srcImageDataPtr, &srcSBuf, NULL);    gxj_screen_buffer * pdstSBuf =      getScreenBuffer(gxj_get_image_screen_buffer_impl(dstMutableImageDataPtr,						       &dstSBuf, NULL));    draw_imageregion(psrcSBuf, pdstSBuf,		     clip,		     x_dest, y_dest,		     width, height,		     x_src, y_src,		     transform);}

⌨️ 快捷键说明

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