fix f16 -> sRGB in encode_bitmap_for_png()
The existing logic looks wrong to me:
- clamp premul to [0,1]
- unpremul, ignoring zero alpha
It seems like we should do:
- unpremul, avoiding any divide by zero
- clamp unpremul to [0,1]
Am I misunderstanding this or has this just always been wrong?
Change-Id: I9636b9566c746bc05371e1e660f4e59dde16827b
Reviewed-on: https://skia-review.googlesource.com/19264
Reviewed-by: Matt Sarett <msarett@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
diff --git a/tools/picture_utils.cpp b/tools/picture_utils.cpp
index bd32f19..db24553 100644
--- a/tools/picture_utils.cpp
+++ b/tools/picture_utils.cpp
@@ -113,10 +113,11 @@
SkHalfToFloat(static_cast<SkHalf>(px[i] >> (1 * 16))),
SkHalfToFloat(static_cast<SkHalf>(px[i] >> (2 * 16))),
SkHalfToFloat(static_cast<SkHalf>(px[i] >> (3 * 16))));
- fs = Sk4f::Max(0.0f, Sk4f::Min(fs, 1.0f)); // Clamp
- float invA = 1.0f / fs[3];
- fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
- rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
+ if (fs[3]) { // Unpremultiply.
+ fs *= Sk4f(1/fs[3], 1/fs[3], 1/fs[3], 1);
+ }
+ fs = Sk4f::Max(0.0f, Sk4f::Min(fs, 1.0f)); // Clamp to [0,1].
+ rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
}
} else {