65 lines
4.3 KiB
C#
65 lines
4.3 KiB
C#
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);
|
|
/// <summary>Display-only area reduction and screen transfer. Never used by a quality detector.</summary>
|
|
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;y<h;y++)for(int x=0;x<w;x++)
|
|
{
|
|
double sum=0;int count=0;
|
|
for(int dy=y*step;dy<Math.Min((y+1)*step,image.Height);dy++)for(int dx=x*step;dx<Math.Min((x+1)*step,image.Width);dx++){sum+=image.Pixels[dy*image.Width+dx];count++;}
|
|
pixels[y*w+x]=(float)(sum/Math.Max(count,1));
|
|
}
|
|
return new(w,h,pixels);
|
|
}
|
|
public static byte[] Render(PreviewPlane image,double target=.18,bool nebula=false)
|
|
=>Render(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<double> differences=[];for(int y=0;y<plane.Height;y++)for(int x=0;x<plane.Width-1;x++)differences.Add(Math.Abs(plane.Pixels[y*plane.Width+x]-plane.Pixels[y*plane.Width+x+1]));
|
|
double noise=Math.Max(.001,Robust.Median(differences)/.953872),black=median-2.8*noise,white=Math.Max(Robust.Quantile(values,.9995),median+30*noise),span=white-black;
|
|
return new(plane,black,white,(median-black)/span);
|
|
}
|
|
public static byte[] Render(PreviewDisplay display,double target=.18)
|
|
{
|
|
double mid=display.Mid,t=Math.Clamp(target,.06,.45),m=mid*(t-1)/((2*t-1)*mid-t),span=display.White-display.Black;byte[] output=new byte[display.Plane.Pixels.Length];
|
|
for(int i=0;i<output.Length;i++){double v=Math.Clamp((display.Plane.Pixels[i]-display.Black)/span,0,1);output[i]=(byte)Math.Clamp(255*(m-1)*v/((2*m-1)*v-m),0,255);}
|
|
return output;
|
|
}
|
|
public static void Write(ImagePlane image,string path,Frame? frame=null)
|
|
{
|
|
var plane=Reduce(image,Math.Max(1,(image.Height+899)/900));string tmp=path+".tmp";Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))!);
|
|
try
|
|
{
|
|
using(var stream=File.Create(tmp))
|
|
{
|
|
using(var writer=new BinaryWriter(stream,System.Text.Encoding.UTF8,true)){writer.Write(Magic);writer.Write(frame?.Length??0);writer.Write(frame?.Modified??0);writer.Write(plane.Width);writer.Write(plane.Height);}
|
|
using var compressed=new DeflateStream(stream,CompressionLevel.Fastest,true);
|
|
if(BitConverter.IsLittleEndian)compressed.Write(MemoryMarshal.AsBytes(plane.Pixels.AsSpan()));
|
|
else{using var data=new BinaryWriter(compressed);foreach(float v in plane.Pixels)data.Write(v);}
|
|
}
|
|
File.Move(tmp,path,true);
|
|
}
|
|
finally{if(File.Exists(tmp))File.Delete(tmp);}
|
|
}
|
|
public static PreviewPlane Read(string path,Frame? frame=null)
|
|
{
|
|
using var stream=File.OpenRead(path);using var header=new BinaryReader(stream,System.Text.Encoding.UTF8,true);
|
|
if(header.ReadInt32()!=Magic)throw new InvalidDataException("Outdated preview cache");
|
|
long length=header.ReadInt64(),modified=header.ReadInt64();if(frame!=null&&(length!=frame.Length||modified!=frame.Modified))throw new InvalidDataException("Preview source fingerprint changed");
|
|
int w=header.ReadInt32(),h=header.ReadInt32();if(w<=0||h<=0||(long)w*h>2_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<pixels.Length;i++)pixels[i]=reader.ReadSingle();
|
|
if(pixels.Any(v=>!float.IsFinite(v)))throw new InvalidDataException("Nonfinite preview");
|
|
return new(w,h,pixels);
|
|
}
|
|
}
|