impl. image-as-is option

This commit is contained in:
Syping 2025-11-23 05:45:55 +01:00
parent 175b6c2c5c
commit 63bee738b2
2 changed files with 69 additions and 22 deletions

51
Jpeg.cs
View file

@ -1,4 +1,5 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
namespace RagePhoto.Cli;
@ -6,7 +7,11 @@ namespace RagePhoto.Cli;
internal class Jpeg {
internal static Byte[] GetEmptyJpeg(PhotoFormat format, out Size size) {
size = format == PhotoFormat.GTA5 ? new(960, 536) : new(1920, 1080);
size = format switch {
PhotoFormat.GTA5 => new(960, 536),
PhotoFormat.RDR2 => new(1920, 1080),
_ => throw new ArgumentException("Invalid Format", nameof(format))
};
using Image<Rgb24> image = new(size.Width, size.Height);
image.ProcessPixelRows(static pixelAccessor => {
for (Int32 y = 0; y < pixelAccessor.Height; y++) {
@ -16,27 +21,43 @@ internal class Jpeg {
}
}
});
using MemoryStream output = new();
image.SaveAsJpeg(output, new() {
using MemoryStream jpegStream = new();
image.SaveAsJpeg(jpegStream, new() {
Quality = 100,
ColorType = JpegEncodingColor.YCbCrRatio444
});
return output.ToArray();
return jpegStream.ToArray();
}
internal static Byte[] GetJpeg(Stream stream, out Size size) {
using Image image = Image.Load(stream);
size = image.Size;
image.Metadata.ExifProfile = null;
using MemoryStream output = new();
image.SaveAsJpeg(output, new() {
Quality = 100,
ColorType = JpegEncodingColor.YCbCrRatio444
});
return output.ToArray();
internal static Byte[] GetJpeg(Stream input, bool imageAsIs, out Size size) {
if (!imageAsIs) {
using Image image = Image.Load(input);
size = image.Size;
image.Metadata.ExifProfile = null;
using MemoryStream jpegStream = new();
image.SaveAsJpeg(jpegStream, new() {
Quality = 100,
ColorType = JpegEncodingColor.YCbCrRatio444
});
return jpegStream.ToArray();
}
else {
using MemoryStream jpegStream = new();
input.CopyTo(jpegStream);
Byte[] jpeg = jpegStream.ToArray();
size = GetSize(jpeg);
return jpeg;
}
}
internal static Size GetSize(ReadOnlySpan<Byte> jpeg) {
return Image.Identify(jpeg).Size;
try {
return Image.Identify(new DecoderOptions {
Configuration = new(new JpegConfigurationModule())
}, jpeg).Size;
}
catch (UnknownImageFormatException exception) {
throw new Exception("Unsupported Image Format", exception);
}
}
}