0N/A/*
3909N/A * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file was based upon the example.c stub file included in the
0N/A * release 6 of the Independent JPEG Group's free JPEG software.
0N/A * It has been updated to conform to release 6b.
0N/A */
0N/A
0N/A/* First, if system header files define "boolean" map it to "system_boolean" */
0N/A#define boolean system_boolean
0N/A
0N/A#include <stdio.h>
0N/A#include <setjmp.h>
0N/A#include <string.h>
0N/A#include <stdlib.h>
0N/A#include <assert.h>
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A
0N/A/* undo "system_boolean" hack and undef FAR since we don't use it anyway */
0N/A#undef boolean
0N/A#undef FAR
0N/A#include <jpeglib.h>
0N/A#include "jerror.h"
0N/A
0N/A/* The method IDs we cache. Note that the last two belongs to the
0N/A * java.io.InputStream class.
0N/A */
0N/Astatic jmethodID sendHeaderInfoID;
0N/Astatic jmethodID sendPixelsByteID;
0N/Astatic jmethodID sendPixelsIntID;
0N/Astatic jmethodID InputStream_readID;
0N/Astatic jmethodID InputStream_availableID;
0N/A
0N/A/* Initialize the Java VM instance variable when the library is
0N/A first loaded */
0N/AJavaVM *jvm;
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJNI_OnLoad(JavaVM *vm, void *reserved)
0N/A{
0N/A jvm = vm;
0N/A return JNI_VERSION_1_2;
0N/A}
0N/A
0N/A/*
0N/A * ERROR HANDLING:
0N/A *
0N/A * The JPEG library's standard error handler (jerror.c) is divided into
0N/A * several "methods" which you can override individually. This lets you
0N/A * adjust the behavior without duplicating a lot of code, which you might
0N/A * have to update with each future release.
0N/A *
0N/A * Our example here shows how to override the "error_exit" method so that
0N/A * control is returned to the library's caller when a fatal error occurs,
0N/A * rather than calling exit() as the standard error_exit method does.
0N/A *
0N/A * We use C's setjmp/longjmp facility to return control. This means that the
0N/A * routine which calls the JPEG library must first execute a setjmp() call to
0N/A * establish the return point. We want the replacement error_exit to do a
0N/A * longjmp(). But we need to make the setjmp buffer accessible to the
0N/A * error_exit routine. To do this, we make a private extension of the
0N/A * standard JPEG error handler object. (If we were using C++, we'd say we
0N/A * were making a subclass of the regular error handler.)
0N/A *
0N/A * Here's the extended error handler struct:
0N/A */
0N/A
0N/Astruct sun_jpeg_error_mgr {
0N/A struct jpeg_error_mgr pub; /* "public" fields */
0N/A
0N/A jmp_buf setjmp_buffer; /* for return to caller */
0N/A};
0N/A
0N/Atypedef struct sun_jpeg_error_mgr * sun_jpeg_error_ptr;
0N/A
0N/A/*
0N/A * Here's the routine that will replace the standard error_exit method:
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Asun_jpeg_error_exit (j_common_ptr cinfo)
0N/A{
0N/A /* cinfo->err really points to a sun_jpeg_error_mgr struct */
0N/A sun_jpeg_error_ptr myerr = (sun_jpeg_error_ptr) cinfo->err;
0N/A
0N/A /* Always display the message. */
0N/A /* We could postpone this until after returning, if we chose. */
0N/A /* (*cinfo->err->output_message) (cinfo); */
0N/A /* For Java, we will format the message and put it in the error we throw. */
0N/A
0N/A /* Return control to the setjmp point */
0N/A longjmp(myerr->setjmp_buffer, 1);
0N/A}
0N/A
0N/A/*
0N/A * Error Message handling
0N/A *
0N/A * This overrides the output_message method to send JPEG messages
0N/A *
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Asun_jpeg_output_message (j_common_ptr cinfo)
0N/A{
0N/A char buffer[JMSG_LENGTH_MAX];
0N/A
0N/A /* Create the message */
0N/A (*cinfo->err->format_message) (cinfo, buffer);
0N/A
0N/A /* Send it to stderr, adding a newline */
0N/A fprintf(stderr, "%s\n", buffer);
0N/A}
0N/A
0N/A
0N/A
0N/A
0N/A/*
0N/A * INPUT HANDLING:
0N/A *
0N/A * The JPEG library's input management is defined by the jpeg_source_mgr
0N/A * structure which contains two fields to convey the information in the
0N/A * buffer and 5 methods which perform all buffer management. The library
0N/A * defines a standard input manager that uses stdio for obtaining compressed
0N/A * jpeg data, but here we need to use Java to get our data.
0N/A *
0N/A * We need to make the Java class information accessible to the source_mgr
0N/A * input routines. We also need to store a pointer to the start of the
0N/A * Java array being used as an input buffer so that it is not moved or
0N/A * garbage collected while the JPEG library is using it. To store these
0N/A * things, we make a private extension of the standard JPEG jpeg_source_mgr
0N/A * object.
0N/A *
0N/A * Here's the extended source manager struct:
0N/A */
0N/A
0N/Astruct sun_jpeg_source_mgr {
0N/A struct jpeg_source_mgr pub; /* "public" fields */
0N/A
0N/A jobject hInputStream;
0N/A int suspendable;
0N/A unsigned long remaining_skip;
0N/A
0N/A JOCTET *inbuf;
0N/A jbyteArray hInputBuffer;
0N/A size_t inbufoffset;
0N/A
0N/A /* More stuff */
0N/A union pixptr {
0N/A int *ip;
0N/A unsigned char *bp;
0N/A } outbuf;
0N/A jobject hOutputBuffer;
0N/A};
0N/A
0N/Atypedef struct sun_jpeg_source_mgr * sun_jpeg_source_ptr;
0N/A
0N/A/* We use Get/ReleasePrimitiveArrayCritical functions to avoid
0N/A * the need to copy buffer elements.
0N/A *
0N/A * MAKE SURE TO:
0N/A *
0N/A * - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around
0N/A * callbacks to Java.
0N/A * - call RELEASE_ARRAYS before returning to Java.
0N/A *
0N/A * Otherwise things will go horribly wrong. There may be memory leaks,
0N/A * excessive pinning, or even VM crashes!
0N/A *
0N/A * Note that GetPrimitiveArrayCritical may fail!
0N/A */
0N/Astatic void RELEASE_ARRAYS(JNIEnv *env, sun_jpeg_source_ptr src)
0N/A{
0N/A if (src->inbuf) {
0N/A if (src->pub.next_input_byte == 0) {
0N/A src->inbufoffset = -1;
0N/A } else {
0N/A src->inbufoffset = src->pub.next_input_byte - src->inbuf;
0N/A }
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src->hInputBuffer,
0N/A src->inbuf, 0);
0N/A src->inbuf = 0;
0N/A }
0N/A if (src->outbuf.ip) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src->hOutputBuffer,
0N/A src->outbuf.ip, 0);
0N/A src->outbuf.ip = 0;
0N/A }
0N/A}
0N/A
0N/Astatic int GET_ARRAYS(JNIEnv *env, sun_jpeg_source_ptr src)
0N/A{
0N/A if (src->hInputBuffer) {
0N/A assert(src->inbuf == 0);
0N/A src->inbuf = (JOCTET *)(*env)->GetPrimitiveArrayCritical
0N/A (env, src->hInputBuffer, 0);
0N/A if (src->inbuf == 0) {
0N/A return 0;
0N/A }
0N/A if ((int)(src->inbufoffset) >= 0) {
0N/A src->pub.next_input_byte = src->inbuf + src->inbufoffset;
0N/A }
0N/A }
0N/A if (src->hOutputBuffer) {
0N/A assert(src->outbuf.ip == 0);
0N/A src->outbuf.ip = (int *)(*env)->GetPrimitiveArrayCritical
0N/A (env, src->hOutputBuffer, 0);
0N/A if (src->outbuf.ip == 0) {
0N/A RELEASE_ARRAYS(env, src);
0N/A return 0;
0N/A }
0N/A }
0N/A return 1;
0N/A}
0N/A
0N/A/*
0N/A * Initialize source. This is called by jpeg_read_header() before any
0N/A * data is actually read. Unlike init_destination(), it may leave
0N/A * bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
0N/A * will occur immediately).
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Asun_jpeg_init_source(j_decompress_ptr cinfo)
0N/A{
0N/A sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
0N/A src->pub.next_input_byte = 0;
0N/A src->pub.bytes_in_buffer = 0;
0N/A}
0N/A
0N/A/*
0N/A * This is called whenever bytes_in_buffer has reached zero and more
0N/A * data is wanted. In typical applications, it should read fresh data
0N/A * into the buffer (ignoring the current state of next_input_byte and
0N/A * bytes_in_buffer), reset the pointer & count to the start of the
0N/A * buffer, and return TRUE indicating that the buffer has been reloaded.
0N/A * It is not necessary to fill the buffer entirely, only to obtain at
0N/A * least one more byte. bytes_in_buffer MUST be set to a positive value
0N/A * if TRUE is returned. A FALSE return should only be used when I/O
0N/A * suspension is desired (this mode is discussed in the next section).
0N/A */
0N/A/*
0N/A * Note that with I/O suspension turned on, this procedure should not
0N/A * do any work since the JPEG library has a very simple backtracking
0N/A * mechanism which relies on the fact that the buffer will be filled
0N/A * only when it has backed out to the top application level. When
0N/A * suspendable is turned on, the sun_jpeg_fill_suspended_buffer will
0N/A * do the actual work of filling the buffer.
0N/A */
0N/A
0N/AGLOBAL(boolean)
0N/Asun_jpeg_fill_input_buffer(j_decompress_ptr cinfo)
0N/A{
0N/A sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
0N/A JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
0N/A int ret, buflen;
0N/A
0N/A if (src->suspendable) {
0N/A return FALSE;
0N/A }
0N/A if (src->remaining_skip) {
0N/A src->pub.skip_input_data(cinfo, 0);
0N/A }
0N/A RELEASE_ARRAYS(env, src);
0N/A buflen = (*env)->GetArrayLength(env, src->hInputBuffer);
0N/A ret = (*env)->CallIntMethod(env, src->hInputStream, InputStream_readID,
0N/A src->hInputBuffer, 0, buflen);
0N/A if ((*env)->ExceptionOccurred(env) || !GET_ARRAYS(env, src)) {
0N/A cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
0N/A }
0N/A if (ret <= 0) {
0N/A /* Silently accept truncated JPEG files */
0N/A WARNMS(cinfo, JWRN_JPEG_EOF);
0N/A src->inbuf[0] = (JOCTET) 0xFF;
0N/A src->inbuf[1] = (JOCTET) JPEG_EOI;
0N/A ret = 2;
0N/A }
0N/A
0N/A src->pub.next_input_byte = src->inbuf;
0N/A src->pub.bytes_in_buffer = ret;
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A/*
0N/A * Note that with I/O suspension turned on, the JPEG library requires
0N/A * that all buffer filling be done at the top application level. Due
0N/A * to the way that backtracking works, this procedure should save all
0N/A * of the data that was left in the buffer when suspension occured and
0N/A * only read new data at the end.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Asun_jpeg_fill_suspended_buffer(j_decompress_ptr cinfo)
0N/A{
0N/A sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
0N/A JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
0N/A size_t offset, buflen;
0N/A int ret;
0N/A
0N/A RELEASE_ARRAYS(env, src);
0N/A ret = (*env)->CallIntMethod(env, src->hInputStream,
0N/A InputStream_availableID);
0N/A if ((*env)->ExceptionOccurred(env) || !GET_ARRAYS(env, src)) {
0N/A cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
0N/A }
3538N/A if (ret < 0 || (unsigned int)ret <= src->remaining_skip) {
0N/A return;
0N/A }
0N/A if (src->remaining_skip) {
0N/A src->pub.skip_input_data(cinfo, 0);
0N/A }
0N/A /* Save the data currently in the buffer */
0N/A offset = src->pub.bytes_in_buffer;
0N/A if (src->pub.next_input_byte > src->inbuf) {
5127N/A memmove(src->inbuf, src->pub.next_input_byte, offset);
0N/A }
0N/A RELEASE_ARRAYS(env, src);
0N/A buflen = (*env)->GetArrayLength(env, src->hInputBuffer) - offset;
0N/A if (buflen <= 0) {
0N/A if (!GET_ARRAYS(env, src)) {
0N/A cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
0N/A }
0N/A return;
0N/A }
0N/A ret = (*env)->CallIntMethod(env, src->hInputStream, InputStream_readID,
0N/A src->hInputBuffer, offset, buflen);
0N/A if ((*env)->ExceptionOccurred(env) || !GET_ARRAYS(env, src)) {
0N/A cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
0N/A }
0N/A if (ret <= 0) {
0N/A /* Silently accept truncated JPEG files */
0N/A WARNMS(cinfo, JWRN_JPEG_EOF);
0N/A src->inbuf[offset] = (JOCTET) 0xFF;
0N/A src->inbuf[offset + 1] = (JOCTET) JPEG_EOI;
0N/A ret = 2;
0N/A }
0N/A
0N/A src->pub.next_input_byte = src->inbuf;
0N/A src->pub.bytes_in_buffer = ret + offset;
0N/A
0N/A return;
0N/A}
0N/A
0N/A/*
0N/A * Skip num_bytes worth of data. The buffer pointer and count should
0N/A * be advanced over num_bytes input bytes, refilling the buffer as
0N/A * needed. This is used to skip over a potentially large amount of
0N/A * uninteresting data (such as an APPn marker). In some applications
0N/A * it may be possible to optimize away the reading of the skipped data,
0N/A * but it's not clear that being smart is worth much trouble; large
0N/A * skips are uncommon. bytes_in_buffer may be zero on return.
0N/A * A zero or negative skip count should be treated as a no-op.
0N/A */
0N/A/*
0N/A * Note that with I/O suspension turned on, this procedure should not
0N/A * do any I/O since the JPEG library has a very simple backtracking
0N/A * mechanism which relies on the fact that the buffer will be filled
0N/A * only when it has backed out to the top application level.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Asun_jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
0N/A{
0N/A sun_jpeg_source_ptr src = (sun_jpeg_source_ptr) cinfo->src;
0N/A JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
0N/A int ret;
0N/A int buflen;
0N/A
0N/A
0N/A if (num_bytes < 0) {
0N/A return;
0N/A }
0N/A num_bytes += src->remaining_skip;
0N/A src->remaining_skip = 0;
3538N/A ret = (int)src->pub.bytes_in_buffer; /* this conversion is safe, because capacity of the buffer is limited by jnit */
0N/A if (ret >= num_bytes) {
0N/A src->pub.next_input_byte += num_bytes;
0N/A src->pub.bytes_in_buffer -= num_bytes;
0N/A return;
0N/A }
0N/A num_bytes -= ret;
0N/A if (src->suspendable) {
0N/A src->remaining_skip = num_bytes;
0N/A src->pub.bytes_in_buffer = 0;
0N/A src->pub.next_input_byte = src->inbuf;
0N/A return;
0N/A }
0N/A
0N/A /* Note that the signature for the method indicates that it takes
0N/A * and returns a long. Casting the int num_bytes to a long on
0N/A * the input should work well enough, and if we assume that the
0N/A * return value for this particular method should always be less
0N/A * than the argument value (or -1), then the return value coerced
0N/A * to an int should return us the information we need...
0N/A */
0N/A RELEASE_ARRAYS(env, src);
0N/A buflen = (*env)->GetArrayLength(env, src->hInputBuffer);
0N/A while (num_bytes > 0) {
0N/A ret = (*env)->CallIntMethod(env, src->hInputStream,
0N/A InputStream_readID,
0N/A src->hInputBuffer, 0, buflen);
0N/A if ((*env)->ExceptionOccurred(env)) {
0N/A cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
0N/A }
0N/A if (ret < 0) {
0N/A break;
0N/A }
0N/A num_bytes -= ret;
0N/A }
0N/A if (!GET_ARRAYS(env, src)) {
0N/A cinfo->err->error_exit((struct jpeg_common_struct *) cinfo);
0N/A }
0N/A if (num_bytes > 0) {
0N/A /* Silently accept truncated JPEG files */
0N/A WARNMS(cinfo, JWRN_JPEG_EOF);
0N/A src->inbuf[0] = (JOCTET) 0xFF;
0N/A src->inbuf[1] = (JOCTET) JPEG_EOI;
0N/A src->pub.bytes_in_buffer = 2;
0N/A src->pub.next_input_byte = src->inbuf;
0N/A } else {
0N/A src->pub.bytes_in_buffer = -num_bytes;
0N/A src->pub.next_input_byte = src->inbuf + ret + num_bytes;
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Terminate source --- called by jpeg_finish_decompress() after all
0N/A * data has been read. Often a no-op.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Asun_jpeg_term_source(j_decompress_ptr cinfo)
0N/A{
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_image_JPEGImageDecoder_initIDs(JNIEnv *env, jclass cls,
0N/A jclass InputStreamClass)
0N/A{
0N/A sendHeaderInfoID = (*env)->GetMethodID(env, cls, "sendHeaderInfo",
0N/A "(IIZZZ)Z");
0N/A sendPixelsByteID = (*env)->GetMethodID(env, cls, "sendPixels", "([BI)Z");
0N/A sendPixelsIntID = (*env)->GetMethodID(env, cls, "sendPixels", "([II)Z");
0N/A InputStream_readID = (*env)->GetMethodID(env, InputStreamClass,
0N/A "read", "([BII)I");
0N/A InputStream_availableID = (*env)->GetMethodID(env, InputStreamClass,
0N/A "available", "()I");
0N/A}
0N/A
0N/A
0N/A/*
0N/A * The Windows Itanium Aug 2002 SDK generates bad code
0N/A * for this routine. Disable optimization for now.
0N/A */
0N/A#ifdef _M_IA64
0N/A#pragma optimize ("", off)
0N/A#endif
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_image_JPEGImageDecoder_readImage(JNIEnv *env,
0N/A jobject this,
0N/A jobject hInputStream,
0N/A jbyteArray hInputBuffer)
0N/A{
0N/A /* This struct contains the JPEG decompression parameters and pointers to
0N/A * working space (which is allocated as needed by the JPEG library).
0N/A */
0N/A struct jpeg_decompress_struct cinfo;
0N/A /* We use our private extension JPEG error handler.
0N/A * Note that this struct must live as long as the main JPEG parameter
0N/A * struct, to avoid dangling-pointer problems.
0N/A */
0N/A struct sun_jpeg_error_mgr jerr;
0N/A struct sun_jpeg_source_mgr jsrc;
0N/A
0N/A int ret;
0N/A unsigned char *bp;
0N/A int *ip, pixel;
0N/A int grayscale;
0N/A int hasalpha;
0N/A int buffered_mode;
0N/A int final_pass;
0N/A
0N/A /* Step 0: verify the inputs. */
0N/A
0N/A if (hInputBuffer == 0 || hInputStream == 0) {
0N/A JNU_ThrowNullPointerException(env, 0);
0N/A return;
0N/A }
0N/A
0N/A jsrc.outbuf.ip = 0;
0N/A jsrc.inbuf = 0;
0N/A
0N/A /* Step 1: allocate and initialize JPEG decompression object */
0N/A
0N/A /* We set up the normal JPEG error routines, then override error_exit. */
0N/A cinfo.err = jpeg_std_error(&jerr.pub);
0N/A jerr.pub.error_exit = sun_jpeg_error_exit;
0N/A
0N/A /* We need to setup our own print routines */
0N/A jerr.pub.output_message = sun_jpeg_output_message;
0N/A
0N/A /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
0N/A if (setjmp(jerr.setjmp_buffer)) {
0N/A /* If we get here, the JPEG code has signaled an error.
0N/A * We need to clean up the JPEG object, close the input file, and return.
0N/A */
0N/A jpeg_destroy_decompress(&cinfo);
0N/A RELEASE_ARRAYS(env, &jsrc);
0N/A if (!(*env)->ExceptionOccurred(env)) {
0N/A char buffer[JMSG_LENGTH_MAX];
0N/A (*cinfo.err->format_message) ((struct jpeg_common_struct *) &cinfo,
0N/A buffer);
0N/A JNU_ThrowByName(env, "sun/awt/image/ImageFormatException", buffer);
0N/A }
0N/A return;
0N/A }
0N/A /* Now we can initialize the JPEG decompression object. */
0N/A jpeg_create_decompress(&cinfo);
0N/A
0N/A /* Step 2: specify data source (eg, a file) */
0N/A
0N/A cinfo.src = &jsrc.pub;
0N/A jsrc.hInputStream = hInputStream;
0N/A jsrc.hInputBuffer = hInputBuffer;
0N/A jsrc.hOutputBuffer = 0;
0N/A jsrc.suspendable = FALSE;
0N/A jsrc.remaining_skip = 0;
0N/A jsrc.inbufoffset = -1;
0N/A jsrc.pub.init_source = sun_jpeg_init_source;
0N/A jsrc.pub.fill_input_buffer = sun_jpeg_fill_input_buffer;
0N/A jsrc.pub.skip_input_data = sun_jpeg_skip_input_data;
0N/A jsrc.pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
0N/A jsrc.pub.term_source = sun_jpeg_term_source;
0N/A if (!GET_ARRAYS(env, &jsrc)) {
0N/A jpeg_destroy_decompress(&cinfo);
0N/A return;
0N/A }
0N/A /* Step 3: read file parameters with jpeg_read_header() */
0N/A
0N/A (void) jpeg_read_header(&cinfo, TRUE);
0N/A /* select buffered-image mode if it is a progressive JPEG only */
0N/A buffered_mode = cinfo.buffered_image = jpeg_has_multiple_scans(&cinfo);
0N/A grayscale = (cinfo.out_color_space == JCS_GRAYSCALE);
0N/A#ifdef YCCALPHA
0N/A hasalpha = (cinfo.out_color_space == JCS_RGBA);
0N/A#else
0N/A hasalpha = 0;
0N/A#endif
0N/A /* We can ignore the return value from jpeg_read_header since
0N/A * (a) suspension is not possible with the stdio data source, and
0N/A * (nor with the Java input source)
0N/A * (b) we passed TRUE to reject a tables-only JPEG file as an error.
0N/A * See libjpeg.doc for more info.
0N/A */
0N/A RELEASE_ARRAYS(env, &jsrc);
0N/A ret = (*env)->CallBooleanMethod(env, this, sendHeaderInfoID,
0N/A cinfo.image_width, cinfo.image_height,
0N/A grayscale, hasalpha, buffered_mode);
0N/A if ((*env)->ExceptionOccurred(env) || !ret) {
0N/A /* No more interest in this image... */
0N/A jpeg_destroy_decompress(&cinfo);
0N/A return;
0N/A }
0N/A /* Make a one-row-high sample array with enough room to expand to ints */
0N/A if (grayscale) {
0N/A jsrc.hOutputBuffer = (*env)->NewByteArray(env, cinfo.image_width);
0N/A } else {
0N/A jsrc.hOutputBuffer = (*env)->NewIntArray(env, cinfo.image_width);
0N/A }
0N/A
0N/A if (jsrc.hOutputBuffer == 0 || !GET_ARRAYS(env, &jsrc)) {
0N/A jpeg_destroy_decompress(&cinfo);
0N/A return;
0N/A }
0N/A
0N/A /* Step 4: set parameters for decompression */
0N/A
0N/A /* In this example, we don't need to change any of the defaults set by
0N/A * jpeg_read_header(), so we do nothing here.
0N/A */
0N/A /* For the first pass for Java, we want to deal with RGB for simplicity */
0N/A /* Unfortunately, the JPEG code does not automatically convert Grayscale */
0N/A /* to RGB, so we have to deal with Grayscale explicitly. */
0N/A if (!grayscale && !hasalpha) {
0N/A cinfo.out_color_space = JCS_RGB;
0N/A }
0N/A
0N/A /* Step 5: Start decompressor */
0N/A
0N/A jpeg_start_decompress(&cinfo);
0N/A
0N/A /* We may need to do some setup of our own at this point before reading
0N/A * the data. After jpeg_start_decompress() we have the correct scaled
0N/A * output image dimensions available, as well as the output colormap
0N/A * if we asked for color quantization.
0N/A */
0N/A
0N/A /* Step 6: while (scan lines remain to be read) */
0N/A /* jpeg_read_scanlines(...); */
0N/A
0N/A /* Here we use the library's state variable cinfo.output_scanline as the
0N/A * loop counter, so that we don't have to keep track ourselves.
0N/A */
0N/A if (buffered_mode) {
0N/A final_pass = FALSE;
0N/A cinfo.dct_method = JDCT_IFAST;
0N/A } else {
0N/A final_pass = TRUE;
0N/A }
0N/A do {
0N/A if (buffered_mode) {
0N/A do {
0N/A sun_jpeg_fill_suspended_buffer(&cinfo);
0N/A jsrc.suspendable = TRUE;
0N/A ret = jpeg_consume_input(&cinfo);
0N/A jsrc.suspendable = FALSE;
0N/A } while (ret != JPEG_SUSPENDED && ret != JPEG_REACHED_EOI);
0N/A if (ret == JPEG_REACHED_EOI) {
0N/A final_pass = TRUE;
0N/A cinfo.dct_method = JDCT_ISLOW;
0N/A }
0N/A jpeg_start_output(&cinfo, cinfo.input_scan_number);
0N/A }
0N/A while (cinfo.output_scanline < cinfo.output_height) {
0N/A if (! final_pass) {
0N/A do {
0N/A sun_jpeg_fill_suspended_buffer(&cinfo);
0N/A jsrc.suspendable = TRUE;
0N/A ret = jpeg_consume_input(&cinfo);
0N/A jsrc.suspendable = FALSE;
0N/A } while (ret != JPEG_SUSPENDED && ret != JPEG_REACHED_EOI);
0N/A if (ret == JPEG_REACHED_EOI) {
0N/A break;
0N/A }
0N/A }
0N/A (void) jpeg_read_scanlines(&cinfo, (JSAMPARRAY) &(jsrc.outbuf), 1);
0N/A
0N/A if (grayscale) {
0N/A RELEASE_ARRAYS(env, &jsrc);
0N/A ret = (*env)->CallBooleanMethod(env, this, sendPixelsByteID,
0N/A jsrc.hOutputBuffer,
0N/A cinfo.output_scanline - 1);
0N/A } else {
0N/A if (hasalpha) {
0N/A ip = jsrc.outbuf.ip + cinfo.image_width;
0N/A bp = jsrc.outbuf.bp + cinfo.image_width * 4;
0N/A while (ip > jsrc.outbuf.ip) {
0N/A pixel = (*--bp) << 24;
0N/A pixel |= (*--bp);
0N/A pixel |= (*--bp) << 8;
0N/A pixel |= (*--bp) << 16;
0N/A *--ip = pixel;
0N/A }
0N/A } else {
0N/A ip = jsrc.outbuf.ip + cinfo.image_width;
0N/A bp = jsrc.outbuf.bp + cinfo.image_width * 3;
0N/A while (ip > jsrc.outbuf.ip) {
0N/A pixel = (*--bp);
0N/A pixel |= (*--bp) << 8;
0N/A pixel |= (*--bp) << 16;
0N/A *--ip = pixel;
0N/A }
0N/A }
0N/A RELEASE_ARRAYS(env, &jsrc);
0N/A ret = (*env)->CallBooleanMethod(env, this, sendPixelsIntID,
0N/A jsrc.hOutputBuffer,
0N/A cinfo.output_scanline - 1);
0N/A }
0N/A if ((*env)->ExceptionOccurred(env) || !ret ||
0N/A !GET_ARRAYS(env, &jsrc)) {
0N/A /* No more interest in this image... */
0N/A jpeg_destroy_decompress(&cinfo);
0N/A return;
0N/A }
0N/A }
0N/A if (buffered_mode) {
0N/A jpeg_finish_output(&cinfo);
0N/A }
0N/A } while (! final_pass);
0N/A
0N/A /* Step 7: Finish decompression */
0N/A
0N/A (void) jpeg_finish_decompress(&cinfo);
0N/A /* We can ignore the return value since suspension is not possible
0N/A * with the stdio data source.
0N/A * (nor with the Java data source)
0N/A */
0N/A
0N/A /* Step 8: Release JPEG decompression object */
0N/A
0N/A /* This is an important step since it will release a good deal of memory. */
0N/A jpeg_destroy_decompress(&cinfo);
0N/A
0N/A /* After finish_decompress, we can close the input file.
0N/A * Here we postpone it until after no more JPEG errors are possible,
0N/A * so as to simplify the setjmp error logic above. (Actually, I don't
0N/A * think that jpeg_destroy can do an error exit, but why assume anything...)
0N/A */
0N/A /* Not needed for Java - the Java code will close the file */
0N/A /* fclose(infile); */
0N/A
0N/A /* At this point you may want to check to see whether any corrupt-data
0N/A * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
0N/A */
0N/A
0N/A /* And we're done! */
0N/A
0N/A RELEASE_ARRAYS(env, &jsrc);
0N/A return;
0N/A}
0N/A#ifdef _M_IA64
0N/A#pragma optimize ("", on)
0N/A#endif
0N/A
0N/A
0N/A/*
0N/A * SOME FINE POINTS:
0N/A *
0N/A * In the above code, we ignored the return value of jpeg_read_scanlines,
0N/A * which is the number of scanlines actually read. We could get away with
0N/A * this because we asked for only one line at a time and we weren't using
0N/A * a suspending data source. See libjpeg.doc for more info.
0N/A *
0N/A * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
0N/A * we should have done it beforehand to ensure that the space would be
0N/A * counted against the JPEG max_memory setting. In some systems the above
0N/A * code would risk an out-of-memory error. However, in general we don't
0N/A * know the output image dimensions before jpeg_start_decompress(), unless we
0N/A * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
0N/A *
0N/A * Scanlines are returned in the same order as they appear in the JPEG file,
0N/A * which is standardly top-to-bottom. If you must emit data bottom-to-top,
0N/A * you can use one of the virtual arrays provided by the JPEG memory manager
0N/A * to invert the data. See wrbmp.c for an example.
0N/A *
0N/A * As with compression, some operating modes may require temporary files.
0N/A * On some systems you may need to set up a signal handler to ensure that
0N/A * temporary files are deleted if the program is interrupted. See libjpeg.doc.
0N/A */