1/*
2 * Copyright 2010 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "src/images/SkImageEncoderPriv.h"
9
10#ifdef SK_ENCODE_WEBP
11
12#include "include/core/SkBitmap.h"
13#include "include/core/SkStream.h"
14#include "include/core/SkUnPreMultiply.h"
15#include "include/encode/SkWebpEncoder.h"
16#include "include/private/SkColorData.h"
17#include "include/private/SkTemplates.h"
18#include "src/images/SkImageEncoderFns.h"
19#include "src/utils/SkUTF.h"
20
21// A WebP encoder only, on top of (subset of) libwebp
22// For more information on WebP image format, and libwebp library, see:
23// http://code.google.com/speed/webp/
24// http://www.webmproject.org/code/#libwebp_webp_image_decoder_library
25// http://review.webmproject.org/gitweb?p=libwebp.git
26
27#include <stdio.h>
28extern "C" {
29// If moving libwebp out of skia source tree, path for webp headers must be
30// updated accordingly. Here, we enforce using local copy in webp sub-directory.
31#include "webp/encode.h"
32#include "webp/mux.h"
33}
34
35static transform_scanline_proc choose_proc(const SkImageInfo& info) {
36 switch (info.colorType()) {
37 case kRGBA_8888_SkColorType:
38 switch (info.alphaType()) {
39 case kOpaque_SkAlphaType:
40 return transform_scanline_RGBX;
41 case kUnpremul_SkAlphaType:
42 return transform_scanline_memcpy;
43 case kPremul_SkAlphaType:
44 return transform_scanline_rgbA;
45 default:
46 return nullptr;
47 }
48 case kBGRA_8888_SkColorType:
49 switch (info.alphaType()) {
50 case kOpaque_SkAlphaType:
51 return transform_scanline_BGRX;
52 case kUnpremul_SkAlphaType:
53 return transform_scanline_BGRA;
54 case kPremul_SkAlphaType:
55 return transform_scanline_bgrA;
56 default:
57 return nullptr;
58 }
59 case kRGB_565_SkColorType:
60 if (!info.isOpaque()) {
61 return nullptr;
62 }
63
64 return transform_scanline_565;
65 case kARGB_4444_SkColorType:
66 switch (info.alphaType()) {
67 case kOpaque_SkAlphaType:
68 return transform_scanline_444;
69 case kPremul_SkAlphaType:
70 return transform_scanline_4444;
71 default:
72 return nullptr;
73 }
74 case kGray_8_SkColorType:
75 return transform_scanline_gray;
76 case kRGBA_F16_SkColorType:
77 switch (info.alphaType()) {
78 case kOpaque_SkAlphaType:
79 case kUnpremul_SkAlphaType:
80 return transform_scanline_F16_to_8888;
81 case kPremul_SkAlphaType:
82 return transform_scanline_F16_premul_to_8888;
83 default:
84 return nullptr;
85 }
86 default:
87 return nullptr;
88 }
89}
90
91static int stream_writer(const uint8_t* data, size_t data_size,
92 const WebPPicture* const picture) {
93 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
94 return stream->write(data, data_size) ? 1 : 0;
95}
96
97bool SkWebpEncoder::Encode(SkWStream* stream, const SkPixmap& pixmap, const Options& opts) {
98 if (!SkPixmapIsValid(pixmap)) {
99 return false;
100 }
101
102 const transform_scanline_proc proc = choose_proc(pixmap.info());
103 if (!proc) {
104 return false;
105 }
106
107 int bpp;
108 if (kRGBA_F16_SkColorType == pixmap.colorType()) {
109 bpp = 4;
110 } else {
111 bpp = pixmap.isOpaque() ? 3 : 4;
112 }
113
114 if (nullptr == pixmap.addr()) {
115 return false;
116 }
117
118 WebPConfig webp_config;
119 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, opts.fQuality)) {
120 return false;
121 }
122
123 WebPPicture pic;
124 WebPPictureInit(&pic);
125 SkAutoTCallVProc<WebPPicture, WebPPictureFree> autoPic(&pic);
126 pic.width = pixmap.width();
127 pic.height = pixmap.height();
128 pic.writer = stream_writer;
129
130 // Set compression, method, and pixel format.
131 // libwebp recommends using BGRA for lossless and YUV for lossy.
132 // The choices of |webp_config.method| currently just match Chrome's defaults. We
133 // could potentially expose this decision to the client.
134 if (Compression::kLossy == opts.fCompression) {
135 webp_config.lossless = 0;
136#ifndef SK_WEBP_ENCODER_USE_DEFAULT_METHOD
137 webp_config.method = 3;
138#endif
139 pic.use_argb = 0;
140 } else {
141 webp_config.lossless = 1;
142 webp_config.method = 0;
143 pic.use_argb = 1;
144 }
145
146 // If there is no need to embed an ICC profile, we write directly to the input stream.
147 // Otherwise, we will first encode to |tmp| and use a mux to add the ICC chunk. libwebp
148 // forces us to have an encoded image before we can add a profile.
149 sk_sp<SkData> icc = icc_from_color_space(pixmap.info());
150 SkDynamicMemoryWStream tmp;
151 pic.custom_ptr = icc ? (void*)&tmp : (void*)stream;
152
153 const uint8_t* src = (uint8_t*)pixmap.addr();
154 const int rgbStride = pic.width * bpp;
155 const size_t rowBytes = pixmap.rowBytes();
156
157 // Import (for each scanline) the bit-map image (in appropriate color-space)
158 // to RGB color space.
159 std::unique_ptr<uint8_t[]> rgb(new uint8_t[rgbStride * pic.height]);
160 for (int y = 0; y < pic.height; ++y) {
161 proc((char*) &rgb[y * rgbStride],
162 (const char*) &src[y * rowBytes],
163 pic.width,
164 bpp);
165 }
166
167 auto importProc = WebPPictureImportRGB;
168 if (3 != bpp) {
169 if (pixmap.isOpaque()) {
170 importProc = WebPPictureImportRGBX;
171 } else {
172 importProc = WebPPictureImportRGBA;
173 }
174 }
175
176 if (!importProc(&pic, &rgb[0], rgbStride)) {
177 return false;
178 }
179
180 if (!WebPEncode(&webp_config, &pic)) {
181 return false;
182 }
183
184 if (icc) {
185 sk_sp<SkData> encodedData = tmp.detachAsData();
186 WebPData encoded = { encodedData->bytes(), encodedData->size() };
187 WebPData iccChunk = { icc->bytes(), icc->size() };
188
189 SkAutoTCallVProc<WebPMux, WebPMuxDelete> mux(WebPMuxNew());
190 if (WEBP_MUX_OK != WebPMuxSetImage(mux, &encoded, 0)) {
191 return false;
192 }
193
194 if (WEBP_MUX_OK != WebPMuxSetChunk(mux, "ICCP", &iccChunk, 0)) {
195 return false;
196 }
197
198 WebPData assembled;
199 if (WEBP_MUX_OK != WebPMuxAssemble(mux, &assembled)) {
200 return false;
201 }
202
203 stream->write(assembled.bytes, assembled.size);
204 WebPDataClear(&assembled);
205 }
206
207 return true;
208}
209
210#endif
211