2003/09/25 12:13:46
[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
25
26
27 // builtin.xwar /////////////////////////////////////////////////////////
28
29 extern unsigned char builtin_bytes[];
30 extern int builtin_length;
31
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);
36 }
37
38
39
40 // JPEG ////////////////////////////////////////////////////////////////
41
42 #define INPUT_BUF_SIZE (1024 * 16)
43
44 typedef struct {
45     struct jpeg_source_mgr pub;
46     /* FIXME
47     org::xwt::plat::GCJ$JPEG* myself;
48     */
49 } source_manager;
50
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);
54 }
55
56 void term_source (j_decompress_ptr cinfo) { }
57 void init_source (j_decompress_ptr cinfo) { }
58
59 boolean fill_input_buffer (j_decompress_ptr cinfo) {
60   /* FIXME
61   source_manager* src = (source_manager*)cinfo->src;
62   jint nbytes = src->myself->is->read(src->myself->buffer, 0, INPUT_BUF_SIZE);
63   if (nbytes <= 0) {
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;
67     nbytes = 2;
68   }
69   src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
70   src->pub.bytes_in_buffer = nbytes;
71   */
72   return 1;
73 }
74
75 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
76   source_manager* src = (source_manager*)cinfo->src;
77   if (num_bytes > 0) {
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);
81     }
82     src->pub.next_input_byte += (size_t) num_bytes;
83     src->pub.bytes_in_buffer -= (size_t) num_bytes;
84   }
85 }
86
87 org::xwt::Picture* org::xwt::plat::GCJ::_decodeJPEG(java::io::InputStream* is, jstring name) {
88     struct jpeg_decompress_struct cinfo;
89     jintArray data;
90     jint width;
91     jint height;
92   
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;
97     
98     jpeg_create_decompress(&cinfo);
99     try {
100         source_manager src;
101         /*
102         buffer = JvNewByteArray(INPUT_BUF_SIZE);
103         */
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;
109         /*
110         src.myself = this;
111         src.pub.next_input_byte = (JOCTET*)elements(buffer);
112         */
113         src.pub.bytes_in_buffer = 0;
114         cinfo.src = (jpeg_source_mgr*)&src;
115
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);
121
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);
125         }
126         
127         jpeg_finish_decompress(&cinfo);
128
129         // fill in the alpha components
130         for(int i=0; i<data->length; i++)
131           elements(data)[i] |= (jint)0xff000000;
132
133     } catch (java::lang::Throwable* t) {
134         jpeg_destroy_decompress(&cinfo);
135         throw t;
136     }
137     jpeg_destroy_decompress(&cinfo);
138     return org::xwt::Platform::createPicture(data, width, height);
139 }
140
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); }