/// <inheritdoc /> public void Encode <TPixel>(Image <TPixel> image, Stream stream) where TPixel : unmanaged, IPixel <TPixel> { var buf = new byte[_srcArea.Width * sizeof(short)]; var rgb565Buf = new ushort[_srcArea.Width]; var rgba32 = new Rgba32(); for (var y = 0; y < _srcArea.Height; y++) { var span = image.GetPixelRowSpan(y); for (var x = 0; x < _srcArea.Width; x++) { span[x].ToRgba32(ref rgba32); rgb565Buf[x] = Rgb565.Pack(rgba32.R, rgba32.G, rgba32.B); } stream.Seek(_framebuffer.PointToOffset(_destPoint.X, _destPoint.Y + y), SeekOrigin.Begin); Buffer.BlockCopy(rgb565Buf, 0, buf, 0, buf.Length); stream.Write(buf, 0, buf.Length); } }
/// <summary> /// Decodes the given RGB565 stream into the provided image /// </summary> /// <typeparam name="TPixel">The pixel format</typeparam> /// <param name="stream">The RGB565 stream to read <see cref="ushort" />-encoded image data from</param> /// <param name="image">The image to read data into</param> /// <returns>The image passed as the <paramref name="image" /> argument</returns> private Image <TPixel> DecodeIntoImage <TPixel>(Stream stream, Image <TPixel> image) where TPixel : unmanaged, IPixel <TPixel> { var buf = new byte[_area.Width * sizeof(short)]; var rgb565Buf = new ushort[_area.Width]; for (var y = 0; y < _area.Height; y++) { stream.Seek(_framebuffer.PointToOffset(_area.X, _area.Y + y), SeekOrigin.Begin); stream.Read(buf, 0, buf.Length); Buffer.BlockCopy(buf, 0, rgb565Buf, 0, buf.Length); var span = image.GetPixelRowSpan(y); for (var x = 0; x < _area.Width; x++) { span[x].FromRgb24(Rgb565.Unpack(rgb565Buf[x])); } } return(image); }