imgj_imagedatafactory_kni.c

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

C
980
字号
    KNI_StartHandles(2);    KNI_DeclareHandle(pixelData);    KNI_DeclareHandle(alphaData);    do {        /** Check native and Java buffer parameters */        if (!KNI_IsNullHandle(javaBuffer)) {            if (nativeBuffer != NULL) {                REPORT_ERROR(LC_LOWUI,                    "Native and Java buffers should not be used together");                break;            }            nativeBuffer = get_java_byte_buffer(KNIPASSARGS                javaBuffer, offset, length);        }        if (nativeBuffer == NULL) {            REPORT_ERROR(LC_LOWUI,                "Null raw image buffer is provided");            break;        }        /** Check header */        rawBuffer = (imgdcd_image_buffer_raw *)(nativeBuffer + offset);        if (memcmp(rawBuffer->header, imgdcd_raw_header, 4) != 0) {            REPORT_ERROR(LC_LOWUI, "Unexpected raw image type");            break;        }        imageSize = rawBuffer->width * rawBuffer->height;        pixelSize = sizeof(PIXEL) * imageSize;        alphaSize = 0;        if (rawBuffer->hasAlpha) {            alphaSize = sizeof(ALPHA) * imageSize;        }        /** Check data array length */        if ((unsigned int)length !=            (offsetof(imgdcd_image_buffer_raw, data)                + pixelSize + alphaSize)) {            REPORT_ERROR(LC_LOWUI, "Raw image is corrupted");            break;        }        if (rawBuffer->hasAlpha) {            /* Has alpha */            SNI_NewArray(SNI_BYTE_ARRAY, alphaSize, alphaData);            if (KNI_IsNullHandle(alphaData)) {                KNI_ThrowNew(midpOutOfMemoryError, NULL);                break;            }            /** Link the new array into ImageData to protect if from GC */            midp_set_jobject_field(KNIPASSARGS imageData, "alphaData", "[B", alphaData);            /** New array allocation could cause GC and buffer moving */            if (!KNI_IsNullHandle(javaBuffer)) {                nativeBuffer = get_java_byte_buffer(KNIPASSARGS                    javaBuffer, offset, length);                rawBuffer = (imgdcd_image_buffer_raw *)                    (nativeBuffer + offset);            }            memcpy(JavaByteArray(alphaData),                rawBuffer->data + pixelSize, alphaSize);        }        SNI_NewArray(SNI_BYTE_ARRAY, pixelSize, pixelData);        if (KNI_IsNullHandle(pixelData)) {            KNI_ThrowNew(midpOutOfMemoryError, NULL);            break;        }            midp_set_jobject_field(KNIPASSARGS imageData,                                    "pixelData", "[B", pixelData);        /** New array allocation could cause GC and buffer moving */        if (!KNI_IsNullHandle(javaBuffer)) {            nativeBuffer = get_java_byte_buffer(KNIPASSARGS                javaBuffer, offset, length);            rawBuffer = (imgdcd_image_buffer_raw *)                (nativeBuffer + offset);        }            memcpy(JavaByteArray(pixelData), rawBuffer->data, pixelSize);        IMGAPI_GET_IMAGEDATA_PTR(imageData)->width =            (jint)rawBuffer->width;        IMGAPI_GET_IMAGEDATA_PTR(imageData)->height =            (jint)rawBuffer->height;        status = KNI_TRUE;    } while(0);    KNI_EndHandles();    return status;}/** * Load Java ImageData instance with image data in RAW format. * Image data is provided in native buffer. * * @param imageData Java ImageData object to be loaded with image data * @param buffer pointer to native buffer with raw image data * @param length length of the raw image data in the buffer * * @return KNI_TRUE in the case ImageData is successfully loaded with *    raw image data, otherwise KNI_FALSE. */int img_load_imagedata_from_raw_buffer(KNIDECLARGS jobject imageData,    unsigned char *buffer, int length) {    int status = KNI_FALSE;    KNI_StartHandles(1);    /* A handle for which KNI_IsNullHandle() check is true */    KNI_DeclareHandle(nullHandle);    status = gx_load_imagedata_from_raw_buffer(KNIPASSARGS                    imageData, buffer, nullHandle, 0, length);    KNI_EndHandles();    return status;}/** * Loads the <tt>ImageData</tt> with the given raw data array. * The array consists of raw image data including header info. * <p> * Java declaration: * <pre> *     loadRAW(Ljavax/microedition/lcdui/ImageData;[B)V * </pre> * * @param imageData The instance of ImageData to be loaded from array * @param imageBytes The array of raw image data * @param imageOffset the offset of the start of the data in the array * @param imageLength the length of the data in the array */KNIEXPORT KNI_RETURNTYPE_VOIDKNIDECL(javax_microedition_lcdui_ImageDataFactory_loadRAW) {    int offset = KNI_GetParameterAsInt(3);    int length = KNI_GetParameterAsInt(4);    KNI_StartHandles(2);    KNI_DeclareHandle(imageData);    KNI_DeclareHandle(imageBytes);    KNI_GetParameterAsObject(1, imageData);    KNI_GetParameterAsObject(2, imageBytes);    /* Load ImageData content from Java byte array with raw image data */    if (gx_load_imagedata_from_raw_buffer(KNIPASSARGS            imageData, NULL, imageBytes, offset, length) != KNI_TRUE) {        KNI_ThrowNew(midpIllegalArgumentException, NULL);    }    KNI_EndHandles();    KNI_ReturnVoid();}/** * Decodes the given byte array into the <tt>ImageData</tt>. * <p> * Java declaration: * <pre> *     loadPNG(Ljavax/microedition/lcdui/ImageData;[BII)Z * </pre> * * @param imageData the ImageData to load to * @param imageBytes A byte array containing the encoded PNG image data * @param offset The start of the image data within the byte array * @param length The length of the image data in the byte array * * @return true if there is alpha data */KNIEXPORT KNI_RETURNTYPE_BOOLEANKNIDECL(javax_microedition_lcdui_ImageDataFactory_loadPNG) {    int            length = KNI_GetParameterAsInt(4);    int            offset = KNI_GetParameterAsInt(3);    int            status = KNI_TRUE;    unsigned char* srcBuffer = NULL;    PIXEL *imgPixelData;    ALPHA *imgAlphaData;    java_imagedata * midpImageData = NULL;    /* variable to hold error codes */    img_native_error_codes creationError = IMG_NATIVE_IMAGE_NO_ERROR;    KNI_StartHandles(4);    KNI_DeclareHandle(alphaData);    KNI_DeclareHandle(pixelData);    KNI_DeclareHandle(pngData);    KNI_DeclareHandle(imageData);    KNI_GetParameterAsObject(2, pngData);    KNI_GetParameterAsObject(1, imageData);    midpImageData = IMGAPI_GET_IMAGEDATA_PTR(imageData);    /* assert     * (KNI_IsNullHandle(pngData))     */    srcBuffer = (unsigned char *)JavaByteArray(pngData);    /*     * JAVA_TRACE("loadPNG pngData length=%d  %x\n",     *            JavaByteArray(pngData)->length, srcBuffer);     */    unhand(jbyte_array, pixelData) = midpImageData->pixelData;    if (!KNI_IsNullHandle(pixelData)) {        imgPixelData = (PIXEL *)JavaByteArray(pixelData);        /*         * JAVA_TRACE("loadPNG pixelData length=%d\n",         *            JavaByteArray(pixelData)->length);         */    } else {        imgPixelData = NULL;    }    unhand(jbyte_array, alphaData) = midpImageData->alphaData;    if (!KNI_IsNullHandle(alphaData)) {        imgAlphaData = (ALPHA *)JavaByteArray(alphaData);        /*         * JAVA_TRACE("decodePNG alphaData length=%d\n",         *            JavaByteArray(alphaData)->length);         */    } else {        imgAlphaData = NULL;    }    /* assert     * (imgPixelData != NULL && imgAlphaData != NULL)     */    status = imgdcd_decode_png((srcBuffer + offset), length,                        midpImageData->width,                         midpImageData->width,                        (imgdcd_pixel_type *)imgPixelData,                        (imgdcd_alpha_type *)imgAlphaData,                        &creationError);    if (IMG_NATIVE_IMAGE_NO_ERROR != creationError) {        KNI_ThrowNew(midpIllegalArgumentException, NULL);    }    KNI_EndHandles();    KNI_ReturnBoolean(status);}/** * Decodes the given byte array into the <tt>ImageData</tt>. * <p> * Java declaration: * <pre> *     loadJPG(Ljavax/microedition/lcdui/ImageData;[BII)V * </pre> * * @param imageData the ImageData to load to * @param imageBytes A byte array containing the encoded JPEG image data * @param imageOffset The start of the image data within the byte array * @param imageLength The length of the image data in the byte array */KNIEXPORT KNI_RETURNTYPE_VOIDKNIDECL(javax_microedition_lcdui_ImageDataFactory_loadJPEG) {    int            length = KNI_GetParameterAsInt(4);    int            offset = KNI_GetParameterAsInt(3);    unsigned char* srcBuffer = NULL;    int imgWidth, imgHeight;    PIXEL *imgPixelData = NULL;    ALPHA *imgAlphaData = NULL;    java_imagedata * midpImageData = NULL;    /* variable to hold error codes */    img_native_error_codes creationError = IMG_NATIVE_IMAGE_NO_ERROR;    KNI_StartHandles(3);    /* KNI_DeclareHandle(alphaData); */    KNI_DeclareHandle(pixelData);    KNI_DeclareHandle(jpegData);    KNI_DeclareHandle(imageData);    KNI_GetParameterAsObject(2, jpegData);    KNI_GetParameterAsObject(1, imageData);    midpImageData = IMGAPI_GET_IMAGEDATA_PTR(imageData);    /* assert     * (KNI_IsNullHandle(jpegData))     */    srcBuffer = (unsigned char *)JavaByteArray(jpegData);    /*     * JAVA_TRACE("loadJPEG jpegData length=%d  %x\n",     *            JavaByteArray(jpegData)->length, srcBuffer);     */    imgWidth  = midpImageData->width;    imgHeight = midpImageData->height;    unhand(jbyte_array, pixelData) = midpImageData->pixelData;    if (!KNI_IsNullHandle(pixelData)) {        imgPixelData = (PIXEL *)JavaByteArray(pixelData);        /*         * JAVA_TRACE("loadJPEG pixelData length=%d\n",         *            JavaByteArray(pixelData)->length);         */    }    /* assert     * (imgPixelData != NULL)     */    imgdcd_decode_jpeg((srcBuffer + offset), length,                       imgWidth, imgHeight,                        (imgdcd_pixel_type *)imgPixelData,                       (imgdcd_alpha_type *)imgAlphaData,                        &creationError);    if (IMG_NATIVE_IMAGE_NO_ERROR != creationError) {        KNI_ThrowNew(midpIllegalArgumentException, NULL);    }    KNI_EndHandles();    KNI_ReturnVoid();}/**

⌨️ 快捷键说明

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