2019-08-15 15:30:25 +02:00
|
|
|
/* load a GIF with libnsgif
|
|
|
|
*
|
|
|
|
* 6/10/18
|
|
|
|
* - from gifload.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This file is part of VIPS.
|
|
|
|
|
|
|
|
VIPS is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-09-06 18:24:55 +02:00
|
|
|
#define VERBOSE
|
2019-09-06 13:57:03 +02:00
|
|
|
#define VIPS_DEBUG
|
2019-09-15 18:46:36 +02:00
|
|
|
*/
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
#include <vips/intl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
#include <vips/buf.h>
|
|
|
|
#include <vips/internal.h>
|
|
|
|
#include <vips/debug.h>
|
|
|
|
|
2019-09-06 17:47:35 +02:00
|
|
|
/* TODO:
|
2019-09-18 18:34:49 +02:00
|
|
|
*
|
2019-09-06 17:47:35 +02:00
|
|
|
* - libnsgif does not seem to support comment metadata
|
2019-09-06 18:24:55 +02:00
|
|
|
*
|
2020-07-03 12:02:03 +02:00
|
|
|
* - it always loads the entire source file into memory
|
|
|
|
*
|
2019-09-06 18:24:55 +02:00
|
|
|
* Notes:
|
|
|
|
*
|
|
|
|
* - hard to detect mono images -- local_colour_table in libnsgif is only set
|
|
|
|
* when we decode a frame, so we can't tell just from init whether any
|
2021-02-28 13:20:05 +01:00
|
|
|
* frames have colour info
|
2019-09-06 18:24:55 +02:00
|
|
|
*
|
|
|
|
* - don't bother detecting alpha -- if we can't detect RGB, alpha won't help
|
|
|
|
* much
|
|
|
|
*
|
2019-09-06 17:47:35 +02:00
|
|
|
*/
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
#ifdef HAVE_NSGIF
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
#include <libnsgif/libnsgif.h>
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
#define VIPS_TYPE_FOREIGN_LOAD_GIF (vips_foreign_load_nsgif_get_type())
|
2019-09-06 17:47:35 +02:00
|
|
|
#define VIPS_FOREIGN_LOAD_GIF( obj ) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_TYPE_FOREIGN_LOAD_GIF, VipsForeignLoadNsgif ))
|
2019-09-06 17:47:35 +02:00
|
|
|
#define VIPS_FOREIGN_LOAD_GIF_CLASS( klass ) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST( (klass), \
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_TYPE_FOREIGN_LOAD_GIF, VipsForeignLoadNsgifClass))
|
2019-09-06 17:47:35 +02:00
|
|
|
#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), \
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_TYPE_FOREIGN_LOAD_GIF, VipsForeignLoadNsgifClass ))
|
2019-09-06 17:47:35 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
typedef struct _VipsForeignLoadNsgif {
|
2019-08-15 15:30:25 +02:00
|
|
|
VipsForeignLoad parent_object;
|
|
|
|
|
|
|
|
/* Load this page (frame number).
|
|
|
|
*/
|
|
|
|
int page;
|
|
|
|
|
|
|
|
/* Load this many pages.
|
|
|
|
*/
|
|
|
|
int n;
|
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
/* Load from this source (set by subclasses).
|
|
|
|
*/
|
|
|
|
VipsSource *source;
|
|
|
|
|
2019-08-15 15:30:25 +02:00
|
|
|
/* The animation created by libnsgif.
|
|
|
|
*/
|
2019-09-06 13:57:03 +02:00
|
|
|
gif_animation *anim;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
/* The data/size pair we pass to libnsgif.
|
|
|
|
*/
|
|
|
|
unsigned char *data;
|
2019-09-06 13:57:03 +02:00
|
|
|
size_t size;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2019-09-18 18:34:49 +02:00
|
|
|
/* The frame_count, after we have removed undisplayable frames.
|
|
|
|
*/
|
|
|
|
int frame_count_displayable;
|
|
|
|
|
2021-03-28 14:33:16 +02:00
|
|
|
/* Delays between frames (in milliseconds). Array of length
|
|
|
|
* @frame_count_displayable.
|
2019-09-07 13:13:25 +02:00
|
|
|
*/
|
2019-09-15 18:08:48 +02:00
|
|
|
int *delay;
|
|
|
|
|
|
|
|
/* A single centisecond value for compatibility.
|
|
|
|
*/
|
|
|
|
int gif_delay;
|
2019-09-07 13:13:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
} VipsForeignLoadNsgif;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
typedef VipsForeignLoadClass VipsForeignLoadNsgifClass;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
G_DEFINE_ABSTRACT_TYPE( VipsForeignLoadNsgif, vips_foreign_load_nsgif,
|
2019-08-15 15:30:25 +02:00
|
|
|
VIPS_TYPE_FOREIGN_LOAD );
|
|
|
|
|
|
|
|
static const char *
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_errstr( gif_result result )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
|
|
|
switch( result ) {
|
2019-09-06 17:47:35 +02:00
|
|
|
case GIF_WORKING:
|
2019-08-15 15:30:25 +02:00
|
|
|
return( _( "Working" ) );
|
|
|
|
|
|
|
|
case GIF_OK:
|
|
|
|
return( _( "OK" ) );
|
|
|
|
|
|
|
|
case GIF_INSUFFICIENT_FRAME_DATA:
|
|
|
|
return( _( "Insufficient data to complete frame" ) );
|
|
|
|
|
|
|
|
case GIF_FRAME_DATA_ERROR:
|
|
|
|
return( _( "GIF frame data error" ) );
|
|
|
|
|
|
|
|
case GIF_INSUFFICIENT_DATA:
|
|
|
|
return( _( "Insufficient data to do anything" ) );
|
|
|
|
|
|
|
|
case GIF_DATA_ERROR:
|
|
|
|
return( _( "GIF header data error" ) );
|
|
|
|
|
|
|
|
case GIF_INSUFFICIENT_MEMORY:
|
|
|
|
return( _( "Insuficient memory to process" ) );
|
|
|
|
|
|
|
|
case GIF_FRAME_NO_DISPLAY:
|
|
|
|
return( _( "No display" ) );
|
|
|
|
|
|
|
|
case GIF_END_OF_FRAME:
|
|
|
|
return( _( "At end of frame" ) );
|
|
|
|
|
|
|
|
default:
|
|
|
|
return( _( "Unknown error" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:47:35 +02:00
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_error( VipsForeignLoadNsgif *gif, gif_result result )
|
2019-09-06 17:47:35 +02:00
|
|
|
{
|
|
|
|
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( gif );
|
|
|
|
|
|
|
|
vips_error( class->nickname, "%s",
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_errstr( result ) );
|
2019-09-06 17:47:35 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 15:30:25 +02:00
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_dispose( GObject *gobject )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
2021-02-28 13:20:05 +01:00
|
|
|
VipsForeignLoadNsgif *gif = (VipsForeignLoadNsgif *) gobject;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_DEBUG_MSG( "vips_foreign_load_nsgif_dispose:\n" );
|
2019-09-06 13:57:03 +02:00
|
|
|
|
2019-09-15 18:08:48 +02:00
|
|
|
if( gif->anim ) {
|
|
|
|
gif_finalise( gif->anim );
|
|
|
|
VIPS_FREE( gif->anim );
|
|
|
|
}
|
2020-07-07 16:50:44 +02:00
|
|
|
VIPS_UNREF( gif->source );
|
2019-09-15 18:08:48 +02:00
|
|
|
VIPS_FREE( gif->delay );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
G_OBJECT_CLASS( vips_foreign_load_nsgif_parent_class )->
|
2019-08-15 15:30:25 +02:00
|
|
|
dispose( gobject );
|
|
|
|
}
|
|
|
|
|
|
|
|
static VipsForeignFlags
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_get_flags_filename( const char *filename )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
2019-09-06 13:57:03 +02:00
|
|
|
return( VIPS_FOREIGN_SEQUENTIAL );
|
2019-08-15 15:30:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static VipsForeignFlags
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_get_flags( VipsForeignLoad *load )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
2019-09-06 13:57:03 +02:00
|
|
|
return( VIPS_FOREIGN_SEQUENTIAL );
|
2019-08-15 15:30:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_is_a_source( VipsSource *source )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
2020-07-07 16:50:44 +02:00
|
|
|
const unsigned char *data;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
if( (data = vips_source_sniff( source, 4 )) &&
|
|
|
|
data[0] == 'G' &&
|
|
|
|
data[1] == 'I' &&
|
|
|
|
data[2] == 'F' &&
|
|
|
|
data[3] == '8' )
|
|
|
|
return( TRUE );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
return( FALSE );
|
2019-08-15 15:30:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 18:24:55 +02:00
|
|
|
#ifdef VERBOSE
|
2019-09-06 13:57:03 +02:00
|
|
|
static void
|
|
|
|
print_frame( gif_frame *frame )
|
|
|
|
{
|
|
|
|
printf( "frame:\n" );
|
|
|
|
printf( " display = %d\n", frame->display );
|
|
|
|
printf( " frame_delay = %d\n", frame->frame_delay );
|
|
|
|
printf( " virgin = %d\n", frame->virgin );
|
|
|
|
printf( " opaque = %d\n", frame->opaque );
|
|
|
|
printf( " redraw_required = %d\n", frame->redraw_required );
|
|
|
|
printf( " disposal_method = %d\n", frame->disposal_method );
|
|
|
|
printf( " transparency = %d\n", frame->transparency );
|
|
|
|
printf( " transparency_index = %d\n", frame->transparency_index );
|
|
|
|
printf( " redraw_x = %d\n", frame->redraw_x );
|
|
|
|
printf( " redraw_y = %d\n", frame->redraw_y );
|
|
|
|
printf( " redraw_width = %d\n", frame->redraw_width );
|
|
|
|
printf( " redraw_height = %d\n", frame->redraw_height );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_animation( gif_animation *anim )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf( "animation:\n" );
|
|
|
|
printf( " width = %d\n", anim->width );
|
|
|
|
printf( " height = %d\n", anim->height );
|
|
|
|
printf( " frame_count = %d\n", anim->frame_count );
|
|
|
|
printf( " frame_count_partial = %d\n", anim->frame_count_partial );
|
|
|
|
printf( " decoded_frame = %d\n", anim->decoded_frame );
|
|
|
|
printf( " frame_image = %p\n", anim->frame_image );
|
|
|
|
printf( " loop_count = %d\n", anim->loop_count );
|
|
|
|
printf( " frame_holders = %d\n", anim->frame_holders );
|
|
|
|
printf( " background_index = %d\n", anim->background_index );
|
|
|
|
printf( " colour_table_size = %d\n", anim->colour_table_size );
|
|
|
|
printf( " global_colours = %d\n", anim->global_colours );
|
|
|
|
printf( " global_colour_table = %p\n", anim->global_colour_table );
|
|
|
|
printf( " local_colour_table = %p\n", anim->local_colour_table );
|
|
|
|
|
2019-09-06 17:47:35 +02:00
|
|
|
for( i = 0; i < anim->frame_holders; i++ ) {
|
2019-09-06 13:57:03 +02:00
|
|
|
printf( "%d ", i );
|
|
|
|
print_frame( &anim->frames[i] );
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 18:24:55 +02:00
|
|
|
#endif /*VERBOSE*/
|
2019-09-06 13:57:03 +02:00
|
|
|
|
2019-09-06 17:47:35 +02:00
|
|
|
static int
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_set_header( VipsForeignLoadNsgif *gif,
|
2019-09-18 18:34:49 +02:00
|
|
|
VipsImage *image )
|
2019-09-06 17:47:35 +02:00
|
|
|
{
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_DEBUG_MSG( "vips_foreign_load_nsgif_set_header:\n" );
|
2019-09-15 18:08:48 +02:00
|
|
|
|
2019-09-06 17:47:35 +02:00
|
|
|
vips_image_init_fields( image,
|
2019-09-06 18:24:55 +02:00
|
|
|
gif->anim->width, gif->anim->height * gif->n, 4,
|
2019-09-06 17:47:35 +02:00
|
|
|
VIPS_FORMAT_UCHAR, VIPS_CODING_NONE,
|
2019-09-06 18:24:55 +02:00
|
|
|
VIPS_INTERPRETATION_sRGB, 1.0, 1.0 );
|
2019-09-06 17:47:35 +02:00
|
|
|
vips_image_pipelinev( image, VIPS_DEMAND_STYLE_FATSTRIP, NULL );
|
|
|
|
|
|
|
|
if( vips_object_argument_isset( VIPS_OBJECT( gif ), "n" ) )
|
|
|
|
vips_image_set_int( image,
|
|
|
|
VIPS_META_PAGE_HEIGHT, gif->anim->height );
|
2019-09-18 19:06:33 +02:00
|
|
|
vips_image_set_int( image, VIPS_META_N_PAGES,
|
|
|
|
gif->frame_count_displayable );
|
2021-03-28 14:33:16 +02:00
|
|
|
vips_image_set_int( image, "loop", gif->anim->loop_count );
|
|
|
|
vips_image_set_array_int( image, "delay",
|
|
|
|
gif->delay, gif->frame_count_displayable );
|
2019-09-06 17:47:35 +02:00
|
|
|
|
2021-03-28 14:33:16 +02:00
|
|
|
if( gif->anim->global_colours &&
|
|
|
|
gif->anim->global_colour_table &&
|
|
|
|
gif->anim->background_index >= 0 &&
|
|
|
|
gif->anim->background_index < gif->anim->colour_table_size ) {
|
|
|
|
int index = gif->anim->background_index;
|
|
|
|
unsigned char *entry = (unsigned char *)
|
|
|
|
&gif->anim->global_colour_table[index];
|
|
|
|
|
|
|
|
double array[3];
|
|
|
|
|
|
|
|
array[0] = entry[0];
|
|
|
|
array[1] = entry[1];
|
|
|
|
array[2] = entry[2];
|
|
|
|
|
|
|
|
vips_image_set_array_double( image, "background", array, 3 );
|
|
|
|
}
|
|
|
|
|
2021-03-28 14:38:41 +02:00
|
|
|
VIPS_SETSTR( image->filename,
|
|
|
|
vips_connection_filename( VIPS_CONNECTION( gif->source ) ) );
|
|
|
|
|
2021-03-28 14:33:16 +02:00
|
|
|
/* DEPRECATED "gif-loop"
|
|
|
|
*
|
|
|
|
* Not the correct behavior as loop=1 became gif-loop=0
|
|
|
|
* but we want to keep the old behavior untouched!
|
|
|
|
*/
|
|
|
|
vips_image_set_int( image,
|
|
|
|
"gif-loop", gif->anim->loop_count == 0 ?
|
|
|
|
0 : gif->anim->loop_count - 1 );
|
2019-09-07 13:13:25 +02:00
|
|
|
|
2019-09-15 18:08:48 +02:00
|
|
|
/* The deprecated gif-delay field is in centiseconds.
|
2019-09-06 17:47:35 +02:00
|
|
|
*/
|
2019-09-15 18:08:48 +02:00
|
|
|
vips_image_set_int( image, "gif-delay", gif->gif_delay );
|
2019-09-06 17:47:35 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
/* Scan the GIF as quickly as we can and extract transparency, bands, pages,
|
|
|
|
* etc.
|
|
|
|
*
|
|
|
|
* Don't flag any errors unless we have to: we want to work for corrupt or
|
|
|
|
* malformed GIFs.
|
|
|
|
*
|
|
|
|
* Close as soon as we can to free up the fd.
|
|
|
|
*/
|
2019-08-15 15:30:25 +02:00
|
|
|
static int
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_header( VipsForeignLoad *load )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
2020-07-07 16:50:44 +02:00
|
|
|
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( load );
|
2021-02-28 13:20:05 +01:00
|
|
|
VipsForeignLoadNsgif *gif = (VipsForeignLoadNsgif *) load;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
const void *data;
|
|
|
|
size_t size;
|
2019-08-15 15:30:25 +02:00
|
|
|
gif_result result;
|
2019-09-07 13:13:25 +02:00
|
|
|
int i;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_DEBUG_MSG( "vips_foreign_load_nsgif_header:\n" );
|
2019-09-06 13:57:03 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
/* We map in the image, then minimise to close any underlying file
|
|
|
|
* object. This won't unmap.
|
|
|
|
*/
|
|
|
|
if( !(data = vips_source_map( gif->source, &size )) )
|
|
|
|
return( -1 );
|
|
|
|
vips_source_minimise( gif->source );
|
|
|
|
|
|
|
|
result = gif_initialise( gif->anim, size, (void *) data );
|
2019-09-06 17:47:35 +02:00
|
|
|
VIPS_DEBUG_MSG( "gif_initialise() = %d\n", result );
|
2019-09-06 18:24:55 +02:00
|
|
|
#ifdef VERBOSE
|
2019-09-06 13:57:03 +02:00
|
|
|
print_animation( gif->anim );
|
2019-09-06 18:24:55 +02:00
|
|
|
#endif /*VERBOSE*/
|
2019-09-06 13:57:03 +02:00
|
|
|
if( result != GIF_OK &&
|
2019-09-06 17:47:35 +02:00
|
|
|
result != GIF_WORKING &&
|
|
|
|
result != GIF_INSUFFICIENT_FRAME_DATA ) {
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_error( gif, result );
|
2019-09-06 17:47:35 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
2019-09-15 18:46:36 +02:00
|
|
|
else if( result == GIF_INSUFFICIENT_FRAME_DATA &&
|
|
|
|
load->fail ) {
|
|
|
|
vips_error( class->nickname, "%s", _( "truncated GIF" ) );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2019-09-18 18:34:49 +02:00
|
|
|
|
|
|
|
/* Many GIFs have dead frames at the end. Remove these from our count.
|
|
|
|
*/
|
|
|
|
for( i = gif->anim->frame_count - 1;
|
|
|
|
i >= 0 && !gif->anim->frames[i].display; i-- )
|
|
|
|
;
|
|
|
|
gif->frame_count_displayable = i + 1;
|
|
|
|
#ifdef VERBOSE
|
|
|
|
if( gif->frame_count_displayable != gif->anim->frame_count )
|
2021-02-28 13:20:05 +01:00
|
|
|
printf( "vips_foreign_load_nsgif_open: "
|
2019-09-18 18:34:49 +02:00
|
|
|
"removed %d undisplayable frames\n",
|
|
|
|
gif->anim->frame_count - gif->frame_count_displayable );
|
|
|
|
#endif /*VERBOSE*/
|
|
|
|
|
|
|
|
if( !gif->frame_count_displayable ) {
|
2019-09-06 17:47:35 +02:00
|
|
|
vips_error( class->nickname, "%s", _( "no frames in GIF" ) );
|
2019-09-06 13:57:03 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2019-09-06 18:24:55 +02:00
|
|
|
if( gif->n == -1 )
|
2019-09-18 18:34:49 +02:00
|
|
|
gif->n = gif->frame_count_displayable - gif->page;
|
2019-09-06 17:47:35 +02:00
|
|
|
|
2019-09-06 18:24:55 +02:00
|
|
|
if( gif->page < 0 ||
|
|
|
|
gif->n <= 0 ||
|
2019-09-18 18:34:49 +02:00
|
|
|
gif->page + gif->n > gif->frame_count_displayable ) {
|
2019-09-06 18:24:55 +02:00
|
|
|
vips_error( class->nickname, "%s", _( "bad page number" ) );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2019-09-06 17:47:35 +02:00
|
|
|
|
2019-09-07 13:13:25 +02:00
|
|
|
/* In ms, frame_delay in cs.
|
|
|
|
*/
|
2019-09-18 19:06:33 +02:00
|
|
|
VIPS_FREE( gif->delay );
|
2021-03-28 14:33:16 +02:00
|
|
|
if( !(gif->delay = VIPS_ARRAY( NULL,
|
|
|
|
gif->frame_count_displayable, int )) )
|
2019-09-07 13:13:25 +02:00
|
|
|
return( -1 );
|
2021-03-28 14:33:16 +02:00
|
|
|
for( i = 0; i < gif->frame_count_displayable; i++ )
|
|
|
|
gif->delay[i] = 10 * gif->anim->frames[i].frame_delay;
|
2019-09-07 13:13:25 +02:00
|
|
|
|
2019-09-15 18:08:48 +02:00
|
|
|
gif->gif_delay = gif->anim->frames[0].frame_delay;
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_set_header( gif, load->out );
|
2019-09-06 17:47:35 +02:00
|
|
|
|
2019-09-06 13:57:03 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_generate( VipsRegion *or,
|
2019-09-06 13:57:03 +02:00
|
|
|
void *seq, void *a, void *b, gboolean *stop )
|
|
|
|
{
|
|
|
|
VipsRect *r = &or->valid;
|
2021-02-28 13:20:05 +01:00
|
|
|
VipsForeignLoadNsgif *gif = (VipsForeignLoadNsgif *) a;
|
2019-09-06 13:57:03 +02:00
|
|
|
|
2019-09-06 17:47:35 +02:00
|
|
|
int y;
|
|
|
|
|
2019-09-06 18:24:55 +02:00
|
|
|
#ifdef VERBOSE
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_DEBUG_MSG( "vips_foreign_load_nsgif_generate: "
|
2019-09-06 17:47:35 +02:00
|
|
|
"top = %d, height = %d\n", r->top, r->height );
|
2019-09-06 18:24:55 +02:00
|
|
|
#endif /*VERBOSE*/
|
2019-09-06 17:47:35 +02:00
|
|
|
|
|
|
|
for( y = 0; y < r->height; y++ ) {
|
|
|
|
/* The page for this output line, and the line number in page.
|
|
|
|
*/
|
|
|
|
int page = (r->top + y) / gif->anim->height + gif->page;
|
|
|
|
int line = (r->top + y) % gif->anim->height;
|
|
|
|
|
|
|
|
gif_result result;
|
|
|
|
VipsPel *p, *q;
|
|
|
|
|
|
|
|
g_assert( line >= 0 && line < gif->anim->height );
|
2019-09-18 18:34:49 +02:00
|
|
|
g_assert( page >= 0 && page < gif->frame_count_displayable );
|
2019-09-06 17:47:35 +02:00
|
|
|
|
|
|
|
if( gif->anim->decoded_frame != page ) {
|
|
|
|
result = gif_decode_frame( gif->anim, page );
|
|
|
|
VIPS_DEBUG_MSG( " gif_decode_frame(%d) = %d\n",
|
|
|
|
page, result );
|
|
|
|
if( result != GIF_OK ) {
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_error( gif, result );
|
2019-09-06 17:47:35 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
2019-09-06 18:24:55 +02:00
|
|
|
#ifdef VERBOSE
|
2019-09-06 17:47:35 +02:00
|
|
|
print_animation( gif->anim );
|
2019-09-06 18:24:55 +02:00
|
|
|
#endif /*VERBOSE*/
|
2019-09-06 13:57:03 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 17:47:35 +02:00
|
|
|
p = gif->anim->frame_image +
|
|
|
|
line * gif->anim->width * sizeof( int );
|
|
|
|
q = VIPS_REGION_ADDR( or, 0, r->top + y );
|
|
|
|
memcpy( q, p, VIPS_REGION_SIZEOF_LINE( or ) );
|
|
|
|
}
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-09-06 13:57:03 +02:00
|
|
|
static int
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_load( VipsForeignLoad *load )
|
2019-09-06 13:57:03 +02:00
|
|
|
{
|
2021-02-28 13:20:05 +01:00
|
|
|
VipsForeignLoadNsgif *gif = (VipsForeignLoadNsgif *) load;
|
2019-09-06 13:57:03 +02:00
|
|
|
VipsImage **t = (VipsImage **)
|
|
|
|
vips_object_local_array( VIPS_OBJECT( load ), 4 );
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
VIPS_DEBUG_MSG( "vips_foreign_load_nsgif_load:\n" );
|
2019-09-06 13:57:03 +02:00
|
|
|
|
|
|
|
/* Make the output pipeline.
|
|
|
|
*/
|
|
|
|
t[0] = vips_image_new();
|
2021-02-28 13:20:05 +01:00
|
|
|
if( vips_foreign_load_nsgif_set_header( gif, t[0] ) )
|
2019-09-06 13:57:03 +02:00
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
/* Strips 8 pixels high to avoid too many tiny regions.
|
|
|
|
*/
|
|
|
|
if( vips_image_generate( t[0],
|
2021-02-28 13:20:05 +01:00
|
|
|
NULL, vips_foreign_load_nsgif_generate, NULL, gif, NULL ) ||
|
2019-09-06 13:57:03 +02:00
|
|
|
vips_sequential( t[0], &t[1],
|
|
|
|
"tile_height", VIPS__FATSTRIP_HEIGHT,
|
|
|
|
NULL ) ||
|
|
|
|
vips_image_write( t[1], load->real ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-08-15 15:30:25 +02:00
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_class_init( VipsForeignLoadNsgifClass *class )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
|
|
|
VipsObjectClass *object_class = (VipsObjectClass *) class;
|
2019-09-18 18:34:49 +02:00
|
|
|
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
|
2019-08-15 15:30:25 +02:00
|
|
|
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
gobject_class->dispose = vips_foreign_load_nsgif_dispose;
|
2019-08-15 15:30:25 +02:00
|
|
|
gobject_class->set_property = vips_object_set_property;
|
|
|
|
gobject_class->get_property = vips_object_get_property;
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
object_class->nickname = "gifload_base";
|
2019-08-15 15:30:25 +02:00
|
|
|
object_class->description = _( "load GIF with libnsgif" );
|
|
|
|
|
2019-09-18 18:34:49 +02:00
|
|
|
/* High priority, so that we handle vipsheader etc.
|
|
|
|
*/
|
|
|
|
foreign_class->priority = 50;
|
|
|
|
|
2019-08-15 15:30:25 +02:00
|
|
|
load_class->get_flags_filename =
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_get_flags_filename;
|
|
|
|
load_class->get_flags = vips_foreign_load_nsgif_get_flags;
|
|
|
|
load_class->header = vips_foreign_load_nsgif_header;
|
|
|
|
load_class->load = vips_foreign_load_nsgif_load;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
VIPS_ARG_INT( class, "page", 10,
|
|
|
|
_( "Page" ),
|
|
|
|
_( "Load this page from the file" ),
|
|
|
|
VIPS_ARGUMENT_OPTIONAL_INPUT,
|
2021-02-28 13:20:05 +01:00
|
|
|
G_STRUCT_OFFSET( VipsForeignLoadNsgif, page ),
|
2019-08-15 15:30:25 +02:00
|
|
|
0, 100000, 0 );
|
|
|
|
|
|
|
|
VIPS_ARG_INT( class, "n", 6,
|
|
|
|
_( "n" ),
|
|
|
|
_( "Load this many pages" ),
|
|
|
|
VIPS_ARGUMENT_OPTIONAL_INPUT,
|
2021-02-28 13:20:05 +01:00
|
|
|
G_STRUCT_OFFSET( VipsForeignLoadNsgif, n ),
|
2019-08-15 15:30:25 +02:00
|
|
|
-1, 100000, 1 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-15 18:08:48 +02:00
|
|
|
static void *
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_bitmap_create( int width, int height )
|
2019-09-15 18:08:48 +02:00
|
|
|
{
|
2021-03-28 20:23:06 +02:00
|
|
|
/* Check GIF dimensions fit within 16-bit unsigned.
|
2019-09-15 18:08:48 +02:00
|
|
|
*/
|
2021-03-28 20:23:06 +02:00
|
|
|
if( width <= 0 ||
|
|
|
|
width > 65535 ||
|
|
|
|
height <= 0 ||
|
|
|
|
height > 65535 ) {
|
|
|
|
vips_error( "gifload",
|
|
|
|
"%s", _( "dimensions out of range ") );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
return g_malloc0( width * height * 4 );
|
2019-09-15 18:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_bitmap_set_opaque( void *bitmap, bool opaque )
|
2019-09-15 18:08:48 +02:00
|
|
|
{
|
|
|
|
(void) opaque; /* unused */
|
|
|
|
(void) bitmap; /* unused */
|
2019-09-18 18:34:49 +02:00
|
|
|
g_assert( bitmap );
|
2019-09-15 18:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_bitmap_test_opaque( void *bitmap )
|
2019-09-15 18:08:48 +02:00
|
|
|
{
|
|
|
|
(void) bitmap; /* unused */
|
2019-09-18 18:34:49 +02:00
|
|
|
g_assert( bitmap );
|
|
|
|
|
|
|
|
return( false );
|
2019-09-15 18:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char *
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_bitmap_get_buffer( void *bitmap )
|
2019-09-15 18:08:48 +02:00
|
|
|
{
|
2019-09-18 18:34:49 +02:00
|
|
|
g_assert( bitmap );
|
|
|
|
|
|
|
|
return( bitmap );
|
2019-09-15 18:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_bitmap_destroy( void *bitmap )
|
2019-09-15 18:08:48 +02:00
|
|
|
{
|
2019-09-18 18:34:49 +02:00
|
|
|
g_assert( bitmap );
|
2021-03-28 20:23:06 +02:00
|
|
|
g_free( bitmap );
|
2019-09-15 18:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_bitmap_modified( void *bitmap )
|
2019-09-15 18:08:48 +02:00
|
|
|
{
|
|
|
|
(void) bitmap; /* unused */
|
2019-09-18 18:34:49 +02:00
|
|
|
g_assert( bitmap );
|
|
|
|
|
2019-09-15 18:08:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
static gif_bitmap_callback_vt vips_foreign_load_nsgif_bitmap_callbacks = {
|
|
|
|
vips_foreign_load_nsgif_bitmap_create,
|
|
|
|
vips_foreign_load_nsgif_bitmap_destroy,
|
|
|
|
vips_foreign_load_nsgif_bitmap_get_buffer,
|
|
|
|
vips_foreign_load_nsgif_bitmap_set_opaque,
|
|
|
|
vips_foreign_load_nsgif_bitmap_test_opaque,
|
|
|
|
vips_foreign_load_nsgif_bitmap_modified
|
2019-09-15 18:08:48 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 15:30:25 +02:00
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_init( VipsForeignLoadNsgif *gif )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
2019-09-15 18:08:48 +02:00
|
|
|
gif->anim = g_new0( gif_animation, 1 );
|
2021-02-28 13:20:05 +01:00
|
|
|
gif_create( gif->anim, &vips_foreign_load_nsgif_bitmap_callbacks );
|
2019-08-15 15:30:25 +02:00
|
|
|
gif->n = 1;
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
typedef struct _VipsForeignLoadNsgifFile {
|
|
|
|
VipsForeignLoadNsgif parent_object;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
/* Filename for load.
|
|
|
|
*/
|
|
|
|
char *filename;
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
} VipsForeignLoadNsgifFile;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
typedef VipsForeignLoadNsgifClass VipsForeignLoadNsgifFileClass;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
G_DEFINE_TYPE( VipsForeignLoadNsgifFile, vips_foreign_load_nsgif_file,
|
|
|
|
vips_foreign_load_nsgif_get_type() );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2019-09-15 18:08:48 +02:00
|
|
|
static int
|
2020-07-07 16:50:44 +02:00
|
|
|
vips_foreign_load_gif_file_build( VipsObject *object )
|
2019-09-15 18:08:48 +02:00
|
|
|
{
|
2021-02-28 13:20:05 +01:00
|
|
|
VipsForeignLoadNsgif *gif = (VipsForeignLoadNsgif *) object;
|
|
|
|
VipsForeignLoadNsgifFile *file = (VipsForeignLoadNsgifFile *) object;
|
2019-09-06 13:57:03 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
if( file->filename )
|
|
|
|
if( !(gif->source =
|
|
|
|
vips_source_new_from_file( file->filename )) )
|
2019-09-15 18:08:48 +02:00
|
|
|
return( -1 );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
if( VIPS_OBJECT_CLASS( vips_foreign_load_nsgif_file_parent_class )->
|
2020-07-07 16:50:44 +02:00
|
|
|
build( object ) )
|
|
|
|
return( -1 );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
return( 0 );
|
2019-08-15 15:30:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
static const char *vips_foreign_nsgif_suffs[] = {
|
2020-07-07 16:50:44 +02:00
|
|
|
".gif",
|
|
|
|
NULL
|
|
|
|
};
|
2019-09-15 18:08:48 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
static gboolean
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_file_is_a( const char *filename )
|
2020-07-07 16:50:44 +02:00
|
|
|
{
|
|
|
|
VipsSource *source;
|
|
|
|
gboolean result;
|
2019-09-15 18:08:48 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
if( !(source = vips_source_new_from_file( filename )) )
|
|
|
|
return( FALSE );
|
2021-02-28 13:20:05 +01:00
|
|
|
result = vips_foreign_load_nsgif_is_a_source( source );
|
2020-07-07 16:50:44 +02:00
|
|
|
VIPS_UNREF( source );
|
2019-09-15 18:08:48 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
return( result );
|
2019-09-15 18:08:48 +02:00
|
|
|
}
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_file_class_init(
|
|
|
|
VipsForeignLoadNsgifFileClass *class )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
|
|
|
VipsObjectClass *object_class = (VipsObjectClass *) class;
|
|
|
|
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
|
|
|
|
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
|
|
|
|
|
|
|
|
gobject_class->set_property = vips_object_set_property;
|
|
|
|
gobject_class->get_property = vips_object_get_property;
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
object_class->nickname = "gifload";
|
2019-08-15 15:30:25 +02:00
|
|
|
object_class->description = _( "load GIF with libnsgif" );
|
2020-07-07 16:50:44 +02:00
|
|
|
object_class->build = vips_foreign_load_gif_file_build;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
foreign_class->suffs = vips_foreign_nsgif_suffs;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
load_class->is_a = vips_foreign_load_nsgif_file_is_a;
|
2019-09-15 18:08:48 +02:00
|
|
|
|
2019-08-15 15:30:25 +02:00
|
|
|
VIPS_ARG_STRING( class, "filename", 1,
|
|
|
|
_( "Filename" ),
|
|
|
|
_( "Filename to load from" ),
|
|
|
|
VIPS_ARGUMENT_REQUIRED_INPUT,
|
2021-02-28 13:20:05 +01:00
|
|
|
G_STRUCT_OFFSET( VipsForeignLoadNsgifFile, filename ),
|
2019-08-15 15:30:25 +02:00
|
|
|
NULL );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_file_init( VipsForeignLoadNsgifFile *file )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
typedef struct _VipsForeignLoadNsgifBuffer {
|
|
|
|
VipsForeignLoadNsgif parent_object;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
/* Load from a buffer.
|
|
|
|
*/
|
2020-07-07 16:50:44 +02:00
|
|
|
VipsArea *blob;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
} VipsForeignLoadNsgifBuffer;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
typedef VipsForeignLoadNsgifClass VipsForeignLoadNsgifBufferClass;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
G_DEFINE_TYPE( VipsForeignLoadNsgifBuffer, vips_foreign_load_nsgif_buffer,
|
|
|
|
vips_foreign_load_nsgif_get_type() );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
static int
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_buffer_build( VipsObject *object )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
2021-02-28 13:20:05 +01:00
|
|
|
VipsForeignLoadNsgif *gif = (VipsForeignLoadNsgif *) object;
|
|
|
|
VipsForeignLoadNsgifBuffer *buffer =
|
|
|
|
(VipsForeignLoadNsgifBuffer *) object;
|
2020-07-07 16:50:44 +02:00
|
|
|
|
|
|
|
if( buffer->blob &&
|
|
|
|
!(gif->source = vips_source_new_from_memory(
|
|
|
|
buffer->blob->data,
|
|
|
|
buffer->blob->length )) )
|
|
|
|
return( -1 );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
if( VIPS_OBJECT_CLASS( vips_foreign_load_nsgif_buffer_parent_class )->
|
2020-07-07 16:50:44 +02:00
|
|
|
build( object ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_buffer_is_a_buffer( const void *buf, size_t len )
|
2020-07-07 16:50:44 +02:00
|
|
|
{
|
|
|
|
VipsSource *source;
|
|
|
|
gboolean result;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
if( !(source = vips_source_new_from_memory( buf, len )) )
|
|
|
|
return( FALSE );
|
2021-02-28 13:20:05 +01:00
|
|
|
result = vips_foreign_load_nsgif_is_a_source( source );
|
2020-07-07 16:50:44 +02:00
|
|
|
VIPS_UNREF( source );
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2020-07-07 16:50:44 +02:00
|
|
|
return( result );
|
2019-08-15 15:30:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_buffer_class_init(
|
|
|
|
VipsForeignLoadNsgifBufferClass *class )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
|
|
|
VipsObjectClass *object_class = (VipsObjectClass *) class;
|
|
|
|
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
|
|
|
|
|
|
|
|
gobject_class->set_property = vips_object_set_property;
|
|
|
|
gobject_class->get_property = vips_object_get_property;
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
object_class->nickname = "gifload_buffer";
|
2019-08-15 15:30:25 +02:00
|
|
|
object_class->description = _( "load GIF with libnsgif" );
|
2021-02-28 13:20:05 +01:00
|
|
|
object_class->build = vips_foreign_load_nsgif_buffer_build;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
load_class->is_a_buffer = vips_foreign_load_nsgif_buffer_is_a_buffer;
|
2019-08-15 15:30:25 +02:00
|
|
|
|
|
|
|
VIPS_ARG_BOXED( class, "buffer", 1,
|
|
|
|
_( "Buffer" ),
|
|
|
|
_( "Buffer to load from" ),
|
|
|
|
VIPS_ARGUMENT_REQUIRED_INPUT,
|
2021-02-28 13:20:05 +01:00
|
|
|
G_STRUCT_OFFSET( VipsForeignLoadNsgifBuffer, blob ),
|
2019-08-15 15:30:25 +02:00
|
|
|
VIPS_TYPE_BLOB );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-28 13:20:05 +01:00
|
|
|
vips_foreign_load_nsgif_buffer_init( VipsForeignLoadNsgifBuffer *buffer )
|
2019-08-15 15:30:25 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:20:05 +01:00
|
|
|
typedef struct _VipsForeignLoadNsgifSource {
|
|
|
|
VipsForeignLoadNsgif parent_object;
|
|
|
|
|
|
|
|
/* Load from a source.
|
|
|
|
*/
|
|
|
|
VipsSource *source;
|
|
|
|
|
|
|
|
} VipsForeignLoadNsgifSource;
|
|
|
|
|
|
|
|
typedef VipsForeignLoadClass VipsForeignLoadNsgifSourceClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE( VipsForeignLoadNsgifSource, vips_foreign_load_nsgif_source,
|
|
|
|
vips_foreign_load_nsgif_get_type() );
|
|
|
|
|
|
|
|
static int
|
|
|
|
vips_foreign_load_nsgif_source_build( VipsObject *object )
|
|
|
|
{
|
|
|
|
VipsForeignLoadNsgif *gif = (VipsForeignLoadNsgif *) object;
|
|
|
|
VipsForeignLoadNsgifSource *source =
|
|
|
|
(VipsForeignLoadNsgifSource *) object;
|
|
|
|
|
|
|
|
if( source->source ) {
|
|
|
|
gif->source = source->source;
|
|
|
|
g_object_ref( gif->source );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( VIPS_OBJECT_CLASS( vips_foreign_load_nsgif_source_parent_class )->
|
|
|
|
build( object ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_foreign_load_nsgif_source_class_init(
|
|
|
|
VipsForeignLoadNsgifSourceClass *class )
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
|
|
|
VipsObjectClass *object_class = (VipsObjectClass *) class;
|
|
|
|
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
|
|
|
|
|
|
|
|
gobject_class->set_property = vips_object_set_property;
|
|
|
|
gobject_class->get_property = vips_object_get_property;
|
|
|
|
|
|
|
|
object_class->nickname = "gifload_source";
|
|
|
|
object_class->description = _( "load gif from source" );
|
|
|
|
object_class->build = vips_foreign_load_nsgif_source_build;
|
|
|
|
|
|
|
|
load_class->is_a_source = vips_foreign_load_nsgif_is_a_source;
|
|
|
|
|
|
|
|
VIPS_ARG_OBJECT( class, "source", 1,
|
|
|
|
_( "Source" ),
|
|
|
|
_( "Source to load from" ),
|
|
|
|
VIPS_ARGUMENT_REQUIRED_INPUT,
|
|
|
|
G_STRUCT_OFFSET( VipsForeignLoadNsgifSource, source ),
|
|
|
|
VIPS_TYPE_SOURCE );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_foreign_load_nsgif_source_init( VipsForeignLoadNsgifSource *source )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*HAVE_NSGIF*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_gifload:
|
|
|
|
* @filename: file to load
|
|
|
|
* @out: (out): output image
|
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* Optional arguments:
|
|
|
|
*
|
|
|
|
* * @page: %gint, page (frame) to read
|
|
|
|
* * @n: %gint, load this many pages
|
|
|
|
*
|
|
|
|
* Read a GIF file into a libvips image.
|
|
|
|
*
|
|
|
|
* Use @page to select a page to render, numbering from zero.
|
|
|
|
*
|
|
|
|
* Use @n to select the number of pages to render. The default is 1. Pages are
|
|
|
|
* rendered in a vertical column. Set to -1 to mean "until the end of the
|
|
|
|
* document". Use vips_grid() to change page layout.
|
|
|
|
*
|
|
|
|
* The output image is always RGBA.
|
|
|
|
*
|
|
|
|
* See also: vips_image_new_from_file().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_gifload( const char *filename, VipsImage **out, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start( ap, out );
|
|
|
|
result = vips_call_split( "gifload", ap, filename, out );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_gifload_buffer:
|
|
|
|
* @buf: (array length=len) (element-type guint8): memory area to load
|
|
|
|
* @len: (type gsize): size of memory area
|
|
|
|
* @out: (out): image to write
|
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* Optional arguments:
|
|
|
|
*
|
|
|
|
* * @page: %gint, page (frame) to read
|
|
|
|
* * @n: %gint, load this many pages
|
|
|
|
*
|
|
|
|
* Read a GIF-formatted memory block into a VIPS image. Exactly as
|
|
|
|
* vips_gifload(), but read from a memory buffer.
|
|
|
|
*
|
|
|
|
* You must not free the buffer while @out is active. The
|
|
|
|
* #VipsObject::postclose signal on @out is a good place to free.
|
|
|
|
*
|
|
|
|
* See also: vips_gifload().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_gifload_buffer( void *buf, size_t len, VipsImage **out, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
VipsBlob *blob;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* We don't take a copy of the data or free it.
|
|
|
|
*/
|
|
|
|
blob = vips_blob_new( NULL, buf, len );
|
|
|
|
|
|
|
|
va_start( ap, out );
|
|
|
|
result = vips_call_split( "gifload_buffer", ap, blob, out );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
vips_area_unref( VIPS_AREA( blob ) );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_gifload_source:
|
|
|
|
* @source: source to load
|
|
|
|
* @out: (out): image to write
|
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* Optional arguments:
|
|
|
|
*
|
|
|
|
* * @page: %gint, page (frame) to read
|
|
|
|
* * @n: %gint, load this many pages
|
|
|
|
*
|
|
|
|
* Exactly as vips_gifload(), but read from a source.
|
|
|
|
*
|
|
|
|
* See also: vips_gifload().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_gifload_source( VipsSource *source, VipsImage **out, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start( ap, out );
|
|
|
|
result = vips_call_split( "gifload_source", ap, source, out );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|