blob: 4e6c81e4173dce6d3e117dfff856ff425c5b960c [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"
9#include "SkBitmap.h"
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000010#include "SkColorPriv.h"
11#include "SkImageEncoder.h"
12#include "SkOSFile.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000013#include "SkPicture.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000014#include "SkStream.h"
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000015#include "SkString.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000016
tfarina@chromium.org5a064e32014-01-23 02:02:57 +000017static bool is_path_seperator(const char chr) {
18#if defined(SK_BUILD_FOR_WIN)
19 return chr == '\\' || chr == '/';
20#else
21 return chr == '/';
22#endif
23}
24
twiz@google.com6cf53032012-06-22 18:55:55 +000025namespace sk_tools {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000026 void force_all_opaque(const SkBitmap& bitmap) {
27 SkASSERT(NULL == bitmap.getTexture());
28 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
29 if (NULL != bitmap.getTexture() || SkBitmap::kARGB_8888_Config == bitmap.config()) {
30 return;
31 }
32
33 SkAutoLockPixels lock(bitmap);
34 for (int y = 0; y < bitmap.height(); y++) {
35 for (int x = 0; x < bitmap.width(); x++) {
36 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
37 }
38 }
39 }
twiz@google.com6cf53032012-06-22 18:55:55 +000040
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000041 void replace_char(SkString* str, const char oldChar, const char newChar) {
42 if (NULL == str) {
43 return;
44 }
45 for (size_t i = 0; i < str->size(); ++i) {
46 if (oldChar == str->operator[](i)) {
47 str->operator[](i) = newChar;
48 }
49 }
50 }
51
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000052 void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
53 size_t len = dir.size();
twiz@google.com6cf53032012-06-22 18:55:55 +000054 path->set(dir);
55 if (0 < len && '/' != dir[len - 1]) {
56 path->append("/");
57 }
58 path->append(name);
59 }
60
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000061 void get_basename(SkString* basename, const SkString& path) {
62 if (path.size() == 0) {
63 basename->reset();
64 return;
65 }
66
67 size_t end = path.size() - 1;
68
69 // Paths pointing to directories often have a trailing slash,
70 // we remove it so the name is not empty
71 if (is_path_seperator(path[end])) {
72 if (end == 0) {
73 basename->reset();
74 return;
75 }
76
77 end -= 1;
78 }
79
80 size_t i = end;
81 do {
82 --i;
83 if (is_path_seperator(path[i])) {
84 const char* basenameStart = path.c_str() + i + 1;
85 size_t basenameLength = end - i;
86 basename->set(basenameStart, basenameLength);
87 return;
88 }
89 } while (i > 0);
90
91 basename->set(path.c_str(), end + 1);
92 }
93
scroggo@google.com58b4ead2012-08-31 16:15:22 +000094 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000095 SkString skString(string);
96 return skString.endsWith("%");
97 }
98
twiz@google.com6cf53032012-06-22 18:55:55 +000099 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
reed6c225732014-06-09 19:52:07 -0700100 bitmap->allocN32Pixels(width, height);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000101 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +0000102 }
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000103
104 bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath,
105 const char *subdirOrNull, const SkString& baseName) {
106 SkString partialPath;
107 if (subdirOrNull) {
108 partialPath = SkOSPath::SkPathJoin(dirPath.c_str(), subdirOrNull);
109 sk_mkdir(partialPath.c_str());
110 } else {
111 partialPath.set(dirPath);
112 }
113 SkString fullPath = SkOSPath::SkPathJoin(partialPath.c_str(), baseName.c_str());
114 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) {
115 return true;
116 } else {
117 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
118 return false;
119 }
120 }
121
122} // namespace sk_tools