blob: 527565f6ab4484152f78909583b60400cacfccff [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) {
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:
38 return true;
39 case kGray_8_SkColorType:
40 case kRGB_565_SkColorType:
41 return kOpaque_SkAlphaType == alphaType;
42 default:
43 return false;
44 }
45}
46
scroggob9a1e342015-11-30 06:25:31 -080047static bool read_byte(SkStream* stream, uint8_t* data)
48{
49 return stream->read(data, 1) == 1;
50}
51
halcanarya096d7a2015-03-27 12:16:53 -070052// http://en.wikipedia.org/wiki/Variable-length_quantity
53static bool read_mbf(SkStream* stream, uint64_t* value) {
54 uint64_t n = 0;
55 uint8_t data;
56 const uint64_t kLimit = 0xFE00000000000000;
57 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
58 do {
59 if (n & kLimit) { // Will overflow on shift by 7.
60 return false;
61 }
62 if (stream->read(&data, 1) != 1) {
63 return false;
64 }
65 n = (n << 7) | (data & 0x7F);
66 } while (data & 0x80);
67 *value = n;
68 return true;
69}
70
71static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080072 {
73 uint8_t data;
74 if (!read_byte(stream, &data) || data != 0) { // unknown type
75 return false;
76 }
77 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
78 return false;
79 }
halcanarya096d7a2015-03-27 12:16:53 -070080 }
scroggob9a1e342015-11-30 06:25:31 -080081
82 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070083 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
84 return false;
85 }
msarett99f567e2015-08-05 12:58:26 -070086 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070087 return false;
88 }
89 if (size) {
90 *size = SkISize::Make(SkToS32(width), SkToS32(height));
91 }
92 return true;
93}
94
scroggob427db12015-08-12 07:24:13 -070095bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070096 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070097}
98
msarettfdb47572015-10-13 12:50:14 -070099SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
100 const Options& opts) {
msaretta45a6682016-04-22 13:18:37 -0700101 return SkSwizzler::CreateSwizzler(this->getEncodedInfo(), ctable, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -0700102}
103
msarette6dd0042015-10-09 11:07:34 -0700104bool SkWbmpCodec::readRow(uint8_t* row) {
105 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -0700106}
107
msarettc30c4182016-04-20 11:53:35 -0700108SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream)
109 : INHERITED(width, height, info, stream)
msarett99f567e2015-08-05 12:58:26 -0700110 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -0700111 , fSwizzler(nullptr)
msarettf724b992015-10-15 06:41:06 -0700112 , fColorTable(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700113{}
halcanarya096d7a2015-03-27 12:16:53 -0700114
115SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
116 return kWBMP_SkEncodedFormat;
117}
118
scroggoeb602a52015-07-09 08:16:03 -0700119SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700120 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700121 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700122 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700123 SkPMColor ctable[],
msarette6dd0042015-10-09 11:07:34 -0700124 int* ctableCount,
125 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700126 if (options.fSubset) {
127 // Subsets are not supported.
128 return kUnimplemented;
129 }
msarett99f567e2015-08-05 12:58:26 -0700130
msarettb30d6982016-02-15 10:18:45 -0800131 if (!valid_color_type(info.colorType(), info.alphaType()) ||
132 !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700133 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700134 }
135
msarett99f567e2015-08-05 12:58:26 -0700136 // Prepare a color table if necessary
137 setup_color_table(info.colorType(), ctable, ctableCount);
138
msarett99f567e2015-08-05 12:58:26 -0700139 // Initialize the swizzler
140 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
msarettb30d6982016-02-15 10:18:45 -0800141 SkASSERT(swizzler);
msarett99f567e2015-08-05 12:58:26 -0700142
143 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700144 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700145 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
146 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700147 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700148 if (!this->readRow(src.get())) {
149 *rowsDecoded = y;
150 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700151 }
msarett99f567e2015-08-05 12:58:26 -0700152 swizzler->swizzle(dstRow, src.get());
153 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700154 }
scroggoeb602a52015-07-09 08:16:03 -0700155 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700156}
157
scroggodb30be22015-12-08 18:54:13 -0800158bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
159 SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(buffer, bytesRead));
160 SkMemoryStream stream(data);
161 return read_header(&stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700162}
163
164SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
scroggo0a7e69c2015-04-03 07:22:22 -0700165 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700166 SkISize size;
167 if (!read_header(stream, &size)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700168 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700169 }
msarettc30c4182016-04-20 11:53:35 -0700170 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
171 SkEncodedInfo::kOpaque_Alpha, 1);
172 return new SkWbmpCodec(size.width(), size.height(), info, streamDeleter.release());
halcanarya096d7a2015-03-27 12:16:53 -0700173}
msarett99f567e2015-08-05 12:58:26 -0700174
msarette6dd0042015-10-09 11:07:34 -0700175int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700176 void* dstRow = dst;
177 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700178 if (!this->readRow(fSrcBuffer.get())) {
179 return y;
msarett99f567e2015-08-05 12:58:26 -0700180 }
scroggo46c57472015-09-30 08:57:13 -0700181 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
182 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700183 }
msarette6dd0042015-10-09 11:07:34 -0700184 return count;
msarett99f567e2015-08-05 12:58:26 -0700185}
scroggo46c57472015-09-30 08:57:13 -0700186
msarett9b9497e2016-02-11 13:29:36 -0800187bool SkWbmpCodec::onSkipScanlines(int count) {
188 const size_t bytesToSkip = count * fSrcRowBytes;
189 return this->stream()->skip(bytesToSkip) == bytesToSkip;
190}
191
scroggo46c57472015-09-30 08:57:13 -0700192SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
193 const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
scroggo46c57472015-09-30 08:57:13 -0700194 if (options.fSubset) {
195 // Subsets are not supported.
196 return kUnimplemented;
197 }
scroggo46c57472015-09-30 08:57:13 -0700198
msarettb30d6982016-02-15 10:18:45 -0800199 if (!valid_color_type(dstInfo.colorType(), dstInfo.alphaType()) ||
200 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700201 return kInvalidConversion;
202 }
203
204 // Fill in the color table
205 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
206
207 // Copy the color table to a pointer that can be owned by the scanline decoder
208 if (kIndex_8_SkColorType == dstInfo.colorType()) {
209 fColorTable.reset(new SkColorTable(inputColorTable, 2));
210 }
211
212 // Initialize the swizzler
msarettfdb47572015-10-13 12:50:14 -0700213 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options));
msarettb30d6982016-02-15 10:18:45 -0800214 SkASSERT(fSwizzler);
scroggo46c57472015-09-30 08:57:13 -0700215
216 fSrcBuffer.reset(fSrcRowBytes);
217
218 return kSuccess;
219}