jdk/src/share/native/sun/awt/image/jpeg/imageioJPEG.c
author prr
Fri, 20 Dec 2013 09:58:29 -0800
changeset 23892 9959956918be
parent 23644 0cdb97daeef5
child 23917 d247e52b5262
permissions -rw-r--r--
8029854: Enhance JPEG decodings Reviewed-by: bae, vadim, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5189
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: 5189
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: 5189
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5189
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5189
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * This file contains the code to link the Java Image I/O JPEG plug-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * to the IJG library used to read and write JPEG files.  Much of it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * been copied, updated, and annotated from the jpegdecoder.c AWT JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * decoder.  Where that code was unclear, the present author has either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * rewritten the relevant section or commented it for the sake of future
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * maintainers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * In particular, the way the AWT code handled progressive JPEGs seems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to me to be only accidentally correct and somewhat inefficient.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * scheme used here represents the way I think it should work. (REV 11/00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <setjmp.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <string.h>
8749
ab0c8dca6d47 6989760: cmm native compiler warnings
bae
parents: 8358
diff changeset
    43
#include <limits.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/* java native interface headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include "com_sun_imageio_plugins_jpeg_JPEGImageReader.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include "com_sun_imageio_plugins_jpeg_JPEGImageWriter.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/* headers from the JPEG library */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include <jpeglib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#include "jerror.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#undef MAX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#define MAX(a,b)        ((a) > (b) ? (a) : (b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/* Cached Java method ids */
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
    60
static jmethodID JPEGImageReader_readInputDataID;
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
    61
static jmethodID JPEGImageReader_skipInputBytesID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
static jmethodID JPEGImageReader_warningOccurredID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
static jmethodID JPEGImageReader_warningWithMessageID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
static jmethodID JPEGImageReader_setImageDataID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
static jmethodID JPEGImageReader_acceptPixelsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static jmethodID JPEGImageReader_pushBackID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
static jmethodID JPEGImageReader_passStartedID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
static jmethodID JPEGImageReader_passCompleteID;
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 16881
diff changeset
    69
static jmethodID JPEGImageWriter_writeOutputDataID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
static jmethodID JPEGImageWriter_warningOccurredID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
static jmethodID JPEGImageWriter_warningWithMessageID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
static jmethodID JPEGImageWriter_writeMetadataID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
static jmethodID JPEGImageWriter_grabPixelsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
static jfieldID JPEGQTable_tableID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static jfieldID JPEGHuffmanTable_lengthsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static jfieldID JPEGHuffmanTable_valuesID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Defined in jpegdecoder.c.  Copy code from there if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * when that disappears. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
extern JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * The following sets of defines must match the warning messages in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Java code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
/* Reader warnings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#define READ_NO_EOI          0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
/* Writer warnings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
/* Return codes for various ops */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
#define OK     1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
#define NOT_OK 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * First we define two objects, one for the stream and buffer and one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * for pixels.  Both contain references to Java objects and pointers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * pinned arrays.  These objects can be used for either input or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * output.  Pixels can be accessed as either INT32s or bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Every I/O operation will have one of each these objects, one for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * the stream and the other to hold pixels, regardless of the I/O direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/******************** StreamBuffer definition ************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
typedef struct streamBufferStruct {
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   109
    jweak ioRef;               // weak reference to a provider of I/O routines
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    jbyteArray hstreamBuffer;  // Handle to a Java buffer for the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    JOCTET *buf;               // Pinned buffer pointer */
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
   112
    size_t bufferOffset;          // holds offset between unpin and the next pin
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
   113
    size_t bufferLength;          // Allocated, nut just used
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    int suspendable;           // Set to true to suspend input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    long remaining_skip;       // Used only on input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
} streamBuffer, *streamBufferPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * This buffer size was set to 64K in the old classes, 4K by default in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * IJG library, with the comment "an efficiently freadable size", and 1K
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * in AWT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Unlike in the other Java designs, these objects will persist, so 64K
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * seems too big and 1K seems too small.  If 4K was good enough for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * IJG folks, it's good enough for me.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
#define STREAMBUF_SIZE 4096
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   128
#define GET_IO_REF(io_name)                                            \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   129
    do {                                                               \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   130
        if ((*env)->IsSameObject(env, sb->ioRef, NULL) ||              \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   131
            ((io_name) = (*env)->NewLocalRef(env, sb->ioRef)) == NULL) \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   132
        {                                                              \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   133
            cinfo->err->error_exit((j_common_ptr) cinfo);              \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   134
        }                                                              \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   135
    } while (0)                                                        \
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   136
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * Used to signal that no data need be restored from an unpin to a pin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * I.e. the buffer is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 */
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
   141
#define NO_DATA ((size_t)-1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
// Forward reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
static void resetStreamBuffer(JNIEnv *env, streamBufferPtr sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * Initialize a freshly allocated StreamBuffer object.  The stream is left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * null, as it will be set from Java by setSource, but the buffer object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * is created and a global reference kept.  Returns OK on success, NOT_OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * if allocating the buffer or getting a global reference for it failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
static int initStreamBuffer(JNIEnv *env, streamBufferPtr sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /* Initialize a new buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jbyteArray hInputBuffer = (*env)->NewByteArray(env, STREAMBUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if (hInputBuffer == NULL) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   156
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                         "Initializing Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return NOT_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    sb->bufferLength = (*env)->GetArrayLength(env, hInputBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    sb->hstreamBuffer = (*env)->NewGlobalRef(env, hInputBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    if (sb->hstreamBuffer == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                         "Initializing Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return NOT_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   172
    sb->ioRef = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    sb->buf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    resetStreamBuffer(env, sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    return OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * Free all resources associated with this streamBuffer.  This must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * be called to dispose the object to avoid leaking global references, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * resetStreamBuffer does not release the buffer reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
static void destroyStreamBuffer(JNIEnv *env, streamBufferPtr sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    resetStreamBuffer(env, sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    if (sb->hstreamBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        (*env)->DeleteGlobalRef(env, sb->hstreamBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
// Forward reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
static void unpinStreamBuffer(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                              streamBufferPtr sb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                              const JOCTET *next_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * Resets the state of a streamBuffer object that has been in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * The global reference to the stream is released, but the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * to the buffer is retained.  The buffer is unpinned if it was pinned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * All other state is reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
static void resetStreamBuffer(JNIEnv *env, streamBufferPtr sb) {
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   204
    if (sb->ioRef != NULL) {
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   205
        (*env)->DeleteWeakGlobalRef(env, sb->ioRef);
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   206
        sb->ioRef = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    unpinStreamBuffer(env, sb, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    sb->bufferOffset = NO_DATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    sb->suspendable = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    sb->remaining_skip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * Pins the data buffer associated with this stream.  Returns OK on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * success, NOT_OK on failure, as GetPrimitiveArrayCritical may fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
static int pinStreamBuffer(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                           streamBufferPtr sb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                           const JOCTET **next_byte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    if (sb->hstreamBuffer != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        assert(sb->buf == NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        sb->buf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            (JOCTET *)(*env)->GetPrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                        sb->hstreamBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                                        NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (sb->buf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return NOT_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (sb->bufferOffset != NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            *next_byte = sb->buf + sb->bufferOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    return OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * Unpins the data buffer associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
static void unpinStreamBuffer(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                              streamBufferPtr sb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                              const JOCTET *next_byte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if (sb->buf != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        assert(sb->hstreamBuffer != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (next_byte == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            sb->bufferOffset = NO_DATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            sb->bufferOffset = next_byte - sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        (*env)->ReleasePrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                              sb->hstreamBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                              sb->buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                              0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        sb->buf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * Clear out the streamBuffer.  This just invalidates the data in the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
static void clearStreamBuffer(streamBufferPtr sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    sb->bufferOffset = NO_DATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
/*************************** end StreamBuffer definition *************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
/*************************** Pixel Buffer definition ******************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
typedef struct pixelBufferStruct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    jobject hpixelObject;   // Usually a DataBuffer bank as a byte array
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
   271
    unsigned int byteBufferLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    union pixptr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        INT32         *ip;  // Pinned buffer pointer, as 32-bit ints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        unsigned char *bp;  // Pinned buffer pointer, as bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    } buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
} pixelBuffer, *pixelBufferPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * Initialize a freshly allocated PixelBuffer.  All fields are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * set to NULL, as we have no idea what size buffer we will need.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
static void initPixelBuffer(pixelBufferPtr pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    pb->hpixelObject = NULL;
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
   284
    pb->byteBufferLength = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    pb->buf.ip = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * Set the pixelBuffer to use the given buffer, acquiring a new global
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 * reference for it.  Returns OK on success, NOT_OK on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
static int setPixelBuffer(JNIEnv *env, pixelBufferPtr pb, jobject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    pb->hpixelObject = (*env)->NewGlobalRef(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    if (pb->hpixelObject == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                         "Setting Pixel Buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return NOT_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
   300
    pb->byteBufferLength = (*env)->GetArrayLength(env, pb->hpixelObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    return OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
// Forward reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
static void unpinPixelBuffer(JNIEnv *env, pixelBufferPtr pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * Resets a pixel buffer to its initial state.  Unpins any pixel buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * releases the global reference, and resets fields to NULL.  Use this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * method to dispose the object as well (there is no destroyPixelBuffer).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
static void resetPixelBuffer(JNIEnv *env, pixelBufferPtr pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    if (pb->hpixelObject != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        unpinPixelBuffer(env, pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        (*env)->DeleteGlobalRef(env, pb->hpixelObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        pb->hpixelObject = NULL;
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
   317
        pb->byteBufferLength = 0;
2
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
 * Pins the data buffer.  Returns OK on success, NOT_OK on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
static int pinPixelBuffer(JNIEnv *env, pixelBufferPtr pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    if (pb->hpixelObject != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        assert(pb->buf.ip == NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        pb->buf.bp = (unsigned char *)(*env)->GetPrimitiveArrayCritical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            (env, pb->hpixelObject, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (pb->buf.bp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return NOT_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    return OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 * Unpins the data buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
static void unpinPixelBuffer(JNIEnv *env, pixelBufferPtr pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    if (pb->buf.ip != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        assert(pb->hpixelObject != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        (*env)->ReleasePrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                              pb->hpixelObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                              pb->buf.ip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                              0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        pb->buf.ip = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
/********************* end PixelBuffer definition *******************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
/********************* ImageIOData definition ***********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
#define MAX_BANDS 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
#define JPEG_BAND_SIZE 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
#define NUM_BAND_VALUES (1<<JPEG_BAND_SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
#define MAX_JPEG_BAND_VALUE (NUM_BAND_VALUES-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
#define HALF_MAX_JPEG_BAND_VALUE (MAX_JPEG_BAND_VALUE>>1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
/* The number of possible incoming values to be scaled. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
#define NUM_INPUT_VALUES (1 << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 * The principal imageioData object, opaque to I/O direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 * Each JPEGImageReader will have associated with it a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 * jpeg_decompress_struct, and similarly each JPEGImageWriter will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 * have associated with it a jpeg_compress_struct.  In order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 * ensure that these associations persist from one native call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 * the next, and to provide a central locus of imageio-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * data, we define an imageioData struct containing references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 * to the Java object and the IJG structs.  The functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 * that manipulate these objects know whether input or output is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 * performed and therefore know how to manipulate the contents correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 * If for some reason they don't, the direction can be determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * checking the is_decompressor field of the jpegObj.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 * In order for lower level code to determine a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 * Java object given an IJG struct, such as for dispatching warnings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 * we use the client_data field of the jpeg object to store a pointer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 * to the imageIOData object.  Maintenance of this pointer is performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
 * exclusively within the following access functions.  If you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
 * change that, you run the risk of dangling pointers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
typedef struct imageIODataStruct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    j_common_ptr jpegObj;     // Either struct is fine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    jobject imageIOobj;       // A JPEGImageReader or a JPEGImageWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    streamBuffer streamBuf;   // Buffer for the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    pixelBuffer pixelBuf;     // Buffer for pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    jboolean abortFlag;       // Passed down from Java abort method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
} imageIOData, *imageIODataPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
 * Allocate and initialize a new imageIOData object to associate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
 * jpeg object and the Java object.  Returns a pointer to the new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
 * on success, NULL on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
static imageIODataPtr initImageioData (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                       j_common_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                       jobject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    imageIODataPtr data = (imageIODataPtr) malloc (sizeof(imageIOData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    data->jpegObj = cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    cinfo->client_data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
   411
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    printf("new structures: data is %p, cinfo is %p\n", data, cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    data->imageIOobj = (*env)->NewWeakGlobalRef(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    if (data->imageIOobj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        free (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    if (initStreamBuffer(env, &data->streamBuf) == NOT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        (*env)->DeleteWeakGlobalRef(env, data->imageIOobj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        free (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    initPixelBuffer(&data->pixelBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    data->abortFlag = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * Resets the imageIOData object to its initial state, as though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 * it had just been allocated and initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
static void resetImageIOData(JNIEnv *env, imageIODataPtr data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    resetStreamBuffer(env, &data->streamBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    resetPixelBuffer(env, &data->pixelBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    data->abortFlag = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 * Releases all resources held by this object and its subobjects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 * frees the object, and returns the jpeg object.  This method must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 * be called to avoid leaking global references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * Note that the jpeg object is not freed or destroyed, as that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 * the client's responsibility, although the client_data field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 * cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
static j_common_ptr destroyImageioData(JNIEnv *env, imageIODataPtr data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    j_common_ptr ret = data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    (*env)->DeleteWeakGlobalRef(env, data->imageIOobj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    destroyStreamBuffer(env, &data->streamBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    resetPixelBuffer(env, &data->pixelBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    ret->client_data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    free(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
/******************** end ImageIOData definition ***********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
/******************** Java array pinning and unpinning *****************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
/* We use Get/ReleasePrimitiveArrayCritical functions to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 * the need to copy array elements for the above two objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 * MAKE SURE TO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
 * - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
 *   callbacks to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
 * - call RELEASE_ARRAYS before returning to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 * Otherwise things will go horribly wrong. There may be memory leaks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 * excessive pinning, or even VM crashes!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 * Note that GetPrimitiveArrayCritical may fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
 * Release (unpin) all the arrays in use during a read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
static void RELEASE_ARRAYS(JNIEnv *env, imageIODataPtr data, const JOCTET *next_byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    unpinStreamBuffer(env, &data->streamBuf, next_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    unpinPixelBuffer(env, &data->pixelBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
 * Get (pin) all the arrays in use during a read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
static int GET_ARRAYS(JNIEnv *env, imageIODataPtr data, const JOCTET **next_byte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if (pinStreamBuffer(env, &data->streamBuf, next_byte) == NOT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return NOT_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    if (pinPixelBuffer(env, &data->pixelBuf) == NOT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        RELEASE_ARRAYS(env, data, *next_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return NOT_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    return OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
/****** end of Java array pinning and unpinning ***********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
/****** Error Handling *******/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 * Set up error handling to use setjmp/longjmp.  This is the third such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * setup, as both the AWT jpeg decoder and the com.sun... JPEG classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 * setup thier own.  Ultimately these should be integrated, as they all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 * do pretty much the same thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
struct sun_jpeg_error_mgr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
  struct jpeg_error_mgr pub;    /* "public" fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
  jmp_buf setjmp_buffer;        /* for return to caller */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
typedef struct sun_jpeg_error_mgr * sun_jpeg_error_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
 * Here's the routine that will replace the standard error_exit method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
sun_jpeg_error_exit (j_common_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
  /* cinfo->err really points to a sun_jpeg_error_mgr struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
  sun_jpeg_error_ptr myerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
  /* For Java, we will format the message and put it in the error we throw. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
  /* Return control to the setjmp point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
  longjmp(myerr->setjmp_buffer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
 * Error Message handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
 * This overrides the output_message method to send JPEG messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
sun_jpeg_output_message (j_common_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
  char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
  jstring string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
  imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
  JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
  jobject theObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
  /* Create the message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
  (*cinfo->err->format_message) (cinfo, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
  // Create a new java string from the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
  string = (*env)->NewStringUTF(env, buffer);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   561
  CHECK_NULL(string);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
  theObject = data->imageIOobj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
  if (cinfo->is_decompressor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
      (*env)->CallVoidMethod(env, theObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                             JPEGImageReader_warningWithMessageID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                             string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
      (*env)->CallVoidMethod(env, theObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                             JPEGImageWriter_warningWithMessageID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                             string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
/* End of verbatim copy from jpegdecoder.c */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
/*************** end of error handling *********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
/*************** Shared utility code ***********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
static void imageio_set_stream(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                               j_common_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                               imageIODataPtr data,
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   585
                               jobject io){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    streamBufferPtr sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    sun_jpeg_error_ptr jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    sb = &data->streamBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    resetStreamBuffer(env, sb);  // Removes any old stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   593
    /* Now we need a new weak global reference for the I/O provider */
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   594
    if (io != NULL) { // Fix for 4411955
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   595
        sb->ioRef = (*env)->NewWeakGlobalRef(env, io);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   596
        CHECK_NULL(sb->ioRef);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /* And finally reset state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    data->abortFlag = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    jerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        /* If we get here, the JPEG code has signaled an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
           while aborting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            (*cinfo->err->format_message) (cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                           buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    jpeg_abort(cinfo);  // Frees any markers, but not tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
static void imageio_reset(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                          j_common_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                          imageIODataPtr data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    sun_jpeg_error_ptr jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    resetImageIOData(env, data);  // Mapping to jpeg object is retained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    jerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        /* If we get here, the JPEG code has signaled an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
           while aborting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            (*cinfo->err->format_message) (cinfo, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    jpeg_abort(cinfo);  // Does not reset tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
static void imageio_dispose(j_common_ptr info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    if (info != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        free(info->err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        info->err = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (info->is_decompressor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            j_decompress_ptr dinfo = (j_decompress_ptr) info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            free(dinfo->src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            dinfo->src = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            j_compress_ptr cinfo = (j_compress_ptr) info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            free(cinfo->dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            cinfo->dest = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        jpeg_destroy(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        free(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
static void imageio_abort(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                          imageIODataPtr data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    data->abortFlag = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
static int setQTables(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                      j_common_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                      jobjectArray qtables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                      boolean write) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    jsize qlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    jobject table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    jintArray qdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    jint *qdataBody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    JQUANT_TBL *quant_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    j_compress_ptr comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    j_decompress_ptr decomp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    qlen = (*env)->GetArrayLength(env, qtables);
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
   684
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    printf("in setQTables, qlen = %d, write is %d\n", qlen, write);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
#endif
4203
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   687
    if (qlen > NUM_QUANT_TBLS) {
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   688
        /* Ignore extra qunterization tables. */
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   689
        qlen = NUM_QUANT_TBLS;
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   690
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    for (i = 0; i < qlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        table = (*env)->GetObjectArrayElement(env, qtables, i);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   693
        CHECK_NULL_RETURN(table, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        qdata = (*env)->GetObjectField(env, table, JPEGQTable_tableID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        qdataBody = (*env)->GetPrimitiveArrayCritical(env, qdata, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (cinfo->is_decompressor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            decomp = (j_decompress_ptr) cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            if (decomp->quant_tbl_ptrs[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                decomp->quant_tbl_ptrs[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    jpeg_alloc_quant_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            quant_ptr = decomp->quant_tbl_ptrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            comp = (j_compress_ptr) cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            if (comp->quant_tbl_ptrs[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                comp->quant_tbl_ptrs[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    jpeg_alloc_quant_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            quant_ptr = comp->quant_tbl_ptrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        for (j = 0; j < 64; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            quant_ptr->quantval[j] = (UINT16)qdataBody[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        quant_ptr->sent_table = !write;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        (*env)->ReleasePrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                              qdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                                              qdataBody,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                                              0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    return qlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   725
static boolean setHuffTable(JNIEnv *env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                         JHUFF_TBL *huff_ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                         jobject table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    jshortArray huffLens;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    jshortArray huffValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    jshort *hlensBody, *hvalsBody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    jsize hlensLen, hvalsLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    // lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    huffLens = (*env)->GetObjectField(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                      table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                      JPEGHuffmanTable_lengthsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    hlensLen = (*env)->GetArrayLength(env, huffLens);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    hlensBody = (*env)->GetShortArrayElements(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                              huffLens,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                              NULL);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   743
    CHECK_NULL_RETURN(hlensBody, FALSE);
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   744
4203
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   745
    if (hlensLen > 16) {
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   746
        /* Ignore extra elements of bits array. Only 16 elements can be
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   747
           stored. 0-th element is not used. (see jpeglib.h, line 107)  */
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   748
        hlensLen = 16;
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   749
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    for (i = 1; i <= hlensLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        huff_ptr->bits[i] = (UINT8)hlensBody[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    (*env)->ReleaseShortArrayElements(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                      huffLens,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                                      hlensBody,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                      JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    // values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    huffValues = (*env)->GetObjectField(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                        table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                        JPEGHuffmanTable_valuesID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    hvalsLen = (*env)->GetArrayLength(env, huffValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    hvalsBody = (*env)->GetShortArrayElements(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                                              huffValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                                              NULL);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   765
    CHECK_NULL_RETURN(hvalsBody, FALSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
4203
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   767
    if (hvalsLen > 256) {
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   768
        /* Ignore extra elements of hufval array. Only 256 elements
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   769
           can be stored. (see jpeglib.h, line 109)                  */
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   770
        hlensLen = 256;
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   771
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    for (i = 0; i < hvalsLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        huff_ptr->huffval[i] = (UINT8)hvalsBody[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    (*env)->ReleaseShortArrayElements(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                                      huffValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                      hvalsBody,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                      JNI_ABORT);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   779
    return TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
static int setHTables(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                      j_common_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                      jobjectArray DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                      jobjectArray ACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                      boolean write) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    jobject table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    JHUFF_TBL *huff_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    j_compress_ptr comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    j_decompress_ptr decomp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    jsize hlen = (*env)->GetArrayLength(env, DCHuffmanTables);
4203
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   793
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   794
    if (hlen > NUM_HUFF_TBLS) {
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   795
        /* Ignore extra DC huffman tables. */
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   796
        hlen = NUM_HUFF_TBLS;
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   797
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    for (i = 0; i < hlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (cinfo->is_decompressor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            decomp = (j_decompress_ptr) cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            if (decomp->dc_huff_tbl_ptrs[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                decomp->dc_huff_tbl_ptrs[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    jpeg_alloc_huff_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            huff_ptr = decomp->dc_huff_tbl_ptrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            comp = (j_compress_ptr) cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if (comp->dc_huff_tbl_ptrs[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                comp->dc_huff_tbl_ptrs[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    jpeg_alloc_huff_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            huff_ptr = comp->dc_huff_tbl_ptrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        table = (*env)->GetObjectArrayElement(env, DCHuffmanTables, i);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   815
        if (table == NULL || !setHuffTable(env, huff_ptr, table)) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   816
            return 0;
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   817
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        huff_ptr->sent_table = !write;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    hlen = (*env)->GetArrayLength(env, ACHuffmanTables);
4203
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   821
    if (hlen > NUM_HUFF_TBLS) {
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   822
        /* Ignore extra AC huffman tables. */
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   823
        hlen = NUM_HUFF_TBLS;
059fe9142d1c 6862968: JPEG Image Writer quantization problem
bae
parents: 4201
diff changeset
   824
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    for (i = 0; i < hlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (cinfo->is_decompressor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            decomp = (j_decompress_ptr) cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            if (decomp->ac_huff_tbl_ptrs[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                decomp->ac_huff_tbl_ptrs[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    jpeg_alloc_huff_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            huff_ptr = decomp->ac_huff_tbl_ptrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            comp = (j_compress_ptr) cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            if (comp->ac_huff_tbl_ptrs[i] == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                comp->ac_huff_tbl_ptrs[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    jpeg_alloc_huff_table(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            huff_ptr = comp->ac_huff_tbl_ptrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        table = (*env)->GetObjectArrayElement(env, ACHuffmanTables, i);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   842
        if(table == NULL || !setHuffTable(env, huff_ptr, table)) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   843
            return 0;
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   844
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        huff_ptr->sent_table = !write;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    return hlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
/*************** end of shared utility code ****************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
/********************** Reader Support **************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
/********************** Source Management ***********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
 * INPUT HANDLING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
 * The JPEG library's input management is defined by the jpeg_source_mgr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
 * structure which contains two fields to convey the information in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
 * buffer and 5 methods which perform all buffer management.  The library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
 * defines a standard input manager that uses stdio for obtaining compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
 * jpeg data, but here we need to use Java to get our data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
 * We use the library jpeg_source_mgr but our own routines that access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
 * imageio-specific information in the imageIOData structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
 * Initialize source.  This is called by jpeg_read_header() before any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
 * data is actually read.  Unlike init_destination(), it may leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
 * bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
 * will occur immediately).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
imageio_init_source(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    struct jpeg_source_mgr *src = cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    src->next_input_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    src->bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
 * This is called whenever bytes_in_buffer has reached zero and more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
 * data is wanted.  In typical applications, it should read fresh data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
 * into the buffer (ignoring the current state of next_input_byte and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
 * bytes_in_buffer), reset the pointer & count to the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
 * buffer, and return TRUE indicating that the buffer has been reloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
 * It is not necessary to fill the buffer entirely, only to obtain at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
 * least one more byte.  bytes_in_buffer MUST be set to a positive value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
 * if TRUE is returned.  A FALSE return should only be used when I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
 * suspension is desired (this mode is discussed in the next section).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
 * Note that with I/O suspension turned on, this procedure should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
 * do any work since the JPEG library has a very simple backtracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
 * mechanism which relies on the fact that the buffer will be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
 * only when it has backed out to the top application level.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
 * suspendable is turned on, imageio_fill_suspended_buffer will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
 * do the actual work of filling the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
GLOBAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
imageio_fill_input_buffer(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    struct jpeg_source_mgr *src = cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    streamBufferPtr sb = &data->streamBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    int ret;
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   913
    jobject input = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /* This is where input suspends */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    if (sb->suspendable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
   920
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    printf("Filling input buffer, remaining skip is %ld, ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
           sb->remaining_skip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    printf("Buffer length is %d\n", sb->bufferLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Definitively skips.  Could be left over if we tried to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * more than a buffer's worth but suspended when getting the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * buffer.  Now we aren't suspended, so we can catch up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    if (sb->remaining_skip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        src->skip_input_data(cinfo, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Now fill a complete buffer, or as much of one as the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * will give us if we are near the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
19788
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
   939
    RELEASE_ARRAYS(env, data, src->next_input_byte);
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
   940
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   941
    GET_IO_REF(input);
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   942
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    ret = (*env)->CallIntMethod(env,
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
   944
                                input,
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
   945
                                JPEGImageReader_readInputDataID,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                sb->hstreamBuffer, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                                sb->bufferLength);
23892
9959956918be 8029854: Enhance JPEG decodings
prr
parents: 23644
diff changeset
   948
    if (ret > sb->bufferLength) ret = sb->bufferLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
   954
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
      printf("Buffer filled. ret = %d\n", ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * If we have reached the end of the stream, then the EOI marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * is missing.  We accept such streams but generate a warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * The image is likely to be corrupted, though everything through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * the end of the last complete MCU should be usable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        jobject reader = data->imageIOobj;
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
   965
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
      printf("YO! Early EOI! ret = %d\n", ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        (*env)->CallVoidMethod(env, reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                               JPEGImageReader_warningOccurredID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                               READ_NO_EOI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        sb->buf[0] = (JOCTET) 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        sb->buf[1] = (JOCTET) JPEG_EOI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        ret = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    src->next_input_byte = sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    src->bytes_in_buffer = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
 * With I/O suspension turned on, the JPEG library requires that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
 * buffer filling be done at the top application level, using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
 * function.  Due to the way that backtracking works, this procedure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
 * saves all of the data that was left in the buffer when suspension
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20851
diff changeset
   993
 * occurred and read new data only at the end.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
imageio_fill_suspended_buffer(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    struct jpeg_source_mgr *src = cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    streamBufferPtr sb = &data->streamBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    jint ret;
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1004
    size_t offset, buflen;
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1005
    jobject input = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * The original (jpegdecoder.c) had code here that called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * InputStream.available and just returned if the number of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * available was less than any remaining skip.  Presumably this was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * to avoid blocking, although the benefit was unclear, as no more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * decompression can take place until more data is available, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * the code would block on input a little further along anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * ImageInputStreams don't have an available method, so we'll just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * block in the skip if we have to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    if (sb->remaining_skip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        src->skip_input_data(cinfo, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    /* Save the data currently in the buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    offset = src->bytes_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    if (src->next_input_byte > sb->buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        memcpy(sb->buf, src->next_input_byte, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1027
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1028
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    RELEASE_ARRAYS(env, data, src->next_input_byte);
19788
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  1030
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  1031
    GET_IO_REF(input);
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  1032
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    buflen = sb->bufferLength - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    if (buflen <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (!GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1041
    ret = (*env)->CallIntMethod(env, input,
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1042
                                JPEGImageReader_readInputDataID,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                                sb->hstreamBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                                offset, buflen);
23892
9959956918be 8029854: Enhance JPEG decodings
prr
parents: 23644
diff changeset
  1045
    if (ret > buflen) ret = buflen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * If we have reached the end of the stream, then the EOI marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * is missing.  We accept such streams but generate a warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * The image is likely to be corrupted, though everything through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * the end of the last complete MCU should be usable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        jobject reader = data->imageIOobj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        (*env)->CallVoidMethod(env, reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                               JPEGImageReader_warningOccurredID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                               READ_NO_EOI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        sb->buf[offset] = (JOCTET) 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        sb->buf[offset + 1] = (JOCTET) JPEG_EOI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        ret = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    src->next_input_byte = sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    src->bytes_in_buffer = ret + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
 * Skip num_bytes worth of data.  The buffer pointer and count are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
 * advanced over num_bytes input bytes, using the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
 * skipBytes method if the skip is greater than the number of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
 * in the buffer.  This is used to skip over a potentially large amount of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
 * uninteresting data (such as an APPn marker).  bytes_in_buffer will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
 * zero on return if the skip is larger than the current contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
 * buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
 * A negative skip count is treated as a no-op.  A zero skip count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
 * skips any remaining skip from a previous skip while suspended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
 * Note that with I/O suspension turned on, this procedure does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
 * call skipBytes since the JPEG library has a very simple backtracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
 * mechanism which relies on the fact that the application level has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
 * exclusive control over actual I/O.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
imageio_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    struct jpeg_source_mgr *src = cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    streamBufferPtr sb = &data->streamBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    jlong ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    jobject reader;
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1105
    jobject input = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    if (num_bytes < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    num_bytes += sb->remaining_skip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    sb->remaining_skip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    /* First the easy case where we are skipping <= the current contents. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    ret = src->bytes_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    if (ret >= num_bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        src->next_input_byte += num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        src->bytes_in_buffer -= num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * We are skipping more than is in the buffer.  We empty the buffer and,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * if we aren't suspended, call the Java skipBytes method.  We always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * leave the buffer empty, to be filled by either fill method above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    src->bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    src->next_input_byte = sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    num_bytes -= (long)ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    if (sb->suspendable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        sb->remaining_skip = num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
19788
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  1135
    RELEASE_ARRAYS(env, data, src->next_input_byte);
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  1136
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1137
    GET_IO_REF(input);
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1138
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    ret = (*env)->CallLongMethod(env,
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  1140
                                 input,
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1141
                                 JPEGImageReader_skipInputBytesID,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                                 (jlong) num_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * If we have reached the end of the stream, then the EOI marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * is missing.  We accept such streams but generate a warning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * The image is likely to be corrupted, though everything through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * the end of the last complete MCU should be usable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        reader = data->imageIOobj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        (*env)->CallVoidMethod(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                               reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                               JPEGImageReader_warningOccurredID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                               READ_NO_EOI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        sb->buf[0] = (JOCTET) 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        sb->buf[1] = (JOCTET) JPEG_EOI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        src->bytes_in_buffer = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        src->next_input_byte = sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
 * Terminate source --- called by jpeg_finish_decompress() after all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
 * data for an image has been read.  In our case pushes back any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
 * remaining data, as it will be for another image and must be available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
 * for java to find out that there is another image.  Also called if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
 * reseting state after reading a tables-only image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
imageio_term_source(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    // To pushback, just seek back by src->bytes_in_buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    struct jpeg_source_mgr *src = cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    jobject reader = data->imageIOobj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    if (src->bytes_in_buffer > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         (*env)->CallVoidMethod(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                                reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                                JPEGImageReader_pushBackID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                                src->bytes_in_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
         if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
             || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
             cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
         src->bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         //src->next_input_byte = sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
/********************* end of source manager ******************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
/********************* ICC profile support ********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
 * The following routines are modified versions of the ICC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
 * profile support routines available from the IJG website.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
 * The originals were written by Todd Newman
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
 * <tdn@eccentric.esd.sgi.com> and modified by Tom Lane for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
 * the IJG.  They are further modified to fit in the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
 * of the imageio JPEG plug-in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
 * Since an ICC profile can be larger than the maximum size of a JPEG marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
 * (64K), we need provisions to split it into multiple markers.  The format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
 * defined by the ICC specifies one or more APP2 markers containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
 * following data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
 *      Identifying string      ASCII "ICC_PROFILE\0"  (12 bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
 *      Marker sequence number  1 for first APP2, 2 for next, etc (1 byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
 *      Number of markers       Total number of APP2's used (1 byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
 *      Profile data            (remainder of APP2 data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
 * Decoders should use the marker sequence numbers to reassemble the profile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
 * rather than assuming that the APP2 markers appear in the correct sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
#define ICC_MARKER  (JPEG_APP0 + 2)     /* JPEG marker code for ICC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
#define ICC_OVERHEAD_LEN  14            /* size of non-profile data in APP2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
#define MAX_BYTES_IN_MARKER  65533      /* maximum data len of a JPEG marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
#define MAX_DATA_BYTES_IN_ICC_MARKER  (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
 * Handy subroutine to test whether a saved marker is an ICC profile marker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
static boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
marker_is_icc (jpeg_saved_marker_ptr marker)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
  return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    marker->marker == ICC_MARKER &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    marker->data_length >= ICC_OVERHEAD_LEN &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    /* verify the identifying string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    GETJOCTET(marker->data[0]) == 0x49 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    GETJOCTET(marker->data[1]) == 0x43 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    GETJOCTET(marker->data[2]) == 0x43 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    GETJOCTET(marker->data[3]) == 0x5F &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    GETJOCTET(marker->data[4]) == 0x50 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    GETJOCTET(marker->data[5]) == 0x52 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    GETJOCTET(marker->data[6]) == 0x4F &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    GETJOCTET(marker->data[7]) == 0x46 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    GETJOCTET(marker->data[8]) == 0x49 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    GETJOCTET(marker->data[9]) == 0x4C &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    GETJOCTET(marker->data[10]) == 0x45 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    GETJOCTET(marker->data[11]) == 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
 * See if there was an ICC profile in the JPEG file being read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
 * if so, reassemble and return the profile data as a new Java byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
 * If there was no ICC profile, return NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
 * If the file contains invalid ICC APP2 markers, we throw an IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
 * with an appropriate message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
jbyteArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
read_icc_profile (JNIEnv *env, j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    jpeg_saved_marker_ptr marker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    int num_markers = 0;
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1275
    int num_found_markers = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    int seq_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    JOCTET *icc_data;
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1278
    JOCTET *dst_ptr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    unsigned int total_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
#define MAX_SEQ_NO  255         // sufficient since marker numbers are bytes
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1281
    jpeg_saved_marker_ptr icc_markers[MAX_SEQ_NO + 1];
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1282
    int first;         // index of the first marker in the icc_markers array
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1283
    int last;          // index of the last marker in the icc_markers array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    jbyteArray data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    /* This first pass over the saved markers discovers whether there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * any ICC markers and verifies the consistency of the marker numbering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1290
    for (seq_no = 0; seq_no <= MAX_SEQ_NO; seq_no++)
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1291
        icc_markers[seq_no] = NULL;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1292
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        if (marker_is_icc(marker)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            if (num_markers == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                num_markers = GETJOCTET(marker->data[13]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            else if (num_markers != GETJOCTET(marker->data[13])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                     "Invalid icc profile: inconsistent num_markers fields");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            seq_no = GETJOCTET(marker->data[12]);
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1304
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1305
            /* Some third-party tools produce images with profile chunk
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1306
             * numeration started from zero. It is inconsistent with ICC
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1307
             * spec, but seems to be recognized by majority of image
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1308
             * processing tools, so we should be more tolerant to this
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1309
             * departure from the spec.
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1310
             */
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1311
            if (seq_no < 0 || seq_no > num_markers) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                     "Invalid icc profile: bad sequence number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            }
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1316
            if (icc_markers[seq_no] != NULL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                     "Invalid icc profile: duplicate sequence numbers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            }
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1321
            icc_markers[seq_no] = marker;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1322
            num_found_markers ++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    if (num_markers == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        return NULL;  // There is no profile
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1329
    if (num_markers != num_found_markers) {
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1330
        JNU_ThrowByName(env, "javax/imageio/IIOException",
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1331
                        "Invalid icc profile: invalid number of icc markers");
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1332
        return NULL;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1333
    }
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1334
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1335
    first = icc_markers[0] ? 0 : 1;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1336
    last = num_found_markers + first;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1337
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1338
    /* Check for missing markers, count total space needed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    total_length = 0;
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1341
    for (seq_no = first; seq_no < last; seq_no++) {
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1342
        unsigned int length;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1343
        if (icc_markers[seq_no] == NULL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                 "Invalid icc profile: missing sequence number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1348
        /* check the data length correctness */
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1349
        length = icc_markers[seq_no]->data_length;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1350
        if (ICC_OVERHEAD_LEN > length || length > MAX_BYTES_IN_MARKER) {
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1351
            JNU_ThrowByName(env, "javax/imageio/IIOException",
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1352
                 "Invalid icc profile: invalid data length");
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1353
            return NULL;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1354
        }
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1355
        total_length += (length - ICC_OVERHEAD_LEN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    if (total_length <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
              "Invalid icc profile: found only empty markers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    /* Allocate a Java byte array for assembled data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    data = (*env)->NewByteArray(env, total_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                        "Reading ICC profile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    icc_data = (JOCTET *)(*env)->GetPrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                                                           data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                                                           NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    if (icc_data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                        "Unable to pin icc profile data array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    /* and fill it in */
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1384
    dst_ptr = icc_data;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1385
    for (seq_no = first; seq_no < last; seq_no++) {
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1386
        JOCTET FAR *src_ptr = icc_markers[seq_no]->data + ICC_OVERHEAD_LEN;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1387
        unsigned int length =
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1388
            icc_markers[seq_no]->data_length - ICC_OVERHEAD_LEN;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1389
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1390
        memcpy(dst_ptr, src_ptr, length);
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1391
        dst_ptr += length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    /* finally, unpin the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    (*env)->ReleasePrimitiveArrayCritical(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                                          data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                                          icc_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                                          0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
/********************* end of ICC profile support *************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
/********************* Reader JNI calls ***********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_initReaderIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     jclass ImageInputStreamClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     jclass qTableClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     jclass huffClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1416
    CHECK_NULL(JPEGImageReader_readInputDataID = (*env)->GetMethodID(env,
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1417
                                                  cls,
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1418
                                                  "readInputData",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1419
                                                  "([BII)I"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1420
    CHECK_NULL(JPEGImageReader_skipInputBytesID = (*env)->GetMethodID(env,
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1421
                                                       cls,
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1422
                                                       "skipInputBytes",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1423
                                                       "(J)J"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1424
    CHECK_NULL(JPEGImageReader_warningOccurredID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                                                            cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                                                            "warningOccurred",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1427
                                                            "(I)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1428
    CHECK_NULL(JPEGImageReader_warningWithMessageID =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        (*env)->GetMethodID(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                            cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                            "warningWithMessage",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1432
                            "(Ljava/lang/String;)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1433
    CHECK_NULL(JPEGImageReader_setImageDataID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                                                         cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                                                         "setImageData",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1436
                                                         "(IIIII[B)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1437
    CHECK_NULL(JPEGImageReader_acceptPixelsID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                                                         cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                                                         "acceptPixels",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1440
                                                         "(IZ)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1441
    CHECK_NULL(JPEGImageReader_passStartedID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                                                        cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                                                        "passStarted",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1444
                                                        "(I)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1445
    CHECK_NULL(JPEGImageReader_passCompleteID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                                                         cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                                                         "passComplete",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1448
                                                         "()V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1449
    CHECK_NULL(JPEGImageReader_pushBackID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                                                     cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                                                     "pushBack",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1452
                                                     "(I)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1453
    CHECK_NULL(JPEGQTable_tableID = (*env)->GetFieldID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                                            qTableClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                                            "qTable",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1456
                                            "[I"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1457
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1458
    CHECK_NULL(JPEGHuffmanTable_lengthsID = (*env)->GetFieldID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                                                    huffClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                                                    "lengths",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1461
                                                    "[S"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1462
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1463
    CHECK_NULL(JPEGHuffmanTable_valuesID = (*env)->GetFieldID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                                                    huffClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                                                    "values",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1466
                                                    "[S"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_initJPEGImageReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    imageIODataPtr ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    struct sun_jpeg_error_mgr *jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    /* This struct contains the JPEG decompression parameters and pointers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * working space (which is allocated as needed by the JPEG library).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    struct jpeg_decompress_struct *cinfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        malloc(sizeof(struct jpeg_decompress_struct));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    if (cinfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                         "Initializing Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    /* We use our private extension JPEG error handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    jerr = malloc (sizeof(struct sun_jpeg_error_mgr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    if (jerr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                         "Initializing Reader");
4248
73d530928788 6888215: memory leak in jpeg plugin
bae
parents: 3009
diff changeset
  1496
        free(cinfo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    /* We set up the normal JPEG error routines, then override error_exit. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    cinfo->err = jpeg_std_error(&(jerr->pub));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    jerr->pub.error_exit = sun_jpeg_error_exit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    /* We need to setup our own print routines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    jerr->pub.output_message = sun_jpeg_output_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    /* Now we can setjmp before every call to the library */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        /* If we get here, the JPEG code has signaled an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        (*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                                      buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    /* Perform library initialization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    jpeg_create_decompress(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    // Set up to keep any APP2 markers, as these might contain ICC profile
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    // data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * Now set up our source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    cinfo->src =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        (struct jpeg_source_mgr *) malloc (sizeof(struct jpeg_source_mgr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    if (cinfo->src == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                        "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                        "Initializing Reader");
4248
73d530928788 6888215: memory leak in jpeg plugin
bae
parents: 3009
diff changeset
  1533
        imageio_dispose((j_common_ptr)cinfo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    cinfo->src->bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    cinfo->src->next_input_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    cinfo->src->init_source = imageio_init_source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    cinfo->src->fill_input_buffer = imageio_fill_input_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    cinfo->src->skip_input_data = imageio_skip_input_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    cinfo->src->resync_to_restart = jpeg_resync_to_restart; // use default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    cinfo->src->term_source = imageio_term_source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    /* set up the association to persist for future calls */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    ret = initImageioData(env, (j_common_ptr) cinfo, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    if (ret == NULL) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1547
        (*env)->ExceptionClear(env);
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1548
        JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1549
                        "Initializing Reader");
4248
73d530928788 6888215: memory leak in jpeg plugin
bae
parents: 3009
diff changeset
  1550
        imageio_dispose((j_common_ptr)cinfo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    }
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1553
    return ptr_to_jlong(ret);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
 * When we set a source from Java, we set up the stream in the streamBuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
 * object.  If there was an old one, it is released first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_setSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     jobject this,
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1565
     jlong ptr) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1567
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    j_common_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                        "Attempting to use reader after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    cinfo = data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
16881
f105ea43e3aa 8007667: Better image reading
bae
parents: 15627
diff changeset
  1579
    imageio_set_stream(env, cinfo, data, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    imageio_init_source((j_decompress_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
#define JPEG_APP1  (JPEG_APP0 + 1)  /* EXIF APP1 marker code  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
 * For EXIF images, the APP1 will appear immediately after the SOI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
 * so it's safe to only look at the first marker in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
 * (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 58)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
#define IS_EXIF(c) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    (((c)->marker_list != NULL) && ((c)->marker_list->marker == JPEG_APP1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_readImageHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     jlong ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     jboolean clearFirst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     jboolean reset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    int h_samp0, h_samp1, h_samp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    int v_samp0, v_samp1, v_samp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    jboolean retval = JNI_FALSE;
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1606
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    j_decompress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    struct jpeg_source_mgr *src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    sun_jpeg_error_ptr jerr;
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1610
    jbyteArray profileData = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                        "Attempting to use reader after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    cinfo = (j_decompress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    src = cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    jerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        /* If we get here, the JPEG code has signaled an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
           while reading the header. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            (*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                                          buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1638
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    printf("In readImageHeader, data is %p cinfo is %p\n", data, cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    printf("clearFirst is %d\n", clearFirst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1644
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                        "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                        "Array pin failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * Now clear the input buffer if the Java code has done a seek
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * on the stream since the last call, invalidating any buffer contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    if (clearFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        clearStreamBuffer(&data->streamBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        src->next_input_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        src->bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    ret = jpeg_read_header(cinfo, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    if (ret == JPEG_HEADER_TABLES_ONLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        retval = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        imageio_term_source(cinfo);  // Pushback remaining buffer contents
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1666
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        printf("just read tables-only image; q table 0 at %p\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
               cinfo->quant_tbl_ptrs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         * Now adjust the jpeg_color_space variable, which was set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
         * default_decompress_parms, to reflect our differences from IJG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        switch (cinfo->jpeg_color_space) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        case JCS_YCbCr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
             * There are several possibilities:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
             *  - we got image with embeded colorspace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
             *     Use it. User knows what he is doing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
             *  - we got JFIF image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
             *     Must be YCbCr (see http://www.w3.org/Graphics/JPEG/jfif3.pdf, page 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
             *  - we got EXIF image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
             *     Must be YCbCr (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 63)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
             *  - something else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
             *     Apply heuristical rules to identify actual colorspace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            if (cinfo->saw_Adobe_marker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                if (cinfo->Adobe_transform != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                     * IJG guesses this is YCbCr and emits a warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                     * We would rather not guess.  Then the user knows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                     * To read this as a Raster if at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                    cinfo->jpeg_color_space = JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                    cinfo->out_color_space = JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            } else if (!cinfo->saw_JFIF_marker && !IS_EXIF(cinfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                 * IJG assumes all unidentified 3-channels are YCbCr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                 * We assume that only if the second two channels are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                 * subsampled (either horizontally or vertically).  If not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                 * we assume RGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                 * 4776576: Some digital cameras output YCbCr JPEG images
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                 * that do not contain a JFIF APP0 marker but are only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                 * vertically subsampled (no horizontal subsampling).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                 * We should only assume this is RGB data if the subsampling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                 * factors for the second two channels are the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                 * first (check both horizontal and vertical factors).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                h_samp0 = cinfo->comp_info[0].h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                h_samp1 = cinfo->comp_info[1].h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                h_samp2 = cinfo->comp_info[2].h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                v_samp0 = cinfo->comp_info[0].v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                v_samp1 = cinfo->comp_info[1].v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                v_samp2 = cinfo->comp_info[2].v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                if ((h_samp1 == h_samp0) && (h_samp2 == h_samp0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    (v_samp1 == v_samp0) && (v_samp2 == v_samp0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    cinfo->jpeg_color_space = JCS_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                    /* output is already RGB, so it stays the same */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
#ifdef YCCALPHA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        case JCS_YCC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            cinfo->out_color_space = JCS_YCC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        case JCS_YCCK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            if ((cinfo->saw_Adobe_marker) && (cinfo->Adobe_transform != 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                 * IJG guesses this is YCCK and emits a warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                 * We would rather not guess.  Then the user knows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                 * To read this as a Raster if at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                cinfo->jpeg_color_space = JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                cinfo->out_color_space = JCS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        case JCS_CMYK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
             * IJG assumes all unidentified 4-channels are CMYK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
             * We assume that only if the second two channels are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
             * not subsampled (either horizontally or vertically).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
             * If they are, we assume YCCK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            h_samp0 = cinfo->comp_info[0].h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            h_samp1 = cinfo->comp_info[1].h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            h_samp2 = cinfo->comp_info[2].h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            v_samp0 = cinfo->comp_info[0].v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            v_samp1 = cinfo->comp_info[1].v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            v_samp2 = cinfo->comp_info[2].v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            if ((h_samp1 > h_samp0) && (h_samp2 > h_samp0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                (v_samp1 > v_samp0) && (v_samp2 > v_samp0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                cinfo->jpeg_color_space = JCS_YCCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                /* Leave the output space as CMYK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        RELEASE_ARRAYS(env, data, src->next_input_byte);
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1773
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1774
        /* read icc profile data */
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1775
        profileData = read_icc_profile(env, cinfo);
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1776
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1777
        if ((*env)->ExceptionCheck(env)) {
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1778
            return retval;
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1779
        }
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1780
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        (*env)->CallVoidMethod(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                               JPEGImageReader_setImageDataID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                               cinfo->image_width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                               cinfo->image_height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                               cinfo->jpeg_color_space,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                               cinfo->out_color_space,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                               cinfo->num_components,
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1788
                               profileData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        if (reset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            jpeg_abort_decompress(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_setOutColorSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     jlong ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     jint code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1805
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    j_decompress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                        "Attempting to use reader after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    cinfo = (j_decompress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    cinfo->out_color_space = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_readImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     jlong ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     jbyteArray buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     jint numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     jintArray srcBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     jintArray bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     jint sourceXStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     jint sourceYStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     jint sourceWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     jint sourceHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     jint stepX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     jint stepY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     jobjectArray qtables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     jobjectArray DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     jobjectArray ACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     jint minProgressivePass,  // Counts from 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     jint maxProgressivePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     jboolean wantUpdates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    struct jpeg_source_mgr *src;
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  1845
    JSAMPROW scanLinePtr = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    jint bands[MAX_BANDS];
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1847
    int i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    jint *body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    int scanlineLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    int pixelStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    unsigned char *in, *out, *pixelLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    int targetLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    int skipLines, linesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    pixelBufferPtr pb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    sun_jpeg_error_ptr jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    boolean mustScale = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    boolean progressive = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    boolean orderedBands = TRUE;
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1860
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    j_decompress_ptr cinfo;
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1862
    size_t numBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    /* verify the inputs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                        "Attempting to use reader after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    if ((buffer == NULL) || (srcBands == NULL))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    cinfo = (j_decompress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
8358
39c22ace748d 7018912: [parfait] potential buffer overruns in imageio jpeg
bae
parents: 8355
diff changeset
  1880
    if ((numBands < 1) || (numBands > MAX_BANDS) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        (sourceXStart < 0) || (sourceXStart >= (jint)cinfo->image_width) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        (sourceYStart < 0) || (sourceYStart >= (jint)cinfo->image_height) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        (sourceWidth < 1) || (sourceWidth > (jint)cinfo->image_width) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        (sourceHeight < 1) || (sourceHeight > (jint)cinfo->image_height) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        (stepX < 1) || (stepY < 1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        (minProgressivePass < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        (maxProgressivePass < minProgressivePass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                        "Invalid argument to native readImage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1894
    if (stepX > (jint)cinfo->image_width) {
4201
b3906ffdbcd6 6874643: ImageI/O JPEG is vulnerable to Heap Overflow
bae
parents: 3009
diff changeset
  1895
        stepX = cinfo->image_width;
b3906ffdbcd6 6874643: ImageI/O JPEG is vulnerable to Heap Overflow
bae
parents: 3009
diff changeset
  1896
    }
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  1897
    if (stepY > (jint)cinfo->image_height) {
4201
b3906ffdbcd6 6874643: ImageI/O JPEG is vulnerable to Heap Overflow
bae
parents: 3009
diff changeset
  1898
        stepY = cinfo->image_height;
b3906ffdbcd6 6874643: ImageI/O JPEG is vulnerable to Heap Overflow
bae
parents: 3009
diff changeset
  1899
    }
b3906ffdbcd6 6874643: ImageI/O JPEG is vulnerable to Heap Overflow
bae
parents: 3009
diff changeset
  1900
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * First get the source bands array and copy it to our local array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * so we don't have to worry about pinning and unpinning it again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    body = (*env)->GetIntArrayElements(env, srcBands, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    if (body == NULL) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1908
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                         "Initializing Read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    for (i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        bands[i] = body[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        if (orderedBands && (bands[i] != i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            orderedBands = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    (*env)->ReleaseIntArrayElements(env, srcBands, body, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  1924
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    printf("---- in reader.read ----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    printf("numBands is %d\n", numBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    printf("bands array: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    for (i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        printf("%d ", bands[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    printf("jq table 0 at %p\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
               cinfo->quant_tbl_ptrs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    src = cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    /* Set the buffer as our PixelBuffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    pb = &data->pixelBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    if (setPixelBuffer(env, pb, buffer) == NOT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        return data->abortFlag;  // We already threw an out of memory exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    jerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        /* If we get here, the JPEG code has signaled an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
           while reading. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            (*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                                          buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        }
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  1959
        if (scanLinePtr != NULL) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  1960
            free(scanLinePtr);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  1961
            scanLinePtr = NULL;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  1962
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        return data->abortFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
    if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  1967
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                        "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                        "Array pin failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        return data->abortFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
    // If there are no tables in our structure and table arguments aren't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    // NULL, use the table arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    if ((qtables != NULL) && (cinfo->quant_tbl_ptrs[0] == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        (void) setQTables(env, (j_common_ptr) cinfo, qtables, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    if ((DCHuffmanTables != NULL) && (cinfo->dc_huff_tbl_ptrs[0] == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        setHTables(env, (j_common_ptr) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                   DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                   ACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                   TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    progressive = jpeg_has_multiple_scans(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    if (progressive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        cinfo->buffered_image = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        cinfo->input_scan_number = minProgressivePass+1; // Java count from 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
#define MAX_JAVA_INT 2147483647 // XXX Is this defined in JNI somewhere?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        if (maxProgressivePass < MAX_JAVA_INT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            maxProgressivePass++; // For testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    data->streamBuf.suspendable = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    jpeg_start_decompress(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2001
    if (numBands !=  cinfo->output_components) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2002
        JNU_ThrowByName(env, "javax/imageio/IIOException",
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2003
                        "Invalid argument to native readImage");
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2004
        return data->abortFlag;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2005
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2006
9827
add64ce324a8 7013519: [parfait] Integer overflows in 2D code
bae
parents: 8358
diff changeset
  2007
    if (cinfo->output_components <= 0 ||
add64ce324a8 7013519: [parfait] Integer overflows in 2D code
bae
parents: 8358
diff changeset
  2008
        cinfo->image_width > (0xffffffffu / (unsigned int)cinfo->output_components))
add64ce324a8 7013519: [parfait] Integer overflows in 2D code
bae
parents: 8358
diff changeset
  2009
    {
add64ce324a8 7013519: [parfait] Integer overflows in 2D code
bae
parents: 8358
diff changeset
  2010
        JNU_ThrowByName(env, "javax/imageio/IIOException",
add64ce324a8 7013519: [parfait] Integer overflows in 2D code
bae
parents: 8358
diff changeset
  2011
                        "Invalid number of output components");
add64ce324a8 7013519: [parfait] Integer overflows in 2D code
bae
parents: 8358
diff changeset
  2012
        return data->abortFlag;
add64ce324a8 7013519: [parfait] Integer overflows in 2D code
bae
parents: 8358
diff changeset
  2013
    }
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2014
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2015
    // Allocate a 1-scanline buffer
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2016
    scanLinePtr = (JSAMPROW)malloc(cinfo->image_width*cinfo->output_components);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2017
    if (scanLinePtr == NULL) {
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2018
        RELEASE_ARRAYS(env, data, src->next_input_byte);
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2019
        JNU_ThrowByName( env,
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2020
                         "java/lang/OutOfMemoryError",
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2021
                         "Reading JPEG Stream");
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2022
        return data->abortFlag;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2023
    }
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2024
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    // loop over progressive passes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
    done = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        if (progressive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            // initialize the next pass.  Note that this skips up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            // the first interesting pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            jpeg_start_output(cinfo, cinfo->input_scan_number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            if (wantUpdates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                (*env)->CallVoidMethod(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                                       JPEGImageReader_passStartedID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                                       cinfo->input_scan_number-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        } else if (wantUpdates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            (*env)->CallVoidMethod(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                                   JPEGImageReader_passStartedID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                                   0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        // Skip until the first interesting line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        while ((data->abortFlag == JNI_FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
               && ((jint)cinfo->output_scanline < sourceYStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            jpeg_read_scanlines(cinfo, &scanLinePtr, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        scanlineLimit = sourceYStart+sourceHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        pixelLimit = scanLinePtr
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2052
            +(sourceXStart+sourceWidth)*cinfo->output_components;
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2053
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2054
        pixelStride = stepX*cinfo->output_components;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        targetLine = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        while ((data->abortFlag == JNI_FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
               && ((jint)cinfo->output_scanline < scanlineLimit)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            jpeg_read_scanlines(cinfo, &scanLinePtr, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            // Now mangle it into our buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            out = data->pixelBuf.buf.bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            if (orderedBands && (pixelStride == numBands)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                // Optimization: The component bands are ordered sequentially,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                // so we can simply use memcpy() to copy the intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                // scanline buffer into the raster.
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2069
                in = scanLinePtr + (sourceXStart * cinfo->output_components);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                if (pixelLimit > in) {
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2071
                    numBytes = pixelLimit - in;
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2072
                    if (numBytes > data->pixelBuf.byteBufferLength) {
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2073
                        numBytes = data->pixelBuf.byteBufferLength;
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2074
                    }
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2075
                    memcpy(out, in, numBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            } else {
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2078
                numBytes = numBands;
3009
de653b2cab31 4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents: 2383
diff changeset
  2079
                for (in = scanLinePtr+sourceXStart*cinfo->output_components;
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2080
                     in < pixelLimit &&
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2081
                       numBytes <= data->pixelBuf.byteBufferLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                     in += pixelStride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                    for (i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                        *out++ = *(in+bands[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                    }
5189
4909c522e208 6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents: 4285
diff changeset
  2086
                    numBytes += numBands;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            // And call it back to Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            (*env)->CallVoidMethod(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                                   this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                                   JPEGImageReader_acceptPixelsID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                                   targetLine++,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                                   progressive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                || !GET_ARRAYS(env, data, &(src->next_input_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            // And skip over uninteresting lines to the next subsampled line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            // Ensure we don't go past the end of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            // Lines to skip based on subsampling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            skipLines = stepY - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            // Lines left in the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            linesLeft =  scanlineLimit - cinfo->output_scanline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            // Take the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            if (skipLines > linesLeft) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                skipLines = linesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            for(i = 0; i < skipLines; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                jpeg_read_scanlines(cinfo, &scanLinePtr, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        if (progressive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            jpeg_finish_output(cinfo); // Increments pass counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            // Call Java to notify pass complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            if (jpeg_input_complete(cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                || (cinfo->input_scan_number > maxProgressivePass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                done = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            done = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        if (wantUpdates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            (*env)->CallVoidMethod(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                                   JPEGImageReader_passCompleteID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * We are done, but we might not have read all the lines, or all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * the passes, so use jpeg_abort instead of jpeg_finish_decompress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    if (cinfo->output_scanline == cinfo->output_height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        //    if ((cinfo->output_scanline == cinfo->output_height) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        //(jpeg_input_complete(cinfo))) {  // We read the whole file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        jpeg_finish_decompress(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        jpeg_abort_decompress(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
    free(scanLinePtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
    RELEASE_ARRAYS(env, data, src->next_input_byte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    return data->abortFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_abortRead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     jlong ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2159
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                        "Attempting to use reader after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    imageio_abort(env, this, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_resetLibraryState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     jlong ptr) {
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2177
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    j_decompress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                        "Attempting to use reader after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    cinfo = (j_decompress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    jpeg_abort_decompress(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_resetReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     jlong ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2199
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    j_decompress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    sun_jpeg_error_ptr jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                        "Attempting to use reader after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    cinfo = (j_decompress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
    jerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    imageio_reset(env, (j_common_ptr) cinfo, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * The tables have not been reset, and there is no way to do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * in IJG without leaking memory.  The only situation in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * this will cause a problem is if an image-only stream is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * with this object without initializing the correct tables first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * This situation, which should cause an error, might work but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * produce garbage instead.  If the huffman tables are wrong,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * it will fail during the decode.  If the q tables are wrong, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * will look strange.  This is very unlikely, so don't worry about
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * it.  To be really robust, we would keep a flag for table state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     * and consult it to catch exceptional situations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    /* above does not clean up the source, so we have to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
      We need to explicitly initialize exception handler or we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
       longjump to random address from the term_source()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
          We may get IOException from pushBack() here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
          However it could be legal if original input stream was closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
          earlier (for example because network connection was closed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
          Unfortunately, image inputstream API has no way to check whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
          stream is already closed or IOException was thrown because of some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
          other IO problem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
          And we can not avoid call to pushBack() on closed stream for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
          same reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
          So, for now we will silently eat this exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
          NB: this may be changed in future when ImageInputStream API will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
          become more flexible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            (*env)->ExceptionClear(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        cinfo->src->term_source(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    cinfo->src->bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
    cinfo->src->next_input_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_disposeReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     jclass reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     jlong ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2272
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    j_common_ptr info = destroyImageioData(env, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    imageio_dispose(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
/********************** end of Reader *************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
/********************** Writer Support ************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
/********************** Destination Manager *******************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
 * Initialize destination --- called by jpeg_start_compress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
 * before any data is actually written.  The data arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
 * must be pinned before this is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
imageio_init_destination (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
    struct jpeg_destination_mgr *dest = cinfo->dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
    imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    streamBufferPtr sb = &data->streamBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
    if (sb->buf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        // We forgot to pin the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        (*env)->FatalError(env, "Output buffer not pinned!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
    dest->next_output_byte = sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
    dest->free_in_buffer = sb->bufferLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
 * Empty the output buffer --- called whenever buffer fills up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
 * This routine writes the entire output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
 * (ignoring the current state of next_output_byte & free_in_buffer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
 * resets the pointer & count to the start of the buffer, and returns TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
 * indicating that the buffer has been dumped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
imageio_empty_output_buffer (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    struct jpeg_destination_mgr *dest = cinfo->dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
    imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    streamBufferPtr sb = &data->streamBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2322
    jobject output = NULL;
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2323
19788
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  2324
    RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte));
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  2325
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2326
    GET_IO_REF(output);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
    (*env)->CallVoidMethod(env,
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2329
                           output,
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 16881
diff changeset
  2330
                           JPEGImageWriter_writeOutputDataID,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                           sb->hstreamBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                           0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                           sb->bufferLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
    if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        || !GET_ARRAYS(env, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                       (const JOCTET **)(&dest->next_output_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
    dest->next_output_byte = sb->buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    dest->free_in_buffer = sb->bufferLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
 * After all of the data has been encoded there may still be some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
 * more left over in some of the working buffers.  Now is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
 * time to clear them out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
imageio_term_destination (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
    struct jpeg_destination_mgr *dest = cinfo->dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
    imageIODataPtr data = (imageIODataPtr) cinfo->client_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
    streamBufferPtr sb = &data->streamBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    /* find out how much needs to be written */
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2360
    /* this conversion from size_t to jint is safe, because the lenght of the buffer is limited by jint */
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2361
    jint datacount = (jint)(sb->bufferLength - dest->free_in_buffer);
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2362
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
    if (datacount != 0) {
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2364
        jobject output = NULL;
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2365
19788
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  2366
        RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte));
2899c95495a9 8024697: Fix for 8020983 causes Xcheck:jni warnings
bae
parents: 19017
diff changeset
  2367
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2368
        GET_IO_REF(output);
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2369
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        (*env)->CallVoidMethod(env,
19017
0528383260ef 8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents: 16882
diff changeset
  2371
                               output,
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 16881
diff changeset
  2372
                               JPEGImageWriter_writeOutputDataID,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                               sb->hstreamBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                               0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                               datacount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            || !GET_ARRAYS(env, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                           (const JOCTET **)(&dest->next_output_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    dest->next_output_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
    dest->free_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
 * Flush the destination buffer.  This is not called by the library,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
 * but by our code below.  This is the simplest implementation, though
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
 * certainly not the most efficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
imageio_flush_destination(j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
    imageio_term_destination(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    imageio_init_destination(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
/********************** end of destination manager ************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
/********************** Writer JNI calls **********************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_initWriterIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     jclass qTableClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     jclass huffClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2413
    CHECK_NULL(JPEGImageWriter_writeOutputDataID = (*env)->GetMethodID(env,
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 16881
diff changeset
  2414
                                                    cls,
c9b0f63bb215 8007918: Better image writing
bae
parents: 16881
diff changeset
  2415
                                                    "writeOutputData",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2416
                                                    "([BII)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2417
    CHECK_NULL(JPEGImageWriter_warningOccurredID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                                                            cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                                                            "warningOccurred",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2420
                                                            "(I)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2421
    CHECK_NULL(JPEGImageWriter_warningWithMessageID =
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2422
                                        (*env)->GetMethodID(env,
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2423
                                                            cls,
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2424
                                                            "warningWithMessage",
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2425
                                                            "(Ljava/lang/String;)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2426
    CHECK_NULL(JPEGImageWriter_writeMetadataID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                                                          cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                                                          "writeMetadata",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2429
                                                          "()V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2430
    CHECK_NULL(JPEGImageWriter_grabPixelsID = (*env)->GetMethodID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                                                       cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                                                       "grabPixels",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2433
                                                       "(I)V"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2434
    CHECK_NULL(JPEGQTable_tableID = (*env)->GetFieldID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                                            qTableClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                                            "qTable",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2437
                                            "[I"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2438
    CHECK_NULL(JPEGHuffmanTable_lengthsID = (*env)->GetFieldID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                                                    huffClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                                                    "lengths",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2441
                                                    "[S"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2442
    CHECK_NULL(JPEGHuffmanTable_valuesID = (*env)->GetFieldID(env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                                                    huffClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                                                    "values",
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2445
                                                    "[S"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_initJPEGImageWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    imageIODataPtr ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    struct sun_jpeg_error_mgr *jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
    struct jpeg_destination_mgr *dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
    /* This struct contains the JPEG compression parameters and pointers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * working space (which is allocated as needed by the JPEG library).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
    struct jpeg_compress_struct *cinfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        malloc(sizeof(struct jpeg_compress_struct));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
    if (cinfo == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                         "Initializing Writer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
    /* We use our private extension JPEG error handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
    jerr = malloc (sizeof(struct sun_jpeg_error_mgr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
    if (jerr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                         "Initializing Writer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        free(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        return 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
    /* We set up the normal JPEG error routines, then override error_exit. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
    cinfo->err = jpeg_std_error(&(jerr->pub));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
    jerr->pub.error_exit = sun_jpeg_error_exit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    /* We need to setup our own print routines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    jerr->pub.output_message = sun_jpeg_output_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
    /* Now we can setjmp before every call to the library */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        /* If we get here, the JPEG code has signaled an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
        char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        (*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                                      buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
    /* Perform library initialization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
    jpeg_create_compress(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    /* Now set up the destination  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
    dest = malloc(sizeof(struct jpeg_destination_mgr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    if (dest == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                         "Initializing Writer");
4248
73d530928788 6888215: memory leak in jpeg plugin
bae
parents: 3009
diff changeset
  2506
        imageio_dispose((j_common_ptr)cinfo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
    dest->init_destination = imageio_init_destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
    dest->empty_output_buffer = imageio_empty_output_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
    dest->term_destination = imageio_term_destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
    dest->next_output_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
    dest->free_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    cinfo->dest = dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    /* set up the association to persist for future calls */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    ret = initImageioData(env, (j_common_ptr) cinfo, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
    if (ret == NULL) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2521
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                         "Initializing Writer");
4248
73d530928788 6888215: memory leak in jpeg plugin
bae
parents: 3009
diff changeset
  2525
        imageio_dispose((j_common_ptr)cinfo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
    }
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2528
    return ptr_to_jlong(ret);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_setDest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     jobject this,
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 16881
diff changeset
  2535
     jlong ptr) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2537
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    j_compress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                        "Attempting to use writer after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
    cinfo = (j_compress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
16882
c9b0f63bb215 8007918: Better image writing
bae
parents: 16881
diff changeset
  2549
    imageio_set_stream(env, data->jpegObj, data, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
    // Don't call the init method, as that depends on pinned arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
    cinfo->dest->next_output_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
    cinfo->dest->free_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_writeTables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     jlong ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     jobjectArray qtables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     jobjectArray DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     jobjectArray ACHuffmanTables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
    struct jpeg_destination_mgr *dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
    sun_jpeg_error_ptr jerr;
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2568
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
    j_compress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                        "Attempting to use writer after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
    cinfo = (j_compress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    dest = cinfo->dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
    jerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        /* If we get here, the JPEG code has signaled an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
           while writing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
            (*cinfo->err->format_message) ((j_common_ptr) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                                          buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
    if (GET_ARRAYS(env, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                   (const JOCTET **)(&dest->next_output_byte)) == NOT_OK) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2599
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                        "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                        "Array pin failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
    jpeg_suppress_tables(cinfo, TRUE);  // Suppress writing of any current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
    data->streamBuf.suspendable = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    if (qtables != NULL) {
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  2610
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        printf("in writeTables: qtables not NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        setQTables(env, (j_common_ptr) cinfo, qtables, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    if (DCHuffmanTables != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        setHTables(env, (j_common_ptr) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                   DCHuffmanTables, ACHuffmanTables, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
    jpeg_write_tables(cinfo); // Flushes the buffer for you
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
    RELEASE_ARRAYS(env, data, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_writeImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
     jlong ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
     jbyteArray buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
     jint inCs, jint outCs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
     jint numBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
     jintArray bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
     jint srcWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
     jint destWidth, jint destHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     jint stepX, jint stepY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     jobjectArray qtables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
     jboolean writeDQT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
     jobjectArray DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
     jobjectArray ACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
     jboolean writeDHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
     jboolean optimize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
     jboolean progressive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
     jint numScans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
     jintArray scanInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
     jintArray componentIds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
     jintArray HsamplingFactors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
     jintArray VsamplingFactors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
     jintArray QtableSelectors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
     jboolean haveMetadata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
     jint restartInterval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    struct jpeg_destination_mgr *dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
    JSAMPROW scanLinePtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
    int pixelStride;
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2657
    unsigned char *in, *out, *pixelLimit, *scanLineLimit;
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2658
    unsigned int scanLineSize, pixelBufferSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
    int targetLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
    pixelBufferPtr pb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
    sun_jpeg_error_ptr jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
    jint *ids, *hfactors, *vfactors, *qsels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
    jsize qlen, hlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
    int *scanptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
    jint *scanData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
    jint *bandSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
    int maxBandValue, halfMaxBandValue;
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2668
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
    j_compress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
    UINT8** scale = NULL;
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2671
    boolean success = TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2673
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
    /* verify the inputs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                        "Attempting to use writer after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    if ((buffer == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        (qtables == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        // H tables can be null if optimizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        (componentIds == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        (HsamplingFactors == NULL) || (VsamplingFactors == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        (QtableSelectors == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
        ((numScans != 0) && (scanInfo != NULL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2696
    scanLineSize = destWidth * numBands;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
    if ((inCs < 0) || (inCs > JCS_YCCK) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        (outCs < 0) || (outCs > JCS_YCCK) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
        (numBands < 1) || (numBands > MAX_BANDS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        (srcWidth < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
        (destWidth < 0) || (destWidth > srcWidth) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
        (destHeight < 0) ||
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2703
        (stepX < 0) || (stepY < 0) ||
8749
ab0c8dca6d47 6989760: cmm native compiler warnings
bae
parents: 8358
diff changeset
  2704
        ((INT_MAX / numBands) < destWidth))  /* destWidth causes an integer overflow */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        JNU_ThrowByName(env, "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                        "Invalid argument to native writeImage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2711
    if (stepX > srcWidth) {
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2712
        stepX = srcWidth;
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2713
    }
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2714
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
    bandSize = (*env)->GetIntArrayElements(env, bandSizes, NULL);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2716
    CHECK_NULL_RETURN(bandSize, JNI_FALSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
    for (i = 0; i < numBands; i++) {
20798
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2719
        if (bandSize[i] <= 0 || bandSize[i] > JPEG_BAND_SIZE) {
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2720
            (*env)->ReleaseIntArrayElements(env, bandSizes,
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2721
                                            bandSize, JNI_ABORT);
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2722
            JNU_ThrowByName(env, "javax/imageio/IIOException", "Invalid Image");
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2723
            return JNI_FALSE;
20798
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2724
        }
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2725
    }
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2726
d03e6abc2b51 8013510: Augment image writing code
jchen
parents: 16882
diff changeset
  2727
    for (i = 0; i < numBands; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        if (bandSize[i] != JPEG_BAND_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            if (scale == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                scale = (UINT8**) calloc(numBands, sizeof(UINT8*));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                if (scale == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                    JNU_ThrowByName( env, "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                                     "Writing JPEG Stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
            maxBandValue = (1 << bandSize[i]) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
            scale[i] = (UINT8*) malloc((maxBandValue + 1) * sizeof(UINT8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
            if (scale[i] == NULL) {
15627
46054ae31dfc 8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents: 9842
diff changeset
  2744
                // Cleanup before throwing an out of memory exception
46054ae31dfc 8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents: 9842
diff changeset
  2745
                for (j = 0; j < i; j++) {
46054ae31dfc 8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents: 9842
diff changeset
  2746
                    free(scale[j]);
46054ae31dfc 8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents: 9842
diff changeset
  2747
                }
46054ae31dfc 8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents: 9842
diff changeset
  2748
                free(scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                JNU_ThrowByName( env, "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                                 "Writing JPEG Stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            halfMaxBandValue = maxBandValue >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            for (j = 0; j <= maxBandValue; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                scale[i][j] = (UINT8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                    ((j*MAX_JPEG_BAND_VALUE + halfMaxBandValue)/maxBandValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    (*env)->ReleaseIntArrayElements(env, bandSizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                                    bandSize, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
    cinfo = (j_compress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
    dest = cinfo->dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
    /* Set the buffer as our PixelBuffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
    pb = &data->pixelBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
    if (setPixelBuffer(env, pb, buffer) == NOT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
        return data->abortFlag;  // We already threw an out of memory exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
    // Allocate a 1-scanline buffer
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2777
    scanLinePtr = (JSAMPROW)malloc(scanLineSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
    if (scanLinePtr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        JNU_ThrowByName( env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
                         "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
                         "Writing JPEG Stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
        return data->abortFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
    }
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2785
    scanLineLimit = scanLinePtr + scanLineSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
    /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
    jerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    if (setjmp(jerr->setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        /* If we get here, the JPEG code has signaled an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
           while writing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
            char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
            (*cinfo->err->format_message) ((j_common_ptr) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                                          buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
            JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
        }
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2800
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2801
        if (scale != NULL) {
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2802
            for (i = 0; i < numBands; i++) {
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2803
                if (scale[i] != NULL) {
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2804
                    free(scale[i]);
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2805
                }
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2806
            }
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2807
            free(scale);
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2808
        }
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  2809
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        free(scanLinePtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
        return data->abortFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
    // set up parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
    cinfo->image_width = destWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
    cinfo->image_height = destHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
    cinfo->input_components = numBands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
    cinfo->in_color_space = inCs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
    jpeg_set_defaults(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    jpeg_set_colorspace(cinfo, outCs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
    cinfo->optimize_coding = optimize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
    cinfo->write_JFIF_header = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
    cinfo->write_Adobe_marker = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
    // copy componentIds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
    ids = (*env)->GetIntArrayElements(env, componentIds, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
    hfactors = (*env)->GetIntArrayElements(env, HsamplingFactors, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
    vfactors = (*env)->GetIntArrayElements(env, VsamplingFactors, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
    qsels = (*env)->GetIntArrayElements(env, QtableSelectors, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2834
    if (ids && hfactors && vfactors && qsels) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2835
        for (i = 0; i < numBands; i++) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2836
            cinfo->comp_info[i].component_id = ids[i];
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2837
            cinfo->comp_info[i].h_samp_factor = hfactors[i];
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2838
            cinfo->comp_info[i].v_samp_factor = vfactors[i];
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2839
            cinfo->comp_info[i].quant_tbl_no = qsels[i];
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2840
        }
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2841
    } else {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2842
        success = FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2845
    if (ids) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2846
        (*env)->ReleaseIntArrayElements(env, componentIds, ids, JNI_ABORT);
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2847
    }
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2848
    if (hfactors) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2849
        (*env)->ReleaseIntArrayElements(env, HsamplingFactors, hfactors, JNI_ABORT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
    }
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2851
    if (vfactors) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2852
        (*env)->ReleaseIntArrayElements(env, VsamplingFactors, vfactors, JNI_ABORT);
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2853
    }
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2854
    if (qsels) {
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2855
        (*env)->ReleaseIntArrayElements(env, QtableSelectors, qsels, JNI_ABORT);
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2856
    }
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2857
    if (!success) return data->abortFlag;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
    jpeg_suppress_tables(cinfo, TRUE);  // Disable writing any current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
    qlen = setQTables(env, (j_common_ptr) cinfo, qtables, writeDQT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
    if (!optimize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        // Set the h tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        hlen = setHTables(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                          (j_common_ptr) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                          DCHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                          ACHuffmanTables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                          writeDHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
    if (GET_ARRAYS(env, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                   (const JOCTET **)(&dest->next_output_byte)) == NOT_OK) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2874
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                        "javax/imageio/IIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                        "Array pin failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        return data->abortFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
    data->streamBuf.suspendable = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
    if (progressive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
        if (numScans == 0) { // then use default scans
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            jpeg_simple_progression(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
            cinfo->num_scans = numScans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
            // Copy the scanInfo to a local array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            // The following is copied from jpeg_simple_progression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
  /* Allocate space for script.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
   * We need to put it in the permanent pool in case the application performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
   * multiple compressions without changing the settings.  To avoid a memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
   * leak if jpeg_simple_progression is called repeatedly for the same JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
   * object, we try to re-use previously allocated space, and we allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
   * enough space to handle YCbCr even if initially asked for grayscale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
            if (cinfo->script_space == NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                || cinfo->script_space_size < numScans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                cinfo->script_space_size = MAX(numScans, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                cinfo->script_space = (jpeg_scan_info *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
                                                JPOOL_PERMANENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                                                cinfo->script_space_size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                                                * sizeof(jpeg_scan_info));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
            cinfo->scan_info = cinfo->script_space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
            scanptr = (int *) cinfo->script_space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
            scanData = (*env)->GetIntArrayElements(env, scanInfo, NULL);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
  2909
            CHECK_NULL_RETURN(scanData, data->abortFlag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
            // number of jints per scan is 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            // We avoid a memcpy to handle different size ints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
            for (i = 0; i < numScans*9; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                scanptr[i] = scanData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
            (*env)->ReleaseIntArrayElements(env, scanInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                                            scanData, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
    cinfo->restart_interval = restartInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  2923
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
    printf("writer setup complete, starting compressor\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
    // start the compressor; tables must already be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
    jpeg_start_compress(cinfo, FALSE); // Leaves sent_table alone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
    if (haveMetadata) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        // Flush the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
        imageio_flush_destination(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        // Call Java to write the metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
        RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        (*env)->CallVoidMethod(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                               this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                               JPEGImageWriter_writeMetadataID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            || !GET_ARRAYS(env, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                           (const JOCTET **)(&dest->next_output_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
    targetLine = 0;
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2946
    pixelBufferSize = srcWidth * numBands;
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2947
    pixelStride = numBands * stepX;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    // for each line in destHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
    while ((data->abortFlag == JNI_FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
           && (cinfo->next_scanline < cinfo->image_height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
        // get the line from Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
        (*env)->CallVoidMethod(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                               this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                               JPEGImageWriter_grabPixelsID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                               targetLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        if ((*env)->ExceptionOccurred(env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
            || !GET_ARRAYS(env, data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                           (const JOCTET **)(&dest->next_output_byte))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
                cinfo->err->error_exit((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        // subsample it into our buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        in = data->pixelBuf.buf.bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
        out = scanLinePtr;
6865
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2968
        pixelLimit = in + ((pixelBufferSize > data->pixelBuf.byteBufferLength) ?
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2969
                           data->pixelBuf.byteBufferLength : pixelBufferSize);
e7987a9205f4 6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents: 5506
diff changeset
  2970
        for (; (in < pixelLimit) && (out < scanLineLimit); in += pixelStride) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            for (i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                if (scale !=NULL && scale[i] != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                    *out++ = scale[i][*(in+i)];
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  2974
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                    if (in == data->pixelBuf.buf.bp){ // Just the first pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                        printf("in %d -> out %d, ", *(in+i), *(out-i-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
2383
c6a2226cc4de 6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents: 2
diff changeset
  2980
#ifdef DEBUG_IIO_JPEG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                    if (in == data->pixelBuf.buf.bp){ // Just the first pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                        printf("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
                    *out++ = *(in+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        // write it out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        jpeg_write_scanlines(cinfo, (JSAMPARRAY)&scanLinePtr, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        targetLine += stepY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     * We are done, but we might not have done all the lines,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * so use jpeg_abort instead of jpeg_finish_compress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
    if (cinfo->next_scanline == cinfo->image_height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
        jpeg_finish_compress(cinfo);  // Flushes buffer with term_dest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        jpeg_abort((j_common_ptr)cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
    if (scale != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
        for (i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            if (scale[i] != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                free(scale[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        free(scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
    free(scanLinePtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
    RELEASE_ARRAYS(env, data, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
    return data->abortFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_abortWrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
     jlong ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  3025
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
                        "Attempting to use writer after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
    imageio_abort(env, this, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_resetWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
     jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
     jlong ptr) {
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  3042
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
    j_compress_ptr cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                        "java/lang/IllegalStateException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
                        "Attempting to use writer after dispose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
    cinfo = (j_compress_ptr) data->jpegObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
    imageio_reset(env, (j_common_ptr) cinfo, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
     * The tables have not been reset, and there is no way to do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
     * in IJG without leaking memory.  The only situation in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
     * this will cause a problem is if an image-only stream is written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
     * with this object without initializing the correct tables first,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * which should not be possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
    cinfo->dest->next_output_byte = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
    cinfo->dest->free_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_disposeWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
    (JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
     jclass writer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
     jlong ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 7668
diff changeset
  3074
    imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
    j_common_ptr info = destroyImageioData(env, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
    imageio_dispose(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
}