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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 uint8_t *buf, volatile int stride,
  128. const Rect& r, const PixelFormat& pf,
  129. int quality, int subsamp)
  130. {
  131. int w = r.width();
  132. int h = r.height();
  133. int pixelsize;
  134. uint8_t * volatile srcBuf = NULL;
  135. volatile bool srcBufIsTemp = false;
  136. JSAMPROW * volatile rowPointer = NULL;
  137. if(setjmp(err->jmpBuffer)) {
  138. // this will execute if libjpeg has an error
  139. jpeg_abort_compress(cinfo);
  140. if (srcBufIsTemp && srcBuf) delete[] srcBuf;
  141. if (rowPointer) delete[] rowPointer;
  142. throw rdr::Exception("%s", err->lastError);
  143. }
  144. cinfo->image_width = w;
  145. cinfo->image_height = h;
  146. cinfo->in_color_space = JCS_RGB;
  147. pixelsize = 3;
  148. #ifdef JCS_EXTENSIONS
  149. // Try to have libjpeg output directly to our native format
  150. // libjpeg can only handle some "standard" formats
  151. if (pfRGBX == pf)
  152. cinfo->in_color_space = JCS_EXT_RGBX;
  153. else if (pfBGRX == pf)
  154. cinfo->in_color_space = JCS_EXT_BGRX;
  155. else if (pfXRGB == pf)
  156. cinfo->in_color_space = JCS_EXT_XRGB;
  157. else if (pfXBGR == pf)
  158. cinfo->in_color_space = JCS_EXT_XBGR;
  159. if (cinfo->in_color_space != JCS_RGB) {
  160. srcBuf = (uint8_t *)buf;
  161. pixelsize = 4;
  162. }
  163. #endif
  164. if (stride == 0)
  165. stride = w;
  166. if (cinfo->in_color_space == JCS_RGB) {
  167. srcBuf = new uint8_t[w * h * pixelsize];
  168. srcBufIsTemp = true;
  169. pf.rgbFromBuffer(srcBuf, (const uint8_t *)buf, w, stride, h);
  170. stride = w;
  171. }
  172. cinfo->input_components = pixelsize;
  173. jpeg_set_defaults(cinfo);
  174. if (quality >= 1 && quality <= 100) {
  175. jpeg_set_quality(cinfo, quality, TRUE);
  176. if (quality >= 96)
  177. cinfo->dct_method = JDCT_ISLOW;
  178. else
  179. cinfo->dct_method = JDCT_FASTEST;
  180. }
  181. switch (subsamp) {
  182. case subsample16X:
  183. case subsample8X:
  184. // FIXME (fall through)
  185. case subsample4X:
  186. cinfo->comp_info[0].h_samp_factor = 2;
  187. cinfo->comp_info[0].v_samp_factor = 2;
  188. break;
  189. case subsample2X:
  190. cinfo->comp_info[0].h_samp_factor = 2;
  191. cinfo->comp_info[0].v_samp_factor = 1;
  192. break;
  193. case subsampleGray:
  194. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  195. // fall through
  196. default:
  197. cinfo->comp_info[0].h_samp_factor = 1;
  198. cinfo->comp_info[0].v_samp_factor = 1;
  199. }
  200. rowPointer = new JSAMPROW[h];
  201. for (int dy = 0; dy < h; dy++)
  202. rowPointer[dy] = (JSAMPROW)(&srcBuf[dy * stride * pixelsize]);
  203. jpeg_start_compress(cinfo, TRUE);
  204. while (cinfo->next_scanline < cinfo->image_height)
  205. jpeg_write_scanlines(cinfo, &rowPointer[cinfo->next_scanline],
  206. cinfo->image_height - cinfo->next_scanline);
  207. jpeg_finish_compress(cinfo);
  208. if (srcBufIsTemp) delete[] srcBuf;
  209. delete[] rowPointer;
  210. }
  211. void JpegCompressor::writeBytes(const uint8_t* /*data*/, int /*length*/)
  212. {
  213. throw rdr::Exception("writeBytes() is not valid with a JpegCompressor instance. Use compress() instead.");
  214. }