| 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 "src/codec/SkWbmpCodec.h" |
| 9 |
|
| 10 |
#include "include/codec/SkCodec.h" |
| 11 |
#include "include/codec/SkEncodedImageFormat.h" |
| 12 |
#include "include/codec/SkWbmpDecoder.h" |
| 13 |
#include "include/core/SkColorType.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkStream.h" |
| 19 |
#include "include/private/SkEncodedInfo.h" |
| 20 |
#include "include/private/base/SkAlign.h" |
| 21 |
#include "include/private/base/SkTo.h" |
| 22 |
#include "modules/skcms/skcms.h" |
| 23 |
#include "src/codec/SkCodecPriv.h" |
| 24 |
|
| 25 |
#include <utility> |
| 26 |
|
| 27 |
using namespace skia_private; |
| 28 |
|
| 29 |
// Each bit represents a pixel, so width is actually a number of bits. |
| 30 |
// A row will always be stored in bytes, so we round width up to the |
| 31 |
// nearest multiple of 8 to get the number of bits actually in the row. |
| 32 |
// We then divide by 8 to convert to bytes. |
| 33 |
static inline size_t get_src_row_bytes(int width) { |
| 34 |
return SkAlign8(width) >> 3; |
| 35 |
} |
| 36 |
|
| 37 |
static inline bool valid_color_type(const SkImageInfo& dstInfo) { |
| 38 |
switch (dstInfo.colorType()) { |
| 39 |
case kRGBA_8888_SkColorType: |
| 40 |
case kBGRA_8888_SkColorType: |
| 41 |
case kGray_8_SkColorType: |
| 42 |
case kRGB_565_SkColorType: |
| 43 |
return true; |
| 44 |
case kRGBA_F16_SkColorType: |
| 45 |
return dstInfo.colorSpace(); |
| 46 |
default: |
| 47 |
return false; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
static bool read_byte(SkStream* stream, uint8_t* data) |
| 52 |
{ |
| 53 |
return stream->read(data, 1) == 1; |
| 54 |
} |
| 55 |
|
| 56 |
// http://en.wikipedia.org/wiki/Variable-length_quantity |
| 57 |
static bool read_mbf(SkStream* stream, uint64_t* value) { |
| 58 |
uint64_t n = 0; |
| 59 |
uint8_t data; |
| 60 |
const uint64_t kLimit = 0xFE00000000000000; |
| 61 |
SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); |
| 62 |
do { |
| 63 |
if (n & kLimit) { // Will overflow on shift by 7. |
| 64 |
return false; |
| 65 |
} |
| 66 |
if (stream->read(&data, 1) != 1) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
n = (n << 7) | (data & 0x7F); |
| 70 |
} while (data & 0x80); |
| 71 |
*value = n; |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
static bool read_header(SkStream* stream, SkISize* size) { |
| 76 |
{ |
| 77 |
uint8_t data; |
| 78 |
if (!read_byte(stream, &data) || data != 0) { // unknown type |
| 79 |
return false; |
| 80 |
} |
| 81 |
if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
uint64_t width, height; |
| 87 |
if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
if (!read_mbf(stream, &height) || height > 0xFFFF || !height) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
if (size) { |
| 94 |
*size = SkISize::Make(SkToS32(width), SkToS32(height)); |
| 95 |
} |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
bool SkWbmpCodec::onRewind() { |
| 100 |
if (!this->rewindStream()) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
return read_header(this->stream(), nullptr); |
| 104 |
} |
| 105 |
|
| 106 |
bool SkWbmpCodec::readRow(uint8_t* row) { |
| 107 |
return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes; |
| 108 |
} |
| 109 |
|
| 110 |
SkWbmpCodec::SkWbmpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream) |
| 111 |
// Wbmp does not need a colorXform, so choose an arbitrary srcFormat. |
| 112 |
: INHERITED(std::move(info), skcms_PixelFormat(), |
| 113 |
std::move(stream)) |
| 114 |
, fSrcRowBytes(get_src_row_bytes(this->dimensions().width())) |
| 115 |
, fSwizzler(nullptr) |
| 116 |
{} |
| 117 |
|
| 118 |
SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const { |
| 119 |
return SkEncodedImageFormat::kWBMP; |
| 120 |
} |
| 121 |
|
| 122 |
bool SkWbmpCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, |
| 123 |
bool /*needsColorXform*/) { |
| 124 |
return valid_color_type(dst) && SkCodecPriv::ValidAlpha(dst.alphaType(), srcIsOpaque); |
| 125 |
} |
| 126 |
|
| 127 |
SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
| 128 |
void* dst, |
| 129 |
size_t rowBytes, |
| 130 |
const Options& options, |
| 131 |
int* rowsDecoded) { |
| 132 |
if (options.fSubset) { |
| 133 |
// Subsets are not supported. |
| 134 |
return kUnimplemented; |
| 135 |
} |
| 136 |
|
| 137 |
// Initialize the swizzler |
| 138 |
std::unique_ptr<SkSwizzler> swizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, info, |
| 139 |
options); |
| 140 |
SkASSERT(swizzler); |
| 141 |
|
| 142 |
// Perform the decode |
| 143 |
SkISize size = info.dimensions(); |
| 144 |
AutoTMalloc<uint8_t> src(fSrcRowBytes); |
| 145 |
void* dstRow = dst; |
| 146 |
for (int y = 0; y < size.height(); ++y) { |
| 147 |
if (!this->readRow(src.get())) { |
| 148 |
*rowsDecoded = y; |
| 149 |
return kIncompleteInput; |
| 150 |
} |
| 151 |
swizzler->swizzle(dstRow, src.get()); |
| 152 |
dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
| 153 |
} |
| 154 |
return kSuccess; |
| 155 |
} |
| 156 |
|
| 157 |
bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) { |
| 158 |
SkMemoryStream stream(buffer, bytesRead, false); |
| 159 |
return read_header(&stream, nullptr); |
| 160 |
} |
| 161 |
|
| 162 |
std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 163 |
Result* result) { |
| 164 |
SkASSERT(result); |
| 165 |
if (!stream) { |
| 166 |
*result = SkCodec::kInvalidInput; |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
SkISize size; |
| 170 |
if (!read_header(stream.get(), &size)) { |
| 171 |
// This already succeeded in IsWbmp, so this stream was corrupted in/ |
| 172 |
// after rewind. |
| 173 |
*result = kCouldNotRewind; |
| 174 |
return nullptr; |
| 175 |
} |
| 176 |
*result = kSuccess; |
| 177 |
auto info = SkEncodedInfo::Make(size.width(), size.height(), SkEncodedInfo::kGray_Color, |
| 178 |
SkEncodedInfo::kOpaque_Alpha, 1); |
| 179 |
return std::unique_ptr<SkCodec>(new SkWbmpCodec(std::move(info), std::move(stream))); |
| 180 |
} |
| 181 |
|
| 182 |
int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
| 183 |
void* dstRow = dst; |
| 184 |
for (int y = 0; y < count; ++y) { |
| 185 |
if (!this->readRow(fSrcBuffer.get())) { |
| 186 |
return y; |
| 187 |
} |
| 188 |
fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
| 189 |
dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
| 190 |
} |
| 191 |
return count; |
| 192 |
} |
| 193 |
|
| 194 |
bool SkWbmpCodec::onSkipScanlines(int count) { |
| 195 |
const size_t bytesToSkip = count * fSrcRowBytes; |
| 196 |
return this->stream()->skip(bytesToSkip) == bytesToSkip; |
| 197 |
} |
| 198 |
|
| 199 |
SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 200 |
const Options& options) { |
| 201 |
if (options.fSubset) { |
| 202 |
// Subsets are not supported. |
| 203 |
return kUnimplemented; |
| 204 |
} |
| 205 |
|
| 206 |
fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, dstInfo, options); |
| 207 |
SkASSERT(fSwizzler); |
| 208 |
|
| 209 |
fSrcBuffer.reset(fSrcRowBytes); |
| 210 |
|
| 211 |
return kSuccess; |
| 212 |
} |
| 213 |
|
| 214 |
namespace SkWbmpDecoder { |
| 215 |
bool IsWbmp(const void* data, size_t len) { |
| 216 |
return SkWbmpCodec::IsWbmp(data, len); |
| 217 |
} |
| 218 |
|
| 219 |
std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream, |
| 220 |
SkCodec::Result* outResult, |
| 221 |
SkCodecs::DecodeContext) { |
| 222 |
SkCodec::Result resultStorage; |
| 223 |
if (!outResult) { |
| 224 |
outResult = &resultStorage; |
| 225 |
} |
| 226 |
return SkWbmpCodec::MakeFromStream(std::move(stream), outResult); |
| 227 |
} |
| 228 |
|
| 229 |
std::unique_ptr<SkCodec> Decode(sk_sp<const SkData> data, |
| 230 |
SkCodec::Result* outResult, |
| 231 |
SkCodecs::DecodeContext) { |
| 232 |
if (!data) { |
| 233 |
if (outResult) { |
| 234 |
*outResult = SkCodec::kInvalidInput; |
| 235 |
} |
| 236 |
return nullptr; |
| 237 |
} |
| 238 |
return Decode(SkMemoryStream::Make(std::move(data)), outResult, nullptr); |
| 239 |
} |
| 240 |
} // namespace SkWbmpDecoder |