Files
sbox-public/engine/Sandbox.Engine/Resources/Textures/Bitmap/Bitmap.Saving.cs
Carson Kompon 8c9270c625 Fix Texture color space and add FormatOverride to TextureGenerator (#4993)
* Only linearize during EncodeTo, not on load/FloatBitmap creation (so we don't end up linearizing twice in some instances)

* Lossless texturegenerators are stored as png blob so they are still compressed without any artifacting

* Only RGBA8888 uses ToPng, and update comments to make more sense

* Revert any SRGB-related stuff

* use formatoverride instead of lossless, matches behaviour or Texture.Save

* Something interesting I noticed from Carson's changes, VTEX_FORMAT_PNG_RGBA8888 stores the RGBA8 texture on disk with png compression, let's experiment with that for both TextureCompiler and Texture.Save

* Remove mipCount disablement for formatOverride in TextureResourceCompiler

---------

Co-authored-by: Sam Pavlovic <sam@sampavlovic.com>
2026-06-05 11:25:19 -04:00

72 lines
2.1 KiB
C#

using Sandbox.Utility;
using SkiaSharp;
namespace Sandbox;
public partial class Bitmap
{
/// <summary>
/// Exports the bitmap as a JPEG byte array with the specified quality.
/// </summary>
/// <param name="quality">The quality of the JPEG, between 0 and 100.</param>
/// <returns>A byte array containing the JPEG image data.</returns>
public byte[] ToJpg( int quality = 100 )
{
return Encode( SKEncodedImageFormat.Jpeg, quality );
}
/// <summary>
/// Exports the bitmap as a PNG byte array.
/// </summary>
/// <returns>A byte array containing the PNG image data.</returns>
public byte[] ToPng()
{
return Encode( SKEncodedImageFormat.Png, 100 );
}
/// <summary>
/// Exports the bitmap as a BMP byte array.
/// </summary>
/// <returns>A byte array containing the BMP image data.</returns>
public byte[] ToBmp()
{
return Encode( SKEncodedImageFormat.Bmp, 100 );
}
/// <summary>
/// Exports the bitmap as an HDR WebP byte array with the specified quality.
/// </summary>
/// <param name="quality">The quality of the WebP image, between 0 and 100.</param>
/// <returns>A byte array containing the WebP HDR image data.</returns>
public byte[] ToWebP( int quality = 100 )
{
return Encode( SKEncodedImageFormat.Webp, quality );
}
/// <summary>
/// Exports the bitmap to the specified image format with optional quality.
/// </summary>
/// <param name="format">The image format (e.g., PNG, JPEG, BMP).</param>
/// <param name="quality">The quality of the image, used for formats like JPEG.</param>
/// <returns>A byte array containing the image data.</returns>
private byte[] Encode( SKEncodedImageFormat format, int quality )
{
using var image = SKImage.FromBitmap( _bitmap );
using var data = image.Encode( format, quality );
return data.ToArray();
}
/// <summary>
/// Exports the bitmap to the specified engine format
/// </summary>
/// <param name="format">The target image format to encode to.</param>
public byte[] ToFormat( ImageFormat format )
{
var data = _bitmap.GetPixels();
using var fbm = new FloatBitmap( Width, Height, ImageFormat, data, ByteCount, srgb: false );
return fbm.EncodeTo( format );
}
}