tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 1 | /* |
| 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" |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 9 | #include "SkBitmap.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "SkCommandLineFlags.h" |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 11 | #include "SkData.h" |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 12 | #include "SkImage.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 13 | #include "SkImageGenerator.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 14 | #include "SkOSFile.h" |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkTypeface.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 17 | |
| 18 | DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc."); |
| 19 | |
| 20 | SkString GetResourcePath(const char* resource) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 21 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 22 | } |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 23 | |
| 24 | void SetResourcePath(const char* resource) { |
| 25 | FLAGS_resourcePath.set(0, resource); |
| 26 | } |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 27 | |
| 28 | bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) { |
| 29 | SkString resourcePath = GetResourcePath(resource); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 30 | SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(resourcePath.c_str())); |
reed | 4d5b676 | 2015-09-13 11:03:32 -0700 | [diff] [blame] | 31 | SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(resourceData)); |
| 32 | return gen && gen->tryGenerateBitmap(dst); |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 33 | } |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 34 | |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 35 | SkImage* 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 | |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 41 | SkStreamAsset* 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | SkTypeface* GetResourceAsTypeface(const char* resource) { |
| 53 | SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream(resource)); |
| 54 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 56 | } |
reed | 933d986 | 2015-06-01 12:51:23 -0700 | [diff] [blame] | 57 | return SkTypeface::CreateFromStream(stream.detach()); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 58 | } |