Esempio n. 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);
                }
        }
Esempio n. 2
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
                    });
                }
        }
Esempio n. 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)
                    {
                        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
                    });
                }
        }