-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJpeg.cs
More file actions
68 lines (64 loc) · 2.41 KB
/
Jpeg.cs
File metadata and controls
68 lines (64 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
namespace RagePhoto.Cli;
internal class Jpeg {
internal static Byte[] GetEmptyJpeg(PhotoFormat format, out Size size) {
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++) {
Span<Rgb24> pixelRow = pixelAccessor.GetRowSpan(y);
for (Int32 x = 0; x < pixelRow.Length; x++) {
pixelRow[x] = Color.Black;
}
}
});
using MemoryStream jpegStream = new();
image.SaveAsJpeg(jpegStream, new() {
Quality = 100,
ColorType = JpegEncodingColor.YCbCrRatio444
});
return jpegStream.ToArray();
}
internal static Byte[] GetJpeg(Stream input, Boolean imageAsIs, out Size size) {
try {
if (imageAsIs) {
using MemoryStream jpegStream = new();
input.CopyTo(jpegStream);
Byte[] jpeg = jpegStream.ToArray();
size = GetSize(jpeg);
return jpeg;
}
else {
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();
}
}
catch (UnknownImageFormatException exception) {
throw new Exception("Unsupported Image Format", exception);
}
}
internal static Size GetSize(ReadOnlySpan<Byte> jpeg) {
try {
return Image.Identify(new DecoderOptions {
Configuration = new(new JpegConfigurationModule())
}, jpeg).Size;
}
catch (UnknownImageFormatException exception) {
throw new Exception("Unsupported Image Format", exception);
}
}
}