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