From 3e46f1455486d2cde7efd6b791bb037e17554213 Mon Sep 17 00:00:00 2001 From: Nhowka Date: Thu, 19 Jan 2017 17:16:55 -0200 Subject: [PATCH 1/2] Single use of GetPixel per actual pixel (#1) GetPixel is a rather expensive function, so calling it once per pixel instead of once per pixel color is desirable. --- Initialize.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Initialize.cs b/Initialize.cs index 7d9cf5f..f09fa4c 100644 --- a/Initialize.cs +++ b/Initialize.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -101,9 +101,10 @@ private static double GetWeight(char c, SizeF size) { for (int j = 0; j < btm.Height; j++) { - totalsum = totalsum + (btm.GetPixel(i, j).R - + btm.GetPixel(i, j).G - + btm.GetPixel(i, j).B)/3; + Color pixel = btm.GetPixel(i, j); + totalsum = totalsum + (pixel.R + + pixel.G + + pixel.B)/3; } } // Weight = (sum of (R+G+B)/3 for all pixels in image) / Area. (Where Area = Width*Height ) From a008b550e6271e8d7c2a056e10be7c736a1dcd5b Mon Sep 17 00:00:00 2001 From: Nhowka Date: Thu, 19 Jan 2017 17:17:36 -0200 Subject: [PATCH 2/2] Single use of GetPixel per actual pixel (#2) GetPixel is a rather expensive function, so calling it once per pixel instead of once per pixel color is desirable. --- HelperMethods.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/HelperMethods.cs b/HelperMethods.cs index bae111e..5ab0338 100644 --- a/HelperMethods.cs +++ b/HelperMethods.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -47,9 +47,10 @@ public static Image Convert2ASCII(Image BW_Image, List characters, List RowText = new List { }; for (int i=0; i Math.Abs(t.Weight-targetvalue)==characters.Min(e => Math.Abs(e.Weight - targetvalue))).FirstOrDefault(); RowText.Add(closestchar.Character); @@ -138,8 +139,8 @@ public static Bitmap Grayscale(Image image) for (int j = 0; j < btm.Height; j++) { // Visit https://en.wikipedia.org/wiki/Grayscale for 0.3, 0.59 & 0.11 values - - int ser = (int)(btm.GetPixel(i, j).R*0.3 + btm.GetPixel(i, j).G*0.59 + btm.GetPixel(i, j).B*0.11); + Color pixel = btm.GetPixel(i, j); + int ser = (int)(pixel.R*0.3 + pixel.G*0.59 + pixel.B*0.11); btm.SetPixel(i, j, Color.FromArgb(ser, ser, ser)); } }