blob: 780ae5e5d0b96feb6fc1aeb6cc7e5144401ece35 [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
24static inline void setup_color_table(SkColorType colorType,
25 SkPMColor* colorPtr, int* colorCount) {
26 if (kIndex_8_SkColorType == colorType) {
27 colorPtr[0] = SK_ColorBLACK;
28 colorPtr[1] = SK_ColorWHITE;
29 *colorCount = 2;
30 }
31}
32
Matt Saretta225e9b2016-11-07 10:26:17 -050033static inline bool valid_color_type(const SkImageInfo& dstInfo) {
34 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -070035 case kRGBA_8888_SkColorType:
36 case kBGRA_8888_SkColorType:
msarettb30d6982016-02-15 10:18:45 -080037 case kIndex_8_SkColorType:
msarettb30d6982016-02-15 10:18:45 -080038 case kGray_8_SkColorType:
39 case kRGB_565_SkColorType:
scroggoba584892016-05-20 13:56:13 -070040 return true;
Matt Saretta225e9b2016-11-07 10:26:17 -050041 case kRGBA_F16_SkColorType:
42 return dstInfo.colorSpace() && dstInfo.colorSpace()->gammaIsLinear();
msarettb30d6982016-02-15 10:18:45 -080043 default:
44 return false;
45 }
46}
47
scroggob9a1e342015-11-30 06:25:31 -080048static bool read_byte(SkStream* stream, uint8_t* data)
49{
50 return stream->read(data, 1) == 1;
51}
52
halcanarya096d7a2015-03-27 12:16:53 -070053// http://en.wikipedia.org/wiki/Variable-length_quantity
54static bool read_mbf(SkStream* stream, uint64_t* value) {
55 uint64_t n = 0;
56 uint8_t data;
57 const uint64_t kLimit = 0xFE00000000000000;
58 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
59 do {
60 if (n & kLimit) { // Will overflow on shift by 7.
61 return false;
62 }
63 if (stream->read(&data, 1) != 1) {
64 return false;
65 }
66 n = (n << 7) | (data & 0x7F);
67 } while (data & 0x80);
68 *value = n;
69 return true;
70}
71
72static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080073 {
74 uint8_t data;
75 if (!read_byte(stream, &data) || data != 0) { // unknown type
76 return false;
77 }
78 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
79 return false;
80 }
halcanarya096d7a2015-03-27 12:16:53 -070081 }
scroggob9a1e342015-11-30 06:25:31 -080082
83 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070084 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
85 return false;
86 }
msarett99f567e2015-08-05 12:58:26 -070087 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070088 return false;
89 }
90 if (size) {
91 *size = SkISize::Make(SkToS32(width), SkToS32(height));
92 }
93 return true;
94}
95
scroggob427db12015-08-12 07:24:13 -070096bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070097 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070098}
99
msarettfdb47572015-10-13 12:50:14 -0700100SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
101 const Options& opts) {
msaretta45a6682016-04-22 13:18:37 -0700102 return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), ctable, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -0700103}
104
msarette6dd0042015-10-09 11:07:34 -0700105bool SkWbmpCodec::readRow(uint8_t* row) {
106 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -0700107}
108
msarettc30c4182016-04-20 11:53:35 -0700109SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400110 // Wbmp does not need a colorXform, so choose an arbitrary srcFormat.
111 : INHERITED(width, height, info, SkColorSpaceXform::ColorFormat(),
112 stream, SkColorSpace::MakeSRGB())
msarett99f567e2015-08-05 12:58:26 -0700113 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -0700114 , fSwizzler(nullptr)
msarettf724b992015-10-15 06:41:06 -0700115 , fColorTable(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700116{}
halcanarya096d7a2015-03-27 12:16:53 -0700117
Hal Canarydb683012016-11-23 08:55:18 -0700118SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
119 return SkEncodedImageFormat::kWBMP;
halcanarya096d7a2015-03-27 12:16:53 -0700120}
121
scroggoeb602a52015-07-09 08:16:03 -0700122SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700123 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700124 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700125 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700126 SkPMColor ctable[],
msarette6dd0042015-10-09 11:07:34 -0700127 int* ctableCount,
128 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700129 if (options.fSubset) {
130 // Subsets are not supported.
131 return kUnimplemented;
132 }
msarett99f567e2015-08-05 12:58:26 -0700133
Matt Saretta225e9b2016-11-07 10:26:17 -0500134 if (!valid_color_type(info) || !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700135 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700136 }
137
msarett99f567e2015-08-05 12:58:26 -0700138 // Prepare a color table if necessary
139 setup_color_table(info.colorType(), ctable, ctableCount);
140
msarett99f567e2015-08-05 12:58:26 -0700141 // Initialize the swizzler
Ben Wagner145dbcd2016-11-03 14:40:50 -0400142 std::unique_ptr<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
msarettb30d6982016-02-15 10:18:45 -0800143 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700144
145 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700146 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700147 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
148 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700149 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700150 if (!this->readRow(src.get())) {
151 *rowsDecoded = y;
152 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700153 }
msarett99f567e2015-08-05 12:58:26 -0700154 swizzler->swizzle(dstRow, src.get());
155 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700156 }
scroggoeb602a52015-07-09 08:16:03 -0700157 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700158}
159
scroggodb30be22015-12-08 18:54:13 -0800160bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
bungeman38d909e2016-08-02 14:40:46 -0700161 SkMemoryStream stream(buffer, bytesRead, false);
scroggodb30be22015-12-08 18:54:13 -0800162 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700163}
164
165SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400166 std::unique_ptr<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700167 SkISize size;
168 if (!read_header(stream, &size)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700170 }
msarettc30c4182016-04-20 11:53:35 -0700171 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
172 SkEncodedInfo::kOpaque_Alpha, 1);
173 return new SkWbmpCodec(size.width(), size.height(), info, streamDeleter.release());
halcanarya096d7a2015-03-27 12:16:53 -0700174}
msarett99f567e2015-08-05 12:58:26 -0700175
msarette6dd0042015-10-09 11:07:34 -0700176int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700177 void* dstRow = dst;
178 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700179 if (!this->readRow(fSrcBuffer.get())) {
180 return y;
msarett99f567e2015-08-05 12:58:26 -0700181 }
scroggo46c57472015-09-30 08:57:13 -0700182 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
183 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700184 }
msarette6dd0042015-10-09 11:07:34 -0700185 return count;
msarett99f567e2015-08-05 12:58:26 -0700186}
scroggo46c57472015-09-30 08:57:13 -0700187
msarett9b9497e2016-02-11 13:29:36 -0800188bool SkWbmpCodec::onSkipScanlines(int count) {
189 const size_t bytesToSkip = count * fSrcRowBytes;
190 return this->stream()->skip(bytesToSkip) == bytesToSkip;
191}
192
scroggo46c57472015-09-30 08:57:13 -0700193SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
194 const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
scroggo46c57472015-09-30 08:57:13 -0700195 if (options.fSubset) {
196 // Subsets are not supported.
197 return kUnimplemented;
198 }
scroggo46c57472015-09-30 08:57:13 -0700199
Matt Saretta225e9b2016-11-07 10:26:17 -0500200 if (!valid_color_type(dstInfo) ||
201 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType()))
202 {
scroggo46c57472015-09-30 08:57:13 -0700203 return kInvalidConversion;
204 }
205
206 // Fill in the color table
207 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
208
209 // Copy the color table to a pointer that can be owned by the scanline decoder
210 if (kIndex_8_SkColorType == dstInfo.colorType()) {
211 fColorTable.reset(new SkColorTable(inputColorTable, 2));
212 }
213
214 // Initialize the swizzler
msarettfdb47572015-10-13 12:50:14 -0700215 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options));
msarettb30d6982016-02-15 10:18:45 -0800216 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700217
218 fSrcBuffer.reset(fSrcRowBytes);
219
220 return kSuccess;
221}