scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #ifndef SkSwizzler_DEFINED |
| 9 | #define SkSwizzler_DEFINED |
| 10 | |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 11 | #include "SkCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 12 | #include "SkColor.h" |
| 13 | #include "SkImageInfo.h" |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 14 | #include "SkSampler.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 15 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 16 | class SkSwizzler : public SkSampler { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 17 | public: |
| 18 | /** |
| 19 | * Enum describing the config of the source data. |
| 20 | */ |
| 21 | enum SrcConfig { |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 22 | kUnknown, // Invalid type. |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 23 | kBit, // A single bit to distinguish between white and black |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 24 | kGray, |
| 25 | kIndex1, |
| 26 | kIndex2, |
| 27 | kIndex4, |
| 28 | kIndex, |
| 29 | kRGB, |
| 30 | kBGR, |
| 31 | kRGBX, |
| 32 | kBGRX, |
| 33 | kRGBA, |
| 34 | kBGRA, |
| 35 | kRGB_565, |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame^] | 36 | kCMYK, |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 39 | /* |
| 40 | * |
| 41 | * Result code for the alpha components of a row. |
| 42 | * |
| 43 | */ |
| 44 | typedef uint16_t ResultAlpha; |
| 45 | static const ResultAlpha kOpaque_ResultAlpha = 0xFFFF; |
| 46 | static const ResultAlpha kTransparent_ResultAlpha = 0x0000; |
| 47 | |
| 48 | /* |
| 49 | * |
| 50 | * Checks if the result of decoding a row indicates that the row was |
| 51 | * transparent. |
| 52 | * |
| 53 | */ |
| 54 | static bool IsTransparent(ResultAlpha r) { |
| 55 | return kTransparent_ResultAlpha == r; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * |
| 60 | * Checks if the result of decoding a row indicates that the row was |
| 61 | * opaque. |
| 62 | * |
| 63 | */ |
| 64 | static bool IsOpaque(ResultAlpha r) { |
| 65 | return kOpaque_ResultAlpha == r; |
| 66 | } |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 67 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 68 | /* |
| 69 | * |
| 70 | * Constructs the proper result code based on accumulated alpha masks |
| 71 | * |
| 72 | */ |
| 73 | static ResultAlpha GetResult(uint8_t zeroAlpha, uint8_t maxAlpha); |
| 74 | |
| 75 | /* |
| 76 | * |
| 77 | * Returns bits per pixel for source config |
| 78 | * |
| 79 | */ |
| 80 | static int BitsPerPixel(SrcConfig sc) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 81 | switch (sc) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 82 | case kBit: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 83 | case kIndex1: |
| 84 | return 1; |
| 85 | case kIndex2: |
| 86 | return 2; |
| 87 | case kIndex4: |
| 88 | return 4; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 89 | case kGray: |
| 90 | case kIndex: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 91 | return 8; |
| 92 | case kRGB_565: |
| 93 | return 16; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 94 | case kRGB: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 95 | case kBGR: |
| 96 | return 24; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 97 | case kRGBX: |
| 98 | case kRGBA: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 99 | case kBGRX: |
| 100 | case kBGRA: |
scroggo | ef27d89 | 2015-10-23 09:29:22 -0700 | [diff] [blame^] | 101 | case kCMYK: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 102 | return 32; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 103 | default: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 104 | SkASSERT(false); |
| 105 | return 0; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 109 | /* |
| 110 | * |
| 111 | * Returns bytes per pixel for source config |
| 112 | * Raises an error if each pixel is not stored in an even number of bytes |
| 113 | * |
| 114 | */ |
| 115 | static int BytesPerPixel(SrcConfig sc) { |
| 116 | SkASSERT(SkIsAlign8(BitsPerPixel(sc))); |
| 117 | return BitsPerPixel(sc) >> 3; |
| 118 | } |
| 119 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Create a new SkSwizzler. |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 122 | * @param SrcConfig Description of the format of the source. |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 123 | * @param ctable Unowned pointer to an array of up to 256 colors for an |
| 124 | * index source. |
| 125 | * @param dstInfo Describes the destination. |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 126 | * @param options Indicates if dst is zero-initialized. The |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 127 | * implementation may choose to skip writing zeroes |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 128 | * if set to kYes_ZeroInitialized. |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 129 | * Contains subset information. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | * @return A new SkSwizzler or nullptr on failure. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 131 | */ |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 132 | static SkSwizzler* CreateSwizzler(SrcConfig, const SkPMColor* ctable, |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 133 | const SkImageInfo& dstInfo, const SkCodec::Options&); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 134 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 135 | /** |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 136 | * Swizzle a line. Generally this will be called height times, once |
| 137 | * for each row of source. |
| 138 | * By allowing the caller to pass in the dst pointer, we give the caller |
| 139 | * flexibility to use the swizzler even when the encoded data does not |
| 140 | * store the rows in order. This also improves usability for scaled and |
| 141 | * subset decodes. |
| 142 | * @param dst Where we write the output. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 143 | * @param src The next row of the source data. |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 144 | * @return A result code describing if the row was fully opaque, fully |
| 145 | * transparent, or neither |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 146 | */ |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 147 | ResultAlpha swizzle(void* dst, const uint8_t* SK_RESTRICT src); |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 148 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Implement fill using a custom width. |
| 151 | */ |
| 152 | void fill(const SkImageInfo& info, void* dst, size_t rowBytes, uint32_t colorOrIndex, |
| 153 | SkCodec::ZeroInitialized zeroInit) override { |
| 154 | const SkImageInfo fillInfo = info.makeWH(fDstWidth, info.height()); |
| 155 | SkSampler::Fill(fillInfo, dst, rowBytes, colorOrIndex, zeroInit); |
| 156 | } |
| 157 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 158 | private: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 159 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 160 | /** |
| 161 | * Method for converting raw data to Skia pixels. |
| 162 | * @param dstRow Row in which to write the resulting pixels. |
| 163 | * @param src Row of src data, in format specified by SrcConfig |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 164 | * @param dstWidth Width in pixels of the destination |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 165 | * @param bpp if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel |
| 166 | * else, deltaSrc is bitsPerPixel |
| 167 | * @param deltaSrc bpp * sampleX |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 168 | * @param ctable Colors (used for kIndex source). |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 169 | * @param offset The offset before the first pixel to sample. |
| 170 | Is in bytes or bits based on what deltaSrc is in. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 171 | */ |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 172 | typedef ResultAlpha (*RowProc)(void* SK_RESTRICT dstRow, |
| 173 | const uint8_t* SK_RESTRICT src, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 174 | int dstWidth, int bpp, int deltaSrc, int offset, |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 175 | const SkPMColor ctable[]); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 176 | |
| 177 | const RowProc fRowProc; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 178 | const SkPMColor* fColorTable; // Unowned pointer |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 179 | const int fSrcOffset; // Offset of the src in pixels, allows for partial |
| 180 | // scanline decodes. |
| 181 | int fX0; // Start coordinate for the src, may be different than |
| 182 | // fSrcOffset if we are sampling. |
msarett | 80803ff | 2015-10-16 10:54:12 -0700 | [diff] [blame] | 183 | const int fSubsetWidth; // Width of the subset of the source before any sampling. |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 184 | int fDstWidth; // Width of dst, which may differ with sampling. |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 185 | int fSampleX; // step between X samples |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 186 | const int fBPP; // if bitsPerPixel % 8 == 0 |
| 187 | // fBPP is bytesPerPixel |
| 188 | // else |
| 189 | // fBPP is bitsPerPixel |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 190 | |
msarett | 80803ff | 2015-10-16 10:54:12 -0700 | [diff] [blame] | 191 | SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcOffset, int subsetWidth, int bpp); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 192 | |
| 193 | int onSetSampleX(int) override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 194 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 195 | }; |
| 196 | #endif // SkSwizzler_DEFINED |