blob: 8d4beb12cb772e384c02b39154f6c7094253b074 [file] [log] [blame]
halcanarya096d7a2015-03-27 12:16:53 -07001/*
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
Hal Canaryc640d0d2018-06-13 09:59:02 -04008#include "SkWbmpCodec.h"
9
halcanarya096d7a2015-03-27 12:16:53 -070010#include "SkCodec.h"
msarett99f567e2015-08-05 12:58:26 -070011#include "SkCodecPriv.h"
Cary Clarka4083c92017-09-15 11:59:23 -040012#include "SkColorData.h"
msarett99f567e2015-08-05 12:58:26 -070013#include "SkColorTable.h"
scroggodb30be22015-12-08 18:54:13 -080014#include "SkData.h"
halcanarya096d7a2015-03-27 12:16:53 -070015#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040016#include "SkTo.h"
halcanarya096d7a2015-03-27 12:16:53 -070017
msarett99f567e2015-08-05 12:58:26 -070018// Each bit represents a pixel, so width is actually a number of bits.
19// A row will always be stored in bytes, so we round width up to the
20// nearest multiple of 8 to get the number of bits actually in the row.
21// We then divide by 8 to convert to bytes.
22static inline size_t get_src_row_bytes(int width) {
23 return SkAlign8(width) >> 3;
24}
25
Matt Saretta225e9b2016-11-07 10:26:17 -050026static inline bool valid_color_type(const SkImageInfo& dstInfo) {
27 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -070028 case kRGBA_8888_SkColorType:
29 case kBGRA_8888_SkColorType:
msarettb30d6982016-02-15 10:18:45 -080030 case kGray_8_SkColorType:
31 case kRGB_565_SkColorType:
scroggoba584892016-05-20 13:56:13 -070032 return true;
Matt Saretta225e9b2016-11-07 10:26:17 -050033 case kRGBA_F16_SkColorType:
Mike Kleince4cf722018-05-10 11:29:15 -040034 return dstInfo.colorSpace();
msarettb30d6982016-02-15 10:18:45 -080035 default:
36 return false;
37 }
38}
39
scroggob9a1e342015-11-30 06:25:31 -080040static bool read_byte(SkStream* stream, uint8_t* data)
41{
42 return stream->read(data, 1) == 1;
43}
44
halcanarya096d7a2015-03-27 12:16:53 -070045// http://en.wikipedia.org/wiki/Variable-length_quantity
46static bool read_mbf(SkStream* stream, uint64_t* value) {
47 uint64_t n = 0;
48 uint8_t data;
49 const uint64_t kLimit = 0xFE00000000000000;
50 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
51 do {
52 if (n & kLimit) { // Will overflow on shift by 7.
53 return false;
54 }
55 if (stream->read(&data, 1) != 1) {
56 return false;
57 }
58 n = (n << 7) | (data & 0x7F);
59 } while (data & 0x80);
60 *value = n;
61 return true;
62}
63
64static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080065 {
66 uint8_t data;
67 if (!read_byte(stream, &data) || data != 0) { // unknown type
68 return false;
69 }
70 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
71 return false;
72 }
halcanarya096d7a2015-03-27 12:16:53 -070073 }
scroggob9a1e342015-11-30 06:25:31 -080074
75 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070076 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
77 return false;
78 }
msarett99f567e2015-08-05 12:58:26 -070079 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070080 return false;
81 }
82 if (size) {
83 *size = SkISize::Make(SkToS32(width), SkToS32(height));
84 }
85 return true;
86}
87
scroggob427db12015-08-12 07:24:13 -070088bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070089 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070090}
91
Leon Scroggins571b30f2017-07-11 17:35:31 +000092SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const Options& opts) {
93 return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), nullptr, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -070094}
95
msarette6dd0042015-10-09 11:07:34 -070096bool SkWbmpCodec::readRow(uint8_t* row) {
97 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -070098}
99
Mike Reedede7bac2017-07-23 15:30:02 -0400100SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info,
101 std::unique_ptr<SkStream> stream)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400102 // Wbmp does not need a colorXform, so choose an arbitrary srcFormat.
103 : INHERITED(width, height, info, SkColorSpaceXform::ColorFormat(),
Mike Reedede7bac2017-07-23 15:30:02 -0400104 std::move(stream), SkColorSpace::MakeSRGB())
msarett99f567e2015-08-05 12:58:26 -0700105 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -0700106 , fSwizzler(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700107{}
halcanarya096d7a2015-03-27 12:16:53 -0700108
Hal Canarydb683012016-11-23 08:55:18 -0700109SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
110 return SkEncodedImageFormat::kWBMP;
halcanarya096d7a2015-03-27 12:16:53 -0700111}
112
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500113bool SkWbmpCodec::conversionSupported(const SkImageInfo& dst, SkColorType /*srcColor*/,
Leon Scroggins III07418182017-08-15 12:24:02 -0400114 bool srcIsOpaque, const SkColorSpace* srcCS) const {
115 return valid_color_type(dst) && valid_alpha(dst.alphaType(), srcIsOpaque);
116}
117
scroggoeb602a52015-07-09 08:16:03 -0700118SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700119 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700120 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700121 const Options& options,
msarette6dd0042015-10-09 11:07:34 -0700122 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700123 if (options.fSubset) {
124 // Subsets are not supported.
125 return kUnimplemented;
126 }
msarett99f567e2015-08-05 12:58:26 -0700127
msarett99f567e2015-08-05 12:58:26 -0700128 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000129 std::unique_ptr<SkSwizzler> swizzler(this->initializeSwizzler(info, options));
msarettb30d6982016-02-15 10:18:45 -0800130 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700131
132 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700133 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700134 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
135 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700136 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700137 if (!this->readRow(src.get())) {
138 *rowsDecoded = y;
139 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700140 }
msarett99f567e2015-08-05 12:58:26 -0700141 swizzler->swizzle(dstRow, src.get());
142 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700143 }
scroggoeb602a52015-07-09 08:16:03 -0700144 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700145}
146
scroggodb30be22015-12-08 18:54:13 -0800147bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
bungeman38d909e2016-08-02 14:40:46 -0700148 SkMemoryStream stream(buffer, bytesRead, false);
scroggodb30be22015-12-08 18:54:13 -0800149 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700150}
151
Mike Reedede7bac2017-07-23 15:30:02 -0400152std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
153 Result* result) {
halcanarya096d7a2015-03-27 12:16:53 -0700154 SkISize size;
Mike Reedede7bac2017-07-23 15:30:02 -0400155 if (!read_header(stream.get(), &size)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400156 // This already succeeded in IsWbmp, so this stream was corrupted in/
157 // after rewind.
158 *result = kCouldNotRewind;
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700160 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400161 *result = kSuccess;
msarettc30c4182016-04-20 11:53:35 -0700162 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
163 SkEncodedInfo::kOpaque_Alpha, 1);
Mike Reedede7bac2017-07-23 15:30:02 -0400164 return std::unique_ptr<SkCodec>(new SkWbmpCodec(size.width(), size.height(), info,
165 std::move(stream)));
halcanarya096d7a2015-03-27 12:16:53 -0700166}
msarett99f567e2015-08-05 12:58:26 -0700167
msarette6dd0042015-10-09 11:07:34 -0700168int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700169 void* dstRow = dst;
170 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700171 if (!this->readRow(fSrcBuffer.get())) {
172 return y;
msarett99f567e2015-08-05 12:58:26 -0700173 }
scroggo46c57472015-09-30 08:57:13 -0700174 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
175 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700176 }
msarette6dd0042015-10-09 11:07:34 -0700177 return count;
msarett99f567e2015-08-05 12:58:26 -0700178}
scroggo46c57472015-09-30 08:57:13 -0700179
msarett9b9497e2016-02-11 13:29:36 -0800180bool SkWbmpCodec::onSkipScanlines(int count) {
181 const size_t bytesToSkip = count * fSrcRowBytes;
182 return this->stream()->skip(bytesToSkip) == bytesToSkip;
183}
184
scroggo46c57472015-09-30 08:57:13 -0700185SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000186 const Options& options) {
scroggo46c57472015-09-30 08:57:13 -0700187 if (options.fSubset) {
188 // Subsets are not supported.
189 return kUnimplemented;
190 }
scroggo46c57472015-09-30 08:57:13 -0700191
scroggo46c57472015-09-30 08:57:13 -0700192 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000193 fSwizzler.reset(this->initializeSwizzler(dstInfo, options));
msarettb30d6982016-02-15 10:18:45 -0800194 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700195
196 fSrcBuffer.reset(fSrcRowBytes);
197
198 return kSuccess;
199}