|
| 1 | +using ImageMagick; |
| 2 | +using Microsoft.UI.Xaml.Controls; |
| 3 | +using Windows.Storage; |
| 4 | + |
| 5 | +namespace Simple_Icon_File_Maker.Helpers; |
| 6 | + |
| 7 | +public static class ImageHelper |
| 8 | +{ |
| 9 | + public static async Task<MagickImage?> LoadImageAsync(string imagePath) |
| 10 | + { |
| 11 | + if (string.IsNullOrWhiteSpace(imagePath)) |
| 12 | + return null; |
| 13 | + |
| 14 | + try |
| 15 | + { |
| 16 | + MagickImage image; |
| 17 | + // For .ico files, load the largest frame instead of the first one |
| 18 | + if (Path.GetExtension(imagePath).Equals(".ico", StringComparison.InvariantCultureIgnoreCase)) |
| 19 | + { |
| 20 | + MagickImageCollection collection = new(imagePath); |
| 21 | + // Find the largest frame by area (width * height) |
| 22 | + MagickImage? largestFrame = collection.Cast<MagickImage>() |
| 23 | + .OrderByDescending(img => (int)img.Width * (int)img.Height) |
| 24 | + .FirstOrDefault(); |
| 25 | + |
| 26 | + if (largestFrame != null) |
| 27 | + { |
| 28 | + // Create a new image from the largest frame to avoid disposal issues |
| 29 | + image = (MagickImage)largestFrame.Clone(); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + // Fallback to the first frame if something goes wrong |
| 34 | + image = new(imagePath); |
| 35 | + } |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + image = new(imagePath); |
| 40 | + } |
| 41 | + |
| 42 | + // If the image is smaller than 512px, scale it up using NearestNeighbor |
| 43 | + // to maintain sharp pixels when displayed |
| 44 | + int smallerDimension = (int)Math.Min(image.Width, image.Height); |
| 45 | + if (smallerDimension is < 512 and > 0) |
| 46 | + { |
| 47 | + // Scale up to 512px using NearestNeighbor (point sampling) to keep pixels sharp |
| 48 | + int targetSize = 512; |
| 49 | + image.FilterType = FilterType.Point; // Point filter = NearestNeighbor |
| 50 | + image.Resize((uint)targetSize, (uint)targetSize); |
| 51 | + } |
| 52 | + |
| 53 | + return image; |
| 54 | + } |
| 55 | + catch |
| 56 | + { |
| 57 | + return null; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static int GetSmallerImageSide(string imagePath) |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + MagickImage image = new(imagePath); |
| 66 | + return (int)Math.Min(image.Width, image.Height); |
| 67 | + } |
| 68 | + catch |
| 69 | + { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static async Task<string> ApplyGrayscaleAsync(string imagePath, Image? displayImage = null) |
| 75 | + { |
| 76 | + StorageFolder sf = ApplicationData.Current.LocalCacheFolder; |
| 77 | + string fileName = Path.GetFileNameWithoutExtension(imagePath); |
| 78 | + string extension = Path.GetExtension(imagePath); |
| 79 | + string grayFilePath = Path.Combine(sf.Path, $"{fileName}_gray{extension}"); |
| 80 | + MagickImage image = new(imagePath); |
| 81 | + |
| 82 | + image.Grayscale(); |
| 83 | + await image.WriteAsync(grayFilePath); |
| 84 | + |
| 85 | + if (displayImage != null) |
| 86 | + displayImage.Source = image.ToImageSource(); |
| 87 | + |
| 88 | + return grayFilePath; |
| 89 | + } |
| 90 | + |
| 91 | + public static async Task<string> ApplyBlackWhiteOtsuAsync(string imagePath, Image? displayImage = null) |
| 92 | + { |
| 93 | + StorageFolder sf = ApplicationData.Current.LocalCacheFolder; |
| 94 | + string fileName = Path.GetFileNameWithoutExtension(imagePath); |
| 95 | + string extension = Path.GetExtension(imagePath); |
| 96 | + string bwFilePath = Path.Combine(sf.Path, $"{fileName}_bw{extension}"); |
| 97 | + MagickImage image = new(imagePath); |
| 98 | + |
| 99 | + image.Grayscale(); |
| 100 | + image.AutoThreshold(AutoThresholdMethod.OTSU); |
| 101 | + await image.WriteAsync(bwFilePath); |
| 102 | + |
| 103 | + if (displayImage != null) |
| 104 | + displayImage.Source = image.ToImageSource(); |
| 105 | + |
| 106 | + return bwFilePath; |
| 107 | + } |
| 108 | + |
| 109 | + public static async Task<string> ApplyBlackWhiteKapurAsync(string imagePath, Image? displayImage = null) |
| 110 | + { |
| 111 | + StorageFolder sf = ApplicationData.Current.LocalCacheFolder; |
| 112 | + string fileName = Path.GetFileNameWithoutExtension(imagePath); |
| 113 | + string extension = Path.GetExtension(imagePath); |
| 114 | + string bwkFilePath = Path.Combine(sf.Path, $"{fileName}_bwk{extension}"); |
| 115 | + MagickImage image = new(imagePath); |
| 116 | + |
| 117 | + image.Grayscale(); |
| 118 | + image.AutoThreshold(AutoThresholdMethod.Kapur); |
| 119 | + await image.WriteAsync(bwkFilePath); |
| 120 | + |
| 121 | + if (displayImage != null) |
| 122 | + displayImage.Source = image.ToImageSource(); |
| 123 | + |
| 124 | + return bwkFilePath; |
| 125 | + } |
| 126 | + |
| 127 | + public static async Task<string> ApplyInvertAsync(string imagePath, Image? displayImage = null) |
| 128 | + { |
| 129 | + StorageFolder sf = ApplicationData.Current.LocalCacheFolder; |
| 130 | + string fileName = Path.GetFileNameWithoutExtension(imagePath); |
| 131 | + string extension = Path.GetExtension(imagePath); |
| 132 | + string invFilePath = Path.Combine(sf.Path, $"{fileName}_inv{extension}"); |
| 133 | + MagickImage image = new(imagePath); |
| 134 | + |
| 135 | + image.Negate(Channels.RGB); |
| 136 | + await image.WriteAsync(invFilePath); |
| 137 | + |
| 138 | + if (displayImage != null) |
| 139 | + displayImage.Source = image.ToImageSource(); |
| 140 | + |
| 141 | + return invFilePath; |
| 142 | + } |
| 143 | +} |
0 commit comments