awt_ImageRep.c revision 0
342N/A/*
2655N/A * Copyright 1997-1998 Sun Microsystems, Inc. All Rights Reserved.
342N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
342N/A *
342N/A * This code is free software; you can redistribute it and/or modify it
342N/A * under the terms of the GNU General Public License version 2 only, as
342N/A * published by the Free Software Foundation. Sun designates this
342N/A * particular file as subject to the "Classpath" exception as provided
342N/A * by Sun in the LICENSE file that accompanied this code.
342N/A *
342N/A * This code is distributed in the hope that it will be useful, but WITHOUT
342N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
342N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
342N/A * version 2 for more details (a copy is included in the LICENSE file that
342N/A * accompanied this code).
342N/A *
342N/A * You should have received a copy of the GNU General Public License version
342N/A * 2 along with this work; if not, write to the Free Software Foundation,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
342N/A * CA 95054 USA or visit www.sun.com if you need additional information or
342N/A * have any questions.
342N/A */
1879N/A
1879N/A#include <string.h>
1879N/A
1879N/A#include "jni.h"
1879N/A#include "jni_util.h"
1879N/A#include "awt_parseImage.h"
1879N/A#include "imageInitIDs.h"
342N/A#include "sun_awt_image_ImageRepresentation.h"
342N/A
342N/Astatic int compareLUTs(unsigned int *lut1, int numLut1, int transIdx,
342N/A unsigned int *lut2, int numLut2, unsigned char *cvtLut,
342N/A int *retNumLut1, int *retTransIdx, int *jniFlagP);
342N/A
342N/Astatic int findIdx(unsigned int rgb, unsigned int *lut, int numLut1);
342N/A
342N/A#define ALPHA_MASK 0xff000000
342N/A#ifndef FALSE
342N/A# define FALSE 0
342N/A#endif
342N/A#ifndef TRUE
342N/A# define TRUE 1
342N/A#endif
342N/A
342N/Astatic jfieldID s_JnumSrcLUTID;
342N/Astatic jfieldID s_JsrcLUTtransIndexID;
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_sun_awt_image_ImageRepresentation_initIDs(JNIEnv *env, jclass cls) {
342N/A s_JnumSrcLUTID = (*env)->GetFieldID(env, cls, "numSrcLUT", "I");
342N/A s_JsrcLUTtransIndexID = (*env)->GetFieldID(env, cls, "srcLUTtransIndex",
342N/A "I");
342N/A}
342N/A
549N/A/*
342N/A * This routine is used to draw ICM pixels into a default color model
342N/A */
342N/AJNIEXPORT void JNICALL
342N/AJava_sun_awt_image_ImageRepresentation_setICMpixels(JNIEnv *env, jclass cls,
342N/A jint x, jint y, jint w,
342N/A jint h, jintArray jlut,
342N/A jbyteArray jpix, jint off,
549N/A jint scansize,
342N/A jobject jict)
342N/A{
342N/A unsigned char *srcData = NULL;
342N/A int *dstData;
342N/A int *dstP, *dstyP;
342N/A unsigned char *srcyP, *srcP;
342N/A int *srcLUT = NULL;
342N/A int yIdx, xIdx;
342N/A int sStride;
342N/A int *cOffs;
342N/A int pixelStride;
342N/A jobject joffs = NULL;
342N/A jobject jdata = NULL;
677N/A
677N/A if (JNU_IsNull(env, jlut)) {
342N/A JNU_ThrowNullPointerException(env, "NullPointerException");
342N/A return;
342N/A }
342N/A
342N/A if (JNU_IsNull(env, jpix)) {
342N/A JNU_ThrowNullPointerException(env, "NullPointerException");
342N/A return;
342N/A }
342N/A
342N/A sStride = (*env)->GetIntField(env, jict, g_ICRscanstrID);
342N/A pixelStride = (*env)->GetIntField(env, jict, g_ICRpixstrID);
342N/A joffs = (*env)->GetObjectField(env, jict, g_ICRdataOffsetsID);
342N/A jdata = (*env)->GetObjectField(env, jict, g_ICRdataID);
342N/A
942N/A srcLUT = (int *) (*env)->GetPrimitiveArrayCritical(env, jlut, NULL);
342N/A if (srcLUT == NULL) {
342N/A JNU_ThrowNullPointerException(env, "Null IndexColorModel LUT");
342N/A return;
342N/A }
342N/A
342N/A srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
342N/A NULL);
342N/A if (srcData == NULL) {
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
342N/A JNU_ThrowNullPointerException(env, "Null data array");
342N/A return;
342N/A }
342N/A
342N/A cOffs = (int *) (*env)->GetPrimitiveArrayCritical(env, joffs, NULL);
342N/A if (cOffs == NULL) {
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
342N/A JNU_ThrowNullPointerException(env, "Null channel offset array");
342N/A return;
342N/A }
342N/A
342N/A dstData = (int *) (*env)->GetPrimitiveArrayCritical(env, jdata, NULL);
342N/A if (dstData == NULL) {
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, joffs, cOffs, JNI_ABORT);
890N/A JNU_ThrowNullPointerException(env, "Null tile data array");
890N/A return;
890N/A }
890N/A
890N/A dstyP = dstData + cOffs[0] + y*sStride + x*pixelStride;
890N/A srcyP = srcData + off;
890N/A for (yIdx = 0; yIdx < h; yIdx++, srcyP += scansize, dstyP+=sStride) {
890N/A srcP = srcyP;
890N/A dstP = dstyP;
2648N/A for (xIdx = 0; xIdx < w; xIdx++, dstP+=pixelStride) {
2648N/A *dstP = srcLUT[*srcP++];
342N/A }
342N/A }
342N/A
342N/A /* Release the locked arrays */
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, joffs, cOffs, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
342N/A
342N/A}
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_sun_awt_image_ImageRepresentation_setBytePixels(JNIEnv *env, jclass cls,
677N/A jint x, jint y, jint w,
342N/A jint h, jbyteArray jpix,
342N/A jint off, jint scansize,
342N/A jobject jbct,
342N/A jint chanOffs)
342N/A{
342N/A int sStride;
342N/A int pixelStride;
342N/A jobject jdata;
342N/A unsigned char *srcData;
342N/A unsigned char *dstData;
342N/A unsigned char *dataP;
342N/A unsigned char *pixP;
342N/A int i;
342N/A int j;
342N/A
342N/A
342N/A if (JNU_IsNull(env, jpix)) {
342N/A JNU_ThrowNullPointerException(env, "NullPointerException");
342N/A return;
342N/A }
1531N/A
342N/A sStride = (*env)->GetIntField(env, jbct, g_BCRscanstrID);
342N/A pixelStride = (*env)->GetIntField(env, jbct, g_BCRpixstrID);
342N/A jdata = (*env)->GetObjectField(env, jbct, g_BCRdataID);
342N/A
342N/A srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
342N/A NULL);
342N/A if (srcData == NULL) {
1531N/A /* out of memory error already thrown */
1531N/A return;
2277N/A }
342N/A
342N/A dstData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jdata,
342N/A NULL);
342N/A if (dstData == NULL) {
342N/A /* out of memory error already thrown */
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
342N/A return;
342N/A }
342N/A
2754N/A dataP = dstData + chanOffs + y*sStride + x*pixelStride;
342N/A pixP = srcData + off;
1898N/A if (pixelStride == 1) {
1898N/A if (sStride == scansize && scansize == w) {
1898N/A memcpy(dataP, pixP, w*h);
1898N/A }
342N/A else {
342N/A for (i=0; i < h; i++) {
342N/A memcpy(dataP, pixP, w);
342N/A dataP += sStride;
342N/A pixP += scansize;
342N/A }
342N/A }
342N/A }
342N/A else {
342N/A unsigned char *ydataP = dataP;
342N/A unsigned char *ypixP = pixP;
342N/A
342N/A for (i=0; i < h; i++) {
342N/A dataP = ydataP;
1356N/A pixP = ypixP;
1356N/A for (j=0; j < w; j++) {
2753N/A *dataP = *pixP++;
2753N/A dataP += pixelStride;
2753N/A }
342N/A ydataP += sStride;
342N/A ypixP += scansize;
342N/A }
342N/A }
342N/A
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
342N/A
342N/A}
342N/A
342N/AJNIEXPORT jint JNICALL
342N/AJava_sun_awt_image_ImageRepresentation_setDiffICM(JNIEnv *env, jclass cls,
342N/A jint x, jint y, jint w,
342N/A jint h, jintArray jlut,
342N/A jint transIdx, jint numLut,
342N/A jobject jicm,
342N/A jbyteArray jpix, jint off,
342N/A jint scansize,
342N/A jobject jbct, jint chanOff)
342N/A{
342N/A unsigned int *srcLUT = NULL;
342N/A unsigned int *newLUT = NULL;
342N/A int sStride;
342N/A int pixelStride;
342N/A int mapSize;
342N/A jobject jdata = NULL;
342N/A jobject jnewlut = NULL;
342N/A unsigned char *srcData;
342N/A unsigned char *dstData;
342N/A unsigned char *dataP;
342N/A unsigned char *pixP;
342N/A int i;
342N/A int j;
342N/A int newNumLut;
342N/A int newTransIdx;
342N/A int jniFlag = JNI_ABORT;
2754N/A unsigned char *ydataP;
2754N/A unsigned char *ypixP;
2754N/A unsigned char cvtLut[256];
2754N/A
342N/A if (JNU_IsNull(env, jlut)) {
342N/A JNU_ThrowNullPointerException(env, "NullPointerException");
342N/A return 0;
342N/A }
342N/A
342N/A if (JNU_IsNull(env, jpix)) {
342N/A JNU_ThrowNullPointerException(env, "NullPointerException");
342N/A return 0;
342N/A }
342N/A
342N/A sStride = (*env)->GetIntField(env, jbct, g_BCRscanstrID);
342N/A pixelStride =(*env)->GetIntField(env, jbct, g_BCRpixstrID);
342N/A jdata = (*env)->GetObjectField(env, jbct, g_BCRdataID);
342N/A jnewlut = (*env)->GetObjectField(env, jicm, g_ICMrgbID);
342N/A mapSize = (*env)->GetIntField(env, jicm, g_ICMmapSizeID);
342N/A
342N/A srcLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jlut,
342N/A NULL);
342N/A if (srcLUT == NULL) {
342N/A /* out of memory error already thrown */
342N/A return 0;
342N/A }
342N/A
342N/A newLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jnewlut,
342N/A NULL);
342N/A if (newLUT == NULL) {
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
342N/A JNI_ABORT);
342N/A /* out of memory error already thrown */
342N/A return 0;
342N/A }
342N/A
342N/A newNumLut = numLut;
342N/A newTransIdx = transIdx;
342N/A if (compareLUTs(srcLUT, numLut, transIdx, newLUT, mapSize,
342N/A cvtLut, &newNumLut, &newTransIdx, &jniFlag) == FALSE) {
342N/A /* Need to convert to ICR */
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
342N/A JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jnewlut, newLUT, JNI_ABORT);
342N/A return 0;
342N/A }
342N/A
342N/A /* Don't need these any more */
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, jniFlag);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jnewlut, newLUT, JNI_ABORT);
342N/A
342N/A if (newNumLut != numLut) {
342N/A /* Need to write back new number of entries in lut */
342N/A (*env)->SetIntField(env, cls, s_JnumSrcLUTID, newNumLut);
342N/A }
342N/A
342N/A if (newTransIdx != transIdx) {
342N/A (*env)->SetIntField(env, cls, s_JsrcLUTtransIndexID, newTransIdx);
342N/A }
342N/A
342N/A srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
342N/A NULL);
342N/A if (srcData == NULL) {
342N/A /* out of memory error already thrown */
1111N/A return 0;
1111N/A }
1111N/A
1111N/A dstData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jdata,
342N/A NULL);
342N/A if (dstData == NULL) {
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
342N/A /* out of memory error already thrown */
342N/A return 0;
342N/A }
342N/A
342N/A ydataP = dstData + chanOff + y*sStride + x*pixelStride;
342N/A ypixP = srcData + off;
342N/A
342N/A for (i=0; i < h; i++) {
342N/A dataP = ydataP;
342N/A pixP = ypixP;
342N/A for (j=0; j < w; j++) {
342N/A *dataP = cvtLut[*pixP];
342N/A dataP += pixelStride;
342N/A pixP++;
342N/A }
342N/A ydataP += sStride;
342N/A ypixP += scansize;
342N/A }
342N/A
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
342N/A (*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
342N/A
342N/A return 1;
342N/A}
342N/A
342N/Astatic int compareLUTs(unsigned int *lut1, int numLut1, int transIdx,
342N/A unsigned int *lut2, int numLut2, unsigned char *cvtLut,
342N/A int *retNumLut1, int *retTransIdx, int *jniFlagP)
342N/A{
342N/A int i;
342N/A int idx;
342N/A int newTransIdx = -1;
342N/A unsigned int rgb;
342N/A int changed = FALSE;
342N/A int maxSize = (numLut1 > numLut2 ? numLut1 : numLut2);
342N/A
342N/A *jniFlagP = JNI_ABORT;
342N/A
342N/A for (i=0; i < maxSize; i++) {
342N/A cvtLut[i] = i;
342N/A }
342N/A
342N/A for (i=0; i < numLut2; i++) {
342N/A /* If this slot in new palette is different from the
342N/A * same slot in current palette, then we try to find
342N/A * this color in other slots. On failure, add this color
342N/A * to current palette.
342N/A */
342N/A if ((i >= numLut1) ||
342N/A (lut1[i] != lut2[i]))
342N/A {
342N/A rgb = lut2[i];
342N/A /* Transparent */
342N/A if ((rgb & ALPHA_MASK) == 0) {
342N/A if (transIdx == -1) {
342N/A if (numLut1 < 256) {
342N/A cvtLut[i] = numLut1;
342N/A newTransIdx = i;
342N/A transIdx = i;
342N/A numLut1++;
342N/A changed = TRUE;
342N/A }
342N/A else {
342N/A return FALSE;
342N/A }
342N/A }
342N/A cvtLut[i] = transIdx;
342N/A }
342N/A else {
342N/A if ((idx = findIdx(rgb, lut1, numLut1)) == -1) {
342N/A if (numLut1 < 256) {
342N/A lut1[numLut1] = rgb;
342N/A cvtLut[i] = numLut1;
342N/A numLut1++;
342N/A changed = TRUE;
342N/A }
342N/A else {
342N/A /* Bad news... need to convert image */
342N/A return FALSE;
342N/A }
342N/A } else {
342N/A cvtLut[i] = idx;
342N/A }
342N/A }
342N/A }
342N/A }
342N/A
342N/A if (changed) {
342N/A *jniFlagP = 0;
342N/A *retNumLut1 = numLut1;
342N/A if (newTransIdx != -1) {
342N/A *retTransIdx = newTransIdx;
342N/A }
342N/A }
342N/A return TRUE;
342N/A}
342N/A
342N/Astatic int findIdx(unsigned int rgb, unsigned int *lut, int numLut) {
342N/A int i;
342N/A
342N/A if ((rgb&0xff000000)==0) {
342N/A for (i=0; i < numLut; i++) {
342N/A if ((lut[i]&0xff000000)==0) return i;
342N/A }
342N/A }
342N/A else {
342N/A for (i=0; i < numLut; i++) {
342N/A if (lut[i] == rgb) return i;
342N/A }
342N/A }
342N/A return -1;
342N/A}
342N/A