1 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 Copyright (C) 2010 Red Hat, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "config.h"
19
20 #include "decode.h"
21
22 #ifdef G_OS_WIN32
23 /* We need some hacks to avoid warnings from the jpeg headers, ex: */
24 /* #define HAVE_BOOLEAN */
25 #define XMD_H
26 /* #undef FAR */
27 /* but they are not compatible: uchar vs int........!@@(#$$??!@! */
28 /* fix this with UGLY HACK! */
29 /* #define boolean spice_jpeg_boolean */
30 /* #define INT32 spice_jpeg_int32 */
31 #endif
32
33 #include <stdio.h>
34 #include <jpeglib.h>
35
36 typedef struct GlibJpegDecoder
37 {
38 SpiceJpegDecoder base;
39 struct jpeg_decompress_struct _cinfo;
40 struct jpeg_error_mgr _jerr;
41 struct jpeg_source_mgr _jsrc;
42
43 uint8_t* _data;
44 int _data_size;
45 int _width;
46 int _height;
47 } GlibJpegDecoder;
48
begin_decode(SpiceJpegDecoder * decoder,uint8_t * data,int data_size,int * out_width,int * out_height)49 static void begin_decode(SpiceJpegDecoder *decoder,
50 uint8_t* data, int data_size,
51 int* out_width, int* out_height)
52 {
53 GlibJpegDecoder *d = SPICE_CONTAINEROF(decoder, GlibJpegDecoder, base);
54
55 g_return_if_fail(data != NULL);
56 g_return_if_fail(data_size != 0);
57
58 if (d->_data)
59 jpeg_abort_decompress(&d->_cinfo);
60
61 d->_data = data;
62 d->_data_size = data_size;
63
64 d->_cinfo.src->next_input_byte = d->_data;
65 d->_cinfo.src->bytes_in_buffer = d->_data_size;
66
67 jpeg_read_header(&d->_cinfo, TRUE);
68
69 d->_cinfo.out_color_space = JCS_RGB;
70 d->_width = d->_cinfo.image_width;
71 d->_height = d->_cinfo.image_height;
72
73 *out_width = d->_width;
74 *out_height = d->_height;
75 }
76
77 /* TODO: move it elsewhere and reuse it in get_pixbuf(), optimize? */
78 typedef void (*converter_rgb_t)(uint8_t* src, uint8_t* dest, int width);
79
convert_rgb_to_bgr(uint8_t * src,uint8_t * dest,int width)80 static void convert_rgb_to_bgr(uint8_t* src, uint8_t* dest, int width)
81 {
82 int x;
83
84 for (x = 0; x < width; x++) {
85 *dest++ = src[2];
86 *dest++ = src[1];
87 *dest++ = src[0];
88 src += 3;
89 }
90 }
91
convert_rgb_to_bgrx(uint8_t * src,uint8_t * dest,int width)92 static void convert_rgb_to_bgrx(uint8_t* src, uint8_t* dest, int width)
93 {
94 int x;
95
96 for (x = 0; x < width; x++) {
97 *dest++ = src[2];
98 *dest++ = src[1];
99 *dest++ = src[0];
100 *dest++ = 0;
101 src += 3;
102 }
103 }
104
decode(SpiceJpegDecoder * decoder,uint8_t * dest,int stride,int format)105 static void decode(SpiceJpegDecoder *decoder,
106 uint8_t* dest, int stride, int format)
107 {
108 GlibJpegDecoder *d = SPICE_CONTAINEROF(decoder, GlibJpegDecoder, base);
109 uint8_t* scan_line = g_alloca(d->_width * 3);
110 converter_rgb_t converter = NULL;
111 int row;
112
113 switch (format) {
114 case SPICE_BITMAP_FMT_24BIT:
115 converter = convert_rgb_to_bgr;
116 break;
117 case SPICE_BITMAP_FMT_32BIT:
118 converter = convert_rgb_to_bgrx;
119 break;
120 default:
121 g_warning("bad bitmap format, %d", format);
122 return;
123 }
124
125 g_return_if_fail(converter != NULL);
126
127 jpeg_start_decompress(&d->_cinfo);
128
129 for (row = 0; row < d->_height; row++) {
130 jpeg_read_scanlines(&d->_cinfo, &scan_line, 1);
131 converter(scan_line, dest, d->_width);
132 dest += stride;
133 }
134
135 jpeg_finish_decompress(&d->_cinfo);
136 }
137
138 static SpiceJpegDecoderOps jpeg_decoder_ops = {
139 .begin_decode = begin_decode,
140 .decode = decode,
141 };
142
jpeg_decoder_init_source(j_decompress_ptr cinfo)143 static void jpeg_decoder_init_source(j_decompress_ptr cinfo)
144 {
145 }
146
jpeg_decoder_fill_input_buffer(j_decompress_ptr cinfo)147 static boolean jpeg_decoder_fill_input_buffer(j_decompress_ptr cinfo)
148 {
149 g_warning("no more data for jpeg");
150 return FALSE;
151 }
152
jpeg_decoder_skip_input_data(j_decompress_ptr cinfo,long num_bytes)153 static void jpeg_decoder_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
154 {
155 g_return_if_fail(num_bytes < (long)cinfo->src->bytes_in_buffer);
156
157 cinfo->src->next_input_byte += num_bytes;
158 cinfo->src->bytes_in_buffer -= num_bytes;
159 }
160
jpeg_decoder_term_source(j_decompress_ptr cinfo)161 static void jpeg_decoder_term_source (j_decompress_ptr cinfo)
162 {
163 return;
164 }
165
jpeg_decoder_new(void)166 SpiceJpegDecoder *jpeg_decoder_new(void)
167 {
168 GlibJpegDecoder *d = g_new0(GlibJpegDecoder, 1);
169
170 d->_cinfo.err = jpeg_std_error(&d->_jerr);
171 jpeg_create_decompress(&d->_cinfo);
172
173 d->_cinfo.src = &d->_jsrc;
174 d->_cinfo.src->init_source = jpeg_decoder_init_source;
175 d->_cinfo.src->fill_input_buffer = jpeg_decoder_fill_input_buffer;
176 d->_cinfo.src->skip_input_data = jpeg_decoder_skip_input_data;
177 d->_cinfo.src->resync_to_restart = jpeg_resync_to_restart;
178 d->_cinfo.src->term_source = jpeg_decoder_term_source;
179
180 d->base.ops = &jpeg_decoder_ops;
181
182 return &d->base;
183 }
184
jpeg_decoder_destroy(SpiceJpegDecoder * decoder)185 void jpeg_decoder_destroy(SpiceJpegDecoder *decoder)
186 {
187 GlibJpegDecoder *d = SPICE_CONTAINEROF(decoder, GlibJpegDecoder, base);
188
189 jpeg_destroy_decompress(&d->_cinfo);
190 g_free(d);
191 }
192