/// <summary> /// Creates an empty <see cref="TgaImage"/> with the given specification. /// </summary> /// <param name="width">The width in pixels.</param> /// <param name="height">The height in pixels.</param> /// <param name="format">The <see cref="TgaFormat"/> to create the image in.</param> public TgaImage(int width, int height, TgaFormat format) { if (width < 0 || width > ushort.MaxValue) { throw new ArgumentOutOfRangeException(nameof(width)); } if (height < 0 || height > ushort.MaxValue) { throw new ArgumentOutOfRangeException(nameof(height)); } header = new TgaHeader { ImageType = 2, ColorMapSpec = new TgaColorMapSpec { }, ImageSpec = new TgaImageSpec { ImageWidth = (ushort)width, ImageHeight = (ushort)height, BitsPerPixel = 16, AlphaWidthAndDirection = (byte)(format == TgaFormat.Argb4 ? 36 : 32), } }; data = new TgaImageData { ColorMapData = new byte[0], ImageId = new byte[0], PixelData = new byte[width * height * 2], }; }
public static TgaImageData Deserialize(TgaHeader header, BinaryReader reader) { var colorMapByteLen = header.ColorMapSpec.ColorMapLength * header.ColorMapSpec.ColorMapEntrySize; var imageByteLen = (int)Math.Ceiling(header.ImageSpec.ImageWidth * header.ImageSpec.ImageHeight * header.ImageSpec.BitsPerPixel / 8.0); return(new TgaImageData { ImageId = reader.ReadBytes(header.IdLength), ColorMapData = reader.ReadBytes(colorMapByteLen), PixelData = reader.ReadBytes(imageByteLen), }); }
/// <summary> /// Parses a TGA format image from bytes. /// </summary> /// <param name="reader">The binary reader to read the bytes from.</param> /// <returns>The parsed <see cref="TgaImage">.</returns> public static TgaImage FromBytes(BinaryReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var header = TgaHeader.Deserialize(reader); var img = TgaImageData.Deserialize(header, reader); return(new TgaImage(header, img)); }
private TgaImage(TgaHeader header, TgaImageData data) { this.header = header; this.data = data; }