use a single loop to get frame and alpha metadata

we had two before, plus use the do/while form recommended for libwebp
frame iteration
This commit is contained in:
John Cupitt 2019-07-06 14:46:29 +01:00
parent 515bf68f46
commit e7b6709f99

View File

@ -22,6 +22,8 @@
* lets us do faster and more accurate thumbnailing * lets us do faster and more accurate thumbnailing
* 27/6/19 * 27/6/19
* - disable alpha output if all frame fill the canvas and are solid * - disable alpha output if all frame fill the canvas and are solid
* 6/7/19 [deftomat]
* - support array of delays
*/ */
/* /*
@ -475,40 +477,38 @@ read_header( Read *read, VipsImage *out )
vips_image_set_int( out, vips_image_set_int( out,
VIPS_META_PAGE_HEIGHT, read->frame_height ); VIPS_META_PAGE_HEIGHT, read->frame_height );
if ( read->frame_count > 1) { if( WebPDemuxGetFrame( read->demux, 1, &iter ) ) {
int i; int i;
read->delays = (int *) g_malloc0( read->frame_count * sizeof(int) );
for( i = 0; i < read->frame_count; i++ ) { read->delays = (int *)
if( WebPDemuxGetFrame( read->demux, i + 1, &iter ) ) g_malloc0( read->frame_count * sizeof( int ) );
read->delays[i] = iter.duration; for( i = 0; i < read->frame_count; i++ )
} read->delays[i] = 40;
#ifdef DEBUG do {
for( i = 0; i < read->frame_count; i++ ) { g_assert( iter.frame_num >= 1 &&
printf( "webp2vips: frame = %d; duration = %d\n", i + 1, read->delays[i] ); iter.frame_num < read->frame_count );
}
#endif /*DEBUG*/
vips_image_set_array_int( out, "delay", read->delays, read->frame_count ); read->delays[iter.frame_num - 1] =
iter.duration;
/* We need the alpha in an animation if:
* - any frame has transparent pixels
* - any frame doesn't fill the whole canvas.
*/
if( iter.has_alpha ||
iter.width != read->canvas_width ||
iter.height != read->canvas_height )
read->alpha = TRUE;
} while( WebPDemuxNextFrame( &iter ) );
vips_image_set_array_int( out,
"delay", read->delays, read->frame_count );
/* webp uses ms for delays, gif uses centiseconds. /* webp uses ms for delays, gif uses centiseconds.
*/ */
vips_image_set_int( out, "gif-delay", vips_image_set_int( out, "gif-delay",
VIPS_RINT( read->delays[0] / 10.0 ) ); VIPS_RINT( read->delays[0] / 10.0 ) );
/* We need the alpha in an animation if:
* - any frame has transparent pixels
* - any frame doesn't fill the whole canvas.
*/
do {
if( iter.has_alpha ||
iter.width != read->canvas_width ||
iter.height != read->canvas_height ) {
read->alpha = TRUE;
break;
}
} while( WebPDemuxNextFrame( &iter ) );
} }
WebPDemuxReleaseIterator( &iter ); WebPDemuxReleaseIterator( &iter );