twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 9 | #include "SkColorPriv.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkPicture.h" |
| 12 | #include "SkString.h" |
| 13 | #include "SkStream.h" |
| 14 | |
tfarina@chromium.org | 5a064e3 | 2014-01-23 02:02:57 +0000 | [diff] [blame] | 15 | static 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.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 23 | namespace sk_tools { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 24 | 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.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 39 | 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.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 50 | void make_filepath(SkString* path, const SkString& dir, const SkString& name) { |
| 51 | size_t len = dir.size(); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 52 | path->set(dir); |
| 53 | if (0 < len && '/' != dir[len - 1]) { |
| 54 | path->append("/"); |
| 55 | } |
| 56 | path->append(name); |
| 57 | } |
| 58 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 59 | 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.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 92 | bool is_percentage(const char* const string) { |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 93 | SkString skString(string); |
| 94 | return skString.endsWith("%"); |
| 95 | } |
| 96 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 97 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
| 98 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 99 | bitmap->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 100 | bitmap->eraseColor(SK_ColorTRANSPARENT); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 101 | } |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 102 | } |