blob: 0c85eada7c83c33458558f47b08b7a3dca55d819 [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"
emmaleer8f4ba762015-08-14 07:44:46 -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() {
halcanary96fcdcc2015-08-27 07:41:13 -070071 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070072}
73
74SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info,
75 const SkPMColor* ctable, const Options& opts) {
msarett99f567e2015-08-05 12:58:26 -070076 // Create the swizzler based on the desired color type
77 switch (info.colorType()) {
78 case kIndex_8_SkColorType:
79 case kN32_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -070080 case kRGB_565_SkColorType:
msarett99f567e2015-08-05 12:58:26 -070081 case kGray_8_SkColorType:
emmaleer8f4ba762015-08-14 07:44:46 -070082 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts.fZeroInitialized,
83 this->getInfo());
msarett99f567e2015-08-05 12:58:26 -070084 default:
halcanary96fcdcc2015-08-27 07:41:13 -070085 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -070086 }
87}
88
msarett99f567e2015-08-05 12:58:26 -070089SkCodec::Result SkWbmpCodec::readRow(uint8_t* row) {
90 if (this->stream()->read(row, fSrcRowBytes) != fSrcRowBytes) {
91 return kIncompleteInput;
92 }
93 return kSuccess;
94}
95
halcanarya096d7a2015-03-27 12:16:53 -070096SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
msarett99f567e2015-08-05 12:58:26 -070097 : INHERITED(info, stream)
98 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -070099 , fColorTable(nullptr)
100 , fSwizzler(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700101{}
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())) {
scroggo46c57472015-09-30 08:57:13 -0700125 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700126 }
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));
halcanary96fcdcc2015-08-27 07:41:13 -0700134 if (nullptr == swizzler.get()) {
msarett99f567e2015-08-05 12:58:26 -0700135 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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700154 return read_header(stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700155}
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)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700161 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700162 }
msarett99f567e2015-08-05 12:58:26 -0700163 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
164 kGray_8_SkColorType, kOpaque_SkAlphaType);
halcanary385fe4d2015-08-26 13:07:48 -0700165 return new SkWbmpCodec(info, streamDeleter.detach());
halcanarya096d7a2015-03-27 12:16:53 -0700166}
msarett99f567e2015-08-05 12:58:26 -0700167
scroggo46c57472015-09-30 08:57:13 -0700168SkCodec::Result SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
169 void* dstRow = dst;
170 for (int y = 0; y < count; ++y) {
171 Result rowResult = this->readRow(fSrcBuffer.get());
172 if (kSuccess != rowResult) {
173 return rowResult;
msarett99f567e2015-08-05 12:58:26 -0700174 }
scroggo46c57472015-09-30 08:57:13 -0700175 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
176 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
msarett99f567e2015-08-05 12:58:26 -0700177 }
scroggo46c57472015-09-30 08:57:13 -0700178 return kSuccess;
msarett99f567e2015-08-05 12:58:26 -0700179}
scroggo46c57472015-09-30 08:57:13 -0700180
181SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
182 const Options& options, SkPMColor inputColorTable[], int* inputColorCount) {
183 if (!this->rewindIfNeeded()) {
184 return kCouldNotRewind;
185 }
186 if (options.fSubset) {
187 // Subsets are not supported.
188 return kUnimplemented;
189 }
190 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
191 if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) {
192 return kInvalidScale;
193 }
194 }
195
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
209 fSwizzler.reset(this->initializeSwizzler(dstInfo,
210 get_color_ptr(fColorTable.get()), options));
211 if (nullptr == fSwizzler.get()) {
212 return kInvalidConversion;
213 }
214
215 fSrcBuffer.reset(fSrcRowBytes);
216
217 return kSuccess;
218}
219