| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "picture_utils.h" | 8 #include "picture_utils.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkImageEncoder.h" | 11 #include "SkImageEncoder.h" |
| 12 #include "SkOSFile.h" | 12 #include "SkOSFile.h" |
| 13 #include "SkPicture.h" | 13 #include "SkPicture.h" |
| 14 #include "SkStream.h" | 14 #include "SkStream.h" |
| 15 #include "SkString.h" | 15 #include "SkString.h" |
| 16 | 16 |
| 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 | |
| 25 namespace sk_tools { | 17 namespace sk_tools { |
| 26 void force_all_opaque(const SkBitmap& bitmap) { | 18 void force_all_opaque(const SkBitmap& bitmap) { |
| 27 SkASSERT(NULL == bitmap.getTexture()); | 19 SkASSERT(NULL == bitmap.getTexture()); |
| 28 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); | 20 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); |
| 29 if (NULL != bitmap.getTexture() || SkBitmap::kARGB_8888_Config == bitmap
.config()) { | 21 if (NULL != bitmap.getTexture() || SkBitmap::kARGB_8888_Config == bitmap
.config()) { |
| 30 return; | 22 return; |
| 31 } | 23 } |
| 32 | 24 |
| 33 SkAutoLockPixels lock(bitmap); | 25 SkAutoLockPixels lock(bitmap); |
| 34 for (int y = 0; y < bitmap.height(); y++) { | 26 for (int y = 0; y < bitmap.height(); y++) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 51 | 43 |
| 52 void make_filepath(SkString* path, const SkString& dir, const SkString& name
) { | 44 void make_filepath(SkString* path, const SkString& dir, const SkString& name
) { |
| 53 size_t len = dir.size(); | 45 size_t len = dir.size(); |
| 54 path->set(dir); | 46 path->set(dir); |
| 55 if (0 < len && '/' != dir[len - 1]) { | 47 if (0 < len && '/' != dir[len - 1]) { |
| 56 path->append("/"); | 48 path->append("/"); |
| 57 } | 49 } |
| 58 path->append(name); | 50 path->append(name); |
| 59 } | 51 } |
| 60 | 52 |
| 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 | |
| 94 bool is_percentage(const char* const string) { | 53 bool is_percentage(const char* const string) { |
| 95 SkString skString(string); | 54 SkString skString(string); |
| 96 return skString.endsWith("%"); | 55 return skString.endsWith("%"); |
| 97 } | 56 } |
| 98 | 57 |
| 99 void setup_bitmap(SkBitmap* bitmap, int width, int height) { | 58 void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
| 100 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | 59 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 101 bitmap->allocPixels(); | 60 bitmap->allocPixels(); |
| 102 bitmap->eraseColor(SK_ColorTRANSPARENT); | 61 bitmap->eraseColor(SK_ColorTRANSPARENT); |
| 103 } | 62 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 SkString fullPath = SkOSPath::SkPathJoin(partialPath.c_str(), baseName.c
_str()); | 73 SkString fullPath = SkOSPath::SkPathJoin(partialPath.c_str(), baseName.c
_str()); |
| 115 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPN
G_Type, 100)) { | 74 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPN
G_Type, 100)) { |
| 116 return true; | 75 return true; |
| 117 } else { | 76 } else { |
| 118 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); | 77 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); |
| 119 return false; | 78 return false; |
| 120 } | 79 } |
| 121 } | 80 } |
| 122 | 81 |
| 123 } // namespace sk_tools | 82 } // namespace sk_tools |
| OLD | NEW |