Skip to content

Commit

Permalink
Added the IcoOptimizer to the ImageOptimizer.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Jul 21, 2019
1 parent cf5e113 commit c5e41c5
Show file tree
Hide file tree
Showing 6 changed files with 607 additions and 98 deletions.
46 changes: 13 additions & 33 deletions src/Magick.NET/Shared/Optimizers/ImageOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public bool Compress(Stream stream)
/// <param name="file">The file to check.</param>
/// <returns>True when the supplied file name is supported based on the extension of the file.</returns>
/// <returns>True when the image could be compressed otherwise false.</returns>
public bool IsSupported(FileInfo file) => IsSupported(MagickFormatInfo.Create(file));
public bool IsSupported(FileInfo file) => IsSupported(ImageOptimizerHelper.GetFormatInformation(file));

/// <summary>
/// Returns true when the supplied formation information is supported.
Expand All @@ -131,7 +131,7 @@ public bool IsSupported(MagickFormatInfo formatInfo)
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True when the supplied file name is supported based on the extension of the file.</returns>
public bool IsSupported(string fileName) => IsSupported(MagickFormatInfo.Create(fileName));
public bool IsSupported(string fileName) => IsSupported(ImageOptimizerHelper.GetFormatInformation(fileName));

/// <summary>
/// Returns true when the supplied stream is supported.
Expand All @@ -145,10 +145,7 @@ public bool IsSupported(Stream stream)
if (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)
return false;

var startPosition = stream.Position;
var info = new MagickImageInfo(stream);
stream.Position = startPosition;
return IsSupported(MagickFormatInfo.Create(info.Format));
return IsSupported(ImageOptimizerHelper.GetFormatInformation(stream));
}

/// <summary>
Expand Down Expand Up @@ -202,20 +199,11 @@ private static Collection<IImageOptimizer> CreateImageOptimizers()
{
new JpegOptimizer(),
new PngOptimizer(),
new IcoOptimizer(),
new GifOptimizer(),
};
}

private static MagickFormatInfo GetFormatInformation(FileInfo file)
{
var info = MagickNET.GetFormatInformation(file);
if (info != null)
return info;

var imageInfo = new MagickImageInfo(file);
return MagickNET.GetFormatInformation(imageInfo.Format);
}

private bool DoLosslessCompress(FileInfo file)
{
var optimizer = GetOptimizer(file);
Expand All @@ -238,28 +226,20 @@ private bool DoCompress(FileInfo file)

private IImageOptimizer GetOptimizer(FileInfo file)
{
var info = GetFormatInformation(file);
DebugThrow.IfNull(nameof(info), info);

foreach (var optimizer in _optimizers)
{
if (optimizer.Format.Module == info.Module)
return optimizer;
}

if (IgnoreUnsupportedFormats)
return null;

throw new MagickCorruptImageErrorException("Invalid format, supported formats are: " + SupportedFormats);
var info = ImageOptimizerHelper.GetFormatInformation(file);
return GetOptimizer(info);
}

private IImageOptimizer GetOptimizer(Stream stream)
{
var position = stream.Position;
var imageInfo = new MagickImageInfo(stream);
stream.Position = position;
var info = ImageOptimizerHelper.GetFormatInformation(stream);
return GetOptimizer(info);
}

var info = MagickNET.GetFormatInformation(imageInfo.Format);
private IImageOptimizer GetOptimizer(MagickFormatInfo info)
{
if (info == null)
return null;

foreach (var optimizer in _optimizers)
{
Expand Down
86 changes: 86 additions & 0 deletions src/Magick.NET/Shared/Optimizers/ImageOptimizerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,91 @@ public static void CheckStream([ValidatedNotNull] Stream stream)
Throw.IfFalse(nameof(stream), stream.CanWrite, "The stream should be writeable.");
Throw.IfFalse(nameof(stream), stream.CanSeek, "The stream should be seekable.");
}

public static MagickFormatInfo GetFormatInformation(FileInfo file)
{
var info = MagickNET.GetFormatInformation(file);
if (info != null)
return info;

try
{
var imageInfo = new MagickImageInfo(file);
return MagickNET.GetFormatInformation(imageInfo.Format);
}
catch
{
try
{
using (var stream = file.OpenRead())
{
return GetFormatInformationFromHeader(stream);
}
}
catch
{
return null;
}
}
}

public static MagickFormatInfo GetFormatInformation(string fileName)
{
var info = MagickNET.GetFormatInformation(fileName);
if (info != null)
return info;

try
{
var imageInfo = new MagickImageInfo(fileName);
return MagickNET.GetFormatInformation(imageInfo.Format);
}
catch
{
try
{
using (var stream = File.OpenRead(fileName))
{
return GetFormatInformationFromHeader(stream);
}
}
catch
{
return null;
}
}
}

public static MagickFormatInfo GetFormatInformation(Stream stream)
{
var startPosition = stream.Position;

try
{
var info = new MagickImageInfo(stream);
return MagickNET.GetFormatInformation(info.Format);
}
catch
{
stream.Position = startPosition;

return GetFormatInformationFromHeader(stream);
}
finally
{
stream.Position = startPosition;
}
}

private static MagickFormatInfo GetFormatInformationFromHeader(Stream stream)
{
var buffer = new byte[4];
stream.Read(buffer, 0, buffer.Length);

if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 1 && buffer[3] == 0)
return MagickNET.GetFormatInformation(MagickFormat.Ico);

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Magick.NET.Tests.Shared.Optimizers.ImageOptimizerTests
{
public partial class ImageOptimizerTests
{
private static FileStream OpenFile(string path)
private static FileStream OpenStream(string path)
{
return File.Open(path, FileMode.Open, FileAccess.ReadWrite);
}
Expand Down
Loading

0 comments on commit c5e41c5

Please sign in to comment.