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/SkWbmpDecoder.h"
12#include "include/codec/SkEncodedImageFormat.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
27using 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.
33static inline size_t get_src_row_bytes(int width) {
34 return SkAlign8(x: width) >> 3;
35}
36
37static 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
51static bool read_byte(SkStream* stream, uint8_t* data)
52{
53 return stream->read(buffer: data, size: 1) == 1;
54}
55
56// http://en.wikipedia.org/wiki/Variable-length_quantity
57static 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(buffer: &data, size: 1) != 1) {
67 return false;
68 }
69 n = (n << 7) | (data & 0x7F);
70 } while (data & 0x80);
71 *value = n;
72 return true;
73}
74
75static bool read_header(SkStream* stream, SkISize* size) {
76 {
77 uint8_t data;
78 if (!read_byte(stream, data: &data) || data != 0) { // unknown type
79 return false;
80 }
81 if (!read_byte(stream, data: &data) || (data & 0x9F)) { // skip fixed header
82 return false;
83 }
84 }
85
86 uint64_t width, height;
87 if (!read_mbf(stream, value: &width) || width > 0xFFFF || !width) {
88 return false;
89 }
90 if (!read_mbf(stream, value: &height) || height > 0xFFFF || !height) {
91 return false;
92 }
93 if (size) {
94 *size = SkISize::Make(w: SkToS32(x: width), h: SkToS32(x: height));
95 }
96 return true;
97}
98
99bool SkWbmpCodec::onRewind() {
100 return read_header(stream: this->stream(), size: nullptr);
101}
102
103bool SkWbmpCodec::readRow(uint8_t* row) {
104 return this->stream()->read(buffer: row, size: fSrcRowBytes) == fSrcRowBytes;
105}
106
107SkWbmpCodec::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(width: this->dimensions().width()))
112 , fSwizzler(nullptr)
113{}
114
115SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
116 return SkEncodedImageFormat::kWBMP;
117}
118
119bool SkWbmpCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque,
120 bool /*needsColorXform*/) {
121 return valid_color_type(dstInfo: dst) && valid_alpha(dstAlpha: dst.alphaType(), srcIsOpaque);
122}
123
124SkCodec::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(encodedInfo: this->getEncodedInfo(), ctable: nullptr, dstInfo: 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(row: src.get())) {
145 *rowsDecoded = y;
146 return kIncompleteInput;
147 }
148 swizzler->swizzle(dst: dstRow, src: src.get());
149 dstRow = SkTAddOffset<void>(ptr: dstRow, byteOffset: rowBytes);
150 }
151 return kSuccess;
152}
153
154bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
155 SkMemoryStream stream(buffer, bytesRead, false);
156 return read_header(stream: &stream, size: nullptr);
157}
158
159std::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: stream.get(), size: &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(width: size.width(), height: size.height(), color: SkEncodedInfo::kGray_Color,
175 alpha: SkEncodedInfo::kOpaque_Alpha, bitsPerComponent: 1);
176 return std::unique_ptr<SkCodec>(new SkWbmpCodec(std::move(info), std::move(stream)));
177}
178
179int 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(row: fSrcBuffer.get())) {
183 return y;
184 }
185 fSwizzler->swizzle(dst: dstRow, src: fSrcBuffer.get());
186 dstRow = SkTAddOffset<void>(ptr: dstRow, byteOffset: dstRowBytes);
187 }
188 return count;
189}
190
191bool SkWbmpCodec::onSkipScanlines(int count) {
192 const size_t bytesToSkip = count * fSrcRowBytes;
193 return this->stream()->skip(size: bytesToSkip) == bytesToSkip;
194}
195
196SkCodec::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(encodedInfo: this->getEncodedInfo(), ctable: nullptr, dstInfo, options);
204 SkASSERT(fSwizzler);
205
206 fSrcBuffer.reset(count: fSrcRowBytes);
207
208 return kSuccess;
209}
210
211namespace SkWbmpDecoder {
212bool IsWbmp(const void* data, size_t len) {
213 return SkWbmpCodec::IsWbmp(buffer: data, bytesRead: len);
214}
215
216std::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(stream: std::move(stream), result: outResult);
224}
225
226std::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(stream: SkMemoryStream::Make(data: std::move(data)), outResult, nullptr);
236}
237} // namespace SkWbmpDecoder
238

source code of flutter_engine/third_party/skia/src/codec/SkWbmpCodec.cpp