imgj_imagedatafactory_kni.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 980 行 · 第 1/3 页
C
980 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */#include <stddef.h>#include <string.h>#include <sni.h>#include <commonKNIMacros.h>#include <midpUtilKni.h>#include <midpMalloc.h>#include <gxapi_constants.h>#include <imgapi_image.h>#include <img_errorcodes.h>#include <img_imagedata_load.h>#include <imgdcd_image_util.h>#define PIXEL imgdcd_pixel_type#define ALPHA imgdcd_alpha_type/** Convenenient for convert Java image object to screen buffer */#define getImageData(jimgData, width, height, pixelData, alphaData) \ get_imagedata(IMGAPI_GET_IMAGEDATA_PTR(jimgData), \ width, height, pixelData, alphaData)/** Convert 24-bit RGB color to 16bit (565) color */#define RGB24TORGB16(x) (((( x ) & 0x00F80000) >> 8) + \ ((( x ) & 0x0000FC00) >> 5) + \ ((( x ) & 0x000000F8) >> 3) )/** Convert 16-bit (565) color to 24-bit RGB color */#define RGB16TORGB24(x) ( ((x & 0x001F) << 3) | ((x & 0x001C) >> 2) |\ ((x & 0x07E0) << 5) | ((x & 0x0600) >> 1) |\ ((x & 0xF800) << 8) | ((x & 0xE000) << 3) )/** * Create native representation for a image. * * @param jimg Java Image ROM structure to convert from * @param sbuf pointer to Screen buffer structure to populate * @param g optional Graphics object for debugging clip code. * give NULL if don't care. * * @return the given 'sbuf' pointer for convenient usage, * or NULL if the image is null. */static int get_imagedata(const java_imagedata *img, int *width, int *height, PIXEL **pixelData, ALPHA **alphaData) { // NOTE: // Since this routine is called by every graphics operations // We use ROMStruct directly instead of macros // like JavaByteArray, etc, for max performance. // if (img == NULL) { return KNI_FALSE; } *width = img->width; *height = img->height; // Only use nativePixelData and nativeAlphaData if // pixelData is null if (img->pixelData != NULL) { *pixelData = (PIXEL *)&(img->pixelData->elements[0]); *alphaData = (img->alphaData != NULL) ? (ALPHA *)&(img->alphaData->elements[0]) : NULL; } else { *pixelData = (PIXEL *)img->nativePixelData; *alphaData = (ALPHA *)img->nativeAlphaData; } return KNI_TRUE;}/** * Decodes the given input data into a cache representation that can * be saved and loaded quickly. * The input data should be in a self-identifying format; that is, * the data must contain a description of the decoding process. * * @param srcBuffer input data to be decoded. * @param length length of the input data. * @param ret_dataBuffer pointer to the platform representation data that * be saved. * @param ret_length pointer to the length of the return data. * @return one of error codes: * MIDP_ERROR_NONE, * MIDP_ERROR_OUT_MEM, * MIDP_ERROR_UNSUPPORTED, * MIDP_ERROR_OUT_OF_RESOURCE, * MIDP_ERROR_IMAGE_CORRUPTED */MIDP_ERROR img_decode_data2cache(unsigned char* srcBuffer, unsigned int length, unsigned char** ret_dataBuffer, unsigned int* ret_length) { unsigned int pixelSize, alphaSize; imgdcd_image_format format; MIDP_ERROR err; int width, height; PIXEL *pixelData; ALPHA *alphaData; imgdcd_image_buffer_raw *rawBuffer; img_native_error_codes creationError = IMG_NATIVE_IMAGE_NO_ERROR; err = imgdcd_image_get_info(srcBuffer, length, &format, (unsigned int *)&width, (unsigned int *)&height); if (err != MIDP_ERROR_NONE) { return err; } pixelSize = sizeof(PIXEL) * width * height; alphaSize = sizeof(ALPHA) * width * height; switch (format) { case IMGDCD_IMAGE_FORMAT_JPEG: /* JPEG does not contain alpha data */ alphaSize = 0; /* Fall through */ case IMGDCD_IMAGE_FORMAT_PNG: /* Decode PNG/JPEG to screen buffer format */ rawBuffer = (imgdcd_image_buffer_raw *) midpMalloc(offsetof(imgdcd_image_buffer_raw, data)+pixelSize+alphaSize); if (rawBuffer == NULL) { return MIDP_ERROR_OUT_MEM; } pixelData = (PIXEL *)rawBuffer->data; if (format == IMGDCD_IMAGE_FORMAT_PNG) { alphaData = rawBuffer->data + pixelSize; rawBuffer->hasAlpha = imgdcd_decode_png(srcBuffer, length, width, height, (imgdcd_pixel_type *)pixelData, (imgdcd_alpha_type *)alphaData, &creationError); if (!rawBuffer->hasAlpha) { alphaData = NULL; alphaSize = 0; /* Exclude alpha data */ } } else { alphaData = NULL; rawBuffer->hasAlpha = KNI_FALSE; imgdcd_decode_jpeg(srcBuffer, length, width, height, (imgdcd_pixel_type *)pixelData, (imgdcd_alpha_type *)alphaData, &creationError); } if (IMG_NATIVE_IMAGE_NO_ERROR != creationError) { midpFree(rawBuffer); return MIDP_ERROR_IMAGE_CORRUPTED; } memcpy(rawBuffer->header, imgdcd_raw_header, 4); rawBuffer->width = width; /* Use default endian */ rawBuffer->height = height; /* Use default endian */ *ret_dataBuffer = (unsigned char *)rawBuffer; *ret_length = offsetof(imgdcd_image_buffer_raw, data)+pixelSize+alphaSize; return MIDP_ERROR_NONE; case IMGDCD_IMAGE_FORMAT_RAW: /* Already in screen buffer format, simply copy the data */ *ret_dataBuffer = (unsigned char *)midpMalloc(length); if (*ret_dataBuffer == NULL) { return MIDP_ERROR_OUT_MEM; } else { memcpy(*ret_dataBuffer, srcBuffer, length); *ret_length = length; return MIDP_ERROR_NONE; } default: return MIDP_ERROR_UNSUPPORTED; } /* switch (image_type) */}/** * Gets an ARGB integer array from this <tt>ImageData</tt>. The * array consists of values in the form of 0xAARRGGBB. * * @param imageData The ImageData to read the ARGB data from * @param rgbBuffer The target integer array for the ARGB data * @param offset Zero-based index of first ARGB pixel to be saved * @param scanlength Number of intervening pixels between pixels in * the same column but in adjacent rows * @param x The x coordinate of the upper left corner of the * selected region * @param y The y coordinate of the upper left corner of the * selected region * @param width The width of the selected region * @param height The height of the selected region */void imgj_get_argb(const java_imagedata * srcImageDataPtr, jint * rgbBuffer, jint offset, jint scanlength, jint x, jint y, jint width, jint height, img_native_error_codes * errorPtr) { int srcWidth, srcHeight; PIXEL *srcPixelData; ALPHA *srcAlphaData; if (get_imagedata(srcImageDataPtr, &srcWidth, &srcHeight, &srcPixelData, &srcAlphaData) == KNI_TRUE) { // rgbData[offset + (a - x) + (b - y) * scanlength] = P(a, b); // P(a, b) = rgbData[offset + (a - x) + (b - y) * scanlength] // x <= a < x + width // y <= b < y + height int a, b, pixel, alpha; if (srcAlphaData != NULL) { for (b = y; b < y + height; b++) { for (a = x; a < x + width; a++) { pixel = srcPixelData[b*srcWidth + a]; alpha = srcAlphaData[b*srcWidth + a]; rgbBuffer[offset + (a - x) + (b - y) * scanlength] = (alpha << 24) + RGB16TORGB24(pixel); } } } else { for (b = y; b < y + height; b++) { for (a = x; a < x + width; a++) { pixel = srcPixelData[b*srcWidth + a]; rgbBuffer[offset + (a - x) + (b - y) * scanlength] = RGB16TORGB24(pixel) | 0xFF000000; } } } } * errorPtr = IMG_NATIVE_IMAGE_NO_ERROR;}/** * Get pointer to internal buffer of Java byte array and * check that expected offset/length can be applied to the buffer * * @param byteArray Java byte array object to get buffer from * @param offset offset of the data needed in the buffer * @param length length of the data needed in the buffer * starting from the offset * @return pointer to the buffer, or NULL if offset/length are * are not applicable. */static unsigned char *get_java_byte_buffer(KNIDECLARGS jobject byteArray, int offset, int length) { unsigned char *buffer = (unsigned char *)JavaByteArray(byteArray); int byteArrayLength = KNI_GetArrayLength(byteArray); if (offset < 0 || length < 0 || offset + length > byteArrayLength ) { KNI_ThrowNew(midpArrayIndexOutOfBoundsException, NULL); return NULL; } return buffer;}/** * Load Java ImageData instance with image data in RAW format. * Image data is provided either in native buffer, or in Java * byte array. Java array is used with more priority. * * @param imageData Java ImageData object to be loaded with image data * @param nativeBuffer pointer to native buffer with raw image data, * this parameter is alternative to javaBuffer * @param javaBuffer Java byte array with raw image data, * this parameter is alternative to nativeBuffer * @param offset offset of the raw image data in the buffer * @param length length of the raw image data in the buffer * starting from the offset * * @return KNI_TRUE in the case ImageData is successfully loaded with * raw image data, otherwise KNI_FALSE. */static int gx_load_imagedata_from_raw_buffer(KNIDECLARGS jobject imageData, unsigned char *nativeBuffer, jobject javaBuffer, int offset, int length) { int imageSize; int pixelSize, alphaSize; int status = KNI_FALSE; imgdcd_image_buffer_raw *rawBuffer = NULL;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?