blob: 35ac808219c625ad1bacf045d6414fb4b8e40b76 [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"
9#include "SkColorPriv.h"
10#include "SkStream.h"
11#include "SkCodec_wbmp.h"
12
13// http://en.wikipedia.org/wiki/Variable-length_quantity
14static bool read_mbf(SkStream* stream, uint64_t* value) {
15 uint64_t n = 0;
16 uint8_t data;
17 const uint64_t kLimit = 0xFE00000000000000;
18 SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
19 do {
20 if (n & kLimit) { // Will overflow on shift by 7.
21 return false;
22 }
23 if (stream->read(&data, 1) != 1) {
24 return false;
25 }
26 n = (n << 7) | (data & 0x7F);
27 } while (data & 0x80);
28 *value = n;
29 return true;
30}
31
32static bool read_header(SkStream* stream, SkISize* size) {
33 uint64_t width, height;
34 uint16_t data;
35 if (stream->read(&data, 2) != 2 || data != 0) {
36 return false;
37 }
38 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
39 return false;
40 }
41 if (!read_mbf(stream, &height) || width > 0xFFFF || !height) {
42 return false;
43 }
44 if (size) {
45 *size = SkISize::Make(SkToS32(width), SkToS32(height));
46 }
47 return true;
48}
49
50#define BLACK SkPackARGB32NoCheck(0xFF, 0, 0, 0)
51#define WHITE SkPackARGB32NoCheck(0xFF, 0xFF, 0xFF, 0xFF)
52
53#define GRAYSCALE_BLACK 0
54#define GRAYSCALE_WHITE 0xFF
55
56#define RGB565_BLACK 0
57#define RGB565_WHITE 0xFFFF
58
59static SkPMColor bit_to_pmcolor(U8CPU bit) { return bit ? WHITE : BLACK; }
60
61static uint8_t bit_to_bit(U8CPU bit) { return bit; }
62
63static uint8_t bit_to_grayscale(U8CPU bit) {
64 return bit ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
65}
66
67static uint16_t bit_to_rgb565(U8CPU bit) {
68 return bit ? RGB565_WHITE : RGB565_BLACK;
69}
70
71typedef void (*ExpandProc)(uint8_t*, const uint8_t*, int);
72
73// TODO(halcanary): Add this functionality (grayscale and indexed output) to
74// SkSwizzler and use it here.
75template <typename T, T (*TRANSFORM)(U8CPU)>
76static void expand_bits_to_T(uint8_t* dstptr, const uint8_t* src, int bits) {
77 T* dst = reinterpret_cast<T*>(dstptr);
78 int bytes = bits >> 3;
79 for (int i = 0; i < bytes; i++) {
80 U8CPU mask = *src++;
81 for (int j = 0; j < 8; j++) {
82 dst[j] = TRANSFORM((mask >> (7 - j)) & 1);
83 }
84 dst += 8;
85 }
86 bits &= 7;
87 if (bits > 0) {
88 U8CPU mask = *src;
89 do {
90 *dst++ = TRANSFORM((mask >> 7) & 1);
91 mask <<= 1;
92 } while (--bits != 0);
93 }
94}
95
96SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
97 : INHERITED(info, stream) {}
98
99SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const {
100 return kWBMP_SkEncodedFormat;
101}
102
scroggoeb602a52015-07-09 08:16:03 -0700103SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
104 void* pixels,
105 size_t rowBytes,
scroggob636b452015-07-22 07:16:20 -0700106 const Options& options,
scroggoeb602a52015-07-09 08:16:03 -0700107 SkPMColor ctable[],
108 int* ctableCount) {
halcanarya096d7a2015-03-27 12:16:53 -0700109 SkCodec::RewindState rewindState = this->rewindIfNeeded();
110 if (rewindState == kCouldNotRewind_RewindState) {
scroggoeb602a52015-07-09 08:16:03 -0700111 return kCouldNotRewind;
halcanarya096d7a2015-03-27 12:16:53 -0700112 } else if (rewindState == kRewound_RewindState) {
113 (void)read_header(this->stream(), NULL);
114 }
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 }
122 ExpandProc proc = NULL;
123 switch (info.colorType()) {
124 case kGray_8_SkColorType:
125 proc = expand_bits_to_T<uint8_t, bit_to_grayscale>;
126 break;
127 case kN32_SkColorType:
128 proc = expand_bits_to_T<SkPMColor, bit_to_pmcolor>;
129 break;
130 case kIndex_8_SkColorType:
131 ctable[0] = BLACK;
132 ctable[1] = WHITE;
133 *ctableCount = 2;
134 proc = expand_bits_to_T<uint8_t, bit_to_bit>;
135 break;
136 case kRGB_565_SkColorType:
137 proc = expand_bits_to_T<uint16_t, bit_to_rgb565>;
138 break;
139 default:
scroggoeb602a52015-07-09 08:16:03 -0700140 return kInvalidConversion;
halcanarya096d7a2015-03-27 12:16:53 -0700141 }
142 SkISize size = info.dimensions();
143 uint8_t* dst = static_cast<uint8_t*>(pixels);
144 size_t srcRowBytes = SkAlign8(size.width()) >> 3;
145 SkAutoTMalloc<uint8_t> src(srcRowBytes);
146 for (int y = 0; y < size.height(); ++y) {
147 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) {
scroggoeb602a52015-07-09 08:16:03 -0700148 return kIncompleteInput;
halcanarya096d7a2015-03-27 12:16:53 -0700149 }
150 proc(dst, src.get(), size.width());
151 dst += rowBytes;
152 }
scroggoeb602a52015-07-09 08:16:03 -0700153 return kSuccess;
halcanarya096d7a2015-03-27 12:16:53 -0700154}
155
156bool SkWbmpCodec::IsWbmp(SkStream* stream) {
157 return read_header(stream, NULL);
158}
159
160SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) {
scroggo0a7e69c2015-04-03 07:22:22 -0700161 SkAutoTDelete<SkStream> streamDeleter(stream);
halcanarya096d7a2015-03-27 12:16:53 -0700162 SkISize size;
163 if (!read_header(stream, &size)) {
164 return NULL;
165 }
166 SkImageInfo info =
167 SkImageInfo::Make(size.width(), size.height(), kGray_8_SkColorType,
168 kOpaque_SkAlphaType);
scroggo0a7e69c2015-04-03 07:22:22 -0700169 return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach()));
halcanarya096d7a2015-03-27 12:16:53 -0700170}