blob: 3f4d2d008d752159c8166d16c21fa0f082c7f41f [file] [log] [blame]
DRC7c2a39c2011-11-03 18:51:00 +00001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
2 * Copyright (C) 2004-2005 Cendio AB. All rights reserved.
3 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +01004 * Copyright 2014 Pierre Ossman for Cendio AB
DRC7c2a39c2011-11-03 18:51:00 +00005 *
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this software; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 */
21
22#include <rfb/JpegDecompressor.h>
23#include <rdr/Exception.h>
24#include <rfb/Rect.h>
25#include <rfb/PixelFormat.h>
DRC374b4d52011-11-03 19:26:14 +000026#include <os/print.h>
DRC7c2a39c2011-11-03 18:51:00 +000027
28#include <stdio.h>
29extern "C" {
30#include <jpeglib.h>
31}
32#include <jerror.h>
33#include <setjmp.h>
34
35using namespace rfb;
36
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +010037//
38// Special formats that libjpeg can have optimised code paths for
39//
40
41static const PixelFormat pfRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16);
42static const PixelFormat pfBGRX(32, 24, false, true, 255, 255, 255, 16, 8, 0);
43static const PixelFormat pfXRGB(32, 24, false, true, 255, 255, 255, 8, 16, 24);
44static const PixelFormat pfXBGR(32, 24, false, true, 255, 255, 255, 24, 16, 8);
DRC7c2a39c2011-11-03 18:51:00 +000045
46//
47// Error manager implmentation for the JPEG library
48//
49
50struct JPEG_ERROR_MGR {
51 struct jpeg_error_mgr pub;
52 jmp_buf jmpBuffer;
53 char lastError[JMSG_LENGTH_MAX];
54};
55
56static void
57JpegErrorExit(j_common_ptr dinfo)
58{
59 JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)dinfo->err;
60
61 (*dinfo->err->output_message)(dinfo);
62 longjmp(err->jmpBuffer, 1);
63}
64
65static void
66JpegOutputMessage(j_common_ptr dinfo)
67{
68 JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)dinfo->err;
69
70 (*dinfo->err->format_message)(dinfo, err->lastError);
71}
72
73
74//
75// Source manager implementation for the JPEG library.
76//
77
78struct JPEG_SRC_MGR {
79 struct jpeg_source_mgr pub;
80 JpegDecompressor *instance;
81};
82
83static void
84JpegNoOp(j_decompress_ptr dinfo)
85{
86}
87
88static boolean
89JpegFillInputBuffer(j_decompress_ptr dinfo)
90{
91 ERREXIT(dinfo, JERR_BUFFER_SIZE);
92 return TRUE;
93}
94
95static void
96JpegSkipInputData(j_decompress_ptr dinfo, long num_bytes)
97{
98 JPEG_SRC_MGR *src = (JPEG_SRC_MGR *)dinfo->src;
99
100 if (num_bytes < 0 || (size_t)num_bytes > src->pub.bytes_in_buffer) {
101 ERREXIT(dinfo, JERR_BUFFER_SIZE);
102 } else {
103 src->pub.next_input_byte += (size_t) num_bytes;
104 src->pub.bytes_in_buffer -= (size_t) num_bytes;
105 }
106}
107
108JpegDecompressor::JpegDecompressor(void)
109{
110 dinfo = new jpeg_decompress_struct;
111
112 err = new struct JPEG_ERROR_MGR;
113 dinfo->err = jpeg_std_error(&err->pub);
114 snprintf(err->lastError, JMSG_LENGTH_MAX, "No error");
115 err->pub.error_exit = JpegErrorExit;
116 err->pub.output_message = JpegOutputMessage;
117
118 if(setjmp(err->jmpBuffer)) {
119 // this will execute if libjpeg has an error
120 throw rdr::Exception(err->lastError);
121 }
122
123 jpeg_create_decompress(dinfo);
124
125 src = new struct JPEG_SRC_MGR;
126 src->pub.init_source = JpegNoOp;
127 src->pub.fill_input_buffer = JpegFillInputBuffer;
128 src->pub.skip_input_data = JpegSkipInputData;
129 src->pub.resync_to_restart = jpeg_resync_to_restart;
130 src->pub.term_source = JpegNoOp;
131 src->instance = this;
132 dinfo->src = (struct jpeg_source_mgr *)src;
133}
134
135JpegDecompressor::~JpegDecompressor(void)
136{
137 if(setjmp(err->jmpBuffer)) {
138 // this will execute if libjpeg has an error
139 return;
140 }
141
142 jpeg_destroy_decompress(dinfo);
143
144 delete err;
145 delete src;
146
147 delete dinfo;
148}
149
150void JpegDecompressor::decompress(const rdr::U8 *jpegBuf, int jpegBufLen,
Pierre Ossmana10d8fe2014-01-22 11:28:05 +0100151 rdr::U8 *buf, int stride, const Rect& r, const PixelFormat& pf)
DRC7c2a39c2011-11-03 18:51:00 +0000152{
153 int w = r.width();
154 int h = r.height();
155 int pixelsize;
Pierre Ossmana10d8fe2014-01-22 11:28:05 +0100156 int dstBufStride;
DRC7c2a39c2011-11-03 18:51:00 +0000157 rdr::U8 *dstBuf = NULL;
158 bool dstBufIsTemp = false;
159 JSAMPROW *rowPointer = NULL;
160
161 if(setjmp(err->jmpBuffer)) {
162 // this will execute if libjpeg has an error
163 jpeg_abort_decompress(dinfo);
164 if (dstBufIsTemp && dstBuf) delete[] dstBuf;
165 if (rowPointer) delete[] rowPointer;
166 throw rdr::Exception(err->lastError);
167 }
168
169 src->pub.next_input_byte = jpegBuf;
170 src->pub.bytes_in_buffer = jpegBufLen;
171
172 jpeg_read_header(dinfo, TRUE);
173 dinfo->out_color_space = JCS_RGB;
174 pixelsize = 3;
Pierre Ossmana10d8fe2014-01-22 11:28:05 +0100175 if (stride == 0)
176 stride = w;
177 dstBufStride = stride;
DRC7c2a39c2011-11-03 18:51:00 +0000178
179#ifdef JCS_EXTENSIONS
180 // Try to have libjpeg output directly to our native format
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100181 // libjpeg can only handle some "standard" formats
182 if (pfRGBX.equal(pf))
183 dinfo->out_color_space = JCS_EXT_RGBX;
184 else if (pfBGRX.equal(pf))
185 dinfo->out_color_space = JCS_EXT_BGRX;
186 else if (pfXRGB.equal(pf))
187 dinfo->out_color_space = JCS_EXT_XRGB;
188 else if (pfXBGR.equal(pf))
189 dinfo->out_color_space = JCS_EXT_XBGR;
DRC7c2a39c2011-11-03 18:51:00 +0000190
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100191 if (dinfo->out_color_space != JCS_RGB) {
192 dstBuf = (rdr::U8 *)buf;
193 pixelsize = 4;
DRC7c2a39c2011-11-03 18:51:00 +0000194 }
195#endif
196
197 if (dinfo->out_color_space == JCS_RGB) {
198 dstBuf = new rdr::U8[w * h * pixelsize];
199 dstBufIsTemp = true;
Pierre Ossmana10d8fe2014-01-22 11:28:05 +0100200 dstBufStride = w;
DRC7c2a39c2011-11-03 18:51:00 +0000201 }
202
203 rowPointer = new JSAMPROW[h];
204 for (int dy = 0; dy < h; dy++)
Pierre Ossmana10d8fe2014-01-22 11:28:05 +0100205 rowPointer[dy] = (JSAMPROW)(&dstBuf[dy * dstBufStride * pixelsize]);
DRC7c2a39c2011-11-03 18:51:00 +0000206
207 jpeg_start_decompress(dinfo);
208
209 if (dinfo->output_width != (unsigned)r.width()
210 || dinfo->output_height != (unsigned)r.height()
211 || dinfo->output_components != pixelsize) {
212 jpeg_abort_decompress(dinfo);
213 if (dstBufIsTemp && dstBuf) delete[] dstBuf;
214 if (rowPointer) delete[] rowPointer;
215 throw rdr::Exception("Tight Decoding: Wrong JPEG data received.\n");
216 }
217
218 while (dinfo->output_scanline < dinfo->output_height) {
219 jpeg_read_scanlines(dinfo, &rowPointer[dinfo->output_scanline],
220 dinfo->output_height - dinfo->output_scanline);
221 }
222
223 if (dinfo->out_color_space == JCS_RGB)
Pierre Ossmana10d8fe2014-01-22 11:28:05 +0100224 pf.bufferFromRGB((rdr::U8*)buf, dstBuf, w, stride, h);
DRC7c2a39c2011-11-03 18:51:00 +0000225
226 jpeg_finish_decompress(dinfo);
227
228 if (dstBufIsTemp) delete [] dstBuf;
229 delete[] rowPointer;
230}