OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2010, Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
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. | |
29 */ | |
30 | |
31 #include "platform/image-encoders/PNGImageEncoder.h" | |
32 | |
33 #include <memory> | |
34 #include "platform/graphics/ImageBuffer.h" | |
35 #include "platform/wtf/PtrUtil.h" | |
36 | |
37 #include "zlib.h" | |
38 | |
39 namespace blink { | |
40 | |
41 PNGImageEncoderState::~PNGImageEncoderState() { | |
42 png_destroy_write_struct(&png_, &info_); | |
43 } | |
44 | |
45 static void WriteOutput(png_structp png, png_bytep data, png_size_t size) { | |
46 static_cast<Vector<unsigned char>*>(png_get_io_ptr(png))->Append(data, size); | |
47 } | |
48 | |
49 std::unique_ptr<PNGImageEncoderState> PNGImageEncoderState::Create( | |
50 const IntSize& image_size, | |
51 Vector<unsigned char>* output) { | |
52 if (image_size.Width() <= 0 || image_size.Height() <= 0) | |
53 return nullptr; | |
54 | |
55 png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | |
56 png_info* info = png_create_info_struct(png); | |
57 if (!png || !info || setjmp(png_jmpbuf(png))) { | |
58 png_destroy_write_struct(png ? &png : 0, info ? &info : 0); | |
59 return nullptr; | |
60 } | |
61 | |
62 // Optimize compression for speed. | |
63 // The parameters are the same as what libpng uses by default for RGB and RGBA | |
64 // images, except: | |
65 // The zlib compression level is set to 3 instead of 6, to avoid the lazy | |
66 // Ziv-Lempel match searching. | |
67 png_set_compression_level(png, 3); | |
68 | |
69 // The zlib memory level is set to 8. This actually matches the default, we | |
70 // are just future-proofing. | |
71 png_set_compression_mem_level(png, 8); | |
72 | |
73 // The zlib strategy is set to Z_FILTERED, which does not match the default. | |
74 // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE. | |
75 // Although they are the fastest for poorly-compressible images (e.g. | |
76 // photographs), they are very slow for highly-compressible images (e.g. text, | |
77 // drawings or business graphics) | |
78 png_set_compression_strategy(png, Z_FILTERED); | |
79 | |
80 // The delta filter is PNG_FILTER_SUB instead of PNG_ALL_FILTERS, to reduce | |
81 // the filter computations. | |
82 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); | |
83 | |
84 png_set_write_fn(png, output, WriteOutput, 0); | |
85 png_set_IHDR(png, info, image_size.Width(), image_size.Height(), 8, | |
86 PNG_COLOR_TYPE_RGB_ALPHA, 0, 0, 0); | |
87 png_write_info(png, info); | |
88 | |
89 return WTF::WrapUnique(new PNGImageEncoderState(png, info)); | |
90 } | |
91 | |
92 void PNGImageEncoder::WriteOneRowToPng(unsigned char* pixels, | |
93 PNGImageEncoderState* encoder_state) { | |
94 png_write_row(encoder_state->Png(), pixels); | |
95 } | |
96 | |
97 void PNGImageEncoder::FinalizePng(PNGImageEncoderState* encoder_state) { | |
98 png_write_end(encoder_state->Png(), encoder_state->Info()); | |
99 } | |
100 | |
101 static bool EncodePixels(const IntSize& image_size, | |
102 const unsigned char* input_pixels, | |
103 Vector<unsigned char>* output) { | |
104 std::unique_ptr<PNGImageEncoderState> encoder_state = | |
105 PNGImageEncoderState::Create(image_size, output); | |
106 if (!encoder_state.get()) | |
107 return false; | |
108 | |
109 unsigned char* pixels = const_cast<unsigned char*>(input_pixels); | |
110 const size_t pixel_row_stride = image_size.Width() * 4; | |
111 for (int y = 0; y < image_size.Height(); ++y) { | |
112 PNGImageEncoder::WriteOneRowToPng(pixels, encoder_state.get()); | |
113 pixels += pixel_row_stride; | |
114 } | |
115 | |
116 PNGImageEncoder::FinalizePng(encoder_state.get()); | |
117 return true; | |
118 } | |
119 | |
120 bool PNGImageEncoder::Encode(const ImageDataBuffer& image_data, | |
121 Vector<unsigned char>* output) { | |
122 if (!image_data.Pixels()) | |
123 return false; | |
124 | |
125 return EncodePixels(image_data.size(), image_data.Pixels(), output); | |
126 } | |
127 | |
128 } // namespace blink | |
OLD | NEW |