blob: 52535e1f029a2c065c0a04fc3c8690b4398ce8e3 [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"
halcanarya096d7a2015-03-27 12:16:53 -070012#include "SkStream.h"
13#include "SkCodec_wbmp.h"
14
msarett99f567e2015-08-05 12:58:26 -070015// Each bit represents a pixel, so width is actually a number of bits.
16// A row will always be stored in bytes, so we round width up to the
17// nearest multiple of 8 to get the number of bits actually in the row.
18// We then divide by 8 to convert to bytes.
19static inline size_t get_src_row_bytes(int width) {
20 return SkAlign8(width) >> 3;
21}
22
23static inline void setup_color_table(SkColorType colorType,
24 SkPMColor* colorPtr, int* colorCount) {
25 if (kIndex_8_SkColorType == colorType) {
26 colorPtr[0] = SK_ColorBLACK;
27 colorPtr[1] = SK_ColorWHITE;
28 *colorCount = 2;
29 }
30}
31
halcanarya096d7a2015-03-27 12:16:53 -070032// http://en.wikipedia.org/wiki/Variable-length_quantity
33static bool read_mbf(SkStream* stream, uint64_t* value) {
34 uint64_t n = 0;
35 uint8_t data;
36 const uint64_t kLimit = 0xFE00000000000000;
37 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
38 do {
39 if (n & kLimit) { // Will overflow on shift by 7.
40 return false;
41 }
42 if (stream->read(&data, 1) != 1) {
43 return false;
44 }
45 n = (n << 7) | (data & 0x7F);
46 } while (data & 0x80);
47 *value = n;
48 return true;
49}
50
51static bool read_header(SkStream* stream, SkISize* size) {
52 uint64_t width, height;
53 uint16_t data;
54 if (stream->read(&data, 2) != 2 || data != 0) {
55 return false;
56 }
57 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
58 return false;
59 }
msarett99f567e2015-08-05 12:58:26 -070060 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070061 return false;
62 }
63 if (size) {
64 *size = SkISize::Make(SkToS32(width), SkToS32(height));
65 }
66 return true;
67}
68
scroggob427db12015-08-12 07:24:13 -070069bool SkWbmpCodec::onRewind() {
70 return read_header(this->stream(), NULL);
msarett99f567e2015-08-05 12:58:26 -070071}
72
73SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info,
74 const SkPMColor* ctable, const Options& opts) {
75 // TODO (msarett): Reenable support for 565 if it is desired
76 // skbug.com/3683
77
78 // Create the swizzler based on the desired color type
79 switch (info.colorType()) {
80 case kIndex_8_SkColorType:
81 case kN32_SkColorType:
82 case kGray_8_SkColorType:
83 return SkSwizzler::CreateSwizzler(
84 SkSwizzler::kBit, ctable, info, opts.fZeroInitialized);
85 default:
86 return NULL;
halcanarya096d7a2015-03-27 12:16:53 -070087 }
88}
89
msarett99f567e2015-08-05 12:58:26 -070090SkCodec::Result SkWbmpCodec::readRow(uint8_t* row) {
91 if (this->stream()->read(row, fSrcRowBytes) != fSrcRowBytes) {
92 return kIncompleteInput;
93 }
94 return kSuccess;
95}
96
halcanarya096d7a2015-03-27 12:16:53 -070097SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
msarett99f567e2015-08-05 12:58:26 -070098 : INHERITED(info, stream)
99 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
100{}
halcanarya096d7a2015-03-27 12:16:53 -0700101
102SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
103 return kWBMP_SkEncodedFormat;
104}
105
scroggoeb602a52015-07-09 08:16:03 -0700106SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700107 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700108 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700109 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700110 SkPMColor ctable[],
111 int* ctableCount) {
scroggob427db12015-08-12 07:24:13 -0700112 if (!this->rewindIfNeeded()) {
scroggoeb602a52015-07-09 08:16:03 -0700113 return kCouldNotRewind;
halcanarya096d7a2015-03-27 12:16:53 -0700114 }
scroggob636b452015-07-22 07:16:20 -0700115 if (options.fSubset) {
116 // Subsets are not supported.
117 return kUnimplemented;
118 }
halcanarya096d7a2015-03-27 12:16:53 -0700119 if (info.dimensions() != this->getInfo().dimensions()) {
scroggoeb602a52015-07-09 08:16:03 -0700120 return kInvalidScale;
halcanarya096d7a2015-03-27 12:16:53 -0700121 }
msarett99f567e2015-08-05 12:58:26 -0700122
123 // Prepare a color table if necessary
124 setup_color_table(info.colorType(), ctable, ctableCount);
125
126
127 // Initialize the swizzler
128 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
129 if (NULL == swizzler.get()) {
130 return kInvalidConversion;
halcanarya096d7a2015-03-27 12:16:53 -0700131 }
msarett99f567e2015-08-05 12:58:26 -0700132
133 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700134 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700135 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
136 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700137 for (int y = 0; y < size.height(); ++y) {
msarett99f567e2015-08-05 12:58:26 -0700138 Result rowResult = this->readRow(src.get());
139 if (kSuccess != rowResult) {
140 return rowResult;
halcanarya096d7a2015-03-27 12:16:53 -0700141 }
msarett99f567e2015-08-05 12:58:26 -0700142 swizzler->swizzle(dstRow, src.get());
143 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700144 }
scroggoeb602a52015-07-09 08:16:03 -0700145 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700146}
147
148bool SkWbmpCodec::IsWbmp(SkStream* stream) {
149 return read_header(stream, NULL);
150}
151
152SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
scroggo0a7e69c2015-04-03 07:22:22 -0700153 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700154 SkISize size;
155 if (!read_header(stream, &size)) {
156 return NULL;
157 }
msarett99f567e2015-08-05 12:58:26 -0700158 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
159 kGray_8_SkColorType, kOpaque_SkAlphaType);
scroggo0a7e69c2015-04-03 07:22:22 -0700160 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
halcanarya096d7a2015-03-27 12:16:53 -0700161}
msarett99f567e2015-08-05 12:58:26 -0700162
163class SkWbmpScanlineDecoder : public SkScanlineDecoder {
164public:
165 /*
166 * Takes ownership of all pointer paramters.
167 */
168 SkWbmpScanlineDecoder(SkWbmpCodec* codec)
169 : INHERITED(codec->getInfo())
170 , fCodec(codec)
171 , fColorTable(NULL)
172 , fSwizzler(NULL)
173 , fSrcBuffer(codec->fSrcRowBytes)
174 {}
175
176 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) override {
177 void* dstRow = dst;
178 for (int y = 0; y < count; ++y) {
179 SkCodec::Result rowResult = fCodec->readRow(fSrcBuffer.get());
180 if (SkCodec::kSuccess != rowResult) {
181 return rowResult;
182 }
183 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
184 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
185 }
186 return SkCodec::kSuccess;
187 }
188
189 SkCodec::Result onStart(const SkImageInfo& dstInfo,
190 const SkCodec::Options& options, SkPMColor inputColorTable[],
191 int* inputColorCount) {
scroggob427db12015-08-12 07:24:13 -0700192 if (!fCodec->rewindIfNeeded()) {
msarett99f567e2015-08-05 12:58:26 -0700193 return SkCodec::kCouldNotRewind;
194 }
195 if (options.fSubset) {
196 // Subsets are not supported.
197 return SkCodec::kUnimplemented;
198 }
199 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
200 return SkCodec::kInvalidScale;
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(SkNEW_ARGS(SkColorTable, (inputColorTable, 2)));
209 }
210
211 // Initialize the swizzler
212 fSwizzler.reset(fCodec->initializeSwizzler(dstInfo,
213 get_color_ptr(fColorTable.get()), options));
214 if (NULL == fSwizzler.get()) {
215 return SkCodec::kInvalidInput;
216 }
217
218 return SkCodec::kSuccess;
219 }
220
221private:
222 SkAutoTDelete<SkWbmpCodec> fCodec;
223 SkAutoTUnref<SkColorTable> fColorTable;
224 SkAutoTDelete<SkSwizzler> fSwizzler;
225 SkAutoTMalloc<uint8_t> fSrcBuffer;
226
227 typedef SkScanlineDecoder INHERITED;
228};
229
230SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) {
231 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(
232 SkWbmpCodec::NewFromStream(stream)));
233 if (!codec) {
234 return NULL;
235 }
236
237 // Return the new scanline decoder
238 return SkNEW_ARGS(SkWbmpScanlineDecoder, (codec.detach()));
239}