1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
3 // Inclusions /////////////////////////////////////////////////////////
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>
22 // JPEG ////////////////////////////////////////////////////////////////
24 #define INPUT_BUF_SIZE (1024 * 16)
27 struct jpeg_source_mgr pub;
28 org::xwt::plat::GCJ$JPEG* myself;
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);
36 void term_source (j_decompress_ptr cinfo) { }
37 void init_source (j_decompress_ptr cinfo) { }
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);
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;
48 src->pub.next_input_byte = (JOCTET*)elements(src->myself->buffer);
49 src->pub.bytes_in_buffer = nbytes;
53 void skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
54 source_manager* src = (source_manager*)cinfo->src;
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);
60 src->pub.next_input_byte += (size_t) num_bytes;
61 src->pub.bytes_in_buffer -= (size_t) num_bytes;
65 void org::xwt::plat::GCJ$JPEG::nativeDecompress() {
66 struct jpeg_decompress_struct cinfo;
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;
73 jpeg_create_decompress(&cinfo);
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;
83 src.pub.next_input_byte = (JOCTET*)elements(buffer);
84 src.pub.bytes_in_buffer = 0;
85 cinfo.src = (jpeg_source_mgr*)&src;
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);
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);
98 jpeg_finish_decompress(&cinfo);
100 // fill in the alpha components
101 for(int i=0; i<data->length; i++)
102 elements(data)[i] |= (jint)0xff000000;
104 } catch (java::lang::Throwable* t) {
105 jpeg_destroy_decompress(&cinfo);
108 jpeg_destroy_decompress(&cinfo);
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); }