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 "src/codec/SkWbmpCodec.h"
9
10 #include "include/codec/SkCodec.h"
11 #include "include/codec/SkEncodedImageFormat.h"
12 #include "include/codec/SkWbmpDecoder.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkData.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkStream.h"
19 #include "include/private/SkEncodedInfo.h"
20 #include "include/private/base/SkAlign.h"
21 #include "include/private/base/SkTo.h"
22 #include "modules/skcms/skcms.h"
23 #include "src/codec/SkCodecPriv.h"
24
25 #include <utility>
26
27 using namespace skia_private;
28
29 // Each bit represents a pixel, so width is actually a number of bits.
30 // A row will always be stored in bytes, so we round width up to the
31 // nearest multiple of 8 to get the number of bits actually in the row.
32 // We then divide by 8 to convert to bytes.
get_src_row_bytes(int width)33 static inline size_t get_src_row_bytes(int width) {
34 return SkAlign8(width) >> 3;
35 }
36
valid_color_type(const SkImageInfo & dstInfo)37 static inline bool valid_color_type(const SkImageInfo& dstInfo) {
38 switch (dstInfo.colorType()) {
39 case kRGBA_8888_SkColorType:
40 case kBGRA_8888_SkColorType:
41 case kGray_8_SkColorType:
42 case kRGB_565_SkColorType:
43 return true;
44 case kRGBA_F16_SkColorType:
45 return dstInfo.colorSpace();
46 default:
47 return false;
48 }
49 }
50
read_byte(SkStream * stream,uint8_t * data)51 static bool read_byte(SkStream* stream, uint8_t* data)
52 {
53 return stream->read(data, 1) == 1;
54 }
55
56 // http://en.wikipedia.org/wiki/Variable-length_quantity
read_mbf(SkStream * stream,uint64_t * value)57 static bool read_mbf(SkStream* stream, uint64_t* value) {
58 uint64_t n = 0;
59 uint8_t data;
60 const uint64_t kLimit = 0xFE00000000000000;
61 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
62 do {
63 if (n & kLimit) { // Will overflow on shift by 7.
64 return false;
65 }
66 if (stream->read(&data, 1) != 1) {
67 return false;
68 }
69 n = (n << 7) | (data & 0x7F);
70 } while (data & 0x80);
71 *value = n;
72 return true;
73 }
74
read_header(SkStream * stream,SkISize * size)75 static bool read_header(SkStream* stream, SkISize* size) {
76 {
77 uint8_t data;
78 if (!read_byte(stream, &data) || data != 0) { // unknown type
79 return false;
80 }
81 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
82 return false;
83 }
84 }
85
86 uint64_t width, height;
87 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
88 return false;
89 }
90 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
91 return false;
92 }
93 if (size) {
94 *size = SkISize::Make(SkToS32(width), SkToS32(height));
95 }
96 return true;
97 }
98
onRewind()99 bool SkWbmpCodec::onRewind() {
100 return read_header(this->stream(), nullptr);
101 }
102
readRow(uint8_t * row)103 bool SkWbmpCodec::readRow(uint8_t* row) {
104 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
105 }
106
SkWbmpCodec(SkEncodedInfo && info,std::unique_ptr<SkStream> stream)107 SkWbmpCodec::SkWbmpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream)
108 // Wbmp does not need a colorXform, so choose an arbitrary srcFormat.
109 : INHERITED(std::move(info), skcms_PixelFormat(),
110 std::move(stream))
111 , fSrcRowBytes(get_src_row_bytes(this->dimensions().width()))
112 , fSwizzler(nullptr)
113 {}
114
onGetEncodedFormat() const115 SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
116 return SkEncodedImageFormat::kWBMP;
117 }
118
conversionSupported(const SkImageInfo & dst,bool srcIsOpaque,bool)119 bool SkWbmpCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque,
120 bool /*needsColorXform*/) {
121 return valid_color_type(dst) && valid_alpha(dst.alphaType(), srcIsOpaque);
122 }
123
onGetPixels(const SkImageInfo & info,void * dst,size_t rowBytes,const Options & options,int * rowsDecoded)124 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
125 void* dst,
126 size_t rowBytes,
127 const Options& options,
128 int* rowsDecoded) {
129 if (options.fSubset) {
130 // Subsets are not supported.
131 return kUnimplemented;
132 }
133
134 // Initialize the swizzler
135 std::unique_ptr<SkSwizzler> swizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, info,
136 options);
137 SkASSERT(swizzler);
138
139 // Perform the decode
140 SkISize size = info.dimensions();
141 AutoTMalloc<uint8_t> src(fSrcRowBytes);
142 void* dstRow = dst;
143 for (int y = 0; y < size.height(); ++y) {
144 if (!this->readRow(src.get())) {
145 *rowsDecoded = y;
146 return kIncompleteInput;
147 }
148 swizzler->swizzle(dstRow, src.get());
149 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
150 }
151 return kSuccess;
152 }
153
IsWbmp(const void * buffer,size_t bytesRead)154 bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
155 SkMemoryStream stream(buffer, bytesRead, false);
156 return read_header(&stream, nullptr);
157 }
158
MakeFromStream(std::unique_ptr<SkStream> stream,Result * result)159 std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
160 Result* result) {
161 SkASSERT(result);
162 if (!stream) {
163 *result = SkCodec::kInvalidInput;
164 return nullptr;
165 }
166 SkISize size;
167 if (!read_header(stream.get(), &size)) {
168 // This already succeeded in IsWbmp, so this stream was corrupted in/
169 // after rewind.
170 *result = kCouldNotRewind;
171 return nullptr;
172 }
173 *result = kSuccess;
174 auto info = SkEncodedInfo::Make(size.width(), size.height(), SkEncodedInfo::kGray_Color,
175 SkEncodedInfo::kOpaque_Alpha, 1);
176 return std::unique_ptr<SkCodec>(new SkWbmpCodec(std::move(info), std::move(stream)));
177 }
178
onGetScanlines(void * dst,int count,size_t dstRowBytes)179 int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
180 void* dstRow = dst;
181 for (int y = 0; y < count; ++y) {
182 if (!this->readRow(fSrcBuffer.get())) {
183 return y;
184 }
185 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
186 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
187 }
188 return count;
189 }
190
onSkipScanlines(int count)191 bool SkWbmpCodec::onSkipScanlines(int count) {
192 const size_t bytesToSkip = count * fSrcRowBytes;
193 return this->stream()->skip(bytesToSkip) == bytesToSkip;
194 }
195
onStartScanlineDecode(const SkImageInfo & dstInfo,const Options & options)196 SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
197 const Options& options) {
198 if (options.fSubset) {
199 // Subsets are not supported.
200 return kUnimplemented;
201 }
202
203 fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, dstInfo, options);
204 SkASSERT(fSwizzler);
205
206 fSrcBuffer.reset(fSrcRowBytes);
207
208 return kSuccess;
209 }
210
211 namespace SkWbmpDecoder {
IsWbmp(const void * data,size_t len)212 bool IsWbmp(const void* data, size_t len) {
213 return SkWbmpCodec::IsWbmp(data, len);
214 }
215
Decode(std::unique_ptr<SkStream> stream,SkCodec::Result * outResult,SkCodecs::DecodeContext)216 std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream,
217 SkCodec::Result* outResult,
218 SkCodecs::DecodeContext) {
219 SkCodec::Result resultStorage;
220 if (!outResult) {
221 outResult = &resultStorage;
222 }
223 return SkWbmpCodec::MakeFromStream(std::move(stream), outResult);
224 }
225
Decode(sk_sp<SkData> data,SkCodec::Result * outResult,SkCodecs::DecodeContext)226 std::unique_ptr<SkCodec> Decode(sk_sp<SkData> data,
227 SkCodec::Result* outResult,
228 SkCodecs::DecodeContext) {
229 if (!data) {
230 if (outResult) {
231 *outResult = SkCodec::kInvalidInput;
232 }
233 return nullptr;
234 }
235 return Decode(SkMemoryStream::Make(std::move(data)), outResult, nullptr);
236 }
237 } // namespace SkWbmpDecoder
238