You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.9 KiB
68 lines
1.9 KiB
1 year ago
|
using System.Text.Json;
|
||
1 year ago
|
using MetadataExtractor;
|
||
|
using StabilityMatrix.Core.Extensions;
|
||
1 year ago
|
using StabilityMatrix.Core.Models;
|
||
1 year ago
|
using StabilityMatrix.Core.Models.FileInterfaces;
|
||
1 year ago
|
using Directory = MetadataExtractor.Directory;
|
||
1 year ago
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Helper;
|
||
1 year ago
|
|
||
1 year ago
|
public class ImageMetadata
|
||
1 year ago
|
{
|
||
|
private IReadOnlyList<Directory>? Directories { get; set; }
|
||
|
|
||
|
public static ImageMetadata ParseFile(FilePath path)
|
||
|
{
|
||
1 year ago
|
return new ImageMetadata { Directories = ImageMetadataReader.ReadMetadata(path) };
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
public IEnumerable<Tag>? GetTextualData()
|
||
1 year ago
|
{
|
||
1 year ago
|
// Get the PNG-tEXt directory
|
||
1 year ago
|
return Directories?
|
||
|
.Where(d => d.Name == "PNG-tEXt")
|
||
|
.SelectMany(d => d.Tags)
|
||
|
.Where(t => t.Name == "Textual Data");
|
||
1 year ago
|
}
|
||
|
|
||
|
public GenerationParameters? GetGenerationParameters()
|
||
|
{
|
||
|
var textualData = GetTextualData()?.ToArray();
|
||
|
if (textualData is null)
|
||
1 year ago
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
1 year ago
|
// Use "parameters-json" tag if exists
|
||
1 year ago
|
if (
|
||
1 year ago
|
textualData.FirstOrDefault(
|
||
|
tag => tag.Description is { } desc && desc.StartsWith("parameters-json: ")
|
||
|
) is
|
||
|
{ Description: { } description }
|
||
1 year ago
|
)
|
||
|
{
|
||
1 year ago
|
description = description.StripStart("parameters-json: ");
|
||
|
|
||
|
return JsonSerializer.Deserialize<GenerationParameters>(description);
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// Otherwise parse "parameters" tag
|
||
|
if (
|
||
|
textualData.FirstOrDefault(
|
||
|
tag => tag.Description is { } desc && desc.StartsWith("parameters: ")
|
||
|
) is
|
||
|
{ Description: { } parameters }
|
||
|
)
|
||
|
{
|
||
|
parameters = parameters.StripStart("parameters: ");
|
||
|
|
||
|
if (GenerationParameters.TryParse(parameters, out var generationParameters))
|
||
|
{
|
||
|
return generationParameters;
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return null;
|
||
1 year ago
|
}
|
||
|
}
|