Skip to content

Commit 133d908

Browse files
Merge pull request #1026 from brianpopow/feature/tga
Add support for decoding and encoding of TGA images
2 parents 0049ca2 + d183a51 commit 133d908

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2150
-29
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
*.gif binary
9494
*.jpg binary
9595
*.png binary
96+
*.tga binary
9697
*.ttf binary
9798
*.snk binary
9899

Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<ItemGroup>
2525
<PackageReference Update="BenchmarkDotNet" Version="0.11.5" />
2626
<PackageReference Update="Colourful" Version="2.0.2" />
27-
<PackageReference Update="Magick.NET-Q16-AnyCPU" Version="7.12.0" />
27+
<PackageReference Update="Magick.NET-Q16-AnyCPU" Version="7.14.4" />
2828
<PackageReference Update="Microsoft.Net.Compilers.Toolset" Version="3.3.1" />
2929
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="15.9.0" />
3030
<PackageReference Update="Moq" Version="4.10.0" />

src/ImageSharp/Common/Helpers/ImageMaths.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Numerics;
56
using System.Runtime.CompilerServices;
67

78
using SixLabors.ImageSharp.PixelFormats;
@@ -14,6 +15,20 @@ namespace SixLabors.ImageSharp
1415
/// </summary>
1516
internal static class ImageMaths
1617
{
18+
/// <summary>
19+
/// Vector for converting pixel to gray value as specified by ITU-R Recommendation BT.709.
20+
/// </summary>
21+
private static readonly Vector4 Bt709 = new Vector4(.2126f, .7152f, .0722f, 0.0f);
22+
23+
/// <summary>
24+
/// Convert a pixel value to grayscale using ITU-R Recommendation BT.709.
25+
/// </summary>
26+
/// <param name="vector">The vector to get the luminance from.</param>
27+
/// <param name="luminanceLevels">The number of luminance levels (256 for 8 bit, 65536 for 16 bit grayscale images)</param>
28+
[MethodImpl(InliningOptions.ShortMethod)]
29+
public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels)
30+
=> (int)MathF.Round(Vector4.Dot(vector, Bt709) * (luminanceLevels - 1));
31+
1732
/// <summary>
1833
/// Gets the luminance from the rgb components using the formula as specified by ITU-R Recommendation BT.709.
1934
/// </summary>

src/ImageSharp/Configuration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using SixLabors.ImageSharp.Formats.Gif;
99
using SixLabors.ImageSharp.Formats.Jpeg;
1010
using SixLabors.ImageSharp.Formats.Png;
11+
using SixLabors.ImageSharp.Formats.Tga;
1112
using SixLabors.ImageSharp.IO;
1213
using SixLabors.ImageSharp.Processing;
1314
using SixLabors.Memory;
@@ -150,6 +151,7 @@ public Configuration Clone()
150151
/// <see cref="JpegConfigurationModule"/>
151152
/// <see cref="GifConfigurationModule"/>
152153
/// <see cref="BmpConfigurationModule"/>.
154+
/// <see cref="TgaConfigurationModule"/>.
153155
/// </summary>
154156
/// <returns>The default configuration of <see cref="Configuration"/>.</returns>
155157
internal static Configuration CreateDefaultInstance()
@@ -158,7 +160,8 @@ internal static Configuration CreateDefaultInstance()
158160
new PngConfigurationModule(),
159161
new JpegConfigurationModule(),
160162
new GifConfigurationModule(),
161-
new BmpConfigurationModule());
163+
new BmpConfigurationModule(),
164+
new TgaConfigurationModule());
162165
}
163166
}
164167
}

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,10 @@ private void ReadRle24<TPixel>(Buffer2D<TPixel> pixels, int width, int height, b
387387
if (rowHasUndefinedPixels)
388388
{
389389
// Slow path with undefined pixels.
390+
int rowStartIdx = y * width * 3;
390391
for (int x = 0; x < width; x++)
391392
{
392-
int idx = (y * width * 3) + (x * 3);
393+
int idx = rowStartIdx + (x * 3);
393394
if (undefinedPixels[x, y])
394395
{
395396
switch (this.options.RleSkippedPixelHandling)
@@ -418,9 +419,10 @@ private void ReadRle24<TPixel>(Buffer2D<TPixel> pixels, int width, int height, b
418419
else
419420
{
420421
// Fast path without any undefined pixels.
422+
int rowStartIdx = y * width * 3;
421423
for (int x = 0; x < width; x++)
422424
{
423-
int idx = (y * width * 3) + (x * 3);
425+
int idx = rowStartIdx + (x * 3);
424426
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
425427
pixelRow[x] = color;
426428
}

src/ImageSharp/Formats/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Encoder/Decoder for true vision targa files
2+
3+
Useful links for reference:
4+
5+
- [FileFront](https://www.fileformat.info/format/tga/egff.htm)
6+
- [Tga Specification](http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.Formats.Tga
5+
{
6+
/// <summary>
7+
/// The options for decoding tga images. Currently empty, but this may change in the future.
8+
/// </summary>
9+
internal interface ITgaDecoderOptions
10+
{
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.Formats.Tga
5+
{
6+
/// <summary>
7+
/// Configuration options for use during tga encoding.
8+
/// </summary>
9+
internal interface ITgaEncoderOptions
10+
{
11+
/// <summary>
12+
/// Gets the number of bits per pixel.
13+
/// </summary>
14+
TgaBitsPerPixel? BitsPerPixel { get; }
15+
16+
/// <summary>
17+
/// Gets a value indicating whether run length compression should be used.
18+
/// </summary>
19+
TgaCompression Compression { get; }
20+
}
21+
}
177 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.Formats.Tga
5+
{
6+
/// <summary>
7+
/// Enumerates the available bits per pixel the tga encoder supports.
8+
/// </summary>
9+
public enum TgaBitsPerPixel : byte
10+
{
11+
/// <summary>
12+
/// 8 bits per pixel. Each pixel consists of 1 byte.
13+
/// </summary>
14+
Pixel8 = 8,
15+
16+
/// <summary>
17+
/// 16 bits per pixel. Each pixel consists of 2 bytes.
18+
/// </summary>
19+
Pixel16 = 16,
20+
21+
/// <summary>
22+
/// 24 bits per pixel. Each pixel consists of 3 bytes.
23+
/// </summary>
24+
Pixel24 = 24,
25+
26+
/// <summary>
27+
/// 32 bits per pixel. Each pixel consists of 4 bytes.
28+
/// </summary>
29+
Pixel32 = 32
30+
}
31+
}

0 commit comments

Comments
 (0)