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