Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/WebKit/Source/platform/image-encoders/skia/WEBPImageEncoder.cpp

Issue 1937433002: HTML <canvas>: add WEBP lossless encoding support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge to ToT Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 16 matching lines...) Expand all
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/skia/WEBPImageEncoder.h" 31 #include "platform/image-encoders/skia/WEBPImageEncoder.h"
32 32
33 #include "platform/geometry/IntSize.h" 33 #include "platform/geometry/IntSize.h"
34 #include "platform/graphics/ImageBuffer.h" 34 #include "platform/graphics/ImageBuffer.h"
35 #include "webp/encode.h" 35 #include "webp/encode.h"
36 36
37 typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int r owStride);
38
39 namespace blink { 37 namespace blink {
40 38
41 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* cons t picture) 39 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* cons t picture)
42 { 40 {
43 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size) ; 41 return static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data , size), 1;
urvang 2016/05/03 17:28:19 My first impression was "What sorcery is this?" :P
Noel Gordon 2016/05/04 01:10:30 :P done.
44 return 1;
45 }
46
47 static bool rgbPictureImport(const unsigned char* pixels, WebPImporter importRGB , WebPPicture* picture)
48 {
49 // Write the RGB pixels to an rgb data buffer, alpha premultiplied, then imp ort the rgb data.
50
51 size_t pixelCount = picture->height * picture->width;
52
53 OwnPtr<unsigned char[]> rgb = adoptArrayPtr(new unsigned char[pixelCount * 3 ]);
54 if (!rgb.get())
55 return false;
56
57 for (unsigned char* data = rgb.get(); pixelCount-- > 0; pixels += 4) {
58 unsigned char alpha = pixels[3];
59 if (alpha != 255) {
60 *data++ = SkMulDiv255Round(pixels[0], alpha);
61 *data++ = SkMulDiv255Round(pixels[1], alpha);
62 *data++ = SkMulDiv255Round(pixels[2], alpha);
63 } else {
64 *data++ = pixels[0];
65 *data++ = pixels[1];
66 *data++ = pixels[2];
67 }
68 }
69
70 return importRGB(picture, rgb.get(), picture->width * 3);
71 } 42 }
72 43
73 static bool encodePixels(const IntSize& imageSize, const unsigned char* pixels, int quality, Vector<unsigned char>* output) 44 static bool encodePixels(const IntSize& imageSize, const unsigned char* pixels, int quality, Vector<unsigned char>* output)
74 { 45 {
75 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) 46 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION)
76 return false; 47 return false;
77 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) 48 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION)
78 return false; 49 return false;
79 50
80 WebPConfig config; 51 WebPConfig config;
81 if (!WebPConfigInit(&config)) 52 if (!WebPConfigInit(&config))
82 return false; 53 return false;
83 WebPPicture picture; 54 WebPPicture picture;
84 if (!WebPPictureInit(&picture)) 55 if (!WebPPictureInit(&picture))
85 return false; 56 return false;
86 57
87 picture.width = imageSize.width(); 58 picture.width = imageSize.width();
88 picture.height = imageSize.height(); 59 picture.height = imageSize.height();
89 60
90 if (!rgbPictureImport(pixels, &WebPPictureImportRGB, &picture)) 61 if (quality >= 100)
urvang 2016/05/03 17:28:19 Create and use a variable "bool use_lossless_encod
Noel Gordon 2016/05/04 01:10:30 Done: useLosslessEncoding.
62 picture.use_argb = 1;
63 if (!WebPPictureImportRGBA(&picture, pixels, picture.width * 4))
91 return false; 64 return false;
92 65
93 picture.custom_ptr = output; 66 picture.custom_ptr = output;
94 picture.writer = &writeOutput; 67 picture.writer = &writeOutput;
95 config.quality = quality; 68
96 config.method = 3; 69 if (quality >= 100) {
70 config.lossless = 1;
71 config.quality = 75;
72 config.method = 0;
73 } else {
74 config.quality = quality;
75 config.method = 3;
76 }
97 77
98 bool success = WebPEncode(&config, &picture); 78 bool success = WebPEncode(&config, &picture);
99 WebPPictureFree(&picture); 79 WebPPictureFree(&picture);
100 return success; 80 return success;
101 } 81 }
102 82
103 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output) 83 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output)
104 { 84 {
105 if (!imageData.pixels()) 85 if (!imageData.pixels())
106 return false; 86 return false;
107 87
108 return encodePixels(imageData.size(), imageData.pixels(), quality, output); 88 return encodePixels(imageData.size(), imageData.pixels(), quality, output);
109 } 89 }
110 90
111 } // namespace blink 91 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698