blob: e1e2ecb623b8faf2c040cf496eaaa4fa9e95deca [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
2 * Copyright 2015 Google Inc.
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
msarett74114382015-03-16 11:55:18 -07008#include "SkCodecPriv.h"
Cary Clarka4083c92017-09-15 11:59:23 -04009#include "SkColorData.h"
Matt Saretta225e9b2016-11-07 10:26:17 -050010#include "SkHalf.h"
msaretta51e7782016-01-12 06:51:11 -080011#include "SkOpts.h"
scroggof24f2242015-03-03 08:59:20 -080012#include "SkSwizzler.h"
13#include "SkTemplates.h"
14
msarettbda86092016-01-19 10:40:12 -080015static void copy(void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
16 const SkPMColor ctable[]) {
17 // This function must not be called if we are sampling. If we are not
18 // sampling, deltaSrc should equal bpp.
19 SkASSERT(deltaSrc == bpp);
emmaleer8f4ba762015-08-14 07:44:46 -070020
msarettbda86092016-01-19 10:40:12 -080021 memcpy(dst, src + offset, width * bpp);
22}
23
24static void sample1(void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
25 const SkPMColor ctable[]) {
emmaleer8f4ba762015-08-14 07:44:46 -070026 src += offset;
msarettbda86092016-01-19 10:40:12 -080027 uint8_t* dst8 = (uint8_t*) dst;
emmaleer8f4ba762015-08-14 07:44:46 -070028 for (int x = 0; x < width; x++) {
msarettbda86092016-01-19 10:40:12 -080029 dst8[x] = *src;
emmaleer8f4ba762015-08-14 07:44:46 -070030 src += deltaSrc;
31 }
emmaleer8f4ba762015-08-14 07:44:46 -070032}
33
msarettbda86092016-01-19 10:40:12 -080034static void sample2(void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
35 const SkPMColor ctable[]) {
36 src += offset;
37 uint16_t* dst16 = (uint16_t*) dst;
38 for (int x = 0; x < width; x++) {
39 dst16[x] = *((const uint16_t*) src);
40 src += deltaSrc;
41 }
42}
43
44static void sample4(void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
45 const SkPMColor ctable[]) {
46 src += offset;
47 uint32_t* dst32 = (uint32_t*) dst;
48 for (int x = 0; x < width; x++) {
49 dst32[x] = *((const uint32_t*) src);
50 src += deltaSrc;
51 }
52}
msarett5406d6f2015-08-31 06:55:13 -070053
Matt Sarett34c69d62017-01-19 17:42:23 -050054static void sample6(void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
55 const SkPMColor ctable[]) {
56 src += offset;
57 uint8_t* dst8 = (uint8_t*) dst;
58 for (int x = 0; x < width; x++) {
59 memcpy(dst8, src, 6);
60 dst8 += 6;
61 src += deltaSrc;
62 }
63}
64
Matt Sarett379938e2017-01-12 18:34:29 -050065static void sample8(void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
66 const SkPMColor ctable[]) {
67 src += offset;
68 uint64_t* dst64 = (uint64_t*) dst;
69 for (int x = 0; x < width; x++) {
70 dst64[x] = *((const uint64_t*) src);
71 src += deltaSrc;
72 }
73}
74
msarett99f567e2015-08-05 12:58:26 -070075// kBit
76// These routines exclusively choose between white and black
77
78#define GRAYSCALE_BLACK 0
79#define GRAYSCALE_WHITE 0xFF
80
emmaleer8f4ba762015-08-14 07:44:46 -070081
82// same as swizzle_bit_to_index and swizzle_bit_to_n32 except for value assigned to dst[x]
msaretta4970dc2016-01-11 07:23:23 -080083static void swizzle_bit_to_grayscale(
emmaleer8f4ba762015-08-14 07:44:46 -070084 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -070085 int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) {
emmaleer8f4ba762015-08-14 07:44:46 -070086
msarett99f567e2015-08-05 12:58:26 -070087 uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow;
88
emmaleer8f4ba762015-08-14 07:44:46 -070089 // increment src by byte offset and bitIndex by bit offset
90 src += offset / 8;
91 int bitIndex = offset % 8;
92 uint8_t currByte = *src;
93
94 dst[0] = ((currByte >> (7-bitIndex)) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
95
96 for (int x = 1; x < dstWidth; x++) {
97 int bitOffset = bitIndex + deltaSrc;
98 bitIndex = bitOffset % 8;
99 currByte = *(src += bitOffset / 8);
100 dst[x] = ((currByte >> (7-bitIndex)) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
msarett99f567e2015-08-05 12:58:26 -0700101 }
msarett99f567e2015-08-05 12:58:26 -0700102}
103
104#undef GRAYSCALE_BLACK
105#undef GRAYSCALE_WHITE
106
emmaleer8f4ba762015-08-14 07:44:46 -0700107// same as swizzle_bit_to_grayscale and swizzle_bit_to_index except for value assigned to dst[x]
msaretta4970dc2016-01-11 07:23:23 -0800108static void swizzle_bit_to_n32(
emmaleer8f4ba762015-08-14 07:44:46 -0700109 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700110 int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) {
msarett99f567e2015-08-05 12:58:26 -0700111 SkPMColor* SK_RESTRICT dst = (SkPMColor*) dstRow;
112
emmaleer8f4ba762015-08-14 07:44:46 -0700113 // increment src by byte offset and bitIndex by bit offset
114 src += offset / 8;
115 int bitIndex = offset % 8;
116 uint8_t currByte = *src;
117
118 dst[0] = ((currByte >> (7 - bitIndex)) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
119
120 for (int x = 1; x < dstWidth; x++) {
121 int bitOffset = bitIndex + deltaSrc;
122 bitIndex = bitOffset % 8;
123 currByte = *(src += bitOffset / 8);
124 dst[x] = ((currByte >> (7 - bitIndex)) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
msarett99f567e2015-08-05 12:58:26 -0700125 }
msarett99f567e2015-08-05 12:58:26 -0700126}
127
scroggocc2feb12015-08-14 08:32:46 -0700128#define RGB565_BLACK 0
129#define RGB565_WHITE 0xFFFF
130
msaretta4970dc2016-01-11 07:23:23 -0800131static void swizzle_bit_to_565(
scroggocc2feb12015-08-14 08:32:46 -0700132 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700133 int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) {
scroggocc2feb12015-08-14 08:32:46 -0700134 uint16_t* SK_RESTRICT dst = (uint16_t*) dstRow;
135
136 // increment src by byte offset and bitIndex by bit offset
137 src += offset / 8;
138 int bitIndex = offset % 8;
139 uint8_t currByte = *src;
140
141 dst[0] = ((currByte >> (7 - bitIndex)) & 1) ? RGB565_WHITE : RGB565_BLACK;
142
143 for (int x = 1; x < dstWidth; x++) {
144 int bitOffset = bitIndex + deltaSrc;
145 bitIndex = bitOffset % 8;
146 currByte = *(src += bitOffset / 8);
147 dst[x] = ((currByte >> (7 - bitIndex)) & 1) ? RGB565_WHITE : RGB565_BLACK;
148 }
scroggocc2feb12015-08-14 08:32:46 -0700149}
150
151#undef RGB565_BLACK
152#undef RGB565_WHITE
153
Matt Saretta225e9b2016-11-07 10:26:17 -0500154static void swizzle_bit_to_f16(
155 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
156 int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) {
Leon Scroggins III862c1962017-10-02 16:28:49 -0400157 constexpr uint64_t kWhite = (((uint64_t) SK_Half1) << 0) |
158 (((uint64_t) SK_Half1) << 16) |
159 (((uint64_t) SK_Half1) << 32) |
160 (((uint64_t) SK_Half1) << 48);
161 constexpr uint64_t kBlack = (((uint64_t) 0) << 0) |
162 (((uint64_t) 0) << 16) |
163 (((uint64_t) 0) << 32) |
164 (((uint64_t) SK_Half1) << 48);
Matt Saretta225e9b2016-11-07 10:26:17 -0500165
166 uint64_t* SK_RESTRICT dst = (uint64_t*) dstRow;
167
168 // increment src by byte offset and bitIndex by bit offset
169 src += offset / 8;
170 int bitIndex = offset % 8;
171 uint8_t currByte = *src;
172
173 dst[0] = ((currByte >> (7 - bitIndex)) & 1) ? kWhite : kBlack;
174
175 for (int x = 1; x < dstWidth; x++) {
176 int bitOffset = bitIndex + deltaSrc;
177 bitIndex = bitOffset % 8;
178 currByte = *(src += bitOffset / 8);
179 dst[x] = ((currByte >> (7 - bitIndex)) & 1) ? kWhite : kBlack;
180 }
181}
182
msarett74114382015-03-16 11:55:18 -0700183// kIndex1, kIndex2, kIndex4
184
msaretta4970dc2016-01-11 07:23:23 -0800185static void swizzle_small_index_to_565(
scroggocc2feb12015-08-14 08:32:46 -0700186 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700187 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
scroggocc2feb12015-08-14 08:32:46 -0700188
msarett5406d6f2015-08-31 06:55:13 -0700189 uint16_t* dst = (uint16_t*) dstRow;
190 src += offset / 8;
191 int bitIndex = offset % 8;
192 uint8_t currByte = *src;
193 const uint8_t mask = (1 << bpp) - 1;
194 uint8_t index = (currByte >> (8 - bpp - bitIndex)) & mask;
195 dst[0] = SkPixel32ToPixel16(ctable[index]);
196
197 for (int x = 1; x < dstWidth; x++) {
198 int bitOffset = bitIndex + deltaSrc;
199 bitIndex = bitOffset % 8;
200 currByte = *(src += bitOffset / 8);
201 index = (currByte >> (8 - bpp - bitIndex)) & mask;
202 dst[x] = SkPixel32ToPixel16(ctable[index]);
scroggocc2feb12015-08-14 08:32:46 -0700203 }
scroggocc2feb12015-08-14 08:32:46 -0700204}
205
msaretta4970dc2016-01-11 07:23:23 -0800206static void swizzle_small_index_to_n32(
emmaleer8f4ba762015-08-14 07:44:46 -0700207 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700208 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
msarett74114382015-03-16 11:55:18 -0700209
msarett5406d6f2015-08-31 06:55:13 -0700210 SkPMColor* dst = (SkPMColor*) dstRow;
msarett5406d6f2015-08-31 06:55:13 -0700211 src += offset / 8;
212 int bitIndex = offset % 8;
213 uint8_t currByte = *src;
214 const uint8_t mask = (1 << bpp) - 1;
215 uint8_t index = (currByte >> (8 - bpp - bitIndex)) & mask;
216 dst[0] = ctable[index];
msarett5406d6f2015-08-31 06:55:13 -0700217
218 for (int x = 1; x < dstWidth; x++) {
219 int bitOffset = bitIndex + deltaSrc;
220 bitIndex = bitOffset % 8;
221 currByte = *(src += bitOffset / 8);
222 index = (currByte >> (8 - bpp - bitIndex)) & mask;
223 dst[x] = ctable[index];
msarett74114382015-03-16 11:55:18 -0700224 }
msarett74114382015-03-16 11:55:18 -0700225}
226
msarette16b04a2015-04-15 07:32:19 -0700227// kIndex
228
msaretta4970dc2016-01-11 07:23:23 -0800229static void swizzle_index_to_n32(
emmaleer8f4ba762015-08-14 07:44:46 -0700230 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700231 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
emmaleer8f4ba762015-08-14 07:44:46 -0700232
233 src += offset;
234 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700235 for (int x = 0; x < dstWidth; x++) {
236 SkPMColor c = ctable[*src];
emmaleer8f4ba762015-08-14 07:44:46 -0700237 dst[x] = c;
238 src += deltaSrc;
239 }
emmaleer8f4ba762015-08-14 07:44:46 -0700240}
241
msaretta4970dc2016-01-11 07:23:23 -0800242static void swizzle_index_to_n32_skipZ(
emmaleer8f4ba762015-08-14 07:44:46 -0700243 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700244 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
emmaleer8f4ba762015-08-14 07:44:46 -0700245
246 src += offset;
247 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700248 for (int x = 0; x < dstWidth; x++) {
249 SkPMColor c = ctable[*src];
emmaleer8f4ba762015-08-14 07:44:46 -0700250 if (c != 0) {
251 dst[x] = c;
252 }
253 src += deltaSrc;
254 }
emmaleer8f4ba762015-08-14 07:44:46 -0700255}
256
msaretta4970dc2016-01-11 07:23:23 -0800257static void swizzle_index_to_565(
emmaleer8f4ba762015-08-14 07:44:46 -0700258 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700259 int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) {
emmaleer8f4ba762015-08-14 07:44:46 -0700260 src += offset;
scroggoab60c5b2015-08-06 06:08:18 -0700261 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700262 for (int x = 0; x < dstWidth; x++) {
scroggoab60c5b2015-08-06 06:08:18 -0700263 dst[x] = SkPixel32ToPixel16(ctable[*src]);
msarett5406d6f2015-08-31 06:55:13 -0700264 src += deltaSrc;
scroggoab60c5b2015-08-06 06:08:18 -0700265 }
scroggoab60c5b2015-08-06 06:08:18 -0700266}
267
msarette16b04a2015-04-15 07:32:19 -0700268// kGray
269
msaretta4970dc2016-01-11 07:23:23 -0800270static void swizzle_gray_to_n32(
emmaleer8f4ba762015-08-14 07:44:46 -0700271 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700272 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
msarette16b04a2015-04-15 07:32:19 -0700273
emmaleer8f4ba762015-08-14 07:44:46 -0700274 src += offset;
msarette16b04a2015-04-15 07:32:19 -0700275 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700276 for (int x = 0; x < dstWidth; x++) {
277 dst[x] = SkPackARGB32NoCheck(0xFF, *src, *src, *src);
278 src += deltaSrc;
msarette16b04a2015-04-15 07:32:19 -0700279 }
msarette16b04a2015-04-15 07:32:19 -0700280}
281
msarett2eff71c2016-02-02 12:59:45 -0800282static void fast_swizzle_gray_to_n32(
283 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
284 const SkPMColor ctable[]) {
285
286 // This function must not be called if we are sampling. If we are not
287 // sampling, deltaSrc should equal bpp.
288 SkASSERT(deltaSrc == bpp);
289
290 // Note that there is no need to distinguish between RGB and BGR.
291 // Each color channel will get the same value.
292 SkOpts::gray_to_RGB1((uint32_t*) dst, src + offset, width);
293}
294
msaretta4970dc2016-01-11 07:23:23 -0800295static void swizzle_gray_to_565(
emmaleer8f4ba762015-08-14 07:44:46 -0700296 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700297 int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) {
msaretta4970dc2016-01-11 07:23:23 -0800298
emmaleer8f4ba762015-08-14 07:44:46 -0700299 src += offset;
scroggoab60c5b2015-08-06 06:08:18 -0700300 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700301 for (int x = 0; x < dstWidth; x++) {
scroggoab60c5b2015-08-06 06:08:18 -0700302 dst[x] = SkPack888ToRGB16(src[0], src[0], src[0]);
msarett5406d6f2015-08-31 06:55:13 -0700303 src += deltaSrc;
scroggoab60c5b2015-08-06 06:08:18 -0700304 }
scroggoab60c5b2015-08-06 06:08:18 -0700305}
306
msarett93e613d2016-02-03 10:44:46 -0800307// kGrayAlpha
308
309static void swizzle_grayalpha_to_n32_unpremul(
310 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
311 const SkPMColor ctable[]) {
312
313 src += offset;
314 SkPMColor* dst32 = (SkPMColor*) dst;
315 for (int x = 0; x < width; x++) {
316 dst32[x] = SkPackARGB32NoCheck(src[1], src[0], src[0], src[0]);
317 src += deltaSrc;
318 }
319}
320
msarett1e060792016-02-03 11:17:43 -0800321static void fast_swizzle_grayalpha_to_n32_unpremul(
322 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
323 const SkPMColor ctable[]) {
324
325 // This function must not be called if we are sampling. If we are not
326 // sampling, deltaSrc should equal bpp.
327 SkASSERT(deltaSrc == bpp);
328
329 // Note that there is no need to distinguish between RGB and BGR.
330 // Each color channel will get the same value.
331 SkOpts::grayA_to_RGBA((uint32_t*) dst, src + offset, width);
332}
333
msarett93e613d2016-02-03 10:44:46 -0800334static void swizzle_grayalpha_to_n32_premul(
335 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
336 const SkPMColor ctable[]) {
337
338 src += offset;
339 SkPMColor* dst32 = (SkPMColor*) dst;
340 for (int x = 0; x < width; x++) {
341 uint8_t pmgray = SkMulDiv255Round(src[1], src[0]);
342 dst32[x] = SkPackARGB32NoCheck(src[1], pmgray, pmgray, pmgray);
343 src += deltaSrc;
344 }
345}
346
msarett1e060792016-02-03 11:17:43 -0800347static void fast_swizzle_grayalpha_to_n32_premul(
348 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
349 const SkPMColor ctable[]) {
350
351 // This function must not be called if we are sampling. If we are not
352 // sampling, deltaSrc should equal bpp.
353 SkASSERT(deltaSrc == bpp);
354
355 // Note that there is no need to distinguish between rgb and bgr.
356 // Each color channel will get the same value.
357 SkOpts::grayA_to_rgbA((uint32_t*) dst, src + offset, width);
358}
359
Mike Reedd6cb11e2017-11-30 15:33:04 -0500360static void swizzle_grayalpha_to_a8(void* dst, const uint8_t* src, int width, int bpp,
361 int deltaSrc, int offset, const SkPMColor[]) {
362 src += offset;
363 uint8_t* dst8 = (uint8_t*)dst;
364 for (int x = 0; x < width; ++x) {
365 dst8[x] = src[1]; // src[0] is gray, ignored
366 src += deltaSrc;
367 }
368}
369
msarett34e0ec42016-04-22 16:27:24 -0700370// kBGR
msarette16b04a2015-04-15 07:32:19 -0700371
msarett34e0ec42016-04-22 16:27:24 -0700372static void swizzle_bgr_to_565(
scroggocc2feb12015-08-14 08:32:46 -0700373 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700374 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
msaretta4970dc2016-01-11 07:23:23 -0800375
scroggocc2feb12015-08-14 08:32:46 -0700376 src += offset;
377 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
378 for (int x = 0; x < dstWidth; x++) {
379 dst[x] = SkPack888ToRGB16(src[2], src[1], src[0]);
380 src += deltaSrc;
381 }
scroggocc2feb12015-08-14 08:32:46 -0700382}
383
msarettbda86092016-01-19 10:40:12 -0800384// kRGB
msarett03108de2016-01-15 11:02:36 -0800385
msarett34e0ec42016-04-22 16:27:24 -0700386static void swizzle_rgb_to_rgba(
emmaleer8f4ba762015-08-14 07:44:46 -0700387 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700388 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
msarett74114382015-03-16 11:55:18 -0700389
emmaleer8f4ba762015-08-14 07:44:46 -0700390 src += offset;
scroggof24f2242015-03-03 08:59:20 -0800391 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700392 for (int x = 0; x < dstWidth; x++) {
msarett34e0ec42016-04-22 16:27:24 -0700393 dst[x] = SkPackARGB_as_RGBA(0xFF, src[0], src[1], src[2]);
emmaleer8f4ba762015-08-14 07:44:46 -0700394 src += deltaSrc;
scroggof24f2242015-03-03 08:59:20 -0800395 }
scroggof24f2242015-03-03 08:59:20 -0800396}
397
msarett34e0ec42016-04-22 16:27:24 -0700398static void swizzle_rgb_to_bgra(
399 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
400 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
401
402 src += offset;
403 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
404 for (int x = 0; x < dstWidth; x++) {
405 dst[x] = SkPackARGB_as_BGRA(0xFF, src[0], src[1], src[2]);
406 src += deltaSrc;
407 }
408}
409
410static void fast_swizzle_rgb_to_rgba(
msarettf1b8b6a2016-01-22 09:54:21 -0800411 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc,
412 int offset, const SkPMColor ctable[]) {
msarett03108de2016-01-15 11:02:36 -0800413
msarettf1b8b6a2016-01-22 09:54:21 -0800414 // This function must not be called if we are sampling. If we are not
415 // sampling, deltaSrc should equal bpp.
416 SkASSERT(deltaSrc == bpp);
417
msarettf1b8b6a2016-01-22 09:54:21 -0800418 SkOpts::RGB_to_RGB1((uint32_t*) dst, src + offset, width);
msarett34e0ec42016-04-22 16:27:24 -0700419}
420
421static void fast_swizzle_rgb_to_bgra(
422 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc,
423 int offset, const SkPMColor ctable[]) {
424
425 // This function must not be called if we are sampling. If we are not
426 // sampling, deltaSrc should equal bpp.
427 SkASSERT(deltaSrc == bpp);
428
msarettf1b8b6a2016-01-22 09:54:21 -0800429 SkOpts::RGB_to_BGR1((uint32_t*) dst, src + offset, width);
msarettf1b8b6a2016-01-22 09:54:21 -0800430}
msarett03108de2016-01-15 11:02:36 -0800431
msarettbda86092016-01-19 10:40:12 -0800432static void swizzle_rgb_to_565(
emmaleer8f4ba762015-08-14 07:44:46 -0700433 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700434 int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) {
msaretta4970dc2016-01-11 07:23:23 -0800435
emmaleer8f4ba762015-08-14 07:44:46 -0700436 src += offset;
scroggoab60c5b2015-08-06 06:08:18 -0700437 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700438 for (int x = 0; x < dstWidth; x++) {
scroggoab60c5b2015-08-06 06:08:18 -0700439 dst[x] = SkPack888ToRGB16(src[0], src[1], src[2]);
msarett5406d6f2015-08-31 06:55:13 -0700440 src += deltaSrc;
scroggoab60c5b2015-08-06 06:08:18 -0700441 }
scroggoab60c5b2015-08-06 06:08:18 -0700442}
443
scroggoab60c5b2015-08-06 06:08:18 -0700444// kRGBA
msarett03108de2016-01-15 11:02:36 -0800445
msarett34e0ec42016-04-22 16:27:24 -0700446static void swizzle_rgba_to_rgba_premul(
emmaleer8f4ba762015-08-14 07:44:46 -0700447 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700448 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
msarett74114382015-03-16 11:55:18 -0700449
emmaleer8f4ba762015-08-14 07:44:46 -0700450 src += offset;
scroggof24f2242015-03-03 08:59:20 -0800451 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
emmaleer8f4ba762015-08-14 07:44:46 -0700452 for (int x = 0; x < dstWidth; x++) {
msarett34e0ec42016-04-22 16:27:24 -0700453 dst[x] = premultiply_argb_as_rgba(src[3], src[0], src[1], src[2]);
emmaleer8f4ba762015-08-14 07:44:46 -0700454 src += deltaSrc;
scroggof24f2242015-03-03 08:59:20 -0800455 }
scroggof24f2242015-03-03 08:59:20 -0800456}
457
msarett34e0ec42016-04-22 16:27:24 -0700458static void swizzle_rgba_to_bgra_premul(
459 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
460 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
461
462 src += offset;
463 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
464 for (int x = 0; x < dstWidth; x++) {
465 dst[x] = premultiply_argb_as_bgra(src[3], src[0], src[1], src[2]);
466 src += deltaSrc;
467 }
468}
469
470static void fast_swizzle_rgba_to_rgba_premul(
msaretta51e7782016-01-12 06:51:11 -0800471 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc,
472 int offset, const SkPMColor ctable[]) {
473
474 // This function must not be called if we are sampling. If we are not
475 // sampling, deltaSrc should equal bpp.
476 SkASSERT(deltaSrc == bpp);
477
Mike Klein6e78ae52018-09-19 13:37:16 -0400478 SkOpts::RGBA_to_rgbA((uint32_t*) dst, (const uint32_t*)(src + offset), width);
msaretta51e7782016-01-12 06:51:11 -0800479}
480
msarett34e0ec42016-04-22 16:27:24 -0700481static void fast_swizzle_rgba_to_bgra_premul(
482 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc,
483 int offset, const SkPMColor ctable[]) {
484
485 // This function must not be called if we are sampling. If we are not
486 // sampling, deltaSrc should equal bpp.
487 SkASSERT(deltaSrc == bpp);
488
Mike Klein6e78ae52018-09-19 13:37:16 -0400489 SkOpts::RGBA_to_bgrA((uint32_t*) dst, (const uint32_t*)(src + offset), width);
msarett34e0ec42016-04-22 16:27:24 -0700490}
491
492static void swizzle_rgba_to_bgra_unpremul(
emmaleer8f4ba762015-08-14 07:44:46 -0700493 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
msarett5406d6f2015-08-31 06:55:13 -0700494 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
msarett74114382015-03-16 11:55:18 -0700495
emmaleer8f4ba762015-08-14 07:44:46 -0700496 src += offset;
scroggof24f2242015-03-03 08:59:20 -0800497 uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow);
emmaleer8f4ba762015-08-14 07:44:46 -0700498 for (int x = 0; x < dstWidth; x++) {
scroggof24f2242015-03-03 08:59:20 -0800499 unsigned alpha = src[3];
msarett34e0ec42016-04-22 16:27:24 -0700500 dst[x] = SkPackARGB_as_BGRA(alpha, src[0], src[1], src[2]);
emmaleer8f4ba762015-08-14 07:44:46 -0700501 src += deltaSrc;
scroggof24f2242015-03-03 08:59:20 -0800502 }
scroggof24f2242015-03-03 08:59:20 -0800503}
504
msarett34e0ec42016-04-22 16:27:24 -0700505static void fast_swizzle_rgba_to_bgra_unpremul(
msarettbda86092016-01-19 10:40:12 -0800506 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
507 const SkPMColor ctable[]) {
508
509 // This function must not be called if we are sampling. If we are not
510 // sampling, deltaSrc should equal bpp.
511 SkASSERT(deltaSrc == bpp);
512
Mike Klein6e78ae52018-09-19 13:37:16 -0400513 SkOpts::RGBA_to_BGRA((uint32_t*) dst, (const uint32_t*)(src + offset), width);
msarettbda86092016-01-19 10:40:12 -0800514}
515
Matt Sarett7a1cc672016-12-14 11:48:31 -0500516// 16-bits per component kRGB and kRGBA
517
518static void swizzle_rgb16_to_rgba(
519 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
520 const SkPMColor ctable[]) {
521 auto strip16to8 = [](const uint8_t* ptr) {
522 return 0xFF000000 | (ptr[4] << 16) | (ptr[2] << 8) | ptr[0];
523 };
524
525 src += offset;
526 uint32_t* dst32 = (uint32_t*) dst;
527 for (int x = 0; x < width; x++) {
528 dst32[x] = strip16to8(src);
529 src += deltaSrc;
530 }
531}
532
533static void swizzle_rgb16_to_bgra(
534 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
535 const SkPMColor ctable[]) {
536 auto strip16to8 = [](const uint8_t* ptr) {
537 return 0xFF000000 | (ptr[0] << 16) | (ptr[2] << 8) | ptr[4];
538 };
539
540 src += offset;
541 uint32_t* dst32 = (uint32_t*) dst;
542 for (int x = 0; x < width; x++) {
543 dst32[x] = strip16to8(src);
544 src += deltaSrc;
545 }
546}
547
548static void swizzle_rgb16_to_565(
549 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
550 const SkPMColor ctable[]) {
551 auto strip16to565 = [](const uint8_t* ptr) {
552 return SkPack888ToRGB16(ptr[0], ptr[2], ptr[4]);
553 };
554
555 src += offset;
556 uint16_t* dst16 = (uint16_t*) dst;
557 for (int x = 0; x < width; x++) {
558 dst16[x] = strip16to565(src);
559 src += deltaSrc;
560 }
561}
562
563static void swizzle_rgba16_to_rgba_unpremul(
564 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
565 const SkPMColor ctable[]) {
566 auto strip16to8 = [](const uint8_t* ptr) {
567 return (ptr[6] << 24) | (ptr[4] << 16) | (ptr[2] << 8) | ptr[0];
568 };
569
570 src += offset;
571 uint32_t* dst32 = (uint32_t*) dst;
572 for (int x = 0; x < width; x++) {
573 dst32[x] = strip16to8(src);
574 src += deltaSrc;
575 }
576}
577
578static void swizzle_rgba16_to_rgba_premul(
579 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
580 const SkPMColor ctable[]) {
581 auto stripAndPremul16to8 = [](const uint8_t* ptr) {
582 return premultiply_argb_as_rgba(ptr[6], ptr[0], ptr[2], ptr[4]);
583 };
584
585 src += offset;
586 uint32_t* dst32 = (uint32_t*) dst;
587 for (int x = 0; x < width; x++) {
588 dst32[x] = stripAndPremul16to8(src);
589 src += deltaSrc;
590 }
591}
592
593static void swizzle_rgba16_to_bgra_unpremul(
594 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
595 const SkPMColor ctable[]) {
596 auto strip16to8 = [](const uint8_t* ptr) {
597 return (ptr[6] << 24) | (ptr[0] << 16) | (ptr[2] << 8) | ptr[4];
598 };
599
600 src += offset;
601 uint32_t* dst32 = (uint32_t*) dst;
602 for (int x = 0; x < width; x++) {
603 dst32[x] = strip16to8(src);
604 src += deltaSrc;
605 }
606}
607
608static void swizzle_rgba16_to_bgra_premul(
609 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
610 const SkPMColor ctable[]) {
611 auto stripAndPremul16to8 = [](const uint8_t* ptr) {
612 return premultiply_argb_as_bgra(ptr[6], ptr[0], ptr[2], ptr[4]);
613 };
614
615 src += offset;
616 uint32_t* dst32 = (uint32_t*) dst;
617 for (int x = 0; x < width; x++) {
618 dst32[x] = stripAndPremul16to8(src);
619 src += deltaSrc;
620 }
621}
622
scroggoef27d892015-10-23 09:29:22 -0700623// kCMYK
624//
625// CMYK is stored as four bytes per pixel.
626//
627// We will implement a crude conversion from CMYK -> RGB using formulas
628// from easyrgb.com.
629//
630// CMYK -> CMY
631// C = C * (1 - K) + K
632// M = M * (1 - K) + K
633// Y = Y * (1 - K) + K
634//
635// libjpeg actually gives us inverted CMYK, so we must subtract the
636// original terms from 1.
637// CMYK -> CMY
638// C = (1 - C) * (1 - (1 - K)) + (1 - K)
639// M = (1 - M) * (1 - (1 - K)) + (1 - K)
640// Y = (1 - Y) * (1 - (1 - K)) + (1 - K)
641//
642// Simplifying the above expression.
643// CMYK -> CMY
644// C = 1 - CK
645// M = 1 - MK
646// Y = 1 - YK
647//
648// CMY -> RGB
649// R = (1 - C) * 255
650// G = (1 - M) * 255
651// B = (1 - Y) * 255
652//
653// Therefore the full conversion is below. This can be verified at
654// www.rapidtables.com (assuming inverted CMYK).
655// CMYK -> RGB
656// R = C * K * 255
657// G = M * K * 255
658// B = Y * K * 255
659//
660// As a final note, we have treated the CMYK values as if they were on
661// a scale from 0-1, when in fact they are 8-bit ints scaling from 0-255.
662// We must divide each CMYK component by 255 to obtain the true conversion
663// we should perform.
664// CMYK -> RGB
665// R = C * K / 255
666// G = M * K / 255
667// B = Y * K / 255
msarett34e0ec42016-04-22 16:27:24 -0700668static void swizzle_cmyk_to_rgba(
scroggoef27d892015-10-23 09:29:22 -0700669 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
670 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
671
672 src += offset;
673 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
674 for (int x = 0; x < dstWidth; x++) {
675 const uint8_t r = SkMulDiv255Round(src[0], src[3]);
676 const uint8_t g = SkMulDiv255Round(src[1], src[3]);
677 const uint8_t b = SkMulDiv255Round(src[2], src[3]);
678
msarett34e0ec42016-04-22 16:27:24 -0700679 dst[x] = SkPackARGB_as_RGBA(0xFF, r, g, b);
scroggoef27d892015-10-23 09:29:22 -0700680 src += deltaSrc;
681 }
scroggoef27d892015-10-23 09:29:22 -0700682}
683
msarett34e0ec42016-04-22 16:27:24 -0700684static void swizzle_cmyk_to_bgra(
685 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
686 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
687
688 src += offset;
689 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
690 for (int x = 0; x < dstWidth; x++) {
691 const uint8_t r = SkMulDiv255Round(src[0], src[3]);
692 const uint8_t g = SkMulDiv255Round(src[1], src[3]);
693 const uint8_t b = SkMulDiv255Round(src[2], src[3]);
694
695 dst[x] = SkPackARGB_as_BGRA(0xFF, r, g, b);
696 src += deltaSrc;
697 }
698}
699
700static void fast_swizzle_cmyk_to_rgba(
msarettc5c322d2016-02-08 13:26:25 -0800701 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
702 const SkPMColor ctable[]) {
703
704 // This function must not be called if we are sampling. If we are not
705 // sampling, deltaSrc should equal bpp.
706 SkASSERT(deltaSrc == bpp);
707
Mike Klein6e78ae52018-09-19 13:37:16 -0400708 SkOpts::inverted_CMYK_to_RGB1((uint32_t*) dst, (const uint32_t*)(src + offset), width);
msarett34e0ec42016-04-22 16:27:24 -0700709}
710
711static void fast_swizzle_cmyk_to_bgra(
712 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, int offset,
713 const SkPMColor ctable[]) {
714
715 // This function must not be called if we are sampling. If we are not
716 // sampling, deltaSrc should equal bpp.
717 SkASSERT(deltaSrc == bpp);
718
Mike Klein6e78ae52018-09-19 13:37:16 -0400719 SkOpts::inverted_CMYK_to_BGR1((uint32_t*) dst, (const uint32_t*)(src + offset), width);
msarettc5c322d2016-02-08 13:26:25 -0800720}
721
msaretta4970dc2016-01-11 07:23:23 -0800722static void swizzle_cmyk_to_565(
scroggoef27d892015-10-23 09:29:22 -0700723 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
724 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
725
726 src += offset;
727 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
728 for (int x = 0; x < dstWidth; x++) {
729 const uint8_t r = SkMulDiv255Round(src[0], src[3]);
730 const uint8_t g = SkMulDiv255Round(src[1], src[3]);
731 const uint8_t b = SkMulDiv255Round(src[2], src[3]);
732
733 dst[x] = SkPack888ToRGB16(r, g, b);
734 src += deltaSrc;
735 }
scroggoef27d892015-10-23 09:29:22 -0700736}
737
mtklein8604ca22016-01-11 13:13:55 -0800738template <SkSwizzler::RowProc proc>
msarett93e613d2016-02-03 10:44:46 -0800739void SkSwizzler::SkipLeadingGrayAlphaZerosThen(
740 void* dst, const uint8_t* src, int width,
741 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
742 SkASSERT(!ctable);
743
744 const uint16_t* src16 = (const uint16_t*) (src + offset);
745 uint32_t* dst32 = (uint32_t*) dst;
746
747 // This may miss opportunities to skip when the output is premultiplied,
748 // e.g. for a src pixel 0x00FF which is not zero but becomes zero after premultiplication.
749 while (width > 0 && *src16 == 0x0000) {
750 width--;
751 dst32++;
752 src16 += deltaSrc / 2;
753 }
754 proc(dst32, (const uint8_t*)src16, width, bpp, deltaSrc, 0, ctable);
755}
756
757template <SkSwizzler::RowProc proc>
mtklein8604ca22016-01-11 13:13:55 -0800758void SkSwizzler::SkipLeading8888ZerosThen(
759 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth,
760 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) {
761 SkASSERT(!ctable);
762
763 auto src32 = (const uint32_t*)(src+offset);
764 auto dst32 = (uint32_t*)dstRow;
765
766 // This may miss opportunities to skip when the output is premultiplied,
767 // e.g. for a src pixel 0x00FFFFFF which is not zero but becomes zero after premultiplication.
768 while (dstWidth > 0 && *src32 == 0x00000000) {
769 dstWidth--;
770 dst32++;
771 src32 += deltaSrc/4;
scroggof24f2242015-03-03 08:59:20 -0800772 }
mtklein8604ca22016-01-11 13:13:55 -0800773 proc(dst32, (const uint8_t*)src32, dstWidth, bpp, deltaSrc, 0, ctable);
scroggof24f2242015-03-03 08:59:20 -0800774}
scroggof24f2242015-03-03 08:59:20 -0800775
msaretta45a6682016-04-22 13:18:37 -0700776SkSwizzler* SkSwizzler::CreateSwizzler(const SkEncodedInfo& encodedInfo,
msarett74114382015-03-16 11:55:18 -0700777 const SkPMColor* ctable,
msarettfdb47572015-10-13 12:50:14 -0700778 const SkImageInfo& dstInfo,
msarett5af4e0b2015-11-17 11:18:03 -0800779 const SkCodec::Options& options,
msarett68758ae2016-04-25 11:41:15 -0700780 const SkIRect* frame,
Matt Sarett9bf39c22016-12-13 13:29:54 -0500781 bool skipFormatConversion) {
msaretta45a6682016-04-22 13:18:37 -0700782 if (SkEncodedInfo::kPalette_Color == encodedInfo.color() && nullptr == ctable) {
halcanary96fcdcc2015-08-27 07:41:13 -0700783 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800784 }
msaretta45a6682016-04-22 13:18:37 -0700785
msaretta51e7782016-01-12 06:51:11 -0800786 RowProc fastProc = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -0700787 RowProc proc = nullptr;
Matt Sarett379938e2017-01-12 18:34:29 -0500788 int srcBPP;
Mike Reed7fcfb622018-02-09 13:26:46 -0500789 const int dstBPP = dstInfo.bytesPerPixel();
Matt Sarett9bf39c22016-12-13 13:29:54 -0500790 if (skipFormatConversion) {
Matt Sarett34c69d62017-01-19 17:42:23 -0500791 switch (encodedInfo.color()) {
792 case SkEncodedInfo::kGray_Color:
793 case SkEncodedInfo::kYUV_Color:
794 // We have a jpeg that has already been converted to the dstColorType.
795 srcBPP = dstBPP;
796 switch (dstInfo.colorType()) {
797 case kGray_8_SkColorType:
798 proc = &sample1;
799 fastProc = &copy;
800 break;
801 case kRGB_565_SkColorType:
802 proc = &sample2;
803 fastProc = &copy;
804 break;
805 case kRGBA_8888_SkColorType:
806 case kBGRA_8888_SkColorType:
807 proc = &sample4;
808 fastProc = &copy;
809 break;
810 default:
811 return nullptr;
812 }
813 break;
814 case SkEncodedInfo::kInvertedCMYK_Color:
815 case SkEncodedInfo::kYCCK_Color:
816 // We have a jpeg that remains in its original format.
817 srcBPP = 4;
818 proc = &sample4;
msarett68758ae2016-04-25 11:41:15 -0700819 fastProc = &copy;
820 break;
Matt Sarett34c69d62017-01-19 17:42:23 -0500821 case SkEncodedInfo::kRGBA_Color:
822 // We have a png that should remain in its original format.
Matt Sarett379938e2017-01-12 18:34:29 -0500823 SkASSERT(16 == encodedInfo.bitsPerComponent() ||
824 8 == encodedInfo.bitsPerComponent());
825 if (8 == encodedInfo.bitsPerComponent()) {
Matt Sarett34c69d62017-01-19 17:42:23 -0500826 srcBPP = 4;
Matt Sarett379938e2017-01-12 18:34:29 -0500827 proc = &sample4;
828 } else {
829 srcBPP = 8;
830 proc = &sample8;
831 }
msarett68758ae2016-04-25 11:41:15 -0700832 fastProc = &copy;
833 break;
Matt Sarett34c69d62017-01-19 17:42:23 -0500834 case SkEncodedInfo::kRGB_Color:
835 // We have a png that remains in its original format.
836 SkASSERT(16 == encodedInfo.bitsPerComponent());
837 srcBPP = 6;
838 proc = &sample6;
839 fastProc = &copy;
840 break;
msarett68758ae2016-04-25 11:41:15 -0700841 default:
842 return nullptr;
843 }
844 } else {
845 SkCodec::ZeroInitialized zeroInit = options.fZeroInitialized;
846 const bool premultiply = (SkEncodedInfo::kOpaque_Alpha != encodedInfo.alpha()) &&
847 (kPremul_SkAlphaType == dstInfo.alphaType());
848
849 switch (encodedInfo.color()) {
850 case SkEncodedInfo::kGray_Color:
851 switch (encodedInfo.bitsPerComponent()) {
852 case 1:
853 switch (dstInfo.colorType()) {
854 case kRGBA_8888_SkColorType:
855 case kBGRA_8888_SkColorType:
856 proc = &swizzle_bit_to_n32;
857 break;
msarett68758ae2016-04-25 11:41:15 -0700858 case kRGB_565_SkColorType:
859 proc = &swizzle_bit_to_565;
860 break;
861 case kGray_8_SkColorType:
862 proc = &swizzle_bit_to_grayscale;
863 break;
Matt Saretta225e9b2016-11-07 10:26:17 -0500864 case kRGBA_F16_SkColorType:
865 proc = &swizzle_bit_to_f16;
866 break;
msarett68758ae2016-04-25 11:41:15 -0700867 default:
868 return nullptr;
msaretta45a6682016-04-22 13:18:37 -0700869 }
msarett68758ae2016-04-25 11:41:15 -0700870 break;
871 case 8:
872 switch (dstInfo.colorType()) {
873 case kRGBA_8888_SkColorType:
874 case kBGRA_8888_SkColorType:
875 proc = &swizzle_gray_to_n32;
876 fastProc = &fast_swizzle_gray_to_n32;
877 break;
878 case kGray_8_SkColorType:
879 proc = &sample1;
880 fastProc = &copy;
881 break;
882 case kRGB_565_SkColorType:
883 proc = &swizzle_gray_to_565;
884 break;
885 default:
886 return nullptr;
msarett93e613d2016-02-03 10:44:46 -0800887 }
msarett68758ae2016-04-25 11:41:15 -0700888 break;
889 default:
890 return nullptr;
891 }
892 break;
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400893 case SkEncodedInfo::kXAlpha_Color:
msarett68758ae2016-04-25 11:41:15 -0700894 case SkEncodedInfo::kGrayAlpha_Color:
895 switch (dstInfo.colorType()) {
896 case kRGBA_8888_SkColorType:
897 case kBGRA_8888_SkColorType:
898 if (premultiply) {
msaretta45a6682016-04-22 13:18:37 -0700899 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
msarett68758ae2016-04-25 11:41:15 -0700900 proc = &SkipLeadingGrayAlphaZerosThen
901 <swizzle_grayalpha_to_n32_premul>;
902 fastProc = &SkipLeadingGrayAlphaZerosThen
903 <fast_swizzle_grayalpha_to_n32_premul>;
msaretta45a6682016-04-22 13:18:37 -0700904 } else {
msarett68758ae2016-04-25 11:41:15 -0700905 proc = &swizzle_grayalpha_to_n32_premul;
906 fastProc = &fast_swizzle_grayalpha_to_n32_premul;
msaretta45a6682016-04-22 13:18:37 -0700907 }
scroggof24f2242015-03-03 08:59:20 -0800908 } else {
msarett68758ae2016-04-25 11:41:15 -0700909 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
910 proc = &SkipLeadingGrayAlphaZerosThen
911 <swizzle_grayalpha_to_n32_unpremul>;
912 fastProc = &SkipLeadingGrayAlphaZerosThen
913 <fast_swizzle_grayalpha_to_n32_unpremul>;
914 } else {
915 proc = &swizzle_grayalpha_to_n32_unpremul;
916 fastProc = &fast_swizzle_grayalpha_to_n32_unpremul;
917 }
scroggof24f2242015-03-03 08:59:20 -0800918 }
msarett68758ae2016-04-25 11:41:15 -0700919 break;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500920 case kAlpha_8_SkColorType:
921 proc = &swizzle_grayalpha_to_a8;
922 break;
msarett68758ae2016-04-25 11:41:15 -0700923 default:
924 return nullptr;
925 }
926 break;
927 case SkEncodedInfo::kPalette_Color:
928 // We assume that the color table is premultiplied and swizzled
929 // as desired.
930 switch (encodedInfo.bitsPerComponent()) {
931 case 1:
932 case 2:
933 case 4:
934 switch (dstInfo.colorType()) {
935 case kRGBA_8888_SkColorType:
936 case kBGRA_8888_SkColorType:
937 proc = &swizzle_small_index_to_n32;
938 break;
939 case kRGB_565_SkColorType:
940 proc = &swizzle_small_index_to_565;
941 break;
msarett68758ae2016-04-25 11:41:15 -0700942 default:
943 return nullptr;
944 }
945 break;
946 case 8:
947 switch (dstInfo.colorType()) {
948 case kRGBA_8888_SkColorType:
949 case kBGRA_8888_SkColorType:
950 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
951 proc = &swizzle_index_to_n32_skipZ;
952 } else {
953 proc = &swizzle_index_to_n32;
954 }
955 break;
956 case kRGB_565_SkColorType:
957 proc = &swizzle_index_to_565;
958 break;
msarett68758ae2016-04-25 11:41:15 -0700959 default:
960 return nullptr;
961 }
962 break;
963 default:
964 return nullptr;
965 }
966 break;
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400967 case SkEncodedInfo::k565_Color:
968 // Treat 565 exactly like RGB (since it's still encoded as 8 bits per component).
969 // We just mark as 565 when we have a hint that there are only 5/6/5 "significant"
970 // bits in each channel.
msarett68758ae2016-04-25 11:41:15 -0700971 case SkEncodedInfo::kRGB_Color:
972 switch (dstInfo.colorType()) {
973 case kRGBA_8888_SkColorType:
Matt Sarett7a1cc672016-12-14 11:48:31 -0500974 if (16 == encodedInfo.bitsPerComponent()) {
975 proc = &swizzle_rgb16_to_rgba;
976 break;
977 }
978
979 SkASSERT(8 == encodedInfo.bitsPerComponent());
msarett68758ae2016-04-25 11:41:15 -0700980 proc = &swizzle_rgb_to_rgba;
981 fastProc = &fast_swizzle_rgb_to_rgba;
982 break;
983 case kBGRA_8888_SkColorType:
Matt Sarett7a1cc672016-12-14 11:48:31 -0500984 if (16 == encodedInfo.bitsPerComponent()) {
985 proc = &swizzle_rgb16_to_bgra;
986 break;
987 }
988
989 SkASSERT(8 == encodedInfo.bitsPerComponent());
msarett68758ae2016-04-25 11:41:15 -0700990 proc = &swizzle_rgb_to_bgra;
991 fastProc = &fast_swizzle_rgb_to_bgra;
992 break;
993 case kRGB_565_SkColorType:
Matt Sarett7a1cc672016-12-14 11:48:31 -0500994 if (16 == encodedInfo.bitsPerComponent()) {
995 proc = &swizzle_rgb16_to_565;
996 break;
997 }
998
msarett68758ae2016-04-25 11:41:15 -0700999 proc = &swizzle_rgb_to_565;
1000 break;
1001 default:
1002 return nullptr;
1003 }
1004 break;
1005 case SkEncodedInfo::kRGBA_Color:
1006 switch (dstInfo.colorType()) {
1007 case kRGBA_8888_SkColorType:
Matt Sarett7a1cc672016-12-14 11:48:31 -05001008 if (16 == encodedInfo.bitsPerComponent()) {
1009 proc = premultiply ? &swizzle_rgba16_to_rgba_premul :
1010 &swizzle_rgba16_to_rgba_unpremul;
1011 break;
1012 }
1013
1014 SkASSERT(8 == encodedInfo.bitsPerComponent());
msarett68758ae2016-04-25 11:41:15 -07001015 if (premultiply) {
1016 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1017 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_rgba_premul>;
1018 fastProc = &SkipLeading8888ZerosThen
1019 <fast_swizzle_rgba_to_rgba_premul>;
1020 } else {
1021 proc = &swizzle_rgba_to_rgba_premul;
1022 fastProc = &fast_swizzle_rgba_to_rgba_premul;
1023 }
msaretta45a6682016-04-22 13:18:37 -07001024 } else {
msarett68758ae2016-04-25 11:41:15 -07001025 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1026 proc = &SkipLeading8888ZerosThen<sample4>;
1027 fastProc = &SkipLeading8888ZerosThen<copy>;
1028 } else {
1029 proc = &sample4;
1030 fastProc = &copy;
1031 }
msarett34e0ec42016-04-22 16:27:24 -07001032 }
msarett68758ae2016-04-25 11:41:15 -07001033 break;
1034 case kBGRA_8888_SkColorType:
Matt Sarett7a1cc672016-12-14 11:48:31 -05001035 if (16 == encodedInfo.bitsPerComponent()) {
1036 proc = premultiply ? &swizzle_rgba16_to_bgra_premul :
1037 &swizzle_rgba16_to_bgra_unpremul;
1038 break;
1039 }
1040
1041 SkASSERT(8 == encodedInfo.bitsPerComponent());
msarett68758ae2016-04-25 11:41:15 -07001042 if (premultiply) {
1043 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1044 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_premul>;
1045 fastProc = &SkipLeading8888ZerosThen
1046 <fast_swizzle_rgba_to_bgra_premul>;
1047 } else {
1048 proc = &swizzle_rgba_to_bgra_premul;
1049 fastProc = &fast_swizzle_rgba_to_bgra_premul;
1050 }
msarett34e0ec42016-04-22 16:27:24 -07001051 } else {
msarett68758ae2016-04-25 11:41:15 -07001052 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1053 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_unpremul>;
1054 fastProc = &SkipLeading8888ZerosThen
1055 <fast_swizzle_rgba_to_bgra_unpremul>;
1056 } else {
1057 proc = &swizzle_rgba_to_bgra_unpremul;
1058 fastProc = &fast_swizzle_rgba_to_bgra_unpremul;
1059 }
msarett34e0ec42016-04-22 16:27:24 -07001060 }
msarett68758ae2016-04-25 11:41:15 -07001061 break;
1062 default:
1063 return nullptr;
1064 }
1065 break;
1066 case SkEncodedInfo::kBGR_Color:
1067 switch (dstInfo.colorType()) {
1068 case kBGRA_8888_SkColorType:
1069 proc = &swizzle_rgb_to_rgba;
1070 fastProc = &fast_swizzle_rgb_to_rgba;
1071 break;
1072 case kRGBA_8888_SkColorType:
1073 proc = &swizzle_rgb_to_bgra;
1074 fastProc = &fast_swizzle_rgb_to_bgra;
1075 break;
1076 case kRGB_565_SkColorType:
1077 proc = &swizzle_bgr_to_565;
1078 break;
1079 default:
1080 return nullptr;
1081 }
1082 break;
1083 case SkEncodedInfo::kBGRX_Color:
1084 switch (dstInfo.colorType()) {
1085 case kBGRA_8888_SkColorType:
1086 proc = &swizzle_rgb_to_rgba;
1087 break;
1088 case kRGBA_8888_SkColorType:
1089 proc = &swizzle_rgb_to_bgra;
1090 break;
1091 case kRGB_565_SkColorType:
1092 proc = &swizzle_bgr_to_565;
1093 break;
1094 default:
1095 return nullptr;
1096 }
1097 break;
1098 case SkEncodedInfo::kBGRA_Color:
1099 switch (dstInfo.colorType()) {
1100 case kBGRA_8888_SkColorType:
1101 if (premultiply) {
1102 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1103 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_rgba_premul>;
1104 fastProc = &SkipLeading8888ZerosThen
1105 <fast_swizzle_rgba_to_rgba_premul>;
1106 } else {
1107 proc = &swizzle_rgba_to_rgba_premul;
1108 fastProc = &fast_swizzle_rgba_to_rgba_premul;
1109 }
msarett34e0ec42016-04-22 16:27:24 -07001110 } else {
msarett68758ae2016-04-25 11:41:15 -07001111 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1112 proc = &SkipLeading8888ZerosThen<sample4>;
1113 fastProc = &SkipLeading8888ZerosThen<copy>;
1114 } else {
1115 proc = &sample4;
1116 fastProc = &copy;
1117 }
msaretta45a6682016-04-22 13:18:37 -07001118 }
msarett68758ae2016-04-25 11:41:15 -07001119 break;
1120 case kRGBA_8888_SkColorType:
1121 if (premultiply) {
1122 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1123 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_premul>;
1124 fastProc = &SkipLeading8888ZerosThen
1125 <fast_swizzle_rgba_to_bgra_premul>;
1126 } else {
1127 proc = &swizzle_rgba_to_bgra_premul;
1128 fastProc = &fast_swizzle_rgba_to_bgra_premul;
1129 }
msaretta45a6682016-04-22 13:18:37 -07001130 } else {
msarett68758ae2016-04-25 11:41:15 -07001131 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1132 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_unpremul>;
1133 fastProc = &SkipLeading8888ZerosThen
1134 <fast_swizzle_rgba_to_bgra_unpremul>;
1135 } else {
1136 proc = &swizzle_rgba_to_bgra_unpremul;
1137 fastProc = &fast_swizzle_rgba_to_bgra_unpremul;
1138 }
msaretta45a6682016-04-22 13:18:37 -07001139 }
msarett68758ae2016-04-25 11:41:15 -07001140 break;
1141 default:
1142 return nullptr;
1143 }
1144 break;
1145 case SkEncodedInfo::kInvertedCMYK_Color:
1146 switch (dstInfo.colorType()) {
1147 case kRGBA_8888_SkColorType:
1148 proc = &swizzle_cmyk_to_rgba;
1149 fastProc = &fast_swizzle_cmyk_to_rgba;
1150 break;
1151 case kBGRA_8888_SkColorType:
1152 proc = &swizzle_cmyk_to_bgra;
1153 fastProc = &fast_swizzle_cmyk_to_bgra;
1154 break;
1155 case kRGB_565_SkColorType:
1156 proc = &swizzle_cmyk_to_565;
1157 break;
1158 default:
1159 return nullptr;
1160 }
1161 break;
1162 default:
1163 return nullptr;
1164 }
msarett74114382015-03-16 11:55:18 -07001165
msaretta45a6682016-04-22 13:18:37 -07001166 // Store bpp in bytes if it is an even multiple, otherwise use bits
1167 uint8_t bitsPerPixel = encodedInfo.bitsPerPixel();
1168 srcBPP = SkIsAlign8(bitsPerPixel) ? bitsPerPixel / 8 : bitsPerPixel;
1169 }
mtklein3d00db32016-01-11 06:16:26 -08001170
msarettfdb47572015-10-13 12:50:14 -07001171 int srcOffset = 0;
1172 int srcWidth = dstInfo.width();
msarett5af4e0b2015-11-17 11:18:03 -08001173 int dstOffset = 0;
1174 int dstWidth = srcWidth;
msarettfdb47572015-10-13 12:50:14 -07001175 if (options.fSubset) {
msarett5af4e0b2015-11-17 11:18:03 -08001176 // We do not currently support subset decodes for image types that may have
1177 // frames (gif).
1178 SkASSERT(!frame);
msarettfdb47572015-10-13 12:50:14 -07001179 srcOffset = options.fSubset->left();
1180 srcWidth = options.fSubset->width();
msarett5af4e0b2015-11-17 11:18:03 -08001181 dstWidth = srcWidth;
1182 } else if (frame) {
1183 dstOffset = frame->left();
1184 srcWidth = frame->width();
msarettfdb47572015-10-13 12:50:14 -07001185 }
emmaleer8f4ba762015-08-14 07:44:46 -07001186
msaretta51e7782016-01-12 06:51:11 -08001187 return new SkSwizzler(fastProc, proc, ctable, srcOffset, srcWidth, dstOffset, dstWidth,
1188 srcBPP, dstBPP);
scroggof24f2242015-03-03 08:59:20 -08001189}
1190
msaretta51e7782016-01-12 06:51:11 -08001191SkSwizzler::SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcOffset,
1192 int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP)
1193 : fFastProc(fastProc)
msarett19032f72016-01-21 09:59:38 -08001194 , fSlowProc(proc)
1195 , fActualProc(fFastProc ? fFastProc : fSlowProc)
scroggof24f2242015-03-03 08:59:20 -08001196 , fColorTable(ctable)
msarettfdb47572015-10-13 12:50:14 -07001197 , fSrcOffset(srcOffset)
msarett5af4e0b2015-11-17 11:18:03 -08001198 , fDstOffset(dstOffset)
1199 , fSrcOffsetUnits(srcOffset * srcBPP)
1200 , fDstOffsetBytes(dstOffset * dstBPP)
1201 , fSrcWidth(srcWidth)
1202 , fDstWidth(dstWidth)
1203 , fSwizzleWidth(srcWidth)
1204 , fAllocatedWidth(dstWidth)
scroggoe7fc14b2015-10-02 13:14:46 -07001205 , fSampleX(1)
msarett5af4e0b2015-11-17 11:18:03 -08001206 , fSrcBPP(srcBPP)
1207 , fDstBPP(dstBPP)
scroggoe7fc14b2015-10-02 13:14:46 -07001208{}
1209
1210int SkSwizzler::onSetSampleX(int sampleX) {
msarett9972c422016-02-10 08:39:37 -08001211 SkASSERT(sampleX > 0);
1212
scroggoe7fc14b2015-10-02 13:14:46 -07001213 fSampleX = sampleX;
msarett5af4e0b2015-11-17 11:18:03 -08001214 fSrcOffsetUnits = (get_start_coord(sampleX) + fSrcOffset) * fSrcBPP;
1215 fDstOffsetBytes = (fDstOffset / sampleX) * fDstBPP;
1216 fSwizzleWidth = get_scaled_dimension(fSrcWidth, sampleX);
1217 fAllocatedWidth = get_scaled_dimension(fDstWidth, sampleX);
scroggoe7fc14b2015-10-02 13:14:46 -07001218
msarett9972c422016-02-10 08:39:37 -08001219 // The optimized swizzler functions do not support sampling. Sampled swizzles
1220 // are already fast because they skip pixels. We haven't seen a situation
1221 // where speeding up sampling has a significant impact on total decode time.
msarett19032f72016-01-21 09:59:38 -08001222 if (1 == fSampleX && fFastProc) {
1223 fActualProc = fFastProc;
1224 } else {
1225 fActualProc = fSlowProc;
1226 }
msaretta51e7782016-01-12 06:51:11 -08001227
msarett5af4e0b2015-11-17 11:18:03 -08001228 return fAllocatedWidth;
emmaleer8f4ba762015-08-14 07:44:46 -07001229}
scroggof24f2242015-03-03 08:59:20 -08001230
msaretta4970dc2016-01-11 07:23:23 -08001231void SkSwizzler::swizzle(void* dst, const uint8_t* SK_RESTRICT src) {
halcanary96fcdcc2015-08-27 07:41:13 -07001232 SkASSERT(nullptr != dst && nullptr != src);
msarett19032f72016-01-21 09:59:38 -08001233 fActualProc(SkTAddOffset<void>(dst, fDstOffsetBytes), src, fSwizzleWidth, fSrcBPP,
msarett5af4e0b2015-11-17 11:18:03 -08001234 fSampleX * fSrcBPP, fSrcOffsetUnits, fColorTable);
scroggof24f2242015-03-03 08:59:20 -08001235}