From: Alan Third Date: Wed, 23 Aug 2017 18:53:23 +0000 (+0100) Subject: Fix PNGs on macOS (bug#28176) X-Git-Tag: emacs-26.0.90~373 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=95130f192b7c00a118ce745efb94cd3d0aaabab0;p=emacs.git 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. --- 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