public DiscoverImageFormatTests() { this.localImageFormatMock = new Mock <IImageFormat>(); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.fileSystem = new Mock <IFileSystem>(); this.LocalConfiguration = new Configuration { FileSystem = this.fileSystem.Object }; this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector); TestFormat.RegisterGlobalTestFormat(); this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); this.FilePath = Guid.NewGuid().ToString(); this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream); TestFileSystem.RegisterGlobalTestFormat(); TestFileSystem.Global.AddFile(this.FilePath, this.DataStream); }
protected ImageLoadTestBase() { this.returnImage = new Image <Rgba32>(1, 1); this.localImageFormatMock = new Mock <IImageFormat>(); this.localDecoder = new Mock <IImageDecoder>(); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.localDecoder.Setup(x => x.Decode <Rgba32>(It.IsAny <Configuration>(), It.IsAny <Stream>())) .Callback <Configuration, Stream>((c, s) => { using (var ms = new MemoryStream()) { s.CopyTo(ms); this.DecodedData = ms.ToArray(); } }) .Returns(this.returnImage); this.LocalConfiguration = new Configuration { }; this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); this.LocalConfiguration.ImageFormatsManager.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); this.TopLevelConfiguration = new Configuration(this.TestFormat); this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = this.TestFormat.CreateStream(this.Marker); this.localFileSystemMock.Setup(x => x.OpenRead(this.MockFilePath)).Returns(this.DataStream); this.topLevelFileSystem.AddFile(this.MockFilePath, this.DataStream); this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object; this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; }
/// <summary> /// detects image type from binary data /// </summary> /// <param name="blob">must be supplied</param> public async static Task <(int width, int height, string mimeType)> DetectImageType(this byte[] blob) { if (blob == null) { throw new ArgumentNullException(nameof(blob)); } var config = new Configuration(); var formatsDetectionsWanted = new IImageFormatDetector[] { new JpegImageFormatDetector(), new PngImageFormatDetector(), new SixLabors.ImageSharp.Formats.Gif.GifImageFormatDetector(), new SixLabors.ImageSharp.Formats.Tga.TgaImageFormatDetector() }; var decodersWanted = new (IImageDecoder decoder, IImageFormat format)[] {
private static void ConfigureCodecs( this Configuration cfg, IImageFormat imageFormat, IImageDecoder decoder, IImageEncoder encoder, IImageFormatDetector detector) { cfg.ImageFormatsManager.SetDecoder(imageFormat, decoder); cfg.ImageFormatsManager.SetEncoder(imageFormat, encoder); cfg.ImageFormatsManager.AddImageFormatDetector(detector); }
protected ImageLoadTestBase() { this.localStreamReturnImageRgba32 = new Image <Rgba32>(1, 1); this.localStreamReturnImageAgnostic = new Image <Bgra4444>(1, 1); this.localImageInfoMock = new Mock <IImageInfo>(); this.localImageFormatMock = new Mock <IImageFormat>(); var detector = new Mock <IImageInfoDetector>(); detector.Setup(x => x.Identify(It.IsAny <Configuration>(), It.IsAny <Stream>())).Returns(this.localImageInfoMock.Object); detector.Setup(x => x.IdentifyAsync(It.IsAny <Configuration>(), It.IsAny <Stream>(), It.IsAny <CancellationToken>())).ReturnsAsync(this.localImageInfoMock.Object); this.localDecoder = detector.As <IImageDecoder>(); this.localDecoder.Setup(x => x.Decode <Rgba32>(It.IsAny <Configuration>(), It.IsAny <Stream>())) .Callback <Configuration, Stream>((c, s) => { using (var ms = new MemoryStream()) { s.CopyTo(ms); this.DecodedData = ms.ToArray(); } }) .Returns(this.localStreamReturnImageRgba32); this.localDecoder.Setup(x => x.Decode(It.IsAny <Configuration>(), It.IsAny <Stream>())) .Callback <Configuration, Stream>((c, s) => { using (var ms = new MemoryStream()) { s.CopyTo(ms); this.DecodedData = ms.ToArray(); } }) .Returns(this.localStreamReturnImageAgnostic); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.LocalConfiguration = new Configuration(); this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); this.LocalConfiguration.ImageFormatsManager.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); this.TopLevelConfiguration = new Configuration(this.TestFormat); this.Marker = Guid.NewGuid().ToByteArray(); this.dataStreamLazy = new Lazy <Stream>(this.CreateStream); Stream StreamFactory() => this.DataStream; this.LocalFileSystemMock.Setup(x => x.OpenRead(this.MockFilePath)).Returns(StreamFactory); this.topLevelFileSystem.AddFile(this.MockFilePath, StreamFactory); this.LocalConfiguration.FileSystem = this.LocalFileSystemMock.Object; this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; }
/// <summary> /// detects image type from binary data /// </summary> /// <param name="blob">must be supplied</param> public async static Task <(int width, int height, string mimeType)> DetectImageType(this byte[] blob, CancellationToken cancellationToken = default) { if (blob == null) { throw new ArgumentNullException(nameof(blob)); } if (blob.Length > 20000000) { return(0, 0, "application/octet"); } var config = new Configuration(); var formatsDetectionsWanted = new IImageFormatDetector[] { new JpegImageFormatDetector(), new PngImageFormatDetector(), new SixLabors.ImageSharp.Formats.Gif.GifImageFormatDetector(), new SixLabors.ImageSharp.Formats.Tga.TgaImageFormatDetector() }; var decodersWanted = new (IImageDecoder decoder, IImageFormat format)[] {
public ImageSaveTests() { this.localImageFormat = new Mock <IImageFormat>(); this.localImageFormat.Setup(x => x.FileExtensions).Returns(new[] { "png" }); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormat.Object); this.encoder = new Mock <IImageEncoder>(); this.encoderNotInFormat = new Mock <IImageEncoder>(); this.fileSystem = new Mock <IFileSystem>(); var config = new Configuration { FileSystem = this.fileSystem.Object }; config.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); config.ImageFormatsManager.SetEncoder(this.localImageFormat.Object, this.encoder.Object); this.image = new Image <Rgba32>(config, 1, 1); }
public ImageLoadTests() { this.returnImage = new Image <Rgba32>(1, 1); this.localImageFormatMock = new Mock <IImageFormat>(); this.localDecoder = new Mock <IImageDecoder>(); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.localDecoder.Setup(x => x.Decode <Rgba32>(It.IsAny <Configuration>(), It.IsAny <Stream>())) .Callback <Configuration, Stream>((c, s) => { using (var ms = new MemoryStream()) { s.CopyTo(ms); this.DecodedData = ms.ToArray(); } }) .Returns(this.returnImage); this.fileSystem = new Mock <IFileSystem>(); this.LocalConfiguration = new Configuration { FileSystem = this.fileSystem.Object }; this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector); this.LocalConfiguration.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); TestFormat.RegisterGlobalTestFormat(); this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); this.FilePath = Guid.NewGuid().ToString(); this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream); TestFileSystem.RegisterGlobalTestFormat(); TestFileSystem.Global.AddFile(this.FilePath, this.DataStream); }
/// <summary> /// Adds a new detector for detecting mime types. /// </summary> /// <param name="detector">The detector to add</param> public void AddImageFormatDetector(IImageFormatDetector detector) { Guard.NotNull(detector, nameof(detector)); this.imageFormatDetectors.Add(detector); this.SetMaxHeaderSize(); }
/// <summary> /// Adds a new detector for detecting mime types. /// </summary> /// <param name="detector">The detector to add</param> public void AddImageFormatDetector(IImageFormatDetector detector) { this.imageFormatDetectors.Add(detector); this.MaxHeaderSize = this.imageFormatDetectors.Max(x => x.HeaderSize); }