blob: 62b3f9fd0dbaaed934b023a6028172346bbe7c53 [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"
halcanary2f0a7282015-08-21 07:47:23 -070012#include "SkImage.h"
reed5965c8a2015-01-07 18:04:45 -080013#include "SkImageGenerator.h"
tfarinabcbc1782014-06-18 14:32:48 -070014#include "SkOSFile.h"
bungeman3ffa1262015-04-30 17:12:58 -040015#include "SkStream.h"
16#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070017
18DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
19
20SkString GetResourcePath(const char* resource) {
tfarinaa8e2e152014-07-28 19:26:58 -070021 return SkOSPath::Join(FLAGS_resourcePath[0], resource);
tfarinabcbc1782014-06-18 14:32:48 -070022}
caryclark936b7342014-07-11 12:14:51 -070023
24void SetResourcePath(const char* resource) {
25 FLAGS_resourcePath.set(0, resource);
26}
halcanary30b83d42014-10-26 05:23:53 -070027
28bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) {
29 SkString resourcePath = GetResourcePath(resource);
bungeman3ffa1262015-04-30 17:12:58 -040030 SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(resourcePath.c_str()));
reed4d5b6762015-09-13 11:03:32 -070031 SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(resourceData));
32 return gen && gen->tryGenerateBitmap(dst);
halcanary30b83d42014-10-26 05:23:53 -070033}
bungeman3ffa1262015-04-30 17:12:58 -040034
halcanary2f0a7282015-08-21 07:47:23 -070035SkImage* GetResourceAsImage(const char* resource) {
36 SkString path = GetResourcePath(resource);
37 SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(path.c_str()));
38 return SkImage::NewFromEncoded(resourceData);
39}
40
bungeman3ffa1262015-04-30 17:12:58 -040041SkStreamAsset* GetResourceAsStream(const char* resource) {
42 SkString resourcePath = GetResourcePath(resource);
43 SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
44 if (stream->isValid()) {
45 return stream.detach();
46 } else {
47 SkDebugf("Resource %s not found.\n", resource);
halcanary96fcdcc2015-08-27 07:41:13 -070048 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040049 }
50}
51
52SkTypeface* GetResourceAsTypeface(const char* resource) {
53 SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream(resource));
54 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070055 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040056 }
reed933d9862015-06-01 12:51:23 -070057 return SkTypeface::CreateFromStream(stream.detach());
bungeman3ffa1262015-04-30 17:12:58 -040058}