1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
3 // Inclusions /////////////////////////////////////////////////////////
9 // hack for broken Solaris headers
17 #include <org/ibex/Platform.h>
18 #include <org/ibex/Picture.h>
19 #include <org/ibex/plat/GCJ.h>
20 #include <java/io/InputStream.h>
21 #include <java/io/ByteArrayInputStream.h>
22 #include <java/lang/RuntimeException.h>
23 #include <org/ibex/plat/GCJ.h>
24 #include <org/ibex/util/Log.h>
30 using org::ibex::util::Log;
32 // builtin.xwar /////////////////////////////////////////////////////////
34 extern unsigned char builtin_bytes[];
35 extern int builtin_length;
37 java::io::InputStream* org::ibex::plat::GCJ::_getBuiltinInputStream() {
38 jbyteArray ret = JvNewByteArray(builtin_length);
39 memcpy(elements(ret), builtin_bytes, builtin_length);
40 return new java::io::ByteArrayInputStream(ret);
45 // JPEG ////////////////////////////////////////////////////////////////
48 struct jpeg_source_mgr pub;
49 java::io::InputStream *is;
54 struct jpeg_error_mgr pub;
55 jmp_buf setjmp_buffer;
58 static void error_exit (j_common_ptr cinfo) {
59 my_error_mgr_t *myerr = (my_error_mgr_t*) cinfo->err;
60 longjmp(myerr->setjmp_buffer,1);
63 static void term_source (j_decompress_ptr cinfo) { }
64 static void init_source (j_decompress_ptr cinfo) { }
66 boolean fill_input_buffer (j_decompress_ptr cinfo) {
67 my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
68 JOCTET *p = (JOCTET*)elements(src->buf);
71 n = src->is->read(src->buf, 0, src->buf->length);
72 } catch(java::lang::Throwable *e) {
76 src->pub.next_input_byte = p;
79 // the JPEG library specifically suggests padding the end with EOF markers
82 src->pub.bytes_in_buffer = 2;
84 src->pub.bytes_in_buffer = n;
89 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
90 my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
92 if (num_bytes <= 0) return;
94 while (num_bytes > src->pub.bytes_in_buffer) {
95 num_bytes -= src->pub.bytes_in_buffer;
96 fill_input_buffer(cinfo);
98 src->pub.next_input_byte += num_bytes;
99 src->pub.bytes_in_buffer -= num_bytes;
102 void org::ibex::plat::GCJ::_decodeJPEG(java::io::InputStream* is, org::ibex::Picture* p) {
103 struct jpeg_decompress_struct cinfo;
108 src.buf = JvNewByteArray(16384);
110 src.pub.init_source = init_source;
111 src.pub.fill_input_buffer = fill_input_buffer;
112 src.pub.skip_input_data = skip_input_data;
113 src.pub.resync_to_restart = jpeg_resync_to_restart;
114 src.pub.term_source = term_source;
115 src.pub.next_input_byte = NULL;
116 src.pub.bytes_in_buffer = 0;
118 if (setjmp(jerr.setjmp_buffer)) {
119 // FEATURE - we should handle errors better than this
120 char msgbuf[JMSG_LENGTH_MAX];
121 (jerr.pub.format_message)((j_common_ptr)&cinfo, msgbuf);
122 Log::info(&GCJ::class$,JvNewStringLatin1(msgbuf));
123 jpeg_destroy_decompress(&cinfo);
127 jpeg_create_decompress(&cinfo);
129 cinfo.src = &src.pub;
130 cinfo.err = jpeg_std_error(&jerr.pub);
131 jerr.pub.error_exit = error_exit;
133 jpeg_read_header(&cinfo,TRUE);
134 jpeg_start_decompress(&cinfo);
136 p->width = cinfo.output_width;
137 p->height = cinfo.output_height;
138 p->data = JvNewIntArray(p->width * p->height);
140 while (cinfo.output_scanline < cinfo.output_height) {
141 JSAMPLE *dest = (JSAMPLE*) (elements(p->data) + cinfo.output_scanline * p->width);
142 jpeg_read_scanlines(&cinfo,&dest,1);
144 jpeg_finish_decompress(&cinfo);
145 jpeg_destroy_decompress(&cinfo);
147 for(int i=0;i<p->data->length;i++)
148 elements(p->data)[i] |= 0xff000000; // alpha channel
151 // C++ new/delete operators (JvMalloc never fails)
152 void* operator new(size_t size) { return JvMalloc(size); }
153 void* operator new[](size_t size) { return JvMalloc(size);}
154 void operator delete(void *p) { JvFree(p); }
155 void operator delete[](void *p) { JvFree(p); }