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 * 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 * 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 * 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. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 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/* First, if system header files define "boolean" map it to "system_boolean" */ 0N/A/* undo "system_boolean" hack and undef FAR since we don't use it anyway */ 0N/A/* The method IDs we cache. Note that the last two belongs to the 0N/A * java.io.InputStream class. 0N/A/* Initialize the Java VM instance variable when the library is 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 * 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 * 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 * Here's the extended error handler struct: 0N/A * Here's the routine that will replace the standard error_exit method: 0N/A /* cinfo->err really points to a sun_jpeg_error_mgr struct */ 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 /* Return control to the setjmp point */ 0N/A * Error Message handling 0N/A * This overrides the output_message method to send JPEG messages 0N/A /* Create the message */ 0N/A /* Send it to stderr, adding a newline */ 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 * 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 * Here's the extended source manager struct: 0N/A * the need to copy buffer elements. 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 * Otherwise things will go horribly wrong. There may be memory leaks, 0N/A * excessive pinning, or even VM crashes! 0N/A * Note that GetPrimitiveArrayCritical may fail! 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 * 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 * 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 /* Silently accept truncated JPEG files */ 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 /* Save the data currently in the buffer */ 0N/A /* Silently accept truncated JPEG files */ 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 * 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 /* 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 /* Silently accept truncated JPEG files */ 0N/A * Terminate source --- called by jpeg_finish_decompress() after all 0N/A * data has been read. Often a no-op. 0N/A "available",
"()I");
0N/A * The Windows Itanium Aug 2002 SDK generates bad code 0N/A * for this routine. Disable optimization for now. 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 /* 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 /* Step 0: verify the inputs. */ 0N/A /* Step 1: allocate and initialize JPEG decompression object */ 0N/A /* We set up the normal JPEG error routines, then override error_exit. */ 0N/A /* We need to setup our own print routines */ 0N/A /* Establish the setjmp return context for sun_jpeg_error_exit to use. */ 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 /* Now we can initialize the JPEG decompression object. */ 0N/A /* Step 2: specify data source (eg, a file) */ 0N/A /* Step 3: read file parameters with jpeg_read_header() */ 0N/A /* select buffered-image mode if it is a progressive JPEG only */ 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 /* No more interest in this image... */ 0N/A /* Make a one-row-high sample array with enough room to expand to ints */ 0N/A /* Step 4: set parameters for decompression */ 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 /* 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 /* Step 5: Start decompressor */ 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 /* Step 6: while (scan lines remain to be read) */ 0N/A /* jpeg_read_scanlines(...); */ 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 /* No more interest in this image... */ 0N/A /* Step 7: Finish decompression */ 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 /* Step 8: Release JPEG decompression object */ 0N/A /* This is an important step since it will release a good deal of memory. */ 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 /* Not needed for Java - the Java code will close the file */ 0N/A /* fclose(infile); */ 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 /* And we're done! */ 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 * 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 * 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 * 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.