Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions StabilityMatrix.Avalonia/Languages/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions StabilityMatrix.Avalonia/Languages/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1883,4 +1883,13 @@ Account tokens will not be viewable after saving, please make a note of them if
<data name="Error_DocumentationPageLoadFailed" xml:space="preserve">
<value>Could not load this page. Check your connection and try again.</value>
</data>
<data name="Label_GeneratedWith" xml:space="preserve">
<value>Generated with</value>
</data>
<data name="Text_NoGenerationDataDetected" xml:space="preserve">
<value>No generation data detected</value>
</data>
<data name="Text_GenerationDataNotLoadable" xml:space="preserve">
<value>{0} generation data was found, but its prompt and settings can't be loaded</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using NLog;
using StabilityMatrix.Avalonia.Animations;
using StabilityMatrix.Avalonia.Extensions;
using StabilityMatrix.Avalonia.Languages;
using StabilityMatrix.Avalonia.Models;
using StabilityMatrix.Avalonia.Models.Inference;
using StabilityMatrix.Avalonia.Services;
Expand All @@ -27,6 +28,7 @@
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.Database;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.GenerationData;

#pragma warning disable CS0657 // Not a valid attribute location for this declaration

Expand Down Expand Up @@ -317,7 +319,43 @@ private void LoadImageMetadata(
return;
}

throw new ApplicationException("File does not contain SMProject or Parameters Metadata");
var generationData = GenerationDataParser.TryRead(imageFilePath);
if (generationData?.Parameters is { PositivePrompt: not null } generationParameters)
{
Logger.Info("Loading Parameters from {Source} metadata", generationData.DisplayName);

if (this is IParametersLoadableState paramsLoadableVm)
{
Dispatcher.UIThread.Invoke(() =>
paramsLoadableVm.LoadStateFromParameters(generationParameters)
);
}
else
{
Logger.Warn(
"Load parameters target {Type} does not implement IParametersLoadableState, skipping",
GetType().Name
);
}

if (this is IImageGalleryComponent imageGalleryComponent)
{
Dispatcher.UIThread.Invoke(() =>
imageGalleryComponent.LoadImagesToGallery(new ImageSource(imageFilePath))
);
}

return;
}

if (generationData is not null)
{
throw new ApplicationException(
string.Format(Resources.Text_GenerationDataNotLoadable, generationData.DisplayName)
);
}

throw new ApplicationException(Resources.Text_NoGenerationDataDetected);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Extensions;
using StabilityMatrix.Avalonia.Helpers;
using StabilityMatrix.Avalonia.Languages;
using StabilityMatrix.Avalonia.Models;
using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.Views;
Expand Down Expand Up @@ -39,6 +40,8 @@ ISettingsManager settingsManager

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(HasLocalGenerationParameters))]
[NotifyPropertyChangedFor(nameof(HasLocalImageFile))]
[NotifyPropertyChangedFor(nameof(LocalGenerationDataText))]
private LocalImageFile? localImageFile;

[ObservableProperty]
Expand All @@ -62,6 +65,19 @@ ISettingsManager settingsManager
/// </summary>
public bool HasLocalGenerationParameters => LocalImageFile?.GenerationParameters is not null;

/// <summary>
/// Whether a local file is shown, which always gets the info button to tell where it came from.
/// </summary>
public bool HasLocalImageFile => LocalImageFile is not null;

/// <summary>
/// The tool or format of the local file's generation data, or that none was found.
/// </summary>
public string? LocalGenerationDataText =>
LocalImageFile is null
? null
: LocalImageFile.GenerationDataLabel ?? Resources.Text_NoGenerationDataDetected;

/// <summary>
/// Whether Civitai image metadata is available.
/// </summary>
Expand Down
80 changes: 45 additions & 35 deletions StabilityMatrix.Avalonia/Views/Dialogs/ImageViewerDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@
VerticalAlignment="Top"
icons:Attached.Icon="fa-solid fa-info"
Classes="transparent-full"
Tapped="InfoButton_OnTapped">
Tapped="InfoButton_OnTapped"
ToolTip.Tip="{Binding LocalGenerationDataText}">
<Button.IsEnabled>
<MultiBinding Converter="{x:Static BoolConverters.Or}">
<Binding Path="HasLocalGenerationParameters" />
<Binding Path="HasLocalImageFile" />
<Binding Path="HasCivitImageMetadata" />
</MultiBinding>
</Button.IsEnabled>
Expand Down Expand Up @@ -326,43 +327,52 @@

