Merge branch 'master' of https://github.com/wix-playground/libvips into wix-playground-master

This commit is contained in:
John Cupitt 2020-02-07 16:30:26 +00:00
commit d8ee83e35e
9 changed files with 191 additions and 118 deletions

View File

@ -10,6 +10,7 @@
- add VIPS_LEAK env var
- add vips_pipe_read_limit_set(), --vips-pipe-read-limit,
VIPS_PIPE_READ_LIMIT
- revise gifload to fix BACKGROUND and PREVIOUS dispose [alon-ne]
31/1/19 started 8.9.2
- fix a deadlock with --vips-leak [DarthSim]

View File

@ -37,9 +37,8 @@
* 30/1/19
* - rework on top of VipsSource
* - add gifload_source
* 31/1/20
* - treat DISPOSAL_UNSPECIFIED as _DO_NOT, since that's what many GIFs
* in the wild appear to do
* 5/2/20 alon-ne
* - fix DISPOSE_BACKGROUND and DISPOSE_PREVIOUS
*/
/*
@ -114,6 +113,12 @@
#define DISPOSE_PREVIOUS 3
#endif
#define NO_TRANSPARENT_INDEX -1
#define TRANSPARENT_MASK 0x01
#define DISPOSE_MASK 0x07
#define DISPOSE_SHIFT 2
#define VIPS_TYPE_FOREIGN_LOAD_GIF (vips_foreign_load_gif_get_type())
#define VIPS_FOREIGN_LOAD_GIF( obj ) \
(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
@ -121,10 +126,6 @@
#define VIPS_FOREIGN_LOAD_GIF_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_CAST( (klass), \
VIPS_TYPE_FOREIGN_LOAD_GIF, VipsForeignLoadGifClass))
#define VIPS_IS_FOREIGN_LOAD_GIF( obj ) \
(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_FOREIGN_LOAD_GIF ))
#define VIPS_IS_FOREIGN_LOAD_GIF_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_FOREIGN_LOAD_GIF ))
#define VIPS_FOREIGN_LOAD_GIF_GET_CLASS( obj ) \
(G_TYPE_INSTANCE_GET_CLASS( (obj), \
VIPS_TYPE_FOREIGN_LOAD_GIF, VipsForeignLoadGifClass ))
@ -171,11 +172,15 @@ typedef struct _VipsForeignLoadGif {
*/
int n_pages;
/* A memory image the sized of one frame ... we accumulate to this as
/* A memory image the size of one frame ... we accumulate to this as
* we scan the image, and copy lines to the output on generate.
*/
VipsImage *frame;
/* A scratch buffer the size of frame, used for rendering.
*/
VipsImage *scratch;
/* A copy of the previous frame, in case we need a DISPOSE_PREVIOUS.
*/
VipsImage *previous;
@ -204,7 +209,7 @@ typedef struct _VipsForeignLoadGif {
/* As we scan the file, the index of the transparent pixel for this
* frame.
*/
int transparency;
int transparent_index;
/* Params for DGifOpen(). Set by subclasses, called by base class in
* _open().
@ -410,6 +415,7 @@ vips_foreign_load_gif_dispose( GObject *gobject )
VIPS_UNREF( gif->source );
VIPS_UNREF( gif->frame );
VIPS_UNREF( gif->scratch );
VIPS_UNREF( gif->previous );
VIPS_FREE( gif->comment );
VIPS_FREE( gif->line );
@ -559,7 +565,10 @@ vips_foreign_load_gif_scan_application_ext( VipsForeignLoadGif *gif,
*/
have_netscape = FALSE;
if( extension[0] == 11 &&
vips_isprefix( "NETSCAPE2.0", (const char*) (extension + 1) ) )
(vips_isprefix( "NETSCAPE2.0",
(const char*) (extension + 1) ) ||
vips_isprefix( "ANIMEXTS1.0",
(const char*) (extension + 1) )) )
have_netscape = TRUE;
while( extension != NULL ) {
@ -618,7 +627,7 @@ vips_foreign_load_gif_scan_extension( VipsForeignLoadGif *gif )
switch( ext_code ) {
case GRAPHICS_EXT_FUNC_CODE:
if( extension[0] == 4 &&
extension[1] & 0x1 ) {
extension[1] & TRANSPARENT_MASK ) {
VIPS_DEBUG_MSG( "gifload: has transp.\n" );
gif->has_transparency = TRUE;
}
@ -837,31 +846,17 @@ vips_foreign_load_gif_build_cmap( VipsForeignLoadGif *gif )
static void
vips_foreign_load_gif_render_line( VipsForeignLoadGif *gif,
int width, VipsPel * restrict q, VipsPel * restrict p )
int width, VipsPel * restrict dst )
{
guint32 *iq;
guint32 *idst = (guint32 *) dst;
int x;
iq = (guint32 *) q;
for( x = 0; x < width; x++ ) {
VipsPel v = p[x];
VipsPel v = gif->line[x];
if( v == gif->transparency ) {
/* In DISPOSE_DO_NOT mode, the previous frame shows
* through (ie. we do nothing). In all other modes,
* it's just transparent.
*
* Many GIFs use DISPOSAL_UNSPECIFIED to mean DO_NOT,
* so use that for previous frame as well.
*/
if( gif->dispose != DISPOSE_DO_NOT &&
gif->dispose != DISPOSAL_UNSPECIFIED )
iq[x] = 0;
}
else
/* Blast in the RGBA for this value.
*/
iq[x] = gif->cmap[v];
if( v != gif->transparent_index )
idst[x] = gif->cmap[v];
}
}
@ -882,26 +877,14 @@ vips_foreign_load_gif_render( VipsForeignLoadGif *gif )
*/
vips_foreign_load_gif_build_cmap( gif );
/* BACKGROUND means we reset the frame to 0 (transparent) before we
* render the next set of pixels.
*/
if( gif->dispose == DISPOSE_BACKGROUND )
memset( VIPS_IMAGE_ADDR( gif->frame, 0, 0 ), 0,
VIPS_IMAGE_SIZEOF_IMAGE( gif->frame ) );
/* PREVIOUS means we init the frame with the frame before last, ie. we
* undo the last render.
*
* Anything other than PREVIOUS, we must update the previous buffer,
/* PREVIOUS means we init the frame with the last un-disposed frame.
* So the last un-disposed frame is used as a backdrop for the new
* frame.
*/
if( gif->dispose == DISPOSE_PREVIOUS )
memcpy( VIPS_IMAGE_ADDR( gif->frame, 0, 0 ),
memcpy( VIPS_IMAGE_ADDR( gif->scratch, 0, 0 ),
VIPS_IMAGE_ADDR( gif->previous, 0, 0 ),
VIPS_IMAGE_SIZEOF_IMAGE( gif->frame ) );
else
memcpy( VIPS_IMAGE_ADDR( gif->previous, 0, 0 ),
VIPS_IMAGE_ADDR( gif->frame, 0, 0 ),
VIPS_IMAGE_SIZEOF_IMAGE( gif->frame ) );
VIPS_IMAGE_SIZEOF_IMAGE( gif->scratch ) );
/* giflib does not check that the Left / Top / Width / Height for this
* Image is inside the canvas.
@ -935,20 +918,20 @@ vips_foreign_load_gif_render( VipsForeignLoadGif *gif )
for( i = 0; i < 4; i++ ) {
int y;
for( y = InterlacedOffset[i];
y < file->Image.Height;
for( y = InterlacedOffset[i]; y < file->Image.Height;
y += InterlacedJumps[i] ) {
VipsPel *q = VIPS_IMAGE_ADDR( gif->frame,
VipsPel *dst = VIPS_IMAGE_ADDR( gif->scratch,
file->Image.Left, file->Image.Top + y );
if( DGifGetLine( gif->file, gif->line,
file->Image.Width ) == GIF_ERROR ) {
if( DGifGetLine( gif->file,
gif->line, file->Image.Width ) ==
GIF_ERROR ) {
vips_foreign_load_gif_error( gif );
return( -1 );
}
vips_foreign_load_gif_render_line( gif,
file->Image.Width, q, gif->line );
file->Image.Width, dst );
}
}
}
@ -961,20 +944,66 @@ vips_foreign_load_gif_render( VipsForeignLoadGif *gif )
file->Image.Left, file->Image.Top );
for( y = 0; y < file->Image.Height; y++ ) {
VipsPel *q = VIPS_IMAGE_ADDR( gif->frame,
VipsPel *dst = VIPS_IMAGE_ADDR( gif->scratch,
file->Image.Left, file->Image.Top + y );
if( DGifGetLine( gif->file, gif->line,
file->Image.Width ) == GIF_ERROR ) {
if( DGifGetLine( gif->file,
gif->line, file->Image.Width ) == GIF_ERROR ) {
vips_foreign_load_gif_error( gif );
return( -1 );
}
vips_foreign_load_gif_render_line( gif,
file->Image.Width, q, gif->line );
file->Image.Width, dst );
}
}
/* Copy the result to frame, which then is picked up from outside
*/
memcpy( VIPS_IMAGE_ADDR( gif->frame, 0, 0 ),
VIPS_IMAGE_ADDR(gif->scratch, 0, 0 ),
VIPS_IMAGE_SIZEOF_IMAGE( gif->frame ) );
if( gif->dispose == DISPOSE_BACKGROUND ) {
/* BACKGROUND means we reset the frame to transparent before we
* render the next set of pixels.
*/
guint32 *q = (guint32 *) VIPS_IMAGE_ADDR( gif->scratch,
file->Image.Left, file->Image.Top );
/* What we write for transparent pixels. We want RGB to be
* 255, and A to be 0.
*/
guint32 ink = GUINT32_TO_BE( 0xffffff00 );
int x, y;
/* Generate the first line a pixel at a time, memcpy() for
* subsequent lines.
*/
if( file->Image.Height > 0 )
for( x = 0; x < file->Image.Width; x++ )
q[x] = ink;
for( y = 1; y < file->Image.Height; y++ )
memcpy( q + gif->scratch->Xsize * y,
q,
file->Image.Width * sizeof( guint32 ) );
}
else if( gif->dispose == DISPOSAL_UNSPECIFIED ||
gif->dispose == DISPOSE_DO_NOT )
/* Copy the frame to previous, so it can be restored if
* DISPOSE_PREVIOUS is specified in a later frame.
*/
memcpy( VIPS_IMAGE_ADDR( gif->previous, 0, 0 ),
VIPS_IMAGE_ADDR(gif->frame, 0, 0 ),
VIPS_IMAGE_SIZEOF_IMAGE( gif->previous ) );
/* Reset values, as Graphic Control Extension is optional
*/
gif->dispose = DISPOSAL_UNSPECIFIED;
gif->transparent_index = NO_TRANSPARENT_INDEX;
return( 0 );
}
@ -1009,21 +1038,21 @@ vips_foreign_load_gif_extension( VipsForeignLoadGif *gif )
if( extension &&
ext_code == GRAPHICS_EXT_FUNC_CODE &&
extension[0] == 4 ) {
/* Bytes are flags, delay low, delay high,
* transparency. Flag bit 1 means transparency
* is being set.
int flags = extension[1];
/* Bytes are flags, delay low, delay high, transparency.
* Flag bit 1 means transparency is being set.
*/
gif->transparency = -1;
if( extension[1] & 0x1 ) {
gif->transparency = extension[4];
gif->transparent_index = (flags & TRANSPARENT_MASK) ?
extension[4] : NO_TRANSPARENT_INDEX;
VIPS_DEBUG_MSG( "vips_foreign_load_gif_extension: "
"transparency = %d\n", gif->transparency );
}
"transparency = %d\n", gif->transparent_index );
/* Set the current dispose mode. This is read during frame load
* to set the meaning of background and transparent pixels.
*/
gif->dispose = (extension[1] >> 2) & 0x7;
gif->dispose = (flags >> DISPOSE_SHIFT) & DISPOSE_MASK;
VIPS_DEBUG_MSG( "vips_foreign_load_gif_extension: "
"dispose = %s\n", dispose2str( gif->dispose ) );
}
@ -1185,6 +1214,23 @@ vips_foreign_load_gif_minimise( VipsObject *object, VipsForeignLoadGif *gif )
vips_source_minimise( gif->source );
}
static VipsImage *
vips_foreign_load_gif_temp( VipsForeignLoadGif *gif )
{
VipsImage *temp;
temp = vips_image_new_memory();
vips_image_init_fields( temp,
gif->file->SWidth, gif->file->SHeight, 4, VIPS_FORMAT_UCHAR,
VIPS_CODING_NONE, VIPS_INTERPRETATION_sRGB, 1.0, 1.0 );
if( vips_image_write_prepare( temp ) ) {
VIPS_UNREF( temp );
return( NULL );
}
return( temp );
}
static int
vips_foreign_load_gif_load( VipsForeignLoad *load )
{
@ -1197,25 +1243,11 @@ vips_foreign_load_gif_load( VipsForeignLoad *load )
if( vips_foreign_load_gif_open_giflib( gif ) )
return( -1 );
/* Make the memory image we accumulate pixels in. We always accumulate
* to RGBA, then trim down to whatever the output image needs on
* _generate.
/* Set of temp images we use during rendering.
*/
gif->frame = vips_image_new_memory();
vips_image_init_fields( gif->frame,
gif->file->SWidth, gif->file->SHeight, 4, VIPS_FORMAT_UCHAR,
VIPS_CODING_NONE, VIPS_INTERPRETATION_sRGB, 1.0, 1.0 );
if( vips_image_write_prepare( gif->frame ) )
return( -1 );
/* A copy of the previous state of the frame, in case we have to
* process a DISPOSE_PREVIOUS.
*/
gif->previous = vips_image_new_memory();
vips_image_init_fields( gif->previous,
gif->file->SWidth, gif->file->SHeight, 4, VIPS_FORMAT_UCHAR,
VIPS_CODING_NONE, VIPS_INTERPRETATION_sRGB, 1.0, 1.0 );
if( vips_image_write_prepare( gif->previous ) )
if( !(gif->frame = vips_foreign_load_gif_temp( gif )) ||
!(gif->scratch = vips_foreign_load_gif_temp( gif )) ||
!(gif->previous = vips_foreign_load_gif_temp( gif )) )
return( -1 );
/* Make the output pipeline.
@ -1282,12 +1314,12 @@ static void
vips_foreign_load_gif_init( VipsForeignLoadGif *gif )
{
gif->n = 1;
gif->transparency = -1;
gif->transparent_index = NO_TRANSPARENT_INDEX;
gif->delays = NULL;
gif->delays_length = 0;
gif->loop = 1;
gif->comment = NULL;
gif->dispose = 0;
gif->dispose = DISPOSAL_UNSPECIFIED;
vips_foreign_load_gif_allocate_delays( gif );
}

View File

@ -27,6 +27,11 @@ SVG_FILE = os.path.join(IMAGES, "logo.svg")
SVGZ_FILE = os.path.join(IMAGES, "logo.svgz")
SVG_GZ_FILE = os.path.join(IMAGES, "logo.svg.gz")
GIF_ANIM_FILE = os.path.join(IMAGES, "cogs.gif")
GIF_ANIM_EXPECTED_PNG_FILE = os.path.join(IMAGES, "cogs.png")
GIF_ANIM_DISPOSE_BACKGROUND_FILE = os.path.join(IMAGES, "dispose-background.gif")
GIF_ANIM_DISPOSE_BACKGROUND_EXPECTED_PNG_FILE = os.path.join(IMAGES, "dispose-background.png")
GIF_ANIM_DISPOSE_PREVIOUS_FILE = os.path.join(IMAGES, "dispose-previous.gif")
GIF_ANIM_DISPOSE_PREVIOUS_EXPECTED_PNG_FILE = os.path.join(IMAGES, "dispose-previous.png")
DICOM_FILE = os.path.join(IMAGES, "dicom_test_image.dcm")
BMP_FILE = os.path.join(IMAGES, "MARBLES.BMP")
NIFTI_FILE = os.path.join(IMAGES, "avg152T1_LR_nifti.nii.gz")

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -1,5 +1,5 @@
# vim: set fileencoding=utf-8 :
import filecmp
import sys
import os
import shutil
@ -12,6 +12,9 @@ from helpers import \
ANALYZE_FILE, GIF_FILE, WEBP_FILE, EXR_FILE, FITS_FILE, OPENSLIDE_FILE, \
PDF_FILE, SVG_FILE, SVGZ_FILE, SVG_GZ_FILE, GIF_ANIM_FILE, DICOM_FILE, \
BMP_FILE, NIFTI_FILE, ICO_FILE, HEIC_FILE, TRUNCATED_FILE, \
GIF_ANIM_EXPECTED_PNG_FILE, \
GIF_ANIM_DISPOSE_BACKGROUND_FILE, GIF_ANIM_DISPOSE_BACKGROUND_EXPECTED_PNG_FILE, \
GIF_ANIM_DISPOSE_PREVIOUS_FILE, GIF_ANIM_DISPOSE_PREVIOUS_EXPECTED_PNG_FILE, \
temp_filename, assert_almost_equal_objects, have, skip_if_no
@ -706,6 +709,38 @@ class TestForeign:
x2 = pyvips.Image.new_from_file(GIF_ANIM_FILE, page=1, n=-1)
assert x2.height == 4 * x1.height
animation = pyvips.Image.new_from_file(GIF_ANIM_FILE, n=-1)
filename = temp_filename(self.tempdir, '.png')
animation.write_to_file(filename)
# Uncomment to see output file
# animation.write_to_file('cogs.png')
assert filecmp.cmp(GIF_ANIM_EXPECTED_PNG_FILE, filename, shallow=False)
@skip_if_no("gifload")
def test_gifload_animation_dispose_background(self):
animation = pyvips.Image.new_from_file(GIF_ANIM_DISPOSE_BACKGROUND_FILE, n=-1)
filename = temp_filename(self.tempdir, '.png')
animation.write_to_file(filename)
# Uncomment to see output file
# animation.write_to_file('dispose-background.png')
assert filecmp.cmp(GIF_ANIM_DISPOSE_BACKGROUND_EXPECTED_PNG_FILE, filename, shallow=False)
@skip_if_no("gifload")
def test_gifload_animation_dispose_previous(self):
animation = pyvips.Image.new_from_file(GIF_ANIM_DISPOSE_PREVIOUS_FILE, n=-1)
filename = temp_filename(self.tempdir, '.png')
animation.write_to_file(filename)
# Uncomment to see output file
# animation.write_to_file('dispose-previous.png')
assert filecmp.cmp(GIF_ANIM_DISPOSE_PREVIOUS_EXPECTED_PNG_FILE, filename, shallow=False)
@skip_if_no("svgload")
def test_svgload(self):
def svg_valid(im):