blob: 9a0ed6ee7213d3ed37a1367c95fb2bb8f28ab05f [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:
Mike Reed58050132017-07-12 22:10:29 -040028#ifdef SK_SUPPORT_LEGACY_INDEX_8_COLORTYPE
msarettb30d6982016-02-15 10:18:45 -080029 case kIndex_8_SkColorType:
Mike Reed58050132017-07-12 22:10:29 -040030#endif
msarettb30d6982016-02-15 10:18:45 -080031 case kGray_8_SkColorType:
32 case kRGB_565_SkColorType:
scroggoba584892016-05-20 13:56:13 -070033 return true;
Matt Saretta225e9b2016-11-07 10:26:17 -050034 case kRGBA_F16_SkColorType:
35 return dstInfo.colorSpace() && dstInfo.colorSpace()->gammaIsLinear();
msarettb30d6982016-02-15 10:18:45 -080036 default:
37 return false;
38 }
39}
40
scroggob9a1e342015-11-30 06:25:31 -080041static bool read_byte(SkStream* stream, uint8_t* data)
42{
43 return stream->read(data, 1) == 1;
44}
45
halcanarya096d7a2015-03-27 12:16:53 -070046// http://en.wikipedia.org/wiki/Variable-length_quantity
47static bool read_mbf(SkStream* stream, uint64_t* value) {
48 uint64_t n = 0;
49 uint8_t data;
50 const uint64_t kLimit = 0xFE00000000000000;
51 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
52 do {
53 if (n & kLimit) { // Will overflow on shift by 7.
54 return false;
55 }
56 if (stream->read(&data, 1) != 1) {
57 return false;
58 }
59 n = (n << 7) | (data & 0x7F);
60 } while (data & 0x80);
61 *value = n;
62 return true;
63}
64
65static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080066 {
67 uint8_t data;
68 if (!read_byte(stream, &data) || data != 0) { // unknown type
69 return false;
70 }
71 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
72 return false;
73 }
halcanarya096d7a2015-03-27 12:16:53 -070074 }
scroggob9a1e342015-11-30 06:25:31 -080075
76 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070077 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
78 return false;
79 }
msarett99f567e2015-08-05 12:58:26 -070080 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070081 return false;
82 }
83 if (size) {
84 *size = SkISize::Make(SkToS32(width), SkToS32(height));
85 }
86 return true;
87}
88
scroggob427db12015-08-12 07:24:13 -070089bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070090 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070091}
92
Leon Scroggins571b30f2017-07-11 17:35:31 +000093SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const Options& opts) {
94 return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), nullptr, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -070095}
96
msarette6dd0042015-10-09 11:07:34 -070097bool SkWbmpCodec::readRow(uint8_t* row) {
98 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -070099}
100
msarettc30c4182016-04-20 11:53:35 -0700101SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info, 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(),
104 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
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
Matt Saretta225e9b2016-11-07 10:26:17 -0500123 if (!valid_color_type(info) || !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700124 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700125 }
126
msarett99f567e2015-08-05 12:58:26 -0700127 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000128 std::unique_ptr<SkSwizzler> swizzler(this->initializeSwizzler(info, options));
msarettb30d6982016-02-15 10:18:45 -0800129 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700130
131 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700132 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700133 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
134 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700135 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700136 if (!this->readRow(src.get())) {
137 *rowsDecoded = y;
138 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700139 }
msarett99f567e2015-08-05 12:58:26 -0700140 swizzler->swizzle(dstRow, src.get());
141 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700142 }
scroggoeb602a52015-07-09 08:16:03 -0700143 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700144}
145
scroggodb30be22015-12-08 18:54:13 -0800146bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
bungeman38d909e2016-08-02 14:40:46 -0700147 SkMemoryStream stream(buffer, bytesRead, false);
scroggodb30be22015-12-08 18:54:13 -0800148 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700149}
150
Leon Scroggins III588fb042017-07-14 16:32:31 -0400151SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream, Result* result) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400152 std::unique_ptr<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700153 SkISize size;
154 if (!read_header(stream, &size)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400155 // This already succeeded in IsWbmp, so this stream was corrupted in/
156 // after rewind.
157 *result = kCouldNotRewind;
halcanary96fcdcc2015-08-27 07:41:13 -0700158 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700159 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400160 *result = kSuccess;
msarettc30c4182016-04-20 11:53:35 -0700161 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
162 SkEncodedInfo::kOpaque_Alpha, 1);
163 return new SkWbmpCodec(size.width(), size.height(), info, streamDeleter.release());
halcanarya096d7a2015-03-27 12:16:53 -0700164}
msarett99f567e2015-08-05 12:58:26 -0700165
msarette6dd0042015-10-09 11:07:34 -0700166int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700167 void* dstRow = dst;
168 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700169 if (!this->readRow(fSrcBuffer.get())) {
170 return y;
msarett99f567e2015-08-05 12:58:26 -0700171 }
scroggo46c57472015-09-30 08:57:13 -0700172 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
173 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700174 }
msarette6dd0042015-10-09 11:07:34 -0700175 return count;
msarett99f567e2015-08-05 12:58:26 -0700176}
scroggo46c57472015-09-30 08:57:13 -0700177
msarett9b9497e2016-02-11 13:29:36 -0800178bool SkWbmpCodec::onSkipScanlines(int count) {
179 const size_t bytesToSkip = count * fSrcRowBytes;
180 return this->stream()->skip(bytesToSkip) == bytesToSkip;
181}
182
scroggo46c57472015-09-30 08:57:13 -0700183SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000184 const Options& options) {
scroggo46c57472015-09-30 08:57:13 -0700185 if (options.fSubset) {
186 // Subsets are not supported.
187 return kUnimplemented;
188 }
scroggo46c57472015-09-30 08:57:13 -0700189
Matt Saretta225e9b2016-11-07 10:26:17 -0500190 if (!valid_color_type(dstInfo) ||
191 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType()))
192 {
scroggo46c57472015-09-30 08:57:13 -0700193 return kInvalidConversion;
194 }
195
scroggo46c57472015-09-30 08:57:13 -0700196 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000197 fSwizzler.reset(this->initializeSwizzler(dstInfo, options));
msarettb30d6982016-02-15 10:18:45 -0800198 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700199
200 fSrcBuffer.reset(fSrcRowBytes);
201
202 return kSuccess;
203}