From 0a6b1ea256e2756da4170a32f2914f78e8d7e45e Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Mon, 27 May 2019 12:35:05 -0400 Subject: [PATCH] Improve image alpha handling in PDF and PS plotters Fixes: lp:1825276 * https://bugs.launchpad.net/kicad/+bug/1825276 (cherry picked from commit 4adf89b40b4e471533fb07860a82c5f65133a3b8) --- common/plotters/PDF_plotter.cpp | 15 +++++++++++++++ common/plotters/PS_plotter.cpp | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/common/plotters/PDF_plotter.cpp b/common/plotters/PDF_plotter.cpp index 6e29188b52..cde9f51795 100644 --- a/common/plotters/PDF_plotter.cpp +++ b/common/plotters/PDF_plotter.cpp @@ -390,6 +390,21 @@ void PDF_PLOTTER::PlotImage( const wxImage & aImage, const wxPoint& aPos, unsigned char r = aImage.GetRed( x, y ) & 0xFF; unsigned char g = aImage.GetGreen( x, y ) & 0xFF; unsigned char b = aImage.GetBlue( x, y ) & 0xFF; + + // PDF inline images don't support alpha, so premultiply against white background + if( aImage.HasAlpha() ) + { + unsigned char alpha = aImage.GetAlpha( x, y ) & 0xFF; + + if( alpha < 0xFF ) + { + float a = 1.0 - ( (float) alpha / 255.0 ); + r = ( int )( r + ( a * 0xFF ) ) & 0xFF; + g = ( int )( g + ( a * 0xFF ) ) & 0xFF; + b = ( int )( b + ( a * 0xFF ) ) & 0xFF; + } + } + // As usual these days, stdio buffering has to suffeeeeerrrr if( colorMode ) { diff --git a/common/plotters/PS_plotter.cpp b/common/plotters/PS_plotter.cpp index ac13d2826c..46f7fc174a 100644 --- a/common/plotters/PS_plotter.cpp +++ b/common/plotters/PS_plotter.cpp @@ -709,6 +709,20 @@ void PS_PLOTTER::PlotImage( const wxImage & aImage, const wxPoint& aPos, green = aImage.GetGreen( xx, yy) & 0xFF; blue = aImage.GetBlue( xx, yy) & 0xFF; + // PS doesn't support alpha, so premultiply against white background + if( aImage.HasAlpha() ) + { + unsigned char alpha = aImage.GetAlpha( xx, yy ) & 0xFF; + + if( alpha < 0xFF ) + { + float a = 1.0 - ( (float) alpha / 255.0 ); + red = ( int )( red + ( a * 0xFF ) ) & 0xFF; + green = ( int )( green + ( a * 0xFF ) ) & 0xFF; + blue = ( int )( blue + ( a * 0xFF ) ) & 0xFF; + } + } + if( colorMode ) fprintf( outputFile, "%2.2X%2.2X%2.2X", red, green, blue ); else