diff --git a/src/ImageSharp/Formats/README.md b/src/ImageSharp/Formats/README.md new file mode 100644 index 0000000000..4a2b401b1d --- /dev/null +++ b/src/ImageSharp/Formats/README.md @@ -0,0 +1,6 @@ +# Encoder/Decoder for true vision targa files + +Useful links for reference: + +- [FileFront](https://www.fileformat.info/format/tga/egff.htm) +- [Tga Specification](http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 7b7f803ca1..8f36a7626b 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -50,6 +50,11 @@ namespace SixLabors.ImageSharp.Formats.Tga /// private readonly ITgaDecoderOptions options; + /// + /// Initializes a new instance of the class. + /// + /// The configuration. + /// The options. public TgaDecoderCore(Configuration configuration, ITgaDecoderOptions options) { this.configuration = configuration; @@ -183,6 +188,16 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Reads a uncompressed TGA image with a palette. + /// + /// The pixel type. + /// The width of the image. + /// The height of the image. + /// The to assign the palette to. + /// The color palette. + /// Color map size of one entry in bytes. + /// Indicates, if the origin of the image is top left rather the bottom left (the default). private void ReadPaletted(int width, int height, Buffer2D pixels, byte[] palette, int colorMapPixelSizeInBytes, bool inverted) where TPixel : struct, IPixel { @@ -236,6 +251,16 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Reads a run length encoded TGA image with a palette. + /// + /// The pixel type. + /// The width of the image. + /// The height of the image. + /// The to assign the palette to. + /// The color palette. + /// Color map size of one entry in bytes. + /// Indicates, if the origin of the image is top left rather the bottom left (the default). private void ReadPalettedRle(int width, int height, Buffer2D pixels, byte[] palette, int colorMapPixelSizeInBytes, bool inverted) where TPixel : struct, IPixel { @@ -279,6 +304,14 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Reads a uncompressed monochrome TGA image. + /// + /// The pixel type. + /// The width of the image. + /// The height of the image. + /// The to assign the palette to. + /// Indicates, if the origin of the image is top left rather the bottom left (the default). private void ReadMonoChrome(int width, int height, Buffer2D pixels, bool inverted) where TPixel : struct, IPixel { @@ -298,6 +331,14 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Reads a uncompressed TGA image where each pixels has 16 bit. + /// + /// The pixel type. + /// The width of the image. + /// The height of the image. + /// The to assign the palette to. + /// Indicates, if the origin of the image is top left rather the bottom left (the default). private void ReadBgra16(int width, int height, Buffer2D pixels, bool inverted) where TPixel : struct, IPixel { @@ -323,6 +364,14 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Reads a uncompressed TGA image where each pixels has 24 bit. + /// + /// The pixel type. + /// The width of the image. + /// The height of the image. + /// The to assign the palette to. + /// Indicates, if the origin of the image is top left rather the bottom left (the default). private void ReadBgr24(int width, int height, Buffer2D pixels, bool inverted) where TPixel : struct, IPixel { @@ -342,6 +391,14 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Reads a uncompressed TGA image where each pixels has 32 bit. + /// + /// The pixel type. + /// The width of the image. + /// The height of the image. + /// The to assign the palette to. + /// Indicates, if the origin of the image is top left rather the bottom left (the default). private void ReadBgra32(int width, int height, Buffer2D pixels, bool inverted) where TPixel : struct, IPixel { @@ -361,6 +418,15 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Reads a run length encoded TGA image. + /// + /// The pixel type. + /// The width of the image. + /// The height of the image. + /// The to assign the palette to. + /// The bytes per pixel. + /// Indicates, if the origin of the image is top left rather the bottom left (the default). private void ReadRle(int width, int height, Buffer2D pixels, int bytesPerPixel, bool inverted) where TPixel : struct, IPixel { @@ -415,6 +481,13 @@ namespace SixLabors.ImageSharp.Formats.Tga this.metadata); } + /// + /// Produce uncompressed tga data from a run length encoded stream. + /// + /// The width of the image. + /// The height of the image. + /// Buffer for uncompressed data. + /// The bytes used per pixel. private void UncompressRle(int width, int height, Span buffer, int bytesPerPixel) { int uncompressedPixels = 0; @@ -452,6 +525,16 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Helper method for decoding BGRA5551 images. Makes the pixels opaque, because the high bit does not + /// represent an alpha channel. + /// TODO: maybe there is a better/faster way to achieve this. + /// + /// The pixel type. + /// The destination pixel buffer. + /// The start position of pixel data. + /// A byte array to store the read pixel data. + /// Bgra pixel row span. private void MakeOpaque(Buffer2D pixels, long currentPosition, IManagedByteBuffer row, Span bgraRowSpan) where TPixel : struct, IPixel { @@ -477,6 +560,13 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Returns the y- value based on the given height. + /// + /// The y- value representing the current row. + /// The height of the bitmap. + /// Whether the bitmap is inverted. + /// The representing the inverted value. [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int Invert(int y, int height, bool inverted) => (!inverted) ? height - y - 1 : y; diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs index 291c61a814..1bde05c937 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -51,6 +51,12 @@ namespace SixLabors.ImageSharp.Formats.Tga this.useCompression = options.Compress; } + /// + /// Encodes the image to the specified stream from the . + /// + /// The pixel format. + /// The to encode from. + /// The to encode the image data to. public void Encode(Image image, Stream stream) where TPixel : struct, IPixel { @@ -135,6 +141,12 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Writes a run length encoded tga image to the stream. + /// + /// The pixel type. + /// The stream to write the image to. + /// The image to encode. private void WriteRunLengthEndcodedImage(Stream stream, ImageFrame image) where TPixel : struct, IPixel { @@ -184,6 +196,12 @@ namespace SixLabors.ImageSharp.Formats.Tga } } + /// + /// Finds consecutive pixels, which have the same value starting from the pixel span offset 0. + /// + /// The pixel type. + /// The pixel span to search in. + /// The number of equal pixels. private byte FindEqualPixels(Span pixelSpan) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Formats/Tga/TgaImageType.cs b/src/ImageSharp/Formats/Tga/TgaImageType.cs index 2c19a06954..cf0eda93c4 100644 --- a/src/ImageSharp/Formats/Tga/TgaImageType.cs +++ b/src/ImageSharp/Formats/Tga/TgaImageType.cs @@ -46,25 +46,4 @@ namespace SixLabors. /// RleBlackAndWhite = 11, } - - /// - /// Extension methods for TgaImageType enum. - /// - public static class TgaImageTypeExtensions - { - /// - /// Checks if this tga image type is run length encoded. - /// - /// The tga image type. - /// True, if this image type is run length encoded, otherwise false. - public static bool IsRunLengthEncoded(this TgaImageType imageType) - { - if (imageType is TgaImageType.RleColorMapped || imageType is TgaImageType.RleBlackAndWhite || imageType is TgaImageType.RleTrueColor) - { - return true; - } - - return false; - } - } } diff --git a/src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs b/src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs new file mode 100644 index 0000000000..406e12d08b --- /dev/null +++ b/src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs @@ -0,0 +1,26 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Formats.Tga +{ + /// + /// Extension methods for TgaImageType enum. + /// + public static class TgaImageTypeExtensions + { + /// + /// Checks if this tga image type is run length encoded. + /// + /// The tga image type. + /// True, if this image type is run length encoded, otherwise false. + public static bool IsRunLengthEncoded(this TgaImageType imageType) + { + if (imageType is TgaImageType.RleColorMapped || imageType is TgaImageType.RleBlackAndWhite || imageType is TgaImageType.RleTrueColor) + { + return true; + } + + return false; + } + } +}