blob: bd32f1938eb73921801c4bba771322e11cb83cae [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"
twiz@google.com6cf53032012-06-22 18:55:55 +000019
Hal Canarydb683012016-11-23 08:55:18 -070020#include "sk_tool_utils.h"
21
twiz@google.com6cf53032012-06-22 18:55:55 +000022namespace sk_tools {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000023 void force_all_opaque(const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070024 SkASSERT(kN32_SkColorType == bitmap.colorType());
reedc7ec7c92016-07-25 08:29:10 -070025 if (kN32_SkColorType == bitmap.colorType()) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000026 return;
27 }
28
keyar@chromium.org9299ede2012-08-21 19:05:08 +000029 for (int y = 0; y < bitmap.height(); y++) {
30 for (int x = 0; x < bitmap.width(); x++) {
31 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
32 }
33 }
34 }
twiz@google.com6cf53032012-06-22 18:55:55 +000035
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000036 void replace_char(SkString* str, const char oldChar, const char newChar) {
halcanary96fcdcc2015-08-27 07:41:13 -070037 if (nullptr == str) {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000038 return;
39 }
40 for (size_t i = 0; i < str->size(); ++i) {
41 if (oldChar == str->operator[](i)) {
42 str->operator[](i) = newChar;
43 }
44 }
45 }
46
scroggo@google.com58b4ead2012-08-31 16:15:22 +000047 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000048 SkString skString(string);
49 return skString.endsWith("%");
50 }
51
twiz@google.com6cf53032012-06-22 18:55:55 +000052 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
reed6c225732014-06-09 19:52:07 -070053 bitmap->allocN32Pixels(width, height);
junov@google.comdbfac8a2012-12-06 21:47:40 +000054 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +000055 }
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000056
57 bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath,
58 const char *subdirOrNull, const SkString& baseName) {
59 SkString partialPath;
60 if (subdirOrNull) {
tfarinaa8e2e152014-07-28 19:26:58 -070061 partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000062 sk_mkdir(partialPath.c_str());
63 } else {
64 partialPath.set(dirPath);
65 }
tfarinaa8e2e152014-07-28 19:26:58 -070066 SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
Hal Canarydb683012016-11-23 08:55:18 -070067 if (sk_tool_utils::EncodeImageToFile(fullPath.c_str(), bm, SkEncodedImageFormat::kPNG, 100)) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000068 return true;
69 } else {
70 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
71 return false;
72 }
73 }
74
brianosman3c579dc2016-04-19 09:18:11 -070075 sk_sp<SkData> encode_bitmap_for_png(SkBitmap bitmap) {
76 const int w = bitmap.width(),
77 h = bitmap.height();
78 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
79 // We leave the gamma of these bytes unspecified, to continue the status quo,
80 // which we think generally is to interpret them as sRGB.
81
82 SkAutoTMalloc<uint32_t> rgba(w*h);
83
Matt Sarett77a7a1b2017-02-07 13:56:11 -050084 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -070085 if (bitmap. colorType() == kN32_SkColorType &&
86 bitmap.colorSpace() == srgbColorSpace.get()) {
brianosman3c579dc2016-04-19 09:18:11 -070087 // These are premul sRGB 8-bit pixels in SkPMColor order.
88 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
brianosman3c579dc2016-04-19 09:18:11 -070089 auto px = (const uint32_t*)bitmap.getPixels();
90 if (!px) {
91 return nullptr;
92 }
93 for (int i = 0; i < w*h; i++) {
94 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
95#if defined(SK_PMCOLOR_IS_BGRA)
96 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
97#endif
98 float invA = 1.0f / fs[3];
99 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
100 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
101 }
102
103 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
104 // These are premul linear half-float pixels in RGBA order.
105 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
brianosman3c579dc2016-04-19 09:18:11 -0700106 auto px = (const uint64_t*)bitmap.getPixels();
107 if (!px) {
108 return nullptr;
109 }
110 for (int i = 0; i < w*h; i++) {
111 // Convert up to linear floats.
112 Sk4f fs(SkHalfToFloat(static_cast<SkHalf>(px[i] >> (0 * 16))),
113 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (1 * 16))),
114 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (2 * 16))),
115 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (3 * 16))));
116 fs = Sk4f::Max(0.0f, Sk4f::Min(fs, 1.0f)); // Clamp
117 float invA = 1.0f / fs[3];
118 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
119 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
120 }
121
122 } else {
123 // We "should" gamma correct in here but we don't.
124 // We want Gold to show exactly what our clients are seeing, broken gamma.
125
126 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
127 if (bitmap.colorType() != kN32_SkColorType) {
128 SkBitmap n32;
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400129 if (!sk_tool_utils::copy_to(&n32, kN32_SkColorType, bitmap)) {
brianosman3c579dc2016-04-19 09:18:11 -0700130 return nullptr;
131 }
132 bitmap = n32;
133 }
134
135 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
136 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
137 kUnpremul_SkAlphaType),
138 rgba, 4*w, 0,0)) {
139 return nullptr;
140 }
141 }
142
143 return SkData::MakeFromMalloc(rgba.release(), w*h*sizeof(uint32_t));
144 }
145
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000146} // namespace sk_tools