blob: b252f8da62501e0280a2c9358153c7dba245ebab [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"
brianosman3c579dc2016-04-19 09:18:11 -070014#include "SkPM4fPriv.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000015#include "SkPicture.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000016#include "SkStream.h"
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000017#include "SkString.h"
twiz@google.com6cf53032012-06-22 18:55:55 +000018
19namespace sk_tools {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000020 void force_all_opaque(const SkBitmap& bitmap) {
reed0689d7b2014-06-14 05:30:20 -070021 SkASSERT(kN32_SkColorType == bitmap.colorType());
reedc7ec7c92016-07-25 08:29:10 -070022 if (kN32_SkColorType == bitmap.colorType()) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000023 return;
24 }
25
26 SkAutoLockPixels lock(bitmap);
27 for (int y = 0; y < bitmap.height(); y++) {
28 for (int x = 0; x < bitmap.width(); x++) {
29 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
30 }
31 }
32 }
twiz@google.com6cf53032012-06-22 18:55:55 +000033
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000034 void replace_char(SkString* str, const char oldChar, const char newChar) {
halcanary96fcdcc2015-08-27 07:41:13 -070035 if (nullptr == str) {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000036 return;
37 }
38 for (size_t i = 0; i < str->size(); ++i) {
39 if (oldChar == str->operator[](i)) {
40 str->operator[](i) = newChar;
41 }
42 }
43 }
44
scroggo@google.com58b4ead2012-08-31 16:15:22 +000045 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000046 SkString skString(string);
47 return skString.endsWith("%");
48 }
49
twiz@google.com6cf53032012-06-22 18:55:55 +000050 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
reed6c225732014-06-09 19:52:07 -070051 bitmap->allocN32Pixels(width, height);
junov@google.comdbfac8a2012-12-06 21:47:40 +000052 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +000053 }
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000054
55 bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath,
56 const char *subdirOrNull, const SkString& baseName) {
57 SkString partialPath;
58 if (subdirOrNull) {
tfarinaa8e2e152014-07-28 19:26:58 -070059 partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000060 sk_mkdir(partialPath.c_str());
61 } else {
62 partialPath.set(dirPath);
63 }
tfarinaa8e2e152014-07-28 19:26:58 -070064 SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000065 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) {
66 return true;
67 } else {
68 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
69 return false;
70 }
71 }
72
brianosman3c579dc2016-04-19 09:18:11 -070073 sk_sp<SkData> encode_bitmap_for_png(SkBitmap bitmap) {
74 const int w = bitmap.width(),
75 h = bitmap.height();
76 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
77 // We leave the gamma of these bytes unspecified, to continue the status quo,
78 // which we think generally is to interpret them as sRGB.
79
80 SkAutoTMalloc<uint32_t> rgba(w*h);
81
Brian Osman526972e2016-10-24 09:24:02 -040082 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
brianosmanb109b8c2016-06-16 13:03:24 -070083 if (bitmap. colorType() == kN32_SkColorType &&
84 bitmap.colorSpace() == srgbColorSpace.get()) {
brianosman3c579dc2016-04-19 09:18:11 -070085 // These are premul sRGB 8-bit pixels in SkPMColor order.
86 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
87 bitmap.lockPixels();
88 auto px = (const uint32_t*)bitmap.getPixels();
89 if (!px) {
90 return nullptr;
91 }
92 for (int i = 0; i < w*h; i++) {
93 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
94#if defined(SK_PMCOLOR_IS_BGRA)
95 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
96#endif
97 float invA = 1.0f / fs[3];
98 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
99 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
100 }
101
102 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
103 // These are premul linear half-float pixels in RGBA order.
104 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
105 bitmap.lockPixels();
106 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;
129 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
130 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