blob: 3081a3bba9c47b45af5c376616465e507ef65873 [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"
emmaleer09441002015-08-13 11:26:57 -070012#include "SkScaledCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070013#include "SkStream.h"
14#include "SkCodec_wbmp.h"
15
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
halcanarya096d7a2015-03-27 12:16:53 -070033// http://en.wikipedia.org/wiki/Variable-length_quantity
34static bool read_mbf(SkStream* stream, uint64_t* value) {
35 uint64_t n = 0;
36 uint8_t data;
37 const uint64_t kLimit = 0xFE00000000000000;
38 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
39 do {
40 if (n & kLimit) { // Will overflow on shift by 7.
41 return false;
42 }
43 if (stream->read(&data, 1) != 1) {
44 return false;
45 }
46 n = (n << 7) | (data & 0x7F);
47 } while (data & 0x80);
48 *value = n;
49 return true;
50}
51
52static bool read_header(SkStream* stream, SkISize* size) {
53 uint64_t width, height;
54 uint16_t data;
55 if (stream->read(&data, 2) != 2 || data != 0) {
56 return false;
57 }
58 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
59 return false;
60 }
msarett99f567e2015-08-05 12:58:26 -070061 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070062 return false;
63 }
64 if (size) {
65 *size = SkISize::Make(SkToS32(width), SkToS32(height));
66 }
67 return true;
68}
69
scroggob427db12015-08-12 07:24:13 -070070bool SkWbmpCodec::onRewind() {
71 return read_header(this->stream(), NULL);
msarett99f567e2015-08-05 12:58:26 -070072}
73
74SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info,
75 const SkPMColor* ctable, const Options& opts) {
76 // TODO (msarett): Reenable support for 565 if it is desired
77 // skbug.com/3683
78
79 // Create the swizzler based on the desired color type
80 switch (info.colorType()) {
81 case kIndex_8_SkColorType:
82 case kN32_SkColorType:
83 case kGray_8_SkColorType:
emmaleer09441002015-08-13 11:26:57 -070084 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts.fZeroInitialized,
85 this->getInfo());
msarett99f567e2015-08-05 12:58:26 -070086 default:
87 return NULL;
halcanarya096d7a2015-03-27 12:16:53 -070088 }
89}
90
msarett99f567e2015-08-05 12:58:26 -070091SkCodec::Result SkWbmpCodec::readRow(uint8_t* row) {
92 if (this->stream()->read(row, fSrcRowBytes) != fSrcRowBytes) {
93 return kIncompleteInput;
94 }
95 return kSuccess;
96}
97
halcanarya096d7a2015-03-27 12:16:53 -070098SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
msarett99f567e2015-08-05 12:58:26 -070099 : INHERITED(info, stream)
100 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
101{}
halcanarya096d7a2015-03-27 12:16:53 -0700102
103SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
104 return kWBMP_SkEncodedFormat;
105}
106
scroggoeb602a52015-07-09 08:16:03 -0700107SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700108 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700109 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700110 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700111 SkPMColor ctable[],
112 int* ctableCount) {
scroggob427db12015-08-12 07:24:13 -0700113 if (!this->rewindIfNeeded()) {
scroggoeb602a52015-07-09 08:16:03 -0700114 return kCouldNotRewind;
halcanarya096d7a2015-03-27 12:16:53 -0700115 }
scroggob636b452015-07-22 07:16:20 -0700116 if (options.fSubset) {
117 // Subsets are not supported.
118 return kUnimplemented;
119 }
halcanarya096d7a2015-03-27 12:16:53 -0700120 if (info.dimensions() != this->getInfo().dimensions()) {
scroggoeb602a52015-07-09 08:16:03 -0700121 return kInvalidScale;
halcanarya096d7a2015-03-27 12:16:53 -0700122 }
msarett99f567e2015-08-05 12:58:26 -0700123
scroggod1bc5742015-08-12 08:31:44 -0700124 if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
125 return SkCodec::kInvalidConversion;
126 }
127
msarett99f567e2015-08-05 12:58:26 -0700128 // Prepare a color table if necessary
129 setup_color_table(info.colorType(), ctable, ctableCount);
130
131
132 // Initialize the swizzler
133 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
134 if (NULL == swizzler.get()) {
135 return kInvalidConversion;
halcanarya096d7a2015-03-27 12:16:53 -0700136 }
msarett99f567e2015-08-05 12:58:26 -0700137
138 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700139 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700140 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
141 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700142 for (int y = 0; y < size.height(); ++y) {
msarett99f567e2015-08-05 12:58:26 -0700143 Result rowResult = this->readRow(src.get());
144 if (kSuccess != rowResult) {
145 return rowResult;
halcanarya096d7a2015-03-27 12:16:53 -0700146 }
msarett99f567e2015-08-05 12:58:26 -0700147 swizzler->swizzle(dstRow, src.get());
148 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700149 }
scroggoeb602a52015-07-09 08:16:03 -0700150 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700151}
152
153bool SkWbmpCodec::IsWbmp(SkStream* stream) {
154 return read_header(stream, NULL);
155}
156
157SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
scroggo0a7e69c2015-04-03 07:22:22 -0700158 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700159 SkISize size;
160 if (!read_header(stream, &size)) {
161 return NULL;
162 }
msarett99f567e2015-08-05 12:58:26 -0700163 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
164 kGray_8_SkColorType, kOpaque_SkAlphaType);
scroggo0a7e69c2015-04-03 07:22:22 -0700165 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
halcanarya096d7a2015-03-27 12:16:53 -0700166}
msarett99f567e2015-08-05 12:58:26 -0700167
168class SkWbmpScanlineDecoder : public SkScanlineDecoder {
169public:
170 /*
171 * Takes ownership of all pointer paramters.
172 */
173 SkWbmpScanlineDecoder(SkWbmpCodec* codec)
174 : INHERITED(codec->getInfo())
175 , fCodec(codec)
176 , fColorTable(NULL)
177 , fSwizzler(NULL)
178 , fSrcBuffer(codec->fSrcRowBytes)
179 {}
180
181 SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) override {
182 void* dstRow = dst;
183 for (int y = 0; y < count; ++y) {
184 SkCodec::Result rowResult = fCodec->readRow(fSrcBuffer.get());
185 if (SkCodec::kSuccess != rowResult) {
186 return rowResult;
187 }
188 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
189 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
190 }
191 return SkCodec::kSuccess;
192 }
193
194 SkCodec::Result onStart(const SkImageInfo& dstInfo,
195 const SkCodec::Options& options, SkPMColor inputColorTable[],
196 int* inputColorCount) {
scroggob427db12015-08-12 07:24:13 -0700197 if (!fCodec->rewindIfNeeded()) {
msarett99f567e2015-08-05 12:58:26 -0700198 return SkCodec::kCouldNotRewind;
199 }
200 if (options.fSubset) {
201 // Subsets are not supported.
202 return SkCodec::kUnimplemented;
203 }
204 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
emmaleer09441002015-08-13 11:26:57 -0700205 if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) {
206 return SkCodec::kInvalidScale;
207 }
msarett99f567e2015-08-05 12:58:26 -0700208 }
209
scroggod1bc5742015-08-12 08:31:44 -0700210 if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
211 return SkCodec::kInvalidConversion;
212 }
213
msarett99f567e2015-08-05 12:58:26 -0700214 // Fill in the color table
215 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
216
217 // Copy the color table to a pointer that can be owned by the scanline decoder
218 if (kIndex_8_SkColorType == dstInfo.colorType()) {
219 fColorTable.reset(SkNEW_ARGS(SkColorTable, (inputColorTable, 2)));
220 }
221
222 // Initialize the swizzler
223 fSwizzler.reset(fCodec->initializeSwizzler(dstInfo,
224 get_color_ptr(fColorTable.get()), options));
225 if (NULL == fSwizzler.get()) {
emmaleer09441002015-08-13 11:26:57 -0700226 return SkCodec::kInvalidConversion;
msarett99f567e2015-08-05 12:58:26 -0700227 }
228
229 return SkCodec::kSuccess;
230 }
231
emmaleer09441002015-08-13 11:26:57 -0700232 SkEncodedFormat onGetEncodedFormat() const {
233 return kWBMP_SkEncodedFormat;
234 }
235
msarett99f567e2015-08-05 12:58:26 -0700236private:
237 SkAutoTDelete<SkWbmpCodec> fCodec;
238 SkAutoTUnref<SkColorTable> fColorTable;
239 SkAutoTDelete<SkSwizzler> fSwizzler;
240 SkAutoTMalloc<uint8_t> fSrcBuffer;
241
242 typedef SkScanlineDecoder INHERITED;
243};
244
245SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) {
246 SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>(
247 SkWbmpCodec::NewFromStream(stream)));
248 if (!codec) {
249 return NULL;
250 }
251
252 // Return the new scanline decoder
253 return SkNEW_ARGS(SkWbmpScanlineDecoder, (codec.detach()));
254}