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" |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 14 | #include "SkPM4fPriv.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 15 | #include "SkPicture.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 17 | #include "SkString.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 18 | |
| 19 | namespace sk_tools { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 20 | void force_all_opaque(const SkBitmap& bitmap) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | SkASSERT(nullptr == bitmap.getTexture()); |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 22 | SkASSERT(kN32_SkColorType == bitmap.colorType()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 23 | if (bitmap.getTexture() || kN32_SkColorType == bitmap.colorType()) { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 24 | 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.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 34 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 35 | void replace_char(SkString* str, const char oldChar, const char newChar) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | if (nullptr == str) { |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 37 | 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.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 46 | bool is_percentage(const char* const string) { |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 47 | SkString skString(string); |
| 48 | return skString.endsWith("%"); |
| 49 | } |
| 50 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 51 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 52 | bitmap->allocN32Pixels(width, height); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 53 | bitmap->eraseColor(SK_ColorTRANSPARENT); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 54 | } |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 55 | |
| 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) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 60 | partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 61 | sk_mkdir(partialPath.c_str()); |
| 62 | } else { |
| 63 | partialPath.set(dirPath); |
| 64 | } |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 65 | SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 66 | 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 | |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 74 | 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 | |
brianosman | ab82418 | 2016-06-16 11:41:44 -0700 | [diff] [blame] | 83 | if (bitmap. colorType() == kN32_SkColorType && |
| 84 | bitmap.profileType() == kSRGB_SkColorProfileType) { |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 85 | // 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.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 146 | } // namespace sk_tools |