OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011, 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/skia/WEBPImageEncoder.h" | |
32 | |
33 #include "platform/geometry/IntSize.h" | |
34 #include "platform/graphics/ImageBuffer.h" | |
35 #include "webp/encode.h" | |
36 | |
37 namespace blink { | |
38 | |
39 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* cons
t picture) | |
40 { | |
41 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size)
; | |
42 return 1; | |
43 } | |
44 | |
45 static bool encodePixels(const IntSize& imageSize, const unsigned char* pixels,
int quality, Vector<unsigned char>* output) | |
46 { | |
47 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) | |
48 return false; | |
49 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) | |
50 return false; | |
51 | |
52 WebPConfig config; | |
53 if (!WebPConfigInit(&config)) | |
54 return false; | |
55 WebPPicture picture; | |
56 if (!WebPPictureInit(&picture)) | |
57 return false; | |
58 | |
59 picture.width = imageSize.width(); | |
60 picture.height = imageSize.height(); | |
61 | |
62 bool useLosslessEncoding = (quality >= 100); | |
63 if (useLosslessEncoding) | |
64 picture.use_argb = 1; | |
65 if (!WebPPictureImportRGBA(&picture, pixels, picture.width * 4)) | |
66 return false; | |
67 | |
68 picture.custom_ptr = output; | |
69 picture.writer = &writeOutput; | |
70 | |
71 if (useLosslessEncoding) { | |
72 config.lossless = 1; | |
73 config.quality = 75; | |
74 config.method = 0; | |
75 } else { | |
76 config.quality = quality; | |
77 config.method = 3; | |
78 } | |
79 | |
80 bool success = WebPEncode(&config, &picture); | |
81 WebPPictureFree(&picture); | |
82 return success; | |
83 } | |
84 | |
85 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec
tor<unsigned char>* output) | |
86 { | |
87 if (!imageData.pixels()) | |
88 return false; | |
89 | |
90 return encodePixels(imageData.size(), imageData.pixels(), quality, output); | |
91 } | |
92 | |
93 } // namespace blink | |
OLD | NEW |