blob: 0c6e4b4adf2f394eefb646a7ad0666afb574ba99 [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 Osman06826d92016-11-29 15:10:13 -050033 if (!gen) {
34 return false;
35 }
Brian Osman7992da32016-11-18 11:28:24 -050036 SkPMColor ctStorage[256];
37 sk_sp<SkColorTable> ctable(new SkColorTable(ctStorage, 256));
38 int count = ctable->count();
39 return dst->tryAllocPixels(gen->getInfo(), nullptr, ctable.get()) &&
40 gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(),
41 const_cast<SkPMColor*>(ctable->readColors()), &count);
halcanary30b83d42014-10-26 05:23:53 -070042}
bungeman3ffa1262015-04-30 17:12:58 -040043
reed9ce9d672016-03-17 10:51:11 -070044sk_sp<SkImage> GetResourceAsImage(const char* resource) {
halcanary2f0a7282015-08-21 07:47:23 -070045 SkString path = GetResourcePath(resource);
bungeman38d909e2016-08-02 14:40:46 -070046 sk_sp<SkData> resourceData(SkData::MakeFromFileName(path.c_str()));
reed9ce9d672016-03-17 10:51:11 -070047 return SkImage::MakeFromEncoded(resourceData);
halcanary2f0a7282015-08-21 07:47:23 -070048}
49
bungeman3ffa1262015-04-30 17:12:58 -040050SkStreamAsset* GetResourceAsStream(const char* resource) {
51 SkString resourcePath = GetResourcePath(resource);
Ben Wagner145dbcd2016-11-03 14:40:50 -040052 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
bungemanf93d7112016-09-16 06:24:20 -070053 if (!stream->isValid()) {
bungeman3ffa1262015-04-30 17:12:58 -040054 SkDebugf("Resource %s not found.\n", resource);
halcanary96fcdcc2015-08-27 07:41:13 -070055 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040056 }
bungemanf93d7112016-09-16 06:24:20 -070057 return stream.release();
bungeman3ffa1262015-04-30 17:12:58 -040058}
59
bungeman13b9c952016-05-12 10:09:30 -070060sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040061 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
bungeman3ffa1262015-04-30 17:12:58 -040062 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070063 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040064 }
bungeman13b9c952016-05-12 10:09:30 -070065 return SkTypeface::MakeFromStream(stream.release());
bungeman3ffa1262015-04-30 17:12:58 -040066}