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 | |
| 15 | namespace sk_tools { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 16 | void force_all_opaque(const SkBitmap& bitmap) { |
| 17 | SkASSERT(NULL == bitmap.getTexture()); |
| 18 | SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); |
| 19 | if (NULL != bitmap.getTexture() || SkBitmap::kARGB_8888_Config == bitmap.config()) { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | SkAutoLockPixels lock(bitmap); |
| 24 | for (int y = 0; y < bitmap.height(); y++) { |
| 25 | for (int x = 0; x < bitmap.width(); x++) { |
| 26 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 27 | } |
| 28 | } |
| 29 | } |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 30 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 31 | void make_filepath(SkString* path, const SkString& dir, const SkString& name) { |
| 32 | size_t len = dir.size(); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 33 | path->set(dir); |
| 34 | if (0 < len && '/' != dir[len - 1]) { |
| 35 | path->append("/"); |
| 36 | } |
| 37 | path->append(name); |
| 38 | } |
| 39 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | bool is_path_seperator(const char chr) { |
| 42 | #if defined(SK_BUILD_FOR_WIN) |
| 43 | return chr == '\\' || chr == '/'; |
| 44 | #else |
| 45 | return chr == '/'; |
| 46 | #endif |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void get_basename(SkString* basename, const SkString& path) { |
| 51 | if (path.size() == 0) { |
| 52 | basename->reset(); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | size_t end = path.size() - 1; |
| 57 | |
| 58 | // Paths pointing to directories often have a trailing slash, |
| 59 | // we remove it so the name is not empty |
| 60 | if (is_path_seperator(path[end])) { |
| 61 | if (end == 0) { |
| 62 | basename->reset(); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | end -= 1; |
| 67 | } |
| 68 | |
| 69 | size_t i = end; |
| 70 | do { |
| 71 | --i; |
| 72 | if (is_path_seperator(path[i])) { |
| 73 | const char* basenameStart = path.c_str() + i + 1; |
| 74 | size_t basenameLength = end - i; |
| 75 | basename->set(basenameStart, basenameLength); |
| 76 | return; |
| 77 | } |
| 78 | } while (i > 0); |
| 79 | |
| 80 | basename->set(path.c_str(), end + 1); |
| 81 | } |
| 82 | |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 83 | bool is_percentage(const char* const string) { |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 84 | SkString skString(string); |
| 85 | return skString.endsWith("%"); |
| 86 | } |
| 87 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 88 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
| 89 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 90 | bitmap->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 91 | bitmap->eraseColor(SK_ColorTRANSPARENT); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 92 | } |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 93 | } |