99 lines
4.1 KiB
C#
99 lines
4.1 KiB
C#
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
|
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
using MaddoShared.ImageSharpTests.Helpers;
|
|
using Shouldly;
|
|
|
|
namespace MaddoShared.ImageSharpTests.Tests
|
|
{
|
|
[TestClass]
|
|
public class ImageResizingTests
|
|
{
|
|
[TestMethod]
|
|
public async Task BigImageResizesRespectSettings()
|
|
{
|
|
using var ws = new TempWorkspace();
|
|
|
|
// create a large input image
|
|
var inputPath = TestImageFactory.CreateSolidJpeg(ws.SourceDir.FullName, "input.jpg", 1600, 1200, new Rgba32(200, 200, 200, 255));
|
|
|
|
var pic = CreatorFactory.CreateDefaultPicSettings();
|
|
pic.LarghezzaBig = 800;
|
|
pic.AltezzaBig = 600;
|
|
pic.CreaMiniature = false;
|
|
|
|
var svc = CreatorFactory.CreateImageCreator(pic);
|
|
|
|
var state = new MaddoShared.ImageState
|
|
{
|
|
WorkFile = new FileInfo(inputPath),
|
|
DestDir = ws.DestDir,
|
|
SourceDir = ws.SourceDir
|
|
};
|
|
|
|
await svc.CreateImageAsync(state, null);
|
|
|
|
var outPath = Path.Combine(ws.DestDir.FullName, state.NomeFileBig);
|
|
using var outImg = SixLabors.ImageSharp.Image.Load<Rgba32>(outPath);
|
|
|
|
outImg.Width.ShouldBe(800);
|
|
outImg.Height.ShouldBe(600);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task RemoveExifData_StripsExifProfileWhenEnabledAndPreservesItWhenDisabled()
|
|
{
|
|
using var ws = new TempWorkspace();
|
|
var preserveInputPath = CreateJpegWithExifMake(ws.SourceDir.FullName, "preserve.jpg");
|
|
var stripInputPath = CreateJpegWithExifMake(ws.SourceDir.FullName, "strip.jpg");
|
|
|
|
var preserveSettings = CreatorFactory.CreateDefaultPicSettings();
|
|
preserveSettings.RemoveExifData = false;
|
|
var preserveCreator = CreatorFactory.CreateImageCreator(preserveSettings);
|
|
var preserveState = new MaddoShared.ImageState
|
|
{
|
|
WorkFile = new FileInfo(preserveInputPath),
|
|
DestDir = ws.DestDir
|
|
};
|
|
await preserveCreator.CreateImageAsync(preserveState, null);
|
|
|
|
var stripSettings = CreatorFactory.CreateDefaultPicSettings();
|
|
stripSettings.RemoveExifData = true;
|
|
stripSettings.CreaMiniature = true;
|
|
var stripCreator = CreatorFactory.CreateImageCreator(stripSettings);
|
|
var stripState = new MaddoShared.ImageState
|
|
{
|
|
WorkFile = new FileInfo(stripInputPath),
|
|
DestDir = ws.DestDir
|
|
};
|
|
await stripCreator.CreateImageAsync(stripState, null);
|
|
|
|
using var preservedOutput = Image.Load<Rgba32>(Path.Combine(ws.DestDir.FullName, preserveState.NomeFileBig));
|
|
var preservedExif = preservedOutput.Metadata.ExifProfile;
|
|
preservedExif.ShouldNotBeNull();
|
|
preservedExif.TryGetValue(ExifTag.Make, out var make).ShouldBeTrue();
|
|
make!.Value.ShouldBe("Test camera");
|
|
|
|
using var strippedOutput = Image.Load<Rgba32>(Path.Combine(ws.DestDir.FullName, stripState.NomeFileBig));
|
|
strippedOutput.Metadata.ExifProfile.ShouldBeNull();
|
|
|
|
using var strippedThumbnail = Image.Load<Rgba32>(Path.Combine(ws.DestDir.FullName, stripState.NomeFileSmall));
|
|
strippedThumbnail.Metadata.ExifProfile.ShouldBeNull();
|
|
}
|
|
|
|
private static string CreateJpegWithExifMake(string directory, string fileName)
|
|
{
|
|
var path = TestImageFactory.CreateSolidJpeg(directory, fileName, 160, 120, new Rgba32(200, 200, 200, 255));
|
|
using var image = Image.Load<Rgba32>(path);
|
|
var exifProfile = new ExifProfile();
|
|
exifProfile.SetValue(ExifTag.Make, "Test camera");
|
|
image.Metadata.ExifProfile = exifProfile;
|
|
image.Save(path, new JpegEncoder { Quality = 90 });
|
|
return path;
|
|
}
|
|
}
|
|
}
|