blob: 84925b4dad268acd0b4dd51cb013d44c25f26325 [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
29 SkAutoLockPixels lock(bitmap);
30 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(),
78 h = bitmap.height();
79 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
80 // We leave the gamma of these bytes unspecified, to continue the status quo,
81 // which we think generally is to interpret them as sRGB.
82
83 SkAutoTMalloc<uint32_t> rgba(w*h);
84
Brian Osman526972e2016-10-24 09:24:02 -040085 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
brianosmanb109b8c2016-06-16 13:03:24 -070086 if (bitmap. colorType() == kN32_SkColorType &&
87 bitmap.colorSpace() == srgbColorSpace.get()) {
brianosman3c579dc2016-04-19 09:18:11 -070088 // These are premul sRGB 8-bit pixels in SkPMColor order.
89 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
90 bitmap.lockPixels();
91 auto px = (const uint32_t*)bitmap.getPixels();
92 if (!px) {
93 return nullptr;
94 }
95 for (int i = 0; i < w*h; i++) {
96 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
97#if defined(SK_PMCOLOR_IS_BGRA)
98 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
99#endif
100 float invA = 1.0f / fs[3];
101 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
102 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
103 }
104
105 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
106 // These are premul linear half-float pixels in RGBA order.
107 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
108 bitmap.lockPixels();
109 auto px = (const uint64_t*)bitmap.getPixels();
110 if (!px) {
111 return nullptr;
112 }
113 for (int i = 0; i < w*h; i++) {
114 // Convert up to linear floats.
115 Sk4f fs(SkHalfToFloat(static_cast<SkHalf>(px[i] >> (0 * 16))),
116 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (1 * 16))),
117 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (2 * 16))),
118 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (3 * 16))));
119 fs = Sk4f::Max(0.0f, Sk4f::Min(fs, 1.0f)); // Clamp
120 float invA = 1.0f / fs[3];
121 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
122 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
123 }
124
125 } else {
126 // We "should" gamma correct in here but we don't.
127 // We want Gold to show exactly what our clients are seeing, broken gamma.
128
129 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
130 if (bitmap.colorType() != kN32_SkColorType) {
131 SkBitmap n32;
132 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
133 return nullptr;
134 }
135 bitmap = n32;
136 }
137
138 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
139 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
140 kUnpremul_SkAlphaType),
141 rgba, 4*w, 0,0)) {
142 return nullptr;
143 }
144 }
145
146 return SkData::MakeFromMalloc(rgba.release(), w*h*sizeof(uint32_t));
147 }
148
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000149} // namespace sk_tools