38 lines
3.9 KiB
C#
38 lines
3.9 KiB
C#
using AstroFrame.Engine;
|
|
|
|
static class PreviewRegression
|
|
{
|
|
public static void Run(Action<bool,string> check)
|
|
{
|
|
const int w=360,h=640;var random=new Random(839);float[] pixels=new float[w*h];
|
|
for(int y=0;y<h;y++)for(int x=0;x<w;x++)pixels[y*w+x]=(float)(1000+40*Math.Exp(-Math.Pow((x-190)/90.0,2)-Math.Pow((y-300)/200.0,2))+(random.NextDouble()-.5)*100);
|
|
pixels[290*w+172]=5000;var image=new ImagePlane(w,h,pixels,1);float[] untouched=(float[])pixels.Clone();
|
|
var reduced=PreviewRenderer.Reduce(image,3);var frame=PreviewRenderer.Render(reduced);
|
|
List<double> diff=[];for(int y=0;y<reduced.Height;y++)for(int x=0;x<reduced.Width-1;x++)diff.Add(Math.Abs(frame[y*reduced.Width+x]-frame[y*reduced.Width+x+1])/255.0);
|
|
check(Robust.Median(diff)<.06&&Robust.Median(frame.Select(v=>v/255.0))<.25,"preview transfer keeps background grain subordinate to the image");
|
|
check(untouched.SequenceEqual(pixels),"preview reduction and stretching never alter analysis pixels");
|
|
check(frame.Max()>frame[0]+80,"frame preview preserves conspicuous stars");
|
|
const int side=96;random=new Random(7461);float[] sky=new float[side*side];for(int i=0;i<sky.Length;i++)sky[i]=(float)(1000+(random.NextDouble()-.5)*60);
|
|
float[] original=(float[])sky.Clone();var standard=PreviewRenderer.Prepare(new(side,side,sky));var denoised=DenoisedPreview.Prepare(standard);
|
|
double Spread(byte[] values){double mean=values.Average(v=>(double)v);return Math.Sqrt(values.Average(v=>Math.Pow(v-mean,2)));}
|
|
check(Spread(PreviewRenderer.Render(denoised))<Spread(PreviewRenderer.Render(standard))*.5,"denoised view reduces displayed empty-sky noise instead of amplifying residual blotches");
|
|
check(denoised.Black==standard.Black&&denoised.White==standard.White&&denoised.Mid==standard.Mid,"denoising retains the original screen-transfer parameters");
|
|
check(sky.SequenceEqual(original)&&denoised.Plane.Width==side&&denoised.Plane.Height==side,"denoising preserves input pixels and preview geometry");
|
|
var dim=PreviewRenderer.Render(denoised,.10);var bright=PreviewRenderer.Render(denoised,.30);
|
|
check(dim.Zip(bright).All(pair=>pair.Second>=pair.First),"denoised brightness adjustments remain monotonic");
|
|
sky[48*side+48]=3000;for(int y=0;y<side;y++)for(int x=0;x<side;x++)sky[y*side+x]+=(float)(20*Math.Exp(-Math.Pow((x-30)/10.0,2)-Math.Pow((y-30)/14.0,2)));
|
|
var detail=DenoisedPreview.Denoise(new(side,side,sky));
|
|
check(detail.Pixels[48*side+48]>2900&&detail.Pixels[30*side+30]>1010,"patch denoising preserves bright stars and diffuse signal");
|
|
var flat=Enumerable.Repeat(1000f,1024).ToArray();check(DenoisedPreview.Denoise(new(32,32,flat)).Pixels.SequenceEqual(flat),"denoising a constant field invents no structure");
|
|
check(DenoisedPreview.Denoise(new(1,3,[1000,1001,999])).Pixels.All(float.IsFinite),"small preview dimensions use safe reflected borders");
|
|
using(var cancel=new CancellationTokenSource()){cancel.Cancel();bool cancelled=false;try{DenoisedPreview.Denoise(new(side,side,sky),cancel.Token);}catch(OperationCanceledException){cancelled=true;}check(cancelled,"denoised preview preparation respects cancellation");}
|
|
string path=Path.Combine(Path.GetTempPath(),"AstroFrame-preview-"+Guid.NewGuid()+".bin");var f=new Frame{Length=7654,Modified=4321};
|
|
try
|
|
{
|
|
PreviewRenderer.Write(image,path,f);var cached=PreviewRenderer.Read(path,f);
|
|
check(cached.Pixels.SequenceEqual(PreviewRenderer.Reduce(image,1).Pixels),"linear display cache reopens without lost samples or another FITS read");
|
|
bool refused=false;try{PreviewRenderer.Read(path,new Frame{Length=7655,Modified=4321});}catch(InvalidDataException){refused=true;}check(refused,"changed sources invalidate display caches independently of quality measurements");
|
|
}
|
|
finally{if(File.Exists(path))File.Delete(path);}
|
|
}
|
|
}
|