Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
LFS: Add option 'auto' format; new options Resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daky authored and knah committed Feb 22, 2022
1 parent b604c67 commit f9a6582
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions LagFreeScreenshots/LagFreeScreenshotsMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ internal partial class LagFreeScreenshotsMod : MelonMod
{
private const string SettingsCategory = "LagFreeScreenshots";
private const string SettingEnableMod = "Enabled";
private const string SettingScreenshotResolution = "ScreenshotResolution";
private const string SettingScreenshotFormat = "ScreenshotFormat";
private const string SettingJpegPercent = "JpegPercent";
private const string SettingAutorotation = "Auto-rotation";
private const string SettingMetadata = "Metadata";

private static MelonPreferences_Entry<bool> ourEnabled;
private static MelonPreferences_Entry<string> ourResolution;
private static MelonPreferences_Entry<string> ourFormat;
private static MelonPreferences_Entry<int> ourJpegPercent;
private static MelonPreferences_Entry<bool> ourAutorotation;
Expand All @@ -56,7 +58,8 @@ public override void OnApplicationStart()
{
var category = MelonPreferences.CreateCategory(SettingsCategory, "Lag Free Screenshots");
ourEnabled = category.CreateEntry(SettingEnableMod, true, "Enabled");
ourFormat = category.CreateEntry( SettingScreenshotFormat, "png", "Screenshot format");
ourResolution = category.CreateEntry( SettingScreenshotResolution, "default", "Screenshot resolution");
ourFormat = category.CreateEntry( SettingScreenshotFormat, "auto", "Screenshot format");
ourJpegPercent = category.CreateEntry(SettingJpegPercent, 95, "JPEG quality (0-100)");
ourAutorotation = category.CreateEntry(SettingAutorotation, true, "Rotate picture to match camera");
ourMetadata = category.CreateEntry(SettingMetadata, false, "Save metadata in picture");
Expand All @@ -66,19 +69,22 @@ public override void OnApplicationStart()
{
MelonLogger.Error("UI Expansion Kit is not found. Lag Free Screenshots will not work.");
return;
}
}

HarmonyInstance.Patch(
typeof(CameraTakePhotoEnumerator).GetMethod("MoveNext"),
new HarmonyMethod(AccessTools.Method(typeof(LagFreeScreenshotsMod), nameof(MoveNextPatchAsyncReadback))));

AddEnumSettings();
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void AddEnumSettings()
{
ExpansionKitApi.RegisterSettingAsStringEnum(SettingsCategory, SettingScreenshotFormat, new []{("png", "PNG"), ("jpeg", "JPEG")});
ExpansionKitApi.RegisterSettingAsStringEnum(SettingsCategory, SettingScreenshotFormat,
new []{("png", "PNG"), ("jpeg", "JPEG"), ("auto", "Auto")});
ExpansionKitApi.RegisterSettingAsStringEnum(SettingsCategory, SettingScreenshotResolution,
new []{("default", "Default"), ("100x100", "Thumbnail 100x100"), ("1024x1024", "Square 1024x1024"), ("720p", "720p"), ("1080p","1080p"), ("4K","4K"), ("8K", "8K")});
}

private static ScreenshotRotation GetPictureAutorotation(Camera camera)
Expand Down Expand Up @@ -141,6 +147,10 @@ public static bool MoveNextPatchAsyncReadback(ref bool __result, CameraTakePhoto

ourMainThread = Thread.CurrentThread;

var resFromOption = ImageResolution(ourResolution.Value);
if (resFromOption.HasValue)
(resX, resY) = resFromOption.Value;

__result = false;
TakeScreenshot(__instance.field_Public_Camera_0, resX,
resY, hasAlpha).ContinueWith(t =>
Expand All @@ -151,6 +161,20 @@ public static bool MoveNextPatchAsyncReadback(ref bool __result, CameraTakePhoto
return false;
}

public static (int width, int height)? ImageResolution(String d)
{
return d switch
{
"100x100" => (100, 100),
"1024x1024" => (1024, 1024),
"720p" => (1280, 720),
"1080p" => (1920, 1080),
"4K" => (3840, 2160),
"8K" => (7680, 4320),
_ => null,
};
}

private static int ourLastUsedMsaaLevel = 0;
private static int MaxMsaaCount(int w, int h)
{
Expand Down Expand Up @@ -394,6 +418,7 @@ private static async Task EncodeAndSavePicture(string filePath, (IntPtr, int Len


var pixelFormat = hasAlpha ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb;
var format = ourFormat.Value == "auto" ? (hasAlpha ? "png" : "jpeg") : ourFormat.Value;
using var bitmap = new Bitmap(w, h, pixelFormat);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, pixelFormat);
unsafe
Expand All @@ -410,7 +435,7 @@ private static async Task EncodeAndSavePicture(string filePath, (IntPtr, int Len
if (description != null)
{
// png description is saved as iTXt chunk manually
if (ourFormat.Value == "jpeg")
if (format == "jpeg")
{
var stringBytesCount = Encoding.Unicode.GetByteCount(description);
var allBytes = new byte[8 + stringBytesCount];
Expand All @@ -426,7 +451,7 @@ private static async Task EncodeAndSavePicture(string filePath, (IntPtr, int Len
}
}

if (ourFormat.Value == "jpeg")
if (format == "jpeg")
{
var encoder = GetEncoder(ImageFormat.Jpeg);
using var parameters = new EncoderParameters(1)
Expand Down Expand Up @@ -480,4 +505,4 @@ static string GetPath(int w, int h)
return ourOurGetPathMethod(w, h);
}
}
}
}

0 comments on commit f9a6582

Please sign in to comment.