blob: af1ab46f730c9375cf111b2914081ddcf88d6bfe [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
scroggob9a1e342015-11-30 06:25:31 -080033static bool read_byte(SkStream* stream, uint8_t* data)
34{
35 return stream->read(data, 1) == 1;
36}
37
halcanarya096d7a2015-03-27 12:16:53 -070038// http://en.wikipedia.org/wiki/Variable-length_quantity
39static bool read_mbf(SkStream* stream, uint64_t* value) {
40 uint64_t n = 0;
41 uint8_t data;
42 const uint64_t kLimit = 0xFE00000000000000;
43 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
44 do {
45 if (n & kLimit) { // Will overflow on shift by 7.
46 return false;
47 }
48 if (stream->read(&data, 1) != 1) {
49 return false;
50 }
51 n = (n << 7) | (data & 0x7F);
52 } while (data & 0x80);
53 *value = n;
54 return true;
55}
56
57static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080058 {
59 uint8_t data;
60 if (!read_byte(stream, &data) || data != 0) { // unknown type
61 return false;
62 }
63 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
64 return false;
65 }
halcanarya096d7a2015-03-27 12:16:53 -070066 }
scroggob9a1e342015-11-30 06:25:31 -080067
68 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070069 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
70 return false;
71 }
msarett99f567e2015-08-05 12:58:26 -070072 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070073 return false;
74 }
75 if (size) {
76 *size = SkISize::Make(SkToS32(width), SkToS32(height));
77 }
78 return true;
79}
80
scroggob427db12015-08-12 07:24:13 -070081bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070082 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070083}
84
msarettfdb47572015-10-13 12:50:14 -070085SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
86 const Options& opts) {
msarett99f567e2015-08-05 12:58:26 -070087 // Create the swizzler based on the desired color type
88 switch (info.colorType()) {
89 case kIndex_8_SkColorType:
90 case kN32_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -070091 case kRGB_565_SkColorType:
msarett99f567e2015-08-05 12:58:26 -070092 case kGray_8_SkColorType:
scroggoe7fc14b2015-10-02 13:14:46 -070093 break;
msarett99f567e2015-08-05 12:58:26 -070094 default:
halcanary96fcdcc2015-08-27 07:41:13 -070095 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -070096 }
msarettfdb47572015-10-13 12:50:14 -070097 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -070098}
99
msarette6dd0042015-10-09 11:07:34 -0700100bool SkWbmpCodec::readRow(uint8_t* row) {
101 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -0700102}
103
halcanarya096d7a2015-03-27 12:16:53 -0700104SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
msarett99f567e2015-08-05 12:58:26 -0700105 : INHERITED(info, stream)
106 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -0700107 , fSwizzler(nullptr)
msarettf724b992015-10-15 06:41:06 -0700108 , fColorTable(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700109{}
halcanarya096d7a2015-03-27 12:16:53 -0700110
111SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
112 return kWBMP_SkEncodedFormat;
113}
114
scroggoeb602a52015-07-09 08:16:03 -0700115SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700116 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700117 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700118 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700119 SkPMColor ctable[],
msarette6dd0042015-10-09 11:07:34 -0700120 int* ctableCount,
121 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700122 if (options.fSubset) {
123 // Subsets are not supported.
124 return kUnimplemented;
125 }
msarett99f567e2015-08-05 12:58:26 -0700126
scroggod1bc5742015-08-12 08:31:44 -0700127 if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700128 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700129 }
130
msarett99f567e2015-08-05 12:58:26 -0700131 // Prepare a color table if necessary
132 setup_color_table(info.colorType(), ctable, ctableCount);
133
msarett99f567e2015-08-05 12:58:26 -0700134 // Initialize the swizzler
135 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
halcanary96fcdcc2015-08-27 07:41:13 -0700136 if (nullptr == swizzler.get()) {
msarett99f567e2015-08-05 12:58:26 -0700137 return kInvalidConversion;
halcanarya096d7a2015-03-27 12:16:53 -0700138 }
msarett99f567e2015-08-05 12:58:26 -0700139
140 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700141 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700142 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
143 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700144 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700145 if (!this->readRow(src.get())) {
146 *rowsDecoded = y;
147 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700148 }
msarett99f567e2015-08-05 12:58:26 -0700149 swizzler->swizzle(dstRow, src.get());
150 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700151 }
scroggoeb602a52015-07-09 08:16:03 -0700152 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700153}
154
scroggodb30be22015-12-08 18:54:13 -0800155bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
156 SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(buffer, bytesRead));
157 SkMemoryStream stream(data);
158 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700159}
160
161SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
scroggo0a7e69c2015-04-03 07:22:22 -0700162 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700163 SkISize size;
164 if (!read_header(stream, &size)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700165 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700166 }
msarett99f567e2015-08-05 12:58:26 -0700167 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
168 kGray_8_SkColorType, kOpaque_SkAlphaType);
halcanary385fe4d2015-08-26 13:07:48 -0700169 return new SkWbmpCodec(info, streamDeleter.detach());
halcanarya096d7a2015-03-27 12:16:53 -0700170}
msarett99f567e2015-08-05 12:58:26 -0700171
msarette6dd0042015-10-09 11:07:34 -0700172int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700173 void* dstRow = dst;
174 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700175 if (!this->readRow(fSrcBuffer.get())) {
176 return y;
msarett99f567e2015-08-05 12:58:26 -0700177 }
scroggo46c57472015-09-30 08:57:13 -0700178 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
179 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700180 }
msarette6dd0042015-10-09 11:07:34 -0700181 return count;
msarett99f567e2015-08-05 12:58:26 -0700182}
scroggo46c57472015-09-30 08:57:13 -0700183
msarett9b9497e2016-02-11 13:29:36 -0800184bool SkWbmpCodec::onSkipScanlines(int count) {
185 const size_t bytesToSkip = count * fSrcRowBytes;
186 return this->stream()->skip(bytesToSkip) == bytesToSkip;
187}
188
scroggo46c57472015-09-30 08:57:13 -0700189SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
190 const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
scroggo46c57472015-09-30 08:57:13 -0700191 if (options.fSubset) {
192 // Subsets are not supported.
193 return kUnimplemented;
194 }
scroggo46c57472015-09-30 08:57:13 -0700195
196 if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
197 return kInvalidConversion;
198 }
199
200 // Fill in the color table
201 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
202
203 // Copy the color table to a pointer that can be owned by the scanline decoder
204 if (kIndex_8_SkColorType == dstInfo.colorType()) {
205 fColorTable.reset(new SkColorTable(inputColorTable, 2));
206 }
207
208 // Initialize the swizzler
msarettfdb47572015-10-13 12:50:14 -0700209 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options));
scroggo46c57472015-09-30 08:57:13 -0700210 if (nullptr == fSwizzler.get()) {
211 return kInvalidConversion;
212 }
213
214 fSrcBuffer.reset(fSrcRowBytes);
215
216 return kSuccess;
217}