From 95130f192b7c00a118ce745efb94cd3d0aaabab0 Mon Sep 17 00:00:00 2001 From: Alan Third Date: Wed, 23 Aug 2017 19:53:23 +0100 Subject: [PATCH] Fix PNGs on macOS (bug#28176) * src/nsimage.m (ns_load_image): Remove index check. (EmacsImage::getAnimatedBitmapImageRep): New function. (EmacsImage::getMetadata): Use getAnimatedBitmapImageRep. (EmacsImage::setFrame): Use getAnimatedBitmapImageRep and check index is valid. --- src/nsimage.m | 66 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/nsimage.m b/src/nsimage.m index 94b24a3912c..3c81dea67a9 100644 --- a/src/nsimage.m +++ b/src/nsimage.m @@ -106,7 +106,7 @@ ns_load_image (struct frame *f, struct image *img, return 0; } - if (index < 0 || ![eImg setFrame: index]) + if (![eImg setFrame: index]) { add_to_log ("Unable to set index %d for image %s", index, img->spec); return 0; @@ -450,49 +450,63 @@ ns_set_alpha (void *img, int x, int y, unsigned char a) return stippleMask; } -/* If the image has multiple frames, get a count of them and the - animation delay, if available. */ -- (Lisp_Object)getMetadata +/* Find the first NSBitmapImageRep which has multiple frames. */ +- (NSBitmapImageRep *)getAnimatedBitmapImageRep { - Lisp_Object metadata = Qnil; - for (NSImageRep * r in [self representations]) { if ([r isKindOfClass:[NSBitmapImageRep class]]) { NSBitmapImageRep * bm = (NSBitmapImageRep *)r; - int frames = [[bm valueForProperty: NSImageFrameCount] intValue]; - float delay = [[bm valueForProperty: NSImageCurrentFrameDuration] - floatValue]; - - if (frames > 1) - metadata = Fcons (Qcount, Fcons (make_number (frames), metadata)); - if (delay > 0) - metadata = Fcons (Qdelay, Fcons (make_float (delay), metadata)); - break; + if ([[bm valueForProperty:NSImageFrameCount] intValue] > 0) + return bm; } } + return nil; +} + +/* If the image has multiple frames, get a count of them and the + animation delay, if available. */ +- (Lisp_Object)getMetadata +{ + Lisp_Object metadata = Qnil; + + NSBitmapImageRep * bm = [self getAnimatedBitmapImageRep]; + + if (bm != nil) + { + int frames = [[bm valueForProperty:NSImageFrameCount] intValue]; + float delay = [[bm valueForProperty:NSImageCurrentFrameDuration] + floatValue]; + + if (frames > 1) + metadata = Fcons (Qcount, Fcons (make_number (frames), metadata)); + if (delay > 0) + metadata = Fcons (Qdelay, Fcons (make_float (delay), metadata)); + } return metadata; } /* Attempt to set the animation frame to be displayed. */ - (BOOL)setFrame: (unsigned int) index { - for (NSImageRep * r in [self representations]) + NSBitmapImageRep * bm = [self getAnimatedBitmapImageRep]; + + if (bm != nil) { - if ([r isKindOfClass:[NSBitmapImageRep class]]) - { - NSBitmapImageRep * bm = (NSBitmapImageRep *)r; - if ([[bm valueForProperty: NSImageFrameCount] intValue] <= index) - continue; + int frames = [[bm valueForProperty:NSImageFrameCount] intValue]; - [bm setProperty: NSImageCurrentFrame - withValue: [NSNumber numberWithUnsignedInt: index]]; - return YES; - } + /* If index is invalid, give up. */ + if (index < 0 || index > frames) + return NO; + + [bm setProperty: NSImageCurrentFrame + withValue: [NSNumber numberWithUnsignedInt:index]]; } - return NO; + /* Setting the frame has succeeded, or the image doesn't have + multiple frames. */ + return YES; } @end -- 2.39.2