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" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 15 | #include "SkOSPath.h" |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | #include "SkTypeface.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 18 | |
| 19 | DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc."); |
| 20 | |
| 21 | SkString GetResourcePath(const char* resource) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 22 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 23 | } |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 24 | |
| 25 | void SetResourcePath(const char* resource) { |
| 26 | FLAGS_resourcePath.set(0, resource); |
| 27 | } |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 28 | |
| 29 | bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) { |
| 30 | SkString resourcePath = GetResourcePath(resource); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 31 | sk_sp<SkData> resourceData(SkData::MakeFromFileName(resourcePath.c_str())); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 32 | std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(resourceData.get())); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 33 | SkPMColor ctStorage[256]; |
| 34 | sk_sp<SkColorTable> ctable(new SkColorTable(ctStorage, 256)); |
| 35 | int count = ctable->count(); |
| 36 | return dst->tryAllocPixels(gen->getInfo(), nullptr, ctable.get()) && |
| 37 | gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(), |
| 38 | const_cast<SkPMColor*>(ctable->readColors()), &count); |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 39 | } |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 40 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 41 | sk_sp<SkImage> GetResourceAsImage(const char* resource) { |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 42 | SkString path = GetResourcePath(resource); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 43 | sk_sp<SkData> resourceData(SkData::MakeFromFileName(path.c_str())); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 44 | return SkImage::MakeFromEncoded(resourceData); |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 45 | } |
| 46 | |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 47 | SkStreamAsset* GetResourceAsStream(const char* resource) { |
| 48 | SkString resourcePath = GetResourcePath(resource); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 49 | std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str())); |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 50 | if (!stream->isValid()) { |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 51 | SkDebugf("Resource %s not found.\n", resource); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 53 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 54 | return stream.release(); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 55 | } |
| 56 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 57 | sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 58 | std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource)); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 59 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 61 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 62 | return SkTypeface::MakeFromStream(stream.release()); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 63 | } |