blob: 5c120b010d98ca56ebd7fe079eb01c0740190f36 [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"
9#include "SkBitmap.h"
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000010#include "SkColorPriv.h"
11#include "SkImageEncoder.h"
12#include "SkOSFile.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000013#include "SkPicture.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000014#include "SkStream.h"
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000015#include "SkString.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000016
17namespace sk_tools {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000018 void force_all_opaque(const SkBitmap& bitmap) {
19 SkASSERT(NULL == bitmap.getTexture());
reed0689d7b2014-06-14 05:30:20 -070020 SkASSERT(kN32_SkColorType == bitmap.colorType());
bsalomon49f085d2014-09-05 13:34:00 -070021 if (bitmap.getTexture() || kN32_SkColorType == bitmap.colorType()) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000022 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.com6cf53032012-06-22 18:55:55 +000032
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000033 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
scroggo@google.com58b4ead2012-08-31 16:15:22 +000044 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000045 SkString skString(string);
46 return skString.endsWith("%");
47 }
48
twiz@google.com6cf53032012-06-22 18:55:55 +000049 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
reed6c225732014-06-09 19:52:07 -070050 bitmap->allocN32Pixels(width, height);
junov@google.comdbfac8a2012-12-06 21:47:40 +000051 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +000052 }
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000053
54 bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath,
55 const char *subdirOrNull, const SkString& baseName) {
56 SkString partialPath;
57 if (subdirOrNull) {
tfarinaa8e2e152014-07-28 19:26:58 -070058 partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000059 sk_mkdir(partialPath.c_str());
60 } else {
61 partialPath.set(dirPath);
62 }
tfarinaa8e2e152014-07-28 19:26:58 -070063 SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000064 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) {
65 return true;
66 } else {
67 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
68 return false;
69 }
70 }
71
72} // namespace sk_tools