src/java.desktop/share/native/libawt/awt/image/awt_ImageRep.c
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4207
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4207
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4207
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4207
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4207
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "awt_parseImage.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "imageInitIDs.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "sun_awt_image_ImageRepresentation.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
static int compareLUTs(unsigned int *lut1, int numLut1, int transIdx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
                       unsigned int *lut2, int numLut2, unsigned char *cvtLut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                       int *retNumLut1, int *retTransIdx, int *jniFlagP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
static int findIdx(unsigned int rgb, unsigned int *lut, int numLut1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#define ALPHA_MASK    0xff000000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifndef FALSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#  define FALSE 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#ifndef TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#  define TRUE 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    48
#define CHECK_STRIDE(yy, hh, ss)                            \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    49
    if ((ss) != 0) {                                        \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    50
        int limit = 0x7fffffff / ((ss) > 0 ? (ss) : -(ss)); \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    51
        if (limit < (yy) || limit < ((yy) + (hh) - 1)) {    \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    52
            /* integer oveflow */                           \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    53
            return JNI_FALSE;                               \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    54
        }                                                   \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    55
    }                                                       \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    56
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    57
#define CHECK_SRC()                                      \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    58
    do {                                                 \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    59
        int pixeloffset;                                 \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    60
        if (off < 0 || off >= srcDataLength) {           \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    61
            return JNI_FALSE;                            \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    62
        }                                                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    63
        CHECK_STRIDE(0, h, scansize);                    \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    64
                                                         \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    65
        /* check scansize */                             \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    66
        pixeloffset = scansize * (h - 1);                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    67
        if ((w - 1) > (0x7fffffff - pixeloffset)) {      \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    68
            return JNI_FALSE;                            \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    69
        }                                                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    70
        pixeloffset += (w - 1);                          \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    71
                                                         \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    72
        if (off > (0x7fffffff - pixeloffset)) {          \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    73
            return JNI_FALSE;                            \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    74
        }                                                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    75
    } while (0)                                          \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    76
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    77
#define CHECK_DST(xx, yy)                                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    78
    do {                                                 \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    79
        int soffset = (yy) * sStride;                    \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    80
        int poffset = (xx) * pixelStride;                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    81
        if (poffset > (0x7fffffff - soffset)) {          \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    82
            return JNI_FALSE;                            \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    83
        }                                                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    84
        poffset += soffset;                              \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    85
        if (dstDataOff > (0x7fffffff - poffset)) {       \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    86
            return JNI_FALSE;                            \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    87
        }                                                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    88
        poffset += dstDataOff;                           \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    89
                                                         \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    90
        if (poffset < 0 || poffset >= dstDataLength) {   \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    91
            return JNI_FALSE;                            \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    92
        }                                                \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    93
    } while (0)                                          \
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
static jfieldID s_JnumSrcLUTID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
static jfieldID s_JsrcLUTtransIndexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
Java_sun_awt_image_ImageRepresentation_initIDs(JNIEnv *env, jclass cls) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   100
    CHECK_NULL(s_JnumSrcLUTID = (*env)->GetFieldID(env, cls, "numSrcLUT", "I"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   101
    CHECK_NULL(s_JsrcLUTtransIndexID = (*env)->GetFieldID(env, cls,
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   102
                                                          "srcLUTtransIndex", "I"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * This routine is used to draw ICM pixels into a default color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   108
JNIEXPORT jboolean JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
Java_sun_awt_image_ImageRepresentation_setICMpixels(JNIEnv *env, jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                                    jint x, jint y, jint w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                                    jint h, jintArray jlut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                                    jbyteArray jpix, jint off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                                    jint scansize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                                    jobject jict)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    unsigned char *srcData = NULL;
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   117
    jint srcDataLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    int *dstData;
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   119
    jint dstDataLength;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   120
    jint dstDataOff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    int *dstP, *dstyP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    unsigned char *srcyP, *srcP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int *srcLUT = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    int yIdx, xIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    int sStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    int *cOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    jobject joffs = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    jobject jdata = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (JNU_IsNull(env, jlut)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        JNU_ThrowNullPointerException(env, "NullPointerException");
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   133
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    if (JNU_IsNull(env, jpix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        JNU_ThrowNullPointerException(env, "NullPointerException");
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   138
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   139
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   140
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   141
    if (x < 0 || w < 1 || (0x7fffffff - x) < w) {
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   142
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   143
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   144
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   145
    if (y < 0 || h < 1 || (0x7fffffff - y) < h) {
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   146
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    sStride = (*env)->GetIntField(env, jict, g_ICRscanstrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    pixelStride = (*env)->GetIntField(env, jict, g_ICRpixstrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    joffs = (*env)->GetObjectField(env, jict, g_ICRdataOffsetsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    jdata = (*env)->GetObjectField(env, jict, g_ICRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   154
    if (JNU_IsNull(env, jdata)) {
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   155
        /* no destination buffer */
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   156
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   157
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   158
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   159
    if (JNU_IsNull(env, joffs) || (*env)->GetArrayLength(env, joffs) < 1) {
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   160
        /* invalid data offstes in raster */
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   161
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   162
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   163
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   164
    srcDataLength = (*env)->GetArrayLength(env, jpix);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   165
    dstDataLength = (*env)->GetArrayLength(env, jdata);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   166
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   167
    cOffs = (int *) (*env)->GetPrimitiveArrayCritical(env, joffs, NULL);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   168
    if (cOffs == NULL) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   169
        (*env)->ExceptionClear(env);
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   170
        JNU_ThrowNullPointerException(env, "Null channel offset array");
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   171
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   172
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   173
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   174
    dstDataOff = cOffs[0];
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   175
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   176
    /* the offset array is not needed anymore and can be released */
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   177
    (*env)->ReleasePrimitiveArrayCritical(env, joffs, cOffs, JNI_ABORT);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   178
    joffs = NULL;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   179
    cOffs = NULL;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   180
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   181
    /* do basic validation: make sure that offsets for
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   182
    * first pixel and for last pixel are safe to calculate and use */
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   183
    CHECK_STRIDE(y, h, sStride);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   184
    CHECK_STRIDE(x, w, pixelStride);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   185
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   186
    CHECK_DST(x, y);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   187
    CHECK_DST(x + w -1, y + h - 1);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   188
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   189
    /* check source array */
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   190
    CHECK_SRC();
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   191
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    srcLUT = (int *) (*env)->GetPrimitiveArrayCritical(env, jlut, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    if (srcLUT == NULL) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   194
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        JNU_ThrowNullPointerException(env, "Null IndexColorModel LUT");
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   196
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                                                  NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    if (srcData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   203
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        JNU_ThrowNullPointerException(env, "Null data array");
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   205
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    dstData = (int *) (*env)->GetPrimitiveArrayCritical(env, jdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    if (dstData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   212
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        JNU_ThrowNullPointerException(env, "Null tile data array");
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   214
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   217
    dstyP = dstData + dstDataOff + y*sStride + x*pixelStride;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    srcyP = srcData + off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    for (yIdx = 0; yIdx < h; yIdx++, srcyP += scansize, dstyP+=sStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        srcP = srcyP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        dstP = dstyP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        for (xIdx = 0; xIdx < w; xIdx++, dstP+=pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            *dstP = srcLUT[*srcP++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /* Release the locked arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,  JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    (*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   232
    return JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   235
JNIEXPORT jboolean JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
Java_sun_awt_image_ImageRepresentation_setDiffICM(JNIEnv *env, jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                                  jint x, jint y, jint w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                                  jint h, jintArray jlut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                                  jint transIdx, jint numLut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                                  jobject jicm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                                  jbyteArray jpix, jint off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                                  jint scansize,
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   243
                                                  jobject jbct, jint dstDataOff)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    unsigned int *srcLUT = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    unsigned int *newLUT = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    int sStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    int pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    int mapSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    jobject jdata = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    jobject jnewlut = NULL;
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   252
    jint srcDataLength;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   253
    jint dstDataLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    unsigned char *srcData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    unsigned char *dstData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    unsigned char *dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    unsigned char *pixP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    int newNumLut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    int newTransIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    int jniFlag = JNI_ABORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    unsigned char *ydataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    unsigned char *ypixP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    unsigned char cvtLut[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if (JNU_IsNull(env, jlut)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        JNU_ThrowNullPointerException(env, "NullPointerException");
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   269
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    if (JNU_IsNull(env, jpix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        JNU_ThrowNullPointerException(env, "NullPointerException");
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   274
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   275
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   276
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   277
    if (x < 0 || w < 1 || (0x7fffffff - x) < w) {
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   278
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   281
    if (y < 0 || h < 1 || (0x7fffffff - y) < h) {
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   282
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   283
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   284
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   285
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    sStride = (*env)->GetIntField(env, jbct, g_BCRscanstrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    pixelStride =(*env)->GetIntField(env, jbct, g_BCRpixstrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    jdata = (*env)->GetObjectField(env, jbct, g_BCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    jnewlut = (*env)->GetObjectField(env, jicm, g_ICMrgbID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    mapSize = (*env)->GetIntField(env, jicm, g_ICMmapSizeID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
4202
20061d84dd36 6872357: JRE AWT setDifflCM vulnerable to Stack Overflow
bae
parents: 2
diff changeset
   292
    if (numLut < 0 || numLut > 256 || mapSize < 0 || mapSize > 256) {
20061d84dd36 6872357: JRE AWT setDifflCM vulnerable to Stack Overflow
bae
parents: 2
diff changeset
   293
        /* Ether old or new ICM has a palette that exceeds capacity
20061d84dd36 6872357: JRE AWT setDifflCM vulnerable to Stack Overflow
bae
parents: 2
diff changeset
   294
           of byte data type, so we have to convert the image data
20061d84dd36 6872357: JRE AWT setDifflCM vulnerable to Stack Overflow
bae
parents: 2
diff changeset
   295
           to default representation.
20061d84dd36 6872357: JRE AWT setDifflCM vulnerable to Stack Overflow
bae
parents: 2
diff changeset
   296
        */
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   297
        return JNI_FALSE;
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   298
    }
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   299
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   300
    if (JNU_IsNull(env, jdata)) {
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   301
        /* no destination buffer */
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   302
        return JNI_FALSE;
4202
20061d84dd36 6872357: JRE AWT setDifflCM vulnerable to Stack Overflow
bae
parents: 2
diff changeset
   303
    }
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   304
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   305
    srcDataLength = (*env)->GetArrayLength(env, jpix);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   306
    dstDataLength = (*env)->GetArrayLength(env, jdata);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   307
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   308
    CHECK_STRIDE(y, h, sStride);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   309
    CHECK_STRIDE(x, w, pixelStride);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   310
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   311
    CHECK_DST(x, y);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   312
    CHECK_DST(x + w -1, y + h - 1);
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   313
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   314
    /* check source array */
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   315
    CHECK_SRC();
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   316
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    srcLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jlut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                                NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (srcLUT == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /* out of memory error already thrown */
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   321
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    newLUT = (unsigned int *) (*env)->GetPrimitiveArrayCritical(env, jnewlut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                                                NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    if (newLUT == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /* out of memory error already thrown */
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   330
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    newNumLut = numLut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    newTransIdx = transIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    if (compareLUTs(srcLUT, numLut, transIdx, newLUT, mapSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    cvtLut, &newNumLut, &newTransIdx, &jniFlag) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /* Need to convert to ICR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        (*env)->ReleasePrimitiveArrayCritical(env, jnewlut, newLUT, JNI_ABORT);
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   341
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /* Don't need these any more */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    (*env)->ReleasePrimitiveArrayCritical(env, jlut, srcLUT, jniFlag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    (*env)->ReleasePrimitiveArrayCritical(env, jnewlut, newLUT, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    if (newNumLut != numLut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /* Need to write back new number of entries in lut */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        (*env)->SetIntField(env, cls, s_JnumSrcLUTID, newNumLut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    if (newTransIdx != transIdx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        (*env)->SetIntField(env, cls, s_JsrcLUTtransIndexID, newTransIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    srcData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                                                  NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    if (srcData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /* out of memory error already thrown */
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   361
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    dstData = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, jdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                                                  NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    if (dstData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        /* out of memory error already thrown */
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   369
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   372
    ydataP = dstData + dstDataOff + y*sStride + x*pixelStride;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    ypixP  = srcData + off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    for (i=0; i < h; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        dataP = ydataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        pixP = ypixP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        for (j=0; j < w; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            *dataP = cvtLut[*pixP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            dataP += pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            pixP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        ydataP += sStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        ypixP  += scansize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    (*env)->ReleasePrimitiveArrayCritical(env, jpix, srcData, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    (*env)->ReleasePrimitiveArrayCritical(env, jdata, dstData, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
16880
558619642e45 8007617: Better validation of images
bae
parents: 5506
diff changeset
   390
    return JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
static int compareLUTs(unsigned int *lut1, int numLut1, int transIdx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                       unsigned int *lut2, int numLut2, unsigned char *cvtLut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                       int *retNumLut1, int *retTransIdx, int *jniFlagP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    int idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    int newTransIdx = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    unsigned int rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    int changed = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    int maxSize = (numLut1 > numLut2 ? numLut1 : numLut2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    *jniFlagP = JNI_ABORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    for (i=0; i < maxSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        cvtLut[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    for (i=0; i < numLut2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        /* If this slot in new palette is different from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * same slot in current palette, then we try to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * this color in other slots. On failure, add this color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * to current palette.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if ((i >= numLut1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            (lut1[i] != lut2[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            rgb = lut2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            /* Transparent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if ((rgb & ALPHA_MASK) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (transIdx == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (numLut1 < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        cvtLut[i] = numLut1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        newTransIdx = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        transIdx = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        numLut1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        changed = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                cvtLut[i] = transIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if ((idx = findIdx(rgb, lut1, numLut1)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    if (numLut1 < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        lut1[numLut1] = rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        cvtLut[i] = numLut1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        numLut1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        changed = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        /* Bad news...  need to convert image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    cvtLut[i] = idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    if (changed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        *jniFlagP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        *retNumLut1 = numLut1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (newTransIdx != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            *retTransIdx = newTransIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
static int findIdx(unsigned int rgb, unsigned int *lut, int numLut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    if ((rgb&0xff000000)==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        for (i=0; i < numLut; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if ((lut[i]&0xff000000)==0) return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        for (i=0; i < numLut; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (lut[i] == rgb) return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
}