Skip to content

Commit c7c6a3b

Browse files
Add support for multi page Tiff and Gif
1 parent 20cc132 commit c7c6a3b

3 files changed

Lines changed: 314 additions & 33 deletions

File tree

Binary file not shown.

IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using SixLabors.ImageSharp;
22
using SixLabors.ImageSharp.Processing;
33
using System;
4+
using System.Collections.Generic;
45
using System.IO;
6+
using System.Linq;
57
using Xunit;
68
using Xunit.Abstractions;
79

@@ -376,30 +378,88 @@ public void CastSixLabors_from_AnyBitmap()
376378
public void Load_Tiff_Image()
377379
{
378380
AnyBitmap anyBitmap = AnyBitmap.FromFile(GetRelativeFilePath("IRON-274-39065.tif"));
379-
Assert.Equal(1, anyBitmap.FrameCount);
381+
Assert.Equal(2, anyBitmap.FrameCount);
380382

381383
AnyBitmap multiPage = AnyBitmap.FromFile(GetRelativeFilePath("animated_qr.gif"));
382384
Assert.Equal(4, multiPage.FrameCount);
383-
Assert.Equal(4, multiPage.Frames.Count);
384-
multiPage.Frames[0].SaveAs("first.png");
385-
multiPage.Frames[3].SaveAs("last.png");
385+
Assert.Equal(4, multiPage.GetAllFrames.Count());
386+
multiPage.GetAllFrames.First().SaveAs("first.png");
387+
multiPage.GetAllFrames.Last().SaveAs("last.png");
386388
AssertImageAreEqual(GetRelativeFilePath("first-animated-qr.png"), "first.png");
387389
AssertImageAreEqual(GetRelativeFilePath("last-animated-qr.png"), "last.png");
388390

389391
byte[] bytes = File.ReadAllBytes(GetRelativeFilePath("IRON-274-39065.tif"));
390392
anyBitmap = AnyBitmap.FromBytes(bytes);
391-
Assert.Equal(1, anyBitmap.FrameCount);
393+
Assert.Equal(2, anyBitmap.FrameCount);
392394

393395
byte[] multiPageBytes = File.ReadAllBytes(GetRelativeFilePath("animated_qr.gif"));
394396
multiPage = AnyBitmap.FromBytes(multiPageBytes);
395397
Assert.Equal(4, multiPage.FrameCount);
396-
Assert.Equal(4, multiPage.Frames.Count);
397-
multiPage.Frames[0].SaveAs("first.png");
398-
multiPage.Frames[3].SaveAs("last.png");
398+
Assert.Equal(4, multiPage.GetAllFrames.Count());
399+
multiPage.GetAllFrames.First().SaveAs("first.png");
400+
multiPage.GetAllFrames.Last().SaveAs("last.png");
399401
AssertImageAreEqual(GetRelativeFilePath("first-animated-qr.png"), "first.png");
400402
AssertImageAreEqual(GetRelativeFilePath("last-animated-qr.png"), "last.png");
401403
}
402404

405+
[FactWithAutomaticDisplayName]
406+
public void Try_UnLoad_Tiff_Image()
407+
{
408+
AnyBitmap anyBitmap = AnyBitmap.FromFile(GetRelativeFilePath("multiframe.tiff"));
409+
Assert.Equal(2, anyBitmap.FrameCount);
410+
}
411+
412+
[FactWithAutomaticDisplayName]
413+
public void Create_Multi_page_Tiff()
414+
{
415+
List<AnyBitmap> bitmaps = new List<AnyBitmap>()
416+
{
417+
AnyBitmap.FromFile(GetRelativeFilePath("first-animated-qr.png")),
418+
AnyBitmap.FromFile(GetRelativeFilePath("last-animated-qr.png"))
419+
};
420+
421+
AnyBitmap anyBitmap = AnyBitmap.CreateMultiFrameTiff(bitmaps);
422+
Assert.Equal(2, anyBitmap.FrameCount);
423+
Assert.Equal(2, anyBitmap.GetAllFrames.Count());
424+
anyBitmap.GetAllFrames.ElementAt(0).SaveAs("first.png");
425+
anyBitmap.GetAllFrames.ElementAt(1).SaveAs("last.png");
426+
AssertImageAreEqual(GetRelativeFilePath("first-animated-qr.png"), "first.png");
427+
AssertImageAreEqual(GetRelativeFilePath("last-animated-qr.png"), "last.png");
428+
}
429+
430+
[FactWithAutomaticDisplayName]
431+
public void Create_Multi_page_Gif()
432+
{
433+
List<AnyBitmap> bitmaps = new List<AnyBitmap>()
434+
{
435+
AnyBitmap.FromFile(GetRelativeFilePath("first-animated-qr.png")),
436+
AnyBitmap.FromFile(GetRelativeFilePath("mountainclimbers.jpg"))
437+
};
438+
439+
AnyBitmap anyBitmap = AnyBitmap.CreateMultiFrameGif(bitmaps);
440+
Assert.Equal(2, anyBitmap.FrameCount);
441+
Assert.Equal(2, anyBitmap.GetAllFrames.Count());
442+
anyBitmap.GetAllFrames.ElementAt(0).SaveAs("first.png");
443+
Image first = Image.Load(GetRelativeFilePath("first-animated-qr.png"));
444+
first.Mutate(img => img.Resize(new ResizeOptions
445+
{
446+
Size = new SixLabors.ImageSharp.Size(anyBitmap.GetAllFrames.ElementAt(0).Width, anyBitmap.GetAllFrames.ElementAt(0).Height),
447+
Mode = SixLabors.ImageSharp.Processing.ResizeMode.BoxPad
448+
}));
449+
first.Save("first-expected.jpg");
450+
AssertImageAreEqual("first-expected.jpg", "first.png", true);
451+
452+
anyBitmap.GetAllFrames.ElementAt(1).SaveAs("last.png");
453+
Image last = Image.Load(GetRelativeFilePath("mountainclimbers.jpg"));
454+
last.Mutate(img => img.Resize(new ResizeOptions
455+
{
456+
Size = new SixLabors.ImageSharp.Size(anyBitmap.GetAllFrames.ElementAt(1).Width, anyBitmap.GetAllFrames.ElementAt(1).Height),
457+
Mode = SixLabors.ImageSharp.Processing.ResizeMode.BoxPad
458+
}));
459+
last.Save("last-expected.jpg");
460+
AssertImageAreEqual("last-expected.jpg", "last.png", true);
461+
}
462+
403463
#if !NET472
404464
[FactWithAutomaticDisplayName]
405465
public void CastMaui_to_AnyBitmap()

0 commit comments

Comments
 (0)