msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "SkBitmap.h" |
| 9 | #include "SkCodec.h" |
| 10 | #include "SkData.h" |
| 11 | |
| 12 | inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 13 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(SkData::MakeWithoutCopy(mem, size))); |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 14 | if (!codec) { |
| 15 | return false; |
| 16 | } |
| 17 | |
| 18 | // Construct a color table for the decode if necessary |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 19 | sk_sp<SkColorTable> colorTable(nullptr); |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 20 | SkPMColor* colorPtr = nullptr; |
| 21 | int* colorCountPtr = nullptr; |
| 22 | int maxColors = 256; |
| 23 | if (kIndex_8_SkColorType == codec->getInfo().colorType()) { |
| 24 | SkPMColor colors[256]; |
| 25 | colorTable.reset(new SkColorTable(colors, maxColors)); |
| 26 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 27 | colorCountPtr = &maxColors; |
| 28 | } |
| 29 | |
| 30 | bm->allocPixels(codec->getInfo(), nullptr, colorTable.get()); |
| 31 | const SkCodec::Result result = codec->getPixels(codec->getInfo(), bm->getPixels(), |
| 32 | bm->rowBytes(), nullptr, colorPtr, colorCountPtr); |
| 33 | return result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput; |
| 34 | } |