OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2011, Google Inc. All rights reserved. | 2 * Copyright (c) 2011, 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 "config.h" | 31 #include "config.h" |
32 #include "platform/image-encoders/skia/WEBPImageEncoder.h" | 32 #include "platform/image-encoders/skia/WEBPImageEncoder.h" |
33 | 33 |
34 #include "SkBitmap.h" | |
35 #include "platform/geometry/IntSize.h" | 34 #include "platform/geometry/IntSize.h" |
36 #include "platform/graphics/ImageBuffer.h" | 35 #include "platform/graphics/ImageBuffer.h" |
37 #include "webp/encode.h" | 36 #include "webp/encode.h" |
38 | 37 |
39 typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int r owStride); | 38 typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int r owStride); |
40 | 39 |
41 namespace blink { | 40 namespace blink { |
42 | 41 |
43 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* cons t picture) | 42 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* cons t picture) |
44 { | 43 { |
45 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size) ; | 44 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size) ; |
46 return 1; | 45 return 1; |
47 } | 46 } |
48 | 47 |
49 static bool rgbPictureImport(const unsigned char* pixels, bool premultiplied, We bPImporter importRGBX, WebPImporter importRGB, WebPPicture* picture) | 48 static bool rgbPictureImport(const unsigned char* pixels, WebPImporter importRGB , WebPPicture* picture) |
50 { | 49 { |
51 if (premultiplied) | |
52 return importRGBX(picture, pixels, picture->width * 4); | |
53 | |
54 // Write the RGB pixels to an rgb data buffer, alpha premultiplied, then imp ort the rgb data. | 50 // Write the RGB pixels to an rgb data buffer, alpha premultiplied, then imp ort the rgb data. |
55 | 51 |
56 size_t pixelCount = picture->height * picture->width; | 52 size_t pixelCount = picture->height * picture->width; |
57 | 53 |
58 OwnPtr<unsigned char[]> rgb = adoptArrayPtr(new unsigned char[pixelCount * 3 ]); | 54 OwnPtr<unsigned char[]> rgb = adoptArrayPtr(new unsigned char[pixelCount * 3 ]); |
59 if (!rgb.get()) | 55 if (!rgb.get()) |
60 return false; | 56 return false; |
61 | 57 |
62 for (unsigned char* data = rgb.get(); pixelCount-- > 0; pixels += 4) { | 58 for (unsigned char* data = rgb.get(); pixelCount-- > 0; pixels += 4) { |
63 unsigned char alpha = pixels[3]; | 59 unsigned char alpha = pixels[3]; |
64 if (alpha != 255) { | 60 if (alpha != 255) { |
65 *data++ = SkMulDiv255Round(pixels[0], alpha); | 61 *data++ = SkMulDiv255Round(pixels[0], alpha); |
66 *data++ = SkMulDiv255Round(pixels[1], alpha); | 62 *data++ = SkMulDiv255Round(pixels[1], alpha); |
67 *data++ = SkMulDiv255Round(pixels[2], alpha); | 63 *data++ = SkMulDiv255Round(pixels[2], alpha); |
68 } else { | 64 } else { |
69 *data++ = pixels[0]; | 65 *data++ = pixels[0]; |
70 *data++ = pixels[1]; | 66 *data++ = pixels[1]; |
71 *data++ = pixels[2]; | 67 *data++ = pixels[2]; |
72 } | 68 } |
73 } | 69 } |
74 | 70 |
75 return importRGB(picture, rgb.get(), picture->width * 3); | 71 return importRGB(picture, rgb.get(), picture->width * 3); |
76 } | 72 } |
77 | 73 |
78 template <bool Premultiplied> inline bool importPictureBGRX(const unsigned char* pixels, WebPPicture* picture) | 74 static bool encodePixels(IntSize imageSize, const unsigned char* pixels, int qua lity, Vector<unsigned char>* output) |
79 { | |
80 return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportBGRX, &WebP PictureImportBGR, picture); | |
81 } | |
82 | |
83 template <bool Premultiplied> inline bool importPictureRGBX(const unsigned char* pixels, WebPPicture* picture) | |
84 { | |
85 return rgbPictureImport(pixels, Premultiplied, &WebPPictureImportRGBX, &WebP PictureImportRGB, picture); | |
86 } | |
87 | |
88 static bool platformPremultipliedImportPicture(const unsigned char* pixels, WebP Picture* picture) | |
89 { | |
90 if (SK_B32_SHIFT) // Android | |
91 return importPictureRGBX<true>(pixels, picture); | |
92 | |
93 return importPictureBGRX<true>(pixels, picture); | |
94 } | |
95 | |
96 static bool encodePixels(IntSize imageSize, const unsigned char* pixels, bool pr emultiplied, int quality, Vector<unsigned char>* output) | |
97 { | 75 { |
98 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) | 76 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) |
99 return false; | 77 return false; |
100 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) | 78 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) |
101 return false; | 79 return false; |
102 | 80 |
103 WebPConfig config; | 81 WebPConfig config; |
104 if (!WebPConfigInit(&config)) | 82 if (!WebPConfigInit(&config)) |
105 return false; | 83 return false; |
106 WebPPicture picture; | 84 WebPPicture picture; |
107 if (!WebPPictureInit(&picture)) | 85 if (!WebPPictureInit(&picture)) |
108 return false; | 86 return false; |
109 | 87 |
110 picture.width = imageSize.width(); | 88 picture.width = imageSize.width(); |
111 picture.height = imageSize.height(); | 89 picture.height = imageSize.height(); |
112 | 90 |
113 if (premultiplied && !platformPremultipliedImportPicture(pixels, &picture)) | 91 if (!rgbPictureImport(pixels, &WebPPictureImportRGB, &picture)) |
urvang
2015/08/28 17:20:58
It would be helpful to put a comment that ImageDat
| |
114 return false; | |
115 if (!premultiplied && !importPictureRGBX<false>(pixels, &picture)) | |
116 return false; | 92 return false; |
117 | 93 |
118 picture.custom_ptr = output; | 94 picture.custom_ptr = output; |
119 picture.writer = &writeOutput; | 95 picture.writer = &writeOutput; |
120 config.quality = quality; | 96 config.quality = quality; |
121 config.method = 3; | 97 config.method = 3; |
122 | 98 |
123 bool success = WebPEncode(&config, &picture); | 99 bool success = WebPEncode(&config, &picture); |
124 WebPPictureFree(&picture); | 100 WebPPictureFree(&picture); |
125 return success; | 101 return success; |
126 } | 102 } |
127 | 103 |
128 bool WEBPImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsign ed char>* output) | |
129 { | |
130 SkAutoLockPixels bitmapLock(bitmap); | |
131 | |
132 if (bitmap.colorType() != kN32_SkColorType || !bitmap.getPixels()) | |
133 return false; // Only support 32 bit/pixel skia bitmaps. | |
134 | |
135 return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<un signed char *>(bitmap.getPixels()), true, quality, output); | |
136 } | |
137 | |
138 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output) | 104 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output) |
139 { | 105 { |
140 if (!imageData.pixels()) | 106 if (!imageData.pixels()) |
141 return false; | 107 return false; |
142 | 108 |
143 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat a.pixels(), false, quality, output); | 109 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat a.pixels(), quality, output); |
144 } | 110 } |
145 | 111 |
146 } // namespace blink | 112 } // namespace blink |
OLD | NEW |