• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Resources.h"
9 #include "SkBitmap.h"
10 #include "SkCommandLineFlags.h"
11 #include "SkData.h"
12 #include "SkImage.h"
13 #include "SkImageGenerator.h"
14 #include "SkOSFile.h"
15 #include "SkStream.h"
16 #include "SkTypeface.h"
17 
18 DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
19 
GetResourcePath(const char * resource)20 SkString GetResourcePath(const char* resource) {
21     return SkOSPath::Join(FLAGS_resourcePath[0], resource);
22 }
23 
SetResourcePath(const char * resource)24 void SetResourcePath(const char* resource) {
25     FLAGS_resourcePath.set(0, resource);
26 }
27 
GetResourceAsBitmap(const char * resource,SkBitmap * dst)28 bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) {
29     SkString resourcePath = GetResourcePath(resource);
30     SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(resourcePath.c_str()));
31     SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(resourceData));
32     return gen && gen->tryGenerateBitmap(dst);
33 }
34 
GetResourceAsImage(const char * resource)35 SkImage* GetResourceAsImage(const char* resource) {
36     SkString path = GetResourcePath(resource);
37     SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(path.c_str()));
38     return SkImage::NewFromEncoded(resourceData);
39 }
40 
GetResourceAsStream(const char * resource)41 SkStreamAsset* GetResourceAsStream(const char* resource) {
42     SkString resourcePath = GetResourcePath(resource);
43     SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
44     if (stream->isValid()) {
45         return stream.detach();
46     } else {
47         SkDebugf("Resource %s not found.\n", resource);
48         return nullptr;
49     }
50 }
51 
GetResourceAsTypeface(const char * resource)52 SkTypeface* GetResourceAsTypeface(const char* resource) {
53     SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream(resource));
54     if (!stream) {
55         return nullptr;
56     }
57     return SkTypeface::CreateFromStream(stream.detach());
58 }
59