blob: 4e73e4ff7540856f9447a102100fda28ea0be5a3 [file] [log] [blame]
epoger@google.com4ce738b2012-11-16 18:44:18 +00001/*
2 * Copyright 2012 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 */
Matt Sarett5df93de2017-03-22 21:52:47 +00007
8#ifndef SkImageEncoderFns_DEFINED
9#define SkImageEncoderFns_DEFINED
epoger@google.com4ce738b2012-11-16 18:44:18 +000010
11/**
12 * Functions to transform scanlines between packed-pixel formats.
13 */
14
15#include "SkBitmap.h"
16#include "SkColor.h"
17#include "SkColorPriv.h"
Matt Sarett1950e0a2017-06-12 16:17:30 -040018#include "SkColorSpace_Base.h"
Matt Sarett5df93de2017-03-22 21:52:47 +000019#include "SkICC.h"
Matt Sarett2e61b182017-05-09 12:46:50 -040020#include "SkOpts.h"
epoger@google.com4ce738b2012-11-16 18:44:18 +000021#include "SkPreConfig.h"
Matt Sarett84014f02017-01-10 11:28:54 -050022#include "SkRasterPipeline.h"
epoger@google.com4ce738b2012-11-16 18:44:18 +000023#include "SkUnPreMultiply.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050024#include "SkUnPreMultiplyPriv.h"
epoger@google.com4ce738b2012-11-16 18:44:18 +000025
26/**
27 * Function template for transforming scanlines.
28 * Transform 'width' pixels from 'src' buffer into 'dst' buffer,
29 * repacking color channel data as appropriate for the given transformation.
msarettf17b71f2016-09-12 14:30:03 -070030 * 'bpp' is bytes per pixel in the 'src' buffer.
epoger@google.com4ce738b2012-11-16 18:44:18 +000031 */
msarettf17b71f2016-09-12 14:30:03 -070032typedef void (*transform_scanline_proc)(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
Matt Sarett62bb2802017-01-23 12:28:02 -050033 int width, int bpp, const SkPMColor* colors);
epoger@google.com4ce738b2012-11-16 18:44:18 +000034
35/**
36 * Identity transformation: just copy bytes from src to dst.
37 */
Matt Sarett62bb2802017-01-23 12:28:02 -050038static inline void transform_scanline_memcpy(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
39 int width, int bpp, const SkPMColor*) {
msarettf17b71f2016-09-12 14:30:03 -070040 memcpy(dst, src, width * bpp);
epoger@google.com4ce738b2012-11-16 18:44:18 +000041}
42
Matt Sarett62bb2802017-01-23 12:28:02 -050043static inline void transform_scanline_index8_opaque(char* SK_RESTRICT dst,
44 const char* SK_RESTRICT src, int width, int,
45 const SkPMColor* colors) {
46 for (int i = 0; i < width; i++) {
47 const uint32_t c = colors[(uint8_t)*src++];
48 dst[0] = SkGetPackedR32(c);
49 dst[1] = SkGetPackedG32(c);
50 dst[2] = SkGetPackedB32(c);
51 dst += 3;
52 }
53}
54
55static inline void transform_scanline_index8_unpremul(char* SK_RESTRICT dst,
56 const char* SK_RESTRICT src, int width, int,
57 const SkPMColor* colors) {
58 uint32_t* SK_RESTRICT dst32 = (uint32_t*) dst;
59 for (int i = 0; i < width; i++) {
60 // This function swizzles R and B on platforms where SkPMColor is BGRA. This is
61 // exactly what we want.
62 dst32[i] = SkSwizzle_RGBA_to_PMColor(colors[(uint8_t)*src++]);
63 }
64}
65
66static inline void transform_scanline_gray(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
67 int width, int, const SkPMColor* colors) {
68 for (int i = 0; i < width; i++) {
69 const uint8_t g = (uint8_t) *src++;
70 dst[0] = g;
71 dst[1] = g;
72 dst[2] = g;
73 dst += 3;
74 }
75}
76
epoger@google.com4ce738b2012-11-16 18:44:18 +000077/**
78 * Transform from kRGB_565_Config to 3-bytes-per-pixel RGB.
79 * Alpha channel data is not present in kRGB_565_Config format, so there is no
80 * alpha channel data to preserve.
81 */
Matt Sarett62bb2802017-01-23 12:28:02 -050082static inline void transform_scanline_565(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
83 int width, int, const SkPMColor*) {
msarettf17b71f2016-09-12 14:30:03 -070084 const uint16_t* srcP = (const uint16_t*)src;
epoger@google.com4ce738b2012-11-16 18:44:18 +000085 for (int i = 0; i < width; i++) {
86 unsigned c = *srcP++;
87 *dst++ = SkPacked16ToR32(c);
88 *dst++ = SkPacked16ToG32(c);
89 *dst++ = SkPacked16ToB32(c);
90 }
91}
92
93/**
msarettf17b71f2016-09-12 14:30:03 -070094 * Transform from kRGBA_8888_SkColorType to 3-bytes-per-pixel RGB.
95 * Alpha channel data is abandoned.
epoger@google.com4ce738b2012-11-16 18:44:18 +000096 */
Matt Sarett62bb2802017-01-23 12:28:02 -050097static inline void transform_scanline_RGBX(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
98 int width, int, const SkPMColor*) {
msarettf17b71f2016-09-12 14:30:03 -070099 const uint32_t* srcP = (const SkPMColor*)src;
epoger@google.com4ce738b2012-11-16 18:44:18 +0000100 for (int i = 0; i < width; i++) {
msarettf17b71f2016-09-12 14:30:03 -0700101 uint32_t c = *srcP++;
102 *dst++ = (c >> 0) & 0xFF;
103 *dst++ = (c >> 8) & 0xFF;
104 *dst++ = (c >> 16) & 0xFF;
105 }
106}
107
108/**
109 * Transform from kBGRA_8888_SkColorType to 3-bytes-per-pixel RGB.
110 * Alpha channel data is abandoned.
111 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500112static inline void transform_scanline_BGRX(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
113 int width, int, const SkPMColor*) {
msarettf17b71f2016-09-12 14:30:03 -0700114 const uint32_t* srcP = (const SkPMColor*)src;
115 for (int i = 0; i < width; i++) {
116 uint32_t c = *srcP++;
117 *dst++ = (c >> 16) & 0xFF;
118 *dst++ = (c >> 8) & 0xFF;
119 *dst++ = (c >> 0) & 0xFF;
epoger@google.com4ce738b2012-11-16 18:44:18 +0000120 }
121}
122
123/**
124 * Transform from kARGB_4444_Config to 3-bytes-per-pixel RGB.
125 * Alpha channel data, if any, is abandoned.
126 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500127static inline void transform_scanline_444(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
128 int width, int, const SkPMColor*) {
msarettf17b71f2016-09-12 14:30:03 -0700129 const SkPMColor16* srcP = (const SkPMColor16*)src;
epoger@google.com4ce738b2012-11-16 18:44:18 +0000130 for (int i = 0; i < width; i++) {
131 SkPMColor16 c = *srcP++;
132 *dst++ = SkPacked4444ToR32(c);
133 *dst++ = SkPacked4444ToG32(c);
134 *dst++ = SkPacked4444ToB32(c);
135 }
136}
137
epoger@google.com4ce738b2012-11-16 18:44:18 +0000138/**
Matt Sarett84014f02017-01-10 11:28:54 -0500139 * Transform from legacy kPremul, kRGBA_8888_SkColorType to 4-bytes-per-pixel unpremultiplied RGBA.
140 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500141static inline void transform_scanline_rgbA(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
142 int width, int, const SkPMColor*) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500143 SkUnpremultiplyRow<false>((uint32_t*) dst, (const uint32_t*) src, width);
Matt Sarett84014f02017-01-10 11:28:54 -0500144}
145
146/**
147 * Transform from legacy kPremul, kBGRA_8888_SkColorType to 4-bytes-per-pixel unpremultiplied RGBA.
148 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500149static inline void transform_scanline_bgrA(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
150 int width, int, const SkPMColor*) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500151 SkUnpremultiplyRow<true>((uint32_t*) dst, (const uint32_t*) src, width);
Matt Sarett84014f02017-01-10 11:28:54 -0500152}
153
154template <bool kIsRGBA>
Matt Sarett1da27ef2017-01-19 17:14:07 -0500155static inline void transform_scanline_unpremultiply_sRGB(void* dst, const void* src, int width) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400156 SkRasterPipeline_<256> p;
Mike Kleinc2d20762017-06-27 19:53:21 -0400157 if (kIsRGBA) {
158 p.append(SkRasterPipeline::load_8888, &src);
159 } else {
160 p.append(SkRasterPipeline::load_bgra, &src);
Matt Sarett84014f02017-01-10 11:28:54 -0500161 }
162
163 p.append_from_srgb(kPremul_SkAlphaType);
164 p.append(SkRasterPipeline::unpremul);
165 p.append(SkRasterPipeline::to_srgb);
166 p.append(SkRasterPipeline::store_8888, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400167 p.run(0,0, width);
Matt Sarett84014f02017-01-10 11:28:54 -0500168}
169
170/**
Matt Sarett2e61b182017-05-09 12:46:50 -0400171 * Premultiply RGBA to rgbA.
172 */
173static inline void transform_scanline_to_premul_legacy(char* SK_RESTRICT dst,
174 const char* SK_RESTRICT src,
175 int width, int, const SkPMColor*) {
176 SkOpts::RGBA_to_rgbA((uint32_t*)dst, (const uint32_t*)src, width);
177}
178
179/**
180 * Premultiply RGBA to rgbA linearly.
181 */
182static inline void transform_scanline_to_premul_linear(char* SK_RESTRICT dst,
183 const char* SK_RESTRICT src,
184 int width, int, const SkPMColor*) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400185 SkRasterPipeline_<256> p;
Matt Sarett2e61b182017-05-09 12:46:50 -0400186 p.append(SkRasterPipeline::load_8888, (const void**) &src);
187 p.append_from_srgb(kUnpremul_SkAlphaType);
188 p.append(SkRasterPipeline::premul);
189 p.append(SkRasterPipeline::to_srgb);
190 p.append(SkRasterPipeline::store_8888, (void**) &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400191 p.run(0,0, width);
Matt Sarett2e61b182017-05-09 12:46:50 -0400192}
193
194/**
msarettf17b71f2016-09-12 14:30:03 -0700195 * Transform from kPremul, kRGBA_8888_SkColorType to 4-bytes-per-pixel unpremultiplied RGBA.
196 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500197static inline void transform_scanline_srgbA(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
198 int width, int, const SkPMColor*) {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500199 transform_scanline_unpremultiply_sRGB<true>(dst, src, width);
msarettf17b71f2016-09-12 14:30:03 -0700200}
201
202/**
203 * Transform from kPremul, kBGRA_8888_SkColorType to 4-bytes-per-pixel unpremultiplied RGBA.
204 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500205static inline void transform_scanline_sbgrA(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
206 int width, int, const SkPMColor*) {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500207 transform_scanline_unpremultiply_sRGB<false>(dst, src, width);
msarettf17b71f2016-09-12 14:30:03 -0700208}
209
210/**
211 * Transform from kUnpremul, kBGRA_8888_SkColorType to 4-bytes-per-pixel unpremultiplied RGBA.
212 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500213static inline void transform_scanline_BGRA(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
214 int width, int, const SkPMColor*) {
msarettf17b71f2016-09-12 14:30:03 -0700215 const uint32_t* srcP = (const SkPMColor*)src;
216 for (int i = 0; i < width; i++) {
217 uint32_t c = *srcP++;
218 *dst++ = (c >> 16) & 0xFF;
219 *dst++ = (c >> 8) & 0xFF;
220 *dst++ = (c >> 0) & 0xFF;
221 *dst++ = (c >> 24) & 0xFF;
222 }
223}
224
225/**
epoger@google.com4ce738b2012-11-16 18:44:18 +0000226 * Transform from kARGB_8888_Config to 4-bytes-per-pixel RGBA,
227 * with scaling of RGB based on alpha channel.
228 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500229static inline void transform_scanline_4444(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
230 int width, int, const SkPMColor*) {
msarettf17b71f2016-09-12 14:30:03 -0700231 const SkPMColor16* srcP = (const SkPMColor16*)src;
232 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable();
epoger@google.com4ce738b2012-11-16 18:44:18 +0000233
234 for (int i = 0; i < width; i++) {
235 SkPMColor16 c = *srcP++;
236 unsigned a = SkPacked4444ToA32(c);
237 unsigned r = SkPacked4444ToR32(c);
238 unsigned g = SkPacked4444ToG32(c);
239 unsigned b = SkPacked4444ToB32(c);
240
241 if (0 != a && 255 != a) {
242 SkUnPreMultiply::Scale scale = table[a];
243 r = SkUnPreMultiply::ApplyScale(scale, r);
244 g = SkUnPreMultiply::ApplyScale(scale, g);
245 b = SkUnPreMultiply::ApplyScale(scale, b);
246 }
247 *dst++ = r;
248 *dst++ = g;
249 *dst++ = b;
250 *dst++ = a;
251 }
252}
Matt Sarett1da27ef2017-01-19 17:14:07 -0500253
254/**
Matt Sarett55213562017-01-23 19:37:37 -0500255 * Transform from kRGBA_F16 to 8-bytes-per-pixel RGBA.
Matt Sarett1da27ef2017-01-19 17:14:07 -0500256 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500257static inline void transform_scanline_F16(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
258 int width, int, const SkPMColor*) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400259 SkRasterPipeline_<256> p;
Matt Sarett1da27ef2017-01-19 17:14:07 -0500260 p.append(SkRasterPipeline::load_f16, (const void**) &src);
Matt Sarett1950e0a2017-06-12 16:17:30 -0400261 p.append(SkRasterPipeline::to_srgb);
Matt Sarett1da27ef2017-01-19 17:14:07 -0500262 p.append(SkRasterPipeline::store_u16_be, (void**) &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400263 p.run(0,0, width);
Matt Sarett1da27ef2017-01-19 17:14:07 -0500264}
265
266/**
Matt Sarett55213562017-01-23 19:37:37 -0500267 * Transform from kPremul, kRGBA_F16 to 8-bytes-per-pixel RGBA.
Matt Sarett1da27ef2017-01-19 17:14:07 -0500268 */
Matt Sarett62bb2802017-01-23 12:28:02 -0500269static inline void transform_scanline_F16_premul(char* SK_RESTRICT dst, const char* SK_RESTRICT src,
270 int width, int, const SkPMColor*) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400271 SkRasterPipeline_<256> p;
Matt Sarett1da27ef2017-01-19 17:14:07 -0500272 p.append(SkRasterPipeline::load_f16, (const void**) &src);
273 p.append(SkRasterPipeline::unpremul);
Matt Sarett1950e0a2017-06-12 16:17:30 -0400274 p.append(SkRasterPipeline::to_srgb);
Matt Sarett1da27ef2017-01-19 17:14:07 -0500275 p.append(SkRasterPipeline::store_u16_be, (void**) &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400276 p.run(0,0, width);
Matt Sarett1da27ef2017-01-19 17:14:07 -0500277}
Matt Sarett55213562017-01-23 19:37:37 -0500278
279/**
280 * Transform from kRGBA_F16 to 4-bytes-per-pixel RGBA.
281 */
282static inline void transform_scanline_F16_to_8888(char* SK_RESTRICT dst,
283 const char* SK_RESTRICT src, int width, int,
284 const SkPMColor*) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400285 SkRasterPipeline_<256> p;
Matt Sarett55213562017-01-23 19:37:37 -0500286 p.append(SkRasterPipeline::load_f16, (const void**) &src);
287 p.append(SkRasterPipeline::to_srgb);
288 p.append(SkRasterPipeline::store_8888, (void**) &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400289 p.run(0,0, width);
Matt Sarett55213562017-01-23 19:37:37 -0500290}
291
292/**
293 * Transform from kPremul, kRGBA_F16 to 4-bytes-per-pixel RGBA.
294 */
295static inline void transform_scanline_F16_premul_to_8888(char* SK_RESTRICT dst,
296 const char* SK_RESTRICT src, int width,
297 int, const SkPMColor*) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400298 SkRasterPipeline_<256> p;
Matt Sarett55213562017-01-23 19:37:37 -0500299 p.append(SkRasterPipeline::load_f16, (const void**) &src);
300 p.append(SkRasterPipeline::unpremul);
301 p.append(SkRasterPipeline::to_srgb);
302 p.append(SkRasterPipeline::store_8888, (void**) &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400303 p.run(0,0, width);
Matt Sarett55213562017-01-23 19:37:37 -0500304}
Matt Sarett5df93de2017-03-22 21:52:47 +0000305
Matt Sarett2e61b182017-05-09 12:46:50 -0400306/**
307 * Transform from kUnpremul, kRGBA_F16 to premultiplied rgbA 8888.
308 */
309static inline void transform_scanline_F16_to_premul_8888(char* SK_RESTRICT dst,
310 const char* SK_RESTRICT src, int width, int, const SkPMColor*) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400311 SkRasterPipeline_<256> p;
Matt Sarett2e61b182017-05-09 12:46:50 -0400312 p.append(SkRasterPipeline::load_f16, (const void**) &src);
313 p.append(SkRasterPipeline::premul);
314 p.append(SkRasterPipeline::to_srgb);
315 p.append(SkRasterPipeline::store_8888, (void**) &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400316 p.run(0,0, width);
Matt Sarett2e61b182017-05-09 12:46:50 -0400317}
318
Matt Sarett1950e0a2017-06-12 16:17:30 -0400319static inline sk_sp<SkData> icc_from_color_space(const SkImageInfo& info) {
320 SkColorSpace* cs = info.colorSpace();
321 if (!cs) {
322 return nullptr;
323 }
324
325 sk_sp<SkColorSpace> owned;
326 if (kRGBA_F16_SkColorType == info.colorType()) {
327 owned = as_CSB(cs)->makeSRGBGamma();
328 cs = owned.get();
329 }
330
Matt Sarett5df93de2017-03-22 21:52:47 +0000331 SkColorSpaceTransferFn fn;
332 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
Matt Sarett1950e0a2017-06-12 16:17:30 -0400333 if (cs->isNumericalTransferFn(&fn) && cs->toXYZD50(&toXYZD50)) {
Matt Sarett5df93de2017-03-22 21:52:47 +0000334 return SkICC::WriteToICC(fn, toXYZD50);
335 }
336
337 // TODO: Should we support writing ICC profiles for additional color spaces?
338 return nullptr;
339}
340
341#endif // SkImageEncoderFns_DEFINED