@@ -36,6 +36,7 @@ class WebpOutput final : public ImageOutput {
3636 std::string m_filename;
3737 imagesize_t m_scanline_size;
3838 unsigned int m_dither;
39+ bool m_convert_alpha; // Do we deassociate alpha?
3940 std::vector<uint8_t > m_uncompressed_image;
4041
4142 void init ()
@@ -113,7 +114,9 @@ WebpOutput::open(const std::string& name, const ImageSpec& spec, OpenMode mode)
113114
114115 // forcing UINT8 format
115116 m_spec.set_format (TypeDesc::UINT8);
116- m_dither = m_spec.get_int_attribute (" oiio:dither" , 0 );
117+ m_dither = m_spec.get_int_attribute (" oiio:dither" , 0 );
118+ m_convert_alpha = m_spec.alpha_channel != -1
119+ && !m_spec.get_int_attribute (" oiio:UnassociatedAlpha" , 0 );
117120
118121 m_scanline_size = m_spec.scanline_bytes ();
119122 m_uncompressed_image.resize (m_spec.image_bytes (), 0 );
@@ -136,20 +139,25 @@ WebpOutput::write_scanline(int y, int z, TypeDesc format, const void* data,
136139
137140 if (y == m_spec.height - 1 ) {
138141 if (m_spec.nchannels == 4 ) {
139- // WebP requires unassociated alpha, and it's sRGB.
140- // Handle this all by wrapping an IB around it.
141- ImageSpec specwrap (m_spec.width , m_spec.height , 4 , TypeUInt8);
142- ImageBuf bufwrap (specwrap, cspan<uint8_t >(m_uncompressed_image));
143- ROI rgbroi (0 , m_spec.width , 0 , m_spec.height , 0 , 1 , 0 , 3 );
144- ImageBufAlgo::pow (bufwrap, bufwrap, 2 .2f , rgbroi);
145- ImageBufAlgo::unpremult (bufwrap, bufwrap);
146- ImageBufAlgo::pow (bufwrap, bufwrap, 1 .0f / 2 .2f , rgbroi);
142+ if (m_convert_alpha) {
143+ // WebP requires unassociated alpha, and it's sRGB.
144+ // Handle this all by wrapping an IB around it.
145+ ImageSpec specwrap (m_spec.width , m_spec.height , 4 , TypeUInt8);
146+ ImageBuf bufwrap (specwrap,
147+ cspan<uint8_t >(m_uncompressed_image));
148+ ROI rgbroi (0 , m_spec.width , 0 , m_spec.height , 0 , 1 , 0 , 3 );
149+ ImageBufAlgo::pow (bufwrap, bufwrap, 2 .2f , rgbroi);
150+ ImageBufAlgo::unpremult (bufwrap, bufwrap);
151+ ImageBufAlgo::pow (bufwrap, bufwrap, 1 .0f / 2 .2f , rgbroi);
152+ }
153+
147154 WebPPictureImportRGBA (&m_webp_picture, m_uncompressed_image.data (),
148155 m_scanline_size);
149156 } else {
150157 WebPPictureImportRGB (&m_webp_picture, m_uncompressed_image.data (),
151158 m_scanline_size);
152159 }
160+
153161 if (!WebPEncode (&m_webp_config, &m_webp_picture)) {
154162 errorfmt (" Failed to encode {} as WebP image" , m_filename);
155163 close ();
0 commit comments