From 0727fd5899156d649a88038629753c11042228fd Mon Sep 17 00:00:00 2001 From: Hugo Cuvillier Date: Wed, 16 Jan 2019 09:30:51 -0800 Subject: [PATCH] Fixes `pixelSpecificationNarrower` for the png plugin. Summary: In some cases (e.g when the input image would have a `skipFirst` / `skipLast` alphaInfo) encoding an image to png would fail as the narrowed pixel spec wasn't accepted by the png compressor. Reviewed By: lambdapioneer Differential Revision: D13693679 fbshipit-source-id: c8793147a468e676aaa5570d1e148b903cfe6075 --- .../plugins/png/LibPngTranscodingPlugin.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cpp/spectrum/plugins/png/LibPngTranscodingPlugin.cpp b/cpp/spectrum/plugins/png/LibPngTranscodingPlugin.cpp index b7478890..6e2794eb 100644 --- a/cpp/spectrum/plugins/png/LibPngTranscodingPlugin.cpp +++ b/cpp/spectrum/plugins/png/LibPngTranscodingPlugin.cpp @@ -30,11 +30,17 @@ inline codecs::DecompressorProvider::Factory makeLibPngDecompressorFactory() { image::pixel::Specification pixelSpecificationNarrower( const image::pixel::Specification& pixelSpecification) { - if (pixelSpecification == image::pixel::specifications::RGBA) { - // PNGs cannot encode RGBA. - return image::pixel::specifications::ARGB; + if (pixelSpecification.colorModel == image::pixel::colormodels::Gray) { + return image::pixel::specifications::Gray; } else { - return pixelSpecification; + switch (pixelSpecification.alphaInfo) { + case image::pixel::AlphaInfo::None: + case image::pixel::AlphaInfo::SkipLast: + case image::pixel::AlphaInfo::SkipFirst: + return image::pixel::specifications::RGB; + default: + return image::pixel::specifications::ARGB; + } } }