skia2/samplecode/DecodeFile.h
Hal Canary b021f3e3fb Sample/Filter2View: make it work without /skimages
Change-Id: I8958e026ca9e2c5a6c99a7a1cd1ec97088c25fe9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/225548
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
2019-07-08 17:05:23 +00:00

47 lines
1.5 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef DecodeFile_DEFINED
#define DecodeFile_DEFINED
#include "include/codec/SkCodec.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
static inline bool decode_file(sk_sp<SkData> data, SkBitmap* bitmap,
SkColorType colorType = kN32_SkColorType,
bool requireUnpremul = false) {
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(data));
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());
}
static inline bool decode_file(const char* filename, SkBitmap* bitmap,
SkColorType colorType = kN32_SkColorType,
bool requireUnpremul = false) {
return decode_file(SkData::MakeFromFileName(filename), bitmap, colorType, requireUnpremul);
}
static inline sk_sp<SkImage> decode_file(const char filename[]) {
sk_sp<SkData> data(SkData::MakeFromFileName(filename));
return data ? SkImage::MakeFromEncoded(data) : nullptr;
}
#endif // DecodeFile_DEFINED