2016-03-01 20:12:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2017-02-10 14:06:38 +00:00
|
|
|
#ifndef CodecPriv_DEFINED
|
|
|
|
#define CodecPriv_DEFINED
|
2016-03-01 20:12:27 +00:00
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCodec.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
|
|
|
|
inline bool decode_memory(const void* mem, size_t size, SkBitmap* bm) {
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(SkData::MakeWithoutCopy(mem, size)));
|
2016-03-01 20:12:27 +00:00
|
|
|
if (!codec) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a color table for the decode if necessary
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkColorTable> colorTable(nullptr);
|
2016-03-01 20:12:27 +00:00
|
|
|
SkPMColor* colorPtr = nullptr;
|
|
|
|
int* colorCountPtr = nullptr;
|
|
|
|
int maxColors = 256;
|
|
|
|
if (kIndex_8_SkColorType == codec->getInfo().colorType()) {
|
|
|
|
SkPMColor colors[256];
|
|
|
|
colorTable.reset(new SkColorTable(colors, maxColors));
|
|
|
|
colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
|
|
|
|
colorCountPtr = &maxColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bm->allocPixels(codec->getInfo(), nullptr, colorTable.get());
|
|
|
|
const SkCodec::Result result = codec->getPixels(codec->getInfo(), bm->getPixels(),
|
|
|
|
bm->rowBytes(), nullptr, colorPtr, colorCountPtr);
|
|
|
|
return result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput;
|
|
|
|
}
|
2017-02-10 14:06:38 +00:00
|
|
|
#endif // CodecPriv_DEFINED
|