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

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: Add kBit mode to SkSwizzler 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 "SkColorPriv.h" 9 #include "SkColorPriv.h"
10 #include "SkColorTable.h"
10 #include "SkStream.h" 11 #include "SkStream.h"
11 #include "SkCodec_wbmp.h" 12 #include "SkCodec_wbmp.h"
12 13
14 // Each bit represents a pixel, so width is actually a number of bits.
15 // A row will always be stored in bytes, so we round width up to the
16 // nearest multiple of 8 to get the number of bits actually in the row.
17 // We then divide by 8 to convert to bytes.
18 static inline size_t get_src_row_bytes(int width) {
19 return SkAlign8(width) >> 3;
20 }
21
22 static inline void setup_color_table(SkColorType colorType,
23 SkPMColor* colorPtr, int* colorCount) {
24 if (kIndex_8_SkColorType == colorType) {
25 colorPtr[0] = SK_ColorBLACK;
26 colorPtr[1] = SK_ColorWHITE;
27 *colorCount = 2;
28 }
29 }
30
13 // http://en.wikipedia.org/wiki/Variable-length_quantity 31 // http://en.wikipedia.org/wiki/Variable-length_quantity
14 static bool read_mbf(SkStream* stream, uint64_t* value) { 32 static bool read_mbf(SkStream* stream, uint64_t* value) {
15 uint64_t n = 0; 33 uint64_t n = 0;
16 uint8_t data; 34 uint8_t data;
17 const uint64_t kLimit = 0xFE00000000000000; 35 const uint64_t kLimit = 0xFE00000000000000;
18 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); 36 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
19 do { 37 do {
20 if (n & kLimit) { // Will overflow on shift by 7. 38 if (n & kLimit) { // Will overflow on shift by 7.
21 return false; 39 return false;
22 } 40 }
23 if (stream->read(&data, 1) != 1) { 41 if (stream->read(&data, 1) != 1) {
24 return false; 42 return false;
25 } 43 }
26 n = (n << 7) | (data & 0x7F); 44 n = (n << 7) | (data & 0x7F);
27 } while (data & 0x80); 45 } while (data & 0x80);
28 *value = n; 46 *value = n;
29 return true; 47 return true;
30 } 48 }
31 49
32 static bool read_header(SkStream* stream, SkISize* size) { 50 static bool read_header(SkStream* stream, SkISize* size) {
33 uint64_t width, height; 51 uint64_t width, height;
34 uint16_t data; 52 uint16_t data;
35 if (stream->read(&data, 2) != 2 || data != 0) { 53 if (stream->read(&data, 2) != 2 || data != 0) {
36 return false; 54 return false;
37 } 55 }
38 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { 56 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
39 return false; 57 return false;
40 } 58 }
41 if (!read_mbf(stream, &height) || width > 0xFFFF || !height) { 59 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
42 return false; 60 return false;
43 } 61 }
44 if (size) { 62 if (size) {
45 *size = SkISize::Make(SkToS32(width), SkToS32(height)); 63 *size = SkISize::Make(SkToS32(width), SkToS32(height));
46 } 64 }
47 return true; 65 return true;
48 } 66 }
49 67
50 #define BLACK SkPackARGB32NoCheck(0xFF, 0, 0, 0) 68 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info,
51 #define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF) 69 const SkPMColor* ctable, const Options& opts) {
70 // TODO (msarett): Reenable support for 565 if it is desired
71 // skbug.com/3683
52 72
53 #define GRAYSCALE_BLACK 0 73 // Create the swizzler based on the desired color type
54 #define GRAYSCALE_WHITE 0xFF 74 switch (info.colorType()) {
55 75 case kIndex_8_SkColorType:
56 #define RGB565_BLACK 0 76 case kN32_SkColorType:
57 #define RGB565_WHITE 0xFFFF 77 case kGray_8_SkColorType:
58 78 return SkSwizzler::CreateSwizzler(
59 static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; } 79 SkSwizzler::kBit, ctable, info, opts.fZeroInitialized);
60 80 default:
61 static uint8_t bit_to_bit(U8CPU bit) { return bit; } 81 return NULL;
62
63 static uint8_t bit_to_grayscale(U8CPU bit) {
64 return bit ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
65 }
66
67 static uint16_t bit_to_rgb565(U8CPU bit) {
68 return bit ? RGB565_WHITE : RGB565_BLACK;
69 }
70
71 typedef void (*ExpandProc)(uint8_t*, const uint8_t*, int);
72
73 // TODO(halcanary): Add this functionality (grayscale and indexed output) to
74 // SkSwizzler and use it here.
75 template <typename T, T (*TRANSFORM)(U8CPU)>
76 static void expand_bits_to_T(uint8_t* dstptr, const uint8_t* src, int bits) {
77 T* dst = reinterpret_cast<T*>(dstptr);
78 int bytes = bits >> 3;
79 for (int i = 0; i < bytes; i++) {
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 } 82 }
94 } 83 }
95 84
96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) 85 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
97 : INHERITED(info, stream) {} 86 : INHERITED(info, stream) {}
98 87
99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { 88 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
100 return kWBMP_SkEncodedFormat; 89 return kWBMP_SkEncodedFormat;
101 } 90 }
102 91
103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, 92 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
104 void* pixels, 93 void* dst,
105 size_t rowBytes, 94 size_t rowBytes,
106 const Options& options, 95 const Options& options,
107 SkPMColor ctable[], 96 SkPMColor ctable[],
108 int* ctableCount) { 97 int* ctableCount) {
109 SkCodec::RewindState rewindState = this->rewindIfNeeded(); 98 SkCodec::RewindState rewindState = this->rewindIfNeeded();
110 if (rewindState == kCouldNotRewind_RewindState) { 99 if (rewindState == kCouldNotRewind_RewindState) {
111 return kCouldNotRewind; 100 return kCouldNotRewind;
112 } else if (rewindState == kRewound_RewindState) { 101 } else if (rewindState == kRewound_RewindState) {
113 (void)read_header(this->stream(), NULL); 102 (void)read_header(this->stream(), NULL);
114 } 103 }
115 if (options.fSubset) { 104 if (options.fSubset) {
116 // Subsets are not supported. 105 // Subsets are not supported.
117 return kUnimplemented; 106 return kUnimplemented;
118 } 107 }
119 if (info.dimensions() != this->getInfo().dimensions()) { 108 if (info.dimensions() != this->getInfo().dimensions()) {
120 return kInvalidScale; 109 return kInvalidScale;
121 } 110 }
122 ExpandProc proc = NULL; 111
123 switch (info.colorType()) { 112 // Prepare a color table if necessary
124 case kGray_8_SkColorType: 113 setup_color_table(info.colorType(), ctable, ctableCount);
125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; 114
126 break; 115
127 case kN32_SkColorType: 116 // Initialize the swizzler
128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; 117 SkAutoTDelete<SkSwizzler> swizzler(
129 break; 118 initializeSwizzler(info, ctable, options));
130 case kIndex_8_SkColorType: 119 if (NULL == swizzler.get()) {
131 ctable[0] = BLACK; 120 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 } 121 }
122
123 // Perform the decode
142 SkISize size = info.dimensions(); 124 SkISize size = info.dimensions();
143 uint8_t* dst = static_cast<uint8_t*>(pixels); 125 size_t srcRowBytes = get_src_row_bytes(this->getInfo().width());
144 size_t srcRowBytes = SkAlign8(size.width()) >> 3;
145 SkAutoTMalloc<uint8_t> src(srcRowBytes); 126 SkAutoTMalloc<uint8_t> src(srcRowBytes);
127 void* dstRow = dst;
146 for (int y = 0; y < size.height(); ++y) { 128 for (int y = 0; y < size.height(); ++y) {
147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { 129 if (stream()->read(src.get(), srcRowBytes) != srcRowBytes) {
148 return kIncompleteInput; 130 return SkCodec::kIncompleteInput;
149 } 131 }
150 proc(dst, src.get(), size.width()); 132 swizzler->swizzle(dstRow, src.get());
151 dst += rowBytes; 133 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
152 } 134 }
153 return kSuccess; 135 return kSuccess;
154 } 136 }
155 137
156 bool SkWbmpCodec::IsWbmp(SkStream* stream) { 138 bool SkWbmpCodec::IsWbmp(SkStream* stream) {
157 return read_header(stream, NULL); 139 return read_header(stream, NULL);
158 } 140 }
159 141
160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { 142 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
161 SkAutoTDelete<SkStream> streamDeleter(stream); 143 SkAutoTDelete<SkStream> streamDeleter(stream);
162 SkISize size; 144 SkISize size;
163 if (!read_header(stream, &size)) { 145 if (!read_header(stream, &size)) {
164 return NULL; 146 return NULL;
165 } 147 }
166 SkImageInfo info = 148 SkImageInfo info =
167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, 149 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType,
168 kOpaque_SkAlphaType); 150 kOpaque_SkAlphaType);
169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); 151 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
170 } 152 }
153
154 class SkWbmpScanlineDecoder : public SkScanlineDecoder {
155 public:
156 /*
157 * Takes ownership of both pointer paramters.
158 */
159 SkWbmpScanlineDecoder(const SkImageInfo& dstInfo, SkWbmpCodec* codec,
160 SkSwizzler* swizzler)
161 : INHERITED(dstInfo)
162 , fCodec(codec)
163 , fSwizzler(swizzler)
164 , fSrcRowBytes(get_src_row_bytes(codec->getInfo().width()))
165 , fSrcBuffer(fSrcRowBytes)
166 {}
167
168 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride {
169 void* dstRow = dst;
170 for (int y = 0; y < count; ++y) {
171 if (fCodec->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRo wBytes) {
172 return SkCodec::kIncompleteInput;
173 }
174 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
175 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
176 }
177 return SkCodec::kSuccess;
178 }
179
180 private:
181 SkAutoTDelete<SkWbmpCodec> fCodec;
182 SkAutoTDelete<SkSwizzler> fSwizzler;
183 const size_t fSrcRowBytes;
184 SkAutoTMalloc<uint8_t> fSrcBuffer;
185
186 typedef SkScanlineDecoder INHERITED;
187 };
188
189
190 SkScanlineDecoder* SkWbmpCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
191 const Options& options, SkPMColor ctable[], int* ctableCount) {
192 if (options.fSubset) {
193 // Subsets are not supported.
194 return NULL;
195 }
196 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
197 return NULL;
198 }
199 // Create a new SkWbmpCodec, to be owned by the scanline decoder.
200 SkStream* stream = this->stream()->duplicate();
201 if (!stream) {
202 return NULL;
203 }
204 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(
205 SkWbmpCodec::NewFromStream(stream)));
206 if (!codec) {
207 return NULL;
208 }
209
210 // Prepare a color table
211 setup_color_table(dstInfo.colorType(), ctable, ctableCount);
212
213 // Initialize the swizzler
214 SkAutoTDelete<SkSwizzler> swizzler(
215 initializeSwizzler(dstInfo, ctable, options));
scroggo 2015/07/29 17:47:12 This seems dangerous. The swizzler will hang on to
msarett 2015/07/29 18:53:57 Gotcha agreed.
216 if (NULL == swizzler.get()) {
217 return NULL;
218 }
219
220 return SkNEW_ARGS(SkWbmpScanlineDecoder, (dstInfo, codec.detach(),
221 swizzler.detach()));
222 }
OLDNEW
« no previous file with comments | « src/codec/SkCodec_wbmp.h ('k') | src/codec/SkSwizzler.h » ('j') | src/codec/SkSwizzler.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698