mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / ibex / plat / GCJ.cc
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
2
3 // Inclusions /////////////////////////////////////////////////////////
4
5 #include <gcj/cni.h>
6 #include <gcj/array.h>
7 extern "C" {
8
9 // hack for broken Solaris headers
10 #define _WCHAR_T
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <setjmp.h>
15 #include "jpeglib.h"
16 }
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>
25
26 #ifdef TRUE
27 #undef TRUE
28 #endif
29 #define TRUE 1
30 using org::ibex::util::Log;
31
32 // builtin.xwar /////////////////////////////////////////////////////////
33
34 extern unsigned char builtin_bytes[];
35 extern int builtin_length;
36
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);
41 }
42
43
44
45 // JPEG ////////////////////////////////////////////////////////////////
46
47 typedef struct {
48     struct jpeg_source_mgr pub;
49     java::io::InputStream *is;
50     jbyteArray buf;
51 } my_source_mgr_t;
52
53 typedef struct {
54     struct jpeg_error_mgr pub;
55     jmp_buf setjmp_buffer;
56 } my_error_mgr_t;
57
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);
61 }
62
63 static void term_source (j_decompress_ptr cinfo) { }
64 static void init_source (j_decompress_ptr cinfo) { }
65
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);
69     jint n;
70     try {
71         n = src->is->read(src->buf, 0, src->buf->length);
72     } catch(java::lang::Throwable *e) {
73         n = -1;
74     }
75     
76     src->pub.next_input_byte = p;
77     
78     if (n <= 0) {
79         // the JPEG library specifically suggests padding the end with EOF markers
80         p[0] = 0xff;
81         p[1] = JPEG_EOI;
82         src->pub.bytes_in_buffer = 2;
83     } else {
84         src->pub.bytes_in_buffer = n;
85     }
86     return 1;
87 }
88
89 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
90     my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
91     
92     if (num_bytes <= 0) return; 
93     
94     while (num_bytes > src->pub.bytes_in_buffer) {
95         num_bytes -= src->pub.bytes_in_buffer;
96         fill_input_buffer(cinfo);
97     }
98     src->pub.next_input_byte += num_bytes;
99     src->pub.bytes_in_buffer -= num_bytes;
100 }
101
102 void org::ibex::plat::GCJ::_decodeJPEG(java::io::InputStream* is, org::ibex::Picture* p) {
103     struct jpeg_decompress_struct cinfo;
104     my_error_mgr_t jerr;
105     my_source_mgr_t src;
106     
107     src.is = is;
108     src.buf = JvNewByteArray(16384);
109     
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;
117     
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);
124         return;
125     }
126   
127     jpeg_create_decompress(&cinfo);
128     
129     cinfo.src = &src.pub;
130     cinfo.err = jpeg_std_error(&jerr.pub);
131     jerr.pub.error_exit = error_exit;
132     
133     jpeg_read_header(&cinfo,TRUE);
134     jpeg_start_decompress(&cinfo);
135     
136     p->width = cinfo.output_width;
137     p->height = cinfo.output_height;
138     p->data = JvNewIntArray(p->width * p->height);
139     
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);
143     }
144     jpeg_finish_decompress(&cinfo);
145     jpeg_destroy_decompress(&cinfo);
146     
147     for(int i=0;i<p->data->length;i++)
148         elements(p->data)[i] |= 0xff000000; // alpha channel
149 }
150
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); }