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" |
| 9 | #include "SkBitmap.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
| 11 | #include "SkImageEncoder.h" |
| 12 | #include "SkOSFile.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 13 | #include "SkPicture.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 15 | #include "SkString.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 16 | |
tfarina@chromium.org | 5a064e3 | 2014-01-23 02:02:57 +0000 | [diff] [blame] | 17 | static 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.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 25 | namespace sk_tools { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 26 | 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.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 41 | 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.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 52 | void make_filepath(SkString* path, const SkString& dir, const SkString& name) { |
| 53 | size_t len = dir.size(); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 54 | path->set(dir); |
| 55 | if (0 < len && '/' != dir[len - 1]) { |
| 56 | path->append("/"); |
| 57 | } |
| 58 | path->append(name); |
| 59 | } |
| 60 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 61 | 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.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 94 | bool is_percentage(const char* const string) { |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 95 | SkString skString(string); |
| 96 | return skString.endsWith("%"); |
| 97 | } |
| 98 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 99 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
| 100 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 101 | bitmap->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 102 | bitmap->eraseColor(SK_ColorTRANSPARENT); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 103 | } |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 104 | |
| 105 | bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath, |
| 106 | const char *subdirOrNull, const SkString& baseName) { |
| 107 | SkString partialPath; |
| 108 | if (subdirOrNull) { |
| 109 | partialPath = SkOSPath::SkPathJoin(dirPath.c_str(), subdirOrNull); |
| 110 | sk_mkdir(partialPath.c_str()); |
| 111 | } else { |
| 112 | partialPath.set(dirPath); |
| 113 | } |
| 114 | SkString fullPath = SkOSPath::SkPathJoin(partialPath.c_str(), baseName.c_str()); |
| 115 | if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) { |
| 116 | return true; |
| 117 | } else { |
| 118 | SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | } // namespace sk_tools |