jdk/src/share/native/sun/awt/image/jpeg/jpegdecoder.c
author prr
Fri, 20 Dec 2013 09:58:29 -0800
changeset 23892 9959956918be
parent 23644 0cdb97daeef5
child 23917 d247e52b5262
permissions -rw-r--r--
8029854: Enhance JPEG decodings Reviewed-by: bae, vadim, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
     2
 * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * This file was based upon the example.c stub file included in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * release 6 of the Independent JPEG Group's free JPEG software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * It has been updated to conform to release 6b.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/* First, if system header files define "boolean" map it to "system_boolean" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#define boolean system_boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <setjmp.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/* undo "system_boolean" hack and undef FAR since we don't use it anyway */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#undef boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#undef FAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include <jpeglib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include "jerror.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/* The method IDs we cache. Note that the last two belongs to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * java.io.InputStream class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static jmethodID sendHeaderInfoID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static jmethodID sendPixelsByteID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
static jmethodID sendPixelsIntID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
static jmethodID InputStream_readID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static jmethodID InputStream_availableID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/* Initialize the Java VM instance variable when the library is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
   first loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
JNI_OnLoad(JavaVM *vm, void *reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    return JNI_VERSION_1_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * ERROR HANDLING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The JPEG library's standard error handler (jerror.c) is divided into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * several "methods" which you can override individually.  This lets you
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * adjust the behavior without duplicating a lot of code, which you might
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * have to update with each future release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Our example here shows how to override the "error_exit" method so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * control is returned to the library's caller when a fatal error occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * rather than calling exit() as the standard error_exit method does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * We use C's setjmp/longjmp facility to return control.  This means that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * routine which calls the JPEG library must first execute a setjmp() call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * establish the return point.  We want the replacement error_exit to do a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * longjmp().  But we need to make the setjmp buffer accessible to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * error_exit routine.  To do this, we make a private extension of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * standard JPEG error handler object.  (If we were using C++, we'd say we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * were making a subclass of the regular error handler.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Here's the extended error handler struct:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
struct sun_jpeg_error_mgr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  struct jpeg_error_mgr pub;    /* "public" fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  jmp_buf setjmp_buffer;        /* for return to caller */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
typedef struct sun_jpeg_error_mgr * sun_jpeg_error_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Here's the routine that will replace the standard error_exit method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
sun_jpeg_error_exit (j_common_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
  /* cinfo->err really points to a sun_jpeg_error_mgr struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  sun_jpeg_error_ptr myerr = (sun_jpeg_error_ptr) cinfo->err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  /* Always display the message. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  /* We could postpone this until after returning, if we chose. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  /* (*cinfo->err->output_message) (cinfo); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  /* For Java, we will format the message and put it in the error we throw. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  /* Return control to the setjmp point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  longjmp(myerr->setjmp_buffer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Error Message handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * This overrides the output_message method to send JPEG messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
sun_jpeg_output_message (j_common_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  /* Create the message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  (*cinfo->err->format_message) (cinfo, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  /* Send it to stderr, adding a newline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  fprintf(stderr, "%s\n", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * INPUT HANDLING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * The JPEG library's input management is defined by the jpeg_source_mgr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * structure which contains two fields to convey the information in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * buffer and 5 methods which perform all buffer management.  The library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * defines a standard input manager that uses stdio for obtaining compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * jpeg data, but here we need to use Java to get our data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * We need to make the Java class information accessible to the source_mgr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * input routines.  We also need to store a pointer to the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * Java array being used as an input buffer so that it is not moved or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * garbage collected while the JPEG library is using it.  To store these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * things, we make a private extension of the standard JPEG jpeg_source_mgr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * Here's the extended source manager struct:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
struct sun_jpeg_source_mgr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
  struct jpeg_source_mgr pub;   /* "public" fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
  jobject hInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  int suspendable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
  unsigned long remaining_skip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  JOCTET *inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
  jbyteArray hInputBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
  size_t inbufoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
  /* More stuff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
  union pixptr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
      int               *ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
      unsigned char     *bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
  } outbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  jobject hOutputBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
typedef struct sun_jpeg_source_mgr * sun_jpeg_source_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
/* We use Get/ReleasePrimitiveArrayCritical functions to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * the need to copy buffer elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * MAKE SURE TO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *   callbacks to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * - call RELEASE_ARRAYS before returning to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * Otherwise things will go horribly wrong. There may be memory leaks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * excessive pinning, or even VM crashes!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * Note that GetPrimitiveArrayCritical may fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
static void RELEASE_ARRAYS(JNIEnv *env, sun_jpeg_source_ptr src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    if (src->inbuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (src->pub.next_input_byte == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            src->inbufoffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            src->inbufoffset = src->pub.next_input_byte - src->inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        (*env)->ReleasePrimitiveArrayCritical(env, src->hInputBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                              src->inbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        src->inbuf = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    if (src->outbuf.ip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        (*env)->ReleasePrimitiveArrayCritical(env, src->hOutputBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                              src->outbuf.ip, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        src->outbuf.ip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
static int GET_ARRAYS(JNIEnv *env, sun_jpeg_source_ptr src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    if (src->hInputBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        assert(src->inbuf == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        src->inbuf = (JOCTET *)(*env)->GetPrimitiveArrayCritical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            (env, src->hInputBuffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (src->inbuf == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if ((int)(src->inbufoffset) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            src->pub.next_input_byte = src->inbuf + src->inbufoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    if (src->hOutputBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        assert(src->outbuf.ip == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        src->outbuf.ip = (int *)(*env)->GetPrimitiveArrayCritical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            (env, src->hOutputBuffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (src->outbuf.ip == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            RELEASE_ARRAYS(env, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * Initialize source.  This is called by jpeg_read_header() before any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * data is actually read.  Unlike init_destination(), it may leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * will occur immediately).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
sun_jpeg_init_source(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    src->pub.next_input_byte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    src->pub.bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * This is called whenever bytes_in_buffer has reached zero and more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * data is wanted.  In typical applications, it should read fresh data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * into the buffer (ignoring the current state of next_input_byte and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * bytes_in_buffer), reset the pointer & count to the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * buffer, and return TRUE indicating that the buffer has been reloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * It is not necessary to fill the buffer entirely, only to obtain at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * least one more byte.  bytes_in_buffer MUST be set to a positive value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * if TRUE is returned.  A FALSE return should only be used when I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * suspension is desired (this mode is discussed in the next section).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * Note that with I/O suspension turned on, this procedure should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * do any work since the JPEG library has a very simple backtracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * mechanism which relies on the fact that the buffer will be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * only when it has backed out to the top application level.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * suspendable is turned on, the sun_jpeg_fill_suspended_buffer will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * do the actual work of filling the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
GLOBAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
sun_jpeg_fill_input_buffer(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    int ret, buflen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    if (src->suspendable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    if (src->remaining_skip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        src->pub.skip_input_data(cinfo, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    RELEASE_ARRAYS(env, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    buflen = (*env)->GetArrayLength(env, src->hInputBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    ret = (*env)->CallIntMethod(env, src->hInputStream, InputStream_readID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                src->hInputBuffer, 0, buflen);
23892
9959956918be 8029854: Enhance JPEG decodings
prr
parents: 23644
diff changeset
   292
    if (ret > buflen) ret = buflen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if ((*env)->ExceptionOccurred(env) || !GET_ARRAYS(env, src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        /* Silently accept truncated JPEG files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        WARNMS(cinfo, JWRN_JPEG_EOF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        src->inbuf[0] = (JOCTET) 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        src->inbuf[1] = (JOCTET) JPEG_EOI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        ret = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    src->pub.next_input_byte = src->inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    src->pub.bytes_in_buffer = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 * Note that with I/O suspension turned on, the JPEG library requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * that all buffer filling be done at the top application level.  Due
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * to the way that backtracking works, this procedure should save all
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 12994
diff changeset
   314
 * of the data that was left in the buffer when suspension occurred and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * only read new data at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
sun_jpeg_fill_suspended_buffer(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    size_t offset, buflen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    RELEASE_ARRAYS(env, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    ret = (*env)->CallIntMethod(env, src->hInputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                InputStream_availableID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if ((*env)->ExceptionOccurred(env) || !GET_ARRAYS(env, src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 5506
diff changeset
   332
    if (ret < 0 || (unsigned int)ret <= src->remaining_skip) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    if (src->remaining_skip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        src->pub.skip_input_data(cinfo, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /* Save the data currently in the buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    offset = src->pub.bytes_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    if (src->pub.next_input_byte > src->inbuf) {
12994
a758ccd05133 7153693: Three 2D_ImageIO tests failed due ImageFormatException on OEL 6.* Unbreakable Kernel x64
bae
parents: 9035
diff changeset
   341
        memmove(src->inbuf, src->pub.next_input_byte, offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    RELEASE_ARRAYS(env, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    buflen = (*env)->GetArrayLength(env, src->hInputBuffer) - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if (buflen <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (!GET_ARRAYS(env, src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    ret = (*env)->CallIntMethod(env, src->hInputStream, InputStream_readID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                src->hInputBuffer, offset, buflen);
23892
9959956918be 8029854: Enhance JPEG decodings
prr
parents: 23644
diff changeset
   353
    if (ret > buflen) ret = buflen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    if ((*env)->ExceptionOccurred(env) || !GET_ARRAYS(env, src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        /* Silently accept truncated JPEG files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        WARNMS(cinfo, JWRN_JPEG_EOF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        src->inbuf[offset] = (JOCTET) 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        src->inbuf[offset + 1] = (JOCTET) JPEG_EOI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        ret = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    src->pub.next_input_byte = src->inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    src->pub.bytes_in_buffer = ret + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 * Skip num_bytes worth of data.  The buffer pointer and count should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 * be advanced over num_bytes input bytes, refilling the buffer as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 * needed.  This is used to skip over a potentially large amount of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 * uninteresting data (such as an APPn marker).  In some applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * it may be possible to optimize away the reading of the skipped data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 * but it's not clear that being smart is worth much trouble; large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 * skips are uncommon.  bytes_in_buffer may be zero on return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 * A zero or negative skip count should be treated as a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
 * Note that with I/O suspension turned on, this procedure should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 * do any I/O since the JPEG library has a very simple backtracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
 * mechanism which relies on the fact that the buffer will be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 * only when it has backed out to the top application level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
sun_jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    int buflen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    if (num_bytes < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    num_bytes += src->remaining_skip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    src->remaining_skip = 0;
8355
6b58fe58e767 6989774: imageio compiler warnings in native code
bae
parents: 5506
diff changeset
   402
    ret = (int)src->pub.bytes_in_buffer; /* this conversion is safe, because capacity of the buffer is limited by jnit */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    if (ret >= num_bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        src->pub.next_input_byte += num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        src->pub.bytes_in_buffer -= num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    num_bytes -= ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    if (src->suspendable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        src->remaining_skip = num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        src->pub.bytes_in_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        src->pub.next_input_byte = src->inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /* Note that the signature for the method indicates that it takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * and returns a long.  Casting the int num_bytes to a long on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * the input should work well enough, and if we assume that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * return value for this particular method should always be less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * than the argument value (or -1), then the return value coerced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * to an int should return us the information we need...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    RELEASE_ARRAYS(env, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    buflen =  (*env)->GetArrayLength(env, src->hInputBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    while (num_bytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        ret = (*env)->CallIntMethod(env, src->hInputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                    InputStream_readID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                    src->hInputBuffer, 0, buflen);
23892
9959956918be 8029854: Enhance JPEG decodings
prr
parents: 23644
diff changeset
   429
        if (ret > buflen) ret = buflen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (ret < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        num_bytes -= ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    if (!GET_ARRAYS(env, src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    if (num_bytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        /* Silently accept truncated JPEG files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        WARNMS(cinfo, JWRN_JPEG_EOF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        src->inbuf[0] = (JOCTET) 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        src->inbuf[1] = (JOCTET) JPEG_EOI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        src->pub.bytes_in_buffer = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        src->pub.next_input_byte = src->inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        src->pub.bytes_in_buffer = -num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        src->pub.next_input_byte = src->inbuf + ret + num_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 * Terminate source --- called by jpeg_finish_decompress() after all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 * data has been read.  Often a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
sun_jpeg_term_source(j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
Java_sun_awt_image_JPEGImageDecoder_initIDs(JNIEnv *env, jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                            jclass InputStreamClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
{
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   468
    CHECK_NULL(sendHeaderInfoID = (*env)->GetMethodID(env, cls, "sendHeaderInfo",
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   469
                                           "(IIZZZ)Z"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   470
    CHECK_NULL(sendPixelsByteID = (*env)->GetMethodID(env, cls, "sendPixels", "([BI)Z"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   471
    CHECK_NULL(sendPixelsIntID = (*env)->GetMethodID(env, cls, "sendPixels", "([II)Z"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   472
    CHECK_NULL(InputStream_readID = (*env)->GetMethodID(env, InputStreamClass,
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   473
                                             "read", "([BII)I"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   474
    CHECK_NULL(InputStream_availableID = (*env)->GetMethodID(env, InputStreamClass,
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 23010
diff changeset
   475
                                                  "available", "()I"));
2
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
 * The Windows Itanium Aug 2002 SDK generates bad code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
 * for this routine.  Disable optimization for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
#ifdef _M_IA64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
#pragma optimize ("", off)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
Java_sun_awt_image_JPEGImageDecoder_readImage(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                              jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                              jobject hInputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                              jbyteArray hInputBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
  /* This struct contains the JPEG decompression parameters and pointers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
   * working space (which is allocated as needed by the JPEG library).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
  struct jpeg_decompress_struct cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
  /* We use our private extension JPEG error handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
   * Note that this struct must live as long as the main JPEG parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
   * struct, to avoid dangling-pointer problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
  struct sun_jpeg_error_mgr jerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
  struct sun_jpeg_source_mgr jsrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
  int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
  unsigned char *bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
  int *ip, pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
  int grayscale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
  int hasalpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
  int buffered_mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
  int final_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
  /* Step 0: verify the inputs. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
  if (hInputBuffer == 0 || hInputStream == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
  jsrc.outbuf.ip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
  jsrc.inbuf = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
  /* Step 1: allocate and initialize JPEG decompression object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
  /* We set up the normal JPEG error routines, then override error_exit. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
  cinfo.err = jpeg_std_error(&jerr.pub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
  jerr.pub.error_exit = sun_jpeg_error_exit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
  /* We need to setup our own print routines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
  jerr.pub.output_message = sun_jpeg_output_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
  /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
  if (setjmp(jerr.setjmp_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /* If we get here, the JPEG code has signaled an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * We need to clean up the JPEG object, close the input file, and return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    jpeg_destroy_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    RELEASE_ARRAYS(env, &jsrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        char buffer[JMSG_LENGTH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        (*cinfo.err->format_message) ((struct jpeg_common_struct *) &cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                      buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        JNU_ThrowByName(env, "sun/awt/image/ImageFormatException", buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
  /* Now we can initialize the JPEG decompression object. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
  jpeg_create_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  /* Step 2: specify data source (eg, a file) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
  cinfo.src = &jsrc.pub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
  jsrc.hInputStream = hInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
  jsrc.hInputBuffer = hInputBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
  jsrc.hOutputBuffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
  jsrc.suspendable = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
  jsrc.remaining_skip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
  jsrc.inbufoffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
  jsrc.pub.init_source = sun_jpeg_init_source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
  jsrc.pub.fill_input_buffer = sun_jpeg_fill_input_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
  jsrc.pub.skip_input_data = sun_jpeg_skip_input_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
  jsrc.pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
  jsrc.pub.term_source = sun_jpeg_term_source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
  if (!GET_ARRAYS(env, &jsrc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    jpeg_destroy_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
  /* Step 3: read file parameters with jpeg_read_header() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
  (void) jpeg_read_header(&cinfo, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
  /* select buffered-image mode if it is a progressive JPEG only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
  buffered_mode = cinfo.buffered_image = jpeg_has_multiple_scans(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
  grayscale = (cinfo.out_color_space == JCS_GRAYSCALE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
#ifdef YCCALPHA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
  hasalpha = (cinfo.out_color_space == JCS_RGBA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
  hasalpha = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
  /* We can ignore the return value from jpeg_read_header since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
   *   (a) suspension is not possible with the stdio data source, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
   *                                    (nor with the Java input source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
   * See libjpeg.doc for more info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
  RELEASE_ARRAYS(env, &jsrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
  ret = (*env)->CallBooleanMethod(env, this, sendHeaderInfoID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                  cinfo.image_width, cinfo.image_height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                  grayscale, hasalpha, buffered_mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
  if ((*env)->ExceptionOccurred(env) || !ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /* No more interest in this image... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    jpeg_destroy_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
  /* Make a one-row-high sample array with enough room to expand to ints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
  if (grayscale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
      jsrc.hOutputBuffer = (*env)->NewByteArray(env, cinfo.image_width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
      jsrc.hOutputBuffer = (*env)->NewIntArray(env, cinfo.image_width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
  if (jsrc.hOutputBuffer == 0 || !GET_ARRAYS(env, &jsrc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    jpeg_destroy_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
  /* Step 4: set parameters for decompression */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
  /* In this example, we don't need to change any of the defaults set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
   * jpeg_read_header(), so we do nothing here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
  /* For the first pass for Java, we want to deal with RGB for simplicity */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
  /* Unfortunately, the JPEG code does not automatically convert Grayscale */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
  /* to RGB, so we have to deal with Grayscale explicitly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
  if (!grayscale && !hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
      cinfo.out_color_space = JCS_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
  /* Step 5: Start decompressor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
  jpeg_start_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
  /* We may need to do some setup of our own at this point before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
   * the data.  After jpeg_start_decompress() we have the correct scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
   * output image dimensions available, as well as the output colormap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
   * if we asked for color quantization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
  /* Step 6: while (scan lines remain to be read) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
  /*           jpeg_read_scanlines(...); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
  /* Here we use the library's state variable cinfo.output_scanline as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
   * loop counter, so that we don't have to keep track ourselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
  if (buffered_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
      final_pass = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
      cinfo.dct_method = JDCT_IFAST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
      final_pass = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
  do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
      if (buffered_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
          do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
              sun_jpeg_fill_suspended_buffer(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
              jsrc.suspendable = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
              ret = jpeg_consume_input(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
              jsrc.suspendable = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
          } while (ret != JPEG_SUSPENDED && ret != JPEG_REACHED_EOI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
          if (ret == JPEG_REACHED_EOI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
              final_pass = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
              cinfo.dct_method = JDCT_ISLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
          jpeg_start_output(&cinfo, cinfo.input_scan_number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
      while (cinfo.output_scanline < cinfo.output_height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
          if (! final_pass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
              do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                  sun_jpeg_fill_suspended_buffer(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                  jsrc.suspendable = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                  ret = jpeg_consume_input(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                  jsrc.suspendable = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
              } while (ret != JPEG_SUSPENDED && ret != JPEG_REACHED_EOI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
              if (ret == JPEG_REACHED_EOI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
          (void) jpeg_read_scanlines(&cinfo, (JSAMPARRAY) &(jsrc.outbuf), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
          if (grayscale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
              RELEASE_ARRAYS(env, &jsrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
              ret = (*env)->CallBooleanMethod(env, this, sendPixelsByteID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                                              jsrc.hOutputBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                                              cinfo.output_scanline - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
              if (hasalpha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                  ip = jsrc.outbuf.ip + cinfo.image_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                  bp = jsrc.outbuf.bp + cinfo.image_width * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                  while (ip > jsrc.outbuf.ip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                      pixel = (*--bp) << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                      pixel |= (*--bp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                      pixel |= (*--bp) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                      pixel |= (*--bp) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                      *--ip = pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                  ip = jsrc.outbuf.ip + cinfo.image_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                  bp = jsrc.outbuf.bp + cinfo.image_width * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                  while (ip > jsrc.outbuf.ip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                      pixel = (*--bp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                      pixel |= (*--bp) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                      pixel |= (*--bp) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                      *--ip = pixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
              RELEASE_ARRAYS(env, &jsrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
              ret = (*env)->CallBooleanMethod(env, this, sendPixelsIntID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                              jsrc.hOutputBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                              cinfo.output_scanline - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
          if ((*env)->ExceptionOccurred(env) || !ret ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
              !GET_ARRAYS(env, &jsrc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
              /* No more interest in this image... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
              jpeg_destroy_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
      if (buffered_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
          jpeg_finish_output(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
  } while (! final_pass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
  /* Step 7: Finish decompression */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
  (void) jpeg_finish_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
  /* We can ignore the return value since suspension is not possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
   * with the stdio data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
   * (nor with the Java data source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
  /* Step 8: Release JPEG decompression object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
  /* This is an important step since it will release a good deal of memory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
  jpeg_destroy_decompress(&cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
  /* After finish_decompress, we can close the input file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
   * Here we postpone it until after no more JPEG errors are possible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
   * so as to simplify the setjmp error logic above.  (Actually, I don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
   * think that jpeg_destroy can do an error exit, but why assume anything...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
  /* Not needed for Java - the Java code will close the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
  /* fclose(infile); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
  /* At this point you may want to check to see whether any corrupt-data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
   * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
  /* And we're done! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
  RELEASE_ARRAYS(env, &jsrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
#ifdef _M_IA64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
#pragma optimize ("", on)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
 * SOME FINE POINTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
 * In the above code, we ignored the return value of jpeg_read_scanlines,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
 * which is the number of scanlines actually read.  We could get away with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
 * this because we asked for only one line at a time and we weren't using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
 * a suspending data source.  See libjpeg.doc for more info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
 * we should have done it beforehand to ensure that the space would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
 * counted against the JPEG max_memory setting.  In some systems the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
 * code would risk an out-of-memory error.  However, in general we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 * know the output image dimensions before jpeg_start_decompress(), unless we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
 * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
 * Scanlines are returned in the same order as they appear in the JPEG file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
 * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
 * you can use one of the virtual arrays provided by the JPEG memory manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
 * to invert the data.  See wrbmp.c for an example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
 * As with compression, some operating modes may require temporary files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
 * On some systems you may need to set up a signal handler to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
 * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
 */