diff --git a/source/blender/blendthumb/src/thumbnail_provider.mm b/source/blender/blendthumb/src/thumbnail_provider.mm index 6ca562e23e9..985b0e052e1 100644 --- a/source/blender/blendthumb/src/thumbnail_provider.mm +++ b/source/blender/blendthumb/src/thumbnail_provider.mm @@ -152,23 +152,33 @@ static NSImage *generate_nsimage_for_file(const char *src_blend_path, NSError *e NSLog(@"Generating thumbnail for %@", request.fileURL.path); @autoreleasepool { NSError *error = nil; - NSImage *ns_image = generate_nsimage_for_file(request.fileURL.path.fileSystemRepresentation, - error); - if (ns_image == nil) { + NSImage *image = generate_nsimage_for_file(request.fileURL.path.fileSystemRepresentation, + error); + if (image == nil || image.size.width <= 0 || image.size.height <= 0) { handler(nil, error); return; } - handler([QLThumbnailReply replyWithContextSize:request.maximumSize - currentContextDrawingBlock:^BOOL { - [ns_image drawInRect:NSMakeRect(0, - 0, - request.maximumSize.width, - request.maximumSize.height)]; - // Release the ns_image that was strongly captured by the block. - [ns_image release]; - return YES; - }], - nil); + + const CGFloat width_ratio = request.maximumSize.width / image.size.width; + const CGFloat height_ratio = request.maximumSize.height / image.size.height; + const CGFloat scale_factor = MIN(width_ratio, height_ratio); + + const NSSize context_size = NSMakeSize(image.size.width * scale_factor, + image.size.height * scale_factor); + + const NSRect context_rect = NSMakeRect(0, 0, context_size.width, context_size.height); + + QLThumbnailReply *thumbnailReply = [QLThumbnailReply replyWithContextSize:context_size + currentContextDrawingBlock:^BOOL { + [image drawInRect:context_rect]; + /* Release the image that was strongly + * captured by this block. */ + [image release]; + return YES; + }]; + + /* Return the thumbnail reply. */ + handler(thumbnailReply, nil); } NSLog(@"Thumbnail generation succcessfully completed"); }