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

View file

@ -5,7 +5,8 @@ namespace RagePhoto.Cli;
internal static partial class Commands { internal static partial class Commands {
internal static Int32 CreateFunction(String format, String? imageFile, String? description, String? json, String? title, String? outputFile) { internal static Int32 CreateFunction(String format, String? imageFile, bool imageAsIs,
String? description, String? json, String? title, String? outputFile) {
try { try {
using Photo photo = new(); using Photo photo = new();
@ -28,7 +29,7 @@ internal static partial class Commands {
} }
else { else {
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile); using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
photo.Jpeg = Jpeg.GetJpeg(input, out size); photo.Jpeg = Jpeg.GetJpeg(input, imageAsIs, out size);
} }
photo.Json = json == null ? photo.Json = json == null ?
@ -147,7 +148,8 @@ internal static partial class Commands {
} }
} }
internal static Int32 SetFunction(String inputFile, String? format, String? imageFile, String? description, String? json, String? title, bool updateJson, String? outputFile) { internal static Int32 SetFunction(String inputFile, String? format, String? imageFile, bool imageAsIs,
String? description, String? json, String? title, bool updateJson, String? outputFile) {
try { try {
if (format == null && imageFile == null && description == null if (format == null && imageFile == null && description == null
&& json == null && title == null && !updateJson) { && json == null && title == null && !updateJson) {
@ -193,7 +195,7 @@ internal static partial class Commands {
} }
else if (imageFile != null) { else if (imageFile != null) {
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile); using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
photo.Jpeg = Jpeg.GetJpeg(input, out size); photo.Jpeg = Jpeg.GetJpeg(input, imageAsIs, out size);
photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid); photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid);
} }
else if (updateJson) { else if (updateJson) {
@ -243,6 +245,9 @@ internal static partial class Commands {
Option<String?> imageOption = new("--image", "-i", "--jpeg") { Option<String?> imageOption = new("--image", "-i", "--jpeg") {
Description = "Image File" Description = "Image File"
}; };
Option<bool> imageAsIsOption = new("--image-as-is") {
Description = "Image as-is"
};
Option<String?> descriptionOption = new("--description", "-d") { Option<String?> descriptionOption = new("--description", "-d") {
Description = "Description" Description = "Description"
}; };
@ -256,11 +261,18 @@ internal static partial class Commands {
Description = "Output File" Description = "Output File"
}; };
Command createCommand = new("create", "Create a new Photo") { Command createCommand = new("create", "Create a new Photo") {
formatArgument, imageOption, descriptionOption, jsonOption, titleOption, outputOption formatArgument,
imageOption,
imageAsIsOption,
descriptionOption,
jsonOption,
titleOption,
outputOption
}; };
createCommand.SetAction(result => Environment.ExitCode = CreateFunction( createCommand.SetAction(result => Environment.ExitCode = CreateFunction(
result.GetRequiredValue(formatArgument), result.GetRequiredValue(formatArgument),
result.GetValue(imageOption), result.GetValue(imageOption),
result.GetValue(imageAsIsOption),
result.GetValue(descriptionOption), result.GetValue(descriptionOption),
result.GetValue(jsonOption), result.GetValue(jsonOption),
result.GetValue(titleOption), result.GetValue(titleOption),
@ -302,7 +314,9 @@ internal static partial class Commands {
DefaultValueFactory = _ => "-" DefaultValueFactory = _ => "-"
}; };
Command getCommand = new("get", "Get Data from a Photo") { Command getCommand = new("get", "Get Data from a Photo") {
inputArgument, typeArgument, outputOption inputArgument,
typeArgument,
outputOption
}; };
getCommand.SetAction(result => Environment.ExitCode = GetFunction( getCommand.SetAction(result => Environment.ExitCode = GetFunction(
result.GetRequiredValue(inputArgument), result.GetRequiredValue(inputArgument),
@ -323,6 +337,9 @@ internal static partial class Commands {
Option<String?> imageOption = new("--image", "-i", "--jpeg") { Option<String?> imageOption = new("--image", "-i", "--jpeg") {
Description = "Image File" Description = "Image File"
}; };
Option<bool> imageAsIsOption = new("--image-as-is") {
Description = "Image as-is"
};
Option<String?> descriptionOption = new("--description", "-d") { Option<String?> descriptionOption = new("--description", "-d") {
Description = "Description" Description = "Description"
}; };
@ -339,12 +356,21 @@ internal static partial class Commands {
Description = "Output File" Description = "Output File"
}; };
Command setCommand = new("set", "Set Data from a Photo") { Command setCommand = new("set", "Set Data from a Photo") {
inputArgument, formatOption, imageOption, descriptionOption, jsonOption, titleOption, updateJsonOption, outputOption inputArgument,
formatOption,
imageOption,
imageAsIsOption,
descriptionOption,
jsonOption,
titleOption,
updateJsonOption,
outputOption
}; };
setCommand.SetAction(result => Environment.ExitCode = SetFunction( setCommand.SetAction(result => Environment.ExitCode = SetFunction(
result.GetRequiredValue(inputArgument), result.GetRequiredValue(inputArgument),
result.GetValue(formatOption), result.GetValue(formatOption),
result.GetValue(imageOption), result.GetValue(imageOption),
result.GetValue(imageAsIsOption),
result.GetValue(descriptionOption), result.GetValue(descriptionOption),
result.GetValue(jsonOption), result.GetValue(jsonOption),
result.GetValue(titleOption), result.GetValue(titleOption),

41
Jpeg.cs
View file

@ -1,4 +1,5 @@
using SixLabors.ImageSharp; using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
namespace RagePhoto.Cli; namespace RagePhoto.Cli;
@ -6,7 +7,11 @@ namespace RagePhoto.Cli;
internal class Jpeg { internal class Jpeg {
internal static Byte[] GetEmptyJpeg(PhotoFormat format, out Size size) { 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); using Image<Rgb24> image = new(size.Width, size.Height);
image.ProcessPixelRows(static pixelAccessor => { image.ProcessPixelRows(static pixelAccessor => {
for (Int32 y = 0; y < pixelAccessor.Height; y++) { for (Int32 y = 0; y < pixelAccessor.Height; y++) {
@ -16,27 +21,43 @@ internal class Jpeg {
} }
} }
}); });
using MemoryStream output = new(); using MemoryStream jpegStream = new();
image.SaveAsJpeg(output, new() { image.SaveAsJpeg(jpegStream, new() {
Quality = 100, Quality = 100,
ColorType = JpegEncodingColor.YCbCrRatio444 ColorType = JpegEncodingColor.YCbCrRatio444
}); });
return output.ToArray(); return jpegStream.ToArray();
} }
internal static Byte[] GetJpeg(Stream stream, out Size size) { internal static Byte[] GetJpeg(Stream input, bool imageAsIs, out Size size) {
using Image image = Image.Load(stream); if (!imageAsIs) {
using Image image = Image.Load(input);
size = image.Size; size = image.Size;
image.Metadata.ExifProfile = null; image.Metadata.ExifProfile = null;
using MemoryStream output = new(); using MemoryStream jpegStream = new();
image.SaveAsJpeg(output, new() { image.SaveAsJpeg(jpegStream, new() {
Quality = 100, Quality = 100,
ColorType = JpegEncodingColor.YCbCrRatio444 ColorType = JpegEncodingColor.YCbCrRatio444
}); });
return output.ToArray(); 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) { 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);
}
} }
} }