Skip to content

Commit

Permalink
Better image loading and saving support for non Windows OS.
Browse files Browse the repository at this point in the history
More examples.
Remove images used to test.
  • Loading branch information
gitmylo committed Jun 8, 2023
1 parent be43c54 commit 9e07516
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 32 deletions.
70 changes: 45 additions & 25 deletions TavernAICardLib/TavernAiCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ namespace TavernAICardLib;
public class TavernAiCard
{
public Image? Image;

public string? ImagePath;

public static bool ImageFullySupported()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
}

// Card Fields:
[JsonInclude] [JsonPropertyName("alternate_greetings")] public List<string>? AlternateGreetings { get; set; }
Expand All @@ -31,8 +36,9 @@ public class TavernAiCard
[JsonInclude] [JsonPropertyName("system_prompt")] public string? SystemPrompt { get; set; }
[JsonInclude] [JsonPropertyName("tags")] public List<string>? Tags { get; set; }

public TavernAiCard(Image? image)
public TavernAiCard(string? imagePath, Image? image)
{
this.ImagePath = imagePath;
this.Image = image;
}

Expand All @@ -51,25 +57,22 @@ public TavernAiCard(Image? image)
{JsonFileTypes, new JsonCardSaver()}
};

public static TavernAiCard? Load(string filePath)
public static TavernAiCard Load(string filePath)
{
foreach (var key in _cardLoaders.Keys)
foreach (var end in key)
if (filePath.ToLower().EndsWith(end))
return _cardLoaders[key].Load(filePath);
foreach (var key in _cardLoaders.Keys) foreach (var end in key) if (filePath.ToLower().EndsWith(end)) return _cardLoaders[key].Load(filePath);

return null;
throw new UnsupportedFileFormatException(filePath);
}

public void Save(string filePath)
{
foreach (var key in _cardSavers.Keys)
foreach (var end in key)
if (filePath.ToLower().EndsWith(end))
{
_cardSavers[key].Save(filePath, this);
return;
}
foreach (var key in _cardSavers.Keys) foreach (var end in key) if (filePath.ToLower().EndsWith(end))
{
_cardSavers[key].Save(filePath, this);
return;
}

throw new UnsupportedFileFormatException(filePath);
}
}

Expand All @@ -80,7 +83,7 @@ public class Extensions

public abstract class CardLoader
{
public abstract TavernAiCard? Load(string filePath);
public abstract TavernAiCard Load(string filePath);
}

public abstract class CardSaver
Expand All @@ -99,8 +102,11 @@ public override TavernAiCard Load(string filePath)
reader.End();

Image? image = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
string? imagePath = null;
if (TavernAiCard.ImageFullySupported())
image = new Bitmap(filePath);
else
imagePath = filePath;

string jsonData = Encoding.ASCII.GetString(Convert.FromBase64String(data));
TavernAiCard? card = JsonSerializer.Deserialize<TavernAiCard>(jsonData, new JsonSerializerOptions()
Expand All @@ -110,6 +116,7 @@ public override TavernAiCard Load(string filePath)
if (card != null)
{
card.Image = image;
card.ImagePath = imagePath;
return card;
}

Expand All @@ -119,7 +126,7 @@ public override TavernAiCard Load(string filePath)

public class JsonCardLoader : CardLoader
{
public override TavernAiCard? Load(string filePath)
public override TavernAiCard Load(string filePath)
{
string jsonData = File.ReadAllText(filePath);
TavernAiCard? card = JsonSerializer.Deserialize<TavernAiCard>(jsonData, new JsonSerializerOptions()
Expand Down Expand Up @@ -156,10 +163,13 @@ public class ImageCardSaver : CardSaver
public override void Save(string filePath, TavernAiCard card)
{
if (card.Image == null) throw new NoImageException(filePath);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new OsFileSaveException(filePath);
card.Image.Save(filePath);

if (card.Image != null)
card.Image.Save(filePath);
else if (card.ImagePath == null)
throw new NoImageSaveException(filePath);
else if (card.ImagePath != filePath)
File.Copy(card.ImagePath, filePath);

// https://stackoverflow.com/a/32175522/
String tmpFile = "tmp.png";
PngReader reader = FileHelper.CreatePngReader(filePath);
Expand Down Expand Up @@ -213,11 +223,21 @@ public NoMetaDataException(string fileName)
public override string Message { get; }
}

public class OsFileSaveException : Exception
public class NoImageSaveException : Exception
{
public NoImageSaveException(string fileName)
{
Message = $"Failed to save {fileName}, No image or imagepath.";
}

public override string Message { get; }
}

public class UnsupportedFileFormatException : Exception
{
public OsFileSaveException(string fileName)
public UnsupportedFileFormatException(string fileName)
{
Message = $"Failed to save {fileName}, Saving to image formats is only supported on Windows.";
Message = $"Unsupported file format used for {fileName}. Not a supported image or JSON format.";
}

public override string Message { get; }
Expand Down
Binary file modified TavernAICardLib/bin/Debug/net6.0/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLib/bin/Debug/net6.0/TavernAICardLib.pdb
Binary file not shown.
Binary file modified TavernAICardLib/bin/Debug/net6.0/ref/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLib/obj/Debug/net6.0/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLib/obj/Debug/net6.0/TavernAICardLib.pdb
Binary file not shown.
Binary file modified TavernAICardLib/obj/Debug/net6.0/ref/TavernAICardLib.dll
Binary file not shown.
30 changes: 23 additions & 7 deletions TavernAICardLibTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
using TavernAICardLib;
using System.Drawing;
using TavernAICardLib;

TavernAiCard? card = TavernAiCard.Load("main_Walter White_tavern.png");
// Load the character, this can be .json, .png, .webp, .jpeg or .jpg. If the file format is not supported an exception will be thrown.
TavernAiCard card = TavernAiCard.Load("character.json");

if (card != null)
// Example: Load the character from a png card.
// TavernAiCard? card = TavernAiCard.Load("character.png");

// Example: Print the name of the loaded character.
Console.WriteLine(card.Name);

// Example: Set a new name
card.Name = "New name";

// Example: Saving, you can save in .json, .png, .webp, .jpeg or .jpg. The saving format used depends on the file extension.
card.Save("character_saved.json");

// Example: Creating a new image for the character, supported on Windows only.
// Check if full image support is available to create a custom bitmap (only on Windows, on other OS's only image paths are supported.)
if (TavernAiCard.ImageFullySupported())
{
Console.WriteLine(card.Name);
card.Name = "Waltuh White";
card.Save("Waltuh White.json");
card.Save("Waltuh White.png");
// Create a transparent bitmap
card.Image = new Bitmap(400, 600);
// Save the card as an character card
card.Save("character_saved.png");
}
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLib.pdb
Binary file not shown.
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLibTest.dll
Binary file not shown.
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLibTest.pdb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified TavernAICardLibTest/obj/Debug/net6.0/TavernAICardLibTest.dll
Binary file not shown.
Binary file modified TavernAICardLibTest/obj/Debug/net6.0/TavernAICardLibTest.pdb
Binary file not shown.

0 comments on commit 9e07516

Please sign in to comment.