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