blob: 706d5ddd460e4dc7b6764512e0de776f4da0fe41 [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
msarettb30d6982016-02-15 10:18:45 -080033static inline bool valid_color_type(SkColorType colorType, SkAlphaType alphaType) {
34 switch (colorType) {
35 case kN32_SkColorType:
36 case kIndex_8_SkColorType:
37 return true;
38 case kGray_8_SkColorType:
39 case kRGB_565_SkColorType:
40 return kOpaque_SkAlphaType == alphaType;
41 default:
42 return false;
43 }
44}
45
scroggob9a1e342015-11-30 06:25:31 -080046static bool read_byte(SkStream* stream, uint8_t* data)
47{
48 return stream->read(data, 1) == 1;
49}
50
halcanarya096d7a2015-03-27 12:16:53 -070051// http://en.wikipedia.org/wiki/Variable-length_quantity
52static bool read_mbf(SkStream* stream, uint64_t* value) {
53 uint64_t n = 0;
54 uint8_t data;
55 const uint64_t kLimit = 0xFE00000000000000;
56 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
57 do {
58 if (n & kLimit) { // Will overflow on shift by 7.
59 return false;
60 }
61 if (stream->read(&data, 1) != 1) {
62 return false;
63 }
64 n = (n << 7) | (data & 0x7F);
65 } while (data & 0x80);
66 *value = n;
67 return true;
68}
69
70static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080071 {
72 uint8_t data;
73 if (!read_byte(stream, &data) || data != 0) { // unknown type
74 return false;
75 }
76 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
77 return false;
78 }
halcanarya096d7a2015-03-27 12:16:53 -070079 }
scroggob9a1e342015-11-30 06:25:31 -080080
81 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070082 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
83 return false;
84 }
msarett99f567e2015-08-05 12:58:26 -070085 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070086 return false;
87 }
88 if (size) {
89 *size = SkISize::Make(SkToS32(width), SkToS32(height));
90 }
91 return true;
92}
93
scroggob427db12015-08-12 07:24:13 -070094bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070095 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070096}
97
msarettfdb47572015-10-13 12:50:14 -070098SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
99 const Options& opts) {
msarettfdb47572015-10-13 12:50:14 -0700100 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -0700101}
102
msarette6dd0042015-10-09 11:07:34 -0700103bool SkWbmpCodec::readRow(uint8_t* row) {
104 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -0700105}
106
halcanarya096d7a2015-03-27 12:16:53 -0700107SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
msarett99f567e2015-08-05 12:58:26 -0700108 : INHERITED(info, stream)
109 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -0700110 , fSwizzler(nullptr)
msarettf724b992015-10-15 06:41:06 -0700111 , fColorTable(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700112{}
halcanarya096d7a2015-03-27 12:16:53 -0700113
114SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
115 return kWBMP_SkEncodedFormat;
116}
117
scroggoeb602a52015-07-09 08:16:03 -0700118SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700119 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700120 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700121 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700122 SkPMColor ctable[],
msarette6dd0042015-10-09 11:07:34 -0700123 int* ctableCount,
124 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700125 if (options.fSubset) {
126 // Subsets are not supported.
127 return kUnimplemented;
128 }
msarett99f567e2015-08-05 12:58:26 -0700129
msarettb30d6982016-02-15 10:18:45 -0800130 if (!valid_color_type(info.colorType(), info.alphaType()) ||
131 !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700132 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700133 }
134
msarett99f567e2015-08-05 12:58:26 -0700135 // Prepare a color table if necessary
136 setup_color_table(info.colorType(), ctable, ctableCount);
137
msarett99f567e2015-08-05 12:58:26 -0700138 // Initialize the swizzler
139 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
msarettb30d6982016-02-15 10:18:45 -0800140 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700141
142 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700143 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700144 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
145 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700146 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700147 if (!this->readRow(src.get())) {
148 *rowsDecoded = y;
149 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700150 }
msarett99f567e2015-08-05 12:58:26 -0700151 swizzler->swizzle(dstRow, src.get());
152 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700153 }
scroggoeb602a52015-07-09 08:16:03 -0700154 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700155}
156
scroggodb30be22015-12-08 18:54:13 -0800157bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
158 SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(buffer, bytesRead));
159 SkMemoryStream stream(data);
160 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700161}
162
163SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
scroggo0a7e69c2015-04-03 07:22:22 -0700164 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700165 SkISize size;
166 if (!read_header(stream, &size)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700168 }
msarett99f567e2015-08-05 12:58:26 -0700169 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
170 kGray_8_SkColorType, kOpaque_SkAlphaType);
halcanary385fe4d2015-08-26 13:07:48 -0700171 return new SkWbmpCodec(info, streamDeleter.detach());
halcanarya096d7a2015-03-27 12:16:53 -0700172}
msarett99f567e2015-08-05 12:58:26 -0700173
msarette6dd0042015-10-09 11:07:34 -0700174int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700175 void* dstRow = dst;
176 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700177 if (!this->readRow(fSrcBuffer.get())) {
178 return y;
msarett99f567e2015-08-05 12:58:26 -0700179 }
scroggo46c57472015-09-30 08:57:13 -0700180 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
181 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700182 }
msarette6dd0042015-10-09 11:07:34 -0700183 return count;
msarett99f567e2015-08-05 12:58:26 -0700184}
scroggo46c57472015-09-30 08:57:13 -0700185
msarett9b9497e2016-02-11 13:29:36 -0800186bool SkWbmpCodec::onSkipScanlines(int count) {
187 const size_t bytesToSkip = count * fSrcRowBytes;
188 return this->stream()->skip(bytesToSkip) == bytesToSkip;
189}
190
scroggo46c57472015-09-30 08:57:13 -0700191SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
192 const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
scroggo46c57472015-09-30 08:57:13 -0700193 if (options.fSubset) {
194 // Subsets are not supported.
195 return kUnimplemented;
196 }
scroggo46c57472015-09-30 08:57:13 -0700197
msarettb30d6982016-02-15 10:18:45 -0800198 if (!valid_color_type(dstInfo.colorType(), dstInfo.alphaType()) ||
199 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700200 return kInvalidConversion;
201 }
202
203 // Fill in the color table
204 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
205
206 // Copy the color table to a pointer that can be owned by the scanline decoder
207 if (kIndex_8_SkColorType == dstInfo.colorType()) {
208 fColorTable.reset(new SkColorTable(inputColorTable, 2));
209 }
210
211 // Initialize the swizzler
msarettfdb47572015-10-13 12:50:14 -0700212 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options));
msarettb30d6982016-02-15 10:18:45 -0800213 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700214
215 fSrcBuffer.reset(fSrcRowBytes);
216
217 return kSuccess;
218}