DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 1 | /* 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 Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame^] | 4 | * Copyright 2014 Pierre Ossman for Cendio AB |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 5 | * |
| 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> |
DRC | 374b4d5 | 2011-11-03 19:26:14 +0000 | [diff] [blame] | 26 | #include <os/print.h> |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 27 | |
| 28 | #include <stdio.h> |
| 29 | extern "C" { |
| 30 | #include <jpeglib.h> |
| 31 | } |
| 32 | #include <jerror.h> |
| 33 | #include <setjmp.h> |
| 34 | |
| 35 | using namespace rfb; |
| 36 | |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame^] | 37 | // |
| 38 | // Special formats that libjpeg can have optimised code paths for |
| 39 | // |
| 40 | |
| 41 | static const PixelFormat pfRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16); |
| 42 | static const PixelFormat pfBGRX(32, 24, false, true, 255, 255, 255, 16, 8, 0); |
| 43 | static const PixelFormat pfXRGB(32, 24, false, true, 255, 255, 255, 8, 16, 24); |
| 44 | static const PixelFormat pfXBGR(32, 24, false, true, 255, 255, 255, 24, 16, 8); |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 45 | |
| 46 | // |
| 47 | // Error manager implmentation for the JPEG library |
| 48 | // |
| 49 | |
| 50 | struct JPEG_ERROR_MGR { |
| 51 | struct jpeg_error_mgr pub; |
| 52 | jmp_buf jmpBuffer; |
| 53 | char lastError[JMSG_LENGTH_MAX]; |
| 54 | }; |
| 55 | |
| 56 | static void |
| 57 | JpegErrorExit(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 | |
| 65 | static void |
| 66 | JpegOutputMessage(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 | |
| 78 | struct JPEG_SRC_MGR { |
| 79 | struct jpeg_source_mgr pub; |
| 80 | JpegDecompressor *instance; |
| 81 | }; |
| 82 | |
| 83 | static void |
| 84 | JpegNoOp(j_decompress_ptr dinfo) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | static boolean |
| 89 | JpegFillInputBuffer(j_decompress_ptr dinfo) |
| 90 | { |
| 91 | ERREXIT(dinfo, JERR_BUFFER_SIZE); |
| 92 | return TRUE; |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | JpegSkipInputData(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 | |
| 108 | JpegDecompressor::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 | |
| 135 | JpegDecompressor::~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 | |
| 150 | void JpegDecompressor::decompress(const rdr::U8 *jpegBuf, int jpegBufLen, |
Pierre Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 151 | rdr::U8 *buf, int stride, const Rect& r, const PixelFormat& pf) |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 152 | { |
| 153 | int w = r.width(); |
| 154 | int h = r.height(); |
| 155 | int pixelsize; |
Pierre Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 156 | int dstBufStride; |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 157 | 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 Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 175 | if (stride == 0) |
| 176 | stride = w; |
| 177 | dstBufStride = stride; |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 178 | |
| 179 | #ifdef JCS_EXTENSIONS |
| 180 | // Try to have libjpeg output directly to our native format |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame^] | 181 | // 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; |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 190 | |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame^] | 191 | if (dinfo->out_color_space != JCS_RGB) { |
| 192 | dstBuf = (rdr::U8 *)buf; |
| 193 | pixelsize = 4; |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 194 | } |
| 195 | #endif |
| 196 | |
| 197 | if (dinfo->out_color_space == JCS_RGB) { |
| 198 | dstBuf = new rdr::U8[w * h * pixelsize]; |
| 199 | dstBufIsTemp = true; |
Pierre Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 200 | dstBufStride = w; |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | rowPointer = new JSAMPROW[h]; |
| 204 | for (int dy = 0; dy < h; dy++) |
Pierre Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 205 | rowPointer[dy] = (JSAMPROW)(&dstBuf[dy * dstBufStride * pixelsize]); |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 206 | |
| 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 Ossman | a10d8fe | 2014-01-22 11:28:05 +0100 | [diff] [blame] | 224 | pf.bufferFromRGB((rdr::U8*)buf, dstBuf, w, stride, h); |
DRC | 7c2a39c | 2011-11-03 18:51:00 +0000 | [diff] [blame] | 225 | |
| 226 | jpeg_finish_decompress(dinfo); |
| 227 | |
| 228 | if (dstBufIsTemp) delete [] dstBuf; |
| 229 | delete[] rowPointer; |
| 230 | } |