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

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. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #include <rfb/JpegDecompressor.h>
  21. #include <rdr/Exception.h>
  22. #include <rfb/Rect.h>
  23. #include <rfb/PixelFormat.h>
  24. #include <stdio.h>
  25. extern "C" {
  26. #include <jpeglib.h>
  27. }
  28. #include <jerror.h>
  29. #include <setjmp.h>
  30. using namespace rfb;
  31. //
  32. // Error manager implmentation for the JPEG library
  33. //
  34. struct JPEG_ERROR_MGR {
  35. struct jpeg_error_mgr pub;
  36. jmp_buf jmpBuffer;
  37. char lastError[JMSG_LENGTH_MAX];
  38. };
  39. static void
  40. JpegErrorExit(j_common_ptr dinfo)
  41. {
  42. JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)dinfo->err;
  43. (*dinfo->err->output_message)(dinfo);
  44. longjmp(err->jmpBuffer, 1);
  45. }
  46. static void
  47. JpegOutputMessage(j_common_ptr dinfo)
  48. {
  49. JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)dinfo->err;
  50. (*dinfo->err->format_message)(dinfo, err->lastError);
  51. }
  52. //
  53. // Source manager implementation for the JPEG library.
  54. //
  55. struct JPEG_SRC_MGR {
  56. struct jpeg_source_mgr pub;
  57. JpegDecompressor *instance;
  58. };
  59. static void
  60. JpegNoOp(j_decompress_ptr dinfo)
  61. {
  62. }
  63. static boolean
  64. JpegFillInputBuffer(j_decompress_ptr dinfo)
  65. {
  66. ERREXIT(dinfo, JERR_BUFFER_SIZE);
  67. return TRUE;
  68. }
  69. static void
  70. JpegSkipInputData(j_decompress_ptr dinfo, long num_bytes)
  71. {
  72. JPEG_SRC_MGR *src = (JPEG_SRC_MGR *)dinfo->src;
  73. if (num_bytes < 0 || (size_t)num_bytes > src->pub.bytes_in_buffer) {
  74. ERREXIT(dinfo, JERR_BUFFER_SIZE);
  75. } else {
  76. src->pub.next_input_byte += (size_t) num_bytes;
  77. src->pub.bytes_in_buffer -= (size_t) num_bytes;
  78. }
  79. }
  80. JpegDecompressor::JpegDecompressor(void)
  81. {
  82. dinfo = new jpeg_decompress_struct;
  83. err = new struct JPEG_ERROR_MGR;
  84. dinfo->err = jpeg_std_error(&err->pub);
  85. snprintf(err->lastError, JMSG_LENGTH_MAX, "No error");
  86. err->pub.error_exit = JpegErrorExit;
  87. err->pub.output_message = JpegOutputMessage;
  88. if(setjmp(err->jmpBuffer)) {
  89. // this will execute if libjpeg has an error
  90. throw rdr::Exception(err->lastError);
  91. }
  92. jpeg_create_decompress(dinfo);
  93. src = new struct JPEG_SRC_MGR;
  94. src->pub.init_source = JpegNoOp;
  95. src->pub.fill_input_buffer = JpegFillInputBuffer;
  96. src->pub.skip_input_data = JpegSkipInputData;
  97. src->pub.resync_to_restart = jpeg_resync_to_restart;
  98. src->pub.term_source = JpegNoOp;
  99. src->instance = this;
  100. dinfo->src = (struct jpeg_source_mgr *)src;
  101. }
  102. JpegDecompressor::~JpegDecompressor(void)
  103. {
  104. if(setjmp(err->jmpBuffer)) {
  105. // this will execute if libjpeg has an error
  106. return;
  107. }
  108. jpeg_destroy_decompress(dinfo);
  109. delete err;
  110. delete src;
  111. delete dinfo;
  112. }
  113. void JpegDecompressor::decompress(const rdr::U8 *jpegBuf, int jpegBufLen,
  114. rdr::U8 *buf, int pitch, const Rect& r, const PixelFormat& pf)
  115. {
  116. int w = r.width();
  117. int h = r.height();
  118. int pixelsize;
  119. int dstBufPitch;
  120. rdr::U8 *dstBuf = NULL;
  121. bool dstBufIsTemp = false;
  122. JSAMPROW *rowPointer = NULL;
  123. if(setjmp(err->jmpBuffer)) {
  124. // this will execute if libjpeg has an error
  125. jpeg_abort_decompress(dinfo);
  126. if (dstBufIsTemp && dstBuf) delete[] dstBuf;
  127. if (rowPointer) delete[] rowPointer;
  128. throw rdr::Exception(err->lastError);
  129. }
  130. src->pub.next_input_byte = jpegBuf;
  131. src->pub.bytes_in_buffer = jpegBufLen;
  132. jpeg_read_header(dinfo, TRUE);
  133. dinfo->out_color_space = JCS_RGB;
  134. pixelsize = 3;
  135. if (pitch == 0) pitch = w * pf.bpp / 8;
  136. dstBufPitch = pitch;
  137. #ifdef JCS_EXTENSIONS
  138. // Try to have libjpeg output directly to our native format
  139. if (pf.is888()) {
  140. int redShift, greenShift, blueShift;
  141. if(pf.bigEndian) {
  142. redShift = 24 - pf.redShift;
  143. greenShift = 24 - pf.greenShift;
  144. blueShift = 24 - pf.blueShift;
  145. } else {
  146. redShift = pf.redShift;
  147. greenShift = pf.greenShift;
  148. blueShift = pf.blueShift;
  149. }
  150. // libjpeg can only handle some "standard" formats
  151. if(redShift == 0 && greenShift == 8 && blueShift == 16)
  152. dinfo->out_color_space = JCS_EXT_RGBX;
  153. if(redShift == 16 && greenShift == 8 && blueShift == 0)
  154. dinfo->out_color_space = JCS_EXT_BGRX;
  155. if(redShift == 24 && greenShift == 16 && blueShift == 8)
  156. dinfo->out_color_space = JCS_EXT_XBGR;
  157. if(redShift == 8 && greenShift == 16 && blueShift == 24)
  158. dinfo->out_color_space = JCS_EXT_XRGB;
  159. if (dinfo->out_color_space != JCS_RGB) {
  160. dstBuf = (rdr::U8 *)buf;
  161. pixelsize = 4;
  162. }
  163. }
  164. #endif
  165. if (dinfo->out_color_space == JCS_RGB) {
  166. dstBuf = new rdr::U8[w * h * pixelsize];
  167. dstBufIsTemp = true;
  168. dstBufPitch = w * pixelsize;
  169. }
  170. rowPointer = new JSAMPROW[h];
  171. for (int dy = 0; dy < h; dy++)
  172. rowPointer[dy] = (JSAMPROW)(&dstBuf[dy * dstBufPitch]);
  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, pitch, h);
  188. jpeg_finish_decompress(dinfo);
  189. if (dstBufIsTemp) delete [] dstBuf;
  190. delete[] rowPointer;
  191. }