2014-06-18 21:32:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Resources.h"
|
2014-10-26 12:23:53 +00:00
|
|
|
#include "SkBitmap.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
2014-10-26 12:23:53 +00:00
|
|
|
#include "SkData.h"
|
2015-08-21 14:47:23 +00:00
|
|
|
#include "SkImage.h"
|
2015-01-08 02:04:45 +00:00
|
|
|
#include "SkImageGenerator.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "SkOSFile.h"
|
2016-11-07 23:05:29 +00:00
|
|
|
#include "SkOSPath.h"
|
2015-04-30 21:12:58 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTypeface.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
|
|
|
DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
|
|
|
|
|
|
|
|
SkString GetResourcePath(const char* resource) {
|
2014-07-29 02:26:58 +00:00
|
|
|
return SkOSPath::Join(FLAGS_resourcePath[0], resource);
|
2014-06-18 21:32:48 +00:00
|
|
|
}
|
2014-07-11 19:14:51 +00:00
|
|
|
|
|
|
|
void SetResourcePath(const char* resource) {
|
|
|
|
FLAGS_resourcePath.set(0, resource);
|
|
|
|
}
|
2014-10-26 12:23:53 +00:00
|
|
|
|
|
|
|
bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) {
|
|
|
|
SkString resourcePath = GetResourcePath(resource);
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> resourceData(SkData::MakeFromFileName(resourcePath.c_str()));
|
2017-02-15 20:14:16 +00:00
|
|
|
std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(resourceData));
|
2016-11-29 20:10:13 +00:00
|
|
|
if (!gen) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 16:28:24 +00:00
|
|
|
SkPMColor ctStorage[256];
|
|
|
|
sk_sp<SkColorTable> ctable(new SkColorTable(ctStorage, 256));
|
|
|
|
int count = ctable->count();
|
|
|
|
return dst->tryAllocPixels(gen->getInfo(), nullptr, ctable.get()) &&
|
|
|
|
gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(),
|
|
|
|
const_cast<SkPMColor*>(ctable->readColors()), &count);
|
2014-10-26 12:23:53 +00:00
|
|
|
}
|
2015-04-30 21:12:58 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> GetResourceAsImage(const char* resource) {
|
2015-08-21 14:47:23 +00:00
|
|
|
SkString path = GetResourcePath(resource);
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> resourceData(SkData::MakeFromFileName(path.c_str()));
|
2016-03-17 17:51:11 +00:00
|
|
|
return SkImage::MakeFromEncoded(resourceData);
|
2015-08-21 14:47:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 21:12:58 +00:00
|
|
|
SkStreamAsset* GetResourceAsStream(const char* resource) {
|
|
|
|
SkString resourcePath = GetResourcePath(resource);
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
|
2016-09-16 13:24:20 +00:00
|
|
|
if (!stream->isValid()) {
|
2015-04-30 21:12:58 +00:00
|
|
|
SkDebugf("Resource %s not found.\n", resource);
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-04-30 21:12:58 +00:00
|
|
|
}
|
2016-09-16 13:24:20 +00:00
|
|
|
return stream.release();
|
2015-04-30 21:12:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 17:01:12 +00:00
|
|
|
sk_sp<SkData> GetResourceAsData(const char* resource) {
|
|
|
|
SkString resourcePath = GetResourcePath(resource);
|
|
|
|
std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
|
|
|
|
if (!stream->isValid()) {
|
|
|
|
SkDebugf("Resource %s not found.\n", resource);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
size_t bytes = stream->getLength();
|
|
|
|
sk_sp<SkData> data = SkData::MakeUninitialized(bytes);
|
|
|
|
stream->read(data->writable_data(), bytes);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
|
2015-04-30 21:12:58 +00:00
|
|
|
if (!stream) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-04-30 21:12:58 +00:00
|
|
|
}
|
2016-05-12 17:09:30 +00:00
|
|
|
return SkTypeface::MakeFromStream(stream.release());
|
2015-04-30 21:12:58 +00:00
|
|
|
}
|