blob: 224bf3f66e1a9ca1dc5257a65984b6f105575840 [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"
Ben Wagnerbf111d72016-11-07 18:05:29 -050015#include "SkOSPath.h"
bungeman3ffa1262015-04-30 17:12:58 -040016#include "SkStream.h"
17#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070018
19DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
20
21SkString GetResourcePath(const char* resource) {
tfarinaa8e2e152014-07-28 19:26:58 -070022 return SkOSPath::Join(FLAGS_resourcePath[0], resource);
tfarinabcbc1782014-06-18 14:32:48 -070023}
caryclark936b7342014-07-11 12:14:51 -070024
25void SetResourcePath(const char* resource) {
26 FLAGS_resourcePath.set(0, resource);
27}
halcanary30b83d42014-10-26 05:23:53 -070028
29bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) {
30 SkString resourcePath = GetResourcePath(resource);
bungeman38d909e2016-08-02 14:40:46 -070031 sk_sp<SkData> resourceData(SkData::MakeFromFileName(resourcePath.c_str()));
Mike Reed185130c2017-02-15 15:14:16 -050032 std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(resourceData));
Brian Osman06826d92016-11-29 15:10:13 -050033 if (!gen) {
34 return false;
35 }
Brian Osman7992da32016-11-18 11:28:24 -050036 SkPMColor ctStorage[256];
Mike Reed6b3155c2017-04-03 14:41:44 -040037 auto ctable = SkColorTable::Make(ctStorage, 256);
Brian Osman7992da32016-11-18 11:28:24 -050038 int count = ctable->count();
Mike Reed6b3155c2017-04-03 14:41:44 -040039 // ICK -- gotta clean up this pattern of writing to the ctable
40 return dst->tryAllocPixels(gen->getInfo(), ctable) &&
Brian Osman7992da32016-11-18 11:28:24 -050041 gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(),
42 const_cast<SkPMColor*>(ctable->readColors()), &count);
halcanary30b83d42014-10-26 05:23:53 -070043}
bungeman3ffa1262015-04-30 17:12:58 -040044
reed9ce9d672016-03-17 10:51:11 -070045sk_sp<SkImage> GetResourceAsImage(const char* resource) {
halcanary2f0a7282015-08-21 07:47:23 -070046 SkString path = GetResourcePath(resource);
bungeman38d909e2016-08-02 14:40:46 -070047 sk_sp<SkData> resourceData(SkData::MakeFromFileName(path.c_str()));
reed9ce9d672016-03-17 10:51:11 -070048 return SkImage::MakeFromEncoded(resourceData);
halcanary2f0a7282015-08-21 07:47:23 -070049}
50
bungeman3ffa1262015-04-30 17:12:58 -040051SkStreamAsset* GetResourceAsStream(const char* resource) {
52 SkString resourcePath = GetResourcePath(resource);
Ben Wagner145dbcd2016-11-03 14:40:50 -040053 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
bungemanf93d7112016-09-16 06:24:20 -070054 if (!stream->isValid()) {
bungeman3ffa1262015-04-30 17:12:58 -040055 SkDebugf("Resource %s not found.\n", resource);
halcanary96fcdcc2015-08-27 07:41:13 -070056 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040057 }
bungemanf93d7112016-09-16 06:24:20 -070058 return stream.release();
bungeman3ffa1262015-04-30 17:12:58 -040059}
60
Mike Reed463c8482016-12-21 12:01:12 -050061sk_sp<SkData> GetResourceAsData(const char* resource) {
62 SkString resourcePath = GetResourcePath(resource);
Ben Wagner1c1c7402017-03-17 17:56:28 -040063 return SkData::MakeFromFileName(resourcePath.c_str());
Mike Reed463c8482016-12-21 12:01:12 -050064}
65
bungeman13b9c952016-05-12 10:09:30 -070066sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040067 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
bungeman3ffa1262015-04-30 17:12:58 -040068 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070069 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040070 }
bungeman13b9c952016-05-12 10:09:30 -070071 return SkTypeface::MakeFromStream(stream.release());
bungeman3ffa1262015-04-30 17:12:58 -040072}