OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkSwizzler_DEFINED | 8 #ifndef SkSwizzler_DEFINED |
9 #define SkSwizzler_DEFINED | 9 #define SkSwizzler_DEFINED |
10 | 10 |
11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
12 #include "SkColor.h" | 12 #include "SkColor.h" |
13 #include "SkImageInfo.h" | 13 #include "SkImageInfo.h" |
14 | 14 |
15 class SkSwizzler : public SkNoncopyable { | 15 class SkSwizzler : public SkNoncopyable { |
16 public: | 16 public: |
17 /** | 17 /** |
18 * Enum describing the config of the source data. | 18 * Enum describing the config of the source data. |
19 */ | 19 */ |
20 enum SrcConfig { | 20 enum SrcConfig { |
21 kGray, // 1 byte per pixel | 21 kGray, |
22 kIndex, // 1 byte per pixel | 22 kIndex1, |
23 kRGB, // 3 bytes per pixel | 23 kIndex2, |
24 kRGBX, // 4 byes per pixel (ignore 4th) | 24 kIndex4, |
25 kRGBA, // 4 bytes per pixel | 25 kIndex, |
26 kRGB_565 // 2 bytes per pixel | 26 kRGB, |
| 27 kBGR, |
| 28 kRGBX, |
| 29 kBGRX, |
| 30 kRGBA, |
| 31 kBGRA, |
| 32 kRGB_565, |
| 33 kMask16, |
| 34 kMask24, |
| 35 kMask32 |
27 }; | 36 }; |
28 | 37 |
29 static int BytesPerPixel(SrcConfig sc) { | 38 /** |
| 39 * In mask SrcConfigs, used as bit masks for color components. |
| 40 */ |
| 41 struct ColorMasks { |
| 42 uint32_t redMask; |
| 43 uint32_t greenMask; |
| 44 uint32_t blueMask; |
| 45 uint32_t alphaMask; |
| 46 }; |
| 47 |
| 48 /** |
| 49 * Describes if rows of the input start at the top or bottom of the image. |
| 50 */ |
| 51 enum RowOrder { |
| 52 kTopDown_RowOrder, |
| 53 kBottomUp_RowOrder |
| 54 }; |
| 55 |
| 56 /** |
| 57 * Describes if we will render fully transparent images as opaque. |
| 58 */ |
| 59 enum ZeroAlpha { |
| 60 kTransparentAsOpaque_ZeroAlpha, |
| 61 kNormal_ZeroAlpha |
| 62 }; |
| 63 |
| 64 static int BitsPerPixel(SrcConfig sc) { |
30 switch (sc) { | 65 switch (sc) { |
31 case kGray: | 66 case kIndex1: |
| 67 return 1; |
| 68 case kIndex2: |
| 69 return 2; |
| 70 case kIndex4: |
| 71 return 4; |
32 case kIndex: | 72 case kIndex: |
33 return 1; | 73 return 8; |
| 74 case kRGB_565: |
| 75 case kMask16: |
| 76 return 16; |
34 case kRGB: | 77 case kRGB: |
35 return 3; | 78 case kBGR: |
| 79 case kMask24: |
| 80 return 24; |
36 case kRGBX: | 81 case kRGBX: |
37 case kRGBA: | 82 case kRGBA: |
38 return 4; | 83 case kBGRX: |
39 case kRGB_565: | 84 case kBGRA: |
40 return 2; | 85 case kMask32: |
| 86 return 32; |
41 } | 87 } |
42 } | 88 } |
43 | 89 |
44 /** | 90 /** |
45 * Create a new SkSwizzler. | 91 * Create a new SkSwizzler. |
46 * @param sc SrcConfig | 92 * @param sc SrcConfig |
47 * @param info dimensions() describe both the src and the dst. | 93 * @param info dimensions() describe both the src and the dst. |
48 * Other fields describe the dst. | 94 * Other fields describe the dst. |
49 * @param dst Destination to write pixels. Must match info and dstRowBytes | 95 * @param dst Destination to write pixels. Must match info and dstRowBytes |
50 * @param dstRowBytes rowBytes for dst. | 96 * @param dstRowBytes rowBytes for dst. |
51 * @param skipZeroes Whether to skip writing zeroes. Useful if dst is | 97 * @param skipZeroes Whether to skip writing zeroes. Useful if dst is |
52 * zero-initialized. The implementation may or may not respect
this. | 98 * zero-initialized. The implementation may or may not respect
this. |
| 99 * @param bitMasks Masks used to separate the color components of a pixel |
| 100 * @param zeroAlpha Some inputs (bmp) have an alpha channel that is unused. |
| 101 * Users of this channel often fill it with zeros, but still |
| 102 * expect the image to be opaque. When zeroAlpha is |
| 103 * kTransparentAsOpaque, completely transparent images should |
| 104 * be rendered as opaque. Images that are not completely |
| 105 * transparent should be treated normally. |
| 106 * @param rowOrder Order in which input rows are swizzled |
53 * @return A new SkSwizzler or NULL on failure. | 107 * @return A new SkSwizzler or NULL on failure. |
54 */ | 108 */ |
55 static SkSwizzler* CreateSwizzler(SrcConfig sc, const SkPMColor* ctable, | 109 static SkSwizzler* CreateSwizzler( |
56 const SkImageInfo& info, void* dst, | 110 SrcConfig sc, const SkPMColor* ctable, const SkImageInfo& info, |
57 size_t dstRowBytes, bool skipZeroes); | 111 void* dst, size_t dstRowBytes, bool skipZeroes, |
| 112 const ColorMasks bitMasks = { 0, 0, 0, 0 }, |
| 113 const ZeroAlpha zeroAlpha = kNormal_ZeroAlpha, |
| 114 const RowOrder rowOrder = kTopDown_RowOrder); |
58 /** | 115 /** |
59 * Swizzle the next line. Call height times, once for each row of source. | 116 * Swizzle the next line. Call height times, once for each row of source. |
60 * @param src The next row of the source data. | 117 * @param src The next row of the source data. |
61 * @return Whether the row had non-opaque alpha. | 118 * @return Whether the row had non-opaque alpha. |
62 */ | 119 */ |
63 bool next(const uint8_t* SK_RESTRICT src); | 120 bool next(const uint8_t* SK_RESTRICT src); |
64 private: | 121 private: |
65 /** | 122 /** |
66 * Method for converting raw data to Skia pixels. | 123 * Method for converting raw data to Skia pixels. |
67 * @param dstRow Row in which to write the resulting pixels. | 124 * @param dstRow Row in which to write the resulting pixels. |
68 * @param src Row of src data, in format specified by SrcConfig | 125 * @param src Row of src data, in format specified by SrcConfig |
69 * @param width Width in pixels | 126 * @param width Width in pixels |
70 * @param bpp bytes per pixel of the source. | 127 * @param bpp bytes per pixel of the source. |
71 * @param y Line of source. | 128 * @param y Line of source. |
72 * @param ctable Colors (used for kIndex source). | 129 * @param ctable Colors (used for kIndex source). |
| 130 * @param masks Bit masks used to separate the color components of a pixel |
| 131 * @param zeroAlpha Some inputs (bmp) have an alpha channel that is unused. |
| 132 * Users of this channel often fill it with zeros, but still |
| 133 * expect the image to be opaque. When zeroAlpha is |
| 134 * kTransparentAsOpaque, completely transparent images should |
| 135 * be rendered as opaque. Images that are not completely |
| 136 * transparent should be treated normally. |
| 137 * @param seenNonZeroAlphaPtr A ptr to the field that indicates if we have |
| 138 * seen a non-zero alpha value in an image. This is used in |
| 139 * conjunction with zeroAlpha to ensure accidentally fully |
| 140 * transparent images are rendered as opaque. |
| 141 * @param zeroPrevRowsPtr A ptr to the field that indicates if we need |
| 142 * to set all previously decoded rows as transparent. This |
| 143 * occurs when we have decoded transparent rows as opaque |
| 144 * (because zeroAlpha is true), but then find a pixel that is |
| 145 * not transparent. On this event, we must return to |
| 146 * previously decoded rows and set them as transparent. |
73 */ | 147 */ |
74 typedef bool (*RowProc)(void* SK_RESTRICT dstRow, | 148 typedef bool (*RowProc)(void* SK_RESTRICT dstRow, |
75 const uint8_t* SK_RESTRICT src, | 149 const uint8_t* SK_RESTRICT src, |
76 int width, int bpp, int y, | 150 int width, int bitsPerPixel, int y, |
77 const SkPMColor ctable[]); | 151 const SkPMColor ctable[], const ColorMasks masks, |
| 152 const ZeroAlpha zeroAlpha, |
| 153 bool* seenNonZeroAlphaPtr, bool* zeroPrevRowsPtr); |
78 | 154 |
79 const RowProc fRowProc; | 155 const RowProc fRowProc; |
80 const SkPMColor* fColorTable; // Unowned pointer | 156 const SkPMColor* fColorTable; // Unowned pointer |
81 const int fSrcPixelSize; | 157 const ColorMasks fBitMasks; |
| 158 const int fSrcBitsPerPixel; |
82 const SkImageInfo fDstInfo; | 159 const SkImageInfo fDstInfo; |
83 void* fDstRow; | 160 void* fDstRow; |
84 const size_t fDstRowBytes; | 161 const size_t fDstRowBytes; |
85 int fCurrY; | 162 int fCurrY; |
| 163 const ZeroAlpha fZeroAlpha; |
| 164 const RowOrder fRowOrder; |
| 165 bool fSeenNonZeroAlpha; |
| 166 bool fZeroPrevRows; |
86 | 167 |
87 SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcBpp, | 168 SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcBPP, |
88 const SkImageInfo& info, void* dst, size_t rowBytes); | 169 const SkImageInfo& info, void* dst, size_t rowBytes, |
| 170 const ColorMasks bitMasks, const ZeroAlpha zeroAlpha, |
| 171 const RowOrder rowOrder); |
89 | 172 |
90 }; | 173 }; |
91 #endif // SkSwizzler_DEFINED | 174 #endif // SkSwizzler_DEFINED |
92 | 175 |
OLD | NEW |