JPEGImageEncoder.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2009 Torch Mobile, Inc. All rights reserved.
  3. * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include "config.h"
  21. #include "JPEGImageEncoder.h"
  22. #include "IntSize.h"
  23. // FIXME: jpeglib.h requires stdio.h to be included first for FILE
  24. #include <stdio.h>
  25. #include "jpeglib.h"
  26. #include <setjmp.h>
  27. namespace WebCore {
  28. class JPEGDestinationManager : public jpeg_destination_mgr {
  29. public:
  30. explicit JPEGDestinationManager(Vector<char>& toDump)
  31. : m_dump(toDump)
  32. {
  33. // Zero base class memory.
  34. jpeg_destination_mgr* base = this;
  35. memset(base, 0, sizeof(jpeg_destination_mgr));
  36. }
  37. Vector<char> m_buffer;
  38. Vector<char>& m_dump;
  39. };
  40. class JPEGCompressErrorMgr : public jpeg_error_mgr {
  41. public:
  42. JPEGCompressErrorMgr()
  43. {
  44. // Zero memory
  45. memset(this, 0, sizeof(JPEGCompressErrorMgr));
  46. }
  47. jmp_buf m_setjmpBuffer;
  48. };
  49. static void jpegInitializeDestination(j_compress_ptr compressData)
  50. {
  51. JPEGDestinationManager* dest = static_cast<JPEGDestinationManager*>(compressData->dest);
  52. dest->m_buffer.resize(4096);
  53. dest->next_output_byte = reinterpret_cast<JOCTET*>(dest->m_buffer.data());
  54. dest->free_in_buffer = dest->m_buffer.size();
  55. }
  56. static boolean jpegEmptyOutputBuffer(j_compress_ptr compressData)
  57. {
  58. JPEGDestinationManager* dest = static_cast<JPEGDestinationManager*>(compressData->dest);
  59. dest->m_dump.append(dest->m_buffer.data(), dest->m_buffer.size());
  60. dest->next_output_byte = reinterpret_cast<JOCTET*>(dest->m_buffer.data());
  61. dest->free_in_buffer = dest->m_buffer.size();
  62. return TRUE;
  63. }
  64. static void jpegTerminateDestination(j_compress_ptr compressData)
  65. {
  66. JPEGDestinationManager* dest = static_cast<JPEGDestinationManager*>(compressData->dest);
  67. dest->m_dump.append(dest->m_buffer.data(), dest->m_buffer.size() - dest->free_in_buffer);
  68. }
  69. static void jpegErrorExit(j_common_ptr compressData)
  70. {
  71. JPEGCompressErrorMgr* err = static_cast<JPEGCompressErrorMgr*>(compressData->err);
  72. longjmp(err->m_setjmpBuffer, -1);
  73. }
  74. bool compressRGBABigEndianToJPEG(unsigned char* rgbaBigEndianData, const IntSize& size, Vector<char>& jpegData, const double* quality)
  75. {
  76. struct jpeg_compress_struct compressData;
  77. JPEGCompressErrorMgr err;
  78. compressData.err = jpeg_std_error(&err);
  79. err.error_exit = jpegErrorExit;
  80. jpeg_create_compress(&compressData);
  81. JPEGDestinationManager dest(jpegData);
  82. compressData.dest = &dest;
  83. dest.init_destination = jpegInitializeDestination;
  84. dest.empty_output_buffer = jpegEmptyOutputBuffer;
  85. dest.term_destination = jpegTerminateDestination;
  86. compressData.image_width = size.width();
  87. compressData.image_height = size.height();
  88. compressData.input_components = 3;
  89. compressData.in_color_space = JCS_RGB;
  90. jpeg_set_defaults(&compressData);
  91. int compressionQuality = 65;
  92. if (quality && *quality >= 0.0 && *quality <= 1.0)
  93. compressionQuality = static_cast<int>(*quality * 100 + 0.5);
  94. jpeg_set_quality(&compressData, compressionQuality, FALSE);
  95. // rowBuffer must be defined here so that its destructor is always called even when "setjmp" catches an error.
  96. Vector<JSAMPLE, 600 * 3> rowBuffer;
  97. if (setjmp(err.m_setjmpBuffer))
  98. return false;
  99. jpeg_start_compress(&compressData, TRUE);
  100. rowBuffer.resize(compressData.image_width * 3);
  101. const unsigned char* pixel = rgbaBigEndianData;
  102. const unsigned char* pixelEnd = pixel + compressData.image_width * compressData.image_height * 4;
  103. while (pixel < pixelEnd) {
  104. JSAMPLE* output = rowBuffer.data();
  105. for (const unsigned char* rowEnd = pixel + compressData.image_width * 4; pixel < rowEnd;) {
  106. *output++ = static_cast<JSAMPLE>(*pixel++ & 0xFF); // red
  107. *output++ = static_cast<JSAMPLE>(*pixel++ & 0xFF); // green
  108. *output++ = static_cast<JSAMPLE>(*pixel++ & 0xFF); // blue
  109. ++pixel; // skip alpha
  110. }
  111. output = rowBuffer.data();
  112. jpeg_write_scanlines(&compressData, &output, 1);
  113. }
  114. jpeg_finish_compress(&compressData);
  115. return true;
  116. }
  117. } // namespace WebCore