blob: 8190c503221184ad0c96ade1ffab8d48b739c930 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/codec/SkWbmpCodec.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/codec/SkCodec.h"
11#include "include/core/SkData.h"
12#include "include/core/SkStream.h"
13#include "include/private/SkColorData.h"
14#include "include/private/SkTo.h"
15#include "src/codec/SkCodecPriv.h"
16#include "src/codec/SkColorTable.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
msarette6dd0042015-10-09 11:07:34 -070092bool SkWbmpCodec::readRow(uint8_t* row) {
93 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -070094}
95
Leon Scroggins III36f7e322018-08-27 11:55:46 -040096SkWbmpCodec::SkWbmpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -040097 // Wbmp does not need a colorXform, so choose an arbitrary srcFormat.
Leon Scroggins III36f7e322018-08-27 11:55:46 -040098 : INHERITED(std::move(info), skcms_PixelFormat(),
99 std::move(stream))
Leon Scroggins III712476e2018-10-03 15:47:00 -0400100 , fSrcRowBytes(get_src_row_bytes(this->dimensions().width()))
scroggo46c57472015-09-30 08:57:13 -0700101 , fSwizzler(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700102{}
halcanarya096d7a2015-03-27 12:16:53 -0700103
Hal Canarydb683012016-11-23 08:55:18 -0700104SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
105 return SkEncodedImageFormat::kWBMP;
halcanarya096d7a2015-03-27 12:16:53 -0700106}
107
Leon Scroggins III712476e2018-10-03 15:47:00 -0400108bool SkWbmpCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque,
109 bool /*needsColorXform*/) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400110 return valid_color_type(dst) && valid_alpha(dst.alphaType(), srcIsOpaque);
111}
112
scroggoeb602a52015-07-09 08:16:03 -0700113SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700114 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700115 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700116 const Options& options,
msarette6dd0042015-10-09 11:07:34 -0700117 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700118 if (options.fSubset) {
119 // Subsets are not supported.
120 return kUnimplemented;
121 }
msarett99f567e2015-08-05 12:58:26 -0700122
msarett99f567e2015-08-05 12:58:26 -0700123 // Initialize the swizzler
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400124 std::unique_ptr<SkSwizzler> swizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, info,
125 options);
msarettb30d6982016-02-15 10:18:45 -0800126 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700127
128 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700129 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700130 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
131 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700132 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700133 if (!this->readRow(src.get())) {
134 *rowsDecoded = y;
135 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700136 }
msarett99f567e2015-08-05 12:58:26 -0700137 swizzler->swizzle(dstRow, src.get());
138 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700139 }
scroggoeb602a52015-07-09 08:16:03 -0700140 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700141}
142
scroggodb30be22015-12-08 18:54:13 -0800143bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
bungeman38d909e2016-08-02 14:40:46 -0700144 SkMemoryStream stream(buffer, bytesRead, false);
scroggodb30be22015-12-08 18:54:13 -0800145 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700146}
147
Mike Reedede7bac2017-07-23 15:30:02 -0400148std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
149 Result* result) {
halcanarya096d7a2015-03-27 12:16:53 -0700150 SkISize size;
Mike Reedede7bac2017-07-23 15:30:02 -0400151 if (!read_header(stream.get(), &size)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400152 // This already succeeded in IsWbmp, so this stream was corrupted in/
153 // after rewind.
154 *result = kCouldNotRewind;
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700156 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400157 *result = kSuccess;
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400158 auto info = SkEncodedInfo::Make(size.width(), size.height(), SkEncodedInfo::kGray_Color,
159 SkEncodedInfo::kOpaque_Alpha, 1);
160 return std::unique_ptr<SkCodec>(new SkWbmpCodec(std::move(info), std::move(stream)));
halcanarya096d7a2015-03-27 12:16:53 -0700161}
msarett99f567e2015-08-05 12:58:26 -0700162
msarette6dd0042015-10-09 11:07:34 -0700163int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700164 void* dstRow = dst;
165 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700166 if (!this->readRow(fSrcBuffer.get())) {
167 return y;
msarett99f567e2015-08-05 12:58:26 -0700168 }
scroggo46c57472015-09-30 08:57:13 -0700169 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
170 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700171 }
msarette6dd0042015-10-09 11:07:34 -0700172 return count;
msarett99f567e2015-08-05 12:58:26 -0700173}
scroggo46c57472015-09-30 08:57:13 -0700174
msarett9b9497e2016-02-11 13:29:36 -0800175bool SkWbmpCodec::onSkipScanlines(int count) {
176 const size_t bytesToSkip = count * fSrcRowBytes;
177 return this->stream()->skip(bytesToSkip) == bytesToSkip;
178}
179
scroggo46c57472015-09-30 08:57:13 -0700180SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000181 const Options& options) {
scroggo46c57472015-09-30 08:57:13 -0700182 if (options.fSubset) {
183 // Subsets are not supported.
184 return kUnimplemented;
185 }
scroggo46c57472015-09-30 08:57:13 -0700186
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400187 fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, dstInfo, options);
msarettb30d6982016-02-15 10:18:45 -0800188 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700189
190 fSrcBuffer.reset(fSrcRowBytes);
191
192 return kSuccess;
193}