2003/09/20 05:03:47
[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/plat/GCJ.h>
18 #include <java/io/InputStream.h>
19 #include <java/io/ByteArrayInputStream.h>
20 #include <java/lang/RuntimeException.h>
21 #include <org/xwt/plat/GCJ.h>
22 #include <org/xwt/plat/GCJ$JPEG.h>
23
24
25
26 // builtin.xwar /////////////////////////////////////////////////////////
27
28 extern unsigned char builtin_bytes[];
29 extern int builtin_length;
30
31 java::io::InputStream* org::xwt::plat::GCJ::_getBuiltinInputStream() {
32     jbyteArray ret = JvNewByteArray(builtin_length);
33     memcpy(elements(ret), builtin_bytes, builtin_length);
34     return new java::io::ByteArrayInputStream(ret);
35 }
36
37
38
39 // JPEG ////////////////////////////////////////////////////////////////
40
41 #define INPUT_BUF_SIZE (1024 * 16)
42
43 typedef struct {
44     struct jpeg_source_mgr pub;
45     org::xwt::plat::GCJ$JPEG* myself;
46 } source_manager;
47
48 void jpeg_error_handler (j_common_ptr cinfo) {
49   jstring message = JvNewStringLatin1(cinfo->err->jpeg_message_table[cinfo->err->msg_code]);
50   throw new java::lang::RuntimeException(message);
51 }
52
53 void term_source (j_decompress_ptr cinfo) { }
54 void init_source (j_decompress_ptr cinfo) { }
55
56 boolean fill_input_buffer (j_decompress_ptr cinfo) {
57   source_manager* src = (source_manager*)cinfo->src;
58   jint nbytes = src->myself->is->read(src->myself->buffer, 0, INPUT_BUF_SIZE);
59   if (nbytes <= 0) {
60     // the JPEG library specifically suggests padding the end with EOF markers
61     elements(src->myself->buffer)[0] = (jbyte)0xFF;
62     elements(src->myself->buffer)[1] = (jbyte)JPEG_EOI;
63     nbytes = 2;
64   }
65   src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
66   src->pub.bytes_in_buffer = nbytes;
67   return 1;
68 }
69
70 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
71   source_manager* src = (source_manager*)cinfo->src;
72   if (num_bytes > 0) {
73     while (num_bytes > (long) src->pub.bytes_in_buffer) {
74       num_bytes -= (long) src->pub.bytes_in_buffer;
75       (void) fill_input_buffer(cinfo);
76     }
77     src->pub.next_input_byte += (size_t) num_bytes;
78     src->pub.bytes_in_buffer -= (size_t) num_bytes;
79   }
80 }
81
82 void org::xwt::plat::GCJ$JPEG::nativeDecompress() {
83     struct jpeg_decompress_struct cinfo;
84   
85     // set up our error handler
86     struct jpeg_error_mgr error_handler;
87     cinfo.err = jpeg_std_error(&error_handler);
88     error_handler.error_exit = &jpeg_error_handler;
89     
90     jpeg_create_decompress(&cinfo);
91     try {
92         source_manager src;
93         buffer = JvNewByteArray(INPUT_BUF_SIZE);
94         src.pub.init_source = init_source;
95         src.pub.fill_input_buffer = fill_input_buffer;
96         src.pub.skip_input_data = skip_input_data;
97         src.pub.resync_to_restart = jpeg_resync_to_restart;
98         src.pub.term_source = term_source;
99         src.myself = this;
100         src.pub.next_input_byte = (JOCTET*)elements(buffer);
101         src.pub.bytes_in_buffer = 0;
102         cinfo.src = (jpeg_source_mgr*)&src;
103
104         jpeg_read_header(&cinfo, 1);
105         jpeg_start_decompress(&cinfo);
106         width = cinfo.output_width;
107         height = cinfo.output_height;
108         data = JvNewIntArray(width * height);
109
110         while (cinfo.output_scanline < cinfo.output_height) {
111           JSAMPLE* dest = (JSAMPLE*)(elements(data) + cinfo.output_scanline * width);
112           jpeg_read_scanlines(&cinfo, &dest, 1);
113         }
114         
115         jpeg_finish_decompress(&cinfo);
116
117         // fill in the alpha components
118         for(int i=0; i<data->length; i++)
119           elements(data)[i] |= (jint)0xff000000;
120
121     } catch (java::lang::Throwable* t) {
122         jpeg_destroy_decompress(&cinfo);
123         throw t;
124     }
125     jpeg_destroy_decompress(&cinfo);
126 }
127
128 // C++ new/delete operators (JvMalloc never fails)
129 void* operator new(size_t size) { return JvMalloc(size); }
130 void* operator new[](size_t size) { return JvMalloc(size);}
131 void operator delete(void *p) { JvFree(p); }
132 void operator delete[](void *p) { JvFree(p); }