| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010, Google Inc. All rights reserved. | 2 * Copyright (c) 2010, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "platform/image-encoders/PNGImageEncoder.h" | 31 #include "platform/image-encoders/PNGImageEncoder.h" |
| 32 | 32 |
| 33 #include "platform/graphics/ImageBuffer.h" | 33 #include "platform/graphics/ImageBuffer.h" |
| 34 #include "wtf/PtrUtil.h" | 34 #include "wtf/OwnPtr.h" |
| 35 #include <memory> | |
| 36 | 35 |
| 37 namespace blink { | 36 namespace blink { |
| 38 | 37 |
| 39 PNGImageEncoderState::~PNGImageEncoderState() | 38 PNGImageEncoderState::~PNGImageEncoderState() |
| 40 { | 39 { |
| 41 png_destroy_write_struct(&m_png, &m_info); | 40 png_destroy_write_struct(&m_png, &m_info); |
| 42 } | 41 } |
| 43 | 42 |
| 44 static void writeOutput(png_structp png, png_bytep data, png_size_t size) | 43 static void writeOutput(png_structp png, png_bytep data, png_size_t size) |
| 45 { | 44 { |
| 46 static_cast<Vector<unsigned char>*>(png_get_io_ptr(png))->append(data, size)
; | 45 static_cast<Vector<unsigned char>*>(png_get_io_ptr(png))->append(data, size)
; |
| 47 } | 46 } |
| 48 | 47 |
| 49 std::unique_ptr<PNGImageEncoderState> PNGImageEncoderState::create(const IntSize
& imageSize, Vector<unsigned char>* output) | 48 PassOwnPtr<PNGImageEncoderState> PNGImageEncoderState::create(const IntSize& ima
geSize, Vector<unsigned char>* output) |
| 50 { | 49 { |
| 51 if (imageSize.width() <= 0 || imageSize.height() <= 0) | 50 if (imageSize.width() <= 0 || imageSize.height() <= 0) |
| 52 return nullptr; | 51 return nullptr; |
| 53 | 52 |
| 54 png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | 53 png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
| 55 png_info* info = png_create_info_struct(png); | 54 png_info* info = png_create_info_struct(png); |
| 56 if (!png || !info || setjmp(png_jmpbuf(png))) { | 55 if (!png || !info || setjmp(png_jmpbuf(png))) { |
| 57 png_destroy_write_struct(png ? &png : 0, info ? &info : 0); | 56 png_destroy_write_struct(png ? &png : 0, info ? &info : 0); |
| 58 return nullptr; | 57 return nullptr; |
| 59 } | 58 } |
| 60 | 59 |
| 61 // Optimize compression for speed. | 60 // Optimize compression for speed. |
| 62 // The parameters are the same as what libpng uses by default for RGB and RG
BA images, except: | 61 // The parameters are the same as what libpng uses by default for RGB and RG
BA images, except: |
| 63 // - the zlib compression level is 3 instead of 6, to avoid the lazy Ziv-Lem
pel match searching; | 62 // - the zlib compression level is 3 instead of 6, to avoid the lazy Ziv-Lem
pel match searching; |
| 64 // - the delta filter is 1 ("sub") instead of 5 ("all"), to reduce the filte
r computations. | 63 // - the delta filter is 1 ("sub") instead of 5 ("all"), to reduce the filte
r computations. |
| 65 // The zlib memory level (8) and strategy (Z_FILTERED) will be set inside li
bpng. | 64 // The zlib memory level (8) and strategy (Z_FILTERED) will be set inside li
bpng. |
| 66 // | 65 // |
| 67 // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE. | 66 // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE. |
| 68 // Although they are the fastest for poorly-compressible images (e.g. photog
raphs), | 67 // Although they are the fastest for poorly-compressible images (e.g. photog
raphs), |
| 69 // they are very slow for highly-compressible images (e.g. text, drawings or
business graphics) | 68 // they are very slow for highly-compressible images (e.g. text, drawings or
business graphics) |
| 70 png_set_compression_level(png, 3); | 69 png_set_compression_level(png, 3); |
| 71 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); | 70 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); |
| 72 | 71 |
| 73 png_set_write_fn(png, output, writeOutput, 0); | 72 png_set_write_fn(png, output, writeOutput, 0); |
| 74 png_set_IHDR(png, info, imageSize.width(), imageSize.height(), 8, PNG_COLOR_
TYPE_RGB_ALPHA, 0, 0, 0); | 73 png_set_IHDR(png, info, imageSize.width(), imageSize.height(), 8, PNG_COLOR_
TYPE_RGB_ALPHA, 0, 0, 0); |
| 75 png_write_info(png, info); | 74 png_write_info(png, info); |
| 76 | 75 |
| 77 return wrapUnique(new PNGImageEncoderState(png, info)); | 76 return adoptPtr(new PNGImageEncoderState(png, info)); |
| 78 } | 77 } |
| 79 | 78 |
| 80 void PNGImageEncoder::writeOneRowToPng(unsigned char* pixels, PNGImageEncoderSta
te* encoderState) | 79 void PNGImageEncoder::writeOneRowToPng(unsigned char* pixels, PNGImageEncoderSta
te* encoderState) |
| 81 { | 80 { |
| 82 png_write_row(encoderState->png(), pixels); | 81 png_write_row(encoderState->png(), pixels); |
| 83 } | 82 } |
| 84 | 83 |
| 85 void PNGImageEncoder::finalizePng(PNGImageEncoderState* encoderState) | 84 void PNGImageEncoder::finalizePng(PNGImageEncoderState* encoderState) |
| 86 { | 85 { |
| 87 png_write_end(encoderState->png(), encoderState->info()); | 86 png_write_end(encoderState->png(), encoderState->info()); |
| 88 } | 87 } |
| 89 | 88 |
| 90 static bool encodePixels(const IntSize& imageSize, const unsigned char* inputPix
els, Vector<unsigned char>* output) | 89 static bool encodePixels(const IntSize& imageSize, const unsigned char* inputPix
els, Vector<unsigned char>* output) |
| 91 { | 90 { |
| 92 std::unique_ptr<PNGImageEncoderState> encoderState = PNGImageEncoderState::c
reate(imageSize, output); | 91 OwnPtr<PNGImageEncoderState> encoderState = PNGImageEncoderState::create(ima
geSize, output); |
| 93 if (!encoderState.get()) | 92 if (!encoderState.get()) |
| 94 return false; | 93 return false; |
| 95 | 94 |
| 96 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); | 95 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); |
| 97 const size_t pixelRowStride = imageSize.width() * 4; | 96 const size_t pixelRowStride = imageSize.width() * 4; |
| 98 for (int y = 0; y < imageSize.height(); ++y) { | 97 for (int y = 0; y < imageSize.height(); ++y) { |
| 99 PNGImageEncoder::writeOneRowToPng(pixels, encoderState.get()); | 98 PNGImageEncoder::writeOneRowToPng(pixels, encoderState.get()); |
| 100 pixels += pixelRowStride; | 99 pixels += pixelRowStride; |
| 101 } | 100 } |
| 102 | 101 |
| 103 PNGImageEncoder::finalizePng(encoderState.get()); | 102 PNGImageEncoder::finalizePng(encoderState.get()); |
| 104 return true; | 103 return true; |
| 105 } | 104 } |
| 106 | 105 |
| 107 bool PNGImageEncoder::encode(const ImageDataBuffer& imageData, Vector<unsigned c
har>* output) | 106 bool PNGImageEncoder::encode(const ImageDataBuffer& imageData, Vector<unsigned c
har>* output) |
| 108 { | 107 { |
| 109 if (!imageData.pixels()) | 108 if (!imageData.pixels()) |
| 110 return false; | 109 return false; |
| 111 | 110 |
| 112 return encodePixels(imageData.size(), imageData.pixels(), output); | 111 return encodePixels(imageData.size(), imageData.pixels(), output); |
| 113 } | 112 } |
| 114 | 113 |
| 115 } // namespace blink | 114 } // namespace blink |
| OLD | NEW |