예제 #1
0
        public static TgaInfo Load(
            ImageBinReader reader, ReadState state, ArrayPool <byte>?bytePool = null)
        {
            var info = ParseHeader(reader, state);

            reader.Skip(info.offset);

            bytePool ??= ArrayPool <byte> .Shared;

            int lineBufferLength = (state.Width * state.Components * state.Depth + 7) / 8;

            byte[] lineBuffer = bytePool.Rent(lineBufferLength);
            try
            {
                Span <byte> line = lineBuffer.AsSpan(0, lineBufferLength);

                if (info.colormap_type == 0 && !info.is_RLE && state.Depth == 8)
                {
                    for (int y = 0; y < state.Height; ++y)
                    {
                        reader.ReadBytes(line);
                        SwapComponentOrder(line, state.Components, state.Depth);

                        int row = info.inverted != 0 ? state.Height - y - 1 : y;
                        state.OutputPixelLine(AddressingMajor.Row, row, 0, line);
                    }
                }
                else
                {
                    Memory <byte> paletteBuffer = default;
                    Span <byte>   palette       = default;

                    if (info.colormap_type != 0)
                    {
                        reader.Skip(info.palette_start);

                        paletteBuffer = new byte[info.palette_len * state.Components];
                        palette       = paletteBuffer.Span;

                        if (state.Depth == 16)
                        {
                            for (int i = 0; i < palette.Length; i += state.Components)
                            {
                                ReadRgb16(reader, palette[i..]);