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