revise BGRA->RGBA

This commit is contained in:
John Cupitt 2020-10-17 13:42:57 +01:00
parent 93f67a2bdf
commit a1ae0d1d68
5 changed files with 38 additions and 29 deletions

View File

@ -35,17 +35,17 @@
#include <vips/vips.h>
#include <vips/internal.h>
/* Convert from Cairo's BGRA to RGBA and undo premultiplication.
/* Convert from Cairo-style premultiplied BGRA to RGBA.
*
* See also openslide's argb2rgba().
*/
void
vips__cairo2rgba( guint32 * restrict buf, int n )
vips__premultiplied_bgra2rgba( guint32 * restrict p, int n )
{
int i;
int x;
for( i = 0; i < n; i++ ) {
guint32 bgra = GUINT32_FROM_BE( buf[i] );
for( x = 0; x < n; x++ ) {
guint32 bgra = GUINT32_FROM_BE( p[x] );
guint8 a = bgra & 0xff;
guint32 rgba;
@ -65,6 +65,29 @@ vips__cairo2rgba( guint32 * restrict buf, int n )
((255 * ((bgra >> 24) & 0xff) / a) << 8) |
a;
buf[i] = GUINT32_TO_BE( rgba );
p[x] = GUINT32_TO_BE( rgba );
}
}
/* Convert from PDFium-style BGRA to RGBA.
*/
void
vips__bgra2rgba( guint32 * restrict p, int n )
{
int x;
for( x = 0; x < n; x++ ) {
guint32 bgra = GUINT32_FROM_BE( p[x] );
guint rgba;
/* Leave G and A, swap R and B.
*/
rgba =
(bgra & 0x00ff00ff) |
(bgra & 0x0000ff00) << 16 |
(bgra & 0xff000000) >> 16;
p[x] = GUINT32_TO_BE( rgba );
}
}

View File

@ -570,25 +570,10 @@ vips_foreign_load_pdf_generate( VipsRegion *or,
/* PDFium writes BGRA, we must swap.
*/
for( y = 0; y < r->height; y++ ) {
guint32 * restrict p =
(guint32 *) VIPS_REGION_ADDR( or, r->left, r->top + y );
int x;
for( x = 0; x < r->width; x++ ) {
guint32 bgra = GUINT32_FROM_BE( p[x] );
guint rgba;
rgba =
(bgra & 0x00ff00ff) |
(bgra & 0x0000ff00) << 16 |
(bgra & 0xff000000) >> 16;
p[x] = GUINT32_TO_BE( rgba );
}
}
for( y = 0; y < r->height; y++ )
vips__bgra2rgba(
(guint32 *) VIPS_REGION_ADDR( or, r->left, r->top + y ),
r->width );
return( 0 );
}

View File

@ -367,7 +367,7 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load )
/* Convert the background to the image format.
*
* FIXME ... we probably should convert this to pre-multiplied BGRA
* to match the Cairo convention. See vips__cairo2rgba().
* to match the Cairo convention. See vips__premultiplied_bgra2rgba().
*/
if( !(pdf->ink = vips__vector_to_ink( class->nickname,
load->out,
@ -452,7 +452,7 @@ vips_foreign_load_pdf_generate( VipsRegion *or,
/* Cairo makes pre-multipled BRGA, we must byteswap and unpremultiply.
*/
for( y = 0; y < r->height; y++ )
vips__cairo2rgba(
vips__premultiplied_bgra2rgba(
(guint32 *) VIPS_REGION_ADDR( or, r->left, r->top + y ),
r->width );

View File

@ -368,7 +368,7 @@ vips_foreign_load_svg_generate( VipsRegion *or,
/* Cairo makes pre-multipled BRGA -- we must byteswap and unpremultiply.
*/
for( y = 0; y < r->height; y++ )
vips__cairo2rgba(
vips__premultiplied_bgra2rgba(
(guint32 *) VIPS_REGION_ADDR( or, r->left, r->top + y ),
r->width );

View File

@ -236,7 +236,8 @@ int vips__byteswap_bool( VipsImage *in, VipsImage **out, gboolean swap );
char *vips__xml_properties( VipsImage *image );
void vips__cairo2rgba( guint32 *buf, int n );
void vips__premultiplied_bgra2rgba( 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 );