PNGImageEncoder.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2006-2009, Google Inc. All rights reserved.
  3. * Copyright (c) 2009 Torch Mobile, Inc. All rights reserved.
  4. * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "config.h"
  33. #include "PNGImageEncoder.h"
  34. #include "IntSize.h"
  35. #include "png.h"
  36. #include <wtf/Vector.h>
  37. namespace WebCore {
  38. // Encoder --------------------------------------------------------------------
  39. //
  40. // This section of the code is based on nsPNGEncoder.cpp in Mozilla
  41. // (Copyright 2005 Google Inc.)
  42. // Passed around as the io_ptr in the png structs so our callbacks know where
  43. // to write data.
  44. struct PNGEncoderState {
  45. PNGEncoderState(Vector<char>* o) : m_dump(o) {}
  46. Vector<char>* m_dump;
  47. };
  48. // Called by libpng to flush its internal buffer to ours.
  49. void encoderWriteCallback(png_structp png, png_bytep data, png_size_t size)
  50. {
  51. PNGEncoderState* state = static_cast<PNGEncoderState*>(png_get_io_ptr(png));
  52. ASSERT(state->m_dump);
  53. size_t oldSize = state->m_dump->size();
  54. state->m_dump->resize(oldSize + size);
  55. char* destination = state->m_dump->data() + oldSize;
  56. memcpy(destination, data, size);
  57. }
  58. // Automatically destroys the given write structs on destruction to make
  59. // cleanup and error handling code cleaner.
  60. class PNGWriteStructDestroyer {
  61. public:
  62. PNGWriteStructDestroyer(png_struct** ps, png_info** pi)
  63. : m_pngStruct(ps)
  64. , m_pngInfo(pi)
  65. {
  66. }
  67. ~PNGWriteStructDestroyer()
  68. {
  69. png_destroy_write_struct(m_pngStruct, m_pngInfo);
  70. }
  71. private:
  72. png_struct** m_pngStruct;
  73. png_info** m_pngInfo;
  74. };
  75. bool compressRGBABigEndianToPNG(unsigned char* rgbaBigEndianData, const IntSize& size, Vector<char>& pngData)
  76. {
  77. png_struct* pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
  78. if (!pngPtr)
  79. return false;
  80. png_info* infoPtr = png_create_info_struct(pngPtr);
  81. if (!infoPtr) {
  82. png_destroy_write_struct(&pngPtr, 0);
  83. return false;
  84. }
  85. PNGWriteStructDestroyer destroyer(&pngPtr, &infoPtr);
  86. // The destroyer will ensure that the structures are cleaned up in this
  87. // case, even though we may get here as a jump from random parts of the
  88. // PNG library called below.
  89. if (setjmp(png_jmpbuf(pngPtr)))
  90. return false;
  91. // Set our callback for libpng to give us the data.
  92. PNGEncoderState state(&pngData);
  93. png_set_write_fn(pngPtr, &state, encoderWriteCallback, 0);
  94. int pngOutputColorType = PNG_COLOR_TYPE_RGB_ALPHA;
  95. png_set_IHDR(pngPtr, infoPtr, size.width(), size.height(), 8, pngOutputColorType,
  96. PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
  97. PNG_FILTER_TYPE_DEFAULT);
  98. png_write_info(pngPtr, infoPtr);
  99. unsigned bytesPerRow = size.width() * 4;
  100. for (unsigned y = 0; y < size.height(); ++y) {
  101. png_write_row(pngPtr, rgbaBigEndianData);
  102. rgbaBigEndianData += bytesPerRow;
  103. }
  104. png_write_end(pngPtr, infoPtr);
  105. return true;
  106. }
  107. } // namespace WebCore