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

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: Various fixes 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
« no previous file with comments | « src/codec/SkCodec_wbmp.h ('k') | src/codec/SkScanlineDecoder.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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() {
scroggo 2015/08/05 17:55:37 I'm beginning to think this should be in the base
msarett 2015/08/05 18:50:33 +1 for this in a separate CL
scroggo 2015/08/05 19:35:38 Filed skbug.com/4170
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);
scroggo 2015/08/05 17:55:37 FWIW, in SkPngCodec, we check the return value of
msarett 2015/08/05 18:50:33 Agreed. Fixed this in wbmp.
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 SkCodec::Result SkWbmpCodec::readRow(uint8_t* src, size_t rowBytes) {
scroggo 2015/08/05 17:55:37 Why do you need the rowBytes parameter? This is ba
msarett 2015/08/05 18:50:33 Let's compromise on row as the name for the first
97 if (this->stream()->read(src, rowBytes) != rowBytes) {
scroggo 2015/08/05 17:55:37 nit: we use four space indents (Ah, I think you co
msarett 2015/08/05 18:50:33 Yup it's fixed now.
98 return kIncompleteInput;
99 }
100 return kSuccess;
101 }
102
96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) 103 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
97 : INHERITED(info, stream) {} 104 : INHERITED(info, stream)
105 {}
98 106
99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { 107 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
100 return kWBMP_SkEncodedFormat; 108 return kWBMP_SkEncodedFormat;
101 } 109 }
102 110
103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, 111 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
104 void* pixels, 112 void* dst,
105 size_t rowBytes, 113 size_t rowBytes,
106 const Options& options, 114 const Options& options,
107 SkPMColor ctable[], 115 SkPMColor ctable[],
108 int* ctableCount) { 116 int* ctableCount) {
109 SkCodec::RewindState rewindState = this->rewindIfNeeded(); 117 if (!this->handleRewind()) {
110 if (rewindState == kCouldNotRewind_RewindState) {
111 return kCouldNotRewind; 118 return kCouldNotRewind;
112 } else if (rewindState == kRewound_RewindState) {
113 (void)read_header(this->stream(), NULL);
114 } 119 }
115 if (options.fSubset) { 120 if (options.fSubset) {
116 // Subsets are not supported. 121 // Subsets are not supported.
117 return kUnimplemented; 122 return kUnimplemented;
118 } 123 }
119 if (info.dimensions() != this->getInfo().dimensions()) { 124 if (info.dimensions() != this->getInfo().dimensions()) {
120 return kInvalidScale; 125 return kInvalidScale;
121 } 126 }
122 ExpandProc proc = NULL; 127
123 switch (info.colorType()) { 128 // Prepare a color table if necessary
124 case kGray_8_SkColorType: 129 setup_color_table(info.colorType(), ctable, ctableCount);
125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; 130
126 break; 131
127 case kN32_SkColorType: 132 // Initialize the swizzler
128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; 133 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, op tions));
129 break; 134 if (NULL == swizzler.get()) {
130 case kIndex_8_SkColorType: 135 return kInvalidConversion;
131 ctable[0] = BLACK;
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 } 136 }
137
138 // Perform the decode
142 SkISize size = info.dimensions(); 139 SkISize size = info.dimensions();
143 uint8_t* dst = static_cast<uint8_t*>(pixels); 140 size_t srcRowBytes = get_src_row_bytes(this->getInfo().width());
144 size_t srcRowBytes = SkAlign8(size.width()) >> 3;
145 SkAutoTMalloc<uint8_t> src(srcRowBytes); 141 SkAutoTMalloc<uint8_t> src(srcRowBytes);
142 void* dstRow = dst;
146 for (int y = 0; y < size.height(); ++y) { 143 for (int y = 0; y < size.height(); ++y) {
147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { 144 Result rowResult = this->readRow(src.get(), srcRowBytes);
148 return kIncompleteInput; 145 if (kSuccess != rowResult) {
146 return rowResult;
149 } 147 }
150 proc(dst, src.get(), size.width()); 148 swizzler->swizzle(dstRow, src.get());
151 dst += rowBytes; 149 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
152 } 150 }
153 return kSuccess; 151 return kSuccess;
154 } 152 }
155 153
156 bool SkWbmpCodec::IsWbmp(SkStream* stream) { 154 bool SkWbmpCodec::IsWbmp(SkStream* stream) {
157 return read_header(stream, NULL); 155 return read_header(stream, NULL);
158 } 156 }
159 157
160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { 158 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
161 SkAutoTDelete<SkStream> streamDeleter(stream); 159 SkAutoTDelete<SkStream> streamDeleter(stream);
162 SkISize size; 160 SkISize size;
163 if (!read_header(stream, &size)) { 161 if (!read_header(stream, &size)) {
164 return NULL; 162 return NULL;
165 } 163 }
166 SkImageInfo info = 164 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, 165 kGray_8_SkColorType, kOpaque_SkAlphaType);
168 kOpaque_SkAlphaType);
169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); 166 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
170 } 167 }
168
169 class SkWbmpScanlineDecoder : public SkScanlineDecoder {
170 public:
171 /*
172 * Takes ownership of all pointer paramters.
173 */
174 SkWbmpScanlineDecoder(SkWbmpCodec* codec)
175 : INHERITED(codec->getInfo())
176 , fCodec(codec)
177 , fColorTable(NULL)
178 , fSwizzler(NULL)
179 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
180 , fSrcBuffer(fSrcRowBytes)
181 {}
182
183 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride {
184 void* dstRow = dst;
185 for (int y = 0; y < count; ++y) {
186 SkCodec::Result rowResult = fCodec->readRow(fSrcBuffer.get(), fSrcRo wBytes);
187 if (SkCodec::kSuccess != rowResult) {
188 return rowResult;
189 }
190 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
191 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
192 }
193 return SkCodec::kSuccess;
194 }
195
196 SkCodec::Result onStart(const SkImageInfo& dstInfo,
197 const SkCodec::Options& options, SkPMColor inputColorTable[],
198 int* inputColorCount) {
199 if (!fCodec->handleRewind()) {
200 return SkCodec::kCouldNotRewind;
201 }
202 if (options.fSubset) {
203 // Subsets are not supported.
204 return SkCodec::kUnimplemented;
205 }
206 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
207 return SkCodec::kInvalidScale;
208 }
209
210 // Fill in the color table
211 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount) ;
212
213 // Copy the color table to a pointer that can be owned by the scanline d ecoder
214 if (kIndex_8_SkColorType == dstInfo.colorType()) {
215 fColorTable.reset(SkNEW_ARGS(SkColorTable, (inputColorTable, 2)));
216 }
217
218 // Initialize the swizzler
219 fSwizzler.reset(fCodec->initializeSwizzler(dstInfo,
220 get_color_ptr(fColorTable.get()), options));
221 if (NULL == fSwizzler.get()) {
222 return SkCodec::kInvalidInput;
223 }
224
225 return SkCodec::kSuccess;
226 }
227
228 private:
229 SkAutoTDelete<SkWbmpCodec> fCodec;
230 SkAutoTUnref<SkColorTable> fColorTable;
231 SkAutoTDelete<SkSwizzler> fSwizzler;
232 const size_t fSrcRowBytes;
233 SkAutoTMalloc<uint8_t> fSrcBuffer;
234
235 typedef SkScanlineDecoder INHERITED;
236 };
237
238 SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) {
239 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(
240 SkWbmpCodec::NewFromStream(stream)));
241 if (!codec) {
242 return NULL;
243 }
244
245 // Return the new scanline decoder
246 return SkNEW_ARGS(SkWbmpScanlineDecoder, (codec.detach()));
247 }
OLDNEW
« no previous file with comments | « src/codec/SkCodec_wbmp.h ('k') | src/codec/SkScanlineDecoder.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698