blob: 7eebe7f98156506ebd4f45837e2021909fc3b972 [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
8#ifndef SkSwizzler_DEFINED
9#define SkSwizzler_DEFINED
10
scroggo95526622015-03-17 05:02:17 -070011#include "SkCodec.h"
scroggof24f2242015-03-03 08:59:20 -080012#include "SkColor.h"
13#include "SkImageInfo.h"
scroggoe7fc14b2015-10-02 13:14:46 -070014#include "SkSampler.h"
scroggof24f2242015-03-03 08:59:20 -080015
scroggoe7fc14b2015-10-02 13:14:46 -070016class SkSwizzler : public SkSampler {
scroggof24f2242015-03-03 08:59:20 -080017public:
18 /**
19 * Enum describing the config of the source data.
20 */
21 enum SrcConfig {
scroggo05245902015-03-25 11:11:52 -070022 kUnknown, // Invalid type.
msarettbda86092016-01-19 10:40:12 -080023 kBit, // A single bit to distinguish between white and black.
msarett74114382015-03-16 11:55:18 -070024 kGray,
msarett93e613d2016-02-03 10:44:46 -080025 kGrayAlpha,
msarett74114382015-03-16 11:55:18 -070026 kIndex1,
27 kIndex2,
28 kIndex4,
29 kIndex,
30 kRGB,
31 kBGR,
msarettbda86092016-01-19 10:40:12 -080032 kBGRX, // The alpha channel can be anything, but the image is opaque.
msarett74114382015-03-16 11:55:18 -070033 kRGBA,
34 kBGRA,
scroggoef27d892015-10-23 09:29:22 -070035 kCMYK,
msarettbda86092016-01-19 10:40:12 -080036 kNoOp8, // kNoOp modes are used exclusively for sampling, subsetting, and
37 kNoOp16, // copying. The pixels themselves do not need to be modified.
38 kNoOp32,
scroggof24f2242015-03-03 08:59:20 -080039 };
40
msarett74114382015-03-16 11:55:18 -070041 /*
42 *
msarett74114382015-03-16 11:55:18 -070043 * Returns bits per pixel for source config
44 *
45 */
46 static int BitsPerPixel(SrcConfig sc) {
scroggof24f2242015-03-03 08:59:20 -080047 switch (sc) {
msarett99f567e2015-08-05 12:58:26 -070048 case kBit:
msarett74114382015-03-16 11:55:18 -070049 case kIndex1:
50 return 1;
51 case kIndex2:
52 return 2;
53 case kIndex4:
54 return 4;
scroggof24f2242015-03-03 08:59:20 -080055 case kGray:
56 case kIndex:
msarettbda86092016-01-19 10:40:12 -080057 case kNoOp8:
msarett74114382015-03-16 11:55:18 -070058 return 8;
msarett93e613d2016-02-03 10:44:46 -080059 case kGrayAlpha:
msarettbda86092016-01-19 10:40:12 -080060 case kNoOp16:
msarett74114382015-03-16 11:55:18 -070061 return 16;
scroggof24f2242015-03-03 08:59:20 -080062 case kRGB:
msarett74114382015-03-16 11:55:18 -070063 case kBGR:
64 return 24;
scroggof24f2242015-03-03 08:59:20 -080065 case kRGBA:
msarett74114382015-03-16 11:55:18 -070066 case kBGRX:
67 case kBGRA:
scroggoef27d892015-10-23 09:29:22 -070068 case kCMYK:
msarettbda86092016-01-19 10:40:12 -080069 case kNoOp32:
msarett74114382015-03-16 11:55:18 -070070 return 32;
scroggof24f2242015-03-03 08:59:20 -080071 default:
msarett74114382015-03-16 11:55:18 -070072 SkASSERT(false);
73 return 0;
scroggof24f2242015-03-03 08:59:20 -080074 }
75 }
76
msarett74114382015-03-16 11:55:18 -070077 /*
78 *
79 * Returns bytes per pixel for source config
80 * Raises an error if each pixel is not stored in an even number of bytes
81 *
82 */
83 static int BytesPerPixel(SrcConfig sc) {
84 SkASSERT(SkIsAlign8(BitsPerPixel(sc)));
85 return BitsPerPixel(sc) >> 3;
86 }
87
scroggof24f2242015-03-03 08:59:20 -080088 /**
89 * Create a new SkSwizzler.
scroggo95526622015-03-17 05:02:17 -070090 * @param SrcConfig Description of the format of the source.
scroggoe7fc14b2015-10-02 13:14:46 -070091 * @param ctable Unowned pointer to an array of up to 256 colors for an
92 * index source.
93 * @param dstInfo Describes the destination.
msarettfdb47572015-10-13 12:50:14 -070094 * @param options Indicates if dst is zero-initialized. The
scroggoe7fc14b2015-10-02 13:14:46 -070095 * implementation may choose to skip writing zeroes
scroggo95526622015-03-17 05:02:17 -070096 * if set to kYes_ZeroInitialized.
msarett5af4e0b2015-11-17 11:18:03 -080097 * Contains partial scanline information.
98 * @param frame Is non-NULL if the source pixels are part of an image
99 * frame that is a subset of the full image.
100 *
101 * Note that a deeper discussion of partial scanline subsets and image frame
102 * subsets is below. Currently, we do not support both simultaneously. If
103 * options->fSubset is non-NULL, frame must be NULL.
104 *
halcanary96fcdcc2015-08-27 07:41:13 -0700105 * @return A new SkSwizzler or nullptr on failure.
scroggof24f2242015-03-03 08:59:20 -0800106 */
scroggo95526622015-03-17 05:02:17 -0700107 static SkSwizzler* CreateSwizzler(SrcConfig, const SkPMColor* ctable,
msarett5af4e0b2015-11-17 11:18:03 -0800108 const SkImageInfo& dstInfo, const SkCodec::Options&,
109 const SkIRect* frame = nullptr);
scroggoe7fc14b2015-10-02 13:14:46 -0700110
msarett438b2ad2015-04-09 12:43:10 -0700111 /**
msarett614aa072015-07-27 15:13:17 -0700112 * Swizzle a line. Generally this will be called height times, once
113 * for each row of source.
114 * By allowing the caller to pass in the dst pointer, we give the caller
115 * flexibility to use the swizzler even when the encoded data does not
116 * store the rows in order. This also improves usability for scaled and
117 * subset decodes.
118 * @param dst Where we write the output.
scroggof24f2242015-03-03 08:59:20 -0800119 * @param src The next row of the source data.
scroggof24f2242015-03-03 08:59:20 -0800120 */
msaretta4970dc2016-01-11 07:23:23 -0800121 void swizzle(void* dst, const uint8_t* SK_RESTRICT src);
msarett3c309db2015-04-10 14:36:48 -0700122
msarette6dd0042015-10-09 11:07:34 -0700123 /**
124 * Implement fill using a custom width.
125 */
126 void fill(const SkImageInfo& info, void* dst, size_t rowBytes, uint32_t colorOrIndex,
127 SkCodec::ZeroInitialized zeroInit) override {
msarett5af4e0b2015-11-17 11:18:03 -0800128 const SkImageInfo fillInfo = info.makeWH(fAllocatedWidth, info.height());
msarette6dd0042015-10-09 11:07:34 -0700129 SkSampler::Fill(fillInfo, dst, rowBytes, colorOrIndex, zeroInit);
130 }
131
msarettbe8216a2015-12-04 08:00:50 -0800132 /**
133 * If fSampleX > 1, the swizzler is sampling every fSampleX'th pixel and
134 * discarding the rest.
135 *
136 * This getter is currently used by SkBmpStandardCodec for Bmp-in-Ico decodes.
137 * Ideally, the subclasses of SkCodec would have no knowledge of sampling, but
138 * this allows us to apply a transparency mask to pixels after swizzling.
139 */
140 int sampleX() const { return fSampleX; }
141
scroggof24f2242015-03-03 08:59:20 -0800142private:
msarett74114382015-03-16 11:55:18 -0700143
scroggof24f2242015-03-03 08:59:20 -0800144 /**
145 * Method for converting raw data to Skia pixels.
146 * @param dstRow Row in which to write the resulting pixels.
147 * @param src Row of src data, in format specified by SrcConfig
emmaleer8f4ba762015-08-14 07:44:46 -0700148 * @param dstWidth Width in pixels of the destination
msarett5406d6f2015-08-31 06:55:13 -0700149 * @param bpp if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel
150 * else, deltaSrc is bitsPerPixel
151 * @param deltaSrc bpp * sampleX
scroggof24f2242015-03-03 08:59:20 -0800152 * @param ctable Colors (used for kIndex source).
emmaleer8f4ba762015-08-14 07:44:46 -0700153 * @param offset The offset before the first pixel to sample.
154 Is in bytes or bits based on what deltaSrc is in.
scroggof24f2242015-03-03 08:59:20 -0800155 */
msaretta4970dc2016-01-11 07:23:23 -0800156 typedef void (*RowProc)(void* SK_RESTRICT dstRow,
157 const uint8_t* SK_RESTRICT src,
158 int dstWidth, int bpp, int deltaSrc, int offset,
159 const SkPMColor ctable[]);
scroggof24f2242015-03-03 08:59:20 -0800160
mtklein8604ca22016-01-11 13:13:55 -0800161 template <RowProc Proc>
162 static void SkipLeading8888ZerosThen(void* SK_RESTRICT dstRow,
163 const uint8_t* SK_RESTRICT src,
164 int dstWidth, int bpp, int deltaSrc, int offset,
165 const SkPMColor ctable[]);
166
msarett93e613d2016-02-03 10:44:46 -0800167 template <RowProc Proc>
168 static void SkipLeadingGrayAlphaZerosThen(void* dst, const uint8_t* src, int width, int bpp,
169 int deltaSrc, int offset, const SkPMColor ctable[]);
170
msarett19032f72016-01-21 09:59:38 -0800171 // May be NULL. We have not implemented optimized functions for all supported transforms.
172 const RowProc fFastProc;
173 // Always non-NULL. Supports sampling.
174 const RowProc fSlowProc;
175 // The actual RowProc we are using. This depends on if fFastProc is non-NULL and
176 // whether or not we are sampling.
177 RowProc fActualProc;
178
msarett74114382015-03-16 11:55:18 -0700179 const SkPMColor* fColorTable; // Unowned pointer
msarett5af4e0b2015-11-17 11:18:03 -0800180
181 // Subset Swizzles
182 // There are two types of subset swizzles that we support. We do not
183 // support both at the same time.
184 // TODO: If we want to support partial scanlines for gifs (which may
185 // use frame subsets), we will need to support both subsetting
186 // modes at the same time.
187 // (1) Partial Scanlines
188 // The client only wants to write a subset of the source pixels
189 // to the destination. This subset is specified to CreateSwizzler
190 // using options->fSubset. We will store subset information in
191 // the following fields.
192 //
193 // fSrcOffset: The starting pixel of the source.
194 // fSrcOffsetUnits: Derived from fSrcOffset with two key
195 // differences:
196 // (1) This takes the size of source pixels into
197 // account by multiplying by fSrcBPP. This may
198 // be measured in bits or bytes depending on
199 // which is natural for the SrcConfig.
200 // (2) If we are sampling, this will be larger
201 // than fSrcOffset * fSrcBPP, since sampling
202 // implies that we will skip some pixels.
203 // fDstOffset: Will be zero. There is no destination offset
204 // for this type of subset.
205 // fDstOffsetBytes: Will be zero.
206 // fSrcWidth: The width of the desired subset of source
207 // pixels, before any sampling is performed.
208 // fDstWidth: Will be equal to fSrcWidth, since this is also
209 // calculated before any sampling is performed.
210 // For this type of subset, the destination width
211 // matches the desired subset of the source.
212 // fSwizzleWidth: The actual number of pixels that will be
213 // written by the RowProc. This is a scaled
214 // version of fSrcWidth/fDstWidth.
215 // fAllocatedWidth: Will be equal to fSwizzleWidth. For this type
216 // of subset, the number of pixels written is the
217 // same as the actual width of the destination.
218 // (2) Frame Subset
219 // The client will decode the entire width of the source into a
220 // subset of destination memory. This subset is specified to
221 // CreateSwizzler in the "frame" parameter. We store subset
222 // information in the following fields.
223 //
224 // fSrcOffset: Will be zero. The starting pixel of the source.
225 // fSrcOffsetUnits: Will only be non-zero if we are sampling,
226 // since sampling implies that we will skip some
227 // pixels. Note that this is measured in bits
228 // or bytes depending on which is natural for
229 // SrcConfig.
230 // fDstOffset: First pixel to write in destination.
231 // fDstOffsetBytes: fDstOffset * fDstBPP.
232 // fSrcWidth: The entire width of the source pixels, before
233 // any sampling is performed.
234 // fDstWidth: The entire width of the destination memory,
235 // before any sampling is performed.
236 // fSwizzleWidth: The actual number of pixels that will be
237 // written by the RowProc. This is a scaled
238 // version of fSrcWidth.
239 // fAllocatedWidth: The actual number of pixels in destination
240 // memory. This is a scaled version of
241 // fDstWidth.
242 //
243 // If we are not subsetting, these fields are more straightforward.
244 // fSrcOffset = fDstOffet = fDstOffsetBytes = 0
245 // fSrcOffsetUnits may be non-zero (we will skip the first few pixels when sampling)
246 // fSrcWidth = fDstWidth = Full original width
247 // fSwizzleWidth = fAllcoatedWidth = Scaled width (if we are sampling)
248 const int fSrcOffset;
249 const int fDstOffset;
250 int fSrcOffsetUnits;
251 int fDstOffsetBytes;
252 const int fSrcWidth;
253 const int fDstWidth;
254 int fSwizzleWidth;
255 int fAllocatedWidth;
256
257 int fSampleX; // Step between X samples
258 const int fSrcBPP; // Bits/bytes per pixel for the SrcConfig
259 // if bitsPerPixel % 8 == 0
msarettfdb47572015-10-13 12:50:14 -0700260 // fBPP is bytesPerPixel
261 // else
262 // fBPP is bitsPerPixel
msarett5af4e0b2015-11-17 11:18:03 -0800263 const int fDstBPP; // Bytes per pixel for the destination color type
scroggof24f2242015-03-03 08:59:20 -0800264
msaretta51e7782016-01-12 06:51:11 -0800265 SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcOffset,
266 int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP);
scroggoe7fc14b2015-10-02 13:14:46 -0700267
268 int onSetSampleX(int) override;
msarette6dd0042015-10-09 11:07:34 -0700269
scroggof24f2242015-03-03 08:59:20 -0800270};
271#endif // SkSwizzler_DEFINED