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 | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame^] | 129 | * Contains partial scanline information. |
| 130 | * @param frame Is non-NULL if the source pixels are part of an image |
| 131 | * frame that is a subset of the full image. |
| 132 | * |
| 133 | * Note that a deeper discussion of partial scanline subsets and image frame |
| 134 | * subsets is below. Currently, we do not support both simultaneously. If |
| 135 | * options->fSubset is non-NULL, frame must be NULL. |
| 136 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | * @return A new SkSwizzler or nullptr on failure. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 138 | */ |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 139 | static SkSwizzler* CreateSwizzler(SrcConfig, const SkPMColor* ctable, |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame^] | 140 | const SkImageInfo& dstInfo, const SkCodec::Options&, |
| 141 | const SkIRect* frame = nullptr); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 142 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 143 | /** |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 144 | * Swizzle a line. Generally this will be called height times, once |
| 145 | * for each row of source. |
| 146 | * By allowing the caller to pass in the dst pointer, we give the caller |
| 147 | * flexibility to use the swizzler even when the encoded data does not |
| 148 | * store the rows in order. This also improves usability for scaled and |
| 149 | * subset decodes. |
| 150 | * @param dst Where we write the output. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 151 | * @param src The next row of the source data. |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 152 | * @return A result code describing if the row was fully opaque, fully |
| 153 | * transparent, or neither |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 154 | */ |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 155 | ResultAlpha swizzle(void* dst, const uint8_t* SK_RESTRICT src); |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 156 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Implement fill using a custom width. |
| 159 | */ |
| 160 | void fill(const SkImageInfo& info, void* dst, size_t rowBytes, uint32_t colorOrIndex, |
| 161 | SkCodec::ZeroInitialized zeroInit) override { |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame^] | 162 | const SkImageInfo fillInfo = info.makeWH(fAllocatedWidth, info.height()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 163 | SkSampler::Fill(fillInfo, dst, rowBytes, colorOrIndex, zeroInit); |
| 164 | } |
| 165 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 166 | private: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 167 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Method for converting raw data to Skia pixels. |
| 170 | * @param dstRow Row in which to write the resulting pixels. |
| 171 | * @param src Row of src data, in format specified by SrcConfig |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 172 | * @param dstWidth Width in pixels of the destination |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 173 | * @param bpp if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel |
| 174 | * else, deltaSrc is bitsPerPixel |
| 175 | * @param deltaSrc bpp * sampleX |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 176 | * @param ctable Colors (used for kIndex source). |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 177 | * @param offset The offset before the first pixel to sample. |
| 178 | Is in bytes or bits based on what deltaSrc is in. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 179 | */ |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 180 | typedef ResultAlpha (*RowProc)(void* SK_RESTRICT dstRow, |
| 181 | const uint8_t* SK_RESTRICT src, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 182 | int dstWidth, int bpp, int deltaSrc, int offset, |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 183 | const SkPMColor ctable[]); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 184 | |
| 185 | const RowProc fRowProc; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 186 | const SkPMColor* fColorTable; // Unowned pointer |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame^] | 187 | |
| 188 | // Subset Swizzles |
| 189 | // There are two types of subset swizzles that we support. We do not |
| 190 | // support both at the same time. |
| 191 | // TODO: If we want to support partial scanlines for gifs (which may |
| 192 | // use frame subsets), we will need to support both subsetting |
| 193 | // modes at the same time. |
| 194 | // (1) Partial Scanlines |
| 195 | // The client only wants to write a subset of the source pixels |
| 196 | // to the destination. This subset is specified to CreateSwizzler |
| 197 | // using options->fSubset. We will store subset information in |
| 198 | // the following fields. |
| 199 | // |
| 200 | // fSrcOffset: The starting pixel of the source. |
| 201 | // fSrcOffsetUnits: Derived from fSrcOffset with two key |
| 202 | // differences: |
| 203 | // (1) This takes the size of source pixels into |
| 204 | // account by multiplying by fSrcBPP. This may |
| 205 | // be measured in bits or bytes depending on |
| 206 | // which is natural for the SrcConfig. |
| 207 | // (2) If we are sampling, this will be larger |
| 208 | // than fSrcOffset * fSrcBPP, since sampling |
| 209 | // implies that we will skip some pixels. |
| 210 | // fDstOffset: Will be zero. There is no destination offset |
| 211 | // for this type of subset. |
| 212 | // fDstOffsetBytes: Will be zero. |
| 213 | // fSrcWidth: The width of the desired subset of source |
| 214 | // pixels, before any sampling is performed. |
| 215 | // fDstWidth: Will be equal to fSrcWidth, since this is also |
| 216 | // calculated before any sampling is performed. |
| 217 | // For this type of subset, the destination width |
| 218 | // matches the desired subset of the source. |
| 219 | // fSwizzleWidth: The actual number of pixels that will be |
| 220 | // written by the RowProc. This is a scaled |
| 221 | // version of fSrcWidth/fDstWidth. |
| 222 | // fAllocatedWidth: Will be equal to fSwizzleWidth. For this type |
| 223 | // of subset, the number of pixels written is the |
| 224 | // same as the actual width of the destination. |
| 225 | // (2) Frame Subset |
| 226 | // The client will decode the entire width of the source into a |
| 227 | // subset of destination memory. This subset is specified to |
| 228 | // CreateSwizzler in the "frame" parameter. We store subset |
| 229 | // information in the following fields. |
| 230 | // |
| 231 | // fSrcOffset: Will be zero. The starting pixel of the source. |
| 232 | // fSrcOffsetUnits: Will only be non-zero if we are sampling, |
| 233 | // since sampling implies that we will skip some |
| 234 | // pixels. Note that this is measured in bits |
| 235 | // or bytes depending on which is natural for |
| 236 | // SrcConfig. |
| 237 | // fDstOffset: First pixel to write in destination. |
| 238 | // fDstOffsetBytes: fDstOffset * fDstBPP. |
| 239 | // fSrcWidth: The entire width of the source pixels, before |
| 240 | // any sampling is performed. |
| 241 | // fDstWidth: The entire width of the destination memory, |
| 242 | // before any sampling is performed. |
| 243 | // fSwizzleWidth: The actual number of pixels that will be |
| 244 | // written by the RowProc. This is a scaled |
| 245 | // version of fSrcWidth. |
| 246 | // fAllocatedWidth: The actual number of pixels in destination |
| 247 | // memory. This is a scaled version of |
| 248 | // fDstWidth. |
| 249 | // |
| 250 | // If we are not subsetting, these fields are more straightforward. |
| 251 | // fSrcOffset = fDstOffet = fDstOffsetBytes = 0 |
| 252 | // fSrcOffsetUnits may be non-zero (we will skip the first few pixels when sampling) |
| 253 | // fSrcWidth = fDstWidth = Full original width |
| 254 | // fSwizzleWidth = fAllcoatedWidth = Scaled width (if we are sampling) |
| 255 | const int fSrcOffset; |
| 256 | const int fDstOffset; |
| 257 | int fSrcOffsetUnits; |
| 258 | int fDstOffsetBytes; |
| 259 | const int fSrcWidth; |
| 260 | const int fDstWidth; |
| 261 | int fSwizzleWidth; |
| 262 | int fAllocatedWidth; |
| 263 | |
| 264 | int fSampleX; // Step between X samples |
| 265 | const int fSrcBPP; // Bits/bytes per pixel for the SrcConfig |
| 266 | // if bitsPerPixel % 8 == 0 |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 267 | // fBPP is bytesPerPixel |
| 268 | // else |
| 269 | // fBPP is bitsPerPixel |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame^] | 270 | const int fDstBPP; // Bytes per pixel for the destination color type |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 271 | |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame^] | 272 | SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcOffset, int srcWidth, int dstOffset, |
| 273 | int dstWidth, int srcBPP, int dstBPP); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 274 | |
| 275 | int onSetSampleX(int) override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 276 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 277 | }; |
| 278 | #endif // SkSwizzler_DEFINED |