msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 8 | #ifndef DecodeFile_DEFINED |
| 9 | #define DecodeFile_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/codec/SkCodec.h" |
| 12 | #include "include/core/SkBitmap.h" |
| 13 | #include "include/core/SkData.h" |
| 14 | #include "include/core/SkImage.h" |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 15 | |
reed | ccebcec | 2016-08-22 07:41:46 -0700 | [diff] [blame] | 16 | static inline bool decode_file(const char* filename, SkBitmap* bitmap, |
| 17 | SkColorType colorType = kN32_SkColorType, |
| 18 | bool requireUnpremul = false) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 19 | sk_sp<SkData> data(SkData::MakeFromFileName(filename)); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 20 | std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 21 | 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 | } |
reed | ccebcec | 2016-08-22 07:41:46 -0700 | [diff] [blame] | 36 | |
| 37 | static 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 Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 41 | #endif // DecodeFile_DEFINED |