1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
3 // Inclusions /////////////////////////////////////////////////////////
13 #include <org/xwt/util/JSObject.h>
14 #include <org/xwt/plat/GCJ.h>
15 #include <java/io/InputStream.h>
16 #include <java/io/ByteArrayInputStream.h>
17 #include <java/lang/RuntimeException.h>
18 #include <org/xwt/plat/GCJ.h>
19 #include <org/xwt/plat/GCJ$JPEG.h>
23 // builtin.xwar /////////////////////////////////////////////////////////
25 extern unsigned char builtin_xwar[];
26 extern int builtin_xwar_length;
28 java::io::InputStream* org::xwt::plat::GCJ::_getBuiltinInputStream() {
29 jbyteArray ret = JvNewByteArray(builtin_xwar_length);
30 memcpy(elements(ret), builtin_xwar, builtin_xwar_length);
31 return new java::io::ByteArrayInputStream(ret);
36 // JPEG ////////////////////////////////////////////////////////////////
38 #define INPUT_BUF_SIZE (1024 * 16)
41 struct jpeg_source_mgr pub;
42 org::xwt::plat::GCJ$JPEG* myself;
45 void jpeg_error_handler (j_common_ptr cinfo) {
46 jstring message = JvNewStringLatin1(cinfo->err->jpeg_message_table[cinfo->err->msg_code]);
47 throw new java::lang::RuntimeException(message);
50 void term_source (j_decompress_ptr cinfo) { }
51 void init_source (j_decompress_ptr cinfo) { }
53 boolean fill_input_buffer (j_decompress_ptr cinfo) {
54 source_manager* src = (source_manager*)cinfo->src;
55 jint nbytes = src->myself->is->read(src->myself->buffer, 0, INPUT_BUF_SIZE);
57 // the JPEG library specifically suggests padding the end with EOF markers
58 elements(src->myself->buffer)[0] = (jbyte)0xFF;
59 elements(src->myself->buffer)[1] = (jbyte)JPEG_EOI;
62 src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
63 src->pub.bytes_in_buffer = nbytes;
67 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
68 source_manager* src = (source_manager*)cinfo->src;
70 while (num_bytes > (long) src->pub.bytes_in_buffer) {
71 num_bytes -= (long) src->pub.bytes_in_buffer;
72 (void) fill_input_buffer(cinfo);
74 src->pub.next_input_byte += (size_t) num_bytes;
75 src->pub.bytes_in_buffer -= (size_t) num_bytes;
79 void org::xwt::plat::GCJ$JPEG::nativeDecompress() {
80 struct jpeg_decompress_struct cinfo;
82 // set up our error handler
83 struct jpeg_error_mgr error_handler;
84 cinfo.err = jpeg_std_error(&error_handler);
85 error_handler.error_exit = &jpeg_error_handler;
87 jpeg_create_decompress(&cinfo);
90 buffer = JvNewByteArray(INPUT_BUF_SIZE);
91 src.pub.init_source = init_source;
92 src.pub.fill_input_buffer = fill_input_buffer;
93 src.pub.skip_input_data = skip_input_data;
94 src.pub.resync_to_restart = jpeg_resync_to_restart;
95 src.pub.term_source = term_source;
97 src.pub.next_input_byte = (JOCTET*)elements(buffer);
98 src.pub.bytes_in_buffer = 0;
99 cinfo.src = (jpeg_source_mgr*)&src;
101 jpeg_read_header(&cinfo, 1);
102 jpeg_start_decompress(&cinfo);
103 width = cinfo.output_width;
104 height = cinfo.output_height;
105 data = JvNewIntArray(width * height);
107 while (cinfo.output_scanline < cinfo.output_height) {
108 JSAMPLE* dest = (JSAMPLE*)(elements(data) + cinfo.output_scanline * width);
109 jpeg_read_scanlines(&cinfo, &dest, 1);
112 jpeg_finish_decompress(&cinfo);
114 // fill in the alpha components
115 for(int i=0; i<data->length; i++)
116 elements(data)[i] |= (jint)0xff000000;
118 } catch (java::lang::Throwable* t) {
119 jpeg_destroy_decompress(&cinfo);
122 jpeg_destroy_decompress(&cinfo);