Skip to content

Commit

Permalink
Added MagickReadSettings overloads to the Create method of MagickImag…
Browse files Browse the repository at this point in the history
…eInfoFactory.
  • Loading branch information
dlemstra committed Oct 29, 2023
1 parent 4884ee2 commit 937d366
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using System;
using System.IO;

namespace ImageMagick;

/// <summary>
/// Class that can be used to create <see cref="IMagickImageInfo"/> instances.
/// </summary>
/// <typeparam name="TQuantumType">The quantum type.</typeparam>
public partial interface IMagickImageInfoFactory<TQuantumType> : IMagickImageInfoFactory
where TQuantumType : struct, IConvertible
{
/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="data">The byte array to read the information from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
IMagickImageInfo Create(byte[] data, IMagickReadSettings<TQuantumType>? readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="data">The byte array to read the information from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
IMagickImageInfo Create(byte[] data, int offset, int count, IMagickReadSettings<TQuantumType>? readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="file">The file to read the image from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
IMagickImageInfo Create(FileInfo file, IMagickReadSettings<TQuantumType>? readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
IMagickImageInfo Create(Stream stream, IMagickReadSettings<TQuantumType>? readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
IMagickImageInfo Create(string fileName, IMagickReadSettings<TQuantumType>? readSettings);
}
72 changes: 67 additions & 5 deletions src/Magick.NET/Factories/MagickImageInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@

using System.IO;

#if Q8
using QuantumType = System.Byte;
#elif Q16
using QuantumType = System.UInt16;
#elif Q16HDRI
using QuantumType = System.Single;
#else
#error Not implemented!
#endif

namespace ImageMagick;

/// <summary>
/// Class that can be used to create <see cref="IMagickImageInfo"/> instances.
/// </summary>
public sealed partial class MagickImageInfoFactory : IMagickImageInfoFactory
public sealed partial class MagickImageInfoFactory : IMagickImageInfoFactory<QuantumType>
{
/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
Expand All @@ -26,6 +36,16 @@ public IMagickImageInfo Create()
public IMagickImageInfo Create(byte[] data)
=> new MagickImageInfo(data);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="data">The byte array to read the information from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(byte[] data, IMagickReadSettings<QuantumType>? readSettings)
=> new MagickImageInfo(data, readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
Expand All @@ -35,7 +55,19 @@ public IMagickImageInfo Create(byte[] data)
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(byte[] data, int offset, int count)
=> new MagickImageInfo(data, offset, count);
=> Create(data, offset, count, null);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="data">The byte array to read the information from.</param>
/// <param name="offset">The offset at which to begin reading data.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(byte[] data, int offset, int count, IMagickReadSettings<QuantumType>? readSettings)
=> new MagickImageInfo(data, offset, count, readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
Expand All @@ -44,7 +76,17 @@ public IMagickImageInfo Create(byte[] data, int offset, int count)
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(FileInfo file)
=> new MagickImageInfo(file);
=> Create(file, null);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="file">The file to read the image from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(FileInfo file, IMagickReadSettings<QuantumType>? readSettings)
=> new MagickImageInfo(file, readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
Expand All @@ -53,7 +95,17 @@ public IMagickImageInfo Create(FileInfo file)
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(Stream stream)
=> new MagickImageInfo(stream);
=> Create(stream, null);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="stream">The stream to read the image data from.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(Stream stream, IMagickReadSettings<QuantumType>? readSettings)
=> new MagickImageInfo(stream, readSettings);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
Expand All @@ -62,5 +114,15 @@ public IMagickImageInfo Create(Stream stream)
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(string fileName)
=> new MagickImageInfo(fileName);
=> Create(fileName, null);

/// <summary>
/// Initializes a new instance that implements <see cref="IMagickImageInfo"/>.
/// </summary>
/// <param name="fileName">The fully qualified name of the image file, or the relative image file name.</param>
/// <param name="readSettings">The settings to use when reading the image.</param>
/// <returns>A new <see cref="IMagickImageInfo"/> instance.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IMagickImageInfo Create(string fileName, IMagickReadSettings<QuantumType>? readSettings)
=> new MagickImageInfo(fileName, readSettings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using ImageMagick;
using ImageMagick.Formats;
using Xunit;

namespace Magick.NET.Tests;
Expand Down Expand Up @@ -149,6 +150,24 @@ public void ShouldCreateMagickImageInfo()
}
}

public class WithFileNameAndReadSettings
{
[Fact]
public void ShouldUseTheReadSettings()
{
var factory = new MagickImageInfoFactory();
var settings = new MagickReadSettings(new BmpReadDefines
{
IgnoreFileSize = true,
});

var info = factory.Create(Files.Coders.InvalidCrcBMP, settings);

Assert.IsType<MagickImageInfo>(info);
Assert.Equal(1, info.Width);
}
}

public class WithStream
{
[Fact]
Expand Down

0 comments on commit 937d366

Please sign in to comment.