aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/DecodeFile.h
diff options
context:
space:
mode:
authorGravatar msarett <msarett@google.com>2016-03-18 15:48:49 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-03-18 15:48:49 -0700
commitd15750c0c7766ecab7022ea9b2f9e89a9132cbc2 (patch)
treee697c7285402a9b32a1b5b390e20e73a253081c0 /samplecode/DecodeFile.h
parent07ec54d8e6c0e7e6ea11cde867c00bd29da79063 (diff)
Remove uses of SkImageDecoder from samplecode
Diffstat (limited to 'samplecode/DecodeFile.h')
-rw-r--r--samplecode/DecodeFile.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/samplecode/DecodeFile.h b/samplecode/DecodeFile.h
new file mode 100644
index 0000000000..26d5d2dc26
--- /dev/null
+++ b/samplecode/DecodeFile.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBitmap.h"
+#include "SkCodec.h"
+#include "SkData.h"
+
+inline bool decode_file(const char* filename, SkBitmap* bitmap,
+ SkColorType colorType = kN32_SkColorType, bool requireUnpremul = false) {
+ SkASSERT(kIndex_8_SkColorType != colorType);
+ SkAutoTUnref<SkData> data(SkData::NewFromFileName(filename));
+ SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
+ if (!codec) {
+ return false;
+ }
+
+ SkImageInfo info = codec->getInfo().makeColorType(colorType);
+ if (requireUnpremul && kPremul_SkAlphaType == info.alphaType()) {
+ info = info.makeAlphaType(kUnpremul_SkAlphaType);
+ }
+
+ if (!bitmap->tryAllocPixels(info)) {
+ return false;
+ }
+
+ return SkCodec::kSuccess == codec->getPixels(info, bitmap->getPixels(), bitmap->rowBytes());
+}