blob: 116608a253e2d1db65bc5b2d3e5258bf7c285435 [file] [log] [blame]
commit-bot@chromium.orga936e372013-03-14 14:42:18 +00001/*
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
msarette8597a42016-03-24 10:41:47 -070017#include "SkBitmap.h"
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000018#include "SkImageEncoder.h"
19#include "SkColorPriv.h"
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000020#include "SkStream.h"
21#include "SkTemplates.h"
22#include "SkUtils.h"
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000023
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>
31extern "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.
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000034#include "webp/encode.h"
35}
36
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +000037#include "SkUnPreMultiply.h"
38
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000039typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
40 const SkPMColor* SK_RESTRICT ctable);
41
42static 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
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +000054static 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
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000079static 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
91static 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
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000103static 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
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000128static 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
reed0689d7b2014-06-14 05:30:20 -0700140static ScanlineImporter ChooseImporter(SkColorType ct, bool hasAlpha, int* bpp) {
141 switch (ct) {
142 case kN32_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000143 if (hasAlpha) {
144 *bpp = 4;
145 return ARGB_8888_To_RGBA;
146 } else {
147 *bpp = 3;
148 return ARGB_8888_To_RGB;
149 }
reed0689d7b2014-06-14 05:30:20 -0700150 case kARGB_4444_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000151 if (hasAlpha) {
152 *bpp = 4;
153 return ARGB_4444_To_RGBA;
154 } else {
155 *bpp = 3;
156 return ARGB_4444_To_RGB;
157 }
reed0689d7b2014-06-14 05:30:20 -0700158 case kRGB_565_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000159 *bpp = 3;
160 return RGB_565_To_RGB;
reed0689d7b2014-06-14 05:30:20 -0700161 case kIndex_8_SkColorType:
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000162 *bpp = 3;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000163 return Index8_To_RGB;
164 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700165 return nullptr;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000166 }
167}
168
169static 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
175class SkWEBPImageEncoder : public SkImageEncoder {
176protected:
mtklein36352bf2015-03-25 18:17:31 -0700177 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000178
179private:
180 typedef SkImageEncoder INHERITED;
181};
182
183bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
184 int quality) {
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000185 const bool hasAlpha = !bm.isOpaque();
186 int bpp = -1;
reed0689d7b2014-06-14 05:30:20 -0700187 const ScanlineImporter scanline_import = ChooseImporter(bm.colorType(), hasAlpha, &bpp);
halcanary96fcdcc2015-08-27 07:41:13 -0700188 if (nullptr == scanline_import) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000189 return false;
190 }
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000191 if (-1 == bpp) {
192 return false;
193 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000194
195 SkAutoLockPixels alp(bm);
halcanary96fcdcc2015-08-27 07:41:13 -0700196 if (nullptr == bm.getPixels()) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000197 return false;
198 }
199
200 WebPConfig webp_config;
commit-bot@chromium.orgbff83f62013-03-14 15:18:08 +0000201 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000202 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
halcanary96fcdcc2015-08-27 07:41:13 -0700212 const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColors() : nullptr;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000213 const uint8_t* src = (uint8_t*)bm.getPixels();
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000214 const int rgbStride = pic.width * bpp;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000215
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
commit-bot@chromium.org5007aab2014-02-26 21:35:17 +0000224 bool ok;
225 if (bpp == 3) {
226 ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
227 } else {
228 ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride));
229 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000230 delete[] rgb;
231
232 ok = ok && WebPEncode(&webp_config, &pic);
233 WebPPictureFree(&pic);
234
235 return ok;
236}
237
238
239///////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000240DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
241///////////////////////////////////////////////////////////////////////////////
242
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000243static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
halcanary96fcdcc2015-08-27 07:41:13 -0700244 return (SkImageEncoder::kWEBP_Type == t) ? new SkWEBPImageEncoder : nullptr;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000245}
246
mtklein@google.combd6343b2013-09-04 17:20:18 +0000247static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory);