blob: 26d5d2dc2631407b524ced92c462c189e6a4c4ae [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
8#include "SkBitmap.h"
9#include "SkCodec.h"
10#include "SkData.h"
11
12inline bool decode_file(const char* filename, SkBitmap* bitmap,
13 SkColorType colorType = kN32_SkColorType, bool requireUnpremul = false) {
14 SkASSERT(kIndex_8_SkColorType != colorType);
15 SkAutoTUnref<SkData> data(SkData::NewFromFileName(filename));
16 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
17 if (!codec) {
18 return false;
19 }
20
21 SkImageInfo info = codec->getInfo().makeColorType(colorType);
22 if (requireUnpremul && kPremul_SkAlphaType == info.alphaType()) {
23 info = info.makeAlphaType(kUnpremul_SkAlphaType);
24 }
25
26 if (!bitmap->tryAllocPixels(info)) {
27 return false;
28 }
29
30 return SkCodec::kSuccess == codec->getPixels(info, bitmap->getPixels(), bitmap->rowBytes());
31}