blob: 4bcdf8a964620fe974d2eda915c4f67f61341844 [file] [log] [blame]
twiz@google.com6cf53032012-06-22 18:55:55 +00001/*
2 * Copyright 2012 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 "picture_utils.h"
keyar@chromium.org9299ede2012-08-21 19:05:08 +00009#include "SkColorPriv.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000010#include "SkBitmap.h"
11#include "SkPicture.h"
12#include "SkString.h"
13#include "SkStream.h"
14
tfarina@chromium.org5a064e32014-01-23 02:02:57 +000015static bool is_path_seperator(const char chr) {
16#if defined(SK_BUILD_FOR_WIN)
17 return chr == '\\' || chr == '/';
18#else
19 return chr == '/';
20#endif
21}
22
twiz@google.com6cf53032012-06-22 18:55:55 +000023namespace sk_tools {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000024 void force_all_opaque(const SkBitmap& bitmap) {
25 SkASSERT(NULL == bitmap.getTexture());
26 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
27 if (NULL != bitmap.getTexture() || SkBitmap::kARGB_8888_Config == bitmap.config()) {
28 return;
29 }
30
31 SkAutoLockPixels lock(bitmap);
32 for (int y = 0; y < bitmap.height(); y++) {
33 for (int x = 0; x < bitmap.width(); x++) {
34 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
35 }
36 }
37 }
twiz@google.com6cf53032012-06-22 18:55:55 +000038
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000039 void replace_char(SkString* str, const char oldChar, const char newChar) {
40 if (NULL == str) {
41 return;
42 }
43 for (size_t i = 0; i < str->size(); ++i) {
44 if (oldChar == str->operator[](i)) {
45 str->operator[](i) = newChar;
46 }
47 }
48 }
49
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000050 void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
51 size_t len = dir.size();
twiz@google.com6cf53032012-06-22 18:55:55 +000052 path->set(dir);
53 if (0 < len && '/' != dir[len - 1]) {
54 path->append("/");
55 }
56 path->append(name);
57 }
58
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000059 void get_basename(SkString* basename, const SkString& path) {
60 if (path.size() == 0) {
61 basename->reset();
62 return;
63 }
64
65 size_t end = path.size() - 1;
66
67 // Paths pointing to directories often have a trailing slash,
68 // we remove it so the name is not empty
69 if (is_path_seperator(path[end])) {
70 if (end == 0) {
71 basename->reset();
72 return;
73 }
74
75 end -= 1;
76 }
77
78 size_t i = end;
79 do {
80 --i;
81 if (is_path_seperator(path[i])) {
82 const char* basenameStart = path.c_str() + i + 1;
83 size_t basenameLength = end - i;
84 basename->set(basenameStart, basenameLength);
85 return;
86 }
87 } while (i > 0);
88
89 basename->set(path.c_str(), end + 1);
90 }
91
scroggo@google.com58b4ead2012-08-31 16:15:22 +000092 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000093 SkString skString(string);
94 return skString.endsWith("%");
95 }
96
twiz@google.com6cf53032012-06-22 18:55:55 +000097 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
98 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
99 bitmap->allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000100 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +0000101 }
twiz@google.com6cf53032012-06-22 18:55:55 +0000102}