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