1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
3 // Inclusions /////////////////////////////////////////////////////////
9 // hack for broken Solaris headers
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>
26 // JPEG ////////////////////////////////////////////////////////////////
28 #define INPUT_BUF_SIZE (1024 * 16)
31 struct jpeg_source_mgr pub;
32 org::xwt::plat::GCJ$JPEG* myself;
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);
40 void term_source (j_decompress_ptr cinfo) { }
41 void init_source (j_decompress_ptr cinfo) { }
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);
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;
52 src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
53 src->pub.bytes_in_buffer = nbytes;
57 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
58 source_manager* src = (source_manager*)cinfo->src;
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);
64 src->pub.next_input_byte += (size_t) num_bytes;
65 src->pub.bytes_in_buffer -= (size_t) num_bytes;
69 void org::xwt::plat::GCJ$JPEG::nativeDecompress() {
70 struct jpeg_decompress_struct cinfo;
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;
77 jpeg_create_decompress(&cinfo);
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;
87 src.pub.next_input_byte = (JOCTET*)elements(buffer);
88 src.pub.bytes_in_buffer = 0;
89 cinfo.src = (jpeg_source_mgr*)&src;
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);
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);
102 jpeg_finish_decompress(&cinfo);
104 // fill in the alpha components
105 for(int i=0; i<data->length; i++)
106 elements(data)[i] |= (jint)0xff000000;
108 } catch (java::lang::Throwable* t) {
109 jpeg_destroy_decompress(&cinfo);
112 jpeg_destroy_decompress(&cinfo);
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); }