0N/A/*
2362N/A * Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <string.h>
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "awt_parseImage.h"
0N/A#include "imageInitIDs.h"
0N/A#include "sun_awt_image_ImageRepresentation.h"
0N/A
0N/Astatic int compareLUTs(unsigned int *lut1, int numLut1, int transIdx,
0N/A unsigned int *lut2, int numLut2, unsigned char *cvtLut,
0N/A int *retNumLut1, int *retTransIdx, int *jniFlagP);
0N/A
0N/Astatic int findIdx(unsigned int rgb, unsigned int *lut, int numLut1);
0N/A
0N/A#define ALPHA_MASK 0xff000000
0N/A#ifndef FALSE
0N/A# define FALSE 0
0N/A#endif
0N/A#ifndef TRUE
0N/A# define TRUE 1
0N/A#endif
0N/A
5896N/A#define CHECK_STRIDE(yy, hh, ss) \
5896N/A if ((ss) != 0) { \
5896N/A int limit = 0x7fffffff / ((ss) > 0 ? (ss) : -(ss)); \
5896N/A if (limit < (yy) || limit < ((yy) + (hh) - 1)) { \
5896N/A /* integer oveflow */ \
5896N/A return JNI_FALSE; \
5896N/A } \
5896N/A } \
5896N/A
5896N/A#define CHECK_SRC() \
5896N/A do { \
5896N/A int pixeloffset; \
5896N/A if (off < 0 || off >= srcDataLength) { \
5896N/A return JNI_FALSE; \
5896N/A } \
5896N/A CHECK_STRIDE(0, h, scansize); \
5896N/A \
5896N/A /* check scansize */ \
5896N/A pixeloffset = scansize * (h - 1); \
5896N/A if ((w - 1) > (0x7fffffff - pixeloffset)) { \
5896N/A return JNI_FALSE; \
5896N/A } \
5896N/A pixeloffset += (w - 1); \
5896N/A \
5896N/A if (off > (0x7fffffff - pixeloffset)) { \
5896N/A return JNI_FALSE; \
5896N/A } \
5896N/A } while (0) \
5896N/A
5896N/A#define CHECK_DST(xx, yy) \
5896N/A do { \
5896N/A int soffset = (yy) * sStride; \
5896N/A int poffset = (xx) * pixelStride; \
5896N/A if (poffset > (0x7fffffff - soffset)) { \
5896N/A return JNI_FALSE; \
5896N/A } \
5896N/A poffset += soffset; \
5896N/A if (dstDataOff > (0x7fffffff - poffset)) { \
5896N/A return JNI_FALSE; \
5896N/A } \
5896N/A poffset += dstDataOff; \
5896N/A \
5896N/A if (poffset < 0 || poffset >= dstDataLength) { \
5896N/A return JNI_FALSE; \
5896N/A } \
5896N/A } while (0) \
5896N/A
0N/Astatic jfieldID s_JnumSrcLUTID;
0N/Astatic jfieldID s_JsrcLUTtransIndexID;
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_image_ImageRepresentation_initIDs(JNIEnv *env, jclass cls) {
0N/A s_JnumSrcLUTID = (*env)->GetFieldID(env, cls, "numSrcLUT", "I");
0N/A s_JsrcLUTtransIndexID = (*env)->GetFieldID(env, cls, "srcLUTtransIndex",
0N/A "I");
0N/A}
0N/A
0N/A/*
0N/A * This routine is used to draw ICM pixels into a default color model
0N/A */
5896N/AJNIEXPORT jboolean JNICALL
0N/AJava_sun_awt_image_ImageRepresentation_setICMpixels(JNIEnv *env, jclass cls,
0N/A jint x, jint y, jint w,
0N/A jint h, jintArray jlut,
0N/A jbyteArray jpix, jint off,
0N/A jint scansize,
0N/A jobject jict)
0N/A{
0N/A unsigned char *srcData = NULL;
5896N/A jint srcDataLength;
0N/A int *dstData;
5896N/A jint dstDataLength;
5896N/A jint dstDataOff;
0N/A int *dstP, *dstyP;
0N/A unsigned char *srcyP, *srcP;
0N/A int *srcLUT = NULL;
0N/A int yIdx, xIdx;
0N/A int sStride;
0N/A int *cOffs;
0N/A int pixelStride;
0N/A jobject joffs = NULL;
0N/A jobject jdata = NULL;
0N/A
0N/A if (JNU_IsNull(env, jlut)) {
0N/A JNU_ThrowNullPointerException(env, "NullPointerException");
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (JNU_IsNull(env, jpix)) {
0N/A JNU_ThrowNullPointerException(env, "NullPointerException");
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A if (x < 0 || w < 1 || (0x7fffffff - x) < w) {
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A if (y < 0 || h < 1 || (0x7fffffff - y) < h) {
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A sStride = (*env)->GetIntField(env, jict, g_ICRscanstrID);
0N/A pixelStride = (*env)->GetIntField(env, jict, g_ICRpixstrID);
0N/A joffs = (*env)->GetObjectField(env, jict, g_ICRdataOffsetsID);
0N/A jdata = (*env)->GetObjectField(env, jict, g_ICRdataID);
0N/A
5896N/A if (JNU_IsNull(env, jdata)) {
5896N/A /* no destination buffer */
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A if (JNU_IsNull(env, joffs) || (*env)->GetArrayLength(env, joffs) < 1) {
5896N/A /* invalid data offstes in raster */
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A srcDataLength = (*env)->GetArrayLength(env, jpix);
5896N/A dstDataLength = (*env)->GetArrayLength(env, jdata);
5896N/A
5896N/A cOffs = (int *) (*env)->GetPrimitiveArrayCritical(env, joffs, NULL);
5896N/A if (cOffs == NULL) {
5896N/A JNU_ThrowNullPointerException(env, "Null channel offset array");
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A dstDataOff = cOffs[0];
5896N/A
5896N/A /* the offset array is not needed anymore and can be released */
5896N/A (*env)->ReleasePrimitiveArrayCritical(env, joffs, cOffs, JNI_ABORT);
5896N/A joffs = NULL;
5896N/A cOffs = NULL;
5896N/A
5896N/A /* do basic validation: make sure that offsets for
5896N/A * first pixel and for last pixel are safe to calculate and use */
5896N/A CHECK_STRIDE(y, h, sStride);
5896N/A CHECK_STRIDE(x, w, pixelStride);
5896N/A
5896N/A CHECK_DST(x, y);
5896N/A CHECK_DST(x + w -1, y + h - 1);
5896N/A
5896N/A /* check source array */
5896N/A CHECK_SRC();
5896N/A
0N/A srcLUT = (int *) (*env)->GetPrimitiveArrayCritical(env, jlut, NULL);
0N/A if (srcLUT == NULL) {
0N/A JNU_ThrowNullPointerException(env, "Null IndexColorModel LUT");
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
0N/A NULL);
0N/A if (srcData == NULL) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
0N/A JNU_ThrowNullPointerException(env, "Null data array");
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A dstData = (int *) (*env)->GetPrimitiveArrayCritical(env, jdata, NULL);
0N/A if (dstData == NULL) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
0N/A JNU_ThrowNullPointerException(env, "Null tile data array");
5896N/A return JNI_FALSE;
0N/A }
0N/A
5896N/A dstyP = dstData + dstDataOff + y*sStride + x*pixelStride;
0N/A srcyP = srcData + off;
0N/A for (yIdx = 0; yIdx < h; yIdx++, srcyP += scansize, dstyP+=sStride) {
0N/A srcP = srcyP;
0N/A dstP = dstyP;
0N/A for (xIdx = 0; xIdx < w; xIdx++, dstP+=pixelStride) {
0N/A *dstP = srcLUT[*srcP++];
0N/A }
0N/A }
0N/A
0N/A /* Release the locked arrays */
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
0N/A
5896N/A return JNI_TRUE;
0N/A}
0N/A
5896N/AJNIEXPORT jboolean JNICALL
0N/AJava_sun_awt_image_ImageRepresentation_setDiffICM(JNIEnv *env, jclass cls,
0N/A jint x, jint y, jint w,
0N/A jint h, jintArray jlut,
0N/A jint transIdx, jint numLut,
0N/A jobject jicm,
0N/A jbyteArray jpix, jint off,
0N/A jint scansize,
5896N/A jobject jbct, jint dstDataOff)
0N/A{
0N/A unsigned int *srcLUT = NULL;
0N/A unsigned int *newLUT = NULL;
0N/A int sStride;
0N/A int pixelStride;
0N/A int mapSize;
0N/A jobject jdata = NULL;
0N/A jobject jnewlut = NULL;
5896N/A jint srcDataLength;
5896N/A jint dstDataLength;
0N/A unsigned char *srcData;
0N/A unsigned char *dstData;
0N/A unsigned char *dataP;
0N/A unsigned char *pixP;
0N/A int i;
0N/A int j;
0N/A int newNumLut;
0N/A int newTransIdx;
0N/A int jniFlag = JNI_ABORT;
0N/A unsigned char *ydataP;
0N/A unsigned char *ypixP;
0N/A unsigned char cvtLut[256];
0N/A
0N/A if (JNU_IsNull(env, jlut)) {
0N/A JNU_ThrowNullPointerException(env, "NullPointerException");
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (JNU_IsNull(env, jpix)) {
0N/A JNU_ThrowNullPointerException(env, "NullPointerException");
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A if (x < 0 || w < 1 || (0x7fffffff - x) < w) {
5896N/A return JNI_FALSE;
0N/A }
0N/A
5896N/A if (y < 0 || h < 1 || (0x7fffffff - y) < h) {
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A
0N/A sStride = (*env)->GetIntField(env, jbct, g_BCRscanstrID);
0N/A pixelStride =(*env)->GetIntField(env, jbct, g_BCRpixstrID);
0N/A jdata = (*env)->GetObjectField(env, jbct, g_BCRdataID);
0N/A jnewlut = (*env)->GetObjectField(env, jicm, g_ICMrgbID);
0N/A mapSize = (*env)->GetIntField(env, jicm, g_ICMmapSizeID);
0N/A
1836N/A if (numLut < 0 || numLut > 256 || mapSize < 0 || mapSize > 256) {
1836N/A /* Ether old or new ICM has a palette that exceeds capacity
1836N/A of byte data type, so we have to convert the image data
1836N/A to default representation.
1836N/A */
5896N/A return JNI_FALSE;
5896N/A }
5896N/A
5896N/A if (JNU_IsNull(env, jdata)) {
5896N/A /* no destination buffer */
5896N/A return JNI_FALSE;
1836N/A }
5896N/A
5896N/A srcDataLength = (*env)->GetArrayLength(env, jpix);
5896N/A dstDataLength = (*env)->GetArrayLength(env, jdata);
5896N/A
5896N/A CHECK_STRIDE(y, h, sStride);
5896N/A CHECK_STRIDE(x, w, pixelStride);
5896N/A
5896N/A CHECK_DST(x, y);
5896N/A CHECK_DST(x + w -1, y + h - 1);
5896N/A
5896N/A /* check source array */
5896N/A CHECK_SRC();
5896N/A
0N/A srcLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jlut,
0N/A NULL);
0N/A if (srcLUT == NULL) {
0N/A /* out of memory error already thrown */
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A newLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jnewlut,
0N/A NULL);
0N/A if (newLUT == NULL) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
0N/A JNI_ABORT);
0N/A /* out of memory error already thrown */
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A newNumLut = numLut;
0N/A newTransIdx = transIdx;
0N/A if (compareLUTs(srcLUT, numLut, transIdx, newLUT, mapSize,
0N/A cvtLut, &newNumLut, &newTransIdx, &jniFlag) == FALSE) {
0N/A /* Need to convert to ICR */
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
0N/A JNI_ABORT);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jnewlut, newLUT, JNI_ABORT);
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /* Don't need these any more */
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, jniFlag);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jnewlut, newLUT, JNI_ABORT);
0N/A
0N/A if (newNumLut != numLut) {
0N/A /* Need to write back new number of entries in lut */
0N/A (*env)->SetIntField(env, cls, s_JnumSrcLUTID, newNumLut);
0N/A }
0N/A
0N/A if (newTransIdx != transIdx) {
0N/A (*env)->SetIntField(env, cls, s_JsrcLUTtransIndexID, newTransIdx);
0N/A }
0N/A
0N/A srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
0N/A NULL);
0N/A if (srcData == NULL) {
0N/A /* out of memory error already thrown */
5896N/A return JNI_FALSE;
0N/A }
0N/A
0N/A dstData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jdata,
0N/A NULL);
0N/A if (dstData == NULL) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
0N/A /* out of memory error already thrown */
5896N/A return JNI_FALSE;
0N/A }
0N/A
5896N/A ydataP = dstData + dstDataOff + y*sStride + x*pixelStride;
0N/A ypixP = srcData + off;
0N/A
0N/A for (i=0; i < h; i++) {
0N/A dataP = ydataP;
0N/A pixP = ypixP;
0N/A for (j=0; j < w; j++) {
0N/A *dataP = cvtLut[*pixP];
0N/A dataP += pixelStride;
0N/A pixP++;
0N/A }
0N/A ydataP += sStride;
0N/A ypixP += scansize;
0N/A }
0N/A
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
0N/A
5896N/A return JNI_TRUE;
0N/A}
0N/A
0N/Astatic int compareLUTs(unsigned int *lut1, int numLut1, int transIdx,
0N/A unsigned int *lut2, int numLut2, unsigned char *cvtLut,
0N/A int *retNumLut1, int *retTransIdx, int *jniFlagP)
0N/A{
0N/A int i;
0N/A int idx;
0N/A int newTransIdx = -1;
0N/A unsigned int rgb;
0N/A int changed = FALSE;
0N/A int maxSize = (numLut1 > numLut2 ? numLut1 : numLut2);
0N/A
0N/A *jniFlagP = JNI_ABORT;
0N/A
0N/A for (i=0; i < maxSize; i++) {
0N/A cvtLut[i] = i;
0N/A }
0N/A
0N/A for (i=0; i < numLut2; i++) {
0N/A /* If this slot in new palette is different from the
0N/A * same slot in current palette, then we try to find
0N/A * this color in other slots. On failure, add this color
0N/A * to current palette.
0N/A */
0N/A if ((i >= numLut1) ||
0N/A (lut1[i] != lut2[i]))
0N/A {
0N/A rgb = lut2[i];
0N/A /* Transparent */
0N/A if ((rgb & ALPHA_MASK) == 0) {
0N/A if (transIdx == -1) {
0N/A if (numLut1 < 256) {
0N/A cvtLut[i] = numLut1;
0N/A newTransIdx = i;
0N/A transIdx = i;
0N/A numLut1++;
0N/A changed = TRUE;
0N/A }
0N/A else {
0N/A return FALSE;
0N/A }
0N/A }
0N/A cvtLut[i] = transIdx;
0N/A }
0N/A else {
0N/A if ((idx = findIdx(rgb, lut1, numLut1)) == -1) {
0N/A if (numLut1 < 256) {
0N/A lut1[numLut1] = rgb;
0N/A cvtLut[i] = numLut1;
0N/A numLut1++;
0N/A changed = TRUE;
0N/A }
0N/A else {
0N/A /* Bad news... need to convert image */
0N/A return FALSE;
0N/A }
0N/A } else {
0N/A cvtLut[i] = idx;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (changed) {
0N/A *jniFlagP = 0;
0N/A *retNumLut1 = numLut1;
0N/A if (newTransIdx != -1) {
0N/A *retTransIdx = newTransIdx;
0N/A }
0N/A }
0N/A return TRUE;
0N/A}
0N/A
0N/Astatic int findIdx(unsigned int rgb, unsigned int *lut, int numLut) {
0N/A int i;
0N/A
0N/A if ((rgb&0xff000000)==0) {
0N/A for (i=0; i < numLut; i++) {
0N/A if ((lut[i]&0xff000000)==0) return i;
0N/A }
0N/A }
0N/A else {
0N/A for (i=0; i < numLut; i++) {
0N/A if (lut[i] == rgb) return i;
0N/A }
0N/A }
0N/A return -1;
0N/A}