1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
3 // Inclusions /////////////////////////////////////////////////////////
9 // hack for broken Solaris headers
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>
29 using org::xwt::util::Log;
33 // builtin.xwar /////////////////////////////////////////////////////////
35 extern unsigned char builtin_bytes[];
36 extern int builtin_length;
38 java::io::InputStream* org::xwt::plat::GCJ::_getBuiltinInputStream() {
39 jbyteArray ret = JvNewByteArray(builtin_length);
40 memcpy(elements(ret), builtin_bytes, builtin_length);
41 return new java::io::ByteArrayInputStream(ret);
46 // JPEG ////////////////////////////////////////////////////////////////
49 struct jpeg_source_mgr pub;
50 java::io::InputStream *is;
55 struct jpeg_error_mgr pub;
56 jmp_buf setjmp_buffer;
59 static void error_exit (j_common_ptr cinfo) {
60 my_error_mgr_t *myerr = (my_error_mgr_t*) cinfo->err;
61 longjmp(myerr->setjmp_buffer,1);
64 static void term_source (j_decompress_ptr cinfo) { }
65 static void init_source (j_decompress_ptr cinfo) { }
67 boolean fill_input_buffer (j_decompress_ptr cinfo) {
68 my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
69 JOCTET *p = (JOCTET*)elements(src->buf);
72 n = src->is->read(src->buf, 0, src->buf->length);
73 } catch(java::lang::Throwable *e) {
77 src->pub.next_input_byte = p;
80 // the JPEG library specifically suggests padding the end with EOF markers
83 src->pub.bytes_in_buffer = 2;
85 src->pub.bytes_in_buffer = n;
90 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
91 my_source_mgr_t* src = (my_source_mgr_t*) cinfo->src;
93 if (num_bytes <= 0) return;
95 while (num_bytes > src->pub.bytes_in_buffer) {
96 num_bytes -= src->pub.bytes_in_buffer;
97 fill_input_buffer(cinfo);
99 src->pub.next_input_byte += num_bytes;
100 src->pub.bytes_in_buffer -= num_bytes;
103 void org::xwt::plat::GCJ::_decodeJPEG(java::io::InputStream* is, org::xwt::Picture* p) {
104 struct jpeg_decompress_struct cinfo;
109 src.buf = JvNewByteArray(16384);
111 src.pub.init_source = init_source;
112 src.pub.fill_input_buffer = fill_input_buffer;
113 src.pub.skip_input_data = skip_input_data;
114 src.pub.resync_to_restart = jpeg_resync_to_restart;
115 src.pub.term_source = term_source;
116 src.pub.next_input_byte = NULL;
117 src.pub.bytes_in_buffer = 0;
119 if (setjmp(jerr.setjmp_buffer)) {
120 // FEATURE - we should handle errors better than this
121 char msgbuf[JMSG_LENGTH_MAX];
122 (jerr.pub.format_message)((j_common_ptr)&cinfo, msgbuf);
123 Log::info(&GCJ::class$,JvNewStringLatin1(msgbuf));
124 jpeg_destroy_decompress(&cinfo);
128 jpeg_create_decompress(&cinfo);
130 cinfo.src = &src.pub;
131 cinfo.err = jpeg_std_error(&jerr.pub);
132 jerr.pub.error_exit = error_exit;
134 jpeg_read_header(&cinfo,TRUE);
135 jpeg_start_decompress(&cinfo);
137 p->width = cinfo.output_width;
138 p->height = cinfo.output_height;
139 p->data = JvNewIntArray(p->width * p->height);
141 while (cinfo.output_scanline < cinfo.output_height) {
142 JSAMPLE *dest = (JSAMPLE*) (elements(p->data) + cinfo.output_scanline * p->width);
143 jpeg_read_scanlines(&cinfo,&dest,1);
145 jpeg_finish_decompress(&cinfo);
146 jpeg_destroy_decompress(&cinfo);
148 for(int i=0;i<p->data->length;i++)
149 elements(p->data)[i] |= 0xff000000; // alpha channel
152 // C++ new/delete operators (JvMalloc never fails)
153 void* operator new(size_t size) { return JvMalloc(size); }
154 void* operator new[](size_t size) { return JvMalloc(size);}
155 void operator delete(void *p) { JvFree(p); }
156 void operator delete[](void *p) { JvFree(p); }