You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JpegDecompressor.cxx 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.
  4. * Copyright 2014 Pierre Ossman for Cendio AB
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <rfb/JpegDecompressor.h>
  25. #include <rdr/Exception.h>
  26. #include <rfb/Rect.h>
  27. #include <rfb/PixelFormat.h>
  28. #include <stdio.h>
  29. extern "C" {
  30. #include <jpeglib.h>
  31. }
  32. #include <jerror.h>
  33. #include <setjmp.h>
  34. using namespace rfb;
  35. //
  36. // Special formats that libjpeg can have optimised code paths for
  37. //
  38. static const PixelFormat pfRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16);
  39. static const PixelFormat pfBGRX(32, 24, false, true, 255, 255, 255, 16, 8, 0);
  40. static const PixelFormat pfXRGB(32, 24, false, true, 255, 255, 255, 8, 16, 24);
  41. static const PixelFormat pfXBGR(32, 24, false, true, 255, 255, 255, 24, 16, 8);
  42. //
  43. // Error manager implementation for the JPEG library
  44. //
  45. struct JPEG_ERROR_MGR {
  46. struct jpeg_error_mgr pub;
  47. jmp_buf jmpBuffer;
  48. char lastError[JMSG_LENGTH_MAX];
  49. };
  50. static void
  51. JpegErrorExit(j_common_ptr dinfo)
  52. {
  53. JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)dinfo->err;
  54. (*dinfo->err->output_message)(dinfo);
  55. longjmp(err->jmpBuffer, 1);
  56. }
  57. static void
  58. JpegOutputMessage(j_common_ptr dinfo)
  59. {
  60. JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)dinfo->err;
  61. (*dinfo->err->format_message)(dinfo, err->lastError);
  62. }
  63. //
  64. // Source manager implementation for the JPEG library.
  65. //
  66. struct JPEG_SRC_MGR {
  67. struct jpeg_source_mgr pub;
  68. JpegDecompressor *instance;
  69. };
  70. static void
  71. JpegNoOp(j_decompress_ptr /*dinfo*/)
  72. {
  73. }
  74. static boolean
  75. JpegFillInputBuffer(j_decompress_ptr dinfo)
  76. {
  77. ERREXIT(dinfo, JERR_BUFFER_SIZE);
  78. return TRUE;
  79. }
  80. static void
  81. JpegSkipInputData(j_decompress_ptr dinfo, long num_bytes)
  82. {
  83. JPEG_SRC_MGR *src = (JPEG_SRC_MGR *)dinfo->src;
  84. if (num_bytes < 0 || (size_t)num_bytes > src->pub.bytes_in_buffer) {
  85. ERREXIT(dinfo, JERR_BUFFER_SIZE);
  86. } else {
  87. src->pub.next_input_byte += (size_t) num_bytes;
  88. src->pub.bytes_in_buffer -= (size_t) num_bytes;
  89. }
  90. }
  91. JpegDecompressor::JpegDecompressor(void)
  92. {
  93. dinfo = new jpeg_decompress_struct;
  94. err = new struct JPEG_ERROR_MGR;
  95. dinfo->err = jpeg_std_error(&err->pub);
  96. snprintf(err->lastError, JMSG_LENGTH_MAX, "No error");
  97. err->pub.error_exit = JpegErrorExit;
  98. err->pub.output_message = JpegOutputMessage;
  99. if(setjmp(err->jmpBuffer)) {
  100. // this will execute if libjpeg has an error
  101. throw rdr::Exception("%s", err->lastError);
  102. }
  103. jpeg_create_decompress(dinfo);
  104. src = new struct JPEG_SRC_MGR;
  105. src->pub.init_source = JpegNoOp;
  106. src->pub.fill_input_buffer = JpegFillInputBuffer;
  107. src->pub.skip_input_data = JpegSkipInputData;
  108. src->pub.resync_to_restart = jpeg_resync_to_restart;
  109. src->pub.term_source = JpegNoOp;
  110. src->instance = this;
  111. dinfo->src = (struct jpeg_source_mgr *)src;
  112. }
  113. JpegDecompressor::~JpegDecompressor(void)
  114. {
  115. if(setjmp(err->jmpBuffer)) {
  116. // this will execute if libjpeg has an error
  117. return;
  118. }
  119. jpeg_destroy_decompress(dinfo);
  120. delete err;
  121. delete src;
  122. delete dinfo;
  123. }
  124. void JpegDecompressor::decompress(const rdr::U8 *jpegBuf, int jpegBufLen,
  125. rdr::U8 *buf, int stride, const Rect& r, const PixelFormat& pf)
  126. {
  127. int w = r.width();
  128. int h = r.height();
  129. int pixelsize;
  130. int dstBufStride;
  131. rdr::U8 *dstBuf = NULL;
  132. bool dstBufIsTemp = false;
  133. JSAMPROW *rowPointer = NULL;
  134. if(setjmp(err->jmpBuffer)) {
  135. // this will execute if libjpeg has an error
  136. jpeg_abort_decompress(dinfo);
  137. if (dstBufIsTemp && dstBuf) delete[] dstBuf;
  138. if (rowPointer) delete[] rowPointer;
  139. throw rdr::Exception("%s", err->lastError);
  140. }
  141. src->pub.next_input_byte = jpegBuf;
  142. src->pub.bytes_in_buffer = jpegBufLen;
  143. jpeg_read_header(dinfo, TRUE);
  144. dinfo->out_color_space = JCS_RGB;
  145. pixelsize = 3;
  146. if (stride == 0)
  147. stride = w;
  148. dstBufStride = stride;
  149. #ifdef JCS_EXTENSIONS
  150. // Try to have libjpeg output directly to our native format
  151. // libjpeg can only handle some "standard" formats
  152. if (pfRGBX.equal(pf))
  153. dinfo->out_color_space = JCS_EXT_RGBX;
  154. else if (pfBGRX.equal(pf))
  155. dinfo->out_color_space = JCS_EXT_BGRX;
  156. else if (pfXRGB.equal(pf))
  157. dinfo->out_color_space = JCS_EXT_XRGB;
  158. else if (pfXBGR.equal(pf))
  159. dinfo->out_color_space = JCS_EXT_XBGR;
  160. if (dinfo->out_color_space != JCS_RGB) {
  161. dstBuf = (rdr::U8 *)buf;
  162. pixelsize = 4;
  163. }
  164. #endif
  165. if (dinfo->out_color_space == JCS_RGB) {
  166. dstBuf = new rdr::U8[w * h * pixelsize];
  167. dstBufIsTemp = true;
  168. dstBufStride = w;
  169. }
  170. rowPointer = new JSAMPROW[h];
  171. for (int dy = 0; dy < h; dy++)
  172. rowPointer[dy] = (JSAMPROW)(&dstBuf[dy * dstBufStride * pixelsize]);
  173. jpeg_start_decompress(dinfo);
  174. if (dinfo->output_width != (unsigned)r.width()
  175. || dinfo->output_height != (unsigned)r.height()
  176. || dinfo->output_components != pixelsize) {
  177. jpeg_abort_decompress(dinfo);
  178. if (dstBufIsTemp && dstBuf) delete[] dstBuf;
  179. if (rowPointer) delete[] rowPointer;
  180. throw rdr::Exception("Tight Decoding: Wrong JPEG data received.\n");
  181. }
  182. while (dinfo->output_scanline < dinfo->output_height) {
  183. jpeg_read_scanlines(dinfo, &rowPointer[dinfo->output_scanline],
  184. dinfo->output_height - dinfo->output_scanline);
  185. }
  186. if (dinfo->out_color_space == JCS_RGB)
  187. pf.bufferFromRGB((rdr::U8*)buf, dstBuf, w, stride, h);
  188. jpeg_finish_decompress(dinfo);
  189. if (dstBufIsTemp) delete [] dstBuf;
  190. delete[] rowPointer;
  191. }