public static FormatTexture CreateTgaTexture(
            ReadOnlyMemory <byte> tgaData,
            LoaderFlags loaderFlags = LoaderFlags.None
            )
        {
            if (tgaData.Length < sizeof(TGAHeader))
            {
                ThrowHelper.ThrowInvalidDataException("Too small");
            }

            ReadOnlySpan <byte> span = tgaData.Span;

            TGAHeader header = MemoryMarshal.Read <TGAHeader>(span);

            if (IsCompressed(header.DataTypeCode))
            {
                ThrowHelper.ThrowNotSupportedException("Compressed TGA textures are TODO"); // TODO
            }

            int size = header.Height * header.Width * (header.BitsPerPixel / 8);

            var data = new byte[size];
            var buff = data;

            DXGI_FORMAT format = loaderFlags.HasFlag(LoaderFlags.ForceSrgb)
                ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
                : DXGI_FORMAT_R8G8B8A8_UNORM;

            switch (header.BitsPerPixel)
            {
            case 24:
                RbgToRgba(span.Slice(sizeof(TGAHeader)), buff);
                break;

            case 32:
                ArbgToRgba(span.Slice(sizeof(TGAHeader)), buff);
                break;

            case 16:
                Rgba16ToRgba(span.Slice(sizeof(TGAHeader)), buff);
                break;

            default:
                ThrowHelper.ThrowNotSupportedException("Unsupported format");
                break;
            }

            var subresources = new SubresourceData[1];

            subresources[0] = new SubresourceData();

            var desc = new TextureDesc
            {
                Width            = (uint)header.Width,
                Height           = (uint)header.Height,
                DepthOrArraySize = 0, // is this right?
                Format           = (DataFormat)format,
                Dimension        = TextureDimension.Tex2D
            };

            return(new FormatTexture(
                       data,
                       desc,
                       1,
                       loaderFlags,
                       false,
                       subresources,
                       header.BitsPerPixel == 24 ? AlphaMode.Opaque : AlphaMode.Unknown,
                       TexType.Tga
                       ));
        }
Example #2
0
        public static TextureDescription CreateTgaTexture(Memory <byte> tgaData,
                                                          LoaderFlags loaderFlags = LoaderFlags.None)
        {
            if (tgaData.Length < sizeof(TGAHeader))
            {
                ThrowHelper.ThrowInvalidDataException("Too small");
            }

            Span <byte> span = tgaData.Span;

            TGAHeader header = MemoryMarshal.Read <TGAHeader>(span);

            if (IsCompressed(header.DataTypeCode))
            {
                ThrowHelper.ThrowNotSupportedException("Compressed TGA textures are TODO"); // TODO
            }

            int size = header.Height * header.Width * (header.BitsPerPixel / 8);

            var data = new byte[size];
            var buff = data;

            DXGI_FORMAT format = loaderFlags.HasFlag(LoaderFlags.ForceSrgb)
                ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
                : DXGI_FORMAT_R8G8B8A8_UNORM;

            switch (header.BitsPerPixel)
            {
            case 24:
                RbgToRgba(span.Slice(sizeof(TGAHeader)), buff);
                break;

            case 32:
                ArbgToRgba(span.Slice(sizeof(TGAHeader)), buff);
                break;

            case 16:
                Rgba16ToRgba(span.Slice(sizeof(TGAHeader)), buff);
                break;

            default:
                ThrowHelper.ThrowNotSupportedException("Unsupported format");
                break;
            }

            var subresources = new ManagedSubresourceData[1];

            subresources[0] = new ManagedSubresourceData();

            return(new TextureDescription(
                       data,
                       D3D12_RESOURCE_DIMENSION.D3D12_RESOURCE_DIMENSION_TEXTURE2D,
                       new Size3((uint)header.Height, (uint)header.Width, 0),
                       1,
                       1,
                       format,
                       loaderFlags,
                       false,
                       subresources,
                       header.BitsPerPixel == 24 ? AlphaMode.Opaque : AlphaMode.Unknown,
                       TexType.Tga
                       ));
        }