twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 11 | #include "SkHalf.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 14 | #include "SkOSPath.h" |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 15 | #include "SkPM4fPriv.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 16 | #include "SkPicture.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 18 | #include "SkString.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 19 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 20 | #include "sk_tool_utils.h" |
| 21 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 22 | namespace sk_tools { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 23 | void force_all_opaque(const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 24 | SkASSERT(kN32_SkColorType == bitmap.colorType()); |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 25 | if (kN32_SkColorType == bitmap.colorType()) { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 26 | 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.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 37 | void replace_char(SkString* str, const char oldChar, const char newChar) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | if (nullptr == str) { |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 39 | 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.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 48 | bool is_percentage(const char* const string) { |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 49 | SkString skString(string); |
| 50 | return skString.endsWith("%"); |
| 51 | } |
| 52 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 53 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 54 | bitmap->allocN32Pixels(width, height); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 55 | bitmap->eraseColor(SK_ColorTRANSPARENT); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 56 | } |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 57 | |
| 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) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 62 | partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 63 | sk_mkdir(partialPath.c_str()); |
| 64 | } else { |
| 65 | partialPath.set(dirPath); |
| 66 | } |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 67 | SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str()); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 68 | if (sk_tool_utils::EncodeImageToFile(fullPath.c_str(), bm, SkEncodedImageFormat::kPNG, 100)) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 69 | return true; |
| 70 | } else { |
| 71 | SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 76 | 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 Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 85 | auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 86 | if (bitmap. colorType() == kN32_SkColorType && |
| 87 | bitmap.colorSpace() == srgbColorSpace.get()) { |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 88 | // 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.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 149 | } // namespace sk_tools |