Skip to content

Commit

Permalink
Finally fixed anoying issue where some images would be corrupted in T…
Browse files Browse the repository at this point in the history
…WLM++. (Adobe metadata issue)
  • Loading branch information
KirovAir committed Oct 23, 2019
1 parent 851687f commit f383c59
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 30 deletions.
26 changes: 13 additions & 13 deletions TwilightBoxart.UX/App.config
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0"/>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1"/>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0"/>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1"/>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1"/>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV1"/>
<add key="DpiAwareness" value="PerMonitorV1" />
</System.Windows.Forms.ApplicationConfigurationSection>
</configuration>
12 changes: 6 additions & 6 deletions TwilightBoxart.UX/TwilightBoxart.UX.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.Net.Compilers.Toolset.3.2.1\build\Microsoft.Net.Compilers.Toolset.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.Toolset.3.2.1\build\Microsoft.Net.Compilers.Toolset.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.Toolset.3.3.1\build\Microsoft.Net.Compilers.Toolset.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.Toolset.3.3.1\build\Microsoft.Net.Compilers.Toolset.props')" />
<Import Project="..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props" Condition="Exists('..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -87,8 +87,8 @@
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
Expand Down Expand Up @@ -178,14 +178,13 @@
<None Include="Resources\GitHub-Mark-64px.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Fody.6.0.0\build\Fody.targets" Condition="Exists('..\packages\Fody.6.0.0\build\Fody.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Fody.6.0.0\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.6.0.0\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.4.1.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.Toolset.3.2.1\build\Microsoft.Net.Compilers.Toolset.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.Toolset.3.2.1\build\Microsoft.Net.Compilers.Toolset.props'))" />
<Error Condition="!Exists('..\packages\Fody.6.0.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.6.0.2\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.Toolset.3.3.1\build\Microsoft.Net.Compilers.Toolset.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.Toolset.3.3.1\build\Microsoft.Net.Compilers.Toolset.props'))" />
</Target>
<PropertyGroup>
<PostBuildEvent>goto :$(ConfigurationName)
Expand All @@ -201,4 +200,5 @@ goto :exit
:Debug
:exit</PostBuildEvent>
</PropertyGroup>
<Import Project="..\packages\Fody.6.0.2\build\Fody.targets" Condition="Exists('..\packages\Fody.6.0.2\build\Fody.targets')" />
</Project>
10 changes: 5 additions & 5 deletions TwilightBoxart.UX/packages.config
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="4.1.0" targetFramework="net461" />
<package id="Fody" version="6.0.0" targetFramework="net461" developmentDependency="true" />
<package id="Microsoft.Net.Compilers.Toolset" version="3.2.1" targetFramework="net472" developmentDependency="true" />
<package id="Fody" version="6.0.2" targetFramework="net472" developmentDependency="true" />
<package id="Microsoft.Net.Compilers.Toolset" version="3.3.1" targetFramework="net472" developmentDependency="true" />
<package id="SixLabors.Core" version="1.0.0-beta0008" targetFramework="net472" />
<package id="SixLabors.ImageSharp" version="1.0.0-beta0007" targetFramework="net472" />
<package id="System.Buffers" version="4.5.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Reflection.Emit" version="4.3.0" targetFramework="net472" />
<package id="System.Reflection.Emit.Lightweight" version="4.3.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
<package id="System.Reflection.Emit" version="4.6.0" targetFramework="net472" />
<package id="System.Reflection.Emit.Lightweight" version="4.6.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.3" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
<package id="Utf8Json" version="1.3.7" targetFramework="net472" />
Expand Down
62 changes: 58 additions & 4 deletions TwilightBoxart/Helpers/ImgDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System.Net;
using System.IO;
using System.Net;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;

Expand All @@ -14,21 +20,69 @@ public ImgDownloader(int width, int height)
{
_width = width;
_height = height;
}
}

public void DownloadAndResize(string url, string targetFile)
{
using (var webClient = new WebClient())
{
var data = webClient.DownloadData(url);
using (var image = Image.Load(data))
var decoder = GetDecoder(url);

using (var image = decoder == null ? Image.Load(data) : Image.Load(data, decoder))
{
image.Metadata.ExifProfile = null;

image.Mutate(x => x.Resize(_width, _height));
image.Save(targetFile);

var encoder = GetEncoder(image, targetFile);
image.Save(targetFile, encoder);
}
}
}

private IImageDecoder GetDecoder(string sourceFile)
{
var ext = Path.GetExtension(sourceFile)?.ToLower();

switch (ext)
{
case ".png":
return new PngDecoder { IgnoreMetadata = true };
case ".jpg":
case ".jpeg":
return new JpegDecoder { IgnoreMetadata = true };
case ".gif":
return new GifDecoder { IgnoreMetadata = true };
default:
return null;
}
}

private IImageEncoder GetEncoder(Image image, string targetFile)
{
var ext = Path.GetExtension(targetFile);
var manager = image.GetConfiguration().ImageFormatsManager;
var format = manager.FindFormatByFileExtension(ext);
var encoder = manager.FindEncoder(format);

if (encoder is PngEncoder pngEncoder)
{
SetPngSettings(pngEncoder);
}

return encoder;
}

private void SetPngSettings(PngEncoder encoder)
{
encoder.CompressionLevel = 9;
encoder.InterlaceMethod = PngInterlaceMode.None;
encoder.BitDepth = PngBitDepth.Bit8;
encoder.ColorType = PngColorType.Rgb;
encoder.FilterMethod = PngFilterMethod.Adaptive;
}

public void SetSizeAdjustedToAspectRatio(Size aspectRatio)
{
if (_width == aspectRatio.Width || _height == aspectRatio.Height)
Expand Down
5 changes: 3 additions & 2 deletions TwilightBoxart/Models/Base/LibRetroRom.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using KirovAir.Core.Extensions;
using System;
using KirovAir.Core.Extensions;
using KirovAir.Core.Utilities;

namespace TwilightBoxart.Models.Base
Expand Down Expand Up @@ -56,7 +57,7 @@ private void DownloadWithRetry(string name, string targetFile)
{
Download(ConsoleType, name, targetFile);
}
catch
catch (Exception e)
{
if (NoIntroConsoleType == ConsoleType.Unknown || ConsoleType == NoIntroConsoleType) throw;

Expand Down

0 comments on commit f383c59

Please sign in to comment.