Skip to content

Commit aa5abf3

Browse files
committed
fix background byte ordering in PDF
the `background` param was BGRA rather than vips-style RGBA see #2804
1 parent 7561d4e commit aa5abf3

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

libvips/foreign/cairo.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,41 @@ vips__premultiplied_bgra2rgba( guint32 * restrict p, int n )
6969
}
7070
}
7171

72+
/* Unpremultiplied RGBA (vips convention) to cairo-style premul BGRA.
73+
*/
74+
void
75+
vips__rgba2bgra_premultiplied( guint32 * restrict p, int n )
76+
{
77+
int x;
78+
79+
for( x = 0; x < n; x++ ) {
80+
guint32 rgba = GUINT32_FROM_BE( p[x] );
81+
guint8 a = rgba & 0xff;
82+
83+
guint32 bgra;
84+
85+
if( a == 0 )
86+
bgra = 0;
87+
else if( a == 255 )
88+
bgra = (rgba & 0x00ff00ff) |
89+
(rgba & 0x0000ff00) << 16 |
90+
(rgba & 0xff000000) >> 16;
91+
else {
92+
int r = (rgba >> 24) & 0xff;
93+
int g = (rgba >> 16) & 0xff;
94+
int b = (rgba >> 8) & 0xff;
95+
96+
r = ((r * a) + 128) >> 8;
97+
g = ((g * a) + 128) >> 8;
98+
b = ((b * a) + 128) >> 8;
99+
100+
bgra = (b << 24) | (g << 16) | (r << 8) | a;
101+
}
102+
103+
p[x] = GUINT32_TO_BE( bgra );
104+
}
105+
}
106+
72107
/* Convert from PDFium-style BGRA to RGBA.
73108
*/
74109
void

libvips/foreign/pdfiumload.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
524524
VIPS_AREA( pdf->background )->n )) )
525525
return( -1 );
526526

527+
/* Swap B and R.
528+
*/
529+
vips__bgra2rgba( (guint32 *) pdf->ink, 1 );
530+
527531
return( 0 );
528532
}
529533

@@ -572,10 +576,10 @@ vips_foreign_load_pdf_generate( VipsRegion *or,
572576
if( vips_foreign_load_pdf_get_page( pdf, pdf->page_no + i ) )
573577
return( -1 );
574578

575-
/* 4 means RGBA.
576-
*/
577579
g_mutex_lock( vips_pdfium_mutex );
578580

581+
/* 4 means RGBA.
582+
*/
579583
bitmap = FPDFBitmap_CreateEx( rect.width, rect.height, 4,
580584
VIPS_REGION_ADDR( or, rect.left, rect.top ),
581585
VIPS_REGION_LSKIP( or ) );

libvips/foreign/popplerload.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,17 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
380380
vips_foreign_load_pdf_set_image( pdf, load->out );
381381

382382
/* Convert the background to the image format.
383-
*
384-
* FIXME ... we probably should convert this to pre-multiplied BGRA
385-
* to match the Cairo convention. See vips__premultiplied_bgra2rgba().
386383
*/
387384
if( !(pdf->ink = vips__vector_to_ink( class->nickname,
388385
load->out,
389386
VIPS_AREA( pdf->background )->data, NULL,
390387
VIPS_AREA( pdf->background )->n )) )
391388
return( -1 );
392389

390+
/* Swap to cairo-style premultiplied bgra.
391+
*/
392+
vips__rgba2bgra_premultiplied( (guint32 *) pdf->ink, 1 );
393+
393394
vips_source_minimise( pdf->source );
394395

395396
return( 0 );

libvips/include/vips/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ char *vips__xml_properties( VipsImage *image );
325325
*/
326326
VIPS_API
327327
void vips__premultiplied_bgra2rgba( guint32 * restrict p, int n );
328+
VIPS_API
329+
void vips__rgba2bgra_premultiplied( guint32 * restrict p, int n );
328330
void vips__bgra2rgba( guint32 * restrict p, int n );
329331
void vips__Lab2LabQ_vec( VipsPel *out, float *in, int width );
330332
void vips__LabQ2Lab_vec( float *out, VipsPel *in, int width );

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy