jdk/src/share/native/sun/awt/medialib/awt_ImagingLib.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 16868 b5e2827ecc50
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2010, 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: 5187
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: 5187
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: 5187
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5187
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5187
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 <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "sun_awt_image_ImagingLib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "java_awt_Transparency.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "java_awt_image_AffineTransformOp.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "java_awt_image_BufferedImage.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "java_awt_color_ColorSpace.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "java_awt_image_ConvolveOp.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "sun_awt_image_IntegerComponentRaster.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "awt_ImagingLib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "awt_parseImage.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "imageInitIDs.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <jni_util.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "awt_Mlib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "gdefs.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "safe_alloc.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/***************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *                               Definitions                               *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 ***************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define jio_fprintf fprintf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#ifndef TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define TRUE 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#endif /* TRUE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#ifndef FALSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#define FALSE 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#endif /* FALSE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#define TYPE_CUSTOM         java_awt_image_BufferedImage_TYPE_CUSTOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#define TYPE_INT_RGB        java_awt_image_BufferedImage_TYPE_INT_RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#define TYPE_INT_ARGB       java_awt_image_BufferedImage_TYPE_INT_ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#define TYPE_INT_ARGB_PRE   java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#define TYPE_INT_BGR        java_awt_image_BufferedImage_TYPE_INT_BGR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#define TYPE_4BYTE_ABGR     java_awt_image_BufferedImage_TYPE_4BYTE_ABGR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#define TYPE_4BYTE_ABGR_PRE java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/* (alpha*color)>>nbits + alpha>>(nbits-1) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
#define BLEND(color, alpha, alphaNbits) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    ((((alpha)*(color))>>(alphaNbits)) + ((alpha) >> ((alphaNbits)-1)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* ((color - (alpha>>(nBits-1)))<<nBits)/alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
#define UNBLEND(color, alpha, alphaNbits) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    ((((color)-((alpha)>>((alphaNbits)-1)))<<(alphaNbits))/(alpha))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/* Enumeration of all of the mlib functions used */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
typedef enum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    MLIB_CONVMxN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    MLIB_AFFINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    MLIB_LOOKUP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    MLIB_CONVKERNCVT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
} mlibTypeE_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int dataType;           /* One of BYTE_DATA_TYPE, SHORT_DATA_TYPE, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int needToCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int cvtSrcToDefault;    /* If TRUE, convert the src to def CM (pre?) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int allocDefaultDst;    /* If TRUE, alloc def CM dst buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    int cvtToDst;           /* If TRUE, convert dst buffer to Dst CM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    int addAlpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
} mlibHintS_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
/***************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *                     Static Variables/Structures                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 ***************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
static mlibSysFnS_t sMlibSysFns = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    NULL, // placeholder for j2d_mlib_ImageCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    NULL, // placeholder for j2d_mlib_ImageCreateStruct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    NULL, // placeholder for j2d_mlib_ImageDelete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
static mlibFnS_t sMlibFns[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    {NULL, "j2d_mlib_ImageConvMxN"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    {NULL, "j2d_mlib_ImageAffine"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    {NULL, "j2d_mlib_ImageLookUp"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {NULL, "j2d_mlib_ImageConvKernelConvert"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    {NULL, NULL},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
static int s_timeIt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
static int s_printIt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
static int s_startOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
static int s_nomlib = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
/***************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *                          Static Function Prototypes                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 ***************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
allocateArray(JNIEnv *env, BufImageS_t *imageP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
              mlib_image **mlibImagePP, void **dataPP, int isSrc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
              int cvtToDefault, int addAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
allocateRasterArray(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    mlib_image **mlibImagePP, void **dataPP, int isSrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
freeArray(JNIEnv *env, BufImageS_t *srcimageP, mlib_image *srcmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
          void *srcdataP, BufImageS_t *dstimageP, mlib_image *dstmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
          void *dstdataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
freeDataArray(JNIEnv *env, jobject srcJdata, mlib_image *srcmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
          void *srcdataP, jobject dstJdata, mlib_image *dstmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
          void *dstdataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
storeImageArray(JNIEnv *env, BufImageS_t *srcP, BufImageS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                mlib_image *mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
storeRasterArray(JNIEnv *env, RasterS_t *srcP, RasterS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                mlib_image *mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
storeICMarray(JNIEnv *env, BufImageS_t *srcP, BufImageS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
              mlib_image *mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
colorMatch(int r, int g, int b, int a, unsigned char *argb, int numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
setImageHints(JNIEnv *env, BufImageS_t *srcP, BufImageS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
              int expandICM, int useAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
              int premultiply, mlibHintS_t *hintP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
static int expandICM(JNIEnv *env, BufImageS_t *imageP, unsigned int *mDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
static int expandPackedBCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                           unsigned char *outDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
static int expandPackedSCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                           unsigned char *outDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
static int expandPackedICR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                           unsigned char *outDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
static int expandPackedBCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                  int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                  int forceAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
static int expandPackedSCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                  int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                  int forceAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
static int expandPackedICRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                  int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                  int forceAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
static int setPackedBCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        unsigned char *outDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
static int setPackedSCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        unsigned char *outDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
static int setPackedICR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        unsigned char *outDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
static int setPackedBCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                               int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                               int supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
static int setPackedSCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                               int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                               int supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
static int setPackedICRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                               int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                               int supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
mlib_start_timer start_timer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
mlib_stop_timer stop_timer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
/***************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *                          Debugging Definitions                          *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 ***************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
printMedialibError(int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    switch(status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    case MLIB_FAILURE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        jio_fprintf(stderr, "failure\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    case MLIB_NULLPOINTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        jio_fprintf(stderr, "null pointer\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    case MLIB_OUTOFRANGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        jio_fprintf (stderr, "out of range\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        jio_fprintf (stderr, "medialib error\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
#else /* ! DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
#  define printMedialibError(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
#endif /* ! DEBUG */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
2600
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   219
static int
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   220
getMlibEdgeHint(jint edgeHint) {
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   221
    switch (edgeHint) {
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   222
    case java_awt_image_ConvolveOp_EDGE_NO_OP:
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   223
        return MLIB_EDGE_DST_COPY_SRC;
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   224
    case java_awt_image_ConvolveOp_EDGE_ZERO_FILL:
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   225
    default:
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   226
        return MLIB_EDGE_DST_FILL_ZERO;
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   227
    }
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   228
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
/***************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *                          External Functions                             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 ***************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
Java_sun_awt_image_ImagingLib_convolveBI(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                         jobject jsrc, jobject jdst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                         jobject jkernel, jint edgeHint)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    void *sdata, *ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    mlib_image *src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    mlib_image *dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    int i, scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    mlib_d64 *dkern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    mlib_s32 *kdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    int klen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    float kmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    mlib_s32 cmask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    mlib_status status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    int retStatus = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    float *kern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    BufImageS_t *srcImageP, *dstImageP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    jobject jdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    int kwidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    int kheight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    int w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    mlibHintS_t hint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    int nbands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /* This function requires a lot of local refs ??? Is 64 enough ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if ((*env)->EnsureLocalCapacity(env, 64) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    if (s_nomlib) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    if (s_timeIt)     (*start_timer)(3600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    kwidth  = (*env)->GetIntField(env, jkernel, g_KernelWidthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    kheight = (*env)->GetIntField(env, jkernel, g_KernelHeightID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    jdata = (*env)->GetObjectField(env, jkernel, g_KernelDataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    klen  = (*env)->GetArrayLength(env, jdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    kern  = (float *) (*env)->GetPrimitiveArrayCritical(env, jdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    if (kern == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        /* out of memory exception already thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    if ((kwidth&0x1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        /* Kernel has even width */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        w = kwidth+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        w = kwidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    if ((kheight&0x1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        /* Kernel has even height */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        h = kheight+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        h = kheight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    dkern = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    if (SAFE_TO_ALLOC_3(w, h, sizeof(mlib_d64))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        dkern = (mlib_d64 *)calloc(1, w * h * sizeof(mlib_d64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    if (dkern == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        (*env)->ReleasePrimitiveArrayCritical(env, jdata, kern, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /* Need to flip and find max value of the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Also, save the kernel values as mlib_d64 values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * The flip is to operate correctly with medialib,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * which doesn't do the mathemetically correct thing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * i.e. it doesn't rotate the kernel by 180 degrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * REMIND: This should perhaps be done at the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * level by ConvolveOp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * REMIND: Should the max test be looking at absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * values?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * REMIND: What if klen != kheight * kwidth?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    kmax = kern[klen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    i = klen-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    for (y=0; y < kheight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        for (x=0; x < kwidth; x++, i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            dkern[y*w+x] = (mlib_d64) kern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (kern[i] > kmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                kmax = kern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    (*env)->ReleasePrimitiveArrayCritical(env, jdata, kern, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    if (kmax > 1<<16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        /* We can only handle 16 bit max */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /* Parse the source image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    if ((status = awt_parseImage(env, jsrc, &srcImageP, FALSE)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /* Parse the destination image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    if ((status = awt_parseImage(env, jdst, &dstImageP, FALSE)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    nbands = setImageHints(env, srcImageP, dstImageP, TRUE, TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        FALSE, &hint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    if (nbands < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /* Allocate the arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    if (allocateArray(env, srcImageP, &src, &sdata, TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                      hint.cvtSrcToDefault, hint.addAlpha) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    if (allocateArray(env, dstImageP, &dst, &ddata, FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                      hint.cvtToDst, FALSE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        freeArray(env, srcImageP, src, sdata, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    kdata = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    if (SAFE_TO_ALLOC_3(w, h, sizeof(mlib_s32))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        kdata = (mlib_s32 *)malloc(w * h * sizeof(mlib_s32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    if (kdata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        freeArray(env, srcImageP, src, sdata, dstImageP, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    if ((*sMlibFns[MLIB_CONVKERNCVT].fptr)(kdata, &scale, dkern, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                    mlib_ImageGetType(src)) != MLIB_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        freeArray(env, srcImageP, src, sdata, dstImageP, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        free(kdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        fprintf(stderr, "Orig Kernel(len=%d):\n",klen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        for (y=kheight-1; y >= 0; y--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            for (x=kwidth-1; x >= 0; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                fprintf(stderr, "%g ", dkern[y*w+x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            fprintf(stderr, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        fprintf(stderr, "New Kernel(scale=%d):\n", scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        for (y=kheight-1; y >= 0; y--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            for (x=kwidth-1; x >= 0; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                fprintf(stderr, "%d ", kdata[y*w+x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            fprintf(stderr, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    cmask = (1<<src->channels)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    status = (*sMlibFns[MLIB_CONVMxN].fptr)(dst, src, kdata, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                               (w-1)/2, (h-1)/2, scale, cmask,
2600
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   416
                               getMlibEdgeHint(edgeHint));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    if (status != MLIB_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        printMedialibError(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        unsigned int *dP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (s_startOff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            printf("Starting at %d\n", s_startOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (sdata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            dP = (unsigned int *) mlib_ImageGetData(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            dP = (unsigned int *) sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        printf("src is\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            printf("%x ",dP[s_startOff+i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            dP = (unsigned int *)mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            dP = (unsigned int *) ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        printf("dst is \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            printf("%x ",dP[s_startOff+i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /* Means that we couldn't write directly into the destination buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        /* Need to store it back into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (storeImageArray(env, srcImageP, dstImageP, dst) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            /* Error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /* Release the pinned memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    freeArray(env, srcImageP, src, sdata, dstImageP, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    free(kdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    if (s_timeIt) (*stop_timer)(3600, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    return retStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
Java_sun_awt_image_ImagingLib_convolveRaster(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                             jobject jsrc, jobject jdst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                             jobject jkernel, jint edgeHint)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    mlib_image *src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    mlib_image *dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    int i, scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    mlib_d64 *dkern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    mlib_s32 *kdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    int klen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    float kmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    int retStatus = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    mlib_status status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    mlib_s32 cmask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    void *sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    void *ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    RasterS_t *srcRasterP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    RasterS_t *dstRasterP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    int kwidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    int kheight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    int w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    jobject jdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    float *kern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /* This function requires a lot of local refs ??? Is 64 enough ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    if ((*env)->EnsureLocalCapacity(env, 64) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    if (s_nomlib) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    if (s_timeIt)     (*start_timer)(3600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    kwidth  = (*env)->GetIntField(env, jkernel, g_KernelWidthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    kheight = (*env)->GetIntField(env, jkernel, g_KernelHeightID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    jdata = (*env)->GetObjectField(env, jkernel, g_KernelDataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    klen  = (*env)->GetArrayLength(env, jdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    kern  = (float *) (*env)->GetPrimitiveArrayCritical(env, jdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    if (kern == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        /* out of memory exception already thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    if ((kwidth&0x1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        /* Kernel has even width */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        w = kwidth+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        w = kwidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    if ((kheight&0x1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        /* Kernel has even height */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        h = kheight+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        h = kheight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    dkern = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    if (SAFE_TO_ALLOC_3(w, h, sizeof(mlib_d64))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        dkern = (mlib_d64 *)calloc(1, w * h * sizeof(mlib_d64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    if (dkern == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        (*env)->ReleasePrimitiveArrayCritical(env, jdata, kern, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /* Need to flip and find max value of the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Also, save the kernel values as mlib_d64 values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * The flip is to operate correctly with medialib,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * which doesn't do the mathemetically correct thing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * i.e. it doesn't rotate the kernel by 180 degrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * REMIND: This should perhaps be done at the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * level by ConvolveOp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * REMIND: Should the max test be looking at absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * values?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * REMIND: What if klen != kheight * kwidth?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    kmax = kern[klen-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    i = klen-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    for (y=0; y < kheight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        for (x=0; x < kwidth; x++, i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            dkern[y*w+x] = (mlib_d64) kern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (kern[i] > kmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                kmax = kern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    (*env)->ReleasePrimitiveArrayCritical(env, jdata, kern, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    if (kmax > 1<<16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        /* We can only handle 16 bit max */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /* Parse the source image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    if ((srcRasterP = (RasterS_t *) calloc(1, sizeof(RasterS_t))) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        JNU_ThrowOutOfMemoryError(env, "Out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    if ((dstRasterP = (RasterS_t *) calloc(1, sizeof(RasterS_t))) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        JNU_ThrowOutOfMemoryError(env, "Out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        free(srcRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /* Parse the source raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    if ((status = awt_parseRaster(env, jsrc, srcRasterP)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        /* Can't handle any custom rasters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        free(srcRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        free(dstRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /* Parse the destination raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    if ((status = awt_parseRaster(env, jdst, dstRasterP)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        free(dstRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /* Allocate the arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    if (allocateRasterArray(env, srcRasterP, &src, &sdata, TRUE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    if (allocateRasterArray(env, dstRasterP, &dst, &ddata, FALSE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        freeDataArray(env, srcRasterP->jdata, src, sdata, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    kdata = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    if (SAFE_TO_ALLOC_3(w, h, sizeof(mlib_s32))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        kdata = (mlib_s32 *)malloc(w * h * sizeof(mlib_s32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    if (kdata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        freeDataArray(env, srcRasterP->jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                      dstRasterP->jdata, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    if ((*sMlibFns[MLIB_CONVKERNCVT].fptr)(kdata, &scale, dkern, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                    mlib_ImageGetType(src)) != MLIB_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        freeDataArray(env, srcRasterP->jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                      dstRasterP->jdata, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        free(kdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        fprintf(stderr, "Orig Kernel(len=%d):\n",klen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        for (y=kheight-1; y >= 0; y--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            for (x=kwidth-1; x >= 0; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                fprintf(stderr, "%g ", dkern[y*w+x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            fprintf(stderr, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        fprintf(stderr, "New Kernel(scale=%d):\n", scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        for (y=kheight-1; y >= 0; y--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            for (x=kwidth-1; x >= 0; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                fprintf(stderr, "%d ", kdata[y*w+x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            fprintf(stderr, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    cmask = (1<<src->channels)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    status = (*sMlibFns[MLIB_CONVMxN].fptr)(dst, src, kdata, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                               (w-1)/2, (h-1)/2, scale, cmask,
2600
923ecc7ab342 6726779: ConvolveOp on USHORT raster can cause the JVM crash.
bae
parents: 2
diff changeset
   664
                               getMlibEdgeHint(edgeHint));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    if (status != MLIB_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        printMedialibError(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        unsigned int *dP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (s_startOff != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            printf("Starting at %d\n", s_startOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (sdata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            dP = (unsigned int *) mlib_ImageGetData(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            dP = (unsigned int *) sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        printf("src is\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            printf("%x ",dP[s_startOff+i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            dP = (unsigned int *)mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            dP = (unsigned int *) ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        printf("dst is\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            printf("%x ",dP[s_startOff+i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /* Means that we couldn't write directly into the destination buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        unsigned char *bdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        unsigned short *sdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        /* Punt for now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        switch (dstRasterP->dataType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        case BYTE_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            bdataP  = (unsigned char *) mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            retStatus = (awt_setPixelByte(env, -1, dstRasterP, bdataP) >= 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        case SHORT_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            sdataP  = (unsigned short *) mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            retStatus = (awt_setPixelShort(env, -1, dstRasterP, sdataP) >= 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /* Release the pinned memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    freeDataArray(env, srcRasterP->jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                  dstRasterP->jdata, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    free(dkern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    free(kdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    if (s_timeIt) (*stop_timer)(3600,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    return retStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
Java_sun_awt_image_ImagingLib_transformBI(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                                          jobject jsrc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                          jobject jdst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                          jdoubleArray jmatrix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                          jint interpType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    mlib_image *src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    mlib_image *dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    int retStatus = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    mlib_status status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    double *matrix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    mlib_d64 mtx[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    void *sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    void *ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    BufImageS_t *srcImageP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    BufImageS_t *dstImageP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    mlib_filter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    mlibHintS_t hint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    unsigned int *dP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    int useIndexed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    int nbands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /* This function requires a lot of local refs ??? Is 64 enough ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    if ((*env)->EnsureLocalCapacity(env, 64) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    if (s_nomlib) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    if (s_timeIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        (*start_timer)(3600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    switch(interpType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    case java_awt_image_AffineTransformOp_TYPE_BILINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        filter = MLIB_BILINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    case java_awt_image_AffineTransformOp_TYPE_NEAREST_NEIGHBOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        filter = MLIB_NEAREST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    case java_awt_image_AffineTransformOp_TYPE_BICUBIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        filter = MLIB_BICUBIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        JNU_ThrowInternalError(env, "Unknown interpolation type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    if ((*env)->GetArrayLength(env, jmatrix) < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         * Very unlikely, however we should check for this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         * if given matrix array is too short, we can't handle it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    matrix = (*env)->GetPrimitiveArrayCritical(env, jmatrix, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    if (matrix == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        /* out of memory error already thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        printf("matrix is %g %g %g %g %g %g\n", matrix[0], matrix[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
               matrix[2], matrix[3], matrix[4], matrix[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    mtx[0] = matrix[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    mtx[1] = matrix[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    mtx[2] = matrix[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    mtx[3] = matrix[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    mtx[4] = matrix[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    mtx[5] = matrix[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    (*env)->ReleasePrimitiveArrayCritical(env, jmatrix, matrix, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    /* Parse the source image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    if ((status = awt_parseImage(env, jsrc, &srcImageP, FALSE)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /* Parse the destination image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    if ((status = awt_parseImage(env, jdst, &dstImageP, FALSE)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /* REMIND!!  Can't assume that it is the same LUT!! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /* Fix 4213160, 4184283 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    useIndexed = (srcImageP->cmodel.cmType == INDEX_CM_TYPE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                  dstImageP->cmodel.cmType == INDEX_CM_TYPE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                  srcImageP->raster.rasterType == dstImageP->raster.rasterType &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                  srcImageP->raster.rasterType == COMPONENT_RASTER_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    nbands = setImageHints(env, srcImageP, dstImageP, !useIndexed, TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                        FALSE, &hint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    if (nbands < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    /* Allocate the arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    if (allocateArray(env, srcImageP, &src, &sdata, TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                      hint.cvtSrcToDefault, hint.addAlpha) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    if (allocateArray(env, dstImageP, &dst, &ddata, FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                      hint.cvtToDst, FALSE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        freeArray(env, srcImageP, src, sdata, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
fprintf(stderr,"Src----------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
fprintf(stderr,"Type : %d\n",src->type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
fprintf(stderr,"Channels: %d\n",src->channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
fprintf(stderr,"Width   : %d\n",src->width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
fprintf(stderr,"Height  : %d\n",src->height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
fprintf(stderr,"Stride  : %d\n",src->stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
fprintf(stderr,"Flags   : %d\n",src->flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
fprintf(stderr,"Dst----------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
fprintf(stderr,"Type : %d\n",dst->type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
fprintf(stderr,"Channels: %d\n",dst->channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
fprintf(stderr,"Width   : %d\n",dst->width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
fprintf(stderr,"Height  : %d\n",dst->height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
fprintf(stderr,"Stride  : %d\n",dst->stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
fprintf(stderr,"Flags   : %d\n",dst->flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    if (dstImageP->cmodel.cmType == INDEX_CM_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        /* Need to clear the destination to the transparent pixel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        unsigned char *cP = (unsigned char *)mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        memset(cP, dstImageP->cmodel.transIdx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
               mlib_ImageGetWidth(dst)*mlib_ImageGetHeight(dst));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    /* Perform the transformation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    if ((status = (*sMlibFns[MLIB_AFFINE].fptr)(dst, src, mtx, filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                  MLIB_EDGE_SRC_EXTEND) != MLIB_SUCCESS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        printMedialibError(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        freeArray(env, srcImageP, src, sdata, dstImageP, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (sdata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            dP = (unsigned int *) mlib_ImageGetData(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            dP = (unsigned int *) sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        printf("src is\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            printf("%x ",dP[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            dP = (unsigned int *)mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            dP = (unsigned int *) ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        printf("dst is\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            printf("%x ",dP[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /* Means that we couldn't write directly into the destination buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        freeDataArray(env, srcImageP->raster.jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                      NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        /* Need to store it back into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if (storeImageArray(env, srcImageP, dstImageP, dst) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            /* Error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        freeDataArray(env, NULL, NULL, NULL, dstImageP->raster.jdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                      dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        /* Release the pinned memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        freeArray(env, srcImageP, src, sdata, dstImageP, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    if (s_timeIt) (*stop_timer)(3600,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    return retStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
Java_sun_awt_image_ImagingLib_transformRaster(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                                              jobject jsrc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                                              jobject jdst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                              jdoubleArray jmatrix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                                              jint interpType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    mlib_image *src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    mlib_image *dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    int retStatus = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    mlib_status status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    double *matrix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    mlib_d64 mtx[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    void *sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    void *ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    RasterS_t *srcRasterP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    RasterS_t *dstRasterP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    mlib_filter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    unsigned int *dP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    /* This function requires a lot of local refs ??? Is 64 enough ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    if ((*env)->EnsureLocalCapacity(env, 64) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   967
    if (s_nomlib) return 0;
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   968
    if (s_timeIt) {
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   969
        (*start_timer)(3600);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   970
    }
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   971
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    switch(interpType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    case java_awt_image_AffineTransformOp_TYPE_BILINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        filter = MLIB_BILINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    case java_awt_image_AffineTransformOp_TYPE_NEAREST_NEIGHBOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        filter = MLIB_NEAREST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    case java_awt_image_AffineTransformOp_TYPE_BICUBIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        filter = MLIB_BICUBIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        JNU_ThrowInternalError(env, "Unknown interpolation type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   987
    if ((srcRasterP = (RasterS_t *) calloc(1, sizeof(RasterS_t))) == NULL) {
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   988
        JNU_ThrowOutOfMemoryError(env, "Out of memory");
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   989
        return -1;
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   990
    }
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   991
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   992
    if ((dstRasterP = (RasterS_t *) calloc(1, sizeof(RasterS_t))) == NULL) {
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   993
        JNU_ThrowOutOfMemoryError(env, "Out of memory");
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   994
        free(srcRasterP);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
   995
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    if ((*env)->GetArrayLength(env, jmatrix) < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * Very unlikely, however we should check for this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         * if given matrix array is too short, we can't handle it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        free(srcRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        free(dstRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    matrix = (*env)->GetPrimitiveArrayCritical(env, jmatrix, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    if (matrix == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        /* out of memory error already thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        free(srcRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        free(dstRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        printf("matrix is %g %g %g %g %g %g\n", matrix[0], matrix[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
               matrix[2], matrix[3], matrix[4], matrix[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    mtx[0] = matrix[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    mtx[1] = matrix[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    mtx[2] = matrix[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    mtx[3] = matrix[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    mtx[4] = matrix[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    mtx[5] = matrix[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    (*env)->ReleasePrimitiveArrayCritical(env, jmatrix, matrix, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    /* Parse the source raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    if ((status = awt_parseRaster(env, jsrc, srcRasterP)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        /* Can't handle any custom rasters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        free(srcRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        free(dstRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /* Parse the destination raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    if ((status = awt_parseRaster(env, jdst, dstRasterP)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        free(dstRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    /* Allocate the arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    if (allocateRasterArray(env, srcRasterP, &src, &sdata, TRUE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    if (allocateRasterArray(env, dstRasterP, &dst, &ddata, FALSE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        freeDataArray(env, srcRasterP->jdata, src, sdata, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
fprintf(stderr,"Src----------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
fprintf(stderr,"Type : %d\n",src->type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
fprintf(stderr,"Channels: %d\n",src->channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
fprintf(stderr,"Width   : %d\n",src->width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
fprintf(stderr,"Height  : %d\n",src->height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
fprintf(stderr,"Stride  : %d\n",src->stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
fprintf(stderr,"Flags   : %d\n",src->flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
fprintf(stderr,"Dst----------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
fprintf(stderr,"Type : %d\n",dst->type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
fprintf(stderr,"Channels: %d\n",dst->channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
fprintf(stderr,"Width   : %d\n",dst->width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
fprintf(stderr,"Height  : %d\n",dst->height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
fprintf(stderr,"Stride  : %d\n",dst->stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
fprintf(stderr,"Flags   : %d\n",dst->flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        unsigned char *cP = (unsigned char *)mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        memset(cP, 0, mlib_ImageGetWidth(dst)*mlib_ImageGetHeight(dst));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    /* Perform the transformation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    if ((status = (*sMlibFns[MLIB_AFFINE].fptr)(dst, src, mtx, filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                                  MLIB_EDGE_SRC_EXTEND) != MLIB_SUCCESS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        printMedialibError(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        /* REMIND: Free the regions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    if (s_printIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if (sdata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            dP = (unsigned int *) mlib_ImageGetData(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            dP = (unsigned int *) sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        printf("src is\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            printf("%x ",dP[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            dP = (unsigned int *)mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            dP = (unsigned int *) ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        printf("dst is\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        for (i=0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            printf("%x ",dP[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    /* Means that we couldn't write directly into the destination buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        unsigned char *bdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        unsigned short *sdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        /* Need to store it back into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if (storeRasterArray(env, srcRasterP, dstRasterP, dst) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            /* Punt for now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            switch (dst->type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                bdataP  = (unsigned char *) mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                retStatus = (awt_setPixelByte(env, -1, dstRasterP, bdataP) >= 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                sdataP  = (unsigned short *) mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                retStatus = (awt_setPixelShort(env, -1, dstRasterP, sdataP) >= 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /* Release the pinned memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    freeDataArray(env, srcRasterP->jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                  dstRasterP->jdata, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    if (s_timeIt) (*stop_timer)(3600,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    return retStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
Java_sun_awt_image_ImagingLib_lookupByteBI(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                                           jobject jsrc, jobject jdst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                                           jobjectArray jtableArrays)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    mlib_image *src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    mlib_image *dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    void *sdata, *ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    unsigned char **table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    unsigned char **tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    unsigned char lut[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    int retStatus = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    mlib_status status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    int jlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    jobject *jtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    BufImageS_t *srcImageP, *dstImageP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    int nbands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    int ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    mlibHintS_t hint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    /* This function requires a lot of local refs ??? Is 64 enough ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    if ((*env)->EnsureLocalCapacity(env, 64) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    if (s_nomlib) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    if (s_timeIt) (*start_timer)(3600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    /* Parse the source image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    if ((status = awt_parseImage(env, jsrc, &srcImageP, FALSE)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /* Parse the destination image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    if ((status = awt_parseImage(env, jdst, &dstImageP, FALSE)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        /* Can't handle any custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    jlen = (*env)->GetArrayLength(env, jtableArrays);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    ncomponents = srcImageP->cmodel.isDefaultCompatCM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        ? 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        : srcImageP->cmodel.numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    tbl = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    if (SAFE_TO_ALLOC_2(ncomponents, sizeof(unsigned char *))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        tbl = (unsigned char **)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            calloc(1, ncomponents * sizeof(unsigned char *));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    jtable = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    if (SAFE_TO_ALLOC_2(jlen, sizeof(jobject *))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        jtable = (jobject *)malloc(jlen * sizeof (jobject *));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    table = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    if (SAFE_TO_ALLOC_2(jlen, sizeof(unsigned char *))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        table = (unsigned char **)malloc(jlen * sizeof(unsigned char *));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    if (tbl == NULL || table == NULL || jtable == NULL) {
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1218
        if (tbl != NULL) free(tbl);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1219
        if (table != NULL) free(table);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1220
        if (jtable != NULL) free(jtable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        JNU_ThrowNullPointerException(env, "NULL LUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    /* Need to grab these pointers before we lock down arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    for (i=0; i < jlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        jtable[i] = (*env)->GetObjectArrayElement(env, jtableArrays, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        if (jtable[i] == NULL) {
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1230
            free(tbl);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1231
            free(table);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1232
            free(jtable);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1233
            awt_freeParsedImage(srcImageP, TRUE);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1234
            awt_freeParsedImage(dstImageP, TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    nbands = setImageHints(env, srcImageP, dstImageP, FALSE, TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                        FALSE, &hint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    if (nbands < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        /* Can't handle any custom images */
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1243
        free(tbl);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1244
        free(table);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1245
        free(jtable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    /* Allocate the arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    if (allocateArray(env, srcImageP, &src, &sdata, TRUE, FALSE, FALSE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        /* Must be some problem */
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1254
        free(tbl);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1255
        free(table);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1256
        free(jtable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    if (allocateArray(env, dstImageP, &dst, &ddata, FALSE, FALSE, FALSE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        /* Must be some problem */
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1263
        free(tbl);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1264
        free(table);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1265
        free(jtable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        freeArray(env, srcImageP, src, sdata, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    /* Set up a straight lut so we don't mess around with alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * NB: medialib lookup routine expects lookup array for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * component of source image including alpha.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * If lookup table we got form the java layer does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * sufficient number of lookup arrays we add references to identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * lookup array to make medialib happier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    if (jlen < ncomponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        /* REMIND: This should be the size of the input lut!! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        for (j=0; j < 256; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            lut[j] = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        for (j=0; j < ncomponents; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            tbl[j] = lut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    for (i=0; i < jlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        table[i] = (unsigned char *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            (*env)->GetPrimitiveArrayCritical(env, jtable[i], NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        if (table[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            /* Free what we've got so far. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            for (j = 0; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                (*env)->ReleasePrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                                                      jtable[j],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                                                      (jbyte *) table[j],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                                      JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            }
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1304
            free(tbl);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1305
            free(table);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1306
            free(jtable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            freeArray(env, srcImageP, src, sdata, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        tbl[srcImageP->hints.colorOrder[i]] = table[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    if (jlen == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        for (i=1; i < nbands -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                 srcImageP->cmodel.supportsAlpha; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            tbl[srcImageP->hints.colorOrder[i]] = table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    /* Mlib needs 16bit lookuptable and must be signed! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    if (src->type == MLIB_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        unsigned short *sdataP = (unsigned short *) src->data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        unsigned short *sP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        if (dst->type == MLIB_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            unsigned char *cdataP  = (unsigned char *)  dst->data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            unsigned char *cP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            if (nbands > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                for (y=0; y < src->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    cP = cdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    sP = sdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                    for (x=0; x < src->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                        *cP++ = table[0][*sP++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                     * 4554571: increment pointers using the scanline stride
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                     * in pixel units (not byte units)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    cdataP += dstImageP->raster.scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    sdataP += srcImageP->raster.scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        /* How about ddata == null? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    else if ((status = (*sMlibFns[MLIB_LOOKUP].fptr)(dst, src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                                      (void **)tbl) != MLIB_SUCCESS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        printMedialibError(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * Means that we couldn't write directly into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * the destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        /* Need to store it back into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        if (storeImageArray(env, srcImageP, dstImageP, dst) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            /* Error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    /* Release the LUT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    for (i=0; i < jlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        (*env)->ReleasePrimitiveArrayCritical(env, jtable[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                                              (jbyte *) table[i], JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    free ((void *) jtable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    free ((void *) table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    free ((void *) tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    /* Release the pinned memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    freeArray(env, srcImageP, src, sdata, dstImageP, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    awt_freeParsedImage(srcImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    awt_freeParsedImage(dstImageP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    if (s_timeIt) (*stop_timer)(3600, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    return retStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
Java_sun_awt_image_ImagingLib_lookupByteRaster(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                                               jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                                               jobject jsrc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                                               jobject jdst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                                               jobjectArray jtableArrays)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    RasterS_t*     srcRasterP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    RasterS_t*     dstRasterP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    mlib_image*    src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    mlib_image*    dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    void*          sdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    void*          ddata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    jobject        jtable[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    unsigned char* table[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    int            i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    int            retStatus = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    mlib_status    status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    int            jlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    int            lut_nbands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    int            src_nbands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    int            dst_nbands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    unsigned char  ilut[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /* This function requires a lot of local refs ??? Is 64 enough ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    if ((*env)->EnsureLocalCapacity(env, 64) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    if (s_nomlib) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    if (s_timeIt) (*start_timer)(3600);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    if ((srcRasterP = (RasterS_t*) calloc(1, sizeof(RasterS_t))) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        JNU_ThrowOutOfMemoryError(env, "Out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    if ((dstRasterP = (RasterS_t *) calloc(1, sizeof(RasterS_t))) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        JNU_ThrowOutOfMemoryError(env, "Out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        free(srcRasterP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    /* Parse the source raster - reject custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    if ((status = awt_parseRaster(env, jsrc, srcRasterP)) <= 0) {
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1436
        free(srcRasterP);
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1437
        free(dstRasterP);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    /* Parse the destination image - reject custom images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    if ((status = awt_parseRaster(env, jdst, dstRasterP)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        awt_freeParsedRaster(srcRasterP, TRUE);
4249
b890f1c39c38 6888167: memory leaks in the medialib glue code
bae
parents: 2600
diff changeset
  1444
        free(dstRasterP);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    jlen = (*env)->GetArrayLength(env, jtableArrays);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    lut_nbands = jlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    src_nbands = srcRasterP->numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    dst_nbands = dstRasterP->numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    /* MediaLib can't do more than 4 bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    if (src_nbands <= 0 || src_nbands > 4 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        dst_nbands <= 0 || dst_nbands > 4 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        lut_nbands <= 0 || lut_nbands > 4 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        src_nbands != dst_nbands ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        ((lut_nbands != 1) && (lut_nbands != src_nbands)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        // we should free parsed rasters here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    /* Allocate the raster arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    if (allocateRasterArray(env, srcRasterP, &src, &sdata, TRUE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    if (allocateRasterArray(env, dstRasterP, &dst, &ddata, FALSE) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        /* Must be some problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        freeDataArray(env, srcRasterP->jdata, src, sdata, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * Well, until now we have analyzed number of bands in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * src and dst rasters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * However, it is not enough because medialib lookup routine uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * number of channels of medialib image. Note that in certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * case number of channels may differs form the number of bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * Good example is raster that is used in TYPE_INT_RGB buffered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * image: it has 3 bands, but their medialib representation has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * 4 channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * In order to avoid the lookup routine failure, we need:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * 1. verify that src and dst have same number of channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * 2. provide lookup array for every channel. If we have "extra"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     *    channel (like the raster described above) then we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     *    provide identical lookup array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    if (src->channels != dst->channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        freeDataArray(env, srcRasterP->jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                      dstRasterP->jdata, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    if (src_nbands < src->channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            ilut[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    /* Get references to the lookup table arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    /* Need to grab these pointers before we lock down arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    for (i=0; i < lut_nbands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        jtable[i] = (*env)->GetObjectArrayElement(env, jtableArrays, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        if (jtable[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    for (i=0; i < lut_nbands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        table[i] = (unsigned char *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            (*env)->GetPrimitiveArrayCritical(env, jtable[i], NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        if (table[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            /* Free what we've got so far. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            for (j = 0; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                (*env)->ReleasePrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                                                      jtable[j],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                                                      (jbyte *) table[j],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                                                      JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            freeDataArray(env, srcRasterP->jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                          dstRasterP->jdata, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * Medialib routine expects lookup array for each band of raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * Setup the  rest of lookup arrays if supplied lookup table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * contains single lookup array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    for (i = lut_nbands; i < src_nbands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        table[i] = table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * Setup lookup array for "extra" channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    for ( ; i < src->channels; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        table[i] = ilut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
#define NLUT 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    /* Mlib needs 16bit lookuptable and must be signed! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    if (src->type == MLIB_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        unsigned short *sdataP = (unsigned short *) src->data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        unsigned short *sP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        if (dst->type == MLIB_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            unsigned char *cdataP  = (unsigned char *)  dst->data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            unsigned char *cP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            if (lut_nbands > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                unsigned int mask = NLUT-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                unsigned char* pLut = table[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                unsigned int endianTest = 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                for (y=0; y < src->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                    int nloop, nx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                    unsigned short* srcP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                    int* dstP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                    int npix = src->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                    cP = cdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                    sP = sdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                    /* Get to 32 bit-aligned point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                    while(((uintptr_t)cP & 0x3) != 0 && npix>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                          *cP++ = pLut[*sP++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                          npix--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                     * Do NLUT pixels per loop iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                     * Pack into ints and write out 2 at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                    nloop = npix/NLUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                    nx = npix%NLUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                    srcP = sP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                    dstP = (int*)cP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                    if(((char*)(&endianTest))[0] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                        /* Big endian loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                        for(x=nloop; x!=0; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                            dstP[0] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                                    ((pLut[srcP[0]] << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                                     (pLut[srcP[1]] << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                                     (pLut[srcP[2]] << 8)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                                      pLut[srcP[3]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                            dstP[1] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                                    ((pLut[srcP[4]] << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                                     (pLut[srcP[5]] << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                                     (pLut[srcP[6]] << 8)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                                      pLut[srcP[7]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                            dstP += NLUT/4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                            srcP += NLUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                        /* Little endian loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                        for(x=nloop; x!=0; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                            dstP[0] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                                    ((pLut[srcP[3]] << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                                     (pLut[srcP[2]] << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                                     (pLut[srcP[1]] << 8)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                                      pLut[srcP[0]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                            dstP[1] = (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                                    ((pLut[srcP[7]] << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                                     (pLut[srcP[6]] << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                                     (pLut[srcP[5]] << 8)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                                      pLut[srcP[4]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                            dstP += NLUT/4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                            srcP += NLUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                     * Complete any remaining pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                    cP = cP + NLUT * nloop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                    sP = sP + NLUT * nloop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                    for(x=nx; x!=0; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                        *cP++ = pLut[*sP++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                     * 4554571: increment pointers using the scanline stride
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                     * in pixel units (not byte units)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    cdataP += dstRasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                    sdataP += srcRasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        /* How about ddata == null? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    } else if ((status = (*sMlibFns[MLIB_LOOKUP].fptr)(dst, src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                                      (void **)table) != MLIB_SUCCESS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        printMedialibError(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * Means that we couldn't write directly into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * the destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    if (ddata == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        unsigned char*  bdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        unsigned short* sdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        switch (dstRasterP->dataType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
          case BYTE_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            bdataP  = (unsigned char *) mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            retStatus = (awt_setPixelByte(env, -1, dstRasterP, bdataP) >= 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
          case SHORT_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            sdataP  = (unsigned short *) mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            retStatus = (awt_setPixelShort(env, -1, dstRasterP, sdataP) >= 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            retStatus = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    /* Release the LUT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    for (i=0; i < lut_nbands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        (*env)->ReleasePrimitiveArrayCritical(env, jtable[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                                              (jbyte *) table[i], JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    /* Release the pinned memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    freeDataArray(env, srcRasterP->jdata, src, sdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                  dstRasterP->jdata, dst, ddata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    awt_freeParsedRaster(srcRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    awt_freeParsedRaster(dstRasterP, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    if (s_timeIt) (*stop_timer)(3600, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    return retStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
Java_sun_awt_image_ImagingLib_init(JNIEnv *env, jclass thisClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    char *start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    if (getenv("IMLIB_DEBUG")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        start_timer = awt_setMlibStartTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        stop_timer = awt_setMlibStopTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        if (start_timer && stop_timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            s_timeIt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    if (getenv("IMLIB_PRINT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        s_printIt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    if ((start = getenv("IMLIB_START")) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        sscanf(start, "%d", &s_startOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    if (getenv ("IMLIB_NOMLIB")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        s_nomlib = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    /* This function is platform-dependent and is in awt_mlib.c */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    if (awt_getImagingLib(env, (mlibFnS_t *)&sMlibFns, &sMlibSysFns) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        MLIB_SUCCESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        s_nomlib = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
/* REMIND: How to specify border? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
static void extendEdge(JNIEnv *env, BufImageS_t *imageP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                       int *widthP, int *heightP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    RasterS_t *rasterP = &imageP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
    int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    /* Useful for convolution? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    jobject jbaseraster = (*env)->GetObjectField(env, rasterP->jraster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                                                 g_RasterBaseRasterID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    width = rasterP->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    height = rasterP->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
#ifdef WORKING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    if (! JNU_IsNull(env, jbaseraster) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        !(*env)->IsSameObject(env, rasterP->jraster, jbaseraster)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        int xOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        int yOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        int baseWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        int baseHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        int baseXoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        int baseYoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        /* Not the same object so get the width and height */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        xOff = (*env)->GetIntField(env, rasterP->jraster, g_RasterXOffsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        yOff = (*env)->GetIntField(env, rasterP->jraster, g_RasterYOffsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        baseWidth  = (*env)->GetIntField(env, jbaseraster, g_RasterWidthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        baseHeight = (*env)->GetIntField(env, jbaseraster, g_RasterHeightID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        baseXoff   = (*env)->GetIntField(env, jbaseraster, g_RasterXOffsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        baseYoff   = (*env)->GetIntField(env, jbaseraster, g_RasterYOffsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        if (xOff + rasterP->width < baseXoff + baseWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            width++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        if (yOff + rasterP->height < baseYoff + baseHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            height++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
setImageHints(JNIEnv *env, BufImageS_t *srcP, BufImageS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
              int expandICM, int useAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
              int premultiply, mlibHintS_t *hintP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    ColorModelS_t *srcCMP = &srcP->cmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    ColorModelS_t *dstCMP = &dstP->cmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    int nbands = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    int ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    hintP->dataType = srcP->raster.dataType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
    hintP->addAlpha = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    /* Are the color spaces the same? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    if (srcCMP->csType != dstCMP->csType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        /* If the src is GRAY and dst RGB, we can handle it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        if (!(srcCMP->csType == java_awt_color_ColorSpace_TYPE_GRAY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
              dstCMP->csType == java_awt_color_ColorSpace_TYPE_RGB)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            /* Nope, need to handle that in java for now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            hintP->cvtSrcToDefault = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        if (srcP->hints.needToExpand) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            hintP->cvtSrcToDefault = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            /* Need to initialize this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            hintP->cvtSrcToDefault = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    ncomponents = srcCMP->numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    if ((useAlpha == 0) && srcCMP->supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        ncomponents--;  /* ?? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        /* Not really, more like shrink src to get rid of alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        hintP->cvtSrcToDefault = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    hintP->dataType = srcP->raster.dataType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    if (hintP->cvtSrcToDefault == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        if (srcCMP->cmType == INDEX_CM_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            if (expandICM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                nbands = srcCMP->numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                hintP->cvtSrcToDefault = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                if (dstCMP->isDefaultCompatCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                    hintP->allocDefaultDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    hintP->cvtToDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                else if (dstCMP->isDefaultCompatCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                    hintP->allocDefaultDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                    hintP->cvtToDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                nbands = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                hintP->cvtSrcToDefault = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            if (srcP->hints.packing & INTERLEAVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                nbands = srcCMP->numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                nbands = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            /* Look at the packing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            if ((srcP->hints.packing&BYTE_INTERLEAVED)==BYTE_INTERLEAVED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                (srcP->hints.packing&SHORT_INTERLEAVED)==SHORT_INTERLEAVED||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                (srcP->hints.packing&BYTE_SINGLE_BAND) == BYTE_SINGLE_BAND||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                (srcP->hints.packing&SHORT_SINGLE_BAND)==SHORT_SINGLE_BAND||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                (srcP->hints.packing&BYTE_BANDED)  == BYTE_BANDED       ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                (srcP->hints.packing&SHORT_BANDED) == SHORT_BANDED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                /* Can use src directly */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                hintP->cvtSrcToDefault = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                /* Must be packed or custom */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                hintP->cvtSrcToDefault = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
    if (hintP->cvtSrcToDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        /* By definition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        nbands = 4;  /* What about alpha? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        hintP->dataType = BYTE_DATA_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        hintP->needToCopy = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        if (srcP->imageType == dstP->imageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            hintP->cvtToDst = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        else if (dstP->cmodel.isDefaultCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            /* Not necessarily */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            hintP->cvtToDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            hintP->cvtToDst = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        int srcImageType = srcP->imageType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        int dstImageType = dstP->imageType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        /* Special case where we need to fill in alpha values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        if (srcCMP->isDefaultCompatCM && dstCMP->isDefaultCompatCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            if (!srcCMP->supportsAlpha &&dstCMP->supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                hintP->addAlpha = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            for (i=0; i < srcCMP->numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                if (srcP->hints.colorOrder[i] != dstP->hints.colorOrder[i]){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                    if (!srcCMP->isDefaultCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                        hintP->cvtSrcToDefault = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                        srcImageType = java_awt_image_BufferedImage_TYPE_INT_ARGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                    if (!dstCMP->isDefaultCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                        hintP->cvtToDst = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                        dstImageType = java_awt_image_BufferedImage_TYPE_INT_ARGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        else if (srcCMP->cmType != INDEX_CM_TYPE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                 !srcCMP->supportsAlpha && dstCMP->supportsAlpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            /* We've already handled the index case.  This is for the rest of the cases */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            srcImageType = java_awt_image_BufferedImage_TYPE_INT_ARGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            hintP->cvtSrcToDefault = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        hintP->allocDefaultDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        if (srcImageType == dstImageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            /* Same image type so use it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            hintP->cvtToDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        else if (srcImageType == TYPE_INT_RGB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                 (dstImageType == TYPE_INT_ARGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                  dstImageType == TYPE_INT_ARGB_PRE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            hintP->cvtToDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        else if (srcImageType == TYPE_INT_BGR &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                 (dstImageType == TYPE_4BYTE_ABGR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                  dstImageType == TYPE_4BYTE_ABGR_PRE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            hintP->cvtToDst = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        else if (srcP->hints.packing == dstP->hints.packing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            /* Now what? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            /* Check color order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            /* Check if just need to scale the data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            hintP->cvtToDst = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            /* Don't know what it is so convert it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            hintP->allocDefaultDst = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            hintP->cvtToDst = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        hintP->needToCopy = (ncomponents > nbands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    return nbands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
expandPacked(JNIEnv *env, BufImageS_t *img, ColorModelS_t *cmP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
             RasterS_t *rasterP, int component, unsigned char *bdataP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    if (rasterP->rasterType == COMPONENT_RASTER_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        switch (rasterP->dataType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        case BYTE_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            if (expandPackedBCR(env, rasterP, component, bdataP) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                /* Must have been an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        case SHORT_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            if (expandPackedICR(env, rasterP, component, bdataP) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                /* Must have been an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        case INT_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            if (expandPackedICR(env, rasterP, component, bdataP) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                /* Must have been an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            /* REMIND: Return some sort of error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        /* REMIND: Return some sort of error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
cvtCustomToDefault(JNIEnv *env, BufImageS_t *imageP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                   unsigned char *dataP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    ColorModelS_t *cmP = &imageP->cmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    RasterS_t *rasterP = &imageP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    int y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    jobject jpixels = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    jint *pixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    unsigned char *dP = dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
#define NUM_LINES    10
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    int numLines = NUM_LINES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    int nbytes = rasterP->width*4*NUM_LINES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    for (y=0; y < rasterP->height; y+=numLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        /* getData, one scanline at a time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        if (y+numLines > rasterP->height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            numLines = rasterP->height - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            nbytes = rasterP->width*4*numLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        jpixels = (*env)->CallObjectMethod(env, imageP->jimage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                                           g_BImgGetRGBMID, 0, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                                           rasterP->width, numLines,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                                           jpixels,0, rasterP->width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        if (jpixels == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            JNU_ThrowInternalError(env, "Can't retrieve pixels.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        pixels = (*env)->GetPrimitiveArrayCritical(env, jpixels, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        memcpy(dP, pixels, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        dP += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        (*env)->ReleasePrimitiveArrayCritical(env, jpixels, pixels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
cvtDefaultToCustom(JNIEnv *env, BufImageS_t *imageP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                   unsigned char *dataP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    ColorModelS_t *cmP = &imageP->cmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
    RasterS_t *rasterP = &imageP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    int y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    jint *pixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    unsigned char *dP = dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
#define NUM_LINES    10
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    int numLines = NUM_LINES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
    int nbytes = rasterP->width*4*NUM_LINES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    jintArray jpixels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    jpixels = (*env)->NewIntArray(env, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    if (JNU_IsNull(env, jpixels)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        JNU_ThrowOutOfMemoryError(env, "Out of Memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
    for (y=0; y < rasterP->height; y+=NUM_LINES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        if (y+numLines > rasterP->height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            numLines = rasterP->height - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            nbytes = rasterP->width*4*numLines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        pixels = (*env)->GetPrimitiveArrayCritical(env, jpixels, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        if (pixels == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            /* JNI error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        memcpy(pixels, dP, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        dP += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
       (*env)->ReleasePrimitiveArrayCritical(env, jpixels, pixels, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
       /* setData, one scanline at a time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
       /* Fix 4223648, 4184283 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
       (*env)->CallVoidMethod(env, imageP->jimage, g_BImgSetRGBMID, 0, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                                rasterP->width, numLines, jpixels, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                                rasterP->width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
       if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
           return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    /* Need to release the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    (*env)->DeleteLocalRef(env, jpixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
allocateArray(JNIEnv *env, BufImageS_t *imageP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
              mlib_image **mlibImagePP, void **dataPP, int isSrc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
              int cvtToDefault, int addAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    void *dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    unsigned char *cDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
    RasterS_t *rasterP = &imageP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
    ColorModelS_t *cmP = &imageP->cmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
    int dataType = BYTE_DATA_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
    int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    HintS_t *hintP = &imageP->hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    *dataPP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    width = rasterP->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
    height = rasterP->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    /* Useful for convolution? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
    /* This code is zero'ed out so that it cannot be called */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    /* To do this correctly, we need to expand src and dst in the     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    /* same direction up/down/left/right only if both can be expanded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    /* in that direction.  Expanding right and down is easy -         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    /* increment width.  Expanding top and left requires bumping      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    /* around pointers and incrementing the width/height              */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
    if (0 && useEdges) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        baseWidth  = rasterP->baseRasterWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        baseHeight = rasterP->baseRasterHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        baseXoff = rasterP->baseOriginX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        baseYoff = rasterP->baseOriginY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        if (rasterP->minX + rasterP->width < baseXoff + baseWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            width++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        if (rasterP->minY + rasterP->height < baseYoff + baseHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            height++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        if (rasterP->minX > baseXoff ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            width++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            /* NEED TO BUMP POINTER BACK A PIXELSTRIDE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        if (rasterP->minY  > baseYoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            height++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            /* NEED TO BUMP POINTER BACK A SCANLINE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
    if (cvtToDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        int status = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        *mlibImagePP = (*sMlibSysFns.createFP)(MLIB_BYTE, 4, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        cDataP  = (unsigned char *) mlib_ImageGetData(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        /* Make sure the image is cleared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        memset(cDataP, 0, width*height*4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        if (!isSrc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        switch(imageP->cmodel.cmType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        case INDEX_CM_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
            /* REMIND: Need to rearrange according to dst cm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            /* Fix 4213160, 4184283 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            if (rasterP->rasterType == COMPONENT_RASTER_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                return expandICM(env, imageP, (unsigned int *)cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                return cvtCustomToDefault(env, imageP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        case DIRECT_CM_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
            switch(imageP->raster.dataType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            case BYTE_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                return expandPackedBCRdefault(env, rasterP, -1, cDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                                              !imageP->cmodel.supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            case SHORT_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                return expandPackedSCRdefault(env, rasterP, -1, cDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                                              !imageP->cmodel.supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            case INT_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                return expandPackedICRdefault(env, rasterP, -1, cDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                                              !imageP->cmodel.supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        } /* switch(imageP->cmodel.cmType) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        return cvtCustomToDefault(env, imageP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    /* Interleaved with shared data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
    dataP = (void *) (*env)->GetPrimitiveArrayCritical(env, rasterP->jdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                                                       NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    if (dataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
    /* Means we need to fill in alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    if (!cvtToDefault && addAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        *mlibImagePP = (*sMlibSysFns.createFP)(MLIB_BYTE, 4, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        if (*mlibImagePP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            unsigned int *dstP  = (unsigned int *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                mlib_ImageGetData(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            int dstride = (*mlibImagePP)->stride>>2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
            int sstride = hintP->sStride>>2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
            unsigned int *srcP = (unsigned int *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                ((unsigned char *)dataP + hintP->dataOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            unsigned int *dP, *sP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
            int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
            for (y=0; y < height; y++, srcP += sstride, dstP += dstride){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                sP = srcP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                dP = dstP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                for (x=0; x < width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    dP[x] = sP[x] | 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, dataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
    else if ((hintP->packing & BYTE_INTERLEAVED) == BYTE_INTERLEAVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        int nChans = (cmP->isDefaultCompatCM ? 4 : hintP->numChans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        /* Easy case.  It is or is similar to the default CM so use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * the array.  Must be byte data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            /* Create the medialib image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        *mlibImagePP = (*sMlibSysFns.createStructFP)(MLIB_BYTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                                              nChans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                                              width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                                              height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                                              hintP->sStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                                              (unsigned char *)dataP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                                              + hintP->dataOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    else if ((hintP->packing & SHORT_INTERLEAVED) == SHORT_INTERLEAVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        *mlibImagePP = (*sMlibSysFns.createStructFP)(MLIB_SHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                                              hintP->numChans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                                              width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                                              height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                                              imageP->raster.scanlineStride*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                                              (unsigned short *)dataP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                                              + hintP->channelOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        /* Release the data array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, dataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    *dataPP = dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
allocateRasterArray(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                    mlib_image **mlibImagePP, void **dataPP, int isSrc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    void *dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
    unsigned char *cDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    unsigned short *sdataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
    int dataType = BYTE_DATA_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
    int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
    int height;
5187
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2242
    int dataSize;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2243
    int offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
    *dataPP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
    width = rasterP->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
    height = rasterP->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
    if (rasterP->numBands <= 0 || rasterP->numBands > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        /* REMIND: Fix this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    /* Useful for convolution? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
    /* This code is zero'ed out so that it cannot be called */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    /* To do this correctly, we need to expand src and dst in the     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    /* same direction up/down/left/right only if both can be expanded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    /* in that direction.  Expanding right and down is easy -         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    /* increment width.  Expanding top and left requires bumping      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    /* around pointers and incrementing the width/height              */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
    if (0 && useEdges) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        baseWidth  = rasterP->baseRasterWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        baseHeight = rasterP->baseRasterHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        baseXoff = rasterP->baseOriginX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        baseYoff = rasterP->baseOriginY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        if (rasterP->minX + rasterP->width < baseXoff + baseWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            width++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        if (rasterP->minY + rasterP->height < baseYoff + baseHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            height++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
        if (rasterP->minX > baseXoff ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
            width++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            /* NEED TO BUMP POINTER BACK A PIXELSTRIDE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        if (rasterP->minY  > baseYoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            /* Can use edge */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            height++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            /* NEED TO BUMP POINTER BACK A SCANLINE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    switch (rasterP->type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
    case sun_awt_image_IntegerComponentRaster_TYPE_INT_8BIT_SAMPLES:
5187
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2296
        if (!((rasterP->chanOffsets[0] == 0 || SAFE_TO_ALLOC_2(rasterP->chanOffsets[0], 4)) &&
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2297
              SAFE_TO_ALLOC_2(width, 4) &&
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2298
              SAFE_TO_ALLOC_3(height, rasterP->scanlineStride, 4)))
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2299
        {
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2300
            return -1;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2301
        }
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2302
        offset = 4 * rasterP->chanOffsets[0];
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2303
        dataSize = 4 * (*env)->GetArrayLength(env, rasterP->jdata);
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2304
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2305
        if (offset < 0 || offset >= dataSize ||
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2306
            width > rasterP->scanlineStride ||
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2307
            height * rasterP->scanlineStride * 4 > dataSize - offset)
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2308
        {
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2309
            // raster data buffer is too short
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2310
            return -1;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2311
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        dataP = (void *) (*env)->GetPrimitiveArrayCritical(env, rasterP->jdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                                                           NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        if (dataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        *mlibImagePP = (*sMlibSysFns.createStructFP)(MLIB_BYTE, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                                              width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                                              rasterP->scanlineStride*4,
5187
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2320
                                              (unsigned char *)dataP + offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        *dataPP = dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
    case sun_awt_image_IntegerComponentRaster_TYPE_BYTE_SAMPLES:
5187
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2324
        if (!(SAFE_TO_ALLOC_2(width, rasterP->numBands) &&
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2325
              SAFE_TO_ALLOC_2(height, rasterP->scanlineStride)))
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2326
        {
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2327
            return -1;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2328
        }
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2329
        offset = rasterP->chanOffsets[0];
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2330
        dataSize = (*env)->GetArrayLength(env, rasterP->jdata);
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2331
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2332
        if (offset < 0 || offset >= dataSize ||
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2333
            width * rasterP->numBands > rasterP->scanlineStride ||
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2334
            height * rasterP->scanlineStride > dataSize - offset)
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2335
        {
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2336
            // raster data buffer is too short
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2337
            return -1;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2338
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        dataP = (void *) (*env)->GetPrimitiveArrayCritical(env, rasterP->jdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                                                           NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        if (dataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        *mlibImagePP = (*sMlibSysFns.createStructFP)(MLIB_BYTE, rasterP->numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                                              width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                                              rasterP->scanlineStride,
5187
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2347
                                              (unsigned char *)dataP + offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        *dataPP = dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    case sun_awt_image_IntegerComponentRaster_TYPE_USHORT_SAMPLES:
5187
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2351
        if (!((rasterP->chanOffsets[0] == 0 || SAFE_TO_ALLOC_2(rasterP->chanOffsets[0], 2)) &&
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2352
              SAFE_TO_ALLOC_3(width, rasterP->numBands, 2) &&
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2353
              SAFE_TO_ALLOC_3(height, rasterP->scanlineStride, 2)))
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2354
        {
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2355
              return -1;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2356
        }
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2357
        offset = rasterP->chanOffsets[0] * 2;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2358
        dataSize = 2 * (*env)->GetArrayLength(env, rasterP->jdata);
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2359
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2360
        if (offset < 0 || offset >= dataSize ||
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2361
            width * rasterP->numBands > rasterP->scanlineStride ||
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2362
            height * rasterP->scanlineStride * 2 > dataSize - offset)
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2363
        {
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2364
            // raster data buffer is too short
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2365
             return -1;
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2366
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        dataP = (void *) (*env)->GetPrimitiveArrayCritical(env, rasterP->jdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                                                           NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        if (dataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        *mlibImagePP = (*sMlibSysFns.createStructFP)(MLIB_SHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                                                     rasterP->numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                                                     width, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                                                     rasterP->scanlineStride*2,
5187
2358d9e7ca64 6914866: Sun JRE ImagingLib arbitrary code execution vulnerability
bae
parents: 4249
diff changeset
  2376
                                                     (unsigned char *)dataP + offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        *dataPP = dataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    case sun_awt_image_IntegerComponentRaster_TYPE_BYTE_PACKED_SAMPLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        *mlibImagePP = (*sMlibSysFns.createFP)(MLIB_BYTE, rasterP->numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                                        width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        if (!isSrc) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        cDataP  = (unsigned char *) mlib_ImageGetData(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        return expandPackedBCR(env, rasterP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    case sun_awt_image_IntegerComponentRaster_TYPE_USHORT_PACKED_SAMPLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        if (rasterP->sppsm.maxBitSize <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            *mlibImagePP = (*sMlibSysFns.createFP)(MLIB_BYTE, rasterP->numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                                            width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            if (!isSrc) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            cDataP  = (unsigned char *) mlib_ImageGetData(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            return expandPackedSCR(env, rasterP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    case sun_awt_image_IntegerComponentRaster_TYPE_INT_PACKED_SAMPLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        if (rasterP->sppsm.maxBitSize <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            *mlibImagePP = (*sMlibSysFns.createFP)(MLIB_BYTE, rasterP->numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                                            width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            if (!isSrc) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            cDataP  = (unsigned char *) mlib_ImageGetData(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            return expandPackedICR(env, rasterP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
    /* Just expand it right now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    switch (rasterP->dataType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
    case BYTE_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        if ((*mlibImagePP = (*sMlibSysFns.createFP)(MLIB_BYTE, rasterP->numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                                             width, height)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        if (isSrc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            cDataP  = (unsigned char *) mlib_ImageGetData(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            if (awt_getPixelByte(env, -1, rasterP, cDataP) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                (*sMlibSysFns.deleteImageFP)(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
    case SHORT_DATA_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        if ((*mlibImagePP = (*sMlibSysFns.createFP)(MLIB_SHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                                                    rasterP->numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                                                    width, height)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        if (isSrc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            sdataP  = (unsigned short *) mlib_ImageGetData(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            if (awt_getPixelShort(env, -1, rasterP, sdataP) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                (*sMlibSysFns.deleteImageFP)(*mlibImagePP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
freeArray(JNIEnv *env, BufImageS_t *srcimageP, mlib_image *srcmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
          void *srcdataP, BufImageS_t *dstimageP, mlib_image *dstmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
          void *dstdataP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    jobject srcJdata = (srcimageP != NULL ? srcimageP->raster.jdata : NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    jobject dstJdata = (dstimageP != NULL ? dstimageP->raster.jdata : NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    freeDataArray(env, srcJdata, srcmlibImP, srcdataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                  dstJdata, dstmlibImP, dstdataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
freeDataArray(JNIEnv *env, jobject srcJdata, mlib_image *srcmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
          void *srcdataP, jobject dstJdata, mlib_image *dstmlibImP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
          void *dstdataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
    /* Free the medialib image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
    if (srcmlibImP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        (*sMlibSysFns.deleteImageFP)(srcmlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
    /* Release the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    if (srcdataP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        (*env)->ReleasePrimitiveArrayCritical(env, srcJdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                                              srcdataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
    /* Free the medialib image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
    if (dstmlibImP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
        (*sMlibSysFns.deleteImageFP)(dstmlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
    /* Release the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
    if (dstdataP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        (*env)->ReleasePrimitiveArrayCritical(env, dstJdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                                              dstdataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
storeDstArray(JNIEnv *env,  BufImageS_t *srcP, BufImageS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
              mlibHintS_t *hintP, mlib_image *mlibImP, void *ddata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    RasterS_t *rasterP = &dstP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    /* Nothing to do since it is the same image type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    if (srcP->imageType == dstP->imageType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        && srcP->imageType != java_awt_image_BufferedImage_TYPE_CUSTOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        && srcP->imageType != java_awt_image_BufferedImage_TYPE_BYTE_INDEXED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
        && srcP->imageType != java_awt_image_BufferedImage_TYPE_BYTE_BINARY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        /* REMIND: Should check the ICM LUTS to see if it is the same */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
    /* These types are compatible with TYPE_INT_RGB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
    if (srcP->imageType == java_awt_image_BufferedImage_TYPE_INT_RGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        && (dstP->imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
           dstP->imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    if (hintP->cvtSrcToDefault &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        (srcP->cmodel.isAlphaPre == dstP->cmodel.isAlphaPre)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        if (srcP->cmodel.isAlphaPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
            if (dstP->imageType ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            if (!srcP->cmodel.supportsAlpha &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                dstP->imageType == java_awt_image_BufferedImage_TYPE_INT_RGB){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            /* REMIND: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
    if (dstP->cmodel.cmType == DIRECT_CM_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
        /* Just need to move bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        if (mlibImP->type == MLIB_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
            return awt_setPixelByte(env, -1, &dstP->raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                                    (unsigned char *) mlibImP->data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        else if (mlibImP->type == MLIB_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
            return awt_setPixelByte(env, -1, &dstP->raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                                    (unsigned char *) mlibImP->data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
storeImageArray(JNIEnv *env, BufImageS_t *srcP, BufImageS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                mlib_image *mlibImP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    int mStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
    unsigned char *cmDataP, *dataP, *cDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
    HintS_t *hintP = &dstP->hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
    RasterS_t *rasterP = &dstP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
    int y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    /* REMIND: Store mlib data type? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
    /* Check if it is an IndexColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
    if (dstP->cmodel.cmType == INDEX_CM_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        if (dstP->raster.rasterType == COMPONENT_RASTER_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            return storeICMarray(env, srcP, dstP, mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            /* Packed or some other custom raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            cmDataP = (unsigned char *) mlib_ImageGetData(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            return cvtDefaultToCustom(env, dstP, -1, cmDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    if (hintP->packing == BYTE_INTERLEAVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
        /* Write it back to the destination */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        cmDataP = (unsigned char *) mlib_ImageGetData(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        mStride = mlib_ImageGetStride(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        dataP = (unsigned char *)(*env)->GetPrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                                                      rasterP->jdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        if (dataP == NULL) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        cDataP = dataP + hintP->dataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        for (y=0; y < rasterP->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
             y++, cmDataP += mStride, cDataP += hintP->sStride)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            memcpy(cDataP, cmDataP, rasterP->width*hintP->numChans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, dataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    else if (hintP->packing == SHORT_INTERLEAVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        /* Write it back to the destination */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        unsigned short *sdataP, *sDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        unsigned short *smDataP = (unsigned short *)mlib_ImageGetData(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        mStride = mlib_ImageGetStride(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        sdataP = (unsigned short *)(*env)->GetPrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                                                      rasterP->jdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        if (sdataP == NULL) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        sDataP = sdataP + hintP->dataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        for (y=0; y < rasterP->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
             y++, smDataP += mStride, sDataP += hintP->sStride)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
            memcpy(sDataP, smDataP, rasterP->width*hintP->numChans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, sdataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
    else if (dstP->cmodel.cmType == DIRECT_CM_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        /* Just need to move bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        if (mlibImP->type == MLIB_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
            if (dstP->hints.packing == PACKED_BYTE_INTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                return setPackedBCRdefault(env, rasterP, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                                           (unsigned char *) mlibImP->data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                                           dstP->cmodel.supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            } else if (dstP->hints.packing == PACKED_SHORT_INTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                return setPackedSCRdefault(env, rasterP, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                                           (unsigned char *) mlibImP->data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                                           dstP->cmodel.supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            } else if (dstP->hints.packing == PACKED_INT_INTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                return setPackedICRdefault(env, rasterP, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                                           (unsigned char *) mlibImP->data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                                           dstP->cmodel.supportsAlpha);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
        else if (mlibImP->type == MLIB_SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            return awt_setPixelShort(env, -1, rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                                    (unsigned short *) mlibImP->data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        return cvtDefaultToCustom(env, dstP, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                                  (unsigned char *)mlibImP->data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
storeRasterArray(JNIEnv *env, RasterS_t *srcP, RasterS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                mlib_image *mlibImP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
    unsigned char *cDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
    switch(dstP->type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
    case sun_awt_image_IntegerComponentRaster_TYPE_BYTE_PACKED_SAMPLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
        cDataP  = (unsigned char *) mlib_ImageGetData(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        return setPackedBCR(env, dstP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
    case sun_awt_image_IntegerComponentRaster_TYPE_USHORT_PACKED_SAMPLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        if (dstP->sppsm.maxBitSize <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            cDataP  = (unsigned char *) mlib_ImageGetData(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            return setPackedSCR(env, dstP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
    case sun_awt_image_IntegerComponentRaster_TYPE_INT_PACKED_SAMPLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        if (dstP->sppsm.maxBitSize <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            cDataP  = (unsigned char *) mlib_ImageGetData(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            return setPackedICR(env, dstP, -1, cDataP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
storeICMarray(JNIEnv *env, BufImageS_t *srcP, BufImageS_t *dstP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
              mlib_image *mlibImP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
    int *argb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
    unsigned char *dataP, *cDataP, *cP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    unsigned char *sP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
    int aIdx, rIdx, gIdx, bIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
    ColorModelS_t *cmodelP = &dstP->cmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
    RasterS_t *rasterP = &dstP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
    /* REMIND: Only works for RGB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
    if (cmodelP->csType != java_awt_color_ColorSpace_TYPE_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        JNU_ThrowInternalError(env, "Writing to non-RGB images not implemented yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
    if (srcP->imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        srcP->imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        srcP->imageType == java_awt_image_BufferedImage_TYPE_INT_RGB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        aIdx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        rIdx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        gIdx = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
        bIdx = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
    else if (srcP->imageType ==java_awt_image_BufferedImage_TYPE_4BYTE_ABGR||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
        srcP->imageType == java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        aIdx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
        rIdx = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        gIdx = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        bIdx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    else if (srcP->imageType == java_awt_image_BufferedImage_TYPE_3BYTE_BGR){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        rIdx = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        gIdx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        bIdx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        aIdx = 0;       /* Ignored */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
    else if (srcP->cmodel.cmType == INDEX_CM_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        rIdx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        gIdx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        bIdx = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        aIdx = 3;   /* Use supportsAlpha to see if it is really there */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
    /* Lock down the destination raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
    dataP = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                                                  rasterP->jdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
    if (dataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
    argb = (*env)->GetPrimitiveArrayCritical(env, cmodelP->jrgb, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
    if (argb == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
        (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, dataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                                              JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
    cDataP = dataP + dstP->hints.dataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
    sP = (unsigned char *) mlib_ImageGetData(mlibImP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
    for (y=0; y < rasterP->height; y++, cDataP += rasterP->scanlineStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        cP = cDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        for (x=0; x < rasterP->width; x++, cP += rasterP->pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            *cP = colorMatch(sP[rIdx], sP[gIdx], sP[bIdx], sP[aIdx],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                             (unsigned char *)argb, cmodelP->mapSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
            sP += cmodelP->numComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
    (*env)->ReleasePrimitiveArrayCritical(env, cmodelP->jrgb, argb, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
    (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata, dataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                                          JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
static int expandICM(JNIEnv *env, BufImageS_t *imageP, unsigned int *mDataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
    ColorModelS_t *cmP = &imageP->cmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
    RasterS_t *rasterP = &imageP->raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
    HintS_t *hintP     = &imageP->hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
    int *rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
    int status = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
    unsigned char *dataP, *cP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
    unsigned int *mP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
    int width = rasterP->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
    int height = rasterP->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
    int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
    /* Need to grab the lookup tables.  Right now only bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
    rgb = (int *) (*env)->GetPrimitiveArrayCritical(env, cmP->jrgb, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
    /* Interleaved with shared data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
    dataP = (void *) (*env)->GetPrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                                                       rasterP->jdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
    if (rgb == NULL || dataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
        /* Release the lookup tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        if (rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            (*env)->ReleasePrimitiveArrayCritical(env, cmP->jrgb, rgb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                                                  JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        if (dataP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            (*env)->ReleasePrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                                                  rasterP->jdata, dataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                                                  JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    if (rasterP->dataType == BYTE_DATA_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        unsigned char *cDataP = ((unsigned char *)dataP) + hintP->dataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
        for (y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
            mP = mDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            cP = cDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
            for (x=0; x < width; x++, cP += rasterP->pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                *mP++ = rgb[*cP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
            mDataP += width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            cDataP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
    else if (rasterP->dataType == SHORT_DATA_TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
        unsigned short *sDataP, *sP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
        sDataP = ((unsigned short *)dataP) + hintP->channelOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        for (y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
            mP = mDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            sP = sDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
            for (x=0; x < width; x++, sP+=rasterP->pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                *mP++ = rgb[*sP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
            mDataP += width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            sDataP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        /* Unknown type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        status = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
    /* Release the lookup table data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
    (*env)->ReleasePrimitiveArrayCritical(env, imageP->cmodel.jrgb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                                          rgb, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
    /* Release the data array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
    (*env)->ReleasePrimitiveArrayCritical(env, rasterP->jdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                                          dataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
    return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
/* This routine is expecting a ByteComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
static int expandPackedBCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                           unsigned char *outDataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
    unsigned char *outP = outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
    unsigned char *lineInP, *inP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
    jarray jInDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
    jint   *inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
    jInDataP = (*env)->GetObjectField(env, rasterP->jraster, g_BCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
    inDataP = (*env)->GetPrimitiveArrayCritical(env, jInDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
    if (inDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
    lineInP =  (unsigned char *)inDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
            roff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
            if (roff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                loff[c] = -roff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
                roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
            else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
        if (rasterP->numBands < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
            /* Need to put in alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                    for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                            (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
                             <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                    for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                            (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
                             <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        roff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        if (roff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
            loff[0] = -roff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            roff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                    ((*inP & rasterP->sppsm.maskArray[c])>>roff[0])<<loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
    (*env)->ReleasePrimitiveArrayCritical(env, jInDataP, inDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
/* This routine is expecting a ByteComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
static int expandPackedBCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                                  int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                                  int forceAlpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
    unsigned char *outP = outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
    unsigned char *lineInP, *inP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
    jarray jInDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
    jint   *inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
    int numBands = rasterP->numBands - (forceAlpha ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
    int a = numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
    jInDataP = (*env)->GetObjectField(env, rasterP->jraster, g_BCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
    inDataP = (*env)->GetPrimitiveArrayCritical(env, jInDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
    if (inDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
    lineInP =  (unsigned char *)inDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
            roff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
            if (roff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                loff[c] = -roff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
            else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
        /* Need to put in alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
        if (forceAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                    *outP++ = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                    for (c=0; c < numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                            (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                             <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
                    *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                        (((*inP&rasterP->sppsm.maskArray[a]) >> roff[a])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                         <<loff[a]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                    for (c=0; c < numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                            (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                             <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        roff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        if (roff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            loff[0] = -roff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
            roff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
            inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                    ((*inP & rasterP->sppsm.maskArray[c])>>roff[0])<<loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
            lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
    (*env)->ReleasePrimitiveArrayCritical(env, jInDataP, inDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
/* This routine is expecting a ShortComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
static int expandPackedSCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                           unsigned char *outDataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
    unsigned char *outP = outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
    unsigned short *lineInP, *inP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
    jarray jInDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
    jint   *inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
    jInDataP = (*env)->GetObjectField(env, rasterP->jraster, g_SCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
    inDataP = (*env)->GetPrimitiveArrayCritical(env, jInDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
    if (inDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
    lineInP =  (unsigned short *)inDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
            roff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
            if (roff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                loff[c] = -roff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
            else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
        if (rasterP->numBands < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
            /* Need to put in alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                    for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                         *Not correct.  Might need to unpremult,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                         * shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                            (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                             <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
                    for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
                         *Not correct.  Might need to unpremult,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
                         * shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                            (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                             <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        roff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
        if (roff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
            loff[0] = -roff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
            roff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
        else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
                *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
                    ((*inP & rasterP->sppsm.maskArray[c])>>roff[0])<<loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
                inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
            lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
    (*env)->ReleasePrimitiveArrayCritical(env, jInDataP, inDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
/* This routine is expecting a ShortComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
static int expandPackedSCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
                                  int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                                  int forceAlpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
    unsigned char *outP = outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
    unsigned short *lineInP, *inP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
    jarray jInDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
    jint   *inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
    int numBands = rasterP->numBands - (forceAlpha ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
    int a = numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
    jInDataP = (*env)->GetObjectField(env, rasterP->jraster, g_SCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
    inDataP = (*env)->GetPrimitiveArrayCritical(env, jInDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
    if (inDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
    lineInP =  (unsigned short *)inDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
            roff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
            if (roff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                loff[c] = -roff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
                roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
            else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        /* Need to put in alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
        if (forceAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                    *outP++ = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
                    for (c=0; c < numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
                         * Not correct.  Might need to unpremult,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
                         * shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
                                (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
                                   <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                    *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
                        (((*inP&rasterP->sppsm.maskArray[a]) >> roff[a])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
                                   <<loff[a]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                    for (c=0; c < numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                         * Not correct.  Might need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                         * unpremult, shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                        *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                                (((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
                                   <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        roff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
        if (roff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
            loff[0] = -roff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            roff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
        else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
            inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
                *outP++ = (unsigned char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                        ((*inP & rasterP->sppsm.maskArray[c])>>roff[0])<<loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
                inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
            lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
    (*env)->ReleasePrimitiveArrayCritical(env, jInDataP, inDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
/* This routine is expecting a IntegerComponentRaster with a PackedColorModel*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
static int expandPackedICR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
                           unsigned char *outDataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
    unsigned char *outP = outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
    unsigned int *lineInP, *inP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
    jarray jInDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
    jint   *inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
    jInDataP = (*env)->GetObjectField(env, rasterP->jraster, g_ICRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
    inDataP = (*env)->GetPrimitiveArrayCritical(env, jInDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
    if (inDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    lineInP =  (unsigned int *)inDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
            roff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
            if (roff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
                loff[c] = -roff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
                roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
            else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
        if (rasterP->numBands < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                    for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
                         * Not correct.  Might need to unpremult,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                         * shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                        *outP++ = (unsigned char)(((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                                   <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
                    for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                         * Not correct.  Might need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
                         * unpremult, shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
                        *outP++ = (unsigned char)(((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                                   <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
        roff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
        if (roff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
            loff[0] = -roff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            roff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
        else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
                *outP++ = (unsigned char)(((*inP & rasterP->sppsm.maskArray[c])>>roff[0])<<loff[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
                inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
            lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
    (*env)->ReleasePrimitiveArrayCritical(env, jInDataP, inDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
/* This routine is expecting a IntegerComponentRaster with a PackedColorModel*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
static int expandPackedICRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
                                  int component, unsigned char *outDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
                                  int forceAlpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
    unsigned char *outP = outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
    unsigned int *lineInP, *inP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
    jarray jInDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
    jint   *inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
    int numBands = rasterP->numBands - (forceAlpha ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
    int a = numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
    jInDataP = (*env)->GetObjectField(env, rasterP->jraster, g_ICRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
    inDataP = (*env)->GetPrimitiveArrayCritical(env, jInDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
    if (inDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
    lineInP =  (unsigned int *)inDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
            roff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
            if (roff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
                loff[c] = -roff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
            else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
        /* Need to put in alpha */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
        if (forceAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                    *outP++ = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                    for (c=0; c < numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                         * Not correct.  Might need to unpremult,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                         * shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                        *outP++ = (unsigned char)(((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                                   <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
                inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                    *outP++ = (unsigned char)(((*inP&rasterP->sppsm.maskArray[a]) >> roff[a])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                                   <<loff[a]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                    for (c=0; c < numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                         * Not correct.  Might need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                         * unpremult, shift, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                        *outP++ = (unsigned char)(((*inP&rasterP->sppsm.maskArray[c]) >> roff[c])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                                   <<loff[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
        roff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
        if (roff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
            loff[0] = -roff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
            roff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
        else loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
            inP = lineInP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                *outP++ = (unsigned char)(((*inP & rasterP->sppsm.maskArray[c])>>roff[0])<<loff[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
                inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
            lineInP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
    (*env)->ReleasePrimitiveArrayCritical(env, jInDataP, inDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
/* This routine is expecting a ByteComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
static int setPackedBCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
                        unsigned char *inDataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
    unsigned char *inP = inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
    unsigned char *lineOutP, *outP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
    jarray jOutDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    jint   *outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
    jOutDataP = (*env)->GetObjectField(env, rasterP->jraster, g_BCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
    outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
    if (outDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
    lineOutP =  (unsigned char *)outDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
            loff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
            if (loff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                roff[c] = -loff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
            else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
            *outP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
                for (c=0; c < rasterP->numBands; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
                    *outP |= (*inP<<loff[c]>>roff[c])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
        loff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
        if (loff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
            roff[0] = -loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
            loff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
        else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
            for (x=0; x < rasterP->width; x++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                *outP |= (*inP<<loff[0]>>roff[0])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
    (*env)->ReleasePrimitiveArrayCritical(env, jOutDataP, outDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
/* This routine is expecting a ShortComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
static int setPackedSCR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                           unsigned char *inDataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
    unsigned char *inP = inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
    unsigned short *lineOutP, *outP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
    jarray jOutDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
    jint   *outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
    jOutDataP = (*env)->GetObjectField(env, rasterP->jraster, g_SCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
    outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
    if (outDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
    lineOutP =  (unsigned short *)outDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
            loff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
            if (loff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                roff[c] = -loff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
            else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                for (c=0; c < rasterP->numBands; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                    /* Not correct.  Might need to unpremult, shift, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
                    *outP |= (*inP<<loff[c]>>roff[c])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
        loff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
        if (loff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
            roff[0] = -loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
            loff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
        else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
            for (x=0; x < rasterP->width; x++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
                *outP |= (*inP<<loff[0]>>roff[0])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
    (*env)->ReleasePrimitiveArrayCritical(env, jOutDataP, outDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
/* This routine is expecting a IntegerComponentRaster with a PackedColorModel*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
static int setPackedICR(JNIEnv *env, RasterS_t *rasterP, int component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
                           unsigned char *inDataP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
    unsigned char *inP = inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
    unsigned int *lineOutP, *outP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
    jarray jOutDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
    jint   *outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
    jOutDataP = (*env)->GetObjectField(env, rasterP->jraster, g_ICRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
    outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
    if (outDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
    lineOutP =  (unsigned int *)outDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
            loff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
            if (loff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
                roff[c] = -loff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
                loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
            else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
            for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
                for (c=0; c < rasterP->numBands; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
                    /* Not correct.  Might need to unpremult, shift, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
                    *outP |= (*inP<<loff[c]>>roff[c])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        loff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        if (loff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
            roff[0] = -loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            loff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
        else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
            for (x=0; x < rasterP->width; x++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                *outP |= (*inP<<loff[0]>>roff[0])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
    (*env)->ReleasePrimitiveArrayCritical(env, jOutDataP, outDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
/* This routine is expecting a ByteComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
static int setPackedBCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
                               int component, unsigned char *inDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
                               int supportsAlpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
    unsigned char *inP = inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
    unsigned char *lineOutP, *outP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
    jarray jOutDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
    jint   *outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
    int a = rasterP->numBands - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
    jOutDataP = (*env)->GetObjectField(env, rasterP->jraster, g_BCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
    outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
    if (outDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
    lineOutP =  (unsigned char *)outDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
            loff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
            if (loff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
                roff[c] = -loff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
                loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
            else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
                *outP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
                    *outP |= (*inP<<loff[a]>>roff[a])&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
                        rasterP->sppsm.maskArray[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
                    for (c=0; c < rasterP->numBands-1; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
                        *outP |= (*inP<<loff[c]>>roff[c])&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
                            rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
                    outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
                lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
                outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
                *outP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
                    for (c=0; c < rasterP->numBands; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
                        *outP |= (*inP<<loff[c]>>roff[c])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
                    outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
                lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
        loff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
        if (loff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
            roff[0] = -loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            loff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
        else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
            for (x=0; x < rasterP->width; x++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
                *outP |= (*inP<<loff[0]>>roff[0])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
    (*env)->ReleasePrimitiveArrayCritical(env, jOutDataP, outDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
/* This routine is expecting a ShortComponentRaster with a PackedColorModel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
static int setPackedSCRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
                               int component, unsigned char *inDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
                               int supportsAlpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
    unsigned char *inP = inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
    unsigned short *lineOutP, *outP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
    jarray jOutDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
    jint   *outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
    int a = rasterP->numBands - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
    jOutDataP = (*env)->GetObjectField(env, rasterP->jraster, g_SCRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
    outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
    if (outDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
    lineOutP =  (unsigned short *)outDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
            loff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
            if (loff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
                roff[c] = -loff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
                loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
            else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
                outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
                    *outP |= (*inP<<loff[a]>>roff[a])&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
                        rasterP->sppsm.maskArray[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
                    for (c=0; c < rasterP->numBands-1; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
                        /* Not correct.  Might need to unpremult, shift, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
                        *outP |= (*inP<<loff[c]>>roff[c])&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
                            rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
                    outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
                lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
                outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
                    for (c=0; c < rasterP->numBands; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
                        /* Not correct.  Might need to unpremult, shift, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
                        *outP |= (*inP<<loff[c]>>roff[c])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
                    outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
                lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
        loff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
        if (loff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
            roff[0] = -loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
            loff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
        else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
            for (x=0; x < rasterP->width; x++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                *outP |= (*inP<<loff[0]>>roff[0])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
    (*env)->ReleasePrimitiveArrayCritical(env, jOutDataP, outDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
/* This routine is expecting a IntegerComponentRaster with a PackedColorModel*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
static int setPackedICRdefault(JNIEnv *env, RasterS_t *rasterP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                               int component, unsigned char *inDataP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                               int supportsAlpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
    int x, y, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
    unsigned char *inP = inDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
    unsigned int *lineOutP, *outP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
    jarray jOutDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
    jint   *outDataP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
    int loff[MAX_NUMBANDS], roff[MAX_NUMBANDS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
    int a = rasterP->numBands - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
    if (rasterP->numBands > MAX_NUMBANDS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
    /* Grab data ptr, strides, offsets from raster */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
    jOutDataP = (*env)->GetObjectField(env, rasterP->jraster, g_ICRdataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
    outDataP = (*env)->GetPrimitiveArrayCritical(env, jOutDataP, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
    if (outDataP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
    lineOutP =  (unsigned int *)outDataP + rasterP->chanOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
    if (component < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
        for (c=0; c < rasterP->numBands; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
            loff[c] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
            if (loff[c] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
                roff[c] = -loff[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
                loff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
            else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
        /* Convert the all bands */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
        if (supportsAlpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
                outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
                    *outP |= (*inP<<loff[a]>>roff[a])&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
                        rasterP->sppsm.maskArray[a];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                    for (c=0; c < rasterP->numBands-1; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
                        /* Not correct.  Might need to unpremult, shift, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
                        *outP |= (*inP<<loff[c]>>roff[c])&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
                            rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
                    outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
                lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
            for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
                outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
                for (x=0; x < rasterP->width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
                    inP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
                    for (c=0; c < rasterP->numBands; c++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
                        /* Not correct.  Might need to unpremult, shift, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
                        *outP |= (*inP<<loff[c]>>roff[c])&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
                            rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
                    outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
                lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
        c = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        loff[0] = rasterP->sppsm.offsets[c] + (rasterP->sppsm.nBits[c]-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
        if (loff[0] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
            roff[0] = -loff[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
            loff[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
        else roff[c] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
        for (y=0; y < rasterP->height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
            outP = lineOutP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
            for (x=0; x < rasterP->width; x++, inP++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
                *outP |= (*inP<<loff[0]>>roff[0])&rasterP->sppsm.maskArray[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
                outP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
            lineOutP += rasterP->scanlineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
    (*env)->ReleasePrimitiveArrayCritical(env, jOutDataP, outDataP, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
/* This is temporary code.  Should go away when there is better color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
 * conversion code available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
 * REMIND:  Ignoring alpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
/* returns the absolute value x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
#define ABS(x) ((x) < 0 ? -(x) : (x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
#define CLIP(val,min,max)       ((val < min) ? min : ((val > max) ? max : val))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
colorMatch(int r, int g, int b, int a, unsigned char *argb, int numColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
    int besti = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
    int mindist, i, t, d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
    unsigned char red, green, blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
    r = CLIP(r, 0, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
    g = CLIP(g, 0, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
    b = CLIP(b, 0, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
    /* look for pure gray match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
    if ((r == g) && (g == b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
        mindist = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
        for (i = 0 ; i < numColors ; i++, argb+=4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
            red = argb[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
            green = argb[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
            blue = argb[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
            if (! ((red == green) && (green == blue)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
            d = ABS(red - r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
            if (d == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
            if (d < mindist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
                besti = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
                mindist = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
        return besti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
    /* look for non-pure gray match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
    mindist = 256 * 256 * 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
    for (i = 0 ; i < numColors ; i++, argb+=4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
        red = argb[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
        green = argb[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
        blue = argb[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
        t = red - r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
        d = t * t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
        if (d >= mindist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
        t = green - g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
        d += t * t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
        if (d >= mindist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
        t = blue - b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
        d += t * t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
        if (d >= mindist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
        if (d == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
        if (d < mindist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
            besti = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
            mindist = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
    return besti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
}