Skip to content

Commit 63bee73

Browse files
committed
impl. image-as-is option
1 parent 175b6c2 commit 63bee73

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

Commands.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace RagePhoto.Cli;
55

66
internal static partial class Commands {
77

8-
internal static Int32 CreateFunction(String format, String? imageFile, String? description, String? json, String? title, String? outputFile) {
8+
internal static Int32 CreateFunction(String format, String? imageFile, bool imageAsIs,
9+
String? description, String? json, String? title, String? outputFile) {
910
try {
1011
using Photo photo = new();
1112

@@ -28,7 +29,7 @@ internal static Int32 CreateFunction(String format, String? imageFile, String? d
2829
}
2930
else {
3031
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
31-
photo.Jpeg = Jpeg.GetJpeg(input, out size);
32+
photo.Jpeg = Jpeg.GetJpeg(input, imageAsIs, out size);
3233
}
3334

3435
photo.Json = json == null ?
@@ -147,7 +148,8 @@ internal static Int32 GetFunction(String inputFile, String dataType, String outp
147148
}
148149
}
149150

150-
internal static Int32 SetFunction(String inputFile, String? format, String? imageFile, String? description, String? json, String? title, bool updateJson, String? outputFile) {
151+
internal static Int32 SetFunction(String inputFile, String? format, String? imageFile, bool imageAsIs,
152+
String? description, String? json, String? title, bool updateJson, String? outputFile) {
151153
try {
152154
if (format == null && imageFile == null && description == null
153155
&& json == null && title == null && !updateJson) {
@@ -193,7 +195,7 @@ internal static Int32 SetFunction(String inputFile, String? format, String? imag
193195
}
194196
else if (imageFile != null) {
195197
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
196-
photo.Jpeg = Jpeg.GetJpeg(input, out size);
198+
photo.Jpeg = Jpeg.GetJpeg(input, imageAsIs, out size);
197199
photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid);
198200
}
199201
else if (updateJson) {
@@ -243,6 +245,9 @@ internal static Command CreateCommand {
243245
Option<String?> imageOption = new("--image", "-i", "--jpeg") {
244246
Description = "Image File"
245247
};
248+
Option<bool> imageAsIsOption = new("--image-as-is") {
249+
Description = "Image as-is"
250+
};
246251
Option<String?> descriptionOption = new("--description", "-d") {
247252
Description = "Description"
248253
};
@@ -256,11 +261,18 @@ internal static Command CreateCommand {
256261
Description = "Output File"
257262
};
258263
Command createCommand = new("create", "Create a new Photo") {
259-
formatArgument, imageOption, descriptionOption, jsonOption, titleOption, outputOption
264+
formatArgument,
265+
imageOption,
266+
imageAsIsOption,
267+
descriptionOption,
268+
jsonOption,
269+
titleOption,
270+
outputOption
260271
};
261272
createCommand.SetAction(result => Environment.ExitCode = CreateFunction(
262273
result.GetRequiredValue(formatArgument),
263274
result.GetValue(imageOption),
275+
result.GetValue(imageAsIsOption),
264276
result.GetValue(descriptionOption),
265277
result.GetValue(jsonOption),
266278
result.GetValue(titleOption),
@@ -302,7 +314,9 @@ internal static Command GetCommand {
302314
DefaultValueFactory = _ => "-"
303315
};
304316
Command getCommand = new("get", "Get Data from a Photo") {
305-
inputArgument, typeArgument, outputOption
317+
inputArgument,
318+
typeArgument,
319+
outputOption
306320
};
307321
getCommand.SetAction(result => Environment.ExitCode = GetFunction(
308322
result.GetRequiredValue(inputArgument),
@@ -323,6 +337,9 @@ internal static Command SetCommand {
323337
Option<String?> imageOption = new("--image", "-i", "--jpeg") {
324338
Description = "Image File"
325339
};
340+
Option<bool> imageAsIsOption = new("--image-as-is") {
341+
Description = "Image as-is"
342+
};
326343
Option<String?> descriptionOption = new("--description", "-d") {
327344
Description = "Description"
328345
};
@@ -339,12 +356,21 @@ internal static Command SetCommand {
339356
Description = "Output File"
340357
};
341358
Command setCommand = new("set", "Set Data from a Photo") {
342-
inputArgument, formatOption, imageOption, descriptionOption, jsonOption, titleOption, updateJsonOption, outputOption
359+
inputArgument,
360+
formatOption,
361+
imageOption,
362+
imageAsIsOption,
363+
descriptionOption,
364+
jsonOption,
365+
titleOption,
366+
updateJsonOption,
367+
outputOption
343368
};
344369
setCommand.SetAction(result => Environment.ExitCode = SetFunction(
345370
result.GetRequiredValue(inputArgument),
346371
result.GetValue(formatOption),
347372
result.GetValue(imageOption),
373+
result.GetValue(imageAsIsOption),
348374
result.GetValue(descriptionOption),
349375
result.GetValue(jsonOption),
350376
result.GetValue(titleOption),

Jpeg.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using SixLabors.ImageSharp;
2+
using SixLabors.ImageSharp.Formats;
23
using SixLabors.ImageSharp.Formats.Jpeg;
34
using SixLabors.ImageSharp.PixelFormats;
45
namespace RagePhoto.Cli;
56

67
internal class Jpeg {
78

89
internal static Byte[] GetEmptyJpeg(PhotoFormat format, out Size size) {
9-
size = format == PhotoFormat.GTA5 ? new(960, 536) : new(1920, 1080);
10+
size = format switch {
11+
PhotoFormat.GTA5 => new(960, 536),
12+
PhotoFormat.RDR2 => new(1920, 1080),
13+
_ => throw new ArgumentException("Invalid Format", nameof(format))
14+
};
1015
using Image<Rgb24> image = new(size.Width, size.Height);
1116
image.ProcessPixelRows(static pixelAccessor => {
1217
for (Int32 y = 0; y < pixelAccessor.Height; y++) {
@@ -16,27 +21,43 @@ internal static Byte[] GetEmptyJpeg(PhotoFormat format, out Size size) {
1621
}
1722
}
1823
});
19-
using MemoryStream output = new();
20-
image.SaveAsJpeg(output, new() {
24+
using MemoryStream jpegStream = new();
25+
image.SaveAsJpeg(jpegStream, new() {
2126
Quality = 100,
2227
ColorType = JpegEncodingColor.YCbCrRatio444
2328
});
24-
return output.ToArray();
29+
return jpegStream.ToArray();
2530
}
2631

27-
internal static Byte[] GetJpeg(Stream stream, out Size size) {
28-
using Image image = Image.Load(stream);
29-
size = image.Size;
30-
image.Metadata.ExifProfile = null;
31-
using MemoryStream output = new();
32-
image.SaveAsJpeg(output, new() {
33-
Quality = 100,
34-
ColorType = JpegEncodingColor.YCbCrRatio444
35-
});
36-
return output.ToArray();
32+
internal static Byte[] GetJpeg(Stream input, bool imageAsIs, out Size size) {
33+
if (!imageAsIs) {
34+
using Image image = Image.Load(input);
35+
size = image.Size;
36+
image.Metadata.ExifProfile = null;
37+
using MemoryStream jpegStream = new();
38+
image.SaveAsJpeg(jpegStream, new() {
39+
Quality = 100,
40+
ColorType = JpegEncodingColor.YCbCrRatio444
41+
});
42+
return jpegStream.ToArray();
43+
}
44+
else {
45+
using MemoryStream jpegStream = new();
46+
input.CopyTo(jpegStream);
47+
Byte[] jpeg = jpegStream.ToArray();
48+
size = GetSize(jpeg);
49+
return jpeg;
50+
}
3751
}
3852

3953
internal static Size GetSize(ReadOnlySpan<Byte> jpeg) {
40-
return Image.Identify(jpeg).Size;
54+
try {
55+
return Image.Identify(new DecoderOptions {
56+
Configuration = new(new JpegConfigurationModule())
57+
}, jpeg).Size;
58+
}
59+
catch (UnknownImageFormatException exception) {
60+
throw new Exception("Unsupported Image Format", exception);
61+
}
4162
}
4263
}

0 commit comments

Comments
 (0)