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