474e640168648025837bebfdad2a170a8abd74d8
[org.ibex.core.git] / src / org / xwt / 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/xwt/Platform.h>
18 #include <org/xwt/Picture.h>
19 #include <org/xwt/plat/GCJ.h>
20 #include <java/io/InputStream.h>
21 #include <java/io/ByteArrayInputStream.h>
22 #include <java/lang/RuntimeException.h>
23 #include <org/xwt/plat/GCJ.h>
24 #include <org/xwt/util/Log.h>
25
26 #define TRUE 1
27 using org::xwt::util::Log;
28
29 #define TRUE 1
30
31 // builtin.xwar /////////////////////////////////////////////////////////
32
33 extern unsigned char builtin_bytes[];
34 extern int builtin_length;
35
36 java::io::InputStream* org::xwt::plat::GCJ::_getBuiltinInputStream() {
37     jbyteArray ret = JvNewByteArray(builtin_length);
38     memcpy(elements(ret), builtin_bytes, builtin_length);
39     return new java::io::ByteArrayInputStream(ret);
40 }
41
42
43
44 // JPEG ////////////////////////////////////////////////////////////////
45
46 typedef struct {
47     struct jpeg_source_mgr pub;
48     java::io::InputStream *is;
49     jbyteArray buf;
50 } my_source_mgr_t;
51
52 typedef struct {
53     struct jpeg_error_mgr pub;
54     jmp_buf setjmp_buffer;
55 } my_error_mgr_t;
56
57 static void error_exit (j_common_ptr cinfo) {
58     my_error_mgr_t *myerr = (my_error_mgr_t*) cinfo->err;
59     longjmp(myerr->setjmp_buffer,1);
60 }
61
62 static void term_source (j_decompress_ptr cinfo) { }
63 static void init_source (j_decompress_ptr cinfo) { }
64
65 boolean fill_input_buffer (j_decompress_ptr cinfo) {
66     my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
67     JOCTET *p = (JOCTET*)elements(src->buf);
68     jint n;
69     try {
70         n = src->is->read(src->buf, 0, src->buf->length);
71     } catch(java::lang::Throwable *e) {
72         n = -1;
73     }
74     
75     src->pub.next_input_byte = p;
76     
77     if (n <= 0) {
78         // the JPEG library specifically suggests padding the end with EOF markers
79         p[0] = 0xff;
80         p[1] = JPEG_EOI;
81         src->pub.bytes_in_buffer = 2;
82     } else {
83         src->pub.bytes_in_buffer = n;
84     }
85     return 1;
86 }
87
88 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
89     my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
90     
91     if (num_bytes <= 0) return; 
92     
93     while (num_bytes > src->pub.bytes_in_buffer) {
94         num_bytes -= src->pub.bytes_in_buffer;
95         fill_input_buffer(cinfo);
96     }
97     src->pub.next_input_byte += num_bytes;
98     src->pub.bytes_in_buffer -= num_bytes;
99 }
100
101 org::xwt::Picture* org::xwt::plat::GCJ::_decodeJPEG(java::io::InputStream* is, jstring name) {
102     struct jpeg_decompress_struct cinfo;
103     my_error_mgr_t jerr;
104     my_source_mgr_t src;
105     jintArray data;
106     jint width;
107     jint height;
108     
109     src.is = is;
110     src.buf = JvNewByteArray(16384);
111     
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;
119     
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::log(&GCJ::class$,JvNewStringLatin1(msgbuf));
125         jpeg_destroy_decompress(&cinfo);
126         return 0;
127     }
128   
129     jpeg_create_decompress(&cinfo);
130     
131     cinfo.src = &src.pub;
132     cinfo.err = jpeg_std_error(&jerr.pub);
133     jerr.pub.error_exit = error_exit;
134     
135     jpeg_read_header(&cinfo,TRUE);
136     jpeg_start_decompress(&cinfo);
137     
138     width = cinfo.output_width;
139     height = cinfo.output_height;
140     data = JvNewIntArray(width * height);
141     
142     while (cinfo.output_scanline < cinfo.output_height) {
143         JSAMPLE *dest = (JSAMPLE*) (elements(data) + cinfo.output_scanline * width);
144         jpeg_read_scanlines(&cinfo,&dest,1);
145     }
146     jpeg_finish_decompress(&cinfo);
147     jpeg_destroy_decompress(&cinfo);
148     
149     for(int i=0;i<data->length;i++)
150         elements(data)[i] |= 0xff000000; // alpha channel
151     
152     return org::xwt::Platform::createPicture(data, width, height);
153 }
154
155 // C++ new/delete operators (JvMalloc never fails)
156 void* operator new(size_t size) { return JvMalloc(size); }
157 void* operator new[](size_t size) { return JvMalloc(size);}
158 void operator delete(void *p) { JvFree(p); }
159 void operator delete[](void *p) { JvFree(p); }