-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
1109 lines (1003 loc) · 47.7 KB
/
MainForm.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using AstroPhoto.LibRaw;
namespace MyAstroPhotoLibrary
{
public partial class MainForm : Form
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new MainForm() );
}
}
Session currentSession;
StackItem[] currentStack;
int currentStackIndex;
IEnumerable<StackItem> CurrentStack
{
get
{
foreach( var item in currentStack ) {
if( !item.IsExcluded ) {
yield return item;
}
}
}
}
IImage currentImage;
int zoomCenterX = -1;
int zoomCenterY = -1;
Rectangle currentZoomRect;
AstroPhoto.LibRaw.Instance libraw = new AstroPhoto.LibRaw.Instance();
Curve currentCurve = new LinearCurve();
RgbImage stackedZoom;
int stackedZoomCount;
Curve stackedZoomCurve = new LinearCurve();
RgbImage stackedImage;
int stackedImageCount;
Curve stackedImageCurve = new LinearCurve();
//PictureBoxOverlay pictureBoxOverlay;
public MainForm()
{
InitializeComponent();
pictureBox.Cursor = new Cursor( "CrossHair.cur" );
zoomPictureBox.Cursor = pictureBox.Cursor;
thumbnailsView.ItemContextMenu = thumbnailsContextMenu;
//pictureBoxOverlay = new PictureBoxOverlay( pictureBox );
//pictureBoxOverlay.AddBox( 0, new Rectangle( 100, 100, 200, 200 ),
// Color.DodgerBlue, false );
}
private void MainForm_Shown( object sender, EventArgs e )
{
fullScreen( true );
string rootPath = "";
if( Properties.Settings.Default.LibraryPath.Length > 0 ) {
rootPath = Properties.Settings.Default.LibraryPath;
}
if( !Directory.Exists( rootPath ) ) {
if( openFolderDialog( ref rootPath ) != DialogResult.OK ) {
Close();
return;
}
}
currentSession = new Session( rootPath );
thumbnailsView.OpenSession( currentSession );
}
private void MainForm_FormClosing( object sender, FormClosingEventArgs e )
{
Properties.Settings.Default.LibraryPath = currentSession.RootPath;
Properties.Settings.Default.Save();
}
private DialogResult openFolderDialog( ref string rootPath )
{
using( var dialog = new OpenFolderDialog() ) {
dialog.InitialFolder = rootPath;
var result = dialog.ShowDialog( this );
if( result == DialogResult.OK ) {
rootPath = dialog.Folder;
} else {
rootPath = "";
}
return result;
}
}
private void openFolder_Click( object sender, EventArgs e )
{
string rootPath = currentSession.RootPath;
if( openFolderDialog( ref rootPath ) == DialogResult.OK ) {
refreshButton.Enabled = false;
currentSession = new Session( rootPath );
thumbnailsView.OpenSession( currentSession );
}
}
private void refreshButton_Click( object sender, EventArgs e )
{
refreshButton.Enabled = false;
thumbnailsView.OpenSession( currentSession );
}
void thumbnailsView_LoadThumbnailsCompleted( object sender, EventArgs e )
{
refreshButton.Enabled = true;
}
private void createStack_Click( object sender, EventArgs e )
{
using( var inputDialog = new InputDialog( "New Stack", "Name:", "", null ) ) {
if( inputDialog.ShowDialog() == DialogResult.OK ) {
thumbnailsView.CreateStack( libraw, inputDialog.InputText );
}
}
}
void thumbnailsView_SelectionChanged( object sender, EventArgs e )
{
if( currentImage != null ) {
currentImage.Dispose();
currentImage = null;
}
if( stackedImage != null ) {
stackedImage.Dispose();
stackedImage = null;
}
if( stackedZoom != null ) {
stackedZoom.Dispose();
stackedZoom = null;
}
currentStack = null;
currentStackIndex = 0;
zoomCenterX = -1;
zoomCenterY = -1;
BgRnumericUpDown.Value = 0;
BgGnumericUpDown.Value = 0;
BgBnumericUpDown.Value = 0;
MaxVNumericUpDown.Value = 1024;
BgNumericUpDown.Value = 0;
KgNumericUpDown.Value = 135;
KbNumericUpDown.Value = 245;
gammaNumericUpDown.Value = 0;
satNumericUpDown.Value = 0;
recalcCurve();
hideStackPanel();
var start = DateTime.Now;
updateImageView();
System.Diagnostics.Trace.WriteLine( string.Format( "TIMER: {0} ms", ( DateTime.Now - start ).TotalMilliseconds ) );
stackButton.Enabled = thumbnailsView.SelectedImages.Any();
}
private void viewMode_Click( object sender, EventArgs e )
{
updateImageView();
}
private void prepareStack( string rawPath )
{
if( currentStack == null ) {
string stackFile = rawPath + "\\stack.txt";
if( File.Exists( stackFile ) ) {
var lines = File.ReadAllLines( stackFile );
int linesPerRecord = 0;
for( int i = 0; i < lines.Length; i++ ) {
if( lines[i].StartsWith( "#" ) ) {
linesPerRecord++;
continue;
}
currentStack = new StackItem[( lines.Length - i ) / linesPerRecord];
break;
}
for( int i = 0; i < currentStack.Length; i++ ) {
string fileName = lines[( i + 1 ) * linesPerRecord + 0];
string[] offset = lines[( i + 1 ) * linesPerRecord + 1].Split( ',' );
currentStack[i] = new StackItem()
{
FilePath = Path.Combine( rawPath, fileName ),
Offset = new Point( int.Parse( offset[0] ), int.Parse( offset[1] ) )
};
}
zoomCenterX = currentStack[0].Offset.X;
zoomCenterY = currentStack[0].Offset.Y;
} else {
var files = Directory.GetFiles( rawPath ).Where(
s => ".cfa;*.fit;.arw;.cr2".Contains(
Path.GetExtension( s ).ToLower() + ";" ) ).ToArray();
currentStack = new StackItem[files.Length];
for( int i = 0; i < files.Length; i++ ) {
currentStack[i] = new StackItem() { FilePath = files[i] };
}
}
}
}
private void updateImageView()
{
if( stackedImage == null ) {
if( currentImage == null ) {
var path = thumbnailsView.CurrentObjectPath;
if( path.Length > 0 ) {
if( File.Exists( path ) ) {
currentImage = ImageTools.LoadImage( libraw, path, currentSession );
currentStack = null;
positionLabel.Text = "...";
} else if( Directory.Exists( path ) ) {
string cfaPath = path + "\\CFA";
if( Directory.Exists( cfaPath ) ) {
prepareStack( cfaPath );
currentImage = new RawImageWrapper( libraw, currentStack[currentStackIndex].FilePath, currentSession );
positionLabel.Text = string.Format( "{0} of {1}", currentStackIndex + 1, currentStack.Length );
stackCheckBox.Visible = true;
stackSelectedCheckBox.Checked = !currentStack[currentStackIndex].IsExcluded;
stackSelectedCheckBox.Enabled = true;
} else {
string rawPath = path + "\\RAW";
if( Directory.Exists( rawPath ) ) {
prepareStack( rawPath );
currentImage = new RawImageWrapper( libraw, currentStack[currentStackIndex].FilePath, currentSession );
positionLabel.Text = string.Format( "{0} of {1}", currentStackIndex + 1, currentStack.Length );
stackCheckBox.Visible = true;
stackSelectedCheckBox.Checked = !currentStack[currentStackIndex].IsExcluded;
stackSelectedCheckBox.Enabled = true;
}
}
}
} else {
stackSelectedCheckBox.Enabled = true;
}
}
if( currentImage != null ) {
if( quickPreviewRadioButton.Checked ) {
pictureBox.Image = currentImage.Preview;
showZoomImage();
} else if( rawPreviewRadioButton.Checked ) {
RawImage rawImage = currentImage.RawImage;
if( rawImage != null ) {
int saturation = (int) satNumericUpDown.Value;
pictureBox.Image = rawImage.RenderBitmapHalfRes( currentCurve, saturation );
showZoomImage();
} else {
pictureBox.Image = null;
}
}
if( infoRadioButton.Checked ) {
RawImage rawImage = currentImage.RawImage;
if( rawImage != null ) {
pictureBox.Image = rawImage.GetHistogram();
showZoomImage();
} else {
pictureBox.Image = null;
}
}
} else {
pictureBox.Image = null;
}
updateStackView();
stackCheckBox.Visible = currentStack != null;
prevButton.Enabled = ( currentStack != null && currentStackIndex > 0 );
nextButton.Enabled = ( currentStack != null && currentStackIndex < currentStack.Length - 1 );
} else {
if( pictureBox.Image != null ) {
pictureBox.Image.Dispose();
}
int saturation = (int) satNumericUpDown.Value;
pictureBox.Image = stackedImage.RenderBitmap( stackedImageCurve, saturation );
showZoomImage();
}
}
private void showZoomImage()
{
if( noZoomRadioButton.Checked ) {
zoomPanel.Visible = false;
} else if( currentImage != null ) {
Size rawSize = currentImage.RawSize;
int scale = 1;
if( zoom2xRadioButton.Checked ) scale = 2;
if( zoom4xRadioButton.Checked ) scale = 4;
if( zoomCFARadioButton.Checked ) scale = 4;
int X = zoomCenterX;
int Y = zoomCenterY;
if( X == -1 ) {
X = rawSize.Width / 2;
}
if( Y == -1 ) {
Y = rawSize.Height / 2;
}
int width = zoomPictureBox.ClientSize.Width;
int height = zoomPictureBox.ClientSize.Height;
int W = width / scale;
int H = height / scale;
if( W % 2 == 1 ) W += 1;
if( H % 2 == 1 ) H += 1;
currentZoomRect = new Rectangle( X - W / 2, Y - H / 2, W, H );
width = W * scale;
height = H * scale;
var image = new Bitmap( width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
using( Graphics g = Graphics.FromImage( image ) ) {
if( stackedZoom != null ) {
int saturation = (int) satNumericUpDown.Value;
using( var renderedImage = stackedZoom.RenderBitmap( stackedZoomCurve, saturation ) ) {
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage( renderedImage,
new Rectangle( 0, 0, width, height ),
new Rectangle( 0, 0, renderedImage.Width, renderedImage.Height ),
GraphicsUnit.Pixel );
}
} else if( zoomCFARadioButton.Checked ) {
var rawImage = currentImage.RawImage;
using( var extractedImage = rawImage.RenderCFA( currentZoomRect, currentCurve ) ) {
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage( extractedImage,
new Rectangle( 0, 0, width, height ),
new Rectangle( 0, 0, extractedImage.Width, extractedImage.Height ),
GraphicsUnit.Pixel );
}
} else if( rawPreviewRadioButton.Checked ) {
var rawImage = currentImage.RawImage;
int saturation = (int)satNumericUpDown.Value;
using( var extractedImage = rawImage.RenderBitmap( currentZoomRect, currentCurve, saturation ) ) {
//using( var gx = Graphics.FromImage( extractedImage ) ) {
/*gx.DrawLine( Pens.Magenta,
1,
1,
currentZoomRect.Width - 1,
currentZoomRect.Height - 1);
gx.DrawLine( Pens.Magenta,
currentZoomRect.Width - 1,
1,
1,
currentZoomRect.Height - 1);*/
/*gx.DrawLine( Pens.Lime,
currentZoomRect.Width / 2 - 1,
currentZoomRect.Height / 2,
currentZoomRect.Width / 2 + 1,
currentZoomRect.Height / 2);
gx.DrawLine( Pens.Lime,
currentZoomRect.Width / 2,
currentZoomRect.Height / 2 - 1,
currentZoomRect.Width / 2,
currentZoomRect.Height / 2 + 1 );*/
/*gx.FillRectangle( Brushes.Lime,
currentZoomRect.Width / 2,
currentZoomRect.Height / 2,
1,
1 );*/
//}
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage( extractedImage,
new Rectangle( 0, 0, width, height ),
new Rectangle( 0, 0, extractedImage.Width, extractedImage.Height ),
GraphicsUnit.Pixel );
}
} else {
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
var srcImage = currentImage.Preview;
var srcSize = srcImage.Size;
X = ( X * srcSize.Width ) / rawSize.Width;
Y = ( Y * srcSize.Width ) / rawSize.Width;
W = ( W * srcSize.Width ) / rawSize.Width;
H = ( H * srcSize.Width ) / rawSize.Width;
var srcRect = new Rectangle( X - W / 2, Y - H / 2, W, H );
g.DrawImage( srcImage, new Rectangle( 0, 0, width, height ), srcRect, GraphicsUnit.Pixel );
}
}
if( zoomPictureBox.Image != null ) {
zoomPictureBox.Image.Dispose();
}
zoomPictureBox.Image = image;
zoomPanel.Visible = true;
}
}
private void showZoomAt( Point e )
{
if( stackedZoom != null ) {
stackedZoom.Dispose();
stackedZoom = null;
}
if( pictureBox.Image == null ) {
return;
}
if( noZoomRadioButton.Checked ) {
zoom1xRadioButton.Checked = true;
}
var clientSize = pictureBox.ClientSize;
var rawSize = currentImage.RawSize;
int dX = 0;
int dY = 0;
float scaleX = ( 1.0f * rawSize.Width ) / clientSize.Width;
float scaleY = ( 1.0f * rawSize.Height ) / clientSize.Height;
float scale = 1;
if( scaleX > scaleY ) {
scale = scaleX;
dY = ( clientSize.Height - (int) ( rawSize.Height / scale ) ) / 2;
} else {
scale = scaleY;
dX = ( clientSize.Width - (int) ( rawSize.Width / scale ) ) / 2;
}
zoomCenterX = (int)( ( e.X - dX ) * scale );
zoomCenterY = (int)( ( e.Y - dY ) * scale );
showZoomImage();
}
private void zoomPictureBox_Click( object sender, EventArgs e )
{
if( stackedZoom != null ) {
stackedZoom.Dispose();
stackedZoom = null;
}
var p = zoomPictureBox.PointToClient( Control.MousePosition );
var clientSize = zoomPictureBox.ClientSize;
var imageSize = zoomPictureBox.Image.Size;
p.X += ( clientSize.Width - imageSize.Width ) / 2;
p.Y += ( clientSize.Height - imageSize.Height ) / 2;
int dX = ( ( p.X - imageSize.Width / 2 ) * currentZoomRect.Width ) / imageSize.Width;
int dY = ( ( p.Y - imageSize.Height / 2 ) * currentZoomRect.Height ) / imageSize.Height;
zoomCenterX += dX;
zoomCenterY += dY;
/*if( quickPreviewRadioButton.Checked ) {
zoomCenterX = (int) System.Math.Round( newCenter.X );
zoomCenterY = (int) System.Math.Round( newCenter.Y );
label1.Text = newCenter.ToString() + " - " + ImageTools.StarSigma( currentImage, newCenter ).ToString();
} else */
bool refineCenter = ( Control.ModifierKeys & Keys.Shift ) != 0 && rawPreviewRadioButton.Checked;
showZoomImage( refineCenter, true );
}
void showZoomImage( bool refineCenter, bool centerCursor )
{
if( refineCenter ) {
Point newCenter = ImageTools.RefineCenter( currentImage.RawImage,
new Point( zoomCenterX, zoomCenterY ), currentZoomRect.Size );
zoomCenterX = newCenter.X;
zoomCenterY = newCenter.Y;
}
showZoomImage();
if( centerCursor ) {
var clientSize = zoomPictureBox.ClientSize;
Cursor.Position = zoomPictureBox.PointToScreen( new Point( clientSize.Width / 2, clientSize.Width / 2 ) );
}
}
private void zoomRadioButton_CheckedChanged( object sender, EventArgs e )
{
showZoomImage();
}
private void nextButton_Click( object sender, EventArgs e )
{
if( currentStackIndex < currentStack.Length - 1 ) {
currentStackIndex++;
if( currentImage != null ) {
currentImage.Dispose();
currentImage = null;
}
if( zoomCenterX != -1 && currentStack[currentStackIndex].Offset.X != 0 ) {
zoomCenterX += currentStack[currentStackIndex].Offset.X - currentStack[currentStackIndex - 1].Offset.X;
zoomCenterY += currentStack[currentStackIndex].Offset.Y - currentStack[currentStackIndex - 1].Offset.Y;
}
updateImageView();
}
}
private void prevButton_Click( object sender, EventArgs e )
{
if( currentStackIndex > 0 ) {
currentStackIndex--;
if( currentImage != null ) {
currentImage.Dispose();
currentImage = null;
}
if( zoomCenterX != -1 ) {
zoomCenterX += currentStack[currentStackIndex].Offset.X - currentStack[currentStackIndex + 1].Offset.X;
zoomCenterY += currentStack[currentStackIndex].Offset.Y - currentStack[currentStackIndex + 1].Offset.Y;
}
updateImageView();
}
}
private void stackSelectedCheckBox_CheckedChanged( object sender, EventArgs e )
{
if( stackSelectedCheckBox.Enabled ) {
currentStack[currentStackIndex].IsExcluded = !stackSelectedCheckBox.Checked;
}
}
private void pictureBox_MouseDown( object sender, MouseEventArgs e )
{
if( e.Button == MouseButtons.Left ) {
var start = DateTime.Now;
showZoomAt( pictureBox.PointToClient( Control.MousePosition ) );
System.Diagnostics.Trace.WriteLine( string.Format( "TIMER ZOOM: {0} ms", ( DateTime.Now - start ).TotalMilliseconds ) );
if( ( Control.ModifierKeys & Keys.Shift ) != 0 ) {
var size = zoomPictureBox.ClientSize;
Cursor.Position = zoomPictureBox.PointToScreen( new Point( size.Width / 2, size.Height / 2 ) );
}
}
}
void updateStackView()
{
if( currentStack != null ) {
if( stackPanel.Visible ) {
if( stackFlowPanel.Controls.Count == 0 ) {
for( int i = 0; i < currentStack.Length; i++ ) {
var exPanel = new Panel()
{
Height = 20,
Width = 253,
Padding = new Padding( 1 ),
Margin = new Padding( 0 ),
BackColor = stackFlowPanel.BackColor,
Tag = i
};
exPanel.Click += new EventHandler( stackItem_Click );
var panel = new Panel()
{
Dock = DockStyle.Fill,
BackColor = stackFlowPanel.BackColor,
Tag = i
};
panel.Click += new EventHandler( stackItem_Click );
var label = new Label()
{
Text = Path.GetFileName( currentStack[i].FilePath ),
AutoSize = true,
Tag = i
};
label.Click += new EventHandler( stackItem_Click );
panel.Controls.Add( label );
exPanel.Controls.Add( panel );
stackFlowPanel.Controls.Add( exPanel );
}
}
foreach( Control control in stackFlowPanel.Controls ) {
int index = (int) ( control.Tag );
if( index == currentStackIndex ) {
control.BackColor = Color.DodgerBlue;
} else {
control.BackColor = stackFlowPanel.BackColor;
}
}
}
} else {
hideStackPanel();
}
}
void hideStackPanel()
{
stackFlowPanel.Controls.Clear();
stackCheckBox.Checked = false;
stackPanel.Visible = false;
}
void stackItem_Click( object sender, EventArgs e )
{
currentStackIndex = (int)( ( (Control)sender ).Tag );
updateStackView();
if( currentImage != null ) {
currentImage.Dispose();
currentImage = null;
}
viewMode_Click( null, EventArgs.Empty );
}
private void stackCheckBox_CheckedChanged( object sender, EventArgs e )
{
stackPanel.Visible = stackCheckBox.Checked;
if( stackPanel.Visible ) {
updateStackView();
} else {
stackFlowPanel.Controls.Clear();
}
}
private void saveCurrentStack()
{
var lines = new System.Collections.Generic.List<string>();
lines.Add( "#FILENAME" );
lines.Add( "#OFFSET" );
foreach( var stackItem in currentStack ) {
lines.Add( Path.GetFileName( stackItem.FilePath ) );
lines.Add( string.Format( "{0}, {1}", stackItem.Offset.X, stackItem.Offset.Y ) );
}
string rawPath = Path.GetDirectoryName( currentStack[0].FilePath );
string stackFile = rawPath + "\\stack.txt";
File.WriteAllLines( stackFile, lines.ToArray() );
}
private void imageInfo_Click( object sender, EventArgs e )
{
MessageBox.Show( currentImage.RawImage.ImageInfo );
}
private void exportToFITS_Click( object sender, EventArgs e )
{
foreach( var item in currentStack ) {
var fileName = Path.GetFileNameWithoutExtension( item.FilePath );
Export.ToFITS( libraw, item.FilePath, string.Format( "C:\\IRIS_TEMP\\{0}.fits", fileName ) );
}
}
private void exportToIRIS_Click( object sender, EventArgs e )
{
Export.ToIris( libraw, currentStack );
}
private void MainForm_KeyDown( object sender, KeyEventArgs e )
{
if( e.Control ) {
switch( e.KeyCode ) {
case Keys.A:
if( currentStack != null ) {
currentStack[currentStackIndex].Offset = new Point( zoomCenterX, zoomCenterY );
saveCurrentStack();
}
while( nextButton.Enabled ) {
var prevRaw = currentImage.RawImage.GetRawPixels();
var prevCenter = new Point( ( ( zoomCenterX ) / 2 ) * 2, ( ( zoomCenterY ) / 2 ) * 2 );
nextButton_Click( sender, e );
showZoomImage( true, true );
Application.DoEvents();
if( e.Shift ) {
double correlation = ImageTools.CalcCorrelation( currentImage.RawImage, prevRaw,
new Point( ( zoomCenterX / 2 ) * 2, ( zoomCenterY / 2 ) * 2 ), prevCenter, currentZoomRect.Size );
System.Diagnostics.Trace.WriteLine( correlation.ToString() );
if( correlation < 0.95 ) {
break;
} else {
currentStack[currentStackIndex].Offset = new Point( zoomCenterX, zoomCenterY );
saveCurrentStack();
}
} else {
break;
}
}
e.Handled = true;
break;
case Keys.X:
stackSelectedCheckBox.Checked = false;
if( nextButton.Enabled ) {
nextButton_Click( sender, e );
}
e.Handled = true;
break;
case Keys.Z:
{
var width = zoomPanel.Width;
var height = zoomPanel.Height;
if( e.Shift ) {
height /= 2;
width /= 2;
if( height < 448 ) {
noZoomRadioButton.Checked = true;
} else {
zoomPanel.Top += height;
zoomPanel.Height -= height;
zoomPanel.Left += width;
zoomPanel.Width -= width;
}
} else {
if( currentImage != null && !zoomPanel.Visible ) {
zoom1xRadioButton.Checked = true;
} else {
if( height < 448 * 2 ) {
zoomPanel.Top -= height;
zoomPanel.Height += height;
zoomPanel.Left -= width;
zoomPanel.Width += width;
}
}
}
showZoomImage();
}
e.Handled = true;
break;
case Keys.Right:
if( nextButton.Enabled ) {
nextButton_Click( sender, e );
}
e.Handled = true;
break;
case Keys.Left:
if( prevButton.Enabled ) {
prevButton_Click( sender, e );
}
e.Handled = true;
break;
case Keys.F:
mainPictureFullScreen( !exitFullScreenButton.Visible );
e.Handled = true;
break;
case Keys.Delete:
if( currentImage != null ) {
if( currentStack != null ) {
if( currentStackIndex >= 0 ) {
// Удаляем картинку из стека
removeFileFromStack();
}
if( currentStackIndex == -1 ) {
// Удаляем стек
}
} else {
// Удаляем несгруппированную картинку
}
}
break;
}
}
}
private void removeFileFromStack()
{
int deleteIndex = currentStackIndex;
if( currentStackIndex < currentStack.Length - 1 ) {
nextButton_Click( null, EventArgs.Empty );
} else {
prevButton_Click( null, EventArgs.Empty );
}
var filePath = currentStack[deleteIndex].FilePath;
var fileName = Path.GetFileName( filePath );
var dirName = Path.GetDirectoryName( filePath );
var _dirName = Path.GetDirectoryName( dirName );
var trashDirName = _dirName + "\\TRASH";
if( !Directory.Exists( trashDirName ) ) {
Directory.CreateDirectory( trashDirName );
}
File.Move( filePath, Path.Combine( trashDirName, fileName ) );
var newStack = new StackItem[currentStack.Length - 1];
int j = 0;
for( int i = 0; i < currentStack.Length; i++ ) {
if( i != deleteIndex ) {
newStack[j] = currentStack[i];
j++;
}
}
currentStack = newStack;
if( currentStack.Length > 0 ) {
if( currentStackIndex >= deleteIndex ) {
currentStackIndex--;
}
saveCurrentStack();
} else {
currentStack = null;
currentStackIndex = -1;
currentImage.Dispose();
currentImage = null;
Directory.Move( dirName, Path.Combine( trashDirName, Path.GetFileName( dirName ) ) );
positionLabel.Text = "...";
}
updateImageView();
}
void check( bool condition, string message )
{
if( !condition ) {
throw new Exception( message );
}
}
private void recalcCurve()
{
( (LinearCurve) currentCurve ).SetKg( (int) KgNumericUpDown.Value );
( (LinearCurve) currentCurve ).SetKb( (int) KbNumericUpDown.Value );
( (LinearCurve) currentCurve ).SetBg( (int) BgNumericUpDown.Value );
( (LinearCurve) currentCurve ).SetBgRGB( (int) BgRnumericUpDown.Value,
(int) BgGnumericUpDown.Value, (int) BgBnumericUpDown.Value );
( (LinearCurve) currentCurve ).SetMaxV( (int) MaxVNumericUpDown.Value );
( (LinearCurve) currentCurve ).SetGamma( (int) gammaNumericUpDown.Value );
( (LinearCurve) stackedZoomCurve ).SetKg( (int) KgNumericUpDown.Value );
( (LinearCurve) stackedZoomCurve ).SetKb( (int) KbNumericUpDown.Value );
( (LinearCurve) stackedZoomCurve ).SetBg( (int) ( stackedZoomCount * BgNumericUpDown.Value ) );
( (LinearCurve) stackedZoomCurve ).SetBgRGB( (int) ( stackedZoomCount * BgRnumericUpDown.Value ),
(int) ( stackedZoomCount * BgGnumericUpDown.Value), (int) ( stackedZoomCount * BgBnumericUpDown.Value ) );
( (LinearCurve) stackedZoomCurve ).SetMaxV( stackedZoomCount * (int) MaxVNumericUpDown.Value );
( (LinearCurve) stackedZoomCurve ).SetGamma( (int) gammaNumericUpDown.Value );
( (LinearCurve) stackedImageCurve ).SetKg( (int) KgNumericUpDown.Value );
( (LinearCurve) stackedImageCurve ).SetKb( (int) KbNumericUpDown.Value );
( (LinearCurve) stackedImageCurve ).SetBg( (int) ( stackedImageCount * BgNumericUpDown.Value ) );
( (LinearCurve) stackedImageCurve ).SetBgRGB( (int) ( stackedImageCount * BgRnumericUpDown.Value ),
(int) ( stackedImageCount * BgGnumericUpDown.Value ), (int) ( stackedImageCount * BgBnumericUpDown.Value ) );
( (LinearCurve) stackedImageCurve ).SetMaxV( stackedImageCount * (int) MaxVNumericUpDown.Value );
( (LinearCurve) stackedImageCurve ).SetGamma( (int) gammaNumericUpDown.Value );
}
private void updateCurve()
{
recalcCurve();
updateImageView();
}
private void curve_Changed( object sender, EventArgs e )
{
MaxVNumericUpDown.Increment = MaxVNumericUpDown.Value / 5;
updateCurve();
}
private void satNumericUpDown_ValueChanged( object sender, EventArgs e )
{
updateImageView();
}
private void fullScreen( bool fullScreen )
{
if( fullScreen ) {
//this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
} else {
this.FormBorderStyle = FormBorderStyle.Sizable;
//this.TopMost = false;
}
}
private void goFullScreen_Click( object sender, EventArgs e )
{
if( this.FormBorderStyle != FormBorderStyle.None ) {
fullScreen( true );
} else {
fullScreen( false );
}
}
private void close_Click( object sender, EventArgs e )
{
Close();
}
private void toolsMenu_Click( object sender, EventArgs e )
{
toolsContextMenu.Show( toolsMenuButton, 0, toolsMenuButton.Height );
}
private System.Drawing.Imaging.ImageCodecInfo getEncoder( System.Drawing.Imaging.ImageFormat format )
{
foreach( var codec in System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders() ) {
if( codec.FormatID == format.Guid ) {
return codec;
}
}
return null;
}
private void saveImageOpenFolder( Image image, RgbImage rgbImage, string fileName )
{
using( var saveDialog = new SaveFileDialog() ) {
saveDialog.Filter = "PNG|*.png|PNG 16-bit|*.16-bit.png|BMP|*.bmp|JPEG|*.jpg";
saveDialog.FileName = fileName;
saveDialog.InitialDirectory = thumbnailsView.CurrentObjectPath;
if( saveDialog.ShowDialog() == DialogResult.OK ) {
fullScreen( false );
Application.DoEvents();
switch( Path.GetExtension( saveDialog.FileName ).ToLower() ) {
case ".bmp": {
var encoderParams = new System.Drawing.Imaging.EncoderParameters( 1 );
encoderParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(
System.Drawing.Imaging.Encoder.ColorDepth, 24 );
image.Save( saveDialog.FileName,
getEncoder( System.Drawing.Imaging.ImageFormat.Bmp ), encoderParams );
}
break;
case ".png":
if( saveDialog.FileName.ToLower().EndsWith( ".16-bit.png" ) ) {
Export.Save16BitPng( saveDialog.FileName, rgbImage.GetRgbPixels16(), rgbImage.Width, rgbImage.Height );
} else {
image.Save( saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Png );
}
break;
case ".jpg": {
var encoderParams = new System.Drawing.Imaging.EncoderParameters( 1 );
encoderParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 99L );
image.Save( saveDialog.FileName,
getEncoder( System.Drawing.Imaging.ImageFormat.Jpeg ), encoderParams );
};
break;
}
System.Diagnostics.Process.Start( thumbnailsView.CurrentObjectPath );
}
}
}
private void mainPictureFullScreen( bool fullScreen )
{
if( fullScreen ) {
splitContainer.Panel1Collapsed = true;
previewToolsPanel.Visible = false;
mainToolsPanel.Visible = false;
exitFullScreenButton.Visible = true;
fillPanel.BorderStyle = BorderStyle.None;
} else {
exitFullScreenButton.Visible = false;
mainToolsPanel.Visible = true;
previewToolsPanel.Visible = true;
splitContainer.Panel1Collapsed = false;
fillPanel.BorderStyle = BorderStyle.FixedSingle;
}
}
private void enterFullScreen_Click( object sender, EventArgs e )
{
mainPictureFullScreen( true );
}
private void exitFullScreen_Click( object sender, EventArgs e )
{
mainPictureFullScreen( false );
}
struct RawRegion {
public RawImage RawImage;
public Rectangle Rect;
public string FilePath;
}
private RawImage loadRaw( string filePath )
{
var ext = System.IO.Path.GetExtension( filePath ).ToLower();
if( ext == ".cfa" || ext == ".fit" ) {
return new RawImage( filePath );
}
return libraw.load_raw( filePath );
}
private IEnumerable<RawRegion> EnumerateRaw( Rectangle rect )
{
var currentOffset = currentStack[currentStackIndex].Offset;
rect.Offset( -currentOffset.X, -currentOffset.Y );
foreach( var item in currentStack ) {
if( !item.IsExcluded ) {
using( var rawImage = loadRaw( item.FilePath ) ) {
var _rect = rect;
_rect.Offset( item.Offset );
//rawImage.ApplyDark( currentSession.Dark );
/*if( currentSession.Flat != null ) {
rawImage.ApplyFlat( currentSession.Flat, 1 );
}*/
yield return new RawRegion(){
RawImage = rawImage,
Rect = _rect,