Skip to content

fix: Set default quality flags when saving in JPEG format#3034

Merged
Eideren merged 2 commits intostride3d:masterfrom
Ethereal77:fix/image-save-jpeg
Jan 25, 2026
Merged

fix: Set default quality flags when saving in JPEG format#3034
Eideren merged 2 commits intostride3d:masterfrom
Ethereal77:fix/image-save-jpeg

Conversation

@Ethereal77
Copy link
Contributor

PR Details

Context: After the recent migration from System.Drawing.Common (using GDI+, Windows-specific) to FreeImage (cross-platform), the TestLoadAndSave of TestImage (in Stride.Graphics.Tests) started failing for me. However, it only fails when the source (reference image to compare against) and the destination formats are both JPEG.

The failure seems to be caused by differences in the way FreeImage encodes JPEGs, the quality, chroma subsampling, and possibly other things. This makes the resulting JPEG be more compressed and with worse quality, and the test image comparison fails because the differences are greater than the allowed difference of 30.

Solution: This PR sets quality and chroma subsampling flags so FreeImage can save an image more in line with the default configuration GDI+ was producing before the migration, making the test pass again. The key part is in SaveJpegFromMemory(), in StandardImageHelper.Desktop.cs.

As an extra, I've reorganized StandardImageHelper to be more readable and to be able to set custom format-specific flags easier in the future. Currently the files are being saved using FreeImage defaults, and custom flags are hardcoded. I've also improved the documentation and comments.

Related Issue

No issue. Discussed briefly on Discord.

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out.

* Save JPEGs with quality 90 and chroma subsampling 4:2:0 by default.
* Reorganize the class to better allow format-specific tweaks.
@Eideren
Copy link
Collaborator

Eideren commented Jan 24, 2026

Currently the files are being saved using FreeImage defaults, and custom flags are hardcoded.

Fine with me, changing this would be outside the scope of what this PR is trying to solve.
If there are no objections, I'll merge this one in tomorrow.

@Ethereal77
Copy link
Contributor Author

Yeah, I know. I only say that because I've noticed other formats come out also different (different filesize than the original, for example). For those other formats it is not as important, because they are lossless. It's only JPEG that is lossy.

Anyway, for a future PR, yes, I don't know if it merits complicating the logic to allow the user to set the quality or other parameters. What do other engines when saving images?

@Eideren Eideren merged commit 966fdb7 into stride3d:master Jan 25, 2026
7 checks passed
@Eideren
Copy link
Collaborator

Eideren commented Jan 25, 2026

Fairly shallow in terms of feature set, see https://docs.unity3d.com/6000.0/Documentation/ScriptReference/ImageConversion.EncodeToJPG.html
Haven't found UE's API for it, probably expects users to use a third party library.

@Eideren Eideren changed the title Set default quality flags when saving in JPEG format fix: Set default quality flags when saving in JPEG format Jan 25, 2026
@Ethereal77
Copy link
Contributor Author

I find interesting that Unity has specific methods only for specific formats they want to support (JPG, PNG, TGA, and EXR), allowing them to do specific configs and checks, and each of them is pretty well documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants