| 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 17 matching lines...) Expand all Loading... |
| 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 "platform/geometry/IntSize.h" | 34 #include "platform/geometry/IntSize.h" |
| 35 #include "platform/graphics/ImageBuffer.h" | 35 #include "platform/graphics/ImageBuffer.h" |
| 36 #include "webp/encode.h" | 36 #include "webp/encode.h" |
| 37 | 37 |
| 38 typedef int (*WebPImporter)(WebPPicture* const, const uint8_t* const data, int r
owStride); | |
| 39 | |
| 40 namespace blink { | 38 namespace blink { |
| 41 | 39 |
| 42 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* cons
t picture) | 40 static int writeOutput(const uint8_t* data, size_t size, const WebPPicture* cons
t picture) |
| 43 { | 41 { |
| 44 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data, size)
; | 42 return static_cast<Vector<unsigned char>*>(picture->custom_ptr)->append(data
, size), 1; |
| 45 return 1; | |
| 46 } | 43 } |
| 47 | 44 |
| 48 static bool rgbPictureImport(const unsigned char* pixels, WebPImporter importRGB
, WebPPicture* picture) | 45 inline bool importUnpremultipliedRGBA(const unsigned char* pixels, WebPPicture*
picture) |
| 49 { | 46 { |
| 50 // Write the RGB pixels to an rgb data buffer, alpha premultiplied, then imp
ort the rgb data. | 47 return WebPPictureImportRGBA(picture, pixels, picture->width * 4); |
| 51 | |
| 52 size_t pixelCount = picture->height * picture->width; | |
| 53 | |
| 54 OwnPtr<unsigned char[]> rgb = adoptArrayPtr(new unsigned char[pixelCount * 3
]); | |
| 55 if (!rgb.get()) | |
| 56 return false; | |
| 57 | |
| 58 for (unsigned char* data = rgb.get(); pixelCount-- > 0; pixels += 4) { | |
| 59 unsigned char alpha = pixels[3]; | |
| 60 if (alpha != 255) { | |
| 61 *data++ = SkMulDiv255Round(pixels[0], alpha); | |
| 62 *data++ = SkMulDiv255Round(pixels[1], alpha); | |
| 63 *data++ = SkMulDiv255Round(pixels[2], alpha); | |
| 64 } else { | |
| 65 *data++ = pixels[0]; | |
| 66 *data++ = pixels[1]; | |
| 67 *data++ = pixels[2]; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 return importRGB(picture, rgb.get(), picture->width * 3); | |
| 72 } | 48 } |
| 73 | 49 |
| 74 static bool encodePixels(IntSize imageSize, const unsigned char* pixels, int qua
lity, Vector<unsigned char>* output) | 50 static bool encodePixels(IntSize imageSize, const unsigned char* pixels, int qua
lity, Vector<unsigned char>* output, WEBPImageEncoder::Mode mode) |
| 75 { | 51 { |
| 76 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) | 52 if (imageSize.width() <= 0 || imageSize.width() > WEBP_MAX_DIMENSION) |
| 77 return false; | 53 return false; |
| 78 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) | 54 if (imageSize.height() <= 0 || imageSize.height() > WEBP_MAX_DIMENSION) |
| 79 return false; | 55 return false; |
| 80 | 56 |
| 81 WebPConfig config; | 57 WebPConfig config; |
| 82 if (!WebPConfigInit(&config)) | 58 if (!WebPConfigInit(&config)) |
| 83 return false; | 59 return false; |
| 84 WebPPicture picture; | 60 WebPPicture picture; |
| 85 if (!WebPPictureInit(&picture)) | 61 if (!WebPPictureInit(&picture)) |
| 86 return false; | 62 return false; |
| 87 | 63 |
| 88 picture.width = imageSize.width(); | 64 picture.width = imageSize.width(); |
| 89 picture.height = imageSize.height(); | 65 picture.height = imageSize.height(); |
| 90 | 66 |
| 91 if (!rgbPictureImport(pixels, &WebPPictureImportRGB, &picture)) | 67 if (quality > 99 || mode == WEBPImageEncoder::Lossless) |
| 68 config.lossless = picture.use_argb = 1; |
| 69 |
| 70 if (!importUnpremultipliedRGBA(pixels, &picture)) |
| 92 return false; | 71 return false; |
| 93 | 72 |
| 94 picture.custom_ptr = output; | 73 picture.custom_ptr = output; |
| 95 picture.writer = &writeOutput; | 74 picture.writer = &writeOutput; |
| 96 config.quality = quality; | 75 |
| 97 config.method = 3; | 76 if (mode == WEBPImageEncoder::Lossless) { |
| 77 config.quality = quality; |
| 78 config.method = 2; |
| 79 } else if (quality > 99) { |
| 80 config.quality = 75; |
| 81 config.method = 0; |
| 82 } else { |
| 83 ASSERT(!config.lossless); |
| 84 config.quality = quality; |
| 85 config.method = 3; |
| 86 } |
| 98 | 87 |
| 99 bool success = WebPEncode(&config, &picture); | 88 bool success = WebPEncode(&config, &picture); |
| 100 WebPPictureFree(&picture); | 89 WebPPictureFree(&picture); |
| 101 return success; | 90 return success; |
| 102 } | 91 } |
| 103 | 92 |
| 104 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec
tor<unsigned char>* output) | 93 bool WEBPImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec
tor<unsigned char>* output, WEBPImageEncoder::Mode mode) |
| 105 { | 94 { |
| 106 if (!imageData.pixels()) | 95 if (!imageData.pixels()) |
| 107 return false; | 96 return false; |
| 108 | 97 |
| 109 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat
a.pixels(), quality, output); | 98 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat
a.pixels(), quality, output, mode); |
| 110 } | 99 } |
| 111 | 100 |
| 112 } // namespace blink | 101 } // namespace blink |
| OLD | NEW |