Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: src/codec/SkCodec_wbmp.cpp

Issue 1254483004: Scanline decoding for wbmp (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase on new design of SkScanlineDecoder Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "SkCodec.h" 8 #include "SkCodec.h"
9 #include "SkCodecPriv.h"
9 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
11 #include "SkColorTable.h"
10 #include "SkStream.h" 12 #include "SkStream.h"
11 #include "SkCodec_wbmp.h" 13 #include "SkCodec_wbmp.h"
12 14
15 // Each bit represents a pixel, so width is actually a number of bits.
16 // A row will always be stored in bytes, so we round width up to the
17 // nearest multiple of 8 to get the number of bits actually in the row.
18 // We then divide by 8 to convert to bytes.
19 static inline size_t get_src_row_bytes(int width) {
20 return SkAlign8(width) >> 3;
21 }
22
23 static inline void setup_color_table(SkColorType colorType,
24 SkPMColor* colorPtr, int* colorCount) {
25 if (kIndex_8_SkColorType == colorType) {
26 colorPtr[0] = SK_ColorBLACK;
27 colorPtr[1] = SK_ColorWHITE;
28 *colorCount = 2;
29 }
30 }
31
13 // http://en.wikipedia.org/wiki/Variable-length_quantity 32 // http://en.wikipedia.org/wiki/Variable-length_quantity
14 static bool read_mbf(SkStream* stream, uint64_t* value) { 33 static bool read_mbf(SkStream* stream, uint64_t* value) {
15 uint64_t n = 0; 34 uint64_t n = 0;
16 uint8_t data; 35 uint8_t data;
17 const uint64_t kLimit = 0xFE00000000000000; 36 const uint64_t kLimit = 0xFE00000000000000;
18 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); 37 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
19 do { 38 do {
20 if (n & kLimit) { // Will overflow on shift by 7. 39 if (n & kLimit) { // Will overflow on shift by 7.
21 return false; 40 return false;
22 } 41 }
23 if (stream->read(&data, 1) != 1) { 42 if (stream->read(&data, 1) != 1) {
24 return false; 43 return false;
25 } 44 }
26 n = (n << 7) | (data & 0x7F); 45 n = (n << 7) | (data & 0x7F);
27 } while (data & 0x80); 46 } while (data & 0x80);
28 *value = n; 47 *value = n;
29 return true; 48 return true;
30 } 49 }
31 50
32 static bool read_header(SkStream* stream, SkISize* size) { 51 static bool read_header(SkStream* stream, SkISize* size) {
33 uint64_t width, height; 52 uint64_t width, height;
34 uint16_t data; 53 uint16_t data;
35 if (stream->read(&data, 2) != 2 || data != 0) { 54 if (stream->read(&data, 2) != 2 || data != 0) {
36 return false; 55 return false;
37 } 56 }
38 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { 57 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
39 return false; 58 return false;
40 } 59 }
41 if (!read_mbf(stream, &height) || width > 0xFFFF || !height) { 60 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
42 return false; 61 return false;
43 } 62 }
44 if (size) { 63 if (size) {
45 *size = SkISize::Make(SkToS32(width), SkToS32(height)); 64 *size = SkISize::Make(SkToS32(width), SkToS32(height));
46 } 65 }
47 return true; 66 return true;
48 } 67 }
49 68
50 #define BLACK SkPackARGB32NoCheck(0xFF, 0, 0, 0) 69 bool SkWbmpCodec::handleRewind() {
51 #define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF) 70 SkCodec::RewindState rewindState = this->rewindIfNeeded();
52 71 if (rewindState == kCouldNotRewind_RewindState) {
53 #define GRAYSCALE_BLACK 0 72 return false;
54 #define GRAYSCALE_WHITE 0xFF 73 } else if (rewindState == kRewound_RewindState) {
55 74 (void)read_header(this->stream(), NULL);
56 #define RGB565_BLACK 0 75 }
57 #define RGB565_WHITE 0xFFFF 76 return true;
58
59 static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; }
60
61 static uint8_t bit_to_bit(U8CPU bit) { return bit; }
62
63 static uint8_t bit_to_grayscale(U8CPU bit) {
64 return bit ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
65 } 77 }
66 78
67 static uint16_t bit_to_rgb565(U8CPU bit) { 79 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info,
68 return bit ? RGB565_WHITE : RGB565_BLACK; 80 const SkPMColor* ctable, const Options& opts) {
69 } 81 // TODO (msarett): Reenable support for 565 if it is desired
82 // skbug.com/3683
70 83
71 typedef void (*ExpandProc)(uint8_t*, const uint8_t*, int); 84 // Create the swizzler based on the desired color type
72 85 switch (info.colorType()) {
73 // TODO(halcanary): Add this functionality (grayscale and indexed output) to 86 case kIndex_8_SkColorType:
74 // SkSwizzler and use it here. 87 case kN32_SkColorType:
75 template <typename T, T (*TRANSFORM)(U8CPU)> 88 case kGray_8_SkColorType:
76 static void expand_bits_to_T(uint8_t* dstptr, const uint8_t* src, int bits) { 89 return SkSwizzler::CreateSwizzler(
77 T* dst = reinterpret_cast<T*>(dstptr); 90 SkSwizzler::kBit, ctable, info, opts.fZeroInitialized);
78 int bytes = bits >> 3; 91 default:
79 for (int i = 0; i < bytes; i++) { 92 return NULL;
80 U8CPU mask = *src++;
81 for (int j = 0; j < 8; j++) {
82 dst[j] = TRANSFORM((mask >> (7 - j)) & 1);
83 }
84 dst += 8;
85 }
86 bits &= 7;
87 if (bits > 0) {
88 U8CPU mask = *src;
89 do {
90 *dst++ = TRANSFORM((mask >> 7) & 1);
91 mask <<= 1;
92 } while (--bits != 0);
93 } 93 }
94 } 94 }
95 95
96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) 96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
97 : INHERITED(info, stream) {} 97 : INHERITED(info, stream) {}
98 98
99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { 99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
100 return kWBMP_SkEncodedFormat; 100 return kWBMP_SkEncodedFormat;
101 } 101 }
102 102
103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, 103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
104 void* pixels, 104 void* dst,
105 size_t rowBytes, 105 size_t rowBytes,
106 const Options& options, 106 const Options& options,
107 SkPMColor ctable[], 107 SkPMColor ctable[],
108 int* ctableCount) { 108 int* ctableCount) {
109 SkCodec::RewindState rewindState = this->rewindIfNeeded(); 109 if (!this->handleRewind()) {
110 if (rewindState == kCouldNotRewind_RewindState) {
111 return kCouldNotRewind; 110 return kCouldNotRewind;
112 } else if (rewindState == kRewound_RewindState) {
113 (void)read_header(this->stream(), NULL);
114 } 111 }
115 if (options.fSubset) { 112 if (options.fSubset) {
116 // Subsets are not supported. 113 // Subsets are not supported.
117 return kUnimplemented; 114 return kUnimplemented;
118 } 115 }
119 if (info.dimensions() != this->getInfo().dimensions()) { 116 if (info.dimensions() != this->getInfo().dimensions()) {
120 return kInvalidScale; 117 return kInvalidScale;
121 } 118 }
122 ExpandProc proc = NULL; 119
123 switch (info.colorType()) { 120 // Prepare a color table if necessary
124 case kGray_8_SkColorType: 121 setup_color_table(info.colorType(), ctable, ctableCount);
125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; 122
126 break; 123
127 case kN32_SkColorType: 124 // Initialize the swizzler
128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; 125 SkAutoTDelete<SkSwizzler> swizzler(
129 break; 126 initializeSwizzler(info, ctable, options));
130 case kIndex_8_SkColorType: 127 if (NULL == swizzler.get()) {
131 ctable[0] = BLACK; 128 return kInvalidConversion;
132 ctable[1] = WHITE;
133 *ctableCount = 2;
134 proc = expand_bits_to_T<uint8_t, bit_to_bit>;
135 break;
136 case kRGB_565_SkColorType:
137 proc = expand_bits_to_T<uint16_t, bit_to_rgb565>;
138 break;
139 default:
140 return kInvalidConversion;
141 } 129 }
130
131 // Perform the decode
142 SkISize size = info.dimensions(); 132 SkISize size = info.dimensions();
143 uint8_t* dst = static_cast<uint8_t*>(pixels); 133 size_t srcRowBytes = get_src_row_bytes(this->getInfo().width());
144 size_t srcRowBytes = SkAlign8(size.width()) >> 3;
145 SkAutoTMalloc<uint8_t> src(srcRowBytes); 134 SkAutoTMalloc<uint8_t> src(srcRowBytes);
135 void* dstRow = dst;
146 for (int y = 0; y < size.height(); ++y) { 136 for (int y = 0; y < size.height(); ++y) {
147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { 137 if (stream()->read(src.get(), srcRowBytes) != srcRowBytes) {
148 return kIncompleteInput; 138 return SkCodec::kIncompleteInput;
149 } 139 }
150 proc(dst, src.get(), size.width()); 140 swizzler->swizzle(dstRow, src.get());
151 dst += rowBytes; 141 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
152 } 142 }
153 return kSuccess; 143 return kSuccess;
154 } 144 }
155 145
156 bool SkWbmpCodec::IsWbmp(SkStream* stream) { 146 bool SkWbmpCodec::IsWbmp(SkStream* stream) {
157 return read_header(stream, NULL); 147 return read_header(stream, NULL);
158 } 148 }
159 149
160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { 150 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
161 SkAutoTDelete<SkStream> streamDeleter(stream); 151 SkAutoTDelete<SkStream> streamDeleter(stream);
162 SkISize size; 152 SkISize size;
163 if (!read_header(stream, &size)) { 153 if (!read_header(stream, &size)) {
164 return NULL; 154 return NULL;
165 } 155 }
166 SkImageInfo info = 156 SkImageInfo info =
167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, 157 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType,
168 kOpaque_SkAlphaType); 158 kOpaque_SkAlphaType);
169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); 159 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
170 } 160 }
161
162 class SkWbmpScanlineDecoder : public SkScanlineDecoder {
163 public:
164 /*
165 * Takes ownership of all pointer paramters.
166 */
167 SkWbmpScanlineDecoder(const SkImageInfo& dstInfo, SkWbmpCodec* codec)
168 : INHERITED(dstInfo)
169 , fCodec(codec)
170 , fColorTable(NULL)
171 , fSwizzler(NULL)
172 , fSrcRowBytes(get_src_row_bytes(codec->getInfo().width()))
173 , fSrcBuffer(fSrcRowBytes)
174 {}
175
176 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride {
177 void* dstRow = dst;
178 for (int y = 0; y < count; ++y) {
179 if (fCodec->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRo wBytes) {
180 return SkCodec::kIncompleteInput;
181 }
182 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
183 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
184 }
185 return SkCodec::kSuccess;
186 }
187
188 SkCodec::Result onStart(const SkImageInfo& dstInfo,
189 const SkCodec::Options& options, SkPMColor inputColorTable[],
190 int* inputColorCount) {
191 if (!fCodec->handleRewind()) {
192 return SkCodec::kCouldNotRewind;
193 }
194 if (options.fSubset) {
195 // Subsets are not supported.
196 return SkCodec::kUnimplemented;
197 }
198 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
199 return SkCodec::kInvalidScale;
200 }
201
202 // Fill in the color table
203 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount) ;
204
205 // Copy the color table to a pointer that can be owned by the scanline d ecoder
206 SkAutoTUnref<SkColorTable> colorTable(NULL);
scroggo 2015/08/04 20:24:11 It looks like you never initialize colorTable? (yo
msarett 2015/08/04 21:21:26 Yeah my mistake. There is no need for colorTable
207 if (kIndex_8_SkColorType == dstInfo.colorType()) {
208 fColorTable.reset(SkNEW_ARGS(SkColorTable, (inputColorTable, 2)));
209 }
210
211 // Initialize the swizzler
212 fSwizzler.reset(fCodec->initializeSwizzler(dstInfo,
scroggo 2015/08/04 20:24:11 How would you feel about storing the swizzler on t
msarett 2015/08/04 21:21:26 All the way back in Patch Set 1 when we were using
scroggo 2015/08/05 14:36:20 The other issue with putting it on the codec was t
msarett 2015/08/05 15:29:42 Ah yes I remember moving that typedef now.
213 get_color_ptr(colorTable.get()), options));
scroggo 2015/08/04 20:24:11 I think you want fColorTable.get()? colorTable is
msarett 2015/08/04 21:21:26 Yeah this is a bug. Now I'm confused about how my
scroggo 2015/08/05 14:36:20 Maybe the test didn't test what you thought it was
msarett 2015/08/05 15:29:42 Acknowledged.
214 if (NULL == fSwizzler.get()) {
215 return SkCodec::kInvalidInput;
216 }
217
218 return SkCodec::kSuccess;
219 }
220
221 private:
222 SkAutoTDelete<SkWbmpCodec> fCodec;
223 SkAutoTUnref<SkColorTable> fColorTable;
224 SkAutoTDelete<SkSwizzler> fSwizzler;
225 const size_t fSrcRowBytes;
226 SkAutoTMalloc<uint8_t> fSrcBuffer;
227
228 typedef SkScanlineDecoder INHERITED;
229 };
230
231 SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) {
232 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(SkWbmpCodec::NewF romStream(stream)));
233 if (!codec) {
234 return NULL;
235 }
236
237 const SkImageInfo& srcInfo = codec->getInfo();
238 // Return the new scanline decoder
239 return SkNEW_ARGS(SkWbmpScanlineDecoder, (srcInfo, codec.detach()));
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698