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