Skip to content

Commit f2371f2

Browse files
authored
Merge pull request #1155 from SixLabors/bp/saveAsTga
Add image extension method for save as tga
2 parents 6682529 + e80a6e3 commit f2371f2

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.IO;
5+
6+
using SixLabors.ImageSharp.Advanced;
7+
using SixLabors.ImageSharp.Formats.Tga;
8+
9+
namespace SixLabors.ImageSharp
10+
{
11+
/// <summary>
12+
/// Extension methods for the <see cref="Image"/> type.
13+
/// </summary>
14+
public static partial class ImageExtensions
15+
{
16+
/// <summary>
17+
/// Saves the image to the given stream with the tga format.
18+
/// </summary>
19+
/// <param name="source">The image this method extends.</param>
20+
/// <param name="stream">The stream to save the image to.</param>
21+
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
22+
public static void SaveAsTga(this Image source, Stream stream) => SaveAsTga(source, stream, null);
23+
24+
/// <summary>
25+
/// Saves the image to the given stream with the tga format.
26+
/// </summary>
27+
/// <param name="source">The image this method extends.</param>
28+
/// <param name="stream">The stream to save the image to.</param>
29+
/// <param name="encoder">The options for the encoder.</param>
30+
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
31+
public static void SaveAsTga(this Image source, Stream stream, TgaEncoder encoder) =>
32+
source.Save(
33+
stream,
34+
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance));
35+
}
36+
}

tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
using Xunit;
1717

18-
namespace SixLabors.ImageSharp.Tests
18+
namespace SixLabors.ImageSharp.Tests.Formats
1919
{
2020
public class GeneralFormatTests : FileTestBase
2121
{
@@ -41,7 +41,7 @@ public void ImageCanEncodeToString()
4141
{
4242
using (Image<Rgba32> image = file.CreateRgba32Image())
4343
{
44-
string filename = path + "/" + file.FileNameWithoutExtension + ".txt";
44+
string filename = Path.Combine(path, $"{file.FileNameWithoutExtension}.txt");
4545
File.WriteAllText(filename, image.ToBase64String(PngFormat.Instance));
4646
}
4747
}
@@ -56,7 +56,7 @@ public void DecodeThenEncodeImageFromStreamShouldSucceed()
5656
{
5757
using (Image<Rgba32> image = file.CreateRgba32Image())
5858
{
59-
image.Save($"{path}/{file.FileName}");
59+
image.Save(Path.Combine(path, file.FileName));
6060
}
6161
}
6262
}
@@ -103,25 +103,30 @@ public void ImageCanConvertFormat()
103103
{
104104
using (Image<Rgba32> image = file.CreateRgba32Image())
105105
{
106-
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp"))
106+
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.bmp")))
107107
{
108108
image.SaveAsBmp(output);
109109
}
110110

111-
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.jpg"))
111+
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.jpg")))
112112
{
113113
image.SaveAsJpeg(output);
114114
}
115115

116-
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.png"))
116+
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.png")))
117117
{
118118
image.SaveAsPng(output);
119119
}
120120

121-
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.gif"))
121+
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.gif")))
122122
{
123123
image.SaveAsGif(output);
124124
}
125+
126+
using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.tga")))
127+
{
128+
image.SaveAsTga(output);
129+
}
125130
}
126131
}
127132
}

tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
using SixLabors.ImageSharp.Formats.Gif;
1111
using SixLabors.ImageSharp.Formats.Jpeg;
1212
using SixLabors.ImageSharp.Formats.Png;
13+
using SixLabors.ImageSharp.Formats.Tga;
1314
using SixLabors.ImageSharp.PixelFormats;
1415
using Xunit;
1516

16-
namespace SixLabors.ImageSharp.Tests
17+
namespace SixLabors.ImageSharp.Tests.Formats
1718
{
1819
public class ImageFormatManagerTests
1920
{
@@ -34,11 +35,13 @@ public void IfAutoLoadWellKnownFormatsIsTrueAllFormatsAreLoaded()
3435
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<BmpEncoder>().Count());
3536
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<JpegEncoder>().Count());
3637
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<GifEncoder>().Count());
38+
Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType<TgaEncoder>().Count());
3739

3840
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<PngDecoder>().Count());
3941
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
4042
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<JpegDecoder>().Count());
4143
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<BmpDecoder>().Count());
44+
Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType<TgaDecoder>().Count());
4245
}
4346

4447
[Fact]

0 commit comments

Comments
 (0)