Home | History | Annotate | Download | only in tools
      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 "SkColorPriv.h"
     10 #include "SkBitmap.h"
     11 #include "SkPicture.h"
     12 #include "SkString.h"
     13 #include "SkStream.h"
     14 
     15 namespace sk_tools {
     16     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     }
     30 
     31     void make_filepath(SkString* path, const SkString& dir, const SkString& name) {
     32         size_t len = dir.size();
     33         path->set(dir);
     34         if (0 < len  && '/' != dir[len - 1]) {
     35             path->append("/");
     36         }
     37         path->append(name);
     38     }
     39 
     40     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 
     83     bool is_percentage(const char* const string) {
     84         SkString skString(string);
     85         return skString.endsWith("%");
     86     }
     87 
     88     void setup_bitmap(SkBitmap* bitmap, int width, int height) {
     89         bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
     90         bitmap->allocPixels();
     91         bitmap->eraseColor(SK_ColorTRANSPARENT);
     92     }
     93 }
     94