blob: 934d7d5b9ae2871105e2c62345905914cd5ecd82 [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
8#include "SkCodec.h"
msarett99f567e2015-08-05 12:58:26 -07009#include "SkCodecPriv.h"
halcanarya096d7a2015-03-27 12:16:53 -070010#include "SkColorPriv.h"
msarett99f567e2015-08-05 12:58:26 -070011#include "SkColorTable.h"
scroggodb30be22015-12-08 18:54:13 -080012#include "SkData.h"
halcanarya096d7a2015-03-27 12:16:53 -070013#include "SkStream.h"
msarett1a464672016-01-07 13:17:19 -080014#include "SkWbmpCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070015
msarett99f567e2015-08-05 12:58:26 -070016// Each bit represents a pixel, so width is actually a number of bits.
17// A row will always be stored in bytes, so we round width up to the
18// nearest multiple of 8 to get the number of bits actually in the row.
19// We then divide by 8 to convert to bytes.
20static inline size_t get_src_row_bytes(int width) {
21 return SkAlign8(width) >> 3;
22}
23
Matt Saretta225e9b2016-11-07 10:26:17 -050024static inline bool valid_color_type(const SkImageInfo& dstInfo) {
25 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -070026 case kRGBA_8888_SkColorType:
27 case kBGRA_8888_SkColorType:
msarettb30d6982016-02-15 10:18:45 -080028 case kGray_8_SkColorType:
29 case kRGB_565_SkColorType:
scroggoba584892016-05-20 13:56:13 -070030 return true;
Matt Saretta225e9b2016-11-07 10:26:17 -050031 case kRGBA_F16_SkColorType:
32 return dstInfo.colorSpace() && dstInfo.colorSpace()->gammaIsLinear();
msarettb30d6982016-02-15 10:18:45 -080033 default:
34 return false;
35 }
36}
37
scroggob9a1e342015-11-30 06:25:31 -080038static bool read_byte(SkStream* stream, uint8_t* data)
39{
40 return stream->read(data, 1) == 1;
41}
42
halcanarya096d7a2015-03-27 12:16:53 -070043// http://en.wikipedia.org/wiki/Variable-length_quantity
44static bool read_mbf(SkStream* stream, uint64_t* value) {
45 uint64_t n = 0;
46 uint8_t data;
47 const uint64_t kLimit = 0xFE00000000000000;
48 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
49 do {
50 if (n & kLimit) { // Will overflow on shift by 7.
51 return false;
52 }
53 if (stream->read(&data, 1) != 1) {
54 return false;
55 }
56 n = (n << 7) | (data & 0x7F);
57 } while (data & 0x80);
58 *value = n;
59 return true;
60}
61
62static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080063 {
64 uint8_t data;
65 if (!read_byte(stream, &data) || data != 0) { // unknown type
66 return false;
67 }
68 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
69 return false;
70 }
halcanarya096d7a2015-03-27 12:16:53 -070071 }
scroggob9a1e342015-11-30 06:25:31 -080072
73 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070074 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
75 return false;
76 }
msarett99f567e2015-08-05 12:58:26 -070077 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070078 return false;
79 }
80 if (size) {
81 *size = SkISize::Make(SkToS32(width), SkToS32(height));
82 }
83 return true;
84}
85
scroggob427db12015-08-12 07:24:13 -070086bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070087 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070088}
89
Leon Scroggins571b30f2017-07-11 17:35:31 +000090SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const Options& opts) {
91 return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), nullptr, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -070092}
93
msarette6dd0042015-10-09 11:07:34 -070094bool SkWbmpCodec::readRow(uint8_t* row) {
95 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -070096}
97
Mike Reedede7bac2017-07-23 15:30:02 -040098SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info,
99 std::unique_ptr<SkStream> stream)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400100 // Wbmp does not need a colorXform, so choose an arbitrary srcFormat.
101 : INHERITED(width, height, info, SkColorSpaceXform::ColorFormat(),
Mike Reedede7bac2017-07-23 15:30:02 -0400102 std::move(stream), SkColorSpace::MakeSRGB())
msarett99f567e2015-08-05 12:58:26 -0700103 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -0700104 , fSwizzler(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700105{}
halcanarya096d7a2015-03-27 12:16:53 -0700106
Hal Canarydb683012016-11-23 08:55:18 -0700107SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
108 return SkEncodedImageFormat::kWBMP;
halcanarya096d7a2015-03-27 12:16:53 -0700109}
110
scroggoeb602a52015-07-09 08:16:03 -0700111SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700112 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700113 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700114 const Options& options,
msarette6dd0042015-10-09 11:07:34 -0700115 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700116 if (options.fSubset) {
117 // Subsets are not supported.
118 return kUnimplemented;
119 }
msarett99f567e2015-08-05 12:58:26 -0700120
Matt Saretta225e9b2016-11-07 10:26:17 -0500121 if (!valid_color_type(info) || !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700122 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700123 }
124
msarett99f567e2015-08-05 12:58:26 -0700125 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000126 std::unique_ptr<SkSwizzler> swizzler(this->initializeSwizzler(info, options));
msarettb30d6982016-02-15 10:18:45 -0800127 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700128
129 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700130 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700131 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
132 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700133 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700134 if (!this->readRow(src.get())) {
135 *rowsDecoded = y;
136 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700137 }
msarett99f567e2015-08-05 12:58:26 -0700138 swizzler->swizzle(dstRow, src.get());
139 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700140 }
scroggoeb602a52015-07-09 08:16:03 -0700141 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700142}
143
scroggodb30be22015-12-08 18:54:13 -0800144bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
bungeman38d909e2016-08-02 14:40:46 -0700145 SkMemoryStream stream(buffer, bytesRead, false);
scroggodb30be22015-12-08 18:54:13 -0800146 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700147}
148
Mike Reedede7bac2017-07-23 15:30:02 -0400149std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
150 Result* result) {
halcanarya096d7a2015-03-27 12:16:53 -0700151 SkISize size;
Mike Reedede7bac2017-07-23 15:30:02 -0400152 if (!read_header(stream.get(), &size)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400153 // This already succeeded in IsWbmp, so this stream was corrupted in/
154 // after rewind.
155 *result = kCouldNotRewind;
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700157 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400158 *result = kSuccess;
msarettc30c4182016-04-20 11:53:35 -0700159 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
160 SkEncodedInfo::kOpaque_Alpha, 1);
Mike Reedede7bac2017-07-23 15:30:02 -0400161 return std::unique_ptr<SkCodec>(new SkWbmpCodec(size.width(), size.height(), info,
162 std::move(stream)));
halcanarya096d7a2015-03-27 12:16:53 -0700163}
msarett99f567e2015-08-05 12:58:26 -0700164
msarette6dd0042015-10-09 11:07:34 -0700165int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700166 void* dstRow = dst;
167 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700168 if (!this->readRow(fSrcBuffer.get())) {
169 return y;
msarett99f567e2015-08-05 12:58:26 -0700170 }
scroggo46c57472015-09-30 08:57:13 -0700171 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
172 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700173 }
msarette6dd0042015-10-09 11:07:34 -0700174 return count;
msarett99f567e2015-08-05 12:58:26 -0700175}
scroggo46c57472015-09-30 08:57:13 -0700176
msarett9b9497e2016-02-11 13:29:36 -0800177bool SkWbmpCodec::onSkipScanlines(int count) {
178 const size_t bytesToSkip = count * fSrcRowBytes;
179 return this->stream()->skip(bytesToSkip) == bytesToSkip;
180}
181
scroggo46c57472015-09-30 08:57:13 -0700182SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000183 const Options& options) {
scroggo46c57472015-09-30 08:57:13 -0700184 if (options.fSubset) {
185 // Subsets are not supported.
186 return kUnimplemented;
187 }
scroggo46c57472015-09-30 08:57:13 -0700188
Matt Saretta225e9b2016-11-07 10:26:17 -0500189 if (!valid_color_type(dstInfo) ||
190 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType()))
191 {
scroggo46c57472015-09-30 08:57:13 -0700192 return kInvalidConversion;
193 }
194
scroggo46c57472015-09-30 08:57:13 -0700195 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000196 fSwizzler.reset(this->initializeSwizzler(dstInfo, options));
msarettb30d6982016-02-15 10:18:45 -0800197 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700198
199 fSrcBuffer.reset(fSrcRowBytes);
200
201 return kSuccess;
202}