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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 uint8_t *jpegBuf,
  125. int jpegBufLen, uint8_t *buf,
  126. volatile int stride,
  127. const Rect& r, const PixelFormat& pf)
  128. {
  129. int w = r.width();
  130. int h = r.height();
  131. int pixelsize;
  132. int dstBufStride;
  133. uint8_t * volatile dstBuf = NULL;
  134. volatile bool dstBufIsTemp = false;
  135. JSAMPROW * volatile rowPointer = NULL;
  136. if(setjmp(err->jmpBuffer)) {
  137. // this will execute if libjpeg has an error
  138. jpeg_abort_decompress(dinfo);
  139. if (dstBufIsTemp && dstBuf) delete[] dstBuf;
  140. if (rowPointer) delete[] rowPointer;
  141. throw rdr::Exception("%s", err->lastError);
  142. }
  143. src->pub.next_input_byte = jpegBuf;
  144. src->pub.bytes_in_buffer = jpegBufLen;
  145. jpeg_read_header(dinfo, TRUE);
  146. dinfo->out_color_space = JCS_RGB;
  147. pixelsize = 3;
  148. if (stride == 0)
  149. stride = w;
  150. dstBufStride = stride;
  151. #ifdef JCS_EXTENSIONS
  152. // Try to have libjpeg output directly to our native format
  153. // libjpeg can only handle some "standard" formats
  154. if (pfRGBX == pf)
  155. dinfo->out_color_space = JCS_EXT_RGBX;
  156. else if (pfBGRX == pf)
  157. dinfo->out_color_space = JCS_EXT_BGRX;
  158. else if (pfXRGB == pf)
  159. dinfo->out_color_space = JCS_EXT_XRGB;
  160. else if (pfXBGR == pf)
  161. dinfo->out_color_space = JCS_EXT_XBGR;
  162. if (dinfo->out_color_space != JCS_RGB) {
  163. dstBuf = (uint8_t *)buf;
  164. pixelsize = 4;
  165. }
  166. #endif
  167. if (dinfo->out_color_space == JCS_RGB) {
  168. dstBuf = new uint8_t[w * h * pixelsize];
  169. dstBufIsTemp = true;
  170. dstBufStride = w;
  171. }
  172. rowPointer = new JSAMPROW[h];
  173. for (int dy = 0; dy < h; dy++)
  174. rowPointer[dy] = (JSAMPROW)(&dstBuf[dy * dstBufStride * pixelsize]);
  175. jpeg_start_decompress(dinfo);
  176. if (dinfo->output_width != (unsigned)r.width()
  177. || dinfo->output_height != (unsigned)r.height()
  178. || dinfo->output_components != pixelsize) {
  179. jpeg_abort_decompress(dinfo);
  180. if (dstBufIsTemp && dstBuf) delete[] dstBuf;
  181. if (rowPointer) delete[] rowPointer;
  182. throw rdr::Exception("Tight Decoding: Wrong JPEG data received.\n");
  183. }
  184. while (dinfo->output_scanline < dinfo->output_height) {
  185. jpeg_read_scanlines(dinfo, &rowPointer[dinfo->output_scanline],
  186. dinfo->output_height - dinfo->output_scanline);
  187. }
  188. if (dinfo->out_color_space == JCS_RGB)
  189. pf.bufferFromRGB((uint8_t*)buf, dstBuf, w, stride, h);
  190. jpeg_finish_decompress(dinfo);
  191. if (dstBufIsTemp) delete [] dstBuf;
  192. delete[] rowPointer;
  193. }