blob: f476c0dbc0d389806e6d2e6e339d0675b6b12f0a [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"
brianosman3c579dc2016-04-19 09:18:11 -070011#include "SkHalf.h"
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000012#include "SkImageEncoder.h"
13#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050014#include "SkOSPath.h"
brianosman3c579dc2016-04-19 09:18:11 -070015#include "SkPM4fPriv.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000016#include "SkPicture.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000017#include "SkStream.h"
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000018#include "SkString.h"
Mike Kleinfc334162017-06-12 08:59:16 -040019#include "SkRasterPipeline.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000020
Hal Canarydb683012016-11-23 08:55:18 -070021#include "sk_tool_utils.h"
22
twiz@google.com6cf53032012-06-22 18:55:55 +000023namespace sk_tools {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000024 void force_all_opaque(const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070025 SkASSERT(kN32_SkColorType == bitmap.colorType());
reedc7ec7c92016-07-25 08:29:10 -070026 if (kN32_SkColorType == bitmap.colorType()) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000027 return;
28 }
29
keyar@chromium.org9299ede2012-08-21 19:05:08 +000030 for (int y = 0; y < bitmap.height(); y++) {
31 for (int x = 0; x < bitmap.width(); x++) {
32 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
33 }
34 }
35 }
twiz@google.com6cf53032012-06-22 18:55:55 +000036
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000037 void replace_char(SkString* str, const char oldChar, const char newChar) {
halcanary96fcdcc2015-08-27 07:41:13 -070038 if (nullptr == str) {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000039 return;
40 }
41 for (size_t i = 0; i < str->size(); ++i) {
42 if (oldChar == str->operator[](i)) {
43 str->operator[](i) = newChar;
44 }
45 }
46 }
47
scroggo@google.com58b4ead2012-08-31 16:15:22 +000048 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000049 SkString skString(string);
50 return skString.endsWith("%");
51 }
52
twiz@google.com6cf53032012-06-22 18:55:55 +000053 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
reed6c225732014-06-09 19:52:07 -070054 bitmap->allocN32Pixels(width, height);
junov@google.comdbfac8a2012-12-06 21:47:40 +000055 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +000056 }
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000057
58 bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath,
59 const char *subdirOrNull, const SkString& baseName) {
60 SkString partialPath;
61 if (subdirOrNull) {
tfarinaa8e2e152014-07-28 19:26:58 -070062 partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000063 sk_mkdir(partialPath.c_str());
64 } else {
65 partialPath.set(dirPath);
66 }
tfarinaa8e2e152014-07-28 19:26:58 -070067 SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
Hal Canarydb683012016-11-23 08:55:18 -070068 if (sk_tool_utils::EncodeImageToFile(fullPath.c_str(), bm, SkEncodedImageFormat::kPNG, 100)) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000069 return true;
70 } else {
71 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
72 return false;
73 }
74 }
75
brianosman3c579dc2016-04-19 09:18:11 -070076 sk_sp<SkData> encode_bitmap_for_png(SkBitmap bitmap) {
77 const int w = bitmap.width(),
Mike Kleinfc334162017-06-12 08:59:16 -040078 h = bitmap.height();
79
brianosman3c579dc2016-04-19 09:18:11 -070080 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
81 // We leave the gamma of these bytes unspecified, to continue the status quo,
82 // which we think generally is to interpret them as sRGB.
83
84 SkAutoTMalloc<uint32_t> rgba(w*h);
85
Mike Klein45c16fa2017-07-18 18:15:13 -040086 SkJumper_MemoryCtx src = { bitmap.getPixels(), bitmap.rowBytesAsPixels() },
87 dst = { rgba.get(), w };
brianosman3c579dc2016-04-19 09:18:11 -070088
Mike Kleinfc334162017-06-12 08:59:16 -040089 SkRasterPipeline_<256> p;
90 switch (bitmap.colorType()) {
91 case kRGBA_F16_SkColorType: p.append(SkRasterPipeline::load_f16, &src); break;
Mike Kleinc2d20762017-06-27 19:53:21 -040092 case kBGRA_8888_SkColorType: p.append(SkRasterPipeline::load_bgra, &src); break;
93 case kRGBA_8888_SkColorType: p.append(SkRasterPipeline::load_8888, &src); break;
Mike Kleinfc334162017-06-12 08:59:16 -040094 case kRGB_565_SkColorType: p.append(SkRasterPipeline::load_565, &src); break;
95 default: SkASSERT(false); // DM doesn't support any other formats, does it?
96 }
97 if (bitmap.info().gammaCloseToSRGB()) {
Mike Klein03a78792017-06-12 11:18:59 -040098 p.append_from_srgb(kUnpremul_SkAlphaType);
Mike Kleinfc334162017-06-12 08:59:16 -040099 }
Mike Kleinfc334162017-06-12 08:59:16 -0400100 p.append(SkRasterPipeline::unpremul);
101 p.append(SkRasterPipeline::clamp_0);
102 p.append(SkRasterPipeline::clamp_1);
103 if (bitmap.info().colorSpace()) {
104 // We leave legacy modes as-is. They're already sRGB encoded (kind of).
105 p.append(SkRasterPipeline::to_srgb);
106 }
107 p.append(SkRasterPipeline::store_8888, &dst);
brianosman3c579dc2016-04-19 09:18:11 -0700108
Mike Klein45c16fa2017-07-18 18:15:13 -0400109 p.run(0,0, w,h);
brianosman3c579dc2016-04-19 09:18:11 -0700110
111 return SkData::MakeFromMalloc(rgba.release(), w*h*sizeof(uint32_t));
112 }
113
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000114} // namespace sk_tools