blob: a6803c67db42efdb26d5a4d8a50869860350a65c [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) {
halcanary96fcdcc2015-08-27 07:41:13 -070021 SkASSERT(nullptr == bitmap.getTexture());
reed0689d7b2014-06-14 05:30:20 -070022 SkASSERT(kN32_SkColorType == bitmap.colorType());
bsalomon49f085d2014-09-05 13:34:00 -070023 if (bitmap.getTexture() || kN32_SkColorType == bitmap.colorType()) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +000024 return;
25 }
26
27 SkAutoLockPixels lock(bitmap);
28 for (int y = 0; y < bitmap.height(); y++) {
29 for (int x = 0; x < bitmap.width(); x++) {
30 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
31 }
32 }
33 }
twiz@google.com6cf53032012-06-22 18:55:55 +000034
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000035 void replace_char(SkString* str, const char oldChar, const char newChar) {
halcanary96fcdcc2015-08-27 07:41:13 -070036 if (nullptr == str) {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000037 return;
38 }
39 for (size_t i = 0; i < str->size(); ++i) {
40 if (oldChar == str->operator[](i)) {
41 str->operator[](i) = newChar;
42 }
43 }
44 }
45
scroggo@google.com58b4ead2012-08-31 16:15:22 +000046 bool is_percentage(const char* const string) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000047 SkString skString(string);
48 return skString.endsWith("%");
49 }
50
twiz@google.com6cf53032012-06-22 18:55:55 +000051 void setup_bitmap(SkBitmap* bitmap, int width, int height) {
reed6c225732014-06-09 19:52:07 -070052 bitmap->allocN32Pixels(width, height);
junov@google.comdbfac8a2012-12-06 21:47:40 +000053 bitmap->eraseColor(SK_ColorTRANSPARENT);
twiz@google.com6cf53032012-06-22 18:55:55 +000054 }
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000055
56 bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath,
57 const char *subdirOrNull, const SkString& baseName) {
58 SkString partialPath;
59 if (subdirOrNull) {
tfarinaa8e2e152014-07-28 19:26:58 -070060 partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000061 sk_mkdir(partialPath.c_str());
62 } else {
63 partialPath.set(dirPath);
64 }
tfarinaa8e2e152014-07-28 19:26:58 -070065 SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000066 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) {
67 return true;
68 } else {
69 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
70 return false;
71 }
72 }
73
brianosman3c579dc2016-04-19 09:18:11 -070074 sk_sp<SkData> encode_bitmap_for_png(SkBitmap bitmap) {
75 const int w = bitmap.width(),
76 h = bitmap.height();
77 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
78 // We leave the gamma of these bytes unspecified, to continue the status quo,
79 // which we think generally is to interpret them as sRGB.
80
81 SkAutoTMalloc<uint32_t> rgba(w*h);
82
brianosmanb109b8c2016-06-16 13:03:24 -070083 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
84 if (bitmap. colorType() == kN32_SkColorType &&
85 bitmap.colorSpace() == srgbColorSpace.get()) {
brianosman3c579dc2016-04-19 09:18:11 -070086 // These are premul sRGB 8-bit pixels in SkPMColor order.
87 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
88 bitmap.lockPixels();
89 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.
106 bitmap.lockPixels();
107 auto px = (const uint64_t*)bitmap.getPixels();
108 if (!px) {
109 return nullptr;
110 }
111 for (int i = 0; i < w*h; i++) {
112 // Convert up to linear floats.
113 Sk4f fs(SkHalfToFloat(static_cast<SkHalf>(px[i] >> (0 * 16))),
114 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (1 * 16))),
115 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (2 * 16))),
116 SkHalfToFloat(static_cast<SkHalf>(px[i] >> (3 * 16))));
117 fs = Sk4f::Max(0.0f, Sk4f::Min(fs, 1.0f)); // Clamp
118 float invA = 1.0f / fs[3];
119 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
120 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
121 }
122
123 } else {
124 // We "should" gamma correct in here but we don't.
125 // We want Gold to show exactly what our clients are seeing, broken gamma.
126
127 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
128 if (bitmap.colorType() != kN32_SkColorType) {
129 SkBitmap n32;
130 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
131 return nullptr;
132 }
133 bitmap = n32;
134 }
135
136 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
137 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
138 kUnpremul_SkAlphaType),
139 rgba, 4*w, 0,0)) {
140 return nullptr;
141 }
142 }
143
144 return SkData::MakeFromMalloc(rgba.release(), w*h*sizeof(uint32_t));
145 }
146
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000147} // namespace sk_tools