/// <summary> /// Converts an Enumerable of colors into a byte[]. /// Palette will be written before the image data /// </summary> /// <param name="colors"></param> /// <returns></returns> public byte[] Save(IEnumerable <Color> colors) { var redColors = CreatePalette(colors.ToList()); paletteBytes = (paletteFormat == null) ? null : paletteFormat.Save(redColors); var alphaShift = indexSize; var ms = new MemoryStream(); using (var bw = new BinaryWriterX(ms, true, byteOrder)) { foreach (var color in colors) { switch (BitDepth) { case 8: byte b = (byte)(Support.Support.ChangeBitDepth(color.A, 8, alpha) << alphaShift); bw.Write((byte)(b | redColors.FindIndex(c => c == color))); break; default: throw new Exception($"BitDepth {BitDepth} not supported!"); } } } return(ms.ToArray()); }
public (byte[] indexData, byte[] paletteData) Save(IEnumerable <IndexData> indeces, IList <Color> palette) { var ms = new MemoryStream(); using (var bw = new BinaryWriterX(ms, true, _byteOrder)) { foreach (var indexData in indeces) { switch (_indexDepth) { case 4: bw.WriteNibble(indexData.Index); break; case 8: bw.Write((byte)indexData.Index); break; default: throw new Exception($"IndexDepth {_indexDepth} not supported!"); } } } return(ms.ToArray(), _paletteFormat.Save(palette)); }