| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "blimp/common/compositor/blimp_image_serialization_processor.h" | 5 #include "blimp/common/compositor/blimp_image_serialization_processor.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 // Ensure width and height are valid dimensions. | 44 // Ensure width and height are valid dimensions. |
| 45 if (!pixmap.width() || pixmap.width() > WEBP_MAX_DIMENSION) | 45 if (!pixmap.width() || pixmap.width() > WEBP_MAX_DIMENSION) |
| 46 return nullptr; | 46 return nullptr; |
| 47 picture.width = pixmap.width(); | 47 picture.width = pixmap.width(); |
| 48 if (!pixmap.height() || pixmap.height() > WEBP_MAX_DIMENSION) | 48 if (!pixmap.height() || pixmap.height() > WEBP_MAX_DIMENSION) |
| 49 return nullptr; | 49 return nullptr; |
| 50 picture.height = pixmap.height(); | 50 picture.height = pixmap.height(); |
| 51 | 51 |
| 52 // Import picture from raw pixels. | 52 // Import picture from raw pixels. |
| 53 DCHECK(pixmap.alphaType() == kPremul_SkAlphaType); | |
| 54 auto pixel_chars = static_cast<const unsigned char*>(pixmap.addr()); | 53 auto pixel_chars = static_cast<const unsigned char*>(pixmap.addr()); |
| 55 if (!PlatformPictureImport(pixel_chars, &picture)) | 54 if (!PlatformPictureImport(pixel_chars, &picture, pixmap.alphaType())) |
| 56 return nullptr; | 55 return nullptr; |
| 57 | 56 |
| 58 // Create a buffer for where to store the output data. | 57 // Create a buffer for where to store the output data. |
| 59 std::vector<unsigned char> data; | 58 std::vector<unsigned char> data; |
| 60 picture.custom_ptr = &data; | 59 picture.custom_ptr = &data; |
| 61 | 60 |
| 62 // Use our own WebPWriterFunction implementation. | 61 // Use our own WebPWriterFunction implementation. |
| 63 picture.writer = &WebPImageEncoder::WriteOutput; | 62 picture.writer = &WebPImageEncoder::WriteOutput; |
| 64 | 63 |
| 65 // Setup the configuration for the output WebP picture. This is currently | 64 // Setup the configuration for the output WebP picture. This is currently |
| 66 // the same as the default configuration for WebP, but since any change in | 65 // the same as the default configuration for WebP, but since any change in |
| 67 // the WebP defaults would invalidate all caches they are hard coded. | 66 // the WebP defaults would invalidate all caches they are hard coded. |
| 67 config.lossless = 0; |
| 68 config.quality = 75.0; // between 0 (smallest file) and 100 (biggest). | 68 config.quality = 75.0; // between 0 (smallest file) and 100 (biggest). |
| 69 config.method = 4; // quality/speed trade-off (0=fast, 6=slower-better). | 69 config.method = 4; // quality/speed trade-off (0=fast, 6=slower-better). |
| 70 | 70 |
| 71 // Encode the picture using the given configuration. | 71 // Encode the picture using the given configuration. |
| 72 bool success = WebPEncode(&config, &picture); | 72 bool success = WebPEncode(&config, &picture); |
| 73 | 73 |
| 74 // Release the memory allocated by WebPPictureImport*(). This does not free | 74 // Release the memory allocated by WebPPictureImport*(). This does not free |
| 75 // the memory used by the picture object itself. | 75 // the memory used by the picture object itself. |
| 76 WebPPictureFree(&picture); | 76 WebPPictureFree(&picture); |
| 77 | 77 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 SkUnPreMultiply::Scale scale = table[alpha]; | 114 SkUnPreMultiply::Scale scale = table[alpha]; |
| 115 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[0]); | 115 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[0]); |
| 116 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[1]); | 116 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[1]); |
| 117 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[2]); | 117 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[2]); |
| 118 *out_pixels++ = alpha; | 118 *out_pixels++ = alpha; |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 bool PlatformPictureImport(const unsigned char* pixels, | 123 bool PlatformPictureImport(const unsigned char* pixels, |
| 124 WebPPicture* picture) { | 124 WebPPicture* picture, |
| 125 // Need to unpremultiply each pixel, each pixel using 4 bytes (RGBA). | 125 SkAlphaType alphaType) { |
| 126 size_t pixel_count = picture->height * picture->width; | |
| 127 std::vector<unsigned char> unpremul_pixels(pixel_count * 4); | |
| 128 UnPremultiply(pixels, unpremul_pixels.data(), pixel_count); | |
| 129 | |
| 130 // Each pixel uses 4 bytes (RGBA) which affects the stride per row. | 126 // Each pixel uses 4 bytes (RGBA) which affects the stride per row. |
| 131 int row_stride = picture->width * 4; | 127 int row_stride = picture->width * 4; |
| 128 if (alphaType == kPremul_SkAlphaType) { |
| 129 // Need to unpremultiply each pixel, each pixel using 4 bytes (RGBA). |
| 130 size_t pixel_count = picture->height * picture->width; |
| 131 std::vector<unsigned char> unpremul_pixels(pixel_count * 4); |
| 132 UnPremultiply(pixels, unpremul_pixels.data(), pixel_count); |
| 133 if (SK_B32_SHIFT) // Android |
| 134 return WebPPictureImportRGBA(picture, unpremul_pixels.data(), |
| 135 row_stride); |
| 136 return WebPPictureImportBGRA(picture, unpremul_pixels.data(), row_stride); |
| 137 } |
| 132 | 138 |
| 133 if (SK_B32_SHIFT) // Android | 139 if (SK_B32_SHIFT) // Android |
| 134 return WebPPictureImportRGBA(picture, unpremul_pixels.data(), row_stride); | 140 return WebPPictureImportRGBA(picture, pixels, row_stride); |
| 135 return WebPPictureImportBGRA(picture, unpremul_pixels.data(), row_stride); | 141 return WebPPictureImportBGRA(picture, pixels, row_stride); |
| 136 } | 142 } |
| 137 }; | 143 }; |
| 138 | 144 |
| 139 } // namespace | 145 } // namespace |
| 140 | 146 |
| 141 namespace blimp { | 147 namespace blimp { |
| 142 | 148 |
| 143 BlimpImageSerializationProcessor::BlimpImageSerializationProcessor(Mode mode) { | 149 BlimpImageSerializationProcessor::BlimpImageSerializationProcessor(Mode mode) { |
| 144 switch (mode) { | 150 switch (mode) { |
| 145 case Mode::SERIALIZATION: | 151 case Mode::SERIALIZATION: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 160 SkPixelSerializer* BlimpImageSerializationProcessor::GetPixelSerializer() { | 166 SkPixelSerializer* BlimpImageSerializationProcessor::GetPixelSerializer() { |
| 161 return pixel_serializer_.get(); | 167 return pixel_serializer_.get(); |
| 162 } | 168 } |
| 163 | 169 |
| 164 SkPicture::InstallPixelRefProc | 170 SkPicture::InstallPixelRefProc |
| 165 BlimpImageSerializationProcessor::GetPixelDeserializer() { | 171 BlimpImageSerializationProcessor::GetPixelDeserializer() { |
| 166 return pixel_deserializer_; | 172 return pixel_deserializer_; |
| 167 } | 173 } |
| 168 | 174 |
| 169 } // namespace blimp | 175 } // namespace blimp |
| OLD | NEW |