1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
3 // Inclusions /////////////////////////////////////////////////////////
9 // hack for broken Solaris headers
17 #include <org/xwt/plat/GCJ.h>
18 #include <java/io/InputStream.h>
19 #include <java/io/ByteArrayInputStream.h>
20 #include <java/lang/RuntimeException.h>
21 #include <org/xwt/plat/GCJ.h>
22 #include <org/xwt/plat/GCJ$JPEG.h>
26 // builtin.xwar /////////////////////////////////////////////////////////
28 extern unsigned char builtin_bytes[];
29 extern int builtin_length;
31 java::io::InputStream* org::xwt::plat::GCJ::_getBuiltinInputStream() {
32 jbyteArray ret = JvNewByteArray(builtin_length);
33 memcpy(elements(ret), builtin_bytes, builtin_length);
34 return new java::io::ByteArrayInputStream(ret);
39 // JPEG ////////////////////////////////////////////////////////////////
41 #define INPUT_BUF_SIZE (1024 * 16)
44 struct jpeg_source_mgr pub;
45 org::xwt::plat::GCJ$JPEG* myself;
48 void jpeg_error_handler (j_common_ptr cinfo) {
49 jstring message = JvNewStringLatin1(cinfo->err->jpeg_message_table[cinfo->err->msg_code]);
50 throw new java::lang::RuntimeException(message);
53 void term_source (j_decompress_ptr cinfo) { }
54 void init_source (j_decompress_ptr cinfo) { }
56 boolean fill_input_buffer (j_decompress_ptr cinfo) {
57 source_manager* src = (source_manager*)cinfo->src;
58 jint nbytes = src->myself->is->read(src->myself->buffer, 0, INPUT_BUF_SIZE);
60 // the JPEG library specifically suggests padding the end with EOF markers
61 elements(src->myself->buffer)[0] = (jbyte)0xFF;
62 elements(src->myself->buffer)[1] = (jbyte)JPEG_EOI;
65 src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
66 src->pub.bytes_in_buffer = nbytes;
70 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
71 source_manager* src = (source_manager*)cinfo->src;
73 while (num_bytes > (long) src->pub.bytes_in_buffer) {
74 num_bytes -= (long) src->pub.bytes_in_buffer;
75 (void) fill_input_buffer(cinfo);
77 src->pub.next_input_byte += (size_t) num_bytes;
78 src->pub.bytes_in_buffer -= (size_t) num_bytes;
82 void org::xwt::plat::GCJ$JPEG::nativeDecompress() {
83 struct jpeg_decompress_struct cinfo;
85 // set up our error handler
86 struct jpeg_error_mgr error_handler;
87 cinfo.err = jpeg_std_error(&error_handler);
88 error_handler.error_exit = &jpeg_error_handler;
90 jpeg_create_decompress(&cinfo);
93 buffer = JvNewByteArray(INPUT_BUF_SIZE);
94 src.pub.init_source = init_source;
95 src.pub.fill_input_buffer = fill_input_buffer;
96 src.pub.skip_input_data = skip_input_data;
97 src.pub.resync_to_restart = jpeg_resync_to_restart;
98 src.pub.term_source = term_source;
100 src.pub.next_input_byte = (JOCTET*)elements(buffer);
101 src.pub.bytes_in_buffer = 0;
102 cinfo.src = (jpeg_source_mgr*)&src;
104 jpeg_read_header(&cinfo, 1);
105 jpeg_start_decompress(&cinfo);
106 width = cinfo.output_width;
107 height = cinfo.output_height;
108 data = JvNewIntArray(width * height);
110 while (cinfo.output_scanline < cinfo.output_height) {
111 JSAMPLE* dest = (JSAMPLE*)(elements(data) + cinfo.output_scanline * width);
112 jpeg_read_scanlines(&cinfo, &dest, 1);
115 jpeg_finish_decompress(&cinfo);
117 // fill in the alpha components
118 for(int i=0; i<data->length; i++)
119 elements(data)[i] |= (jint)0xff000000;
121 } catch (java::lang::Throwable* t) {
122 jpeg_destroy_decompress(&cinfo);
125 jpeg_destroy_decompress(&cinfo);
128 // C++ new/delete operators (JvMalloc never fails)
129 void* operator new(size_t size) { return JvMalloc(size); }
130 void* operator new[](size_t size) { return JvMalloc(size);}
131 void operator delete(void *p) { JvFree(p); }
132 void operator delete[](void *p) { JvFree(p); }