blob: 8f8763fb90e0f0256506c12e82c54c04ab570ce2 [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
scroggob9a1e342015-11-30 06:25:31 -080032static bool read_byte(SkStream* stream, uint8_t* data)
33{
34 return stream->read(data, 1) == 1;
35}
36
halcanarya096d7a2015-03-27 12:16:53 -070037// http://en.wikipedia.org/wiki/Variable-length_quantity
38static bool read_mbf(SkStream* stream, uint64_t* value) {
39 uint64_t n = 0;
40 uint8_t data;
41 const uint64_t kLimit = 0xFE00000000000000;
42 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
43 do {
44 if (n & kLimit) { // Will overflow on shift by 7.
45 return false;
46 }
47 if (stream->read(&data, 1) != 1) {
48 return false;
49 }
50 n = (n << 7) | (data & 0x7F);
51 } while (data & 0x80);
52 *value = n;
53 return true;
54}
55
56static bool read_header(SkStream* stream, SkISize* size) {
scroggob9a1e342015-11-30 06:25:31 -080057 {
58 uint8_t data;
59 if (!read_byte(stream, &data) || data != 0) { // unknown type
60 return false;
61 }
62 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
63 return false;
64 }
halcanarya096d7a2015-03-27 12:16:53 -070065 }
scroggob9a1e342015-11-30 06:25:31 -080066
67 uint64_t width, height;
halcanarya096d7a2015-03-27 12:16:53 -070068 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
69 return false;
70 }
msarett99f567e2015-08-05 12:58:26 -070071 if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
halcanarya096d7a2015-03-27 12:16:53 -070072 return false;
73 }
74 if (size) {
75 *size = SkISize::Make(SkToS32(width), SkToS32(height));
76 }
77 return true;
78}
79
scroggob427db12015-08-12 07:24:13 -070080bool SkWbmpCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -070081 return read_header(this->stream(), nullptr);
msarett99f567e2015-08-05 12:58:26 -070082}
83
msarettfdb47572015-10-13 12:50:14 -070084SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
85 const Options& opts) {
msarett99f567e2015-08-05 12:58:26 -070086 // Create the swizzler based on the desired color type
87 switch (info.colorType()) {
88 case kIndex_8_SkColorType:
89 case kN32_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -070090 case kRGB_565_SkColorType:
msarett99f567e2015-08-05 12:58:26 -070091 case kGray_8_SkColorType:
scroggoe7fc14b2015-10-02 13:14:46 -070092 break;
msarett99f567e2015-08-05 12:58:26 -070093 default:
halcanary96fcdcc2015-08-27 07:41:13 -070094 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -070095 }
msarettfdb47572015-10-13 12:50:14 -070096 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts);
halcanarya096d7a2015-03-27 12:16:53 -070097}
98
msarette6dd0042015-10-09 11:07:34 -070099bool SkWbmpCodec::readRow(uint8_t* row) {
100 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
msarett99f567e2015-08-05 12:58:26 -0700101}
102
halcanarya096d7a2015-03-27 12:16:53 -0700103SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
msarett99f567e2015-08-05 12:58:26 -0700104 : INHERITED(info, stream)
105 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
scroggo46c57472015-09-30 08:57:13 -0700106 , fSwizzler(nullptr)
msarettf724b992015-10-15 06:41:06 -0700107 , fColorTable(nullptr)
msarett99f567e2015-08-05 12:58:26 -0700108{}
halcanarya096d7a2015-03-27 12:16:53 -0700109
110SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
111 return kWBMP_SkEncodedFormat;
112}
113
scroggoeb602a52015-07-09 08:16:03 -0700114SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
msarett99f567e2015-08-05 12:58:26 -0700115 void* dst,
scroggoeb602a52015-07-09 08:16:03 -0700116 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700117 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700118 SkPMColor ctable[],
msarette6dd0042015-10-09 11:07:34 -0700119 int* ctableCount,
120 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700121 if (options.fSubset) {
122 // Subsets are not supported.
123 return kUnimplemented;
124 }
msarett99f567e2015-08-05 12:58:26 -0700125
scroggod1bc5742015-08-12 08:31:44 -0700126 if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
scroggo46c57472015-09-30 08:57:13 -0700127 return kInvalidConversion;
scroggod1bc5742015-08-12 08:31:44 -0700128 }
129
msarett99f567e2015-08-05 12:58:26 -0700130 // Prepare a color table if necessary
131 setup_color_table(info.colorType(), ctable, ctableCount);
132
msarett99f567e2015-08-05 12:58:26 -0700133 // Initialize the swizzler
134 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options));
halcanary96fcdcc2015-08-27 07:41:13 -0700135 if (nullptr == swizzler.get()) {
msarett99f567e2015-08-05 12:58:26 -0700136 return kInvalidConversion;
halcanarya096d7a2015-03-27 12:16:53 -0700137 }
msarett99f567e2015-08-05 12:58:26 -0700138
139 // Perform the decode
halcanarya096d7a2015-03-27 12:16:53 -0700140 SkISize size = info.dimensions();
msarett99f567e2015-08-05 12:58:26 -0700141 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
142 void* dstRow = dst;
halcanarya096d7a2015-03-27 12:16:53 -0700143 for (int y = 0; y < size.height(); ++y) {
msarette6dd0042015-10-09 11:07:34 -0700144 if (!this->readRow(src.get())) {
145 *rowsDecoded = y;
146 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700147 }
msarett99f567e2015-08-05 12:58:26 -0700148 swizzler->swizzle(dstRow, src.get());
149 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
halcanarya096d7a2015-03-27 12:16:53 -0700150 }
scroggoeb602a52015-07-09 08:16:03 -0700151 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700152}
153
154bool SkWbmpCodec::IsWbmp(SkStream* stream) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return read_header(stream, nullptr);
halcanarya096d7a2015-03-27 12:16:53 -0700156}
157
158SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
scroggo0a7e69c2015-04-03 07:22:22 -0700159 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700160 SkISize size;
161 if (!read_header(stream, &size)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return nullptr;
halcanarya096d7a2015-03-27 12:16:53 -0700163 }
msarett99f567e2015-08-05 12:58:26 -0700164 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
165 kGray_8_SkColorType, kOpaque_SkAlphaType);
halcanary385fe4d2015-08-26 13:07:48 -0700166 return new SkWbmpCodec(info, streamDeleter.detach());
halcanarya096d7a2015-03-27 12:16:53 -0700167}
msarett99f567e2015-08-05 12:58:26 -0700168
msarette6dd0042015-10-09 11:07:34 -0700169int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700170 void* dstRow = dst;
171 for (int y = 0; y < count; ++y) {
msarette6dd0042015-10-09 11:07:34 -0700172 if (!this->readRow(fSrcBuffer.get())) {
173 return y;
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 }
msarette6dd0042015-10-09 11:07:34 -0700178 return count;
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) {
scroggo46c57472015-09-30 08:57:13 -0700183 if (options.fSubset) {
184 // Subsets are not supported.
185 return kUnimplemented;
186 }
scroggo46c57472015-09-30 08:57:13 -0700187
188 if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
189 return kInvalidConversion;
190 }
191
192 // Fill in the color table
193 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
194
195 // Copy the color table to a pointer that can be owned by the scanline decoder
196 if (kIndex_8_SkColorType == dstInfo.colorType()) {
197 fColorTable.reset(new SkColorTable(inputColorTable, 2));
198 }
199
200 // Initialize the swizzler
msarettfdb47572015-10-13 12:50:14 -0700201 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options));
scroggo46c57472015-09-30 08:57:13 -0700202 if (nullptr == fSwizzler.get()) {
203 return kInvalidConversion;
204 }
205
206 fSrcBuffer.reset(fSrcRowBytes);
207
208 return kSuccess;
209}