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())); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 32 | std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(resourceData)); |
Brian Osman | 06826d9 | 2016-11-29 15:10:13 -0500 | [diff] [blame] | 33 | if (!gen) { |
| 34 | return false; |
| 35 | } |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 36 | SkPMColor ctStorage[256]; |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 37 | auto ctable = SkColorTable::Make(ctStorage, 256); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 38 | int count = ctable->count(); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 39 | // ICK -- gotta clean up this pattern of writing to the ctable |
| 40 | return dst->tryAllocPixels(gen->getInfo(), ctable) && |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 41 | gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(), |
| 42 | const_cast<SkPMColor*>(ctable->readColors()), &count); |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 43 | } |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 44 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 45 | sk_sp<SkImage> GetResourceAsImage(const char* resource) { |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 46 | SkString path = GetResourcePath(resource); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 47 | sk_sp<SkData> resourceData(SkData::MakeFromFileName(path.c_str())); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 48 | return SkImage::MakeFromEncoded(resourceData); |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 49 | } |
| 50 | |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 51 | SkStreamAsset* GetResourceAsStream(const char* resource) { |
| 52 | SkString resourcePath = GetResourcePath(resource); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 53 | std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str())); |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 54 | if (!stream->isValid()) { |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 55 | SkDebugf("Resource %s not found.\n", resource); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 57 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 58 | return stream.release(); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 61 | sk_sp<SkData> GetResourceAsData(const char* resource) { |
| 62 | SkString resourcePath = GetResourcePath(resource); |
Ben Wagner | 1c1c740 | 2017-03-17 17:56:28 -0400 | [diff] [blame] | 63 | return SkData::MakeFromFileName(resourcePath.c_str()); |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 64 | } |
| 65 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 66 | sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 67 | std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource)); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 68 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 70 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 71 | return SkTypeface::MakeFromStream(stream.release()); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 72 | } |