Skip to content

Commit

Permalink
Show real pixel value in histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
pchev committed Oct 8, 2019
1 parent a361589 commit 3f31b60
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 17 deletions.
18 changes: 12 additions & 6 deletions src/fu_visu.lfm
Expand Up @@ -21,8 +21,8 @@ object f_visu: Tf_visu
AutoSize = True
ChildSizing.Layout = cclLeftToRightThenTopToBottom
ChildSizing.ControlsPerLine = 99
ClientHeight = 92
ClientWidth = 371
ClientHeight = 88
ClientWidth = 367
ParentFont = False
TabOrder = 0
object Panel3: TPanel
Expand Down Expand Up @@ -51,30 +51,36 @@ object f_visu: Tf_visu
OnMouseMove = HistogramMouseMove
OnMouseUp = HistogramMouseUp
end
object SpinEditMin: TSpinEdit
object SpinEditMin: TFloatSpinEdit
Left = 0
Height = 26
Top = 0
Width = 70
Constraints.MaxHeight = 26
DecimalPlaces = 0
Increment = 10
MaxValue = 65536
MinValue = 0
OnChange = SpinEditMinChange
ParentColor = True
TabOrder = 0
Value = 0
Visible = False
end
object SpinEditMax: TSpinEdit
object SpinEditMax: TFloatSpinEdit
Left = 186
Height = 26
Top = 0
Width = 70
Constraints.MaxHeight = 26
DecimalPlaces = 0
Increment = 10
MaxValue = 65536
MinValue = 0
OnChange = SpinEditMaxChange
ParentColor = True
TabOrder = 1
Value = 0
Visible = False
end
end
Expand Down Expand Up @@ -584,7 +590,7 @@ object f_visu: Tf_visu
object BtnFlipHorz: TSpeedButton
Left = 0
Height = 22
Hint = 'Flip the image horizontally'
Hint = 'Flip the image horizontally'
Top = 66
Width = 28
AllowAllUp = True
Expand Down Expand Up @@ -661,7 +667,7 @@ object f_visu: Tf_visu
object BtnFlipVert: TSpeedButton
Left = 29
Height = 22
Hint = 'Flip the image vertically'
Hint = 'Flip the image vertically'
Top = 66
Width = 28
AllowAllUp = True
Expand Down
76 changes: 66 additions & 10 deletions src/fu_visu.pas
Expand Up @@ -52,8 +52,8 @@ Tf_visu = class(TFrame)
Panel3: TPanel;
BtnZoom2: TSpeedButton;
BtnZoom1: TSpeedButton;
SpinEditMin: TSpinEdit;
SpinEditMax: TSpinEdit;
SpinEditMin: TFloatSpinEdit;
SpinEditMax: TFloatSpinEdit;
Title: TLabel;
TimerRedraw: TTimer;
TimerMinMax: TTimer;
Expand Down Expand Up @@ -83,6 +83,8 @@ Tf_visu = class(TFrame)
procedure TimerRedrawTimer(Sender: TObject);
private
{ private declarations }
FimageC, FimageMin, FimageMax : double;
FisFloatingPoint, Finitialized: boolean;
FimgMin, FimgMax: double;
FBullsEye, LockSpinEdit, FClipping, FInvert, FFlipVert, FFlipHorz: Boolean;
FZoom: double;
Expand All @@ -92,13 +94,14 @@ Tf_visu = class(TFrame)
FRedraw: TNotifyEvent;
FonZoom: TNotifyEvent;
FRedrawHistogram: TNotifyEvent;
FShowHistogramPos: TNotifyStr;
procedure SetZoom(value: double);
procedure SetLang;
public
{ public declarations }
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure DrawHistogram(hist:Thistogram; SetLevel: boolean);
procedure DrawHistogram(hist:Thistogram; SetLevel,isFloatingPoint: boolean; iC,iMin,iMax: double);
property Zoom: double read FZoom write SetZoom;
property ImgMin: double read FimgMin write FimgMin;
property ImgMax: double read FimgMax write FimgMax;
Expand All @@ -110,6 +113,7 @@ Tf_visu = class(TFrame)
property onZoom: TNotifyEvent read FonZoom write FonZoom;
property onRedraw: TNotifyEvent read FRedraw write FRedraw;
property onRedrawHistogram: TNotifyEvent read FRedrawHistogram write FRedrawHistogram;
property onShowHistogramPos: TNotifyStr read FShowHistogramPos write FShowHistogramPos;
end;

