blob: ee189ad7855cfa90683b4463197ee05ce20d0d0f [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
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000039 void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
40 size_t len = dir.size();
twiz@google.com6cf53032012-06-22 18:55:55 +000041 path->set(dir);
42 if (0 < len && '/' != dir[len - 1]) {
43 path->append("/");
44 }
45 path->append(name);
46 }
47
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000048 void get_basename(SkString* basename, const SkString& path) {
49 if (path.size() == 0) {
50 basename->reset();
51 return;
52 }
53
54 size_t end = path.size() - 1;
55
56 // Paths pointing to directories often have a trailing slash,
57 // we remove it so the name is not empty
58 if (is_path_seperator(path[end])) {
59 if (end == 0) {
60 basename->reset();
61 return;
62 }
63
64 end -= 1;
65 }
66
67 size_t i = end;
68 do {
69 --i;
70 if (is_path_seperator(path[i])) {
71 const char* basenameStart = path.c_str() + i + 1;
72 size_t basenameLength = end - i;
73 basename->set(basenameStart, basenameLength);
74 return;
75 }
76 } while (i > 0);
77
78 basename->set(path.c_str(), end + 1);
79 }
80
scroggo@google.com58b4ead2012-08-31 16:15:22 +000081 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000082 SkString skString(string);
83 return skString.endsWith("%");
84 }
85
twiz@google.com6cf53032012-06-22 18:55:55 +000086 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
87 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
88 bitmap->allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000089 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +000090 }
twiz@google.com6cf53032012-06-22 18:55:55 +000091}