| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2010, The Android Open Source Project | |
| 3 * | |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 * you may not use this file except in compliance with the License. | |
| 6 * You may obtain a copy of the License at | |
| 7 * | |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 * | |
| 10 * Unless required by applicable law or agreed to in writing, software | |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 * See the License for the specific language governing permissions and | |
| 14 * limitations under the License. | |
| 15 */ | |
| 16 | |
| 17 #include "SkBitmap.h" | |
| 18 #include "SkImageEncoder.h" | |
| 19 #include "SkColorPriv.h" | |
| 20 #include "SkStream.h" | |
| 21 #include "SkTemplates.h" | |
| 22 #include "SkUtils.h" | |
| 23 | |
| 24 // A WebP decoder only, on top of (subset of) libwebp | |
| 25 // For more information on WebP image format, and libwebp library, see: | |
| 26 // http://code.google.com/speed/webp/ | |
| 27 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library | |
| 28 // http://review.webmproject.org/gitweb?p=libwebp.git | |
| 29 | |
| 30 #include <stdio.h> | |
| 31 extern "C" { | |
| 32 // If moving libwebp out of skia source tree, path for webp headers must be | |
| 33 // updated accordingly. Here, we enforce using local copy in webp sub-directory. | |
| 34 #include "webp/encode.h" | |
| 35 } | |
| 36 | |
| 37 #include "SkUnPreMultiply.h" | |
| 38 | |
| 39 typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width, | |
| 40 const SkPMColor* SK_RESTRICT ctable); | |
| 41 | |
| 42 static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width, | |
| 43 const SkPMColor*) { | |
| 44 const uint32_t* SK_RESTRICT src = (const uint32_t*)in; | |
| 45 for (int i = 0; i < width; ++i) { | |
| 46 const uint32_t c = *src++; | |
| 47 rgb[0] = SkGetPackedR32(c); | |
| 48 rgb[1] = SkGetPackedG32(c); | |
| 49 rgb[2] = SkGetPackedB32(c); | |
| 50 rgb += 3; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 static void ARGB_8888_To_RGBA(const uint8_t* in, uint8_t* rgb, int width, | |
| 55 const SkPMColor*) { | |
| 56 const uint32_t* SK_RESTRICT src = (const uint32_t*)in; | |
| 57 const SkUnPreMultiply::Scale* SK_RESTRICT table = | |
| 58 SkUnPreMultiply::GetScaleTable(); | |
| 59 for (int i = 0; i < width; ++i) { | |
| 60 const uint32_t c = *src++; | |
| 61 uint8_t a = SkGetPackedA32(c); | |
| 62 uint8_t r = SkGetPackedR32(c); | |
| 63 uint8_t g = SkGetPackedG32(c); | |
| 64 uint8_t b = SkGetPackedB32(c); | |
| 65 if (0 != a && 255 != a) { | |
| 66 SkUnPreMultiply::Scale scale = table[a]; | |
| 67 r = SkUnPreMultiply::ApplyScale(scale, r); | |
| 68 g = SkUnPreMultiply::ApplyScale(scale, g); | |
| 69 b = SkUnPreMultiply::ApplyScale(scale, b); | |
| 70 } | |
| 71 rgb[0] = r; | |
| 72 rgb[1] = g; | |
| 73 rgb[2] = b; | |
| 74 rgb[3] = a; | |
| 75 rgb += 4; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width, | |
| 80 const SkPMColor*) { | |
| 81 const uint16_t* SK_RESTRICT src = (const uint16_t*)in; | |
| 82 for (int i = 0; i < width; ++i) { | |
| 83 const uint16_t c = *src++; | |
| 84 rgb[0] = SkPacked16ToR32(c); | |
| 85 rgb[1] = SkPacked16ToG32(c); | |
| 86 rgb[2] = SkPacked16ToB32(c); | |
| 87 rgb += 3; | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width, | |
| 92 const SkPMColor*) { | |
| 93 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in; | |
| 94 for (int i = 0; i < width; ++i) { | |
| 95 const SkPMColor16 c = *src++; | |
| 96 rgb[0] = SkPacked4444ToR32(c); | |
| 97 rgb[1] = SkPacked4444ToG32(c); | |
| 98 rgb[2] = SkPacked4444ToB32(c); | |
| 99 rgb += 3; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 static void ARGB_4444_To_RGBA(const uint8_t* in, uint8_t* rgb, int width, | |
| 104 const SkPMColor*) { | |
| 105 const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in; | |
| 106 const SkUnPreMultiply::Scale* SK_RESTRICT table = | |
| 107 SkUnPreMultiply::GetScaleTable(); | |
| 108 for (int i = 0; i < width; ++i) { | |
| 109 const SkPMColor16 c = *src++; | |
| 110 uint8_t a = SkPacked4444ToA32(c); | |
| 111 uint8_t r = SkPacked4444ToR32(c); | |
| 112 uint8_t g = SkPacked4444ToG32(c); | |
| 113 uint8_t b = SkPacked4444ToB32(c); | |
| 114 if (0 != a && 255 != a) { | |
| 115 SkUnPreMultiply::Scale scale = table[a]; | |
| 116 r = SkUnPreMultiply::ApplyScale(scale, r); | |
| 117 g = SkUnPreMultiply::ApplyScale(scale, g); | |
| 118 b = SkUnPreMultiply::ApplyScale(scale, b); | |
| 119 } | |
| 120 rgb[0] = r; | |
| 121 rgb[1] = g; | |
| 122 rgb[2] = b; | |
| 123 rgb[3] = a; | |
| 124 rgb += 4; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width, | |
| 129 const SkPMColor* SK_RESTRICT ctable) { | |
| 130 const uint8_t* SK_RESTRICT src = (const uint8_t*)in; | |
| 131 for (int i = 0; i < width; ++i) { | |
| 132 const uint32_t c = ctable[*src++]; | |
| 133 rgb[0] = SkGetPackedR32(c); | |
| 134 rgb[1] = SkGetPackedG32(c); | |
| 135 rgb[2] = SkGetPackedB32(c); | |
| 136 rgb += 3; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 static ScanlineImporter ChooseImporter(SkColorType ct, bool hasAlpha, int* bpp
) { | |
| 141 switch (ct) { | |
| 142 case kN32_SkColorType: | |
| 143 if (hasAlpha) { | |
| 144 *bpp = 4; | |
| 145 return ARGB_8888_To_RGBA; | |
| 146 } else { | |
| 147 *bpp = 3; | |
| 148 return ARGB_8888_To_RGB; | |
| 149 } | |
| 150 case kARGB_4444_SkColorType: | |
| 151 if (hasAlpha) { | |
| 152 *bpp = 4; | |
| 153 return ARGB_4444_To_RGBA; | |
| 154 } else { | |
| 155 *bpp = 3; | |
| 156 return ARGB_4444_To_RGB; | |
| 157 } | |
| 158 case kRGB_565_SkColorType: | |
| 159 *bpp = 3; | |
| 160 return RGB_565_To_RGB; | |
| 161 case kIndex_8_SkColorType: | |
| 162 *bpp = 3; | |
| 163 return Index8_To_RGB; | |
| 164 default: | |
| 165 return nullptr; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 static int stream_writer(const uint8_t* data, size_t data_size, | |
| 170 const WebPPicture* const picture) { | |
| 171 SkWStream* const stream = (SkWStream*)picture->custom_ptr; | |
| 172 return stream->write(data, data_size) ? 1 : 0; | |
| 173 } | |
| 174 | |
| 175 class SkWEBPImageEncoder : public SkImageEncoder { | |
| 176 protected: | |
| 177 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override; | |
| 178 | |
| 179 private: | |
| 180 typedef SkImageEncoder INHERITED; | |
| 181 }; | |
| 182 | |
| 183 bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm, | |
| 184 int quality) { | |
| 185 const bool hasAlpha = !bm.isOpaque(); | |
| 186 int bpp = -1; | |
| 187 const ScanlineImporter scanline_import = ChooseImporter(bm.colorType(), hasA
lpha, &bpp); | |
| 188 if (nullptr == scanline_import) { | |
| 189 return false; | |
| 190 } | |
| 191 if (-1 == bpp) { | |
| 192 return false; | |
| 193 } | |
| 194 | |
| 195 SkAutoLockPixels alp(bm); | |
| 196 if (nullptr == bm.getPixels()) { | |
| 197 return false; | |
| 198 } | |
| 199 | |
| 200 WebPConfig webp_config; | |
| 201 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) { | |
| 202 return false; | |
| 203 } | |
| 204 | |
| 205 WebPPicture pic; | |
| 206 WebPPictureInit(&pic); | |
| 207 pic.width = bm.width(); | |
| 208 pic.height = bm.height(); | |
| 209 pic.writer = stream_writer; | |
| 210 pic.custom_ptr = (void*)stream; | |
| 211 | |
| 212 const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColor
s() : nullptr; | |
| 213 const uint8_t* src = (uint8_t*)bm.getPixels(); | |
| 214 const int rgbStride = pic.width * bpp; | |
| 215 | |
| 216 // Import (for each scanline) the bit-map image (in appropriate color-space) | |
| 217 // to RGB color space. | |
| 218 uint8_t* rgb = new uint8_t[rgbStride * pic.height]; | |
| 219 for (int y = 0; y < pic.height; ++y) { | |
| 220 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride, | |
| 221 pic.width, colors); | |
| 222 } | |
| 223 | |
| 224 bool ok; | |
| 225 if (bpp == 3) { | |
| 226 ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride)); | |
| 227 } else { | |
| 228 ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride)); | |
| 229 } | |
| 230 delete[] rgb; | |
| 231 | |
| 232 ok = ok && WebPEncode(&webp_config, &pic); | |
| 233 WebPPictureFree(&pic); | |
| 234 | |
| 235 return ok; | |
| 236 } | |
| 237 | |
| 238 | |
| 239 /////////////////////////////////////////////////////////////////////////////// | |
| 240 DEFINE_ENCODER_CREATOR(WEBPImageEncoder); | |
| 241 /////////////////////////////////////////////////////////////////////////////// | |
| 242 | |
| 243 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { | |
| 244 return (SkImageEncoder::kWEBP_Type == t) ? new SkWEBPImageEncoder : nullptr; | |
| 245 } | |
| 246 | |
| 247 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); | |
| OLD | NEW |