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 | |
| 17 | namespace sk_tools { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 18 | void force_all_opaque(const SkBitmap& bitmap) { |
| 19 | SkASSERT(NULL == bitmap.getTexture()); |
| 20 | SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); |
| 21 | if (NULL != bitmap.getTexture() || SkBitmap::kARGB_8888_Config == bitmap.config()) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | SkAutoLockPixels lock(bitmap); |
| 26 | for (int y = 0; y < bitmap.height(); y++) { |
| 27 | for (int x = 0; x < bitmap.width(); x++) { |
| 28 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 29 | } |
| 30 | } |
| 31 | } |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 33 | void replace_char(SkString* str, const char oldChar, const char newChar) { |
| 34 | if (NULL == str) { |
| 35 | return; |
| 36 | } |
| 37 | for (size_t i = 0; i < str->size(); ++i) { |
| 38 | if (oldChar == str->operator[](i)) { |
| 39 | str->operator[](i) = newChar; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 44 | void make_filepath(SkString* path, const SkString& dir, const SkString& name) { |
| 45 | size_t len = dir.size(); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 46 | path->set(dir); |
| 47 | if (0 < len && '/' != dir[len - 1]) { |
| 48 | path->append("/"); |
| 49 | } |
| 50 | path->append(name); |
| 51 | } |
| 52 | |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 53 | bool is_percentage(const char* const string) { |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 54 | SkString skString(string); |
| 55 | return skString.endsWith("%"); |
| 56 | } |
| 57 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 58 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 59 | bitmap->allocN32Pixels(width, height); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 60 | bitmap->eraseColor(SK_ColorTRANSPARENT); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 61 | } |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 62 | |
| 63 | bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath, |
| 64 | const char *subdirOrNull, const SkString& baseName) { |
| 65 | SkString partialPath; |
| 66 | if (subdirOrNull) { |
| 67 | partialPath = SkOSPath::SkPathJoin(dirPath.c_str(), subdirOrNull); |
| 68 | sk_mkdir(partialPath.c_str()); |
| 69 | } else { |
| 70 | partialPath.set(dirPath); |
| 71 | } |
| 72 | SkString fullPath = SkOSPath::SkPathJoin(partialPath.c_str(), baseName.c_str()); |
| 73 | if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) { |
| 74 | return true; |
| 75 | } else { |
| 76 | SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } // namespace sk_tools |