33using System ;
44using System . Collections . Generic ;
55using System . Diagnostics ;
6+ using System . Diagnostics . CodeAnalysis ;
67using System . Drawing ;
78using System . IO ;
89using System . Linq ;
@@ -57,6 +58,7 @@ public FullSearchResult(BaseSearchEngine src, Color color, string name, string u
5758 private FullSearchResult ( Color color , string name , string url , float ? similarity = null )
5859 : this ( null ! , color , name , url , similarity ) { }
5960
61+ #region Properties
6062
6163 /// <summary>
6264 /// Search engine
@@ -79,11 +81,11 @@ public override NConsoleFunction? AltFunction
7981 {
8082 return ( ) =>
8183 {
82- if ( ! ExtendedResults . Any ( ) ) {
84+ if ( ! Enumerable . Any < FullSearchResult > ( ExtendedResults ) ) {
8385 return null ;
8486 }
8587
86- NConsole . ReadOptions ( ExtendedResults ) ;
88+ NConsole . ReadOptions < FullSearchResult > ( ExtendedResults ) ;
8789
8890 return null ;
8991 } ;
@@ -206,6 +208,39 @@ public override NConsoleFunction ComboFunction
206208 /// </summary>
207209 public bool IsOriginal { get ; set ; }
208210
211+ [ CanBeNull ]
212+
213+ public string AspectRatio
214+ {
215+ get
216+ {
217+ if ( HasResolution ) {
218+
219+
220+ // ReSharper disable PossibleInvalidOperationException
221+
222+ var fraction = new Fraction ( Width . Value , Height . Value ) ;
223+
224+
225+ var fractionStr = fraction . ToString ( ) ;
226+
227+ if ( fractionStr == "1" ) {
228+ fractionStr = "1:1" ;
229+ }
230+
231+ string ? aspectRatio = fractionStr . Replace ( '/' , ':' ) ;
232+
233+
234+ return aspectRatio ;
235+
236+ // ReSharper restore PossibleInvalidOperationException
237+
238+ }
239+
240+ return null ;
241+ }
242+ }
243+
209244
210245 /// <inheritdoc cref="ISearchResult.Description" />
211246 public string ? Description { get ; set ; }
@@ -234,10 +269,32 @@ public override NConsoleFunction ComboFunction
234269 /// <inheritdoc cref="ISearchResult.Characters" />
235270 public string ? Characters { get ; set ; }
236271
237- /// <inheritdoc cref="ISearchResult.SiteName" />
238- public string ? SiteName { get ; set ; }
272+ /// <inheritdoc cref="ISearchResult.Site" />
273+ public string ? Site { get ; set ; }
274+
275+ public DateTime ? Date { get ; set ; }
276+ public bool HasResolution => Width . HasValue && Height . HasValue ;
277+
278+ public float ? PixelResolution
279+ {
280+ get
281+ {
282+ if ( HasResolution ) {
283+ // ReSharper disable PossibleInvalidOperationException
284+
285+ float mpx = ( float ) MathHelper . ConvertToUnit ( Width . Value * Height . Value , MetricUnit . Mega ) ;
239286
240- public DateTime ? Date { get ; set ; }
287+
288+ return mpx ;
289+
290+ // ReSharper restore PossibleInvalidOperationException
291+ }
292+
293+ return null ;
294+ }
295+ }
296+
297+ #endregion
241298
242299 public void AddErrorMessage ( string msg )
243300 {
@@ -306,19 +363,22 @@ public override string ToString()
306363
307364 AppendResultInfo ( sb , nameof ( Similarity ) , $ "{ Similarity / 100 : P} ", Similarity . HasValue && ! IsOriginal ) ;
308365
366+ AppendResultInfo ( sb , "Resolution" ,
367+ $ "{ Width } x{ Height } ({ AspectRatio } ) ({ PixelResolution : F} MP)", HasResolution ) ;
368+
309369 AppendResultInfo ( sb , nameof ( Artist ) , Artist ) ;
310370 AppendResultInfo ( sb , nameof ( Characters ) , Characters ) ;
311371 AppendResultInfo ( sb , nameof ( Source ) , Source ) ;
312372 AppendResultInfo ( sb , nameof ( Description ) , Description ) ;
313- AppendResultInfo ( sb , " Site" , SiteName ) ;
373+ AppendResultInfo ( sb , nameof ( Site ) , Site ) ;
314374 AppendResultInfo ( sb , nameof ( Date ) , Date . ToString ( ) ) ;
315375
316- AppendResultInfo ( sb , "Resolution" , $ "{ Width } x{ Height } ", Width . HasValue && Height . HasValue ) ;
317-
376+
318377 foreach ( var ( key , value ) in Metadata ) {
319378 AppendResultInfo ( sb , key , value . ToString ( ) ) ;
320379 }
321380
381+
322382 return sb . ToString ( ) ;
323383 }
324384
@@ -355,7 +415,7 @@ public void UpdateFrom(ISearchResult result)
355415 Source = result . Source ;
356416 Characters = result . Characters ;
357417 Artist = result . Artist ;
358- SiteName = result . SiteName ;
418+ Site = result . Site ;
359419 Description = result . Description ;
360420 Date = result . Date ;
361421 }
@@ -370,7 +430,7 @@ private FullSearchResult CreateExtendedResult(ISearchResult result)
370430 Artist = result . Artist ,
371431 Source = result . Source ,
372432 Characters = result . Characters ,
373- SiteName = result . SiteName
433+ Site = result . Site
374434 } ;
375435 return extendedResult ;
376436 }
@@ -399,20 +459,33 @@ private IEnumerable<FullSearchResult> CreateExtendedResults(IReadOnlyList<ISearc
399459
400460 private const float MAX_SIMILARITY = 100.0f ;
401461
402- private void AddImageInfo ( ImageInputInfo info )
462+ /// <summary>
463+ /// Creates a <see cref="FullSearchResult" /> for the original image
464+ /// </summary>
465+ public static FullSearchResult GetOriginalImageResult ( ImageInputInfo info )
403466 {
404- //todo
467+ using var bmp = ( Bitmap ) Image . FromStream ( info . Stream ) ;
468+
469+ var result = new FullSearchResult ( Interface . ColorMisc2 , ORIGINAL_IMAGE_NAME , info . ImageUrl )
470+ {
471+ IsOriginal = true ,
472+ Similarity = MAX_SIMILARITY ,
473+ Width = bmp . Width ,
474+ Height = bmp . Height
475+
476+ } ;
477+
478+ /*
479+ *
480+ */
405481
406- Bitmap bmp ;
407482 string name ;
408483 FileFormatType fileFormat ;
409484 double bytes ;
410485
411486 if ( info . IsUrl ) {
412487 name = info . Value . ToString ( ) ;
413488
414- //using var netStream = Network.GetStreamFromUrl(info.ImageUrl);
415- bmp = ( Bitmap ) Image . FromStream ( info . Stream ) ;
416489
417490 info . Stream . Position = 0 ;
418491 using var ms = new MemoryStream ( ) ;
@@ -426,7 +499,6 @@ private void AddImageInfo(ImageInputInfo info)
426499
427500 fileFormat = FileSystem . ResolveFileType ( imageFile . FullName ) ;
428501
429- bmp = new Bitmap ( imageFile . FullName ) ;
430502 name = imageFile . Name ;
431503 bytes = FileSystem . GetFileSize ( imageFile . FullName ) ;
432504 }
@@ -436,44 +508,13 @@ private void AddImageInfo(ImageInputInfo info)
436508
437509 string imgSize = MathHelper . ConvertToUnit ( bytes ) ;
438510
439- ( int width , int height ) = ( bmp . Width , bmp . Height ) ;
440-
441- Width = width ;
442- Height = height ;
443-
444- double mpx = MathHelper . ConvertToUnit ( width * height , MetricUnit . Mega ) ;
445-
446-
447- var fraction = new Fraction ( width , height ) ;
448- var fractionStr = fraction . ToString ( ) ;
449-
450- if ( fractionStr == "1" ) {
451- fractionStr = "1:1" ;
452- }
453-
454- string ? aspectRatio = fractionStr . Replace ( '/' , ':' ) ;
455511
456512 string imageInfoStr = $ "{ name } ({ imgSize } )";
457513
458- string infoStr = $ "({ aspectRatio } ) ({ mpx : F} MP) ({ fileFormat . Name } )";
459-
460- Metadata . Add ( "Info" , imageInfoStr ) ;
461- Metadata . Add ( "Image" , infoStr ) ;
462- }
463-
464- /// <summary>
465- /// Creates a <see cref="FullSearchResult" /> for the original image
466- /// </summary>
467- public static FullSearchResult GetOriginalImageResult ( ImageInputInfo info )
468- {
469- var result = new FullSearchResult ( Interface . ColorMisc2 , ORIGINAL_IMAGE_NAME , info . ImageUrl )
470- {
471- IsOriginal = true ,
472- Similarity = MAX_SIMILARITY ,
473-
474- } ;
514+ string infoStr = $ "({ fileFormat . Name } )";
475515
476- result . AddImageInfo ( info ) ;
516+ result . Metadata . Add ( "Info" , imageInfoStr ) ;
517+ result . Metadata . Add ( "Image" , infoStr ) ;
477518
478519 return result ;
479520 }
0 commit comments