implementation
Expand Down Expand Up @@ -139,6 +143,7 @@ constructor Tf_visu.Create(aOwner: TComponent);
{$endif}
ScaleDPI(Self);
SetLang;
Finitialized:=false;
ImgMax:=high(word);
ImgMin:=0;
FBullsEye:=false;
Expand Down Expand Up @@ -182,13 +187,17 @@ procedure Tf_visu.SetLang;
BtnFlipVert.Hint:=rsFlipTheImageV;
end;

procedure Tf_visu.DrawHistogram(hist:Thistogram; SetLevel: boolean);
procedure Tf_visu.DrawHistogram(hist:Thistogram; SetLevel,isFloatingPoint: boolean; iC,iMin,iMax: double);
var i,j,maxp,maxh,h,hd2,l: integer;
sum,sl1,sh1,sl2,sh2,sl3,sh3,sl4,sh4,hc: integer;
sh: double;
begin
try
LockSpinEdit:=true;
FimageC:=iC;
FimageMin:=iMin;
FimageMax:=iMax;
FisFloatingPoint:=isFloatingPoint;
maxh:=0;
sum:=0;
for i:=0 to high(word) do begin
Expand Down Expand Up @@ -240,8 +249,39 @@ procedure Tf_visu.DrawHistogram(hist:Thistogram; SetLevel: boolean);
FImgMax:=h4;
end;
end;
SpinEditMin.Value:=round(FImgMin);
SpinEditMax.Value:=round(FimgMax);
// adjust spinedit for data range
if isFloatingPoint then begin
SpinEditMin.DecimalPlaces:=3;
SpinEditMax.DecimalPlaces:=3;
if abs(FimageMax-FimageMin)<=10 then begin
SpinEditMin.Increment:=0.01;
SpinEditMax.Increment:=0.01;
end
else begin
SpinEditMin.Increment:=1;
SpinEditMax.Increment:=1;
end;
end
else begin
SpinEditMin.DecimalPlaces:=0;
SpinEditMax.DecimalPlaces:=0;
if abs(FimageMax-FimageMin)<=255 then begin
SpinEditMin.Increment:=1;
SpinEditMax.Increment:=1;
end
else begin
SpinEditMin.Increment:=10;
SpinEditMax.Increment:=10;
end;
end;
// histogram is always 0-65535, show real pixel value in the spinedit
SpinEditMin.minValue:=FimageMin;
SpinEditMin.maxValue:=FimageMax;
SpinEditMax.minValue:=FimageMin;
SpinEditMax.maxValue:=FimageMax;
// scale from 0-65535 to image min-max
SpinEditMin.Value:=FimageMin+FImgMin/FimageC;
SpinEditMax.Value:=FimageMin+FimgMax/FimageC;
Histogram.Picture.Bitmap.Width:=Histogram.Width;
Histogram.Picture.Bitmap.Height:=Histogram.Height;
with Histogram.Picture.Bitmap do begin
Expand Down Expand Up @@ -274,6 +314,7 @@ procedure Tf_visu.DrawHistogram(hist:Thistogram; SetLevel: boolean);
Canvas.Pen.Color:=clGreen;
i:=round(ImgMax/255-1);
Canvas.Line(i,0,i,Height);
Finitialized:=true;
end;
finally
if (GetCurrentThreadId=MainThreadID) then Application.ProcessMessages;
Expand Down Expand Up @@ -413,6 +454,7 @@ procedure Tf_visu.HistogramMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var dx1,dx2: double;
begin
if not Finitialized then exit;
dx1:=abs(ImgMin/255-X);
dx2:=abs(ImgMax/255-X);
Updmax:=dx2<dx1;
Expand All @@ -422,24 +464,36 @@ procedure Tf_visu.HistogramMouseDown(Sender: TObject; Button: TMouseButton;

procedure Tf_visu.HistogramMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var xpos,val: double;
txt: string;
begin
if not Finitialized then exit;
if StartUpd then begin
with Histogram.Picture.Bitmap do begin
Canvas.Pen.Color:=clWhite;
Canvas.Pen.Mode:=pmXor;
Canvas.Line(XP,0,XP,Height);
Canvas.Line(X,0,X,Height);
XP:=X;
if Updmax then xpos:=min(high(word),X*255)
else xpos:=max(0,X*255);
val:=FimageMin+xpos/FimageC;
if FisFloatingPoint then
txt:=FormatFloat(f3,val)
else
txt:=FormatFloat(f0,round(val));
if Assigned(FShowHistogramPos) then FShowHistogramPos(txt);
end;
end else begin
SpinEditMin.Visible:=(y<24)and(x<60);
SpinEditMax.Visible:=(y<24)and(x>195);
SpinEditMin.Visible:=(y<SpinEditMin.Height)and(x<SpinEditMin.Width);
SpinEditMax.Visible:=(y<SpinEditMax.Height)and(x>SpinEditMax.Left);
end;
end;

procedure Tf_visu.HistogramMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if not Finitialized then exit;
if Updmax then ImgMax:=min(high(word),X*255)
else ImgMin:=max(0,X*255);
StartUpd:=false;
Expand All @@ -463,14 +517,16 @@ procedure Tf_visu.TimerMinMaxTimer(Sender: TObject);
begin
TimerMinMax.Enabled:=false;
histminmax.Down:=true;
ImgMax:=SpinEditMax.Value;
ImgMin:=SpinEditMin.Value;
// scale from image min-max to 0-65535
FImgMin:=round(FimageC*(SpinEditMin.Value-FimageMin));
FImgMax:=round(FimageC*(SpinEditMax.Value-FimageMin));
if Assigned(FRedraw) then FRedraw(self);
end;

procedure Tf_visu.TimerRedrawTimer(Sender: TObject);
begin
TimerRedraw.Enabled:=false;
if Assigned(FShowHistogramPos) then FShowHistogramPos('');
if Assigned(FRedraw) then FRedraw(self);
end;

Expand Down
9 changes: 8 additions & 1 deletion src/pu_main.pas
Expand Up @@ -695,6 +695,7 @@ Tf_main = class(TForm)
Procedure StartCaptureExposureNow;
Procedure RecenterTarget;
Procedure RedrawHistogram(Sender: TObject);
Procedure ShowHistogramPos(msg:string);
Procedure Redraw(Sender: TObject);
Procedure ZoomImage(Sender: TObject);
Procedure ClearImage;
Expand Down Expand Up @@ -1266,6 +1267,7 @@ procedure Tf_main.FormCreate(Sender: TObject);
f_visu.onRedraw:=@Redraw;
f_visu.onZoom:=@ZoomImage;
f_visu.onRedrawHistogram:=@RedrawHistogram;
f_visu.onShowHistogramPos:=@ShowHistogramPos;

f_frame:=Tf_frame.Create(self);
f_frame.onSet:=@SetFrame;
Expand Down Expand Up @@ -7778,6 +7780,11 @@ procedure Tf_main.CameraVideoFrame(Sender: TObject);
DrawHistogram(false);
end;

Procedure Tf_main.ShowHistogramPos(msg:string);
begin
StatusBar1.Panels[0].Text:=msg;
end;

Procedure Tf_main.Redraw(Sender: TObject);
begin
DrawHistogram(false);
Expand Down Expand Up @@ -8174,7 +8181,7 @@ procedure Tf_main.StarSelection(Sender: TObject);
Procedure Tf_main.DrawHistogram(SetLevel: boolean);
begin
if fits.HeaderInfo.naxis>0 then begin
f_visu.DrawHistogram(fits.Histogram,SetLevel);
f_visu.DrawHistogram(fits.Histogram,SetLevel,fits.HeaderInfo.floatingpoint,fits.imageC,fits.imageMin,fits.imageMax);
end;
end;

Expand Down

0 comments on commit 3f31b60

Please sign in to comment.