mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
5.3 KiB
162 lines
5.3 KiB
// <copyright file="GeneralFormatTests.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
|
|
namespace ImageSharp.Tests
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
using System.Numerics;
|
|
|
|
using Xunit;
|
|
|
|
public class GeneralFormatTests : FileTestBase
|
|
{
|
|
[Fact]
|
|
public void ResolutionShouldChange()
|
|
{
|
|
string path = this.CreateOutputDirectory("Resolution");
|
|
|
|
foreach (TestFile file in Files)
|
|
{
|
|
using (Image image = file.CreateImage())
|
|
{
|
|
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
|
|
{
|
|
image.VerticalResolution = 150;
|
|
image.HorizontalResolution = 150;
|
|
image.Save(output);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageCanEncodeToString()
|
|
{
|
|
string path = this.CreateOutputDirectory("ToString");
|
|
|
|
foreach (TestFile file in Files)
|
|
{
|
|
using (Image image = file.CreateImage())
|
|
{
|
|
string filename = path + "/" + file.FileNameWithoutExtension + ".txt";
|
|
File.WriteAllText(filename, image.ToBase64String());
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DecodeThenEncodeImageFromStreamShouldSucceed()
|
|
{
|
|
string path = this.CreateOutputDirectory("Encode");
|
|
|
|
foreach (TestFile file in Files)
|
|
{
|
|
using (Image image = file.CreateImage())
|
|
{
|
|
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
|
|
{
|
|
image.Save(output);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void QuantizeImageShouldPreserveMaximumColorPrecision()
|
|
{
|
|
string path = this.CreateOutputDirectory("Quantize");
|
|
|
|
foreach (TestFile file in Files)
|
|
{
|
|
using (Image image = file.CreateImage())
|
|
{
|
|
Color[] pixels = new Color[image.Width * image.Height];
|
|
Array.Copy(image.Pixels, pixels, image.Width * image.Height);
|
|
|
|
using (FileStream output = File.OpenWrite($"{path}/Octree-{file.FileName}"))
|
|
{
|
|
image.Quantize(Quantization.Octree)
|
|
.Save(output, image.CurrentImageFormat);
|
|
|
|
}
|
|
|
|
image.SetPixels(image.Width, image.Height, pixels);
|
|
using (FileStream output = File.OpenWrite($"{path}/Wu-{file.FileName}"))
|
|
{
|
|
image.Quantize(Quantization.Wu)
|
|
.Save(output, image.CurrentImageFormat);
|
|
}
|
|
|
|
image.SetPixels(image.Width, image.Height, pixels);
|
|
using (FileStream output = File.OpenWrite($"{path}/Palette-{file.FileName}"))
|
|
{
|
|
image.Quantize(Quantization.Palette)
|
|
.Save(output, image.CurrentImageFormat);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageCanConvertFormat()
|
|
{
|
|
string path = this.CreateOutputDirectory("Format");
|
|
|
|
foreach (TestFile file in Files)
|
|
{
|
|
Image image = file.CreateImage();
|
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.gif"))
|
|
{
|
|
image.SaveAsGif(output);
|
|
}
|
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp"))
|
|
{
|
|
image.SaveAsBmp(output);
|
|
}
|
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.jpg"))
|
|
{
|
|
image.SaveAsJpeg(output);
|
|
}
|
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.png"))
|
|
{
|
|
image.SaveAsPng(output);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldPreservePixelByteOrderWhenSerialized()
|
|
{
|
|
string path = this.CreateOutputDirectory("Serialized");
|
|
|
|
foreach (TestFile file in Files)
|
|
{
|
|
Image image = file.CreateImage();
|
|
|
|
byte[] serialized;
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
image.Save(memoryStream);
|
|
memoryStream.Flush();
|
|
serialized = memoryStream.ToArray();
|
|
}
|
|
|
|
using (MemoryStream memoryStream = new MemoryStream(serialized))
|
|
{
|
|
Image image2 = new Image(memoryStream);
|
|
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
|
|
{
|
|
image2.Save(output);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|