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

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: 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/SkSwizzler.h » ('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 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info,
51 #define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF) 70 const SkPMColor* ctable, const Options& opts) {
71 // TODO (msarett): Reenable support for 565 if it is desired
72 // skbug.com/3683
52 73
53 #define GRAYSCALE_BLACK 0 74 // Create the swizzler based on the desired color type
54 #define GRAYSCALE_WHITE 0xFF 75 switch (info.colorType()) {
55 76 case kIndex_8_SkColorType:
56 #define RGB565_BLACK 0 77 case kN32_SkColorType:
57 #define RGB565_WHITE 0xFFFF 78 case kGray_8_SkColorType:
58 79 return SkSwizzler::CreateSwizzler(
59 static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; } 80 SkSwizzler::kBit, ctable, info, opts.fZeroInitialized);
60 81 default:
61 static uint8_t bit_to_bit(U8CPU bit) { return bit; } 82 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 } 83 }
94 } 84 }
95 85
96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) 86 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
97 : INHERITED(info, stream) {} 87 : INHERITED(info, stream) {}
98 88
99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { 89 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
100 return kWBMP_SkEncodedFormat; 90 return kWBMP_SkEncodedFormat;
101 } 91 }
102 92
103 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, 93 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
104 void* pixels, 94 void* dst,
105 size_t rowBytes, 95 size_t rowBytes,
106 const Options& options, 96 const Options& options,
107 SkPMColor ctable[], 97 SkPMColor ctable[],
108 int* ctableCount) { 98 int* ctableCount) {
109 SkCodec::RewindState rewindState = this->rewindIfNeeded(); 99 SkCodec::RewindState rewindState = this->rewindIfNeeded();
110 if (rewindState == kCouldNotRewind_RewindState) { 100 if (rewindState == kCouldNotRewind_RewindState) {
111 return kCouldNotRewind; 101 return kCouldNotRewind;
112 } else if (rewindState == kRewound_RewindState) { 102 } else if (rewindState == kRewound_RewindState) {
113 (void)read_header(this->stream(), NULL); 103 (void)read_header(this->stream(), NULL);
114 } 104 }
115 if (options.fSubset) { 105 if (options.fSubset) {
116 // Subsets are not supported. 106 // Subsets are not supported.
117 return kUnimplemented; 107 return kUnimplemented;
118 } 108 }
119 if (info.dimensions() != this->getInfo().dimensions()) { 109 if (info.dimensions() != this->getInfo().dimensions()) {
120 return kInvalidScale; 110 return kInvalidScale;
121 } 111 }
122 ExpandProc proc = NULL; 112
123 switch (info.colorType()) { 113 // Prepare a color table if necessary
124 case kGray_8_SkColorType: 114 setup_color_table(info.colorType(), ctable, ctableCount);
125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; 115
126 break; 116
127 case kN32_SkColorType: 117 // Initialize the swizzler
128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; 118 SkAutoTDelete<SkSwizzler> swizzler(
129 break; 119 initializeSwizzler(info, ctable, options));
130 case kIndex_8_SkColorType: 120 if (NULL == swizzler.get()) {
131 ctable[0] = BLACK; 121 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 } 122 }
123
124 // Perform the decode
142 SkISize size = info.dimensions(); 125 SkISize size = info.dimensions();
143 uint8_t* dst = static_cast<uint8_t*>(pixels); 126 size_t srcRowBytes = get_src_row_bytes(this->getInfo().width());
144 size_t srcRowBytes = SkAlign8(size.width()) >> 3;
145 SkAutoTMalloc<uint8_t> src(srcRowBytes); 127 SkAutoTMalloc<uint8_t> src(srcRowBytes);
128 void* dstRow = dst;
146 for (int y = 0; y < size.height(); ++y) { 129 for (int y = 0; y < size.height(); ++y) {
147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { 130 if (stream()->read(src.get(), srcRowBytes) != srcRowBytes) {
148 return kIncompleteInput; 131 return SkCodec::kIncompleteInput;
149 } 132 }
150 proc(dst, src.get(), size.width()); 133 swizzler->swizzle(dstRow, src.get());
151 dst += rowBytes; 134 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
152 } 135 }
153 return kSuccess; 136 return kSuccess;
154 } 137 }
155 138
156 bool SkWbmpCodec::IsWbmp(SkStream* stream) { 139 bool SkWbmpCodec::IsWbmp(SkStream* stream) {
157 return read_header(stream, NULL); 140 return read_header(stream, NULL);
158 } 141 }
159 142
160 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { 143 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
161 SkAutoTDelete<SkStream> streamDeleter(stream); 144 SkAutoTDelete<SkStream> streamDeleter(stream);
162 SkISize size; 145 SkISize size;
163 if (!read_header(stream, &size)) { 146 if (!read_header(stream, &size)) {
164 return NULL; 147 return NULL;
165 } 148 }
166 SkImageInfo info = 149 SkImageInfo info =
167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, 150 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType,
168 kOpaque_SkAlphaType); 151 kOpaque_SkAlphaType);
169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); 152 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
170 } 153 }
154
155 class SkWbmpScanlineDecoder : public SkScanlineDecoder {
156 public:
157 /*
158 * Takes ownership of all pointer paramters.
159 */
160 SkWbmpScanlineDecoder(const SkImageInfo& dstInfo, SkWbmpCodec* codec,
161 SkColorTable* colorTable, SkSwizzler* swizzler)
162 : INHERITED(dstInfo)
163 , fCodec(codec)
164 , fColorTable(colorTable)
165 , fSwizzler(swizzler)
166 , fSrcRowBytes(get_src_row_bytes(codec->getInfo().width()))
167 , fSrcBuffer(fSrcRowBytes)
168 {}
169
170 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) ove rride {
171 void* dstRow = dst;
172 for (int y = 0; y < count; ++y) {
173 if (fCodec->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRo wBytes) {
174 return SkCodec::kIncompleteInput;
175 }
176 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
177 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
178 }
179 return SkCodec::kSuccess;
180 }
181
182 private:
183 SkAutoTDelete<SkWbmpCodec> fCodec;
184 SkAutoTUnref<SkColorTable> fColorTable;
185 SkAutoTDelete<SkSwizzler> fSwizzler;
186 const size_t fSrcRowBytes;
187 SkAutoTMalloc<uint8_t> fSrcBuffer;
188
189 typedef SkScanlineDecoder INHERITED;
190 };
191
192
193 SkScanlineDecoder* SkWbmpCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
194 const Options& options, SkPMColor inputColorTable[],
195 int* inputColorCount) {
196 if (options.fSubset) {
197 // Subsets are not supported.
198 return NULL;
199 }
200 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
201 return NULL;
202 }
203 // Create a new SkWbmpCodec, to be owned by the scanline decoder.
204 SkStream* stream = this->stream()->duplicate();
205 if (!stream) {
206 return NULL;
207 }
208 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(
209 SkWbmpCodec::NewFromStream(stream)));
210 if (!codec) {
211 return NULL;
212 }
213
214 // Fill in the color table
215 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
scroggo 2015/07/29 21:49:17 Does this gracefully handle the case where the cli
msarett 2015/07/29 22:32:58 This does not handle it gracefully :(. I think it
216
217 // Copy the color table to a pointer that can be owned by the scanline decod er
218 SkAutoTUnref<SkColorTable> colorTable(NULL);
219 if (kIndex_8_SkColorType == dstInfo.colorType()) {
220 colorTable.reset(SkNEW_ARGS(SkColorTable, (inputColorTable, 2)));
221 }
222
223 // Initialize the swizzler
224 SkAutoTDelete<SkSwizzler> swizzler(
225 initializeSwizzler(dstInfo, get_color_ptr(colorTable.get()), options ));
226 if (NULL == swizzler.get()) {
227 return NULL;
228 }
229
230 return SkNEW_ARGS(SkWbmpScanlineDecoder, (dstInfo, codec.detach(),
231 colorTable.detach(), swizzler.detach()));
232 }
OLDNEW
« no previous file with comments | « src/codec/SkCodec_wbmp.h ('k') | src/codec/SkSwizzler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698