2016-03-18 22:48:49 +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 DecodeFile_DEFINED
|
|
|
|
#define DecodeFile_DEFINED
|
|
|
|
|
2016-03-18 22:48:49 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCodec.h"
|
|
|
|
#include "SkData.h"
|
2016-08-22 14:41:46 +00:00
|
|
|
#include "SkImage.h"
|
2016-03-18 22:48:49 +00:00
|
|
|
|
2016-08-22 14:41:46 +00:00
|
|
|
static inline bool decode_file(const char* filename, SkBitmap* bitmap,
|
|
|
|
SkColorType colorType = kN32_SkColorType,
|
|
|
|
bool requireUnpremul = false) {
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeFromFileName(filename));
|
2017-07-23 19:30:02 +00:00
|
|
|
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
|
2016-03-18 22:48:49 +00:00
|
|
|
if (!codec) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkImageInfo info = codec->getInfo().makeColorType(colorType);
|
|
|
|
if (requireUnpremul && kPremul_SkAlphaType == info.alphaType()) {
|
|
|
|
info = info.makeAlphaType(kUnpremul_SkAlphaType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bitmap->tryAllocPixels(info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SkCodec::kSuccess == codec->getPixels(info, bitmap->getPixels(), bitmap->rowBytes());
|
|
|
|
}
|
2016-08-22 14:41:46 +00:00
|
|
|
|
|
|
|
static inline sk_sp<SkImage> decode_file(const char filename[]) {
|
|
|
|
sk_sp<SkData> data(SkData::MakeFromFileName(filename));
|
|
|
|
return data ? SkImage::MakeFromEncoded(data) : nullptr;
|
|
|
|
}
|
2017-02-10 14:06:38 +00:00
|
|
|
#endif // DecodeFile_DEFINED
|