blob: 608b03cff650a07acc2410b1eaeb7ddaa052d7b1 [file] [log] [blame]
tfarinabcbc1782014-06-18 14:32:48 -07001/*
2 * Copyright 2014 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 "Resources.h"
halcanary30b83d42014-10-26 05:23:53 -07009#include "SkBitmap.h"
tfarinabcbc1782014-06-18 14:32:48 -070010#include "SkCommandLineFlags.h"
halcanary30b83d42014-10-26 05:23:53 -070011#include "SkData.h"
reed5965c8a2015-01-07 18:04:45 -080012#include "SkImageGenerator.h"
tfarinabcbc1782014-06-18 14:32:48 -070013#include "SkOSFile.h"
bungeman3ffa1262015-04-30 17:12:58 -040014#include "SkStream.h"
15#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070016
17DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
18
19SkString GetResourcePath(const char* resource) {
tfarinaa8e2e152014-07-28 19:26:58 -070020 return SkOSPath::Join(FLAGS_resourcePath[0], resource);
tfarinabcbc1782014-06-18 14:32:48 -070021}
caryclark936b7342014-07-11 12:14:51 -070022
23void SetResourcePath(const char* resource) {
24 FLAGS_resourcePath.set(0, resource);
25}
halcanary30b83d42014-10-26 05:23:53 -070026
27bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) {
28 SkString resourcePath = GetResourcePath(resource);
bungeman3ffa1262015-04-30 17:12:58 -040029 SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(resourcePath.c_str()));
reed5965c8a2015-01-07 18:04:45 -080030 return resourceData && SkInstallDiscardablePixelRef(resourceData, dst);
halcanary30b83d42014-10-26 05:23:53 -070031}
bungeman3ffa1262015-04-30 17:12:58 -040032
33SkStreamAsset* GetResourceAsStream(const char* resource) {
34 SkString resourcePath = GetResourcePath(resource);
35 SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
36 if (stream->isValid()) {
37 return stream.detach();
38 } else {
39 SkDebugf("Resource %s not found.\n", resource);
40 return NULL;
41 }
42}
43
44SkTypeface* GetResourceAsTypeface(const char* resource) {
45 SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream(resource));
46 if (!stream) {
47 return NULL;
48 }
reed933d9862015-06-01 12:51:23 -070049 return SkTypeface::CreateFromStream(stream.detach());
bungeman3ffa1262015-04-30 17:12:58 -040050}