-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThumbnailsView.cs
379 lines (339 loc) · 15.8 KB
/
ThumbnailsView.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using AstroPhoto.LibRaw;
namespace MyAstroPhotoLibrary
{
public class ThumbnailsView : FlowLayoutPanel
{
BackgroundWorker backgroundWorker;
Session session;
string currentObjectPath = "";
List<string> selectedImages = new List<string>();
public IEnumerable<string> SelectedImages { get { return selectedImages; } }
public string CurrentObjectPath { get { return currentObjectPath; } }
public event EventHandler<EventArgs> LoadThumbnailsCompleted;
public event EventHandler<EventArgs> SelectionChanged;
public ContextMenuStrip ItemContextMenu;
public ThumbnailsView()
{
backgroundWorker = new BackgroundWorker()
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
backgroundWorker.DoWork += new DoWorkEventHandler( backgroundWorker_DoWork );
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler( backgroundWorker_ProgressChanged );
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
this.Click += new EventHandler( thumbnailsView_Click );
}
protected override void Dispose( bool disposing )
{
if( disposing ) {
backgroundWorker.Dispose();
}
base.Dispose( disposing );
}
public void OpenSession( Session _session )
{
if( backgroundWorker.IsBusy ) {
backgroundWorker.CancelAsync();
while( backgroundWorker.IsBusy ) {
System.Threading.Thread.Sleep( 1000 );
Application.DoEvents();
}
}
if( selectedImages.Count > 0 ) {
currentObjectPath = "";
selectedImages.Clear();
SelectionChanged( this, EventArgs.Empty );
}
Controls.Clear();
session = _session;
backgroundWorker.RunWorkerAsync();
}
public void CreateStack( AstroPhoto.LibRaw.Instance libraw, string name )
{
string dir = session.CreateStack( name, selectedImages );
SuspendLayout();
for( int i = Controls.Count - 1; i >= 0; i-- ) {
Control control = Controls[i];
Panel panel = (Panel) control;
string path = (string) panel.Tag;
if( selectedImages.Contains( path ) ) {
if( path == currentObjectPath ) {
panel.Tag = dir;
ThumbnailData data = loadThumbnailData( libraw, dir );
panel.Height = buildThumbnail( (Panel) ( panel.Controls[0] ), data ).Height;
} else {
Controls.Remove( control );
}
}
}
ResumeLayout();
selectedImages.Clear();
selectedImages.Add( dir );
currentObjectPath = dir;
updateThumbnails();
}
void addThumbnail( ThumbnailData data )
{
Panel exPanel = new Panel();
exPanel.Padding = new Padding( 2, 2, 2, 2 );
exPanel.Margin = new Padding( 1, 1, 1, 1 );
exPanel.Tag = data.path;
exPanel.Click += new EventHandler( thumbnail_Click );
exPanel.ContextMenuStrip = ItemContextMenu;
Panel panel = new Panel();
panel.Dock = DockStyle.Fill;
panel.BackColor = Color.Black;//Color.FromArgb( 44, 44, 44 );
panel.Tag = data.path;
panel.Click += new EventHandler( thumbnail_Click );
panel.ContextMenuStrip = ItemContextMenu;
var size = buildThumbnail( panel, data );
exPanel.Width = size.Width + 30;
exPanel.Height = size.Height + 4;
exPanel.Controls.Add( panel );
Controls.Add( exPanel );
}
Size buildThumbnail( Panel panel, ThumbnailData data )
{
panel.Controls.Clear();
int width = 120;
int height = 120;
var size = new Size( width, height );
if( data.image != null ) {
PictureBox pictureBox = new PictureBox();
pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox.Dock = DockStyle.Left;
pictureBox.Tag = data.path;
pictureBox.Click += new EventHandler( thumbnail_Click );
pictureBox.ContextMenuStrip = ItemContextMenu;
panel.Controls.Add( pictureBox );
Label label = new Label();
label.Dock = DockStyle.Bottom;
label.Width = width;
label.Tag = data.path;
label.TextAlign = ContentAlignment.MiddleLeft;
label.ForeColor = Color.Gray;
label.Click += new EventHandler( thumbnail_Click );
label.ContextMenuStrip = ItemContextMenu;
panel.Controls.Add( label );
pictureBox.Image = data.image;
label.Text = data.label;
label.Height = label.PreferredHeight;
size.Height = data.image.Height + 4 + label.Height;
} else {
Label label = new Label();
label.Dock = DockStyle.Left;
label.Text = data.label;
label.Width = width;
label.Height = height;
label.Tag = data.path;
label.TextAlign = ContentAlignment.MiddleCenter;
label.ForeColor = Color.LightGray;
label.BackColor = Color.DimGray;
label.Click += new EventHandler( thumbnail_Click );
label.ContextMenuStrip = ItemContextMenu;
panel.Controls.Add( label );
}
return size;
}
class ThumbnailData { public Image image; public string label; public string path; }
Queue<ThumbnailData> thumbnailsData = new Queue<ThumbnailData>();
private void backgroundWorker_DoWork( object sender, DoWorkEventArgs e )
{
using( var libraw = new AstroPhoto.LibRaw.Instance() ) {
var dirs = System.IO.Directory.GetDirectories( session.RootPath );
var files = System.IO.Directory.GetFiles( session.RootPath );
var thumbnailsToLoad = new string[dirs.Length + files.Length];
Array.Copy( dirs, 0, thumbnailsToLoad, 0, dirs.Length );
Array.Copy( files, 0, thumbnailsToLoad, dirs.Length, files.Length );
int count = 0;
int delta = 1;
int nextToReport = 0;
foreach( var path in thumbnailsToLoad ) {
if( System.IO.Directory.Exists( path ) ||
".arw;.cr2;.jpg;.png;.fit;".Contains( System.IO.Path.GetExtension( path ).ToLower() ) ) {
var thumbnailData = loadThumbnailData( libraw, path );
lock( thumbnailsData ) {
thumbnailsData.Enqueue( thumbnailData );
}
if( count == nextToReport ) {
backgroundWorker.ReportProgress( 100 * count / thumbnailsToLoad.Length );
if( delta < 5 ) {
delta = delta + 1;
}
nextToReport = count + delta;
}
count++;
}
if( backgroundWorker.CancellationPending ) {
return;
}
}
}
}
private static ThumbnailData loadThumbnailData( AstroPhoto.LibRaw.Instance libraw, string path )
{
var thumbnailData = new ThumbnailData();
thumbnailData.path = path;
string ext = "";
string[] stackFiles = null;
if( System.IO.Directory.Exists( path ) ) {
var rawDir = System.IO.Path.Combine( path, "RAW" );
if( System.IO.Directory.Exists( rawDir ) ) {
stackFiles = System.IO.Directory.GetFiles( rawDir, "*.arw" );
ext = ".arw";
if( stackFiles.Length == 0 ) {
stackFiles = System.IO.Directory.GetFiles( rawDir, "*.cr2" );
ext = ".cr2";
}
if( stackFiles.Length == 0 ) {
stackFiles = System.IO.Directory.GetFiles( rawDir, "*.cfa" );
ext = ".cfa";
}
if( stackFiles.Length == 0 ) {
stackFiles = System.IO.Directory.GetFiles( rawDir, "*.fit" );
ext = ".fit";
}
}
} else {
ext = System.IO.Path.GetExtension( path ).ToLower();
}
if( ext.Length > 0 ) {
if( ext == ".arw" || ext == ".cr2" ) {
string filePath = stackFiles == null ? path : stackFiles[0];
using( RawImage rawImage = libraw.load_thumbnail( filePath ) ) {
thumbnailData.image = createScaledImage( rawImage.Preview );
string shutter = rawImage.Shutter > 1 ? string.Format( "{0:0}s", rawImage.Shutter ) :
string.Format( "1/{0:0}s", 1 / rawImage.Shutter );
if( stackFiles == null ) {
thumbnailData.label = "ISO" + rawImage.IsoSpeed.ToString( "0" ) + " - " + shutter ;
} else {
thumbnailData.label = System.IO.Path.GetFileName( path ) +
"\nISO" + rawImage.IsoSpeed.ToString( "0" ) + " - " + shutter
+ ( stackFiles == null ? "" : " (" + stackFiles.Length + ")" );
}
}
} else if( ext == ".cfa" || ext == ".fit" ) {
string filePath = stackFiles == null ? path : stackFiles[0];
using( RawImage rawImage = new RawImage( filePath ) ) {
thumbnailData.image = createScaledImage( rawImage.Preview );
string shutter = rawImage.Shutter > 1 ? string.Format( "{0:0}s", rawImage.Shutter ) :
string.Format( "1/{0:0}s", 1 / rawImage.Shutter );
if( stackFiles == null ) {
thumbnailData.label = "ISO" + rawImage.IsoSpeed.ToString( "0" ) + " - " + shutter;
} else {
thumbnailData.label = System.IO.Path.GetFileName( path ) +
"\nISO" + rawImage.IsoSpeed.ToString( "0" ) + " - " + shutter
+ ( stackFiles == null ? "" : " (" + stackFiles.Length + ")" );
}
}
} else if( ext == ".jpg" || ext == ".png" ) {
using( Image preview = Image.FromFile( path ) ) {
thumbnailData.image = createScaledImage( preview );
}
thumbnailData.label = System.IO.Path.GetFileName( path );
}
} else {
thumbnailData.label = System.IO.Path.GetFileName( path );
}
return thumbnailData;
}
private static Image createScaledImage( Image original )
{
int width = 120;
int height = 120;
float scaleHeight = (float) original.Height / height;
float scaleWidth = (float) original.Width / width;
if( scaleHeight > scaleWidth ) {
width = (int) ( original.Width / scaleHeight );
} else {
height = (int) ( original.Height / scaleWidth );
}
Image image = new Bitmap( width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb );
using( Graphics g = Graphics.FromImage( image ) ) {
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage( original, 0, 0, image.Width, image.Height );
}
return image;
}
private void backgroundWorker_ProgressChanged( object sender, ProgressChangedEventArgs e )
{
SuspendLayout();
while( true ) {
ThumbnailData data = null;
lock( thumbnailsData ) {
if( thumbnailsData.Count > 0 ) {
data = thumbnailsData.Dequeue();
}
}
if( data == null ) {
break;
}
addThumbnail( data );
}
ResumeLayout();
}
private void backgroundWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
{
backgroundWorker_ProgressChanged( sender, null );
LoadThumbnailsCompleted( sender, EventArgs.Empty );
}
private void updateThumbnails()
{
foreach( var control in Controls ) {
Panel panel = (Panel) control;
string path = (string) panel.Tag;
if( path == currentObjectPath && selectedImages.Contains( path ) ) {
panel.BackColor = Color.DodgerBlue;
} else if( selectedImages.Contains( path ) ) {
panel.BackColor = Color.Gray;
} else {
panel.BackColor = panel.Parent.BackColor;
}
}
}
private void thumbnail_Click( object sender, EventArgs e )
{
string prevPath = currentObjectPath;
currentObjectPath = (string) ( (Control) sender ).Tag;
if( ( Control.ModifierKeys & Keys.Control ) == 0 ) {
selectedImages.Clear();
}
if( ( Control.ModifierKeys & Keys.Shift ) != 0 && prevPath.Length > 0 ) {
bool adding = false;
foreach( var control in Controls ) {
string path = (string) ( (Control) control ).Tag;
if( path == currentObjectPath || path == prevPath ) {
selectedImages.Add( path );
adding = !adding;
}
if( adding & !selectedImages.Contains( path ) ) {
selectedImages.Add( path );
}
}
} else if( ( Control.ModifierKeys & Keys.Control ) != 0 ) {
if( selectedImages.Contains( currentObjectPath ) ) {
selectedImages.Remove( currentObjectPath );
} else {
selectedImages.Add( currentObjectPath );
}
} else {
selectedImages.Add( currentObjectPath );
}
updateThumbnails();
SelectionChanged( sender, e );
}
private void thumbnailsView_Click( object sender, EventArgs e )
{
selectedImages.Clear();
currentObjectPath = "";
updateThumbnails();
SelectionChanged( sender, e );
}
}
}