initial import
[vuplus_webkit] / Source / WebCore / platform / image-encoders / skia / PNGImageEncoder.cpp
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 "config.h"
32 #include "PNGImageEncoder.h"
33
34 #include "ImageData.h"
35 #include "IntSize.h"
36 #include "SkBitmap.h"
37 #include "SkColorPriv.h"
38 #include "SkUnPreMultiply.h"
39 extern "C" {
40 #include "png.h"
41 }
42
43 namespace WebCore {
44
45 static void writeOutput(png_structp png, png_bytep data, png_size_t size)
46 {
47     static_cast<Vector<unsigned char>*>(png_get_io_ptr(png))->append(data, size);
48 }
49
50 static void preMultipliedBGRAtoRGBA(const void* pixels, int pixelCount, unsigned char* output)
51 {
52     static const SkUnPreMultiply::Scale* scale = SkUnPreMultiply::GetScaleTable();
53     const SkPMColor* input = static_cast<const SkPMColor*>(pixels);
54
55     for (; pixelCount-- > 0; ++input) {
56         const unsigned alpha = SkGetPackedA32(*input);
57         if ((alpha != 0) && (alpha != 255)) {
58             *output++ = SkUnPreMultiply::ApplyScale(scale[alpha], SkGetPackedR32(*input));
59             *output++ = SkUnPreMultiply::ApplyScale(scale[alpha], SkGetPackedG32(*input));
60             *output++ = SkUnPreMultiply::ApplyScale(scale[alpha], SkGetPackedB32(*input));
61             *output++ = alpha;
62         } else {
63             *output++ = SkGetPackedR32(*input);
64             *output++ = SkGetPackedG32(*input);
65             *output++ = SkGetPackedB32(*input);
66             *output++ = alpha;
67         }
68     }
69 }
70
71 static bool encodePixels(const IntSize& inputSize, unsigned char* inputPixels,
72                          bool premultiplied, Vector<unsigned char>* output)
73 {
74     IntSize imageSize(inputSize);
75     imageSize.clampNegativeToZero();
76     Vector<unsigned char> row;
77
78     png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
79     png_info* info = png_create_info_struct(png);
80     if (!png || !info || setjmp(png_jmpbuf(png))) {
81         png_destroy_write_struct(png ? &png : 0, info ? &info : 0);
82         return false;
83     }
84
85     // Optimize compression for speed.
86     // The parameters are the same as what libpng uses by default for RGB and RGBA images, except:
87     // - the zlib compression level is 3 instead of 6, to avoid the lazy Ziv-Lempel match searching;
88     // - the delta filter is 1 ("sub") instead of 5 ("all"), to reduce the filter computations.
89     // The zlib memory level (8) and strategy (Z_FILTERED) will be set inside libpng.
90     //
91     // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE.
92     // Although they are the fastest for poorly-compressible images (e.g. photographs),
93     // they are very slow for highly-compressible images (e.g. text, drawings or business graphics).
94     png_set_compression_level(png, 3);
95     png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
96
97     png_set_write_fn(png, output, writeOutput, 0);
98     png_set_IHDR(png, info, imageSize.width(), imageSize.height(),
99                  8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0, 0);
100     png_write_info(png, info);
101
102     unsigned char* pixels = inputPixels;
103     row.resize(imageSize.width() * sizeof(SkPMColor));
104     for (int y = 0; y < imageSize.height(); ++y) {
105         if (premultiplied) {
106             preMultipliedBGRAtoRGBA(pixels, imageSize.width(), row.data());
107             png_write_row(png, row.data());
108         } else
109             png_write_row(png, pixels);
110         pixels += imageSize.width() * 4;
111     }
112
113     png_write_end(png, info);
114     png_destroy_write_struct(&png, &info);
115     return true;
116 }
117
118 bool PNGImageEncoder::encode(const SkBitmap& bitmap, Vector<unsigned char>* output)
119 {
120     if (bitmap.config() != SkBitmap::kARGB_8888_Config)
121         return false; // Only support ARGB 32 bpp skia bitmaps.
122
123     SkAutoLockPixels bitmapLock(bitmap);
124     IntSize imageSize(bitmap.width(), bitmap.height());
125     return encodePixels(imageSize, static_cast<unsigned char*>(bitmap.getPixels()), true, output);
126 }
127
128 bool PNGImageEncoder::encode(const ImageData& bitmap, Vector<unsigned char>* output)
129 {
130     return encodePixels(bitmap.size(), bitmap.data()->data()->data(), false, output);
131 }
132
133 } // namespace WebCore