blob: bc796c4fcc625d5da5a1e5b9a79363e5a552c6f5 [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
Leon Scroggins III07418182017-08-15 12:24:02 -0400111bool SkWbmpCodec::conversionSupported(const SkImageInfo& dst, SkEncodedInfo::Color srcColor,
112 bool srcIsOpaque, const SkColorSpace* srcCS) const {
113 return valid_color_type(dst) && valid_alpha(dst.alphaType(), srcIsOpaque);
114}
115
scroggoeb602a52015-07-09 08:16:03 -0700116SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700117 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700118 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700119 const Options& options,
msarette6dd0042015-10-09 11:07:34 -0700120 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700121 if (options.fSubset) {
122 // Subsets are not supported.
123 return kUnimplemented;
124 }
msarett99f567e2015-08-05 12:58:26 -0700125
msarett99f567e2015-08-05 12:58:26 -0700126 // Initialize the swizzler
Leon Scroggins571b30f2017-07-11 17:35:31 +0000127 std::unique_ptr<SkSwizzler> swizzler(this->initializeSwizzler(info, options));
msarettb30d6982016-02-15 10:18:45 -0800128 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700129
130 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700131 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700132 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
133 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700134 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700135 if (!this->readRow(src.get())) {
136 *rowsDecoded = y;
137 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700138 }
msarett99f567e2015-08-05 12:58:26 -0700139 swizzler->swizzle(dstRow, src.get());
140 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700141 }
scroggoeb602a52015-07-09 08:16:03 -0700142 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700143}
144
scroggodb30be22015-12-08 18:54:13 -0800145bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
bungeman38d909e2016-08-02 14:40:46 -0700146 SkMemoryStream stream(buffer, bytesRead, false);
scroggodb30be22015-12-08 18:54:13 -0800147 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700148}
149
Mike Reedede7bac2017-07-23 15:30:02 -0400150std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
151 Result* result) {
halcanarya096d7a2015-03-27 12:16:53 -0700152 SkISize size;
Mike Reedede7bac2017-07-23 15:30:02 -0400153 if (!read_header(stream.get(), &size)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400154 // This already succeeded in IsWbmp, so this stream was corrupted in/
155 // after rewind.
156 *result = kCouldNotRewind;
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700158 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400159 *result = kSuccess;
msarettc30c4182016-04-20 11:53:35 -0700160 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
161 SkEncodedInfo::kOpaque_Alpha, 1);
Mike Reedede7bac2017-07-23 15:30:02 -0400162 return std::unique_ptr<SkCodec>(new SkWbmpCodec(size.width(), size.height(), info,
163 std::move(stream)));
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
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}