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 15 matching lines...) Expand all Loading... | |
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/OwnPtr.h" | 34 #include "wtf/OwnPtr.h" |
35 | 35 |
36 #include "zlib.h" | |
37 | |
36 namespace blink { | 38 namespace blink { |
37 | 39 |
38 PNGImageEncoderState::~PNGImageEncoderState() | 40 PNGImageEncoderState::~PNGImageEncoderState() |
39 { | 41 { |
40 png_destroy_write_struct(&m_png, &m_info); | 42 png_destroy_write_struct(&m_png, &m_info); |
41 } | 43 } |
42 | 44 |
43 static void writeOutput(png_structp png, png_bytep data, png_size_t size) | 45 static void writeOutput(png_structp png, png_bytep data, png_size_t size) |
44 { | 46 { |
45 static_cast<Vector<unsigned char>*>(png_get_io_ptr(png))->append(data, size) ; | 47 static_cast<Vector<unsigned char>*>(png_get_io_ptr(png))->append(data, size) ; |
46 } | 48 } |
47 | 49 |
48 PassOwnPtr<PNGImageEncoderState> PNGImageEncoderState::create(const IntSize& ima geSize, Vector<unsigned char>* output) | 50 PassOwnPtr<PNGImageEncoderState> PNGImageEncoderState::create(const IntSize& ima geSize, Vector<unsigned char>* output) |
49 { | 51 { |
50 if (imageSize.width() <= 0 || imageSize.height() <= 0) | 52 if (imageSize.width() <= 0 || imageSize.height() <= 0) |
51 return nullptr; | 53 return nullptr; |
52 | 54 |
53 png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | 55 png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
54 png_info* info = png_create_info_struct(png); | 56 png_info* info = png_create_info_struct(png); |
55 if (!png || !info || setjmp(png_jmpbuf(png))) { | 57 if (!png || !info || setjmp(png_jmpbuf(png))) { |
56 png_destroy_write_struct(png ? &png : 0, info ? &info : 0); | 58 png_destroy_write_struct(png ? &png : 0, info ? &info : 0); |
57 return nullptr; | 59 return nullptr; |
58 } | 60 } |
59 | 61 |
60 // Optimize compression for speed. | 62 // Optimize compression for speed. |
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 set to 3 instead of the default, to avoid t he lazy Ziv-Lempel match searching. |
scroggo_chromium
2016/06/21 17:39:43
I think we've lost some information here. Why did
msarett
2016/06/21 19:06:34
The first time I read it, I thought it was referri
|
scroggo_chromium
2016/06/21 17:39:44
Why not say "instead of 6", like we did before? Wi
msarett
2016/06/21 19:06:33
Done.
|
62 // - the zlib compression level is 3 instead of 6, to avoid the lazy Ziv-Lem pel match searching; | 64 png_set_compression_level(png, 3); |
63 // - the delta filter is 1 ("sub") instead of 5 ("all"), to reduce the filte r computations. | 65 |
64 // The zlib memory level (8) and strategy (Z_FILTERED) will be set inside li bpng. | 66 // The zlib memory level is set to 8, which matches the default. |
scroggo_chromium
2016/06/21 17:39:44
Why do we need to set if it is the default? Do we
msarett
2016/06/21 19:06:33
We now set everything that the past code comments
scroggo_chromium
2016/06/21 19:26:15
+1. Comments can get out of date more easily than
| |
65 // | 67 png_set_compression_mem_level(png, 8); |
68 | |
69 // The zlib strategy is set to Z_FILTERED, which does not match the default. | |
66 // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE. | 70 // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE. |
67 // Although they are the fastest for poorly-compressible images (e.g. photog raphs), | 71 // Although they are the fastest for poorly-compressible images (e.g. photog raphs), |
68 // they are very slow for highly-compressible images (e.g. text, drawings or business graphics) | 72 // they are very slow for highly-compressible images (e.g. text, drawings or business graphics) |
69 png_set_compression_level(png, 3); | 73 png_set_compression_strategy(png, Z_FILTERED); |
74 | |
75 // The delta filter is 1 ("sub") instead of 5 ("all"), to reduce the filter computations. | |
scroggo_chromium
2016/06/21 17:39:44
I think this comment was out of date before you go
msarett
2016/06/21 19:06:33
You're right. Done.
| |
70 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); | 76 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); |
71 | 77 |
72 png_set_write_fn(png, output, writeOutput, 0); | 78 png_set_write_fn(png, output, writeOutput, 0); |
73 png_set_IHDR(png, info, imageSize.width(), imageSize.height(), 8, PNG_COLOR_ TYPE_RGB_ALPHA, 0, 0, 0); | 79 png_set_IHDR(png, info, imageSize.width(), imageSize.height(), 8, PNG_COLOR_ TYPE_RGB_ALPHA, 0, 0, 0); |
74 png_write_info(png, info); | 80 png_write_info(png, info); |
75 | 81 |
76 return adoptPtr(new PNGImageEncoderState(png, info)); | 82 return adoptPtr(new PNGImageEncoderState(png, info)); |
77 } | 83 } |
78 | 84 |
79 void PNGImageEncoder::writeOneRowToPng(unsigned char* pixels, PNGImageEncoderSta te* encoderState) | 85 void PNGImageEncoder::writeOneRowToPng(unsigned char* pixels, PNGImageEncoderSta te* encoderState) |
(...skipping 25 matching lines...) Expand all Loading... | |
105 | 111 |
106 bool PNGImageEncoder::encode(const ImageDataBuffer& imageData, Vector<unsigned c har>* output) | 112 bool PNGImageEncoder::encode(const ImageDataBuffer& imageData, Vector<unsigned c har>* output) |
107 { | 113 { |
108 if (!imageData.pixels()) | 114 if (!imageData.pixels()) |
109 return false; | 115 return false; |
110 | 116 |
111 return encodePixels(imageData.size(), imageData.pixels(), output); | 117 return encodePixels(imageData.size(), imageData.pixels(), output); |
112 } | 118 } |
113 | 119 |
114 } // namespace blink | 120 } // namespace blink |
OLD | NEW |