Chromium Code Reviews| 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 16 matching lines...) Expand all Loading... | |
| 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 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size) ; |
| 44 return 1; | 42 return 1; |
| 45 } | 43 } |
| 46 | 44 |
| 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 } | |
| 72 | |
| 73 static bool encodePixels(const IntSize& imageSize, const unsigned char* pixels, int quality, Vector<unsigned char>* output) | 45 static bool encodePixels(const IntSize& imageSize, const unsigned char* pixels, int quality, Vector<unsigned char>* output) |
| 74 { | 46 { |
| 75 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) | 47 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) |
| 76 return false; | 48 return false; |
| 77 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) | 49 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) |
| 78 return false; | 50 return false; |
| 79 | 51 |
| 80 WebPConfig config; | 52 WebPConfig config; |
| 81 if (!WebPConfigInit(&config)) | 53 if (!WebPConfigInit(&config)) |
| 82 return false; | 54 return false; |
| 83 WebPPicture picture; | 55 WebPPicture picture; |
| 84 if (!WebPPictureInit(&picture)) | 56 if (!WebPPictureInit(&picture)) |
| 85 return false; | 57 return false; |
| 86 | 58 |
| 87 picture.width = imageSize.width(); | 59 picture.width = imageSize.width(); |
| 88 picture.height = imageSize.height(); | 60 picture.height = imageSize.height(); |
| 89 | 61 |
| 90 if (!rgbPictureImport(pixels, &WebPPictureImportRGB, &picture)) | 62 bool useLosslessEncoding = (quality >= 100); |
| 63 if (useLosslessEncoding) | |
| 64 picture.use_argb = 1; | |
| 65 if (!WebPPictureImportRGBA(&picture, pixels, picture.width * 4)) | |
| 91 return false; | 66 return false; |
| 92 | 67 |
| 93 picture.custom_ptr = output; | 68 picture.custom_ptr = output; |
| 94 picture.writer = &writeOutput; | 69 picture.writer = &writeOutput; |
| 95 config.quality = quality; | 70 |
| 96 config.method = 3; | 71 if (useLosslessEncoding) { |
| 72 config.lossless = 1; | |
| 73 config.quality = 75; | |
|
Noel Gordon
2016/05/04 01:15:58
Note urvang@: the config.{quality,method} values u
| |
| 74 config.method = 0; | |
| 75 } else { | |
| 76 config.quality = quality; | |
| 77 config.method = 3; | |
| 78 } | |
| 97 | 79 |
| 98 bool success = WebPEncode(&config, &picture); | 80 bool success = WebPEncode(&config, &picture); |
| 99 WebPPictureFree(&picture); | 81 WebPPictureFree(&picture); |
| 100 return success; | 82 return success; |
| 101 } | 83 } |
| 102 | 84 |
| 103 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output) | 85 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output) |
| 104 { | 86 { |
| 105 if (!imageData.pixels()) | 87 if (!imageData.pixels()) |
| 106 return false; | 88 return false; |
| 107 | 89 |
| 108 return encodePixels(imageData.size(), imageData.pixels(), quality, output); | 90 return encodePixels(imageData.size(), imageData.pixels(), quality, output); |
| 109 } | 91 } |
| 110 | 92 |
| 111 } // namespace blink | 93 } // namespace blink |
| OLD | NEW |