blob: 450c1fb3d701c57757bc60cf26eaac6e8eaa141b [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
msarettd15750c2016-03-18 15:48:49 -070011#include "SkBitmap.h"
12#include "SkCodec.h"
13#include "SkData.h"
reedccebcec2016-08-22 07:41:46 -070014#include "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) {
msarettd15750c2016-03-18 15:48:49 -070019 SkASSERT(kIndex_8_SkColorType != colorType);
bungeman38d909e2016-08-02 14:40:46 -070020 sk_sp<SkData> data(SkData::MakeFromFileName(filename));
Ben Wagner145dbcd2016-11-03 14:40:50 -040021 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettd15750c2016-03-18 15:48:49 -070022 if (!codec) {
23 return false;
24 }
25
26 SkImageInfo info = codec->getInfo().makeColorType(colorType);
27 if (requireUnpremul && kPremul_SkAlphaType == info.alphaType()) {
28 info = info.makeAlphaType(kUnpremul_SkAlphaType);
29 }
30
31 if (!bitmap->tryAllocPixels(info)) {
32 return false;
33 }
34
35 return SkCodec::kSuccess == codec->getPixels(info, bitmap->getPixels(), bitmap->rowBytes());
36}
reedccebcec2016-08-22 07:41:46 -070037
38static inline sk_sp<SkImage> decode_file(const char filename[]) {
39 sk_sp<SkData> data(SkData::MakeFromFileName(filename));
40 return data ? SkImage::MakeFromEncoded(data) : nullptr;
41}
Hal Canary03a7f5f2017-02-10 09:06:38 -050042#endif // DecodeFile_DEFINED