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.

JpegCompressor.cxx 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2014 Pierre Ossman for Cendio AB
  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. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <rfb/JpegCompressor.h>
  24. #include <rdr/Exception.h>
  25. #include <rfb/Rect.h>
  26. #include <rfb/PixelFormat.h>
  27. #include <rfb/ClientParams.h>
  28. #include <stdio.h>
  29. extern "C" {
  30. #include <jpeglib.h>
  31. }
  32. #include <setjmp.h>
  33. using namespace rfb;
  34. //
  35. // Special formats that libjpeg can have optimised code paths for
  36. //
  37. static const PixelFormat pfRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16);
  38. static const PixelFormat pfBGRX(32, 24, false, true, 255, 255, 255, 16, 8, 0);
  39. static const PixelFormat pfXRGB(32, 24, false, true, 255, 255, 255, 8, 16, 24);
  40. static const PixelFormat pfXBGR(32, 24, false, true, 255, 255, 255, 24, 16, 8);
  41. //
  42. // Error manager implementation for the JPEG library
  43. //
  44. struct JPEG_ERROR_MGR {
  45. struct jpeg_error_mgr pub;
  46. jmp_buf jmpBuffer;
  47. char lastError[JMSG_LENGTH_MAX];
  48. };
  49. static void
  50. JpegErrorExit(j_common_ptr cinfo)
  51. {
  52. JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)cinfo->err;
  53. (*cinfo->err->output_message)(cinfo);
  54. longjmp(err->jmpBuffer, 1);
  55. }
  56. static void
  57. JpegOutputMessage(j_common_ptr cinfo)
  58. {
  59. JPEG_ERROR_MGR *err = (JPEG_ERROR_MGR *)cinfo->err;
  60. (*cinfo->err->format_message)(cinfo, err->lastError);
  61. }
  62. //
  63. // Destination manager implementation for the JPEG library.
  64. //
  65. struct JPEG_DEST_MGR {
  66. struct jpeg_destination_mgr pub;
  67. JpegCompressor *instance;
  68. size_t chunkSize;
  69. };
  70. static void
  71. JpegInitDestination(j_compress_ptr cinfo)
  72. {
  73. JPEG_DEST_MGR *dest = (JPEG_DEST_MGR *)cinfo->dest;
  74. JpegCompressor *jc = dest->instance;
  75. jc->clear();
  76. dest->pub.next_output_byte = jc->getptr(jc->length());
  77. dest->pub.free_in_buffer = dest->chunkSize = jc->avail();
  78. }
  79. static boolean
  80. JpegEmptyOutputBuffer(j_compress_ptr cinfo)
  81. {
  82. JPEG_DEST_MGR *dest = (JPEG_DEST_MGR *)cinfo->dest;
  83. JpegCompressor *jc = dest->instance;
  84. jc->setptr(jc->avail());
  85. dest->pub.next_output_byte = jc->getptr(jc->length());
  86. dest->pub.free_in_buffer = dest->chunkSize = jc->avail();
  87. return TRUE;
  88. }
  89. static void
  90. JpegTermDestination(j_compress_ptr cinfo)
  91. {
  92. JPEG_DEST_MGR *dest = (JPEG_DEST_MGR *)cinfo->dest;
  93. JpegCompressor *jc = dest->instance;
  94. jc->setptr(dest->chunkSize - dest->pub.free_in_buffer);
  95. }
  96. JpegCompressor::JpegCompressor(int bufferLen) : MemOutStream(bufferLen)
  97. {
  98. cinfo = new jpeg_compress_struct;
  99. err = new struct JPEG_ERROR_MGR;
  100. cinfo->err = jpeg_std_error(&err->pub);
  101. snprintf(err->lastError, JMSG_LENGTH_MAX, "No error");
  102. err->pub.error_exit = JpegErrorExit;
  103. err->pub.output_message = JpegOutputMessage;
  104. if(setjmp(err->jmpBuffer)) {
  105. // this will execute if libjpeg has an error
  106. throw rdr::Exception("%s", err->lastError);
  107. }
  108. jpeg_create_compress(cinfo);
  109. dest = new struct JPEG_DEST_MGR;
  110. dest->pub.init_destination = JpegInitDestination;
  111. dest->pub.empty_output_buffer = JpegEmptyOutputBuffer;
  112. dest->pub.term_destination = JpegTermDestination;
  113. dest->instance = this;
  114. cinfo->dest = (struct jpeg_destination_mgr *)dest;
  115. }
  116. JpegCompressor::~JpegCompressor(void)
  117. {
  118. if(setjmp(err->jmpBuffer)) {
  119. // this will execute if libjpeg has an error
  120. return;
  121. }
  122. jpeg_destroy_compress(cinfo);
  123. delete err;
  124. delete dest;
  125. delete cinfo;
  126. }
  127. void JpegCompressor::compress(const rdr::U8 *buf, int stride, const Rect& r,
  128. const PixelFormat& pf, int quality, int subsamp)
  129. {
  130. int w = r.width();
  131. int h = r.height();
  132. int pixelsize;
  133. rdr::U8 *srcBuf = NULL;
  134. bool srcBufIsTemp = false;
  135. JSAMPROW *rowPointer = NULL;
  136. if(setjmp(err->jmpBuffer)) {
  137. // this will execute if libjpeg has an error
  138. jpeg_abort_compress(cinfo);
  139. if (srcBufIsTemp && srcBuf) delete[] srcBuf;
  140. if (rowPointer) delete[] rowPointer;
  141. throw rdr::Exception("%s", err->lastError);
  142. }
  143. cinfo->image_width = w;
  144. cinfo->image_height = h;
  145. cinfo->in_color_space = JCS_RGB;
  146. pixelsize = 3;
  147. #ifdef JCS_EXTENSIONS
  148. // Try to have libjpeg output directly to our native format
  149. // libjpeg can only handle some "standard" formats
  150. if (pfRGBX.equal(pf))
  151. cinfo->in_color_space = JCS_EXT_RGBX;
  152. else if (pfBGRX.equal(pf))
  153. cinfo->in_color_space = JCS_EXT_BGRX;
  154. else if (pfXRGB.equal(pf))
  155. cinfo->in_color_space = JCS_EXT_XRGB;
  156. else if (pfXBGR.equal(pf))
  157. cinfo->in_color_space = JCS_EXT_XBGR;
  158. if (cinfo->in_color_space != JCS_RGB) {
  159. srcBuf = (rdr::U8 *)buf;
  160. pixelsize = 4;
  161. }
  162. #endif
  163. if (stride == 0)
  164. stride = w;
  165. if (cinfo->in_color_space == JCS_RGB) {
  166. srcBuf = new rdr::U8[w * h * pixelsize];
  167. srcBufIsTemp = true;
  168. pf.rgbFromBuffer(srcBuf, (const rdr::U8 *)buf, w, stride, h);
  169. stride = w;
  170. }
  171. cinfo->input_components = pixelsize;
  172. jpeg_set_defaults(cinfo);
  173. if (quality >= 1 && quality <= 100) {
  174. jpeg_set_quality(cinfo, quality, TRUE);
  175. if (quality >= 96)
  176. cinfo->dct_method = JDCT_ISLOW;
  177. else
  178. cinfo->dct_method = JDCT_FASTEST;
  179. }
  180. switch (subsamp) {
  181. case subsample16X:
  182. case subsample8X:
  183. // FIXME (fall through)
  184. case subsample4X:
  185. cinfo->comp_info[0].h_samp_factor = 2;
  186. cinfo->comp_info[0].v_samp_factor = 2;
  187. break;
  188. case subsample2X:
  189. cinfo->comp_info[0].h_samp_factor = 2;
  190. cinfo->comp_info[0].v_samp_factor = 1;
  191. break;
  192. case subsampleGray:
  193. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  194. default:
  195. cinfo->comp_info[0].h_samp_factor = 1;
  196. cinfo->comp_info[0].v_samp_factor = 1;
  197. }
  198. rowPointer = new JSAMPROW[h];
  199. for (int dy = 0; dy < h; dy++)
  200. rowPointer[dy] = (JSAMPROW)(&srcBuf[dy * stride * pixelsize]);
  201. jpeg_start_compress(cinfo, TRUE);
  202. while (cinfo->next_scanline < cinfo->image_height)
  203. jpeg_write_scanlines(cinfo, &rowPointer[cinfo->next_scanline],
  204. cinfo->image_height - cinfo->next_scanline);
  205. jpeg_finish_compress(cinfo);
  206. if (srcBufIsTemp) delete[] srcBuf;
  207. delete[] rowPointer;
  208. }
  209. void JpegCompressor::writeBytes(const void* /*data*/, int /*length*/)
  210. {
  211. throw rdr::Exception("writeBytes() is not valid with a JpegCompressor instance. Use compress() instead.");
  212. }