OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkCodec.h" | |
9 #include "SkColorPriv.h" | |
10 #include "SkStream.h" | |
11 #include "SkCodec_wbmp.h" | |
12 | |
13 // http://en.wikipedia.org/wiki/Variable-length_quantity | |
14 static bool read_mbf(SkStream* stream, uint64_t* value) { | |
15 uint64_t n = 0; | |
16 uint8_t data; | |
17 const uint64_t kLimit = 0xFE00000000000000; | |
18 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); | |
19 do { | |
20 if (n & kLimit) { // Will overflow on shift by 7. | |
21 return false; | |
22 } | |
23 if (stream->read(&data, 1) != 1) { | |
24 return false; | |
25 } | |
26 n = (n << 7) | (data & 0x7F); | |
27 } while (data & 0x80); | |
28 *value = n; | |
29 return true; | |
30 } | |
31 | |
32 static bool read_header(SkStream* stream, SkISize* size) { | |
33 uint64_t width, height; | |
34 uint16_t data; | |
35 if (stream->read(&data, 2) != 2 || data != 0) { | |
36 return false; | |
37 } | |
38 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { | |
39 return false; | |
40 } | |
41 if (!read_mbf(stream, &height) || width > 0xFFFF || !height) { | |
42 return false; | |
43 } | |
44 if (size) { | |
45 *size = SkISize::Make(SkToS32(width), SkToS32(height)); | |
46 } | |
47 return true; | |
48 } | |
49 | |
50 #define BLACK SkPackARGB32NoCheck(0xFF, 0, 0, 0) | |
51 #define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF) | |
52 | |
53 #define GRAYSCALE_BLACK 0 | |
54 #define GRAYSCALE_WHITE 0xFF | |
55 | |
56 #define RGB565_BLACK 0 | |
57 #define RGB565_WHITE 0xFFFF | |
58 | |
59 static SkPMColor bit_to_pmcolor(unsigned bit) { return bit ? WHITE : BLACK; } | |
scroggo
2015/03/26 17:48:04
Why do all these take an unsigned as a parameter?
hal.canary
2015/03/26 19:46:52
Will a U8CPU be okay?
scroggo
2015/03/26 20:29:44
It seems better than unsigned. But why do we use u
| |
60 | |
61 static uint8_t bit_to_bit(unsigned bit) { return bit; } | |
62 | |
63 static uint8_t bit_to_grayscale(unsigned bit) { | |
64 return bit ? GRAYSCALE_WHITE : GRAYSCALE_BLACK; | |
65 } | |
66 | |
67 static uint16_t bit_to_rgb565(unsigned 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 (multiple output types) to | |
scroggo
2015/03/26 17:48:03
It's not clear to me what functionality you want t
hal.canary
2015/03/26 19:46:52
grayscale and indexed output
| |
74 // SkSwizzler and use it there. | |
75 template <typename T, T (*TRANSFORM)(unsigned)> | |
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 unsigned 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 unsigned mask = *src; | |
89 do { | |
90 *dst++ = TRANSFORM((mask >> 7) & 1); | |
91 mask <<= 1; | |
92 } while (--bits != 0); | |
93 } | |
94 } | |
95 | |
96 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) | |
97 : SkCodec(info, stream) {} | |
scroggo
2015/03/26 17:48:03
nit: INHERITED.
hal.canary
2015/03/26 19:46:52
Done.
| |
98 | |
99 SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { | |
100 return kWBMP_SkEncodedFormat; | |
101 } | |
102 | |
103 SkImageGenerator::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, | |
104 void* pixels, | |
scroggo
2015/03/26 17:48:03
nit: Should line up with first parameter.
hal.canary
2015/03/26 19:46:52
Done.
| |
105 size_t rowBytes, | |
106 const Options&, | |
107 SkPMColor ctable[], | |
108 int* ctableCount) { | |
109 bool didTryToRewind = false; | |
110 if (!this->rewindIfNeeded(&didTryToRewind)) { | |
scroggo
2015/03/26 17:48:03
Maybe this should return a tri state?
kCouldNotRe
hal.canary
2015/03/26 19:46:52
Done.
| |
111 return SkImageGenerator::kCouldNotRewind; | |
112 } | |
113 if (didTryToRewind) { | |
114 (void)read_header(this->stream(), NULL); | |
115 } | |
116 if (info.dimensions() != this->getInfo().dimensions()) { | |
117 return SkImageGenerator::kInvalidScale; | |
118 } | |
119 ExpandProc proc = NULL; | |
120 switch (info.colorType()) { | |
121 case kGray_8_SkColorType: | |
122 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>; | |
123 break; | |
124 case kN32_SkColorType: | |
125 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>; | |
126 break; | |
127 case kIndex_8_SkColorType: | |
128 ctable[0] = BLACK; | |
129 ctable[1] = WHITE; | |
130 *ctableCount = 2; | |
131 proc = expand_bits_to_T<uint8_t, bit_to_bit>; | |
132 break; | |
133 case kRGB_565_SkColorType: | |
134 proc = expand_bits_to_T<uint16_t, bit_to_rgb565>; | |
135 break; | |
136 default: | |
137 return SkImageGenerator::kInvalidConversion; | |
138 } | |
139 SkISize size = info.dimensions(); | |
140 uint8_t* dst = static_cast<uint8_t*>(pixels); | |
141 size_t srcRowBytes = SkAlign8(size.width()) >> 3; | |
142 SkAutoTMalloc<uint8_t> src(srcRowBytes); | |
143 for (int y = 0; y < size.height(); ++y) { | |
144 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { | |
145 return SkImageGenerator::kIncompleteInput; | |
146 } | |
147 proc(dst, src.get(), size.width()); | |
148 dst += rowBytes; | |
149 } | |
150 return SkImageGenerator::kSuccess; | |
151 } | |
152 | |
153 bool SkWbmpCodec::IsWbmp(SkStream* stream) { | |
154 return read_header(stream, NULL); | |
155 } | |
156 | |
157 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { | |
158 SkISize size; | |
159 if (!read_header(stream, &size)) { | |
160 return NULL; | |
161 } | |
162 SkImageInfo info = | |
163 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType, | |
164 kOpaque_SkAlphaType); | |
165 return SkNEW_ARGS(SkWbmpCodec, (info, stream)); | |
166 } | |
OLD | NEW |