AstroFrame/src/AstroFrame.Desktop/StarInspector.cs
2026-10-03 18:29:15 -07:00

56 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AstroFrame.Engine;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using System.Runtime.InteropServices;
namespace AstroFrame.Desktop;
public sealed class StarInspector:Window
{
readonly List<WriteableBitmap> bitmaps=[];
readonly CancellationTokenSource cancellation=new();
public StarInspector(Project project,string directory,Frame selected,string? capture=null)
{
Title="AstroFrame · Matched star detail";Width=900;Height=790;MinWidth=660;MinHeight=520;Background=Palette.Background;FontFamily="Inter";WindowStartupLocation=WindowStartupLocation.CenterOwner;
var panel=new StackPanel{Margin=new(24),Spacing=12};Content=new Border{Background=Palette.Background,Child=new ScrollViewer{Content=panel}};var progress=Palette.Label("Reading matched source stars…",16);panel.Children.Add(progress);
Opened+=async(_,_)=>
{
try
{
var peer=StellarDetail.Comparison(project,selected)??throw new InvalidDataException("No comparable KEEP exposure in this session");
var detail=await Task.Run(async()=>{await ResourcePolicy.WaitForHeadroom(ResourcePolicy.EstimateFrameBytes(selected.Width,selected.Height,selected.Bayer!=""?2:1)*2,cancellation.Token);return StellarDetail.Read(directory,selected,peer,cancellation.Token);},cancellation.Token);
cancellation.Token.ThrowIfCancellationRequested();panel.Children.Clear();panel.Children.Add(Palette.Label("Matched stars · original source samples",21));
panel.Children.Add(new TextBlock{Text="Identical stars, aligned between exposures. Each crop has its own brightness normalization. Pixels are enlarged without smoothing; Bayer samples are combined 2×2. Judge shape here, and transparency from the flux measurements.",FontSize=12,Foreground=Palette.Muted,TextWrapping=TextWrapping.Wrap});
var header=new Grid{ColumnDefinitions=new("45,*,*")};
TextBlock Label(string text)=>new(){Text=text,Foreground=Palette.Text,TextWrapping=TextWrapping.Wrap,FontSize=12,Margin=new(8,0)};
var left=Label(selected.Status+" · "+selected.Time+" UTC\n"+selected.Name+$"\nFWHM {selected.Fwhm:0.00} px · ellipticity {selected.Ellipticity:0.00}");Grid.SetColumn(left,1);header.Children.Add(left);
var right=Label(peer.Status+" · "+peer.Time+" UTC\n"+peer.Name+$"\nFWHM {peer.Fwhm:0.00} px · ellipticity {peer.Ellipticity:0.00}");Grid.SetColumn(right,2);header.Children.Add(right);panel.Children.Add(header);
foreach(var star in detail.Stars)
{
var row=new Grid{ColumnDefinitions=new("45,*,*"),Margin=new(0,6)};row.Children.Add(Palette.Label(star.Number.ToString("00"),12,Palette.Muted));
foreach(var entry in new[]{(Pixels:star.Selected,Column:1),(Pixels:star.Comparison,Column:2)})
{
var bitmap=Gray(entry.Pixels,detail.Size);bitmaps.Add(bitmap);var image=new Image{Source=bitmap,Width=130,Height=130,HorizontalAlignment=HorizontalAlignment.Left,Margin=new(8,0)};RenderOptions.SetBitmapInterpolationMode(image,BitmapInterpolationMode.None);Grid.SetColumn(image,entry.Column);row.Children.Add(image);
}
panel.Children.Add(row);
}
if(capture!=null)
{
await Task.Delay(200);if(Content is Control content){using var bitmap=new RenderTargetBitmap(new PixelSize((int)content.Bounds.Width,(int)content.Bounds.Height),new Vector(96,96));bitmap.Render(content);bitmap.Save(capture);}((IClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!).Shutdown(0);
}
}
catch(OperationCanceledException){}
catch(Exception e){progress.Text="Star detail unavailable: "+e.Message;if(capture!=null){File.WriteAllText(capture+".error",e.ToString());((IClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!).Shutdown(1);}}
};
Closing+=(_,_)=>cancellation.Cancel();Closed+=(_,_)=>{foreach(var bitmap in bitmaps)bitmap.Dispose();cancellation.Dispose();};
}
static WriteableBitmap Gray(byte[] pixels,int size)
{
var bitmap=new WriteableBitmap(new(size,size),new(96,96),PixelFormat.Bgra8888,AlphaFormat.Opaque);using var locked=bitmap.Lock();byte[] row=new byte[size*4];
for(int y=0;y<size;y++){for(int x=0;x<size;x++){byte v=pixels[y*size+x];row[4*x]=row[4*x+1]=row[4*x+2]=v;row[4*x+3]=255;}Marshal.Copy(row,0,locked.Address+y*locked.RowBytes,row.Length);}return bitmap;
}
}