blob: 7a5e156fb23173aa2e3a31bb3d46cd20451363f6 [file] [log] [blame]
twiz@google.com6cf53032012-06-22 18:55:55 +00001/*
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.org9299ede2012-08-21 19:05:08 +00009#include "SkColorPriv.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000010#include "SkBitmap.h"
11#include "SkPicture.h"
12#include "SkString.h"
13#include "SkStream.h"
14
15namespace sk_tools {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000016 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.com6cf53032012-06-22 18:55:55 +000030
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000031 void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
32 size_t len = dir.size();
twiz@google.com6cf53032012-06-22 18:55:55 +000033 path->set(dir);
34 if (0 < len && '/' != dir[len - 1]) {
35 path->append("/");
36 }
37 path->append(name);
38 }
39
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000040 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.com58b4ead2012-08-31 16:15:22 +000083 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000084 SkString skString(string);
85 return skString.endsWith("%");
86 }
87
twiz@google.com6cf53032012-06-22 18:55:55 +000088 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
89 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
90 bitmap->allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000091 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +000092 }
twiz@google.com6cf53032012-06-22 18:55:55 +000093}