Skip to content

Commit

Permalink
Merge pull request #31 from the-database/dev
Browse files Browse the repository at this point in the history
add grayscale detection threshold, fix NoneType bug when there is no matched chain
  • Loading branch information
the-database authored Jul 31, 2024
2 parents 4672551 + 0f0dec4 commit 0b1d692
Show file tree
Hide file tree
Showing 3 changed files with 367 additions and 19 deletions.
308 changes: 306 additions & 2 deletions MangaJaNaiConverterGui/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,303 @@ public MainWindowViewModel(IPythonService? pythonService = null, IUpdateManagerS
"0x1984",
"0x1985",];

private static readonly string DEFAULT_WORKFLOW = """
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleWorkflow, MangaJaNaiConverterGui",
"WorkflowName": "Upscale Manga (Default)",
"WorkflowIndex": 0,
"SelectedTabIndex": 0,
"InputFilePath": "",
"InputFolderPath": "",
"OutputFilename": "%filename%-mangajanai",
"OutputFolderPath": "",
"OverwriteExistingFiles": false,
"UpscaleImages": true,
"UpscaleArchives": true,
"ResizeHeightAfterUpscale": 2160,
"ResizeWidthAfterUpscale": 3840,
"WebpSelected": true,
"AvifSelected": false,
"PngSelected": false,
"JpegSelected": false,
"UseLosslessCompression": false,
"LossyCompressionQuality": 80,
"ShowLossySettings": true,
"ModeScaleSelected": true,
"UpscaleScaleFactor": 4,
"ModeWidthSelected": false,
"ModeHeightSelected": false,
"ModeFitToDisplaySelected": false,
"DisplayDevice": "Kobo Elipsa 2E (2023)",
"DisplayDeviceWidth": 1404,
"DisplayDeviceHeight": 1872,
"DisplayPortraitSelected": true,
"ShowAdvancedSettings": false,
"GrayscaleDetectionThreshold": 12,
"Chains": {
"$type": "Avalonia.Collections.AvaloniaList`1[[MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui]], Avalonia.Base",
"$values": [
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "1",
"MinResolution": "0x0",
"MaxResolution": "0x0",
"IsGrayscale": false,
"IsColor": true,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_IllustrationJaNai_V1_ESRGAN_120k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": false,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "2",
"MinResolution": "0x0",
"MaxResolution": "0x0",
"IsGrayscale": false,
"IsColor": true,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_IllustrationJaNai_V1_ESRGAN_135k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": false,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "3",
"MinResolution": "0x0",
"MaxResolution": "0x1250",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_MangaJaNai_1200p_V1_ESRGAN_70k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "4",
"MinResolution": "0x0",
"MaxResolution": "0x1250",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_MangaJaNai_1200p_V1_ESRGAN_70k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "5",
"MinResolution": "0x1251",
"MaxResolution": "0x1350",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_MangaJaNai_1300p_V1_ESRGAN_75k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "6",
"MinResolution": "0x1251",
"MaxResolution": "0x1350",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_MangaJaNai_1300p_V1_ESRGAN_75k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "7",
"MinResolution": "0x1351",
"MaxResolution": "0x1450",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_MangaJaNai_1400p_V1_ESRGAN_70k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "8",
"MinResolution": "0x1351",
"MaxResolution": "0x1450",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_MangaJaNai_1400p_V1_ESRGAN_105k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "9",
"MinResolution": "0x1451",
"MaxResolution": "0x1550",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_MangaJaNai_1500p_V1_ESRGAN_90k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "10",
"MinResolution": "0x1451",
"MaxResolution": "0x1550",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_MangaJaNai_1500p_V1_ESRGAN_105k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "11",
"MinResolution": "0x1551",
"MaxResolution": "0x1760",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_MangaJaNai_1600p_V1_ESRGAN_90k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "12",
"MinResolution": "0x1551",
"MaxResolution": "0x1760",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_MangaJaNai_1600p_V1_ESRGAN_70k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "13",
"MinResolution": "0x1761",
"MaxResolution": "0x1984",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_MangaJaNai_1920p_V1_ESRGAN_70k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "14",
"MinResolution": "0x1761",
"MaxResolution": "0x1984",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_MangaJaNai_1920p_V1_ESRGAN_105k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "15",
"MinResolution": "0x1985",
"MaxResolution": "0x0",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 0,
"MaxScaleFactor": 2,
"ModelFilePath": "2x_MangaJaNai_2048p_V1_ESRGAN_95k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
},
{
"$type": "MangaJaNaiConverterGui.ViewModels.UpscaleChain, MangaJaNaiConverterGui",
"ChainNumber": "16",
"MinResolution": "0x1985",
"MaxResolution": "0x0",
"IsGrayscale": true,
"IsColor": false,
"MinScaleFactor": 2,
"MaxScaleFactor": 0,
"ModelFilePath": "4x_MangaJaNai_2048p_V1_ESRGAN_70k.pth",
"ModelTileSize": "Auto (Estimate)",
"AutoAdjustLevels": true,
"ResizeHeightBeforeUpscale": 0,
"ResizeWidthBeforeUpscale": 0,
"ResizeFactorBeforeUpscale": 100.0
}
]
}
}
""";

