blob: 981bfba337381dc88c2b8ee7274d21d8ef7d4131 [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()));
Ben Wagner145dbcd2016-11-03 14:40:50 -040032 std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(resourceData.get()));
Brian Osman7992da32016-11-18 11:28:24 -050033 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);
halcanary30b83d42014-10-26 05:23:53 -070039}
bungeman3ffa1262015-04-30 17:12:58 -040040
reed9ce9d672016-03-17 10:51:11 -070041sk_sp<SkImage> GetResourceAsImage(const char* resource) {
halcanary2f0a7282015-08-21 07:47:23 -070042 SkString path = GetResourcePath(resource);
bungeman38d909e2016-08-02 14:40:46 -070043 sk_sp<SkData> resourceData(SkData::MakeFromFileName(path.c_str()));
reed9ce9d672016-03-17 10:51:11 -070044 return SkImage::MakeFromEncoded(resourceData);
halcanary2f0a7282015-08-21 07:47:23 -070045}
46
bungeman3ffa1262015-04-30 17:12:58 -040047SkStreamAsset* GetResourceAsStream(const char* resource) {
48 SkString resourcePath = GetResourcePath(resource);
Ben Wagner145dbcd2016-11-03 14:40:50 -040049 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
bungemanf93d7112016-09-16 06:24:20 -070050 if (!stream->isValid()) {
bungeman3ffa1262015-04-30 17:12:58 -040051 SkDebugf("Resource %s not found.\n", resource);
halcanary96fcdcc2015-08-27 07:41:13 -070052 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040053 }
bungemanf93d7112016-09-16 06:24:20 -070054 return stream.release();
bungeman3ffa1262015-04-30 17:12:58 -040055}
56
bungeman13b9c952016-05-12 10:09:30 -070057sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040058 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
bungeman3ffa1262015-04-30 17:12:58 -040059 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070060 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040061 }
bungeman13b9c952016-05-12 10:09:30 -070062 return SkTypeface::MakeFromStream(stream.release());
bungeman3ffa1262015-04-30 17:12:58 -040063}