/
cu_fits.pas
3446 lines (3254 loc) · 107 KB
/
cu_fits.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit cu_fits;
{
Copyright (C) 2005-2015 Patrick Chevalley
http://www.ap-i.net
pch@ap-i.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
{$mode delphi}{$H+}
interface
uses SysUtils, Classes, LazFileUtils, u_utils, u_global, BGRABitmap, BGRABitmapTypes, ExpandedBitmap,
GraphType, FPReadJPEG, LazSysUtils, u_libraw, dateutils,
LazUTF8, Graphics,Math, FPImage, Controls, LCLType, Dialogs, u_translation, IntfGraphics;
type
TFitsInfo = record
valid, solved, floatingpoint: boolean;
bitpix,naxis,naxis1,naxis2,naxis3 : integer;
Frx,Fry,Frwidth,Frheight,BinX,BinY: integer;
bzero,bscale,dmax,dmin,blank : double;
bayerpattern: string;
bayeroffsetx, bayeroffsety: integer;
rmult,gmult,bmult: double;
equinox,ra,dec,crval1,crval2: double;
pixsz1,pixsz2,pixratio,focallen: double;
exptime,airmass: double;
objects,ctype1,ctype2 : string;
end;
THeaderBlock = array[1..36,1..80] of char;
TStar = record
x,y: double;
hfd, fwhm: double;
vmax, snr, bg: double;
end;
TStarList = array of TStar;
Timai8 = array of array of array of byte; TPimai8 = ^Timai8;
Timai16 = array of array of array of smallint; TPimai16 = ^Timai16;
Timaw16 = array of array of array of word; TPimaw16 = ^Timaw16;
Timai32 = array of array of array of longint; TPimai32 = ^Timai32;
Timar32 = array of array of array of single; TPimar32 = ^Timar32;
Timar64 = array of array of array of double; TPimar64 = ^Timar64;
THistogram = array[0..high(word)] of integer;
TMathOperator = (moAdd,moSub,moMean,moMult,moDiv);
TFitsHeader = class(TObject)
private
FRows: TStringList;
FKeys: TStringList;
FValues: TStringList;
FComments:TStringList;
Fvalid : boolean;
public
constructor Create;
destructor Destroy; override;
procedure ClearHeader;
procedure Assign(value: TFitsHeader);
function ReadHeader(ff:TMemoryStream): integer;
function NewWCS(ff:TMemoryStream): boolean;
function GetStream: TMemoryStream;
function Indexof(key: string): integer;
function Valueof(key: string; out val: string): boolean; overload;
function Valueof(key: string; out val: integer): boolean; overload;
function Valueof(key: string; out val: double): boolean; overload;
function Valueof(key: string; out val: boolean): boolean; overload;
function Add(key,val,comment: string; quotedval:boolean=true): integer; overload;
function Add(key:string; val:integer; comment: string): integer; overload;
function Add(key:string; val:double; comment: string): integer; overload;
function Add(key:string; val:boolean; comment: string): integer; overload;
function Insert(idx: integer; key,val,comment: string; quotedval:boolean=true):integer; overload;
function Insert(idx: integer; key:string; val:integer; comment: string):integer; overload;
function Insert(idx: integer; key:string; val:double; comment: string):integer; overload;
function Insert(idx: integer; key:string; val:boolean; comment: string):integer; overload;
procedure Delete(idx: integer);
property Rows: TStringList read FRows;
property Keys: TStringList read FKeys;
property Values: TStringList read FValues;
property Comments:TStringList read FComments;
end;
const maxl = 20000;
type
TFits = class(TComponent)
private
// Original Fits file
FStream : TMemoryStream;
// Fits read buffers
d8 : array[1..2880] of byte;
d16 : array[1..1440] of smallint;
d32 : array[1..720] of Longword;
d64 : array[1..360] of Int64;
// Original image data
imai8 : Timai8;
imai16 : Timai16;
imai32 : Timai32;
imar32 : Timar32;
imar64 : Timar64;
// 16bit image scaled min/max unsigned
Fimage : Timaw16;
// Fimage scaling factor
FimageC, FimageMin,FimageMax : double;
// Histogram of Fimage
FHistogram: THistogram;
// Fits header
FHeader: TFitsHeader;
// same as Fimage in TLazIntfImage format
FIntfImg: TLazIntfImage;
// Fits header values
FFitsInfo : TFitsInfo;
//
n_axis,cur_axis,Fwidth,Fheight,Fhdr_end,colormode : Integer;
FTitle : string;
Fmean,Fsigma,Fdmin,Fdmax : double;
FImgDmin, FImgDmax: Word;
FImgFullRange,FStreamValid: Boolean;
Fbpm: TBpm;
FBPMcount,FBPMnx,FBPMny,FBPMnax: integer;
gamma_c : array[0..32768] of single; {prepared power values for gamma correction}
FGamma: single;
emptybmp:Tbitmap;
FMarkOverflow: boolean;
FMaxADU, FOverflow, FUnderflow: double;
FInvert: boolean;
FStarList: TStarList;
FDark: TFits;
FDarkOn: boolean;
FDarkProcess, FBPMProcess: boolean;
FonMsg: TNotifyMsg;
procedure msg(txt: string; level:integer=3);
procedure SetStream(value:TMemoryStream);
function GetStream: TMemoryStream;
procedure SetVideoStream(value:TMemoryStream);
Procedure ReadFitsImage;
Procedure WriteFitsImage;
Procedure GetImage;
function GammaCorr(value: Word):byte;
procedure SetImgFullRange(value: boolean);
function GetHasBPM: boolean;
procedure SetGamma(value: single);
function GetBayerMode: TBayerMode;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
function GetStatistics: string;
Procedure LoadStream;
procedure GetFitsInfo;
function BayerInterpolationExp(t:TBayerMode; rmult,gmult,bmult:double; pix1,pix2,pix3,pix4,pix5,pix6,pix7,pix8,pix9:integer; row,col:integer):TExpandedPixel; inline;
function BayerInterpolation(t:TBayerMode; rmult,gmult,bmult:double; pix1,pix2,pix3,pix4,pix5,pix6,pix7,pix8,pix9:integer; row,col:integer):TBGRAPixel; inline;
procedure GetExpBitmap(var bgra: TExpandedBitmap; debayer:boolean);
procedure GetBGRABitmap(var bgra: TBGRABitmap; debayer:boolean);
procedure SaveToBitmap(fn: string);
procedure SaveToFile(fn: string; pack: boolean=false);
procedure LoadFromFile(fn:string);
procedure SetBPM(value: TBpm; count,nx,ny,nax:integer);
procedure ApplyBPM;
procedure ApplyDark;
procedure FreeDark;
procedure ClearImage;
procedure Math(operand: TFits; MathOperator:TMathOperator; new: boolean=false);
procedure Shift(dx,dy: double);
procedure ShiftInteger(dx,dy: integer);
procedure Bitpix8to16;
function SameFormat(f:TFits): boolean;
function double_star(ri, x,y : integer):boolean;
function value_subpixel(x1,y1:double):double;
procedure FindBrightestPixel(x,y,s,starwindow2: integer; out xc,yc:integer; out vmax: double; accept_double: boolean=true);
procedure FindStarPos(x,y,s: integer; out xc,yc,ri:integer; out vmax,bg,bg_standard_deviation: double);
procedure GetHFD2(x,y,s: integer; out xc,yc,bg,bg_standard_deviation,hfd,star_fwhm,valmax,snr,flux: double; strict_saturation: boolean=true);{han.k 2018-3-21}
procedure GetStarList(rx,ry,s: integer);
procedure MeasureStarList(s: integer; list: TArrayDouble2);
procedure ClearStarList;
property IntfImg: TLazIntfImage read FIntfImg;
property Title : string read FTitle write FTitle;
Property HeaderInfo : TFitsInfo read FFitsInfo;
property Header: TFitsHeader read FHeader write FHeader;
Property Stream : TMemoryStream read GetStream write SetStream;
Property VideoStream : TMemoryStream write SetVideoStream;
property Histogram : THistogram read FHistogram;
property ImgDmin : Word read FImgDmin write FImgDmin;
property ImgDmax : Word read FImgDmax write FImgDmax;
property Gamma: single read FGamma write SetGamma;
property image : Timaw16 read Fimage;
property imageC : double read FimageC;
property imageMin : double read FimageMin;
property imageMax : double read FimageMax;
property imageMean: double read Fmean;
property imageSigma: double read Fsigma;
property BayerMode: TBayerMode read GetBayerMode;
property ImgFullRange: Boolean read FImgFullRange write SetImgFullRange;
property MaxADU: double read FMaxADU write FMaxADU;
property Invert: boolean read FInvert write FInvert;
property MarkOverflow: boolean read FMarkOverflow write FMarkOverflow;
property Overflow: double read FOverflow write FOverflow;
property Underflow: double read FUnderflow write FUnderflow;
property hasBPM: boolean read GetHasBPM;
property BPMProcess: boolean read FBPMProcess;
property StarList: TStarList read FStarList;
property DarkProcess: boolean read FDarkProcess;
property DarkOn: boolean read FDarkOn write FDarkOn;
property DarkFrame: TFits read FDark write FDark;
property onMsg: TNotifyMsg read FonMsg write FonMsg;
end;
TGetImage = class(TThread)
public
working: boolean;
num, id: integer;
fits: TFits;
hist: THistogram;
Fdmin,c: double;
procedure Execute; override;
constructor Create(CreateSuspended: boolean);
end;
TGetBgraThread = class(TThread)
public
working: boolean;
num, id: integer;
fits: TFits;
bgra: TBGRABitmap;
HighOverflow,LowOverflow: TBGRAPixel;
c,overflow,underflow: double;
rmult,gmult,bmult,mx: double;
debayer: boolean;
t: TBayerMode;
FImgDmin: word;
procedure Execute; override;
constructor Create(CreateSuspended: boolean);
end;
TGetExpThread = class(TThread)
public
working: boolean;
num, id: integer;
fits: TFits;
bgra: TExpandedBitmap;
rmult,gmult,bmult,mx: double;
debayer: boolean;
t: TBayerMode;
procedure Execute; override;
constructor Create(CreateSuspended: boolean);
end;
TGetStarList = class(TThread)
public
working: boolean;
num, id: integer;
fits: TFits;
StarList: TStarList;
rx,ry,overlap,s: integer;
img_temp: Timai8;
procedure Execute; override;
constructor Create(CreateSuspended: boolean);
end;
procedure PictureToFits(pict:TMemoryStream; ext: string; var ImgStream:TMemoryStream; flip:boolean=true;pix:double=-1;piy:double=-1;binx:integer=-1;biny:integer=-1;bayer:string='');
procedure RawToFits(raw:TMemoryStream; var ImgStream:TMemoryStream; out rmsg:string; pix:double=-1;piy:double=-1;binx:integer=-1;biny:integer=-1);
function PackFits(unpackedfilename,packedfilename: string; out rmsg:string):integer;
function UnpackFits(packedfilename: string; var ImgStream:TMemoryStream; out rmsg:string):integer;
implementation
//////////////////// TFitsHeader /////////////////////////
constructor TFitsHeader.Create;
begin
inherited Create;
FRows:=TStringList.Create;
FComments:=TStringList.Create;
FValues:=TStringList.Create;
FKeys:=TStringList.Create;
Fvalid:=false;
end;
destructor TFitsHeader.Destroy;
begin
FRows.Free;
FComments.Free;
FValues.Free;
FKeys.Free;
inherited Destroy;
end;
procedure TFitsHeader.ClearHeader;
begin
Fvalid:=false;
FRows.Clear;
FKeys.Clear;
FValues.Clear;
FComments.Clear;
end;
procedure TFitsHeader.Assign(value: TFitsHeader);
begin
ClearHeader;
FRows.Assign(value.FRows);
FKeys.Assign(value.FKeys);
FValues.Assign(value.FValues);
FComments.Assign(value.FComments);
Fvalid:=value.Fvalid;
end;
function TFitsHeader.NewWCS(ff:TMemoryStream): boolean;
var header : THeaderBlock;
i,p1,p2,n,ii : integer;
eoh : boolean;
row,keyword,value,comment,buf : string;
P: PChar;
const excl1:array[0..18] of string=('CTYPE','WCSAXES','EQUINOX','LONPOLE','LATPOLE','CRVAL','CRPIX','CUNIT','CD','CDELT','A_','B_','AP_','BP_','PV','CROTA','END','IMAGEW','IMAGEH');
excl2:array[0..3] of string=('SIMPLE','BITPIX','EXTEND','NAXIS');
function IsKeywordIn(k:string; klist:array of string): boolean;
var j: integer;
begin
result:=false;
for j:=0 to Length(klist)-1 do begin
if pos(klist[j],k)=1 then begin
result:=true;
break;
end;
end;
end;
begin
result:=false;
if FKeys.Count>0 then begin
// delete old wcs
for i:=FKeys.Count-1 downto 0 do begin
if IsKeywordIn(FKeys[i],excl1) then begin
Delete(i);
end;
end;
// load new wcs
eoh:=false;
ff.Position:=0;
header[1,1]:=chr(0);
repeat
n:=ff.Read(header,sizeof(THeaderBlock));
if n<>sizeof(THeaderBlock) then
Break;
for i:=1 to 36 do begin
row:=header[i];
if trim(row)='' then continue;
p1:=pos('=',row);
if p1=0 then p1:=9;
p2:=pos('/',row);
keyword:=trim(copy(row,1,p1-1));
if p2>0 then begin
value:=trim(copy(row,p1+1,p2-p1-1));
comment:=trim(copy(row,p2,99));
end else begin
value:=trim(copy(row,p1+1,99));
comment:='';
end;
if (keyword='SIMPLE') then
if (copy(value,1,1)='T') then begin
Fvalid:=true;
end
else begin
Fvalid:=false;
Break;
end;
if (keyword='END') then begin
eoh:=true;
end;
P:=PChar(value);
buf:=AnsiExtractQuotedStr(P,'''');
if buf<>'' then value:=buf;
if not IsKeywordIn(keyword,excl2) then begin
if (keyword<>'')and(keyword<>'COMMENT')and(keyword<>'HISTORY') then
ii:=FKeys.IndexOf(keyword)
else
ii:=-1;
if ii<0 then begin
FRows.add(row);
FKeys.add(keyword);
FValues.add(value);
FComments.add(comment);
end
else begin
FRows[ii]:=row;
FKeys[ii]:=keyword;
FValues[ii]:=value;
FComments[ii]:=comment;
end;
end;
end;
if not Fvalid then begin
Break;
end;
until eoh;
end;
end;
function TFitsHeader.ReadHeader(ff:TMemoryStream): integer;
var header : THeaderBlock;
i,p1,p2,n : integer;
eoh : boolean;
row,keyword,value,comment,buf : string;
P: PChar;
begin
ClearHeader;
eoh:=false;
ff.Position:=0;
header[1,1]:=chr(0);
repeat
n:=ff.Read(header,sizeof(THeaderBlock));
if n<>sizeof(THeaderBlock) then
Break;
for i:=1 to 36 do begin
row:=header[i];
if trim(row)='' then continue;
p1:=pos('=',row);
if p1=0 then p1:=9;
p2:=pos('/',row);
keyword:=trim(copy(row,1,p1-1));
if p2>0 then begin
value:=trim(copy(row,p1+1,p2-p1-1));
comment:=trim(copy(row,p2,99));
end else begin
value:=trim(copy(row,p1+1,99));
comment:='';
end;
if (keyword='SIMPLE') then
if (copy(value,1,1)='T') then begin
Fvalid:=true;
end
else begin
Fvalid:=false;
Break;
end;
if (keyword='END') then begin
eoh:=true;
end;
P:=PChar(value);
buf:=AnsiExtractQuotedStr(P,'''');
if buf<>'' then value:=buf;
FRows.add(row);
FKeys.add(keyword);
FValues.add(value);
FComments.add(comment);
end;
if not Fvalid then begin
Break;
end;
until eoh;
result:=ff.position;
end;
function TFitsHeader.GetStream: TMemoryStream;
var i,c:integer;
buf: array[0..79] of char;
begin
result:=TMemoryStream.Create;
for i:=0 to FRows.Count-1 do begin
buf:=FRows[i];
result.Write(buf,80);
end;
if (FRows.Count mod 36)>0 then begin
buf:=b80;
c:=36 - (FRows.Count mod 36);
for i:=1 to c do result.Write(buf,80);
end;
end;
function TFitsHeader.Indexof(key: string): integer;
begin
result:=FKeys.IndexOf(key);
end;
function TFitsHeader.Valueof(key: string; out val: string): boolean; overload;
var k: integer;
begin
val:='';
k:=FKeys.IndexOf(key);
result:=(k>=0);
if result then val:=FValues[k];
end;
function TFitsHeader.Valueof(key: string; out val: integer): boolean; overload;
var k: integer;
begin
val:=0;
k:=FKeys.IndexOf(key);
result:=(k>=0);
if result then val:=StrToIntDef(trim(FValues[k]),0);
end;
function TFitsHeader.Valueof(key: string; out val: double): boolean; overload;
var k: integer;
begin
val:=0;
k:=FKeys.IndexOf(key);
result:=(k>=0);
if result then val:=StrToFloatDef(trim(FValues[k]),0);
end;
function TFitsHeader.Valueof(key: string; out val: boolean): boolean; overload;
var k: integer;
begin
val:=false;
k:=FKeys.IndexOf(key);
result:=(k>=0);
if result then val:=(trim(FValues[k])='T');
end;
function TFitsHeader.Add(key,val,comment: string; quotedval:boolean=true): integer;
begin
result:=Insert(-1,key,val,comment,quotedval);
end;
function TFitsHeader.Add(key:string; val:integer; comment: string): integer;
begin
result:=Insert(-1,key,val,comment);
end;
function TFitsHeader.Add(key:string; val:double; comment: string): integer;
begin
result:=Insert(-1,key,val,comment);
end;
function TFitsHeader.Add(key:string; val:boolean; comment: string): integer;
begin
result:=Insert(-1,key,val,comment);
end;
function TFitsHeader.Insert(idx: integer; key,val,comment: string; quotedval:boolean=true): integer;
var row: string;
ii: integer;
begin
// The END keyword
if (trim(key)='END') then begin
row:=copy('END'+b80,1,80);
val:='';
comment:='';
end
// Comments with keyword
else if (trim(key)='COMMENT') then begin
val:=val+comment;
comment:='';
row:=Format('%0:-8s',[key])+
Format(' %0:-70s',[val]);
end
// Comment without keyword
else if (trim(key)='') then begin
val:=val+comment;
comment:='';
row:=Format(' %0:-70s',[val]);
end
// Quoted string
else if quotedval then begin
row:=Format('%0:-8s',[key])+
Format('= %0:-20s',[QuotedStr(val)])+
Format(' / %0:-47s',[comment]);
end
// Other unquoted values
else begin
row:=Format('%0:-8s',[key])+
Format('= %0:-20s',[val])+
Format(' / %0:-47s',[comment]);
end;
row:=copy(row,1,80);
// Search for existing key
if (key<>'')and(key<>'COMMENT')and(key<>'HISTORY') then
ii:=FKeys.IndexOf(key)
else
ii:=-1;
if ii>=0 then begin
// replace existing key
FRows[ii]:=row;
FKeys[ii]:=key;
FValues[ii]:=val;
FComments[ii]:=comment;
result:=ii;
end
else if idx>=0 then begin
// insert key at position
FRows.Insert(idx,row);
FKeys.Insert(idx,key);
FValues.Insert(idx,val);
FComments.Insert(idx,comment);
result:=idx;
end else begin
// add key at end
result:=FRows.Add(row);
FKeys.Add(key);
FValues.Add(val);
FComments.Add(comment);
end;
end;
function TFitsHeader.Insert(idx: integer; key:string; val:integer; comment: string):integer;
var txt: string;
begin
txt:=Format('%20d',[val]);
result:=Insert(idx,key,txt,comment,false);
end;
function TFitsHeader.Insert(idx: integer; key:string; val:double; comment: string):integer;
var txt: string;
begin
txt:=Format('%20.10g',[val]);
result:=Insert(idx,key,txt,comment,false);
end;
function TFitsHeader.Insert(idx: integer; key:string; val:boolean; comment: string):integer;
var txt,v: string;
begin
if val then v:='T' else v:='F';
txt:=Format('%0:20s',[v]);
result:=Insert(idx,key,txt,comment,false);
if (not Fvalid)and(key='SIMPLE')and(val) then Fvalid:=true;
end;
procedure TFitsHeader.Delete(idx: integer);
begin
FRows.Delete(idx);
FKeys.Delete(idx);
FValues.Delete(idx);
FComments.Delete(idx);
end;
//////////////////// TGetImage /////////////////////////
constructor TGetImage.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := False;
inherited Create(CreateSuspended);
working := True;
end;
procedure TGetImage.Execute;
var
i, j, startline, endline, xs,ys: integer;
x : word;
h: integer;
xx: extended;
begin
xs:= fits.Fwidth;
ys:= fits.FHeight;
i := ys div num;
startline := id * i;
if id = (num - 1) then
endline := ys - 1
else
endline := (id + 1) * i - 1;
FillByte(hist,sizeof(THistogram),0);
// process the rows range for this thread
case fits.FFitsInfo.bitpix of
-64 : begin
for i:=startline to endline do begin
for j := 0 to xs-1 do begin
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imar64[0,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[0,i,j]:=x;
if fits.n_axis=3 then begin
h:=x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imar64[1,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[1,i,j]:=x;
h:=h+x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imar64[2,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[2,i,j]:=x;
x:=(h+x) div 3;
end;
inc(hist[x]);
end;
end;
end;
-32 : begin
for i:=startline to endline do begin
for j := 0 to xs-1 do begin
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imar32[0,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[0,i,j]:=x;
if fits.n_axis=3 then begin
h:=x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imar32[1,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[1,i,j]:=x;
h:=h+x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imar32[2,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[2,i,j]:=x;
x:=(h+x) div 3;
end;
inc(hist[x]);
end;
end;
end;
8 : begin
for i:=startline to endline do begin
for j := 0 to xs-1 do begin
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai8[0,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[0,i,j]:=x;
if fits.n_axis=3 then begin
h:=x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai8[1,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[1,i,j]:=x;
h:=h+x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai8[2,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[2,i,j]:=x;
x:=(h+x) div 3;
end;
inc(hist[x]);
end;
end;
end;
16 : begin
for i:=startline to endline do begin
for j := 0 to xs-1 do begin
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai16[0,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[0,i,j]:=x;
if fits.n_axis=3 then begin
h:=x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai16[1,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[1,i,j]:=x;
h:=h+x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai16[2,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[2,i,j]:=x;
x:=(h+x) div 3;
end;
inc(hist[x]);
end;
end;
end;
32 : begin
for i:=startline to endline do begin
for j := 0 to xs-1 do begin
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai32[0,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[0,i,j]:=x;
if fits.n_axis=3 then begin
h:=x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai32[1,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[1,i,j]:=x;
h:=h+x;
xx:=fits.FFitsInfo.bzero+fits.FFitsInfo.bscale*fits.imai32[2,i,j];
x:=round(max(0,min(MaxWord,(xx-Fdmin) * c )) );
fits.Fimage[2,i,j]:=x;
x:=(h+x) div 3;
end;
inc(hist[x]);
end;
end;
end;
end;
working := False;
end;
//////////////////// TGetBgraThread /////////////////////////
constructor TGetBgraThread.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
working := True;
end;
procedure TGetBgraThread.Execute;
var
i, j, startline, endline, xs,ys: integer;
ii,i1,i2,i3,j1,j2,j3 : integer;
x : word;
xx,xxg,xxb: extended;
p: PBGRAPixel;
pix1,pix2,pix3,pix4,pix5,pix6,pix7,pix8,pix9:integer;
begin
xs:= fits.Fwidth;
ys:= fits.FHeight;
i := ys div num;
startline := id * i;
if id = (num - 1) then
endline := ys - 1
else
endline := (id + 1) * i - 1;
ii:=0; i1:=0; i2:=0; i3:=0;
// process the rows range for this thread
for i:=startline to endline do begin
if debayer then begin
ii:=ys-1-i; // image is flipped in fits, count color order from the bottom
i1:=max(i-1,0);
i2:=i;
i3:= min(i+1,ys-1);
end;
p := bgra.Scanline[i];
for j := 0 to xs-1 do begin
if fits.HeaderInfo.naxis=3 then begin
// 3 chanel color image
xx:=fits.image[0,i,j];
x:=round(max(0,min(MaxWord,(xx-FImgDmin) * c )) );
p^.red:=fits.GammaCorr(x);
xxg:=fits.image[1,i,j];
x:=round(max(0,min(MaxWord,(xxg-FImgDmin) * c )) );
p^.green:=fits.GammaCorr(x);
xxb:=fits.image[2,i,j];
x:=round(max(0,min(MaxWord,(xxb-FImgDmin) * c )) );
p^.blue:=fits.GammaCorr(x);
if fits.MarkOverflow then begin
if maxvalue([xx,xxg,xxb])>=overflow then
p^:=HighOverflow
else if minvalue([xx,xxg,xxb])<=underflow then
p^:=LowOverflow;
end;
end else begin
if debayer then begin
j1:=max(j-1,0);
j2:=j;
j3:=min(j+1,xs-1);
pix1:=round((fits.image[0,i1,j1]-FImgDmin) * c );
pix2:=round((fits.image[0,i1,j2]-FImgDmin) * c );
pix3:=round((fits.image[0,i1,j3]-FImgDmin) * c );
pix4:=round((fits.image[0,i2,j1]-FImgDmin) * c );
pix5:=round((fits.image[0,i2,j2]-FImgDmin) * c );
pix6:=round((fits.image[0,i2,j3]-FImgDmin) * c );
pix7:=round((fits.image[0,i3,j1]-FImgDmin) * c );
pix8:=round((fits.image[0,i3,j2]-FImgDmin) * c );
pix9:=round((fits.image[0,i3,j3]-FImgDmin) * c );
p^:=fits.BayerInterpolation(t,rmult,gmult,bmult,pix1,pix2,pix3,pix4,pix5,pix6,pix7,pix8,pix9,ii,j);
end else begin
// B/W image
xx:=fits.image[0,i,j];
x:=round(max(0,min(MaxWord,(xx-FImgDmin) * c )) );
p^.red:=fits.GammaCorr(x);
p^.green:=p^.red;
p^.blue:=p^.red;
if fits.MarkOverflow then begin
if xx>=overflow then
p^:=HighOverflow
else if xx<=underflow then
p^:=LowOverflow;
end;
end;
end;
p^.alpha:=255;
inc(p);
end;
end;
working := False;
end;
//////////////////// TGetExpThread /////////////////////////
constructor TGetExpThread.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
working := True;
end;
procedure TGetExpThread.Execute;
var
i, j, startline, endline, xs,ys: integer;
ii,i1,i2,i3,j1,j2,j3 : integer;
x : word;
xx,xxg,xxb: extended;
p: PExpandedPixel;
pix1,pix2,pix3,pix4,pix5,pix6,pix7,pix8,pix9:integer;
begin
xs:= fits.Fwidth;
ys:= fits.FHeight;
i := ys div num;
startline := id * i;
if id = (num - 1) then
endline := ys - 1
else
endline := (id + 1) * i - 1;
ii:=0; i1:=0; i2:=0; i3:=0;
// process the rows range for this thread
for i:=startline to endline do begin
if debayer then begin
ii:=ys-1-i; // image is flipped in fits, count color order from the bottom
i1:=max(i-1,0);
i2:=i;
i3:= min(i+1,ys-1);
end;
p := bgra.Scanline[i];
for j := 0 to xs-1 do begin
if fits.HeaderInfo.naxis=3 then begin
// 3 chanel color image
xx:=fits.imageMin+fits.image[0,i,j]/fits.imageC;
x:=round(max(0,min(MaxWord,xx)) );
p^.red:=x;
xxg:=fits.imageMin+fits.image[1,i,j]/fits.imageC;
x:=round(max(0,min(MaxWord,xxg)) );
p^.green:=x;
xxb:=fits.imageMin+fits.image[2,i,j]/fits.imageC;
x:=round(max(0,min(MaxWord,xxb)) );
p^.blue:=x;
end else begin
if debayer then begin
j1:=max(j-1,0);
j2:=j;
j3:=min(j+1,xs-1);
pix1:=round(fits.imageMin+fits.image[0,i1,j1]/fits.imageC);
pix2:=round(fits.imageMin+fits.image[0,i1,j2]/fits.imageC);
pix3:=round(fits.imageMin+fits.image[0,i1,j3]/fits.imageC);
pix4:=round(fits.imageMin+fits.image[0,i2,j1]/fits.imageC);
pix5:=round(fits.imageMin+fits.image[0,i2,j2]/fits.imageC);
pix6:=round(fits.imageMin+fits.image[0,i2,j3]/fits.imageC);
pix7:=round(fits.imageMin+fits.image[0,i3,j1]/fits.imageC);
pix8:=round(fits.imageMin+fits.image[0,i3,j2]/fits.imageC);
pix9:=round(fits.imageMin+fits.image[0,i3,j3]/fits.imageC);
p^:=fits.BayerInterpolationExp(t,rmult,gmult,bmult,pix1,pix2,pix3,pix4,pix5,pix6,pix7,pix8,pix9,ii,j);
end else begin
// B/W image
xx:=fits.imageMin+fits.image[0,i,j]/fits.imageC;
x:=round(max(0,min(MaxWord,xx)));
p^.red:=x;
p^.green:=x;
p^.blue:=x;
end;
end;
p^.alpha:=65535;
inc(p);
end;
end;
working := False;
end;
//////////////////// TGetStarList /////////////////////////
constructor TGetStarList.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := false;
inherited Create(CreateSuspended);
working := True;
end;
procedure TGetStarList.Execute;
var
i, j, starty, endy, ss, xs,ys: integer;
fitsX,fitsY,fx,fy,nhfd,size,marginx,marginy: integer;
hfd1,star_fwhm,vmax,bg,bgdev,xc,yc,snr,flux: double;
begin
xs:= fits.Fwidth;
ys:= fits.FHeight;
// step size
ss := ry div num div s;
marginx:=(xs-rx)div 2 div s;
marginy:=(ys-ry)div 2 div s;
// range for current thread
starty := marginy + id * ss;
if id=(num-1) then
endy := ((ys) div s)-marginy
else
endy := starty + ss;
nhfd:=0;{set counters at zero}
SetLength(StarList,1000);{allocate initial size}
// process the rows range for this thread
for fy:=starty to endy do { move test box with stepsize rs around}
begin
fitsY:=fy*s;
for fx:=marginx to ((xs) div s)-marginx do
begin
fitsX:=fx*s;
fits.GetHFD2(fitsX,fitsY,s+overlap,xc,yc,bg,bgdev,hfd1,star_fwhm,vmax,snr,flux,false);{2018-3-21, calculate HFD}
{scale the result as GetHFD2 work with internal 16 bit values}