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 8 #ifndef DecodeFile_DEFINED 9 #define DecodeFile_DEFINED 10 11 #include "SkBitmap.h" 12 #include "SkCodec.h" 13 #include "SkData.h" 14 #include "SkImage.h" 15 16 static inline bool decode_file(const char* filename, SkBitmap* bitmap, 17 SkColorType colorType = kN32_SkColorType, 18 bool requireUnpremul = false) { 19 sk_sp<SkData> data(SkData::MakeFromFileName(filename)); 20 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); 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 } 36 decode_file(const char filename[])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 } 41 #endif // DecodeFile_DEFINED 42