Don't paint the background if the PDF has transparency (#2804)

This commit is contained in:
Sergey Alexandrovich 2022-05-21 15:17:44 +06:00 committed by GitHub
parent 7d4cba101c
commit 985fb1d1d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -104,6 +104,7 @@ EOF
#include <fpdfview.h> #include <fpdfview.h>
#include <fpdf_doc.h> #include <fpdf_doc.h>
#include <fpdf_edit.h>
typedef struct _VipsForeignLoadPdf { typedef struct _VipsForeignLoadPdf {
VipsForeignLoad parent_object; VipsForeignLoad parent_object;
@ -151,9 +152,9 @@ typedef struct _VipsForeignLoadPdf {
VipsRect image; VipsRect image;
VipsRect *pages; VipsRect *pages;
/* The [double] background converted to the image format. /* The [double] background converted to the PDFium format.
*/ */
VipsPel *ink; FPDF_DWORD ink;
} VipsForeignLoadPdf; } VipsForeignLoadPdf;
@ -437,6 +438,7 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
int top; int top;
int i; int i;
VipsPel *ink;
#ifdef DEBUG #ifdef DEBUG
printf( "vips_foreign_load_pdf_header: %p\n", pdf ); printf( "vips_foreign_load_pdf_header: %p\n", pdf );
@ -518,15 +520,13 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
/* Convert the background to the image format. /* Convert the background to the image format.
*/ */
if( !(pdf->ink = vips__vector_to_ink( class->nickname, if( !(ink = vips__vector_to_ink( class->nickname,
load->out, load->out,
VIPS_AREA( pdf->background )->data, NULL, VIPS_AREA( pdf->background )->data, NULL,
VIPS_AREA( pdf->background )->n )) ) VIPS_AREA( pdf->background )->n )) )
return( -1 ); return( -1 );
/* Swap B and R. pdf->ink = (ink[3] << 24) + (ink[0] << 16) + (ink[1] << 8) + ink[2];
*/
vips__bgra2rgba( (guint32 *) pdf->ink, 1 );
return( 0 ); return( 0 );
} }
@ -554,10 +554,6 @@ vips_foreign_load_pdf_generate( VipsRegion *or,
r->left, r->top, r->width, r->height ); r->left, r->top, r->width, r->height );
*/ */
/* PDFium won't always paint the background.
*/
vips_region_paint_pel( or, r, pdf->ink );
/* Search through the pages we are drawing for the first containing /* Search through the pages we are drawing for the first containing
* this rect. This could be quicker, perhaps a binary search, but who * this rect. This could be quicker, perhaps a binary search, but who
* cares. * cares.
@ -584,6 +580,11 @@ vips_foreign_load_pdf_generate( VipsRegion *or,
VIPS_REGION_ADDR( or, rect.left, rect.top ), VIPS_REGION_ADDR( or, rect.left, rect.top ),
VIPS_REGION_LSKIP( or ) ); VIPS_REGION_LSKIP( or ) );
/* PDFium won't always paint the background.
*/
if ( !FPDFPage_HasTransparency(pdf->page) )
FPDFBitmap_FillRect(bitmap, 0, 0, rect.width, rect.height, pdf->ink);
FPDF_RenderPageBitmap( bitmap, pdf->page, FPDF_RenderPageBitmap( bitmap, pdf->page,
0, 0, rect.width, rect.height, 0, 0, rect.width, rect.height,
0, 0 ); 0, 0 );