1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
3 // Inclusions /////////////////////////////////////////////////////////
13 #include <org/xwt/plat/GCJ.h>
14 #include <java/io/InputStream.h>
15 #include <java/io/ByteArrayInputStream.h>
16 #include <java/lang/RuntimeException.h>
17 #include <org/xwt/plat/GCJ.h>
18 #include <org/xwt/plat/GCJ$JPEG.h>
22 // builtin.xwar /////////////////////////////////////////////////////////
24 extern unsigned char builtin_xwar[];
25 extern int builtin_xwar_length;
27 java::io::InputStream* org::xwt::plat::GCJ::_getBuiltinInputStream() {
28 jbyteArray ret = JvNewByteArray(builtin_xwar_length);
29 memcpy(elements(ret), builtin_xwar, builtin_xwar_length);
30 return new java::io::ByteArrayInputStream(ret);
35 // JPEG ////////////////////////////////////////////////////////////////
37 #define INPUT_BUF_SIZE (1024 * 16)
40 struct jpeg_source_mgr pub;
41 org::xwt::plat::GCJ$JPEG* myself;
44 void jpeg_error_handler (j_common_ptr cinfo) {
45 jstring message = JvNewStringLatin1(cinfo->err->jpeg_message_table[cinfo->err->msg_code]);
46 throw new java::lang::RuntimeException(message);
49 void term_source (j_decompress_ptr cinfo) { }
50 void init_source (j_decompress_ptr cinfo) { }
52 boolean fill_input_buffer (j_decompress_ptr cinfo) {
53 source_manager* src = (source_manager*)cinfo->src;
54 jint nbytes = src->myself->is->read(src->myself->buffer, 0, INPUT_BUF_SIZE);
56 // the JPEG library specifically suggests padding the end with EOF markers
57 elements(src->myself->buffer)[0] = (jbyte)0xFF;
58 elements(src->myself->buffer)[1] = (jbyte)JPEG_EOI;
61 src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
62 src->pub.bytes_in_buffer = nbytes;
66 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
67 source_manager* src = (source_manager*)cinfo->src;
69 while (num_bytes > (long) src->pub.bytes_in_buffer) {
70 num_bytes -= (long) src->pub.bytes_in_buffer;
71 (void) fill_input_buffer(cinfo);
73 src->pub.next_input_byte += (size_t) num_bytes;
74 src->pub.bytes_in_buffer -= (size_t) num_bytes;
78 void org::xwt::plat::GCJ$JPEG::nativeDecompress() {
79 struct jpeg_decompress_struct cinfo;
81 // set up our error handler
82 struct jpeg_error_mgr error_handler;
83 cinfo.err = jpeg_std_error(&error_handler);
84 error_handler.error_exit = &jpeg_error_handler;
86 jpeg_create_decompress(&cinfo);
89 buffer = JvNewByteArray(INPUT_BUF_SIZE);
90 src.pub.init_source = init_source;
91 src.pub.fill_input_buffer = fill_input_buffer;
92 src.pub.skip_input_data = skip_input_data;
93 src.pub.resync_to_restart = jpeg_resync_to_restart;
94 src.pub.term_source = term_source;
96 src.pub.next_input_byte = (JOCTET*)elements(buffer);
97 src.pub.bytes_in_buffer = 0;
98 cinfo.src = (jpeg_source_mgr*)&src;
100 jpeg_read_header(&cinfo, 1);
101 jpeg_start_decompress(&cinfo);
102 width = cinfo.output_width;
103 height = cinfo.output_height;
104 data = JvNewIntArray(width * height);
106 while (cinfo.output_scanline < cinfo.output_height) {
107 JSAMPLE* dest = (JSAMPLE*)(elements(data) + cinfo.output_scanline * width);
108 jpeg_read_scanlines(&cinfo, &dest, 1);
111 jpeg_finish_decompress(&cinfo);
113 // fill in the alpha components
114 for(int i=0; i<data->length; i++)
115 elements(data)[i] |= (jint)0xff000000;
117 } catch (java::lang::Throwable* t) {
118 jpeg_destroy_decompress(&cinfo);
121 jpeg_destroy_decompress(&cinfo);