blob: 1d52cfd5aa2c8a52f72a274dfca902e7a5842e19 [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
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400776std::unique_ptr<SkSwizzler> SkSwizzler::MakeSimple(int srcBPP, const SkImageInfo& dstInfo,
777 const SkCodec::Options& options) {
778 RowProc proc = nullptr;
779 switch (srcBPP) {
780 case 1: // kGray_8_SkColorType
781 proc = &sample1;
782 break;
783 case 2: // kRGB_565_SkColorType
784 proc = &sample2;
785 break;
786 case 4: // kRGBA_8888_SkColorType
787 // kBGRA_8888_SkColorType
788 proc = &sample4;
789 break;
790 case 6: // 16 bit PNG no alpha
791 proc = &sample6;
792 break;
793 case 8: // 16 bit PNG with alpha
794 proc = &sample8;
795 break;
796 default:
797 return nullptr;
798 }
799
800 return Make(dstInfo, &copy, proc, nullptr /*ctable*/, srcBPP,
801 dstInfo.bytesPerPixel(), options, nullptr /*frame*/);
802}
803
804std::unique_ptr<SkSwizzler> SkSwizzler::Make(const SkEncodedInfo& encodedInfo,
805 const SkPMColor* ctable,
806 const SkImageInfo& dstInfo,
807 const SkCodec::Options& options,
808 const SkIRect* frame) {
msaretta45a6682016-04-22 13:18:37 -0700809 if (SkEncodedInfo::kPalette_Color == encodedInfo.color() && nullptr == ctable) {
halcanary96fcdcc2015-08-27 07:41:13 -0700810 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800811 }
msaretta45a6682016-04-22 13:18:37 -0700812
msaretta51e7782016-01-12 06:51:11 -0800813 RowProc fastProc = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -0700814 RowProc proc = nullptr;
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400815 SkCodec::ZeroInitialized zeroInit = options.fZeroInitialized;
816 const bool premultiply = (SkEncodedInfo::kOpaque_Alpha != encodedInfo.alpha()) &&
817 (kPremul_SkAlphaType == dstInfo.alphaType());
msarett68758ae2016-04-25 11:41:15 -0700818
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400819 switch (encodedInfo.color()) {
820 case SkEncodedInfo::kGray_Color:
821 switch (encodedInfo.bitsPerComponent()) {
822 case 1:
823 switch (dstInfo.colorType()) {
824 case kRGBA_8888_SkColorType:
825 case kBGRA_8888_SkColorType:
826 proc = &swizzle_bit_to_n32;
Matt Sarett7a1cc672016-12-14 11:48:31 -0500827 break;
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400828 case kRGB_565_SkColorType:
829 proc = &swizzle_bit_to_565;
Matt Sarett7a1cc672016-12-14 11:48:31 -0500830 break;
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400831 case kGray_8_SkColorType:
832 proc = &swizzle_bit_to_grayscale;
Matt Sarett7a1cc672016-12-14 11:48:31 -0500833 break;
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400834 case kRGBA_F16_SkColorType:
835 proc = &swizzle_bit_to_f16;
Matt Sarett7a1cc672016-12-14 11:48:31 -0500836 break;
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400837 default:
838 return nullptr;
839 }
840 break;
841 case 8:
842 switch (dstInfo.colorType()) {
843 case kRGBA_8888_SkColorType:
844 case kBGRA_8888_SkColorType:
845 proc = &swizzle_gray_to_n32;
846 fastProc = &fast_swizzle_gray_to_n32;
Matt Sarett7a1cc672016-12-14 11:48:31 -0500847 break;
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400848 case kGray_8_SkColorType:
849 proc = &sample1;
850 fastProc = &copy;
851 break;
852 case kRGB_565_SkColorType:
853 proc = &swizzle_gray_to_565;
854 break;
855 default:
856 return nullptr;
857 }
858 break;
859 default:
860 return nullptr;
861 }
862 break;
863 case SkEncodedInfo::kXAlpha_Color:
864 case SkEncodedInfo::kGrayAlpha_Color:
865 switch (dstInfo.colorType()) {
866 case kRGBA_8888_SkColorType:
867 case kBGRA_8888_SkColorType:
868 if (premultiply) {
869 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
870 proc = &SkipLeadingGrayAlphaZerosThen
871 <swizzle_grayalpha_to_n32_premul>;
872 fastProc = &SkipLeadingGrayAlphaZerosThen
873 <fast_swizzle_grayalpha_to_n32_premul>;
874 } else {
875 proc = &swizzle_grayalpha_to_n32_premul;
876 fastProc = &fast_swizzle_grayalpha_to_n32_premul;
Matt Sarett7a1cc672016-12-14 11:48:31 -0500877 }
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400878 } else {
879 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
880 proc = &SkipLeadingGrayAlphaZerosThen
881 <swizzle_grayalpha_to_n32_unpremul>;
882 fastProc = &SkipLeadingGrayAlphaZerosThen
883 <fast_swizzle_grayalpha_to_n32_unpremul>;
884 } else {
885 proc = &swizzle_grayalpha_to_n32_unpremul;
886 fastProc = &fast_swizzle_grayalpha_to_n32_unpremul;
887 }
888 }
889 break;
890 case kAlpha_8_SkColorType:
891 proc = &swizzle_grayalpha_to_a8;
892 break;
893 default:
894 return nullptr;
895 }
896 break;
897 case SkEncodedInfo::kPalette_Color:
898 // We assume that the color table is premultiplied and swizzled
899 // as desired.
900 switch (encodedInfo.bitsPerComponent()) {
901 case 1:
902 case 2:
903 case 4:
904 switch (dstInfo.colorType()) {
905 case kRGBA_8888_SkColorType:
906 case kBGRA_8888_SkColorType:
907 proc = &swizzle_small_index_to_n32;
908 break;
909 case kRGB_565_SkColorType:
910 proc = &swizzle_small_index_to_565;
911 break;
912 default:
913 return nullptr;
914 }
915 break;
916 case 8:
917 switch (dstInfo.colorType()) {
918 case kRGBA_8888_SkColorType:
919 case kBGRA_8888_SkColorType:
920 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
921 proc = &swizzle_index_to_n32_skipZ;
922 } else {
923 proc = &swizzle_index_to_n32;
924 }
925 break;
926 case kRGB_565_SkColorType:
927 proc = &swizzle_index_to_565;
928 break;
929 default:
930 return nullptr;
931 }
932 break;
933 default:
934 return nullptr;
935 }
936 break;
937 case SkEncodedInfo::k565_Color:
938 // Treat 565 exactly like RGB (since it's still encoded as 8 bits per component).
939 // We just mark as 565 when we have a hint that there are only 5/6/5 "significant"
940 // bits in each channel.
941 case SkEncodedInfo::kRGB_Color:
942 switch (dstInfo.colorType()) {
943 case kRGBA_8888_SkColorType:
944 if (16 == encodedInfo.bitsPerComponent()) {
945 proc = &swizzle_rgb16_to_rgba;
946 break;
947 }
Matt Sarett7a1cc672016-12-14 11:48:31 -0500948
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400949 SkASSERT(8 == encodedInfo.bitsPerComponent());
950 proc = &swizzle_rgb_to_rgba;
951 fastProc = &fast_swizzle_rgb_to_rgba;
952 break;
953 case kBGRA_8888_SkColorType:
954 if (16 == encodedInfo.bitsPerComponent()) {
955 proc = &swizzle_rgb16_to_bgra;
msarett68758ae2016-04-25 11:41:15 -0700956 break;
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400957 }
msarett74114382015-03-16 11:55:18 -0700958
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400959 SkASSERT(8 == encodedInfo.bitsPerComponent());
960 proc = &swizzle_rgb_to_bgra;
961 fastProc = &fast_swizzle_rgb_to_bgra;
962 break;
963 case kRGB_565_SkColorType:
964 if (16 == encodedInfo.bitsPerComponent()) {
965 proc = &swizzle_rgb16_to_565;
966 break;
967 }
968
969 proc = &swizzle_rgb_to_565;
970 break;
971 default:
972 return nullptr;
973 }
974 break;
975 case SkEncodedInfo::kRGBA_Color:
976 switch (dstInfo.colorType()) {
977 case kRGBA_8888_SkColorType:
978 if (16 == encodedInfo.bitsPerComponent()) {
979 proc = premultiply ? &swizzle_rgba16_to_rgba_premul :
980 &swizzle_rgba16_to_rgba_unpremul;
981 break;
982 }
983
984 SkASSERT(8 == encodedInfo.bitsPerComponent());
985 if (premultiply) {
986 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
987 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_rgba_premul>;
988 fastProc = &SkipLeading8888ZerosThen
989 <fast_swizzle_rgba_to_rgba_premul>;
990 } else {
991 proc = &swizzle_rgba_to_rgba_premul;
992 fastProc = &fast_swizzle_rgba_to_rgba_premul;
993 }
994 } else {
995 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
996 proc = &SkipLeading8888ZerosThen<sample4>;
997 fastProc = &SkipLeading8888ZerosThen<copy>;
998 } else {
999 proc = &sample4;
1000 fastProc = &copy;
1001 }
1002 }
1003 break;
1004 case kBGRA_8888_SkColorType:
1005 if (16 == encodedInfo.bitsPerComponent()) {
1006 proc = premultiply ? &swizzle_rgba16_to_bgra_premul :
1007 &swizzle_rgba16_to_bgra_unpremul;
1008 break;
1009 }
1010
1011 SkASSERT(8 == encodedInfo.bitsPerComponent());
1012 if (premultiply) {
1013 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1014 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_premul>;
1015 fastProc = &SkipLeading8888ZerosThen
1016 <fast_swizzle_rgba_to_bgra_premul>;
1017 } else {
1018 proc = &swizzle_rgba_to_bgra_premul;
1019 fastProc = &fast_swizzle_rgba_to_bgra_premul;
1020 }
1021 } else {
1022 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1023 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_unpremul>;
1024 fastProc = &SkipLeading8888ZerosThen
1025 <fast_swizzle_rgba_to_bgra_unpremul>;
1026 } else {
1027 proc = &swizzle_rgba_to_bgra_unpremul;
1028 fastProc = &fast_swizzle_rgba_to_bgra_unpremul;
1029 }
1030 }
1031 break;
1032 default:
1033 return nullptr;
1034 }
1035 break;
1036 case SkEncodedInfo::kBGR_Color:
1037 switch (dstInfo.colorType()) {
1038 case kBGRA_8888_SkColorType:
1039 proc = &swizzle_rgb_to_rgba;
1040 fastProc = &fast_swizzle_rgb_to_rgba;
1041 break;
1042 case kRGBA_8888_SkColorType:
1043 proc = &swizzle_rgb_to_bgra;
1044 fastProc = &fast_swizzle_rgb_to_bgra;
1045 break;
1046 case kRGB_565_SkColorType:
1047 proc = &swizzle_bgr_to_565;
1048 break;
1049 default:
1050 return nullptr;
1051 }
1052 break;
1053 case SkEncodedInfo::kBGRX_Color:
1054 switch (dstInfo.colorType()) {
1055 case kBGRA_8888_SkColorType:
1056 proc = &swizzle_rgb_to_rgba;
1057 break;
1058 case kRGBA_8888_SkColorType:
1059 proc = &swizzle_rgb_to_bgra;
1060 break;
1061 case kRGB_565_SkColorType:
1062 proc = &swizzle_bgr_to_565;
1063 break;
1064 default:
1065 return nullptr;
1066 }
1067 break;
1068 case SkEncodedInfo::kBGRA_Color:
1069 switch (dstInfo.colorType()) {
1070 case kBGRA_8888_SkColorType:
1071 if (premultiply) {
1072 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1073 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_rgba_premul>;
1074 fastProc = &SkipLeading8888ZerosThen
1075 <fast_swizzle_rgba_to_rgba_premul>;
1076 } else {
1077 proc = &swizzle_rgba_to_rgba_premul;
1078 fastProc = &fast_swizzle_rgba_to_rgba_premul;
1079 }
1080 } else {
1081 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1082 proc = &SkipLeading8888ZerosThen<sample4>;
1083 fastProc = &SkipLeading8888ZerosThen<copy>;
1084 } else {
1085 proc = &sample4;
1086 fastProc = &copy;
1087 }
1088 }
1089 break;
1090 case kRGBA_8888_SkColorType:
1091 if (premultiply) {
1092 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1093 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_premul>;
1094 fastProc = &SkipLeading8888ZerosThen
1095 <fast_swizzle_rgba_to_bgra_premul>;
1096 } else {
1097 proc = &swizzle_rgba_to_bgra_premul;
1098 fastProc = &fast_swizzle_rgba_to_bgra_premul;
1099 }
1100 } else {
1101 if (SkCodec::kYes_ZeroInitialized == zeroInit) {
1102 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_bgra_unpremul>;
1103 fastProc = &SkipLeading8888ZerosThen
1104 <fast_swizzle_rgba_to_bgra_unpremul>;
1105 } else {
1106 proc = &swizzle_rgba_to_bgra_unpremul;
1107 fastProc = &fast_swizzle_rgba_to_bgra_unpremul;
1108 }
1109 }
1110 break;
1111 default:
1112 return nullptr;
1113 }
1114 break;
1115 case SkEncodedInfo::kInvertedCMYK_Color:
1116 switch (dstInfo.colorType()) {
1117 case kRGBA_8888_SkColorType:
1118 proc = &swizzle_cmyk_to_rgba;
1119 fastProc = &fast_swizzle_cmyk_to_rgba;
1120 break;
1121 case kBGRA_8888_SkColorType:
1122 proc = &swizzle_cmyk_to_bgra;
1123 fastProc = &fast_swizzle_cmyk_to_bgra;
1124 break;
1125 case kRGB_565_SkColorType:
1126 proc = &swizzle_cmyk_to_565;
1127 break;
1128 default:
1129 return nullptr;
1130 }
1131 break;
1132 default:
1133 return nullptr;
msaretta45a6682016-04-22 13:18:37 -07001134 }
mtklein3d00db32016-01-11 06:16:26 -08001135
Leon Scroggins III65f4aea2018-10-24 12:17:22 -04001136 // Store bpp in bytes if it is an even multiple, otherwise use bits
1137 uint8_t bitsPerPixel = encodedInfo.bitsPerPixel();
1138 int srcBPP = SkIsAlign8(bitsPerPixel) ? bitsPerPixel / 8 : bitsPerPixel;
1139 int dstBPP = dstInfo.bytesPerPixel();
1140 return Make(dstInfo, fastProc, proc, ctable, srcBPP, dstBPP, options, frame);
1141}
1142
1143std::unique_ptr<SkSwizzler> SkSwizzler::Make(const SkImageInfo& dstInfo,
1144 RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcBPP,
1145 int dstBPP, const SkCodec::Options& options, const SkIRect* frame) {
msarettfdb47572015-10-13 12:50:14 -07001146 int srcOffset = 0;
1147 int srcWidth = dstInfo.width();
msarett5af4e0b2015-11-17 11:18:03 -08001148 int dstOffset = 0;
1149 int dstWidth = srcWidth;
msarettfdb47572015-10-13 12:50:14 -07001150 if (options.fSubset) {
msarett5af4e0b2015-11-17 11:18:03 -08001151 // We do not currently support subset decodes for image types that may have
1152 // frames (gif).
1153 SkASSERT(!frame);
msarettfdb47572015-10-13 12:50:14 -07001154 srcOffset = options.fSubset->left();
1155 srcWidth = options.fSubset->width();
msarett5af4e0b2015-11-17 11:18:03 -08001156 dstWidth = srcWidth;
1157 } else if (frame) {
1158 dstOffset = frame->left();
1159 srcWidth = frame->width();
msarettfdb47572015-10-13 12:50:14 -07001160 }
emmaleer8f4ba762015-08-14 07:44:46 -07001161
Leon Scroggins III65f4aea2018-10-24 12:17:22 -04001162 return std::unique_ptr<SkSwizzler>(new SkSwizzler(fastProc, proc, ctable, srcOffset, srcWidth,
1163 dstOffset, dstWidth, srcBPP, dstBPP));
scroggof24f2242015-03-03 08:59:20 -08001164}
1165
msaretta51e7782016-01-12 06:51:11 -08001166SkSwizzler::SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcOffset,
1167 int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP)
1168 : fFastProc(fastProc)
msarett19032f72016-01-21 09:59:38 -08001169 , fSlowProc(proc)
1170 , fActualProc(fFastProc ? fFastProc : fSlowProc)
scroggof24f2242015-03-03 08:59:20 -08001171 , fColorTable(ctable)
msarettfdb47572015-10-13 12:50:14 -07001172 , fSrcOffset(srcOffset)
msarett5af4e0b2015-11-17 11:18:03 -08001173 , fDstOffset(dstOffset)
1174 , fSrcOffsetUnits(srcOffset * srcBPP)
1175 , fDstOffsetBytes(dstOffset * dstBPP)
1176 , fSrcWidth(srcWidth)
1177 , fDstWidth(dstWidth)
1178 , fSwizzleWidth(srcWidth)
1179 , fAllocatedWidth(dstWidth)
scroggoe7fc14b2015-10-02 13:14:46 -07001180 , fSampleX(1)
msarett5af4e0b2015-11-17 11:18:03 -08001181 , fSrcBPP(srcBPP)
1182 , fDstBPP(dstBPP)
scroggoe7fc14b2015-10-02 13:14:46 -07001183{}
1184
1185int SkSwizzler::onSetSampleX(int sampleX) {
msarett9972c422016-02-10 08:39:37 -08001186 SkASSERT(sampleX > 0);
1187
scroggoe7fc14b2015-10-02 13:14:46 -07001188 fSampleX = sampleX;
msarett5af4e0b2015-11-17 11:18:03 -08001189 fDstOffsetBytes = (fDstOffset / sampleX) * fDstBPP;
1190 fSwizzleWidth = get_scaled_dimension(fSrcWidth, sampleX);
1191 fAllocatedWidth = get_scaled_dimension(fDstWidth, sampleX);
scroggoe7fc14b2015-10-02 13:14:46 -07001192
Leon Scroggins III68825772018-10-26 14:15:02 -04001193 int frameSampleX = sampleX;
1194 if (fSrcWidth < fDstWidth) {
1195 // Although SkSampledCodec adjusted sampleX so that it will never be
1196 // larger than the width of the image (or subset, if applicable), it
1197 // doesn't account for the width of a subset frame (i.e. gif). As a
1198 // result, get_start_coord(sampleX) could result in fSrcOffsetUnits
1199 // being wider than fSrcWidth. Compute a sampling rate based on the
1200 // frame width to ensure that fSrcOffsetUnits is sensible.
1201 frameSampleX = fSrcWidth / fSwizzleWidth;
1202 }
1203 fSrcOffsetUnits = (get_start_coord(frameSampleX) + fSrcOffset) * fSrcBPP;
1204
Leon Scroggins III07afa232018-10-22 13:16:37 -04001205 if (fDstOffsetBytes > 0) {
1206 const size_t dstSwizzleBytes = fSwizzleWidth * fDstBPP;
1207 const size_t dstAllocatedBytes = fAllocatedWidth * fDstBPP;
1208 if (fDstOffsetBytes + dstSwizzleBytes > dstAllocatedBytes) {
1209 SkASSERT(dstSwizzleBytes < dstAllocatedBytes);
1210 fDstOffsetBytes = dstAllocatedBytes - dstSwizzleBytes;
1211 }
1212 }
1213
msarett9972c422016-02-10 08:39:37 -08001214 // The optimized swizzler functions do not support sampling. Sampled swizzles
1215 // are already fast because they skip pixels. We haven't seen a situation
1216 // where speeding up sampling has a significant impact on total decode time.
msarett19032f72016-01-21 09:59:38 -08001217 if (1 == fSampleX && fFastProc) {
1218 fActualProc = fFastProc;
1219 } else {
1220 fActualProc = fSlowProc;
1221 }
msaretta51e7782016-01-12 06:51:11 -08001222
msarett5af4e0b2015-11-17 11:18:03 -08001223 return fAllocatedWidth;
emmaleer8f4ba762015-08-14 07:44:46 -07001224}
scroggof24f2242015-03-03 08:59:20 -08001225
msaretta4970dc2016-01-11 07:23:23 -08001226void SkSwizzler::swizzle(void* dst, const uint8_t* SK_RESTRICT src) {
halcanary96fcdcc2015-08-27 07:41:13 -07001227 SkASSERT(nullptr != dst && nullptr != src);
msarett19032f72016-01-21 09:59:38 -08001228 fActualProc(SkTAddOffset<void>(dst, fDstOffsetBytes), src, fSwizzleWidth, fSrcBPP,
msarett5af4e0b2015-11-17 11:18:03 -08001229 fSampleX * fSrcBPP, fSrcOffsetUnits, fColorTable);
scroggof24f2242015-03-03 08:59:20 -08001230}