• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "SkCodecPriv.h"
10 #include "SkColorPriv.h"
11 #include "SkColorTable.h"
12 #include "SkData.h"
13 #include "SkStream.h"
14 #include "SkWbmpCodec.h"
15 
16 // 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.
get_src_row_bytes(int width)20 static inline size_t get_src_row_bytes(int width) {
21     return SkAlign8(width) >> 3;
22 }
23 
valid_color_type(const SkImageInfo & dstInfo)24 static inline bool valid_color_type(const SkImageInfo& dstInfo) {
25     switch (dstInfo.colorType()) {
26         case kRGBA_8888_SkColorType:
27         case kBGRA_8888_SkColorType:
28         case kGray_8_SkColorType:
29         case kRGB_565_SkColorType:
30             return true;
31         case kRGBA_F16_SkColorType:
32             return dstInfo.colorSpace() && dstInfo.colorSpace()->gammaIsLinear();
33         default:
34             return false;
35     }
36 }
37 
read_byte(SkStream * stream,uint8_t * data)38 static bool read_byte(SkStream* stream, uint8_t* data)
39 {
40     return stream->read(data, 1) == 1;
41 }
42 
43 // http://en.wikipedia.org/wiki/Variable-length_quantity
read_mbf(SkStream * stream,uint64_t * value)44 static bool read_mbf(SkStream* stream, uint64_t* value) {
45     uint64_t n = 0;
46     uint8_t data;
47     const uint64_t kLimit = 0xFE00000000000000;
48     SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7));
49     do {
50         if (n & kLimit) { // Will overflow on shift by 7.
51             return false;
52         }
53         if (stream->read(&data, 1) != 1) {
54             return false;
55         }
56         n = (n << 7) | (data & 0x7F);
57     } while (data & 0x80);
58     *value = n;
59     return true;
60 }
61 
read_header(SkStream * stream,SkISize * size)62 static bool read_header(SkStream* stream, SkISize* size) {
63     {
64         uint8_t data;
65         if (!read_byte(stream, &data) || data != 0) { // unknown type
66             return false;
67         }
68         if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header
69             return false;
70         }
71     }
72 
73     uint64_t width, height;
74     if (!read_mbf(stream, &width) || width > 0xFFFF || !width) {
75         return false;
76     }
77     if (!read_mbf(stream, &height) || height > 0xFFFF || !height) {
78         return false;
79     }
80     if (size) {
81         *size = SkISize::Make(SkToS32(width), SkToS32(height));
82     }
83     return true;
84 }
85 
onRewind()86 bool SkWbmpCodec::onRewind() {
87     return read_header(this->stream(), nullptr);
88 }
89 
readRow(uint8_t * row)90 bool SkWbmpCodec::readRow(uint8_t* row) {
91     return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
92 }
93 
SkWbmpCodec(int width,int height,const SkEncodedInfo & info,SkStream * stream)94 SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream)
95     // Wbmp does not need a colorXform, so choose an arbitrary srcFormat.
96     : INHERITED(width, height, info, SkColorSpaceXform::ColorFormat(),
97                 stream, SkColorSpace::MakeSRGB())
98     , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
99     , fSwizzler(nullptr)
100 {}
101 
onGetEncodedFormat() const102 SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const {
103     return SkEncodedImageFormat::kWBMP;
104 }
105 
onGetPixels(const SkImageInfo & info,void * dst,size_t rowBytes,const Options & options,int * rowsDecoded)106 SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info,
107                                          void* dst,
108                                          size_t rowBytes,
109                                          const Options& options,
110                                          int* rowsDecoded) {
111     if (options.fSubset) {
112         // Subsets are not supported.
113         return kUnimplemented;
114     }
115 
116     if (!valid_color_type(info) || !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
117         return kInvalidConversion;
118     }
119 
120     // Initialize the swizzler
121     std::unique_ptr<SkSwizzler> swizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, info,
122                                                             options);
123     SkASSERT(swizzler);
124 
125     // Perform the decode
126     SkISize size = info.dimensions();
127     SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
128     void* dstRow = dst;
129     for (int y = 0; y < size.height(); ++y) {
130         if (!this->readRow(src.get())) {
131             *rowsDecoded = y;
132             return kIncompleteInput;
133         }
134         swizzler->swizzle(dstRow, src.get());
135         dstRow = SkTAddOffset<void>(dstRow, rowBytes);
136     }
137     return kSuccess;
138 }
139 
IsWbmp(const void * buffer,size_t bytesRead)140 bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) {
141     SkMemoryStream stream(buffer, bytesRead, false);
142     return read_header(&stream, nullptr);
143 }
144 
NewFromStream(SkStream * stream,Result * result)145 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream, Result* result) {
146     std::unique_ptr<SkStream> streamDeleter(stream);
147     SkISize size;
148     if (!read_header(stream, &size)) {
149         // This already succeeded in IsWbmp, so this stream was corrupted in/
150         // after rewind.
151         *result = kCouldNotRewind;
152         return nullptr;
153     }
154     *result = kSuccess;
155     SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
156             SkEncodedInfo::kOpaque_Alpha, 1);
157     return new SkWbmpCodec(size.width(), size.height(), info, streamDeleter.release());
158 }
159 
onGetScanlines(void * dst,int count,size_t dstRowBytes)160 int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
161     void* dstRow = dst;
162     for (int y = 0; y < count; ++y) {
163         if (!this->readRow(fSrcBuffer.get())) {
164             return y;
165         }
166         fSwizzler->swizzle(dstRow, fSrcBuffer.get());
167         dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
168     }
169     return count;
170 }
171 
onSkipScanlines(int count)172 bool SkWbmpCodec::onSkipScanlines(int count) {
173     const size_t bytesToSkip = count * fSrcRowBytes;
174     return this->stream()->skip(bytesToSkip) == bytesToSkip;
175 }
176 
onStartScanlineDecode(const SkImageInfo & dstInfo,const Options & options)177 SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
178         const Options& options) {
179     if (options.fSubset) {
180         // Subsets are not supported.
181         return kUnimplemented;
182     }
183 
184     if (!valid_color_type(dstInfo) ||
185         !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType()))
186     {
187         return kInvalidConversion;
188     }
189 
190     // Initialize the swizzler
191     fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, dstInfo, options);
192     SkASSERT(fSwizzler);
193 
194     fSrcBuffer.reset(fSrcRowBytes);
195 
196     return kSuccess;
197 }
198