| 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/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, | |
| 40 size_t size, | |
| 41 const WebPPicture* const picture) { | |
| 42 static_cast<Vector<unsigned char>*>(picture->custom_ptr)->Append(data, size); | |
| 43 return 1; | |
| 44 } | |
| 45 | |
| 46 static bool EncodePixels(const IntSize& image_size, | |
| 47 const unsigned char* pixels, | |
| 48 int quality, | |
| 49 Vector<unsigned char>* output) { | |
| 50 if (image_size.Width() <= 0 || image_size.Width() > WEBP_MAX_DIMENSION) | |
| 51 return false; | |
| 52 if (image_size.Height() <= 0 || image_size.Height() > WEBP_MAX_DIMENSION) | |
| 53 return false; | |
| 54 | |
| 55 WebPConfig config; | |
| 56 if (!WebPConfigInit(&config)) | |
| 57 return false; | |
| 58 WebPPicture picture; | |
| 59 if (!WebPPictureInit(&picture)) | |
| 60 return false; | |
| 61 | |
| 62 picture.width = image_size.Width(); | |
| 63 picture.height = image_size.Height(); | |
| 64 | |
| 65 bool use_lossless_encoding = (quality >= 100); | |
| 66 if (use_lossless_encoding) | |
| 67 picture.use_argb = 1; | |
| 68 if (!WebPPictureImportRGBA(&picture, pixels, picture.width * 4)) | |
| 69 return false; | |
| 70 | |
| 71 picture.custom_ptr = output; | |
| 72 picture.writer = &WriteOutput; | |
| 73 | |
| 74 if (use_lossless_encoding) { | |
| 75 config.lossless = 1; | |
| 76 config.quality = 75; | |
| 77 config.method = 0; | |
| 78 } else { | |
| 79 config.quality = quality; | |
| 80 config.method = 3; | |
| 81 } | |
| 82 | |
| 83 bool success = WebPEncode(&config, &picture); | |
| 84 WebPPictureFree(&picture); | |
| 85 return success; | |
| 86 } | |
| 87 | |
| 88 bool WEBPImageEncoder::Encode(const ImageDataBuffer& image_data, | |
| 89 int quality, | |
| 90 Vector<unsigned char>* output) { | |
| 91 if (!image_data.Pixels()) | |
| 92 return false; | |
| 93 | |
| 94 return EncodePixels(image_data.size(), image_data.Pixels(), quality, output); | |
| 95 } | |
| 96 | |
| 97 } // namespace blink | |
| OLD | NEW |