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>
27 // builtin.xwar /////////////////////////////////////////////////////////
29 extern unsigned char builtin_bytes[];
30 extern int builtin_length;
32 java::io::InputStream* org::xwt::plat::GCJ::_getBuiltinInputStream() {
33 jbyteArray ret = JvNewByteArray(builtin_length);
34 memcpy(elements(ret), builtin_bytes, builtin_length);
35 return new java::io::ByteArrayInputStream(ret);
40 // JPEG ////////////////////////////////////////////////////////////////
42 #define INPUT_BUF_SIZE (1024 * 16)
45 struct jpeg_source_mgr pub;
47 org::xwt::plat::GCJ$JPEG* myself;
51 void jpeg_error_handler (j_common_ptr cinfo) {
52 jstring message = JvNewStringLatin1(cinfo->err->jpeg_message_table[cinfo->err->msg_code]);
53 throw new java::lang::RuntimeException(message);
56 void term_source (j_decompress_ptr cinfo) { }
57 void init_source (j_decompress_ptr cinfo) { }
59 boolean fill_input_buffer (j_decompress_ptr cinfo) {
61 source_manager* src = (source_manager*)cinfo->src;
62 jint nbytes = src->myself->is->read(src->myself->buffer, 0, INPUT_BUF_SIZE);
64 // the JPEG library specifically suggests padding the end with EOF markers
65 elements(src->myself->buffer)[0] = (jbyte)0xFF;
66 elements(src->myself->buffer)[1] = (jbyte)JPEG_EOI;
69 src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
70 src->pub.bytes_in_buffer = nbytes;
75 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
76 source_manager* src = (source_manager*)cinfo->src;
78 while (num_bytes > (long) src->pub.bytes_in_buffer) {
79 num_bytes -= (long) src->pub.bytes_in_buffer;
80 (void) fill_input_buffer(cinfo);
82 src->pub.next_input_byte += (size_t) num_bytes;
83 src->pub.bytes_in_buffer -= (size_t) num_bytes;
87 org::xwt::Picture* org::xwt::plat::GCJ::_decodeJPEG(java::io::InputStream* is, jstring name) {
88 struct jpeg_decompress_struct cinfo;
93 // set up our error handler
94 struct jpeg_error_mgr error_handler;
95 cinfo.err = jpeg_std_error(&error_handler);
96 error_handler.error_exit = &jpeg_error_handler;
98 jpeg_create_decompress(&cinfo);
102 buffer = JvNewByteArray(INPUT_BUF_SIZE);
104 src.pub.init_source = init_source;
105 src.pub.fill_input_buffer = fill_input_buffer;
106 src.pub.skip_input_data = skip_input_data;
107 src.pub.resync_to_restart = jpeg_resync_to_restart;
108 src.pub.term_source = term_source;
111 src.pub.next_input_byte = (JOCTET*)elements(buffer);
113 src.pub.bytes_in_buffer = 0;
114 cinfo.src = (jpeg_source_mgr*)&src;
116 jpeg_read_header(&cinfo, 1);
117 jpeg_start_decompress(&cinfo);
118 width = cinfo.output_width;
119 height = cinfo.output_height;
120 data = JvNewIntArray(width * height);
122 while (cinfo.output_scanline < cinfo.output_height) {
123 JSAMPLE* dest = (JSAMPLE*)(elements(data) + cinfo.output_scanline * width);
124 jpeg_read_scanlines(&cinfo, &dest, 1);
127 jpeg_finish_decompress(&cinfo);
129 // fill in the alpha components
130 for(int i=0; i<data->length; i++)
131 elements(data)[i] |= (jint)0xff000000;
133 } catch (java::lang::Throwable* t) {
134 jpeg_destroy_decompress(&cinfo);
137 jpeg_destroy_decompress(&cinfo);
138 return org::xwt::Platform::createPicture(data, width, height);
141 // C++ new/delete operators (JvMalloc never fails)
142 void* operator new(size_t size) { return JvMalloc(size); }
143 void* operator new[](size_t size) { return JvMalloc(size);}
144 void operator delete(void *p) { JvFree(p); }
145 void operator delete[](void *p) { JvFree(p); }