From 0f234f13e6df579995da95e17d92f4e68cdd5f1a Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sun, 12 Nov 2023 22:12:49 +0100 Subject: [PATCH] Optimize --- WebDriverAgentLib/Utilities/FBImageProcessor.m | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/WebDriverAgentLib/Utilities/FBImageProcessor.m b/WebDriverAgentLib/Utilities/FBImageProcessor.m index b52bfbb6f..e9e0ec333 100644 --- a/WebDriverAgentLib/Utilities/FBImageProcessor.m +++ b/WebDriverAgentLib/Utilities/FBImageProcessor.m @@ -108,7 +108,7 @@ - (nullable NSData*)processedJpegImageWithData:(NSData *)imageData : size; CGImageRef resultImage = NULL; - if (orientation != kCGImagePropertyOrientationUp || usesScaling) { + if (orientation != kCGImagePropertyOrientationUp) { resultImage = CGImageSourceCreateImageAtIndex(imageDataRef, 0, NULL); CGImageRef originalImage = resultImage; size_t bitsPerComponent = CGImageGetBitsPerComponent(originalImage); @@ -138,9 +138,21 @@ - (nullable NSData*)processedJpegImageWithData:(NSData *)imageData resultImage = CGBitmapContextCreateImage(ctx); CGContextRelease(ctx); CGImageRelease(originalImage); + } else if (usesScaling) { + CGFloat scaledMaxPixelSize = MAX(size.width, size.height) * scalingFactor; + CFDictionaryRef params = (__bridge CFDictionaryRef)@{ + (const NSString *)kCGImageSourceCreateThumbnailWithTransform: @(YES), + (const NSString *)kCGImageSourceCreateThumbnailFromImageIfAbsent: @(YES), + (const NSString *)kCGImageSourceThumbnailMaxPixelSize: @(scaledMaxPixelSize) + }; + resultImage = CGImageSourceCreateThumbnailAtIndex(imageDataRef, 0, params); } CFRelease(imageDataRef); if (NULL == resultImage) { + if (orientation != kCGImagePropertyOrientationUp || usesScaling) { + // This is suboptimal, but better to have something than nothing at all + // NSLog(@"The image cannot be preprocessed. Passing it as is"); + } // No scaling and/or orientation fixing was neecessary return imageData; }