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

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: More minor 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() {
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 if (!read_header(this->stream(), NULL)) {
56 #define RGB565_BLACK 0 75 return false;
57 #define RGB565_WHITE 0xFFFF 76 }
58 77 }
59 static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; } 78 return true;
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 } 79 }
66 80
67 static uint16_t bit_to_rgb565(U8CPU bit) { 81 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info,
68 return bit ? RGB565_WHITE : RGB565_BLACK; 82 const SkPMColor* ctable, const Options& opts) {
69 } 83 // TODO (msarett): Reenable support for 565 if it is desired
scroggo 2015/08/05 19:35:38 I think we do want to support 565, in order to be
84 // skbug.com/3683
70 85
71 typedef void (*ExpandProc)(uint8_t*, const uint8_t*, int); 86 // Create the swizzler based on the desired color type
72 87 switch (info.colorType()) {
73 // TODO(halcanary): Add this functionality (grayscale and indexed output) to 88 case kIndex_8_SkColorType:
74 // SkSwizzler and use it here. 89 case kN32_SkColorType:
75 template <typename T, T (*TRANSFORM)(U8CPU)> 90 case kGray_8_SkColorType:
76 static void expand_bits_to_T(uint8_t* dstptr, const uint8_t* src, int bits) { 91 return SkSwizzler::CreateSwizzler(
77 T* dst = reinterpret_cast<T*>(dstptr); 92 SkSwizzler::kBit, ctable, info, opts.fZeroInitialized);
78 int bytes = bits >> 3; 93 default:
79 for (int i = 0; i < bytes; i++) { 94 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 } 95 }
94 } 96 }
95 97
98 SkCodec::Result SkWbmpCodec::readRow(uint8_t* row) {
99 if (this->stream()->read(row, fSrcRowBytes) != fSrcRowBytes) {
100 return kIncompleteInput;
101 }
102 return kSuccess;
103 }
104
96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) 105 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
97 : INHERITED(info, stream) {} 106 : INHERITED(info, stream)
107 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
108 {}
98 109
99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { 110 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
100 return kWBMP_SkEncodedFormat; 111 return kWBMP_SkEncodedFormat;
101 } 112 }
102 113
103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, 114 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
104 void* pixels, 115 void* dst,
105 size_t rowBytes, 116 size_t rowBytes,
106 const Options& options, 117 const Options& options,
107 SkPMColor ctable[], 118 SkPMColor ctable[],
108 int* ctableCount) { 119 int* ctableCount) {
109 SkCodec::RewindState rewindState = this->rewindIfNeeded(); 120 if (!this->handleRewind()) {
110 if (rewindState == kCouldNotRewind_RewindState) {
111 return kCouldNotRewind; 121 return kCouldNotRewind;
112 } else if (rewindState == kRewound_RewindState) {
113 (void)read_header(this->stream(), NULL);
114 } 122 }
115 if (options.fSubset) { 123 if (options.fSubset) {
116 // Subsets are not supported. 124 // Subsets are not supported.
117 return kUnimplemented; 125 return kUnimplemented;
118 } 126 }
119 if (info.dimensions() != this->getInfo().dimensions()) { 127 if (info.dimensions() != this->getInfo().dimensions()) {
120 return kInvalidScale; 128 return kInvalidScale;
121 } 129 }
122 ExpandProc proc = NULL; 130
123 switch (info.colorType()) { 131 // Prepare a color table if necessary
124 case kGray_8_SkColorType: 132 setup_color_table(info.colorType(), ctable, ctableCount);
125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; 133
126 break; 134
127 case kN32_SkColorType: 135 // Initialize the swizzler
128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; 136 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, op tions));
129 break; 137 if (NULL == swizzler.get()) {
130 case kIndex_8_SkColorType: 138 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 } 139 }
140
141 // Perform the decode
142 SkISize size = info.dimensions(); 142 SkISize size = info.dimensions();
143 uint8_t* dst = static_cast<uint8_t*>(pixels); 143 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
144 size_t srcRowBytes = SkAlign8(size.width()) >> 3; 144 void* dstRow = dst;
145 SkAutoTMalloc<uint8_t> src(srcRowBytes);
146 for (int y = 0; y < size.height(); ++y) { 145 for (int y = 0; y < size.height(); ++y) {
147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { 146 Result rowResult = this->readRow(src.get());
148 return kIncompleteInput; 147 if (kSuccess != rowResult) {
148 return rowResult;
149 } 149 }
150 proc(dst, src.get(), size.width()); 150 swizzler->swizzle(dstRow, src.get());
151 dst += rowBytes; 151 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
152 } 152 }
153 return kSuccess; 153 return kSuccess;
154 } 154 }
155 155
156 bool SkWbmpCodec::IsWbmp(SkStream* stream) { 156 bool SkWbmpCodec::IsWbmp(SkStream* stream) {
157 return read_header(stream, NULL); 157 return read_header(stream, NULL);
158 } 158 }
159 159
160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { 160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
161 SkAutoTDelete<SkStream> streamDeleter(stream); 161 SkAutoTDelete<SkStream> streamDeleter(stream);
162 SkISize size; 162 SkISize size;
163 if (!read_header(stream, &size)) { 163 if (!read_header(stream, &size)) {
164 return NULL; 164 return NULL;
165 } 165 }
166 SkImageInfo info = 166 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, 167 kGray_8_SkColorType, kOpaque_SkAlphaType);
168 kOpaque_SkAlphaType);
169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); 168 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
170 } 169 }
170
171 class SkWbmpScanlineDecoder : public SkScanlineDecoder {
172 public:
173 /*
174 * Takes ownership of all pointer paramters.
175 */
176 SkWbmpScanlineDecoder(SkWbmpCodec* codec)
177 : INHERITED(codec->getInfo())
178 , fCodec(codec)
179 , fColorTable(NULL)
180 , fSwizzler(NULL)
181 , fSrcBuffer(codec->fSrcRowBytes)
182 {}
183
184 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride {
185 void* dstRow = dst;
186 for (int y = 0; y < count; ++y) {
187 SkCodec::Result rowResult = fCodec->readRow(fSrcBuffer.get());
188 if (SkCodec::kSuccess != rowResult) {
189 return rowResult;
190 }
191 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
192 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
193 }
194 return SkCodec::kSuccess;
195 }
196
197 SkCodec::Result onStart(const SkImageInfo& dstInfo,
198 const SkCodec::Options& options, SkPMColor inputColorTable[],
199 int* inputColorCount) {
200 if (!fCodec->handleRewind()) {
201 return SkCodec::kCouldNotRewind;
202 }
203 if (options.fSubset) {
204 // Subsets are not supported.
205 return SkCodec::kUnimplemented;
206 }
207 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
208 return SkCodec::kInvalidScale;
209 }
210
211 // Fill in the color table
212 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount) ;
213
214 // Copy the color table to a pointer that can be owned by the scanline d ecoder
215 if (kIndex_8_SkColorType == dstInfo.colorType()) {
216 fColorTable.reset(SkNEW_ARGS(SkColorTable, (inputColorTable, 2)));
217 }
218
219 // Initialize the swizzler
220 fSwizzler.reset(fCodec->initializeSwizzler(dstInfo,
221 get_color_ptr(fColorTable.get()), options));
222 if (NULL == fSwizzler.get()) {
223 return SkCodec::kInvalidInput;
224 }
225
226 return SkCodec::kSuccess;
227 }
228
229 private:
230 SkAutoTDelete<SkWbmpCodec> fCodec;
231 SkAutoTUnref<SkColorTable> fColorTable;
232 SkAutoTDelete<SkSwizzler> fSwizzler;
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