blob: fe9a29133ec5260c346264a59c27bf45a08b2b70 [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 kIndex_8_SkColorType:
msarettb30d6982016-02-15 10:18:45 -080029 case kGray_8_SkColorType:
30 case kRGB_565_SkColorType:
scroggoba584892016-05-20 13:56:13 -070031 return true;
Matt Saretta225e9b2016-11-07 10:26:17 -050032 case kRGBA_F16_SkColorType:
33 return dstInfo.colorSpace() && dstInfo.colorSpace()->gammaIsLinear();
msarettb30d6982016-02-15 10:18:45 -080034 default:
35 return false;
36 }
37}
38
scroggob9a1e342015-11-30 06:25:31 -080039static bool read_byte(SkStream* stream, uint8_t* data)
40{
41 return stream->read(data, 1) == 1;
42}
43
halcanarya096d7a2015-03-27 12:16:53 -070044// http://en.wikipedia.org/wiki/Variable-length_quantity
45static bool read_mbf(SkStream* stream, uint64_t* value) {
46 uint64_t n = 0;
47 uint8_t data;
48 const uint64_t kLimit = 0xFE00000000000000;
49 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
50 do {
51 if (n & kLimit) { // Will overflow on shift by 7.
52 return false;
53 }
54 if (stream->read(&data, 1) != 1) {
55 return false;
56 }
57 n = (n << 7) | (data & 0x7F);
58 } while (data & 0x80);
59 *value = n;
60 return true;
61}
62
63static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080064 {
65 uint8_t data;
66 if (!read_byte(stream, &data) || data != 0) { // unknown type
67 return false;
68 }
69 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
70 return false;
71 }
halcanarya096d7a2015-03-27 12:16:53 -070072 }
scroggob9a1e342015-11-30 06:25:31 -080073
74 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070075 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
76 return false;
77 }
msarett99f567e2015-08-05 12:58:26 -070078 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070079 return false;
80 }
81 if (size) {
82 *size = SkISize::Make(SkToS32(width), SkToS32(height));
83 }
84 return true;
85}
86
scroggob427db12015-08-12 07:24:13 -070087bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070088 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070089}
90
Leon Scroggins571b30f2017-07-11 17:35:31 +000091SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const Options& opts) {
92 return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), nullptr, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -070093}
94
msarette6dd0042015-10-09 11:07:34 -070095bool SkWbmpCodec::readRow(uint8_t* row) {
96 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -070097}
98
msarettc30c4182016-04-20 11:53:35 -070099SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info, 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(),
102 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
149SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400150 std::unique_ptr<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700151 SkISize size;
152 if (!read_header(stream, &size)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700153 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700154 }
msarettc30c4182016-04-20 11:53:35 -0700155 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
156 SkEncodedInfo::kOpaque_Alpha, 1);
157 return new SkWbmpCodec(size.width(), size.height(), info, streamDeleter.release());
halcanarya096d7a2015-03-27 12:16:53 -0700158}
msarett99f567e2015-08-05 12:58:26 -0700159
msarette6dd0042015-10-09 11:07:34 -0700160int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700161 void* dstRow = dst;
162 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700163 if (!this->readRow(fSrcBuffer.get())) {
164 return y;
msarett99f567e2015-08-05 12:58:26 -0700165 }
scroggo46c57472015-09-30 08:57:13 -0700166 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
167 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700168 }
msarette6dd0042015-10-09 11:07:34 -0700169 return count;
msarett99f567e2015-08-05 12:58:26 -0700170}
scroggo46c57472015-09-30 08:57:13 -0700171
msarett9b9497e2016-02-11 13:29:36 -0800172bool SkWbmpCodec::onSkipScanlines(int count) {
173 const size_t bytesToSkip = count * fSrcRowBytes;
174 return this->stream()->skip(bytesToSkip) == bytesToSkip;
175}
176
scroggo46c57472015-09-30 08:57:13 -0700177SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000178 const Options& options) {
scroggo46c57472015-09-30 08:57:13 -0700179 if (options.fSubset) {
180 // Subsets are not supported.
181 return kUnimplemented;
182 }
scroggo46c57472015-09-30 08:57:13 -0700183
Matt Saretta225e9b2016-11-07 10:26:17 -0500184 if (!valid_color_type(dstInfo) ||
185 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType()))
186 {
scroggo46c57472015-09-30 08:57:13 -0700187 return kInvalidConversion;
188 }
189
scroggo46c57472015-09-30 08:57:13 -0700190 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000191 fSwizzler.reset(this->initializeSwizzler(dstInfo, options));
msarettb30d6982016-02-15 10:18:45 -0800192 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700193
194 fSrcBuffer.reset(fSrcRowBytes);
195
196 return kSuccess;
197}