using System.IO.Compression;
using System.Runtime.InteropServices;
namespace AstroFrame.Engine;
public sealed record PreviewPlane(int Width,int Height,float[] Pixels);
public sealed record PreviewDisplay(PreviewPlane Plane,double Black,double White,double Mid);
/// Display-only area reduction and screen transfer. Never used by a quality detector.
public static class PreviewRenderer
{
const int Magic=0x41504633;
public static PreviewPlane Reduce(ImagePlane image,int step)
{
int w=(image.Width+step-1)/step,h=(image.Height+step-1)/step;float[] pixels=new float[w*h];
for(int y=0;yRender(Prepare(image,nebula),target);
public static PreviewDisplay Prepare(PreviewPlane image,bool nebula=false)
{
var plane=nebula?Reduce(new(image.Width,image.Height,image.Pixels,1),2):image;
var values=plane.Pixels.Select(v=>(double)v).ToArray();double median=Robust.Median(values);
List differences=[];for(int y=0;y2_000_000)throw new InvalidDataException("Preview dimensions exceed safety budget");
float[] pixels=new float[w*h];using var compressed=new DeflateStream(stream,CompressionMode.Decompress);using var reader=new BinaryReader(compressed);
if(BitConverter.IsLittleEndian)compressed.ReadExactly(MemoryMarshal.AsBytes(pixels.AsSpan()));else for(int i=0;i!float.IsFinite(v)))throw new InvalidDataException("Nonfinite preview");
return new(w,h,pixels);
}
}