blob: a8907e7ecad5606348bbfa80c8f779aec3f645cb [file] [log] [blame]
msarettd15750c2016-03-18 15:48:49 -07001/*
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
Hal Canary03a7f5f2017-02-10 09:06:38 -05008#ifndef DecodeFile_DEFINED
9#define DecodeFile_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/codec/SkCodec.h"
12#include "include/core/SkBitmap.h"
13#include "include/core/SkData.h"
14#include "include/core/SkImage.h"
msarettd15750c2016-03-18 15:48:49 -070015
reedccebcec2016-08-22 07:41:46 -070016static inline bool decode_file(const char* filename, SkBitmap* bitmap,
17 SkColorType colorType = kN32_SkColorType,
18 bool requireUnpremul = false) {
bungeman38d909e2016-08-02 14:40:46 -070019 sk_sp<SkData> data(SkData::MakeFromFileName(filename));
Mike Reedede7bac2017-07-23 15:30:02 -040020 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
msarettd15750c2016-03-18 15:48:49 -070021 if (!codec) {
22 return false;
23 }
24
25 SkImageInfo info = codec->getInfo().makeColorType(colorType);
26 if (requireUnpremul && kPremul_SkAlphaType == info.alphaType()) {
27 info = info.makeAlphaType(kUnpremul_SkAlphaType);
28 }
29
30 if (!bitmap->tryAllocPixels(info)) {
31 return false;
32 }
33
34 return SkCodec::kSuccess == codec->getPixels(info, bitmap->getPixels(), bitmap->rowBytes());
35}
reedccebcec2016-08-22 07:41:46 -070036
37static inline sk_sp<SkImage> decode_file(const char filename[]) {
38 sk_sp<SkData> data(SkData::MakeFromFileName(filename));
39 return data ? SkImage::MakeFromEncoded(data) : nullptr;
40}
Hal Canary03a7f5f2017-02-10 09:06:38 -050041#endif // DecodeFile_DEFINED