OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "picture_utils.h" | 8 #include "picture_utils.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkHalf.h" |
11 #include "SkImageEncoder.h" | 12 #include "SkImageEncoder.h" |
12 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
| 14 #include "SkPM4fPriv.h" |
13 #include "SkPicture.h" | 15 #include "SkPicture.h" |
14 #include "SkStream.h" | 16 #include "SkStream.h" |
15 #include "SkString.h" | 17 #include "SkString.h" |
16 | 18 |
17 namespace sk_tools { | 19 namespace sk_tools { |
18 void force_all_opaque(const SkBitmap& bitmap) { | 20 void force_all_opaque(const SkBitmap& bitmap) { |
19 SkASSERT(nullptr == bitmap.getTexture()); | 21 SkASSERT(nullptr == bitmap.getTexture()); |
20 SkASSERT(kN32_SkColorType == bitmap.colorType()); | 22 SkASSERT(kN32_SkColorType == bitmap.colorType()); |
21 if (bitmap.getTexture() || kN32_SkColorType == bitmap.colorType()) { | 23 if (bitmap.getTexture() || kN32_SkColorType == bitmap.colorType()) { |
22 return; | 24 return; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 } | 64 } |
63 SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str()
); | 65 SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str()
); |
64 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPN
G_Type, 100)) { | 66 if (SkImageEncoder::EncodeFile(fullPath.c_str(), bm, SkImageEncoder::kPN
G_Type, 100)) { |
65 return true; | 67 return true; |
66 } else { | 68 } else { |
67 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); | 69 SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); |
68 return false; | 70 return false; |
69 } | 71 } |
70 } | 72 } |
71 | 73 |
| 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 t
oo). |
| 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 |
| 83 if (bitmap. colorType() == kN32_SkColorType && |
| 84 bitmap.profileType() == kSRGB_SkColorProfileType) { |
| 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 ther
e 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 f
loats. |
| 94 #if defined(SK_PMCOLOR_IS_BGRA) |
| 95 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if no
t 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 byte
s. |
| 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 ther
e 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 o
rder). |
| 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 |
72 } // namespace sk_tools | 146 } // namespace sk_tools |
OLD | NEW |