public async Task <byte[]> ApplyAsync(ISffPngImageHeader imageHeader, byte[] imageData, IPalette palette) { // (Scanline filter byte + imageHeader.Width * 4 RGBA bytes) * imageHeader.Height == imageData.Length if ((1 + imageHeader.Width * 4) * imageHeader.Height == imageData.Length) { // Already in RGBA format return(imageData); } if (palette == null) { throw new ArgumentNullException($"Palette is required to decode PNG image."); } var coloredImage = new List <byte>(); for (var index = 0; index < imageData.Length; ++index) { var paletteIndex = imageData[index]; // For scanline filter type we don't want to perform colorization if (index == 0 || index % imageHeader.BytesInRow == 0) { coloredImage.Add(paletteIndex); continue; } var color = palette.ElementAt(paletteIndex); coloredImage.Add(color.Red); coloredImage.Add(color.Green); coloredImage.Add(color.Blue); coloredImage.Add(color.Alpha); } return(coloredImage.ToArray()); }