Preparations for adding options and different render types to render_pictures.
[platform/upstream/libSkiaSharp.git] / tools / picture_utils.cpp
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"
10 #include "SkPicture.h"
11 #include "SkString.h"
12 #include "SkStream.h"
13
14 namespace sk_tools {
15
16     void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
17         size_t len = dir.size();
18         path->set(dir);
19         if (0 < len  && '/' != dir[len - 1]) {
20             path->append("/");
21         }
22         path->append(name);
23     }
24
25     namespace {
26         bool is_path_seperator(const char chr) {
27 #if defined(SK_BUILD_FOR_WIN)
28             return chr == '\\' || chr == '/';
29 #else
30             return chr == '/';
31 #endif
32         }
33     }
34
35     void get_basename(SkString* basename, const SkString& path) {
36         if (path.size() == 0) {
37             basename->reset();
38             return;
39         }
40
41         size_t end = path.size() - 1;
42
43         // Paths pointing to directories often have a trailing slash,
44         // we remove it so the name is not empty
45         if (is_path_seperator(path[end])) {
46             if (end == 0) {
47                 basename->reset();
48                 return;
49             }
50
51             end -= 1;
52         }
53
54         size_t i = end;
55         do {
56             --i;
57             if (is_path_seperator(path[i])) {
58                   const char* basenameStart = path.c_str() + i + 1;
59                   size_t basenameLength = end - i;
60                   basename->set(basenameStart, basenameLength);
61                   return;
62             }
63         } while (i > 0);
64
65         basename->set(path.c_str(), end + 1);
66     }
67
68     void setup_bitmap(SkBitmap* bitmap, int width, int height) {
69         bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
70         bitmap->allocPixels();
71         bitmap->eraseColor(0);
72     }
73
74 }