From 937d36647784faa521f68a9553bd8ed8136a7986 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sun, 29 Oct 2023 13:36:41 +0100 Subject: [PATCH] Added MagickReadSettings overloads to the Create method of MagickImageInfoFactory. --- .../IMagickImageInfoFactory{TQuantumType}.cs | 62 ++++++++++++++++ .../Factories/MagickImageInfoFactory.cs | 72 +++++++++++++++++-- .../TheCreateMethod.cs | 19 +++++ 3 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 src/Magick.NET.Core/Factories/IMagickImageInfoFactory{TQuantumType}.cs diff --git a/src/Magick.NET.Core/Factories/IMagickImageInfoFactory{TQuantumType}.cs b/src/Magick.NET.Core/Factories/IMagickImageInfoFactory{TQuantumType}.cs new file mode 100644 index 0000000000..c4b94a6aa6 --- /dev/null +++ b/src/Magick.NET.Core/Factories/IMagickImageInfoFactory{TQuantumType}.cs @@ -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; + +/// +/// Class that can be used to create instances. +/// +/// The quantum type. +public partial interface IMagickImageInfoFactory : IMagickImageInfoFactory + where TQuantumType : struct, IConvertible +{ + /// + /// Initializes a new instance that implements . + /// + /// The byte array to read the information from. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + IMagickImageInfo Create(byte[] data, IMagickReadSettings? readSettings); + + /// + /// Initializes a new instance that implements . + /// + /// The byte array to read the information from. + /// The offset at which to begin reading data. + /// The maximum number of bytes to read. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + IMagickImageInfo Create(byte[] data, int offset, int count, IMagickReadSettings? readSettings); + + /// + /// Initializes a new instance that implements . + /// + /// The file to read the image from. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + IMagickImageInfo Create(FileInfo file, IMagickReadSettings? readSettings); + + /// + /// Initializes a new instance that implements . + /// + /// The stream to read the image data from. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + IMagickImageInfo Create(Stream stream, IMagickReadSettings? readSettings); + + /// + /// Initializes a new instance that implements . + /// + /// The fully qualified name of the image file, or the relative image file name. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + IMagickImageInfo Create(string fileName, IMagickReadSettings? readSettings); +} diff --git a/src/Magick.NET/Factories/MagickImageInfoFactory.cs b/src/Magick.NET/Factories/MagickImageInfoFactory.cs index cac9bd0a41..6d1538ec7e 100644 --- a/src/Magick.NET/Factories/MagickImageInfoFactory.cs +++ b/src/Magick.NET/Factories/MagickImageInfoFactory.cs @@ -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; /// /// Class that can be used to create instances. /// -public sealed partial class MagickImageInfoFactory : IMagickImageInfoFactory +public sealed partial class MagickImageInfoFactory : IMagickImageInfoFactory { /// /// Initializes a new instance that implements . @@ -26,6 +36,16 @@ public IMagickImageInfo Create() public IMagickImageInfo Create(byte[] data) => new MagickImageInfo(data); + /// + /// Initializes a new instance that implements . + /// + /// The byte array to read the information from. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + public IMagickImageInfo Create(byte[] data, IMagickReadSettings? readSettings) + => new MagickImageInfo(data, readSettings); + /// /// Initializes a new instance that implements . /// @@ -35,7 +55,19 @@ public IMagickImageInfo Create(byte[] data) /// A new instance. /// Thrown when an error is raised by ImageMagick. public IMagickImageInfo Create(byte[] data, int offset, int count) - => new MagickImageInfo(data, offset, count); + => Create(data, offset, count, null); + + /// + /// Initializes a new instance that implements . + /// + /// The byte array to read the information from. + /// The offset at which to begin reading data. + /// The maximum number of bytes to read. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + public IMagickImageInfo Create(byte[] data, int offset, int count, IMagickReadSettings? readSettings) + => new MagickImageInfo(data, offset, count, readSettings); /// /// Initializes a new instance that implements . @@ -44,7 +76,17 @@ public IMagickImageInfo Create(byte[] data, int offset, int count) /// A new instance. /// Thrown when an error is raised by ImageMagick. public IMagickImageInfo Create(FileInfo file) - => new MagickImageInfo(file); + => Create(file, null); + + /// + /// Initializes a new instance that implements . + /// + /// The file to read the image from. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + public IMagickImageInfo Create(FileInfo file, IMagickReadSettings? readSettings) + => new MagickImageInfo(file, readSettings); /// /// Initializes a new instance that implements . @@ -53,7 +95,17 @@ public IMagickImageInfo Create(FileInfo file) /// A new instance. /// Thrown when an error is raised by ImageMagick. public IMagickImageInfo Create(Stream stream) - => new MagickImageInfo(stream); + => Create(stream, null); + + /// + /// Initializes a new instance that implements . + /// + /// The stream to read the image data from. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + public IMagickImageInfo Create(Stream stream, IMagickReadSettings? readSettings) + => new MagickImageInfo(stream, readSettings); /// /// Initializes a new instance that implements . @@ -62,5 +114,15 @@ public IMagickImageInfo Create(Stream stream) /// A new instance. /// Thrown when an error is raised by ImageMagick. public IMagickImageInfo Create(string fileName) - => new MagickImageInfo(fileName); + => Create(fileName, null); + + /// + /// Initializes a new instance that implements . + /// + /// The fully qualified name of the image file, or the relative image file name. + /// The settings to use when reading the image. + /// A new instance. + /// Thrown when an error is raised by ImageMagick. + public IMagickImageInfo Create(string fileName, IMagickReadSettings? readSettings) + => new MagickImageInfo(fileName, readSettings); } diff --git a/tests/Magick.NET.Tests/Factories/MagickImageInfoFactoryTests/TheCreateMethod.cs b/tests/Magick.NET.Tests/Factories/MagickImageInfoFactoryTests/TheCreateMethod.cs index 951bcec5a1..8a5ef18c14 100644 --- a/tests/Magick.NET.Tests/Factories/MagickImageInfoFactoryTests/TheCreateMethod.cs +++ b/tests/Magick.NET.Tests/Factories/MagickImageInfoFactoryTests/TheCreateMethod.cs @@ -4,6 +4,7 @@ using System; using System.IO; using ImageMagick; +using ImageMagick.Formats; using Xunit; namespace Magick.NET.Tests; @@ -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(info); + Assert.Equal(1, info.Width); + } + } + public class WithStream { [Fact]