fix background byte ordering in PDF

the `background` param was BGRA rather than vips-style RGBA

see https://github.com/libvips/libvips/pull/2804
This commit is contained in:
John Cupitt 2022-05-16 14:28:53 +01:00
parent 7561d4e52c
commit aa5abf3e7e
4 changed files with 47 additions and 5 deletions

View File

@ -69,6 +69,41 @@ vips__premultiplied_bgra2rgba( guint32 * restrict p, int n )
}
}
/* Unpremultiplied RGBA (vips convention) to cairo-style premul BGRA.
*/
void
vips__rgba2bgra_premultiplied( guint32 * restrict p, int n )
{
int x;
for( x = 0; x < n; x++ ) {
guint32 rgba = GUINT32_FROM_BE( p[x] );
guint8 a = rgba & 0xff;
guint32 bgra;
if( a == 0 )
bgra = 0;
else if( a == 255 )
bgra = (rgba & 0x00ff00ff) |
(rgba & 0x0000ff00) << 16 |
(rgba & 0xff000000) >> 16;
else {
int r = (rgba >> 24) & 0xff;
int g = (rgba >> 16) & 0xff;
int b = (rgba >> 8) & 0xff;
r = ((r * a) + 128) >> 8;
g = ((g * a) + 128) >> 8;
b = ((b * a) + 128) >> 8;
bgra = (b << 24) | (g << 16) | (r << 8) | a;
}
p[x] = GUINT32_TO_BE( bgra );
}
}
/* Convert from PDFium-style BGRA to RGBA.
*/
void

View File

@ -524,6 +524,10 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
VIPS_AREA( pdf->background )->n )) )
return( -1 );
/* Swap B and R.
*/
vips__bgra2rgba( (guint32 *) pdf->ink, 1 );
return( 0 );
}
@ -572,10 +576,10 @@ vips_foreign_load_pdf_generate( VipsRegion *or,
if( vips_foreign_load_pdf_get_page( pdf, pdf->page_no + i ) )
return( -1 );
/* 4 means RGBA.
*/
g_mutex_lock( vips_pdfium_mutex );
/* 4 means RGBA.
*/
bitmap = FPDFBitmap_CreateEx( rect.width, rect.height, 4,
VIPS_REGION_ADDR( or, rect.left, rect.top ),
VIPS_REGION_LSKIP( or ) );

View File

@ -380,9 +380,6 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
vips_foreign_load_pdf_set_image( pdf, load->out );
/* Convert the background to the image format.
*
* FIXME ... we probably should convert this to pre-multiplied BGRA
* to match the Cairo convention. See vips__premultiplied_bgra2rgba().
*/
if( !(pdf->ink = vips__vector_to_ink( class->nickname,
load->out,
@ -390,6 +387,10 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
VIPS_AREA( pdf->background )->n )) )
return( -1 );
/* Swap to cairo-style premultiplied bgra.
*/
vips__rgba2bgra_premultiplied( (guint32 *) pdf->ink, 1 );
vips_source_minimise( pdf->source );
return( 0 );

View File

@ -325,6 +325,8 @@ char *vips__xml_properties( VipsImage *image );
*/
VIPS_API
void vips__premultiplied_bgra2rgba( guint32 * restrict p, int n );
VIPS_API
void vips__rgba2bgra_premultiplied( guint32 * restrict p, int n );
void vips__bgra2rgba( guint32 * restrict p, int n );
void vips__Lab2LabQ_vec( VipsPel *out, float *in, int width );
void vips__LabQ2Lab_vec( float *out, VipsPel *in, int width );