Skip to content

Commit

Permalink
♻️ Avoid reprocessing GIF images that are already the correct size.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexawyz committed Feb 8, 2025
1 parent 53924f1 commit 537b510
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Exo/Service/Exo.Service.Core/ImageStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ bool shouldApplyCircularMask
if ((targetAnimatedFormats & ~(ImageFormats.Gif | ImageFormats.Png | ImageFormats.WebPLossy | ImageFormats.WebPLossless)) != 0) throw new ArgumentOutOfRangeException(nameof(targetAnimatedFormats));
if (sourceRectangle.Left + sourceRectangle.Width > metadata.Width || sourceRectangle.Top + sourceRectangle.Height > metadata.Height) throw new ArgumentException(nameof(sourceRectangle));

// Determine now if the image already has the correct size. We want to avoid reprocessing an image that may already be correct.
bool isCorrectSize = sourceRectangle.Left == 0 &&
sourceRectangle.Top == 0 &&
sourceRectangle.Width == metadata.Width &&
sourceRectangle.Height == metadata.Height &&
targetSize.Width == metadata.Width &&
targetSize.Height == metadata.Height;

// First and foremost, adjust the animation stripping requirement based on the image and the supported formats of the device.
bool shouldStripAnimations = metadata.IsAnimated && targetAnimatedFormats == 0;

Expand Down Expand Up @@ -379,6 +387,14 @@ bool shouldApplyCircularMask
else throw new UnreachableException("The code must explicitly check each possible animated image format.");
}

// If the target format is GIF and the image has a correct size, we disable the circular mask.
// The reason for that being that we can not guarantee that the processing we would apply would produce a better result than the current image,
// even if we reduce the number of colors by applying the mask.
// Until we have more faith in the result of image processing, it is better to trust the original image.
// If the original image is not good, the user still has the option of optimizing it externally.
// If we re-processed the image everytime, we would prevent this.
if (targetFormat == ImageFormat.Gif && isCorrectSize) shouldApplyCircularMask = false;

Span<byte> payload = stackalloc byte[30];

payload[0] = (byte)
Expand All @@ -404,12 +420,7 @@ bool shouldApplyCircularMask
// Shortcut to return the existing physical image if it matches perfectly.
// Later on, should still bind the transformation metadata to the existing image.
if (targetFormat == metadata.Format &&
sourceRectangle.Left == 0 &&
sourceRectangle.Top == 0 &&
sourceRectangle.Width == metadata.Width &&
sourceRectangle.Height == metadata.Height &&
targetSize.Width == metadata.Width &&
targetSize.Height == metadata.Height &&
isCorrectSize &&
!shouldStripAnimations &&
!shouldApplyCircularMask)
{
Expand Down

0 comments on commit 537b510

Please sign in to comment.