public void Resize(MagickImage input, Stream output, int width, int height, ResizeBehaviour resizeBehaviour, ProcessingBehaviour processingBehaviour) { switch (resizeBehaviour) { case ResizeBehaviour.MaintainAspectRatio: this.ResizeMaintainAspectRatio(input, output, width, height); break; case ResizeBehaviour.CropToAspectRatio: this.ResizeCropToAspectRatio(input, output, width, height); break; } if (processingBehaviour == ProcessingBehaviour.Darken) { input.Colorize(new MagickColor(0, 0, 0), new Percentage(60)); } else if (processingBehaviour == ProcessingBehaviour.Lighten) { input.Colorize(new MagickColor(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue), new Percentage(75)); input.Draw( new DrawableGravity(Gravity.Center), new DrawableFont("Arial"), new DrawableFillColor(Color.Black), new DrawablePointSize(28), new DrawableText(0, 0, "Preview")); } input.Write(output); }
public static void Erase(string path, List <PatternType> patterns, int scale, MagickColor color) { if (patterns.Count < 1) { return; } MagickImage img = IOUtils.ReadImage(path); if (img == null) { return; } PatternType chosenPattern = GetRandomPatternType(patterns); PreProcessing(path, "- Pattern: " + chosenPattern.ToString()); Random rand = new Random(); MagickImage patternImg = new MagickImage(GetInpaintImage(chosenPattern)); patternImg.FilterType = FilterType.Point; patternImg.Colorize(color, new Percentage(100)); patternImg.BackgroundColor = MagickColors.Transparent; patternImg.Rotate(RandRange(0, 360)); double geomW = img.Width * RandRange(1.0f, 2.0f) * GetScaleMultiplier(scale); double geomH = img.Height * RandRange(1.0f, 2.0f) * GetScaleMultiplier(scale); //Logger.Log("geomW: " + geomW + " - geomH: " + geomW); MagickGeometry upscaleGeom = new MagickGeometry(Math.Round(geomW) + "x" + Math.Round(geomH) + "!"); patternImg.Resize(upscaleGeom); patternImg.BitDepth(Channels.Alpha, 1); img.Composite(patternImg, Gravity.Center, CompositeOperator.Over); img.Write(path); PostProcessing(img, path, path); }
private MagickImage NormalsMap(MagickImage textLayer, string backgroundFile, bool previewMode) { var image = LoadImage(backgroundFile).Clone(); if (image.Width != textLayer.Width || image.Height != textLayer.Height) { image.Resize(textLayer.Width, textLayer.Height); } using (var summaryLayer = GetNormalsMapLayer(textLayer)) { if (!previewMode) { using (var blurredLayer = new MagickImage(summaryLayer)) { blurredLayer.Blur(12, 6); image.Composite(blurredLayer, 0, 0, CompositeOperator.Overlay); } using (var textFlatten = new MagickImage(textLayer)) { textFlatten.Colorize(new MagickColor("#8080ff"), new Percentage(100)); image.Composite(textFlatten, 0, 0, CompositeOperator.Over); } } image.Composite(summaryLayer, 0, 0, CompositeOperator.Overlay); if (!previewMode) { NormalizeNormalsMap(image, 1.0); } } return(image); }
public static ComparisonResult Compare(IMagickImage original, IMagickImage updated) { var compareResult = new ComparisonResult(); // https://github.com/dlemstra/Magick.NET/blob/9943055423b90ac35933dde602b3af9aab746a8a/Source/Magick.NET/Shared/Enums/ErrorMetric.cs // https://www.imagemagick.org/script/command-line-options.php#metric var compareSettings = new CompareSettings() { HighlightColor = MagickColors.White,//MagickColor.FromRgba(255, 20, 147, 255), LowlightColor = MagickColors.Black, Metric = ErrorMetric.Absolute }; using (IMagickImage originalMImage = new MagickImage(original)) { // Set Fuzzing Level originalMImage.ColorFuzz = new Percentage(8); compareResult.TotalPixels = originalMImage.Height * originalMImage.Width; using (IMagickImage diffMImage = new MagickImage()) { using (IMagickImage updatedMImage = new MagickImage(updated)) { compareResult.ChangedPixels = originalMImage.Compare(updatedMImage, compareSettings, diffMImage); } // flatten the colors diffMImage.AutoLevel(); // bold the diff, make the changes pop diffMImage.Morphology(new MorphologySettings { Kernel = Kernel.Octagon, KernelArguments = "5", Method = MorphologyMethod.Dilate }); // flip the colors diffMImage.Opaque(MagickColors.White, MagickColors.Red); diffMImage.Opaque(MagickColors.Black, MagickColors.Transparent); using (var origFadeImg = new MagickImage(original)) { origFadeImg.Colorize(MagickColors.White, new Percentage(75)); using (IMagickImageCollection collection = new MagickImageCollection(origFadeImg.ToByteArray())) { collection.Add(diffMImage); compareResult.DiffImage = new MagickImage(collection.Merge()); } } } } return(compareResult); }
private void TintFolder(string relativePath) { if (Globals.settings.tintEnabled) { foreach (string file in Directory.GetFiles(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), relativePath))) { using (MagickImage image = new MagickImage(file)) { image.Colorize(new MagickColor(Globals.settings.tintColor), new Percentage(100)); image.Write(file); } } } }
public static void GenerateLabel(string text = "Super horse Bros.", string fontName = @"C:\Windows\Fonts\comicbd.ttf") { MagickReadSettings settings = new MagickReadSettings(); settings.SetDefine(MagickFormat.Png, "size", "250x450"); using (var magick = new MagickImage()) { //magick.Settings.Page = new MagickGeometry(400, 175); // magick.RePage(); magick.Draw(new DrawableRectangle (new System.Drawing.Rectangle() { Width = 400, Height = 175 })); magick.Colorize(new MagickColor(System.Drawing.Color.Aqua), new Percentage(100)); // magick.Settings.FillColor = new MagickColor(System.Drawing.Color.DarkCyan); //magick.Settings.Font = fontName; // magick.Settings.FontPointsize = 72; // magick.Settings.StrokeWidth = 2; // magick.Settings.StrokeColor = new MagickColor(System.Drawing.Color.Chocolate); // magick.Settings.BackgroundColor.A = 0; // magick.Settings.TextGravity = Gravity.Center; // magick.Settings.StrokeAntiAlias = true; var clone = magick.Clone(); // clone.Shadow(0,0,1,new Percentage(100),new MagickColor(System.Drawing.Color.Green)); clone.RePage(); clone.Trim(); // var clone2 = clone.Clone(); //clone2.Trim(); clone.Write("preview.png"); } }
private void StartServer_Click(object sender, EventArgs e) { if (isServerUp) { hostg.Stop(); StartServer.Text = "Start Server"; isServerUp = false; } else { if (Globals.settings.tintEnabled) { foreach (string file in Directory.GetFiles(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Content/html/img"))) { using (MagickImage image = new MagickImage(file)) { image.Colorize(new MagickColor(Globals.settings.tintColor), new Percentage(100)); image.Write(file); } } } isServerUp = true; try { UrlLinkLabel.Text = "http://localhost:" + Globals.settings.serverPort + "/Content/html/bts-game.html"; HostConfiguration config = new HostConfiguration(); config.UrlReservations.CreateAutomatically = true; NancyHost host = new NancyHost(config, new Uri("http://localhost:" + Globals.settings.serverPort)); host.Start(); hostg = host; StartServer.Text = "Stop Server"; } catch (Exception ee) { Console.WriteLine(ee.Message); throw; } } }
public static void AnnotateWheel(string filePath, string wheelText, string fontName = @"C:\Windows\Fonts\fuddle.ttf", double fontPointSize = 36, string pattern = "Bricks", System.Drawing.Color fillColor = new System.Drawing.Color(), System.Drawing.Color strokeColor = new System.Drawing.Color(), double strokeWidth = 1.0, System.Drawing.Color shadowColor = new System.Drawing.Color(), int imageWidth = 400, int imageHeight = 175) { if (!IsFontInstalled(fontName)) { return; } fontName = fontName.Replace(" ", "-"); using (MagickImage image = new MagickImage(MagickColors.Transparent, imageWidth, imageHeight)) { image.Settings.Font = fontName; image.Settings.StrokeWidth = strokeWidth; image.Settings.StrokeColor = strokeColor; image.Settings.Density = new Density(72, 72); image.Settings.FontPointsize = fontPointSize; var newWheelText = wheelText.Replace(" ", "\n"); try { TypeMetric typeMetric = image.FontTypeMetrics(newWheelText); while (typeMetric.TextWidth < image.Width) { image.Settings.FontPointsize++; typeMetric = image.FontTypeMetrics(wheelText); } image.Settings.FontPointsize--; image.Settings.FillPattern = new MagickImage("pattern:" + pattern); image.Annotate(wheelText, new MagickGeometry(imageWidth, imageHeight), Gravity.Center); image.Shadow(shadowColor); using (var image2 = new MagickImage(image)) { image2.Colorize(fillColor, new Percentage(30)); image2.Settings.StrokeColor = strokeColor; image2.Settings.StrokeWidth = strokeWidth; image2.Write(filePath); } } catch (MagickException) { } } }
private void GenerateWildcardedPack() { int fileCount = fm.LoadFiles(CurrentPackPath, "png", SearchOption.AllDirectories); int uniqueHashIndex = 0; int wildcardedHashIndex = 0; switch (CurrentHashType) { case HashType.Texture: uniqueHashIndex = 3; wildcardedHashIndex = 2; break; case HashType.TLUT: uniqueHashIndex = 2; wildcardedHashIndex = 3; break; } textures.Clear(); for (int i = 0; i < fileCount; i++) { string hash = fm.fileInfos[i].Name.Split(new char[] { '_' }, StringSplitOptions.None)[uniqueHashIndex]; if (textures.ContainsKey(hash) == false) { textures.Add(hash, new List <FileInfo>()); } textures[hash].Add(fm.fileInfos[i]); } //Create a new directory for the wildcarded textures DirectoryInfo di = new DirectoryInfo(CurrentPackPath); di = di.Parent; string wildcardedPath = Path.Combine(di.FullName, "Wildcarded"); Trace.WriteLine(wildcardedPath); if (Directory.Exists(wildcardedPath)) { MessageBox.Show("Please delete the existing \"Wildcarded\" folder and try again.", "Existing Wildcarded Folder", MessageBoxButton.OK, MessageBoxImage.Error); Dispatcher.BeginInvoke(DispatcherPriority.Normal, new GenerationDoneDelegate(GenerationDone)); return; } DirectoryInfo wildcardedDirInfo = fm.CreateDirectory(wildcardedPath); StringBuilder nameSb = new StringBuilder(); //Copy the files and make sure they are named with a wildcard List <FileInfo>[] wildCardCandidates = textures.Values.ToArray(); for (int i = 0; i < wildCardCandidates.Length; i++) { FileInfo fi = wildCardCandidates[i][0]; //If the IgnoreUnique option is selected, copy the file but do not change its name. string filePath = ""; string subDirPath = ""; subDirPath = fi.FullName.Remove(0, CurrentPackPath.Length); subDirPath = subDirPath.Remove(subDirPath.Length - fi.Name.Length, fi.Name.Length); subDirPath = subDirPath.Remove(0, 1); subDirPath = Path.Combine(wildcardedPath, subDirPath); if (IgnoreUniques && wildCardCandidates[i].Count == 1) { filePath = Path.Combine(subDirPath, fi.Name); } else { string[] nameParts = fi.Name.Split(new char[] { '_' }, StringSplitOptions.None); nameParts[wildcardedHashIndex] = "$"; nameSb.Clear(); nameSb.Append(nameParts[0]); for (int j = 1; j < nameParts.Length; j++) { nameSb.Append("_"); nameSb.Append(nameParts[j]); } filePath = Path.Combine(subDirPath, nameSb.ToString()); Trace.WriteLine(filePath); } //Check if the file already exists if (File.Exists(filePath)) { MessageBox.Show(textures.Keys.ToArray()[i] + " - " + fi.Name + " - " + nameSb.ToString(), "Duplicate detected!", MessageBoxButton.OK, MessageBoxImage.Error); break; } fm.CreateDirectory(subDirPath); if (MarkTexture) { MagickImage magickImage = new MagickImage(fi); //magickImage.Negate(Channels.RGB); magickImage.Colorize(new MagickColor(0, 65535, 0, 0), new Percentage(25.0)); Bitmap bitmap = magickImage.ToBitmap(); bitmap.Save(filePath); magickImage.Dispose(); bitmap.Dispose(); } else { fi.CopyTo(filePath, false); } Dispatcher.BeginInvoke(DispatcherPriority.Normal, new GenerationProgressDelegate(() => GenerationProgress(i, wildCardCandidates.Length))); } Dispatcher.BeginInvoke(DispatcherPriority.Normal, new GenerationDoneDelegate(GenerationDone)); }