<StackPanel
Margin="4,0"
DataContext="{Binding LocalImageFile.GenerationParameters}"
IsVisible="{Binding $parent[Grid].((vmDialogs:ImageViewerViewModel)DataContext).HasLocalGenerationParameters}"
IsVisible="{Binding HasLocalImageFile}"
Spacing="8">
<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Prompt}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding PositivePrompt}" />
<TextBlock Text="{x:Static lang:Resources.Label_GeneratedWith}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding LocalGenerationDataText}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_NegativePrompt}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding NegativePrompt}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Model}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding ModelName}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Sampler}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding Sampler}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Steps}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding Steps}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_CFGScale}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding CfgScale}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Seed}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding Seed}" />
</Grid>
<StackPanel
DataContext="{Binding LocalImageFile.GenerationParameters}"
IsVisible="{Binding $parent[Grid].((vmDialogs:ImageViewerViewModel)DataContext).HasLocalGenerationParameters}"
Spacing="8">
<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Prompt}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding PositivePrompt}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_NegativePrompt}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding NegativePrompt}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Model}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding ModelName}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Sampler}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding Sampler}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Steps}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding Steps}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_CFGScale}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding CfgScale}" />
</Grid>

<Grid RowDefinitions="Auto,*">
<TextBlock Text="{x:Static lang:Resources.Label_Seed}" Theme="{DynamicResource BodyStrongTextBlockStyle}" />
<SelectableTextBlock Grid.Row="1" Text="{Binding Seed}" />
</Grid>
</StackPanel>
</StackPanel>
</Grid>

Expand Down
5 changes: 5 additions & 0 deletions StabilityMatrix.Core/Helper/ImageMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public static (
{
var file = ImageFile.FromFile(filePath.Info.FullName);
var userComment = file.Properties.Get(ExifTag.UserComment);
if (userComment is null)
{
return (null, null, null, null, null);
}

var bytes = userComment.Interoperability.Data.Skip(8).ToArray();
var userCommentString = Encoding.BigEndianUnicode.GetString(bytes);

Expand Down
33 changes: 33 additions & 0 deletions StabilityMatrix.Core/Models/Database/LocalImageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SkiaSharp;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.GenerationData;
using JsonSerializer = System.Text.Json.JsonSerializer;
using Size = System.Drawing.Size;

Expand Down Expand Up @@ -36,6 +37,21 @@ public record LocalImageFile
/// </summary>
public GenerationParameters? GenerationParameters { get; init; }

/// <summary>
/// Format of the generation data embedded in the file, if any was found.
/// </summary>
public GenerationDataSource? GenerationDataSource { get; init; }

/// <summary>
/// The tool that made the file, when it can be told apart from the format of its generation data.
/// </summary>
public string? GenerationSoftware { get; init; }

/// <summary>
/// Name of the tool or format the generation data came from, or null when none was found.
/// </summary>
public string? GenerationDataLabel => GenerationSoftware ?? GenerationDataSource?.GetDisplayName();

/// <summary>
/// Dimensions of the image
/// </summary>
Expand Down Expand Up @@ -99,6 +115,23 @@ public record LocalImageFile
}

public static LocalImageFile FromPath(FilePath filePath)
{
var imageFile = FromPathWithoutGenerationData(filePath);
var generationData = GenerationDataParser.TryRead(filePath);
if (generationData is null)
{
return imageFile;
}

return imageFile with
{
GenerationDataSource = generationData.Source,
GenerationSoftware = generationData.Software,
GenerationParameters = imageFile.GenerationParameters ?? generationData.Parameters,
};
}

private static LocalImageFile FromPathWithoutGenerationData(FilePath filePath)
{
// TODO: Support other types
const LocalImageFileType imageType = LocalImageFileType.Inference | LocalImageFileType.TextToImage;
Expand Down
Loading
Loading