blob: 515dbdec2aa0affd1e958c27839aff884abdecb0 [file] [log] [blame]
msarett7f7ec202016-03-01 12:12:27 -08001/*
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
12inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040013 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(SkData::MakeWithoutCopy(mem, size)));
msarett7f7ec202016-03-01 12:12:27 -080014 if (!codec) {
15 return false;
16 }
17
18 // Construct a color table for the decode if necessary
Hal Canary342b7ac2016-11-04 11:49:42 -040019 sk_sp<SkColorTable> colorTable(nullptr);
msarett7f7ec202016-03-01 12:12:27 -080020 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}