示例#1
0
        public static int GetSquareTextureSize(FileSystemEntry entry)
        {
            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var header = TgaHeader.Parse(reader);

                    if (header.Width != header.Height)
                    {
                        throw new InvalidOperationException();
                    }

                    return(header.Width);
                }
        }
示例#2
0
        private static byte[] ReadRunLengthEncodedRgbData(BinaryReader reader, TgaHeader header, int bytesPerPixel)
        {
            var data      = new byte[header.Width * header.Height * bytesPerPixel];
            var dataIndex = 0;

            while (dataIndex < data.Length)
            {
                var packet = reader.ReadByte();

                var packetType = (RlePacketType)(packet >> 7);
                var value      = (packet & 0b1111111) + 1;

                switch (packetType)
                {
                case RlePacketType.RawPacket:
                {
                    var bytes = reader.ReadBytes(bytesPerPixel * value);
                    Array.Copy(bytes, 0, data, dataIndex, bytes.Length);
                    dataIndex += bytes.Length;
                    break;
                }

                case RlePacketType.RunLengthEncodedPacket:
                {
                    var bytes = reader.ReadBytes(bytesPerPixel);
                    for (var i = 0; i < value; i++)
                    {
                        Array.Copy(bytes, 0, data, dataIndex, bytes.Length);
                        dataIndex += bytes.Length;
                    }
                    break;
                }

                default:
                    throw new InvalidOperationException();
                }
            }

            return(data);
        }
示例#3
0
        public static TgaFile FromFileSystemEntry(FileSystemEntry entry)
        {
            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var header = TgaHeader.Parse(reader);

                    if (header.HasColorMap || header.ImageType != TgaImageType.UncompressedRgb)
                    {
                        throw new NotSupportedException();
                    }

                    var bytesPerPixel = header.ImagePixelSize / 8;
                    var stride        = header.Width * bytesPerPixel;

                    var data = reader.ReadBytes(header.Width * header.Height * bytesPerPixel);

                    // Invert y, because data is stored bottom-up in TGA.
                    var invertedData = new byte[data.Length];
                    var invertedY    = 0;
                    for (var y = header.Height - 1; y >= 0; y--)
                    {
                        Array.Copy(
                            data,
                            y * stride,
                            invertedData,
                            invertedY * stride,
                            stride);

                        invertedY++;
                    }

                    return(new TgaFile
                    {
                        Header = header,
                        Data = invertedData
                    });
                }
        }
示例#4
0
        public static TgaFile FromFileSystemEntry(FileSystemEntry entry)
        {
            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var header = TgaHeader.Parse(reader);

                    if (header.HasColorMap)
                    {
                        throw new NotSupportedException("TGA files with color maps are not supported.");
                    }

                    switch (header.ImageType)
                    {
                    case TgaImageType.UncompressedRgb:
                    case TgaImageType.UncompressedBlackAndWhite:
                    case TgaImageType.RunLengthEncodedRgb:
                        break;

                    default:
                        throw new NotSupportedException($"TGA file is of an unsupported type: {header.ImageType}.");
                    }

                    var bytesPerPixel = header.ImagePixelSize / 8;

                    byte[] data;
                    switch (header.ImageType)
                    {
                    case TgaImageType.UncompressedRgb:
                    case TgaImageType.UncompressedBlackAndWhite:
                        data = reader.ReadBytes(header.Width * header.Height * bytesPerPixel);
                        break;

                    case TgaImageType.RunLengthEncodedRgb:
                        data = ReadRunLengthEncodedRgbData(reader, header, bytesPerPixel);
                        break;

                    default:
                        throw new InvalidOperationException();
                    }

                    var stride = header.Width * bytesPerPixel;

                    // Invert y, because data is stored bottom-up in TGA.
                    var invertedData = new byte[data.Length];
                    var invertedY    = 0;
                    for (var y = header.Height - 1; y >= 0; y--)
                    {
                        Array.Copy(
                            data,
                            y * stride,
                            invertedData,
                            invertedY * stride,
                            stride);

                        invertedY++;
                    }

                    return(new TgaFile
                    {
                        Header = header,
                        Data = invertedData
                    });
                }
        }