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 "include/core/SkBitmap.h"
9 #include "include/core/SkData.h"
10 #include "include/core/SkImage.h"
11 #include "include/core/SkImageGenerator.h"
12 #include "include/core/SkStream.h"
13 #include "include/core/SkTypeface.h"
14 #include "src/core/SkOSFile.h"
15 #include "src/utils/SkOSPath.h"
16 #include "tools/ResourceFactory.h"
17 #include "tools/Resources.h"
18 #include "tools/flags/CommandLineFlags.h"
19
20 static DEFINE_string2(resourcePath, i, "resources",
21 "Directory with test resources: images, fonts, etc.");
22
23 sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr;
24
GetResourcePath(const char * resource)25 SkString GetResourcePath(const char* resource) {
26 return SkOSPath::Join(FLAGS_resourcePath[0], resource);
27 }
28
SetResourcePath(const char * resource)29 void SetResourcePath(const char* resource) {
30 FLAGS_resourcePath.set(0, resource);
31 }
32
DecodeDataToBitmap(sk_sp<SkData> data,SkBitmap * dst)33 bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst) {
34 std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data)));
35 return gen && dst->tryAllocPixels(gen->getInfo()) &&
36 gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes());
37 }
38
GetResourceAsStream(const char * resource)39 std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) {
40 auto data = GetResourceAsData(resource);
41 return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data)))
42 : nullptr;
43 }
44
GetResourceAsData(const char * resource)45 sk_sp<SkData> GetResourceAsData(const char* resource) {
46 if (sk_sp<SkData> data = gResourceFactory
47 ? gResourceFactory(resource)
48 : SkData::MakeFromFileName(GetResourcePath(resource).c_str())) {
49 return data;
50 }
51 SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str());
52 #ifdef SK_TOOLS_REQUIRE_RESOURCES
53 SK_ABORT("missing resource");
54 #endif
55 return nullptr;
56 }
57
MakeResourceAsTypeface(const char * resource,int ttcIndex)58 sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex) {
59 return SkTypeface::MakeFromStream(GetResourceAsStream(resource), ttcIndex);
60 }
61