Browse Source

Fix encoding of 16 tga files

af/merge-core
Brian Popow 7 years ago
parent
commit
c4413d5a55
  1. 9
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  2. 16
      src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

9
src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

@ -141,7 +141,6 @@ namespace SixLabors.ImageSharp.Formats.Tga
case 16:
if (this.fileHeader.ImageType.IsRunLengthEncoded())
{
long currentPosition = this.currentStream.Position;
this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 2, inverted);
}
else
@ -218,7 +217,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
{
int colorIndex = rowSpan[x];
// Set bit 16 to 1, to treat it as opaque for Bgra5551.
// Set alpha value to 1, to treat it as opaque for Bgra5551.
Bgra5551 bgra = Unsafe.As<byte, Bgra5551>(ref palette[colorIndex * colorMapPixelSizeInBytes]);
bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
color.FromBgra5551(bgra);
@ -285,7 +284,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
color.FromGray8(Unsafe.As<byte, Gray8>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
break;
case 2:
// Set bit 16 to 1, to treat it as opaque for Bgra5551.
// Set alpha value to 1, to treat it as opaque for Bgra5551.
Bgra5551 bgra = Unsafe.As<byte, Bgra5551>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]);
bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
color.FromBgra5551(bgra);
@ -449,7 +448,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
color.FromGray8(Unsafe.As<byte, Gray8>(ref bufferSpan[idx]));
break;
case 2:
// Set bit 16 to 1, to treat it as opaque for Bgra5551.
// Set alpha value to 1, to treat it as opaque for Bgra5551.
bufferSpan[idx + 1] = (byte)(bufferSpan[idx + 1] | 128);
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref bufferSpan[idx]));
break;
@ -590,7 +589,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
this.tgaMetadata = this.metadata.GetFormatMetadata(TgaFormat.Instance);
this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth;
// Bit at position 3 of the descriptor indicates, that the origin is top left instead of bottom right.
// Bit at position 5 of the descriptor indicates, that the origin is top left instead of bottom right.
if ((this.fileHeader.ImageDescriptor & (1 << 5)) != 0)
{
return true;

16
src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers.Binary;
using System.IO;
using System.Numerics;
using System.Runtime.CompilerServices;
@ -29,6 +30,11 @@ namespace SixLabors.ImageSharp.Formats.Tga
/// </summary>
private Configuration configuration;
/// <summary>
/// Reusable buffer for writing data.
/// </summary>
private readonly byte[] buffer = new byte[2];
/// <summary>
/// The color depth, in number of bits per pixel.
/// </summary>
@ -74,7 +80,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
imageType = this.useCompression ? TgaImageType.RleBlackAndWhite : TgaImageType.BlackAndWhite;
}
// If compression is used, set byte 3 of the image descriptor to indicate an left top origin.
// If compression is used, set bit 5 of the image descriptor to indicate an left top origin.
byte imageDescriptor = (byte)(this.useCompression ? 32 : 0);
var fileHeader = new TgaFileHeader(
@ -89,7 +95,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
width: (short)image.Width,
height: (short)image.Height,
pixelDepth: (byte)this.bitsPerPixel.Value,
imageDescriptor: (byte)(this.useCompression ? 32 : 0));
imageDescriptor: imageDescriptor);
#if NETCOREAPP2_1
Span<byte> buffer = stackalloc byte[TgaFileHeader.Size];
@ -174,10 +180,10 @@ namespace SixLabors.ImageSharp.Formats.Tga
break;
case TgaBitsPerPixel.Pixel16:
// TODO: this seems to be wrong
var bgra5551 = new Bgra5551(color.ToVector4());
stream.WriteByte((byte)(bgra5551.PackedValue & 0xFF));
stream.WriteByte((byte)(bgra5551.PackedValue & 0xFF00));
BinaryPrimitives.TryWriteInt16LittleEndian(this.buffer, (short)bgra5551.PackedValue);
stream.WriteByte(this.buffer[0]);
stream.WriteByte(this.buffer[1]);
break;

Loading…
Cancel
Save