@@ -27,7 +27,7 @@ namespace SmartImage.Searching
2727 /// </summary>
2828 /// <seealso cref="ISearchResult"/>
2929 /// <seealso cref="BasicSearchResult"/>
30- public sealed class FullSearchResult : NConsoleOption , ISearchResult
30+ public sealed class FullSearchResult : NConsoleOption , ISearchResult , IComparable < FullSearchResult >
3131 {
3232 private const string ORIGINAL_IMAGE_NAME = "(Original image)" ;
3333
@@ -250,6 +250,30 @@ public void AddExtendedResults(ISearchResult[] bestImages)
250250 ExtendedResults . AddRange ( CreateExtendedResults ( bestImages ) ) ;
251251 }
252252
253+ public int CompareTo ( FullSearchResult ? y )
254+ {
255+ float xSim = Similarity ?? 0 ;
256+ float ySim = y ? . Similarity ?? 0 ;
257+
258+ if ( xSim > ySim ) {
259+ return - 1 ;
260+ }
261+
262+ if ( xSim < ySim ) {
263+ return 1 ;
264+ }
265+
266+ if ( ExtendedResults . Count > y ? . ExtendedResults . Count ) {
267+ return - 1 ;
268+ }
269+
270+ if ( Metadata . Count > y ? . Metadata . Count ) {
271+ return - 1 ;
272+ }
273+
274+ return 0 ;
275+ }
276+
253277 public override string ToString ( )
254278 {
255279 var sb = new StringBuilder ( ) ;
@@ -317,7 +341,7 @@ private void AppendResultInfo(StringBuilder sb, string name, string? value, bool
317341
318342 string ? valueStr = value . AddColor ( newColor ) ;
319343 sb . Append ( $ "\t { Formatting . ANSI_RESET } { name } : { valueStr } { Formatting . ANSI_RESET } \n ") ;
320-
344+
321345 }
322346 }
323347
@@ -373,79 +397,85 @@ private IEnumerable<FullSearchResult> CreateExtendedResults(IReadOnlyList<ISearc
373397 return rg ;
374398 }
375399
376- public static int CompareResults ( FullSearchResult x , FullSearchResult y )
400+
401+ private const float MAX_SIMILARITY = 100.0f ;
402+
403+ private void AddImageInfo ( ImageInputInfo info )
377404 {
378- float xSim = x ? . Similarity ?? 0 ;
379- float ySim = y ? . Similarity ?? 0 ;
405+ //todo
380406
381- if ( xSim > ySim ) {
382- return - 1 ;
383- }
407+ Bitmap bmp ;
408+ string name ;
409+ FileFormatType fileFormat ;
410+ double bytes ;
384411
385- if ( xSim < ySim ) {
386- return 1 ;
387- }
412+ if ( info . IsUrl ) {
413+ name = info . Value . ToString ( ) ;
388414
389- if ( x ? . ExtendedResults . Count > y ? . ExtendedResults . Count ) {
390- return - 1 ;
391- }
415+ using var netStream = Network . GetStreamFromUrl ( info . ImageUrl ) ;
416+ bmp = ( Bitmap ) Image . FromStream ( netStream ) ;
392417
393- if ( x ? . Metadata . Count > y ? . Metadata . Count ) {
394- return - 1 ;
418+ netStream . Position = 0 ;
419+ using var ms = new MemoryStream ( ) ;
420+ netStream . CopyTo ( ms ) ;
421+ var rg = ms . ToArray ( ) ;
422+ fileFormat = FileSystem . ResolveFileType ( rg ) ;
423+ bytes = rg . Length ;
395424 }
425+ else if ( info . IsFile ) {
426+ var imageFile = ( FileInfo ) info . Value ;
396427
397- return 0 ;
398- }
428+ fileFormat = FileSystem . ResolveFileType ( imageFile . FullName ) ;
399429
400- private void AddFileInfo ( FileInfo imageFile )
401- {
402-
403- var fileFormat = FileSystem . ResolveFileType ( imageFile . FullName ) ;
430+ bmp = new Bitmap ( imageFile . FullName ) ;
431+ name = imageFile . Name ;
432+ bytes = FileSystem . GetFileSize ( imageFile . FullName ) ;
433+ }
434+ else {
435+ throw new SmartImageException ( ) ;
436+ }
404437
405- double fileSizeMegabytes =
406- MathHelper . ConvertToUnit ( FileSystem . GetFileSize ( imageFile . FullName ) , MetricUnit . Mega ) ;
438+ string fileSize = MathHelper . ConvertToUnit ( bytes ) ;
407439
408- ( int width , int height ) = Images . GetDimensions ( imageFile . FullName ) ;
440+ ( int width , int height ) = Images . GetDimensions ( bmp ) ;
409441
410442 Width = width ;
411443 Height = height ;
412444
413445 double mpx = MathHelper . ConvertToUnit ( width * height , MetricUnit . Mega ) ;
414446
415- string ? aspectRatio = new Fraction ( width , height ) . ToString ( ) . Replace ( '/' , ':' ) ;
416447
417- string infoStr =
418- $ "{ imageFile . Name } ({ aspectRatio } ) ({ mpx : F} MP) ({ fileSizeMegabytes : F} MB) ({ fileFormat . Name } )";
448+ var fraction = new Fraction ( width , height ) ;
449+ var fractionStr = fraction . ToString ( ) ;
450+
451+ if ( fractionStr == "1" ) {
452+ fractionStr = "1:1" ;
453+ }
454+
455+ string ? aspectRatio = fractionStr . Replace ( '/' , ':' ) ;
419456
457+ string fileInfoStr = $ "{ name } ({ fileSize } ) ({ fileFormat . Name } )";
458+
459+ string infoStr = $ "({ aspectRatio } ) ({ mpx : F} MP)";
460+
461+ Metadata . Add ( "File" , fileInfoStr ) ;
420462 Metadata . Add ( "Info" , infoStr ) ;
421463 }
422464
423- private const float MAX_SIMILARITY = 100.0f ;
424-
425465 /// <summary>
426466 /// Creates a <see cref="FullSearchResult" /> for the original image
427467 /// </summary>
428- public static FullSearchResult GetOriginalImageResult ( string imageUrl ,
429- [ CanBeNull ] FileInfo imageFile , [ CanBeNull ] string mimeType )
468+ public static FullSearchResult GetOriginalImageResult ( ImageInputInfo info )
430469 {
431- bool isFile = imageFile != null ;
432- string type = isFile ? "File" : "URI" ;
433-
434-
435- var result = new FullSearchResult ( Color . White , ORIGINAL_IMAGE_NAME + $ " ({ type } )", imageUrl )
470+ var result = new FullSearchResult ( Interface . ColorMisc2 , ORIGINAL_IMAGE_NAME , info . ImageUrl )
436471 {
437472 IsOriginal = true ,
438473 Similarity = MAX_SIMILARITY ,
439474 IsAnalyzed = true
440- } ;
441475
476+ } ;
442477
443- if ( mimeType != null ) {
444- result . Metadata . Add ( "Mime type" , $ "{ mimeType } ") ;
445- }
446- else if ( isFile ) {
447- result . AddFileInfo ( imageFile ) ;
448- }
478+ result . AddImageInfo ( info ) ;
449479
450480 return result ;
451481 }
@@ -461,5 +491,34 @@ public static ISearchResult[] FilterAndSelectBestImages(List<BasicSearchResult>
461491
462492 return best ;
463493 }
494+
495+ /// <summary>
496+ /// Handles result opening from priority engines and filtering
497+ /// </summary>
498+ public void HandlePriorityResult ( )
499+ {
500+ /*
501+ * Filtering is disabled
502+ * Open it anyway
503+ */
504+
505+ if ( ! SearchConfig . Config . FilterResults ) {
506+ Function ( ) ;
507+ return ;
508+ }
509+
510+ /*
511+ * Filtering is enabled
512+ * Determine if it passes the threshold
513+ */
514+
515+ if ( ! Filter ) {
516+ // Open result
517+ Function ( ) ;
518+ }
519+ else {
520+ Debug . WriteLine ( $ "Filtering result { Name } ") ;
521+ }
522+ }
464523 }
465524}
0 commit comments