/// <summary> /// Reads the tga file header from the stream. /// </summary> /// <param name="stream">The <see cref="Stream"/> containing image data.</param> /// <returns>The image origin.</returns> private TgaImageOrigin ReadFileHeader(Stream stream) { this.currentStream = stream; Span <byte> buffer = stackalloc byte[TgaFileHeader.Size]; this.currentStream.Read(buffer, 0, TgaFileHeader.Size); this.fileHeader = TgaFileHeader.Parse(buffer); this.metadata = new ImageMetadata(); this.tgaMetadata = this.metadata.GetTgaMetadata(); this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth; var alphaBits = this.fileHeader.ImageDescriptor & 0xf; if (alphaBits != 0 && alphaBits != 1 && alphaBits != 8) { TgaThrowHelper.ThrowImageFormatException("Invalid alpha channel bits"); } this.tgaMetadata.AlphaChannelBits = (byte)alphaBits; this.hasAlpha = alphaBits > 0; // Bits 4 and 5 describe the image origin. var origin = (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4); return(origin); }
/// <summary> /// Reads the tga file header from the stream. /// </summary> /// <param name="stream">The <see cref="Stream"/> containing image data.</param> /// <returns>true, if the image origin is top left.</returns> private bool ReadFileHeader(Stream stream) { this.currentStream = stream; Span <byte> buffer = stackalloc byte[TgaFileHeader.Size]; this.currentStream.Read(buffer, 0, TgaFileHeader.Size); this.fileHeader = TgaFileHeader.Parse(buffer); this.metadata = new ImageMetadata(); this.tgaMetadata = this.metadata.GetTgaMetadata(); this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth; var alphaBits = this.fileHeader.ImageDescriptor & 0xf; if (alphaBits != 0 && alphaBits != 1 && alphaBits != 8) { TgaThrowHelper.ThrowImageFormatException("Invalid alpha channel bits"); } this.tgaMetadata.AlphaChannelBits = (byte)alphaBits; this.hasAlpha = alphaBits > 0; // TODO: bits 4 and 5 describe the image origin. See spec page 9. bit 4 is currently ignored. // Theoretically the origin could also be top right and bottom right. // Bit at position 5 of the descriptor indicates, that the origin is top left instead of bottom left. if ((this.fileHeader.ImageDescriptor & (1 << 5)) != 0) { return(true); } return(false); }
/// <summary> /// Reads the tga file header from the stream. /// </summary> /// <param name="stream">The <see cref="Stream"/> containing image data.</param> /// <returns>true, if the image origin is top left.</returns> private bool ReadFileHeader(Stream stream) { this.currentStream = stream; Span <byte> buffer = stackalloc byte[TgaFileHeader.Size]; this.currentStream.Read(buffer, 0, TgaFileHeader.Size); this.fileHeader = TgaFileHeader.Parse(buffer); this.metadata = new ImageMetadata(); this.tgaMetadata = this.metadata.GetTgaMetadata(); this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth; // 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); } return(false); }