1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
5 // Inclusions /////////////////////////////////////////////////////////
9 #include <org/ibex/plat/Platform.h>
10 #include <org/ibex/graphics/Picture.h>
11 #include <org/ibex/plat/GCJ.h>
12 #include <java/io/InputStream.h>
13 #include <java/io/ByteArrayInputStream.h>
14 #include <java/lang/RuntimeException.h>
15 #include <org/ibex/plat/GCJ.h>
16 #include <org/ibex/util/Log.h>
19 // hack for broken Solaris headers
32 using org::ibex::util::Log;
34 // builtin.xwar /////////////////////////////////////////////////////////
36 extern unsigned char builtin_bytes[];
37 extern int builtin_length;
39 java::io::InputStream* org::ibex::plat::GCJ::_getBuiltinInputStream() {
40 jbyteArray ret = JvNewByteArray(builtin_length);
41 memcpy(elements(ret), builtin_bytes, builtin_length);
42 return new java::io::ByteArrayInputStream(ret);
47 // JPEG ////////////////////////////////////////////////////////////////
50 struct jpeg_source_mgr pub;
51 java::io::InputStream *is;
56 struct jpeg_error_mgr pub;
57 jmp_buf setjmp_buffer;
60 static void error_exit (j_common_ptr cinfo) {
61 my_error_mgr_t *myerr = (my_error_mgr_t*) cinfo->err;
62 longjmp(myerr->setjmp_buffer,1);
65 static void term_source (j_decompress_ptr cinfo) { }
66 static void init_source (j_decompress_ptr cinfo) { }
68 boolean fill_input_buffer (j_decompress_ptr cinfo) {
69 my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
70 JOCTET *p = (JOCTET*)elements(src->buf);
73 n = src->is->read(src->buf, 0, src->buf->length);
74 } catch(java::lang::Throwable *e) {
78 src->pub.next_input_byte = p;
81 // the JPEG library specifically suggests padding the end with EOF markers
84 src->pub.bytes_in_buffer = 2;
86 src->pub.bytes_in_buffer = n;
91 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
92 my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
94 if (num_bytes <= 0) return;
96 while (num_bytes > src->pub.bytes_in_buffer) {
97 num_bytes -= src->pub.bytes_in_buffer;
98 fill_input_buffer(cinfo);
100 src->pub.next_input_byte += num_bytes;
101 src->pub.bytes_in_buffer -= num_bytes;
104 void org::ibex::plat::GCJ::_decodeJPEG(java::io::InputStream* is, org::ibex::graphics::Picture* p) {
105 struct jpeg_decompress_struct cinfo;
110 src.buf = JvNewByteArray(16384);
112 src.pub.init_source = init_source;
113 src.pub.fill_input_buffer = fill_input_buffer;
114 src.pub.skip_input_data = skip_input_data;
115 src.pub.resync_to_restart = jpeg_resync_to_restart;
116 src.pub.term_source = term_source;
117 src.pub.next_input_byte = NULL;
118 src.pub.bytes_in_buffer = 0;
120 if (setjmp(jerr.setjmp_buffer)) {
121 // FEATURE - we should handle errors better than this
122 char msgbuf[JMSG_LENGTH_MAX];
123 (jerr.pub.format_message)((j_common_ptr)&cinfo, msgbuf);
124 Log::info(&GCJ::class$,JvNewStringLatin1(msgbuf));
125 jpeg_destroy_decompress(&cinfo);
129 jpeg_create_decompress(&cinfo);
131 cinfo.src = &src.pub;
132 cinfo.err = jpeg_std_error(&jerr.pub);
133 jerr.pub.error_exit = error_exit;
135 jpeg_read_header(&cinfo,TRUE);
136 jpeg_start_decompress(&cinfo);
138 p->width = cinfo.output_width;
139 p->height = cinfo.output_height;
140 p->data = JvNewIntArray(p->width * p->height);
142 while (cinfo.output_scanline < cinfo.output_height) {
143 JSAMPLE *dest = (JSAMPLE*) (elements(p->data) + cinfo.output_scanline * p->width);
144 jpeg_read_scanlines(&cinfo,&dest,1);
146 jpeg_finish_decompress(&cinfo);
147 jpeg_destroy_decompress(&cinfo);
149 for(int i=0;i<p->data->length;i++)
150 elements(p->data)[i] |= 0xff000000; // alpha channel
153 // C++ new/delete operators (JvMalloc never fails)
154 void* operator new(size_t size) { return JvMalloc(size); }
155 void* operator new[](size_t size) { return JvMalloc(size);}
156 void operator delete(void *p) { JvFree(p); }
157 void operator delete[](void *p) { JvFree(p); }