Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/ImageSharp/Formats/Tga/ImageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System.IO;

using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats.Tga;

namespace SixLabors.ImageSharp
{
/// <summary>
/// Extension methods for the <see cref="Image"/> type.
/// </summary>
public static partial class ImageExtensions
{
/// <summary>
/// Saves the image to the given stream with the tga format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsTga(this Image source, Stream stream) => SaveAsTga(source, stream, null);

/// <summary>
/// Saves the image to the given stream with the tga format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The options for the encoder.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsTga(this Image source, Stream stream, TgaEncoder encoder) =>
source.Save(
stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance));
}
}
19 changes: 12 additions & 7 deletions tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

using Xunit;

namespace SixLabors.ImageSharp.Tests
namespace SixLabors.ImageSharp.Tests.Formats
{
public class GeneralFormatTests : FileTestBase
{
Expand All @@ -41,7 +41,7 @@ public void ImageCanEncodeToString()
{
using (Image<Rgba32> image = file.CreateRgba32Image())
{
string filename = path + "/" + file.FileNameWithoutExtension + ".txt";
string filename = Path.Combine(path, $"{file.FileNameWithoutExtension}.txt");
File.WriteAllText(filename, image.ToBase64String(PngFormat.Instance));
}
}
Expand All @@ -56,7 +56,7 @@ public void DecodeThenEncodeImageFromStreamShouldSucceed()
{
using (Image<Rgba32> image = file.CreateRgba32Image())
{
image.Save($"{path}/{file.FileName}");
image.Save(Path.Combine(path, file.FileName));
}
}
}
Expand Down Expand Up @@ -103,25 +103,30 @@ public void ImageCanConvertFormat()
{
using (Image<Rgba32> image = file.CreateRgba32Image())
{
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp"))
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.bmp")))
{
image.SaveAsBmp(output);
}

using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.jpg"))
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.jpg")))
{
image.SaveAsJpeg(output);
}

using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.png"))
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.png")))
{
image.SaveAsPng(output);
}

using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.gif"))
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.gif")))
{
image.SaveAsGif(output);
}

using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.tga")))
{
image.SaveAsTga(output);
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Tga;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;

namespace SixLabors.ImageSharp.Tests
namespace SixLabors.ImageSharp.Tests.Formats
{
public class ImageFormatManagerTests
{
Expand All @@ -34,11 +35,13 @@ public void IfAutoLoadWellKnownFormatsIsTrueAllFormatsAreLoaded()
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<BmpEncoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<JpegEncoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<GifEncoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<TgaEncoder>().Count());

Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<PngDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<JpegDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<TgaDecoder>().Count());
}

[Fact]
Expand Down