public string[] CommonResolutions
{
get => _commonResolutions;
Expand Down Expand Up @@ -1152,8 +1449,7 @@ public void ResetCurrentWorkflow()
{
if (CurrentWorkflow != null)
{
var lines = JsonConvert.SerializeObject(Workflows[0], NewtonsoftJsonSuspensionDriver.Settings);
var workflow = JsonConvert.DeserializeObject<UpscaleWorkflow>(lines, NewtonsoftJsonSuspensionDriver.Settings);
var workflow = JsonConvert.DeserializeObject<UpscaleWorkflow>(DEFAULT_WORKFLOW, NewtonsoftJsonSuspensionDriver.Settings);
var workflowIndex = CurrentWorkflow.WorkflowIndex;
var workflowName = $"Custom Workflow {workflowIndex}";

Expand Down Expand Up @@ -1744,6 +2040,14 @@ public bool ShowAdvancedSettings
set => this.RaiseAndSetIfChanged(ref _showAdvancedSettings, value);
}

private int _grayscaleDetectionThreshold = 12;
[DataMember]
public int GrayscaleDetectionThreshold
{
get => _grayscaleDetectionThreshold;
set => this.RaiseAndSetIfChanged(ref _grayscaleDetectionThreshold, value);
}

private AvaloniaList<UpscaleChain> _chains;
[DataMember]
public AvaloniaList<UpscaleChain> Chains
Expand Down
21 changes: 15 additions & 6 deletions MangaJaNaiConverterGui/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@
<!-- Main Form -->
<StackPanel Margin="20">
<Grid>
<StackPanel Orientation="Horizontal" Margin="0,10,0,10" IsVisible="{Binding !CurrentWorkflow.IsDefaultWorkflow}" Grid.Column="0">
<StackPanel Orientation="Horizontal" Margin="0,10,0,10" Grid.Column="0">
<TextBlock Margin="0,0,5,0" VerticalAlignment="Center">Workflow Name</TextBlock>
<TextBox Width="500" Margin="0,0,5,0" Text="{Binding CurrentWorkflow.WorkflowName}"/>
</StackPanel>

<StackPanel Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" IsVisible="{Binding !CurrentWorkflow.IsDefaultWorkflow}">
<StackPanel Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top">
<StackPanel Margin="0,10,0,0" Orientation="Horizontal" HorizontalAlignment="Right">

<Button Margin="10,0,10,0" Click="ImportCurrentWorkflowButtonClick">
Expand Down Expand Up @@ -433,11 +433,20 @@
<TextBlock Margin="0,0,5,0" FontWeight="Bold" Text="Advanced Settings"/>
<materialIcons:MaterialIcon Kind="QuestionMarkCircle" Opacity="0.5" VerticalAlignment="Center" />
</StackPanel>
<TextBlock FontSize="12" Foreground="Gray" Margin="0,10,0,10" IsVisible="{Binding CurrentWorkflow.IsDefaultWorkflow}">
Advanced settings are locked for the default workflow, and are displayed below for reference only. Select any custom workflow to modify the advanced settings for that workflow.
</TextBlock>

<Border Classes="border" IsEnabled="{Binding !CurrentWorkflow.IsDefaultWorkflow}">
<Border Classes="border">
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="10,10,0,0">
<TextBlock Margin="0,0,5,0" Text="Grayscale Detection Threshold" VerticalAlignment="Center" />
<Slider VerticalAlignment="Center" Value="{Binding CurrentWorkflow.GrayscaleDetectionThreshold}" Minimum="0" Maximum="24" Width="500" TickFrequency="1" IsSnapToTickEnabled="True" />
<TextBlock VerticalAlignment="Center" Width="40" Margin="10,10,0,10" Text="{Binding CurrentWorkflow.GrayscaleDetectionThreshold}" HorizontalAlignment="Center"/>
<TextBlock Width="650" TextWrapping="WrapWithOverflow" Foreground="Gray" FontSize="12" VerticalAlignment="Center" Margin="20,0,0,0">The threshold for which an image is considered grayscale. The default value of 12 considers images with slight color as grayscale, because some grayscale images have slight color due to artifacts. Set the threshold to 0 to consider only strictly grayscale images as grayscale, and increase the threshold to consider images with more color as grayscale.</TextBlock>
</StackPanel>

</StackPanel>
</Border>

<Border Classes="border">
<StackPanel>
<ItemsControl ItemsSource="{Binding CurrentWorkflow.Chains}">
<ItemsControl.ItemTemplate>
Expand Down
Loading

0 comments on commit 0b1d692

Please sign in to comment.