Advertisement
muu

beadscount

muu
Feb 18th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. /// <summary>
  2. /// This tool comes handy when buying beads, like Hama, Nabbi, Ikea Pyssla and so on.
  3. /// It takes a pixel art image as argument and it outputs how many colors do you need
  4. /// and how many beads do you need per color.
  5. /// It does not remove alpha channel or colored background from the image, so if your
  6. /// background color matches some part of the image subject, you may have to repaint
  7. /// the background or you have to count them manually.
  8. ///
  9. /// Made by Andrea Giorgio "Muu?" Cerioli
  10. /// http://www.lanoiadimuu.it/
  11. /// http://www.lanoiadimuu.it/2014/02/counting-plastic-beads/
  12. ///
  13. /// Licensed under MIT License
  14. /// http://opensource.org/licenses/MIT
  15. /// </summary>
  16.  
  17. using System;
  18. using System.IO;
  19. using System.Drawing;
  20. using System.Collections.Generic;
  21.  
  22. namespace beadscount
  23. {
  24.     class MainClass
  25.     {
  26.         public static void Main (string[] args)
  27.         {
  28.             // Exit if no arguments are given
  29.             if (args.Length == 0)
  30.             {
  31.                 Console.WriteLine("Usage: \nhamacount.exe file");
  32.                 Console.ReadLine();
  33.                 return;
  34.             }
  35.            
  36.             foreach(string file in args)
  37.             {
  38.                 try
  39.                 {
  40.                     Stream bitmapFile = File.Open(file, FileMode.Open);
  41.                     Bitmap bitmap = new Bitmap(bitmapFile);
  42.                     WriteColors(CountHamas(bitmap), file);
  43.                     bitmapFile.Close();
  44.                     Console.Clear();
  45.                     Console.WriteLine("Done! See " + file + ".txt for colors. \nPress enter to exit.");
  46.                     Console.ReadLine();
  47.                 }
  48.                 catch(Exception ex)
  49.                 {
  50.                     Console.WriteLine("Unable to read file " + file + "\n" + ex);
  51.                 }
  52.             }
  53.         }
  54.        
  55.         // Parse the img to get pixel info
  56.         public static Dictionary<Color, int> CountHamas(Bitmap bitmap)
  57.         {
  58.             try
  59.             {
  60.                 Console.WriteLine("Parsing file, may take a while");
  61.                 // Get image basic infos
  62.                 int size = bitmap.Size.Width * bitmap.Size.Height;
  63.                 int width = bitmap.Size.Width;
  64.                 int height = bitmap.Size.Height;
  65.                
  66.                 // Get all the pixels as a list
  67.                 List<Color> colorList = new List<Color>();
  68.                 for (int i = 0; i < height; i++)
  69.                 {
  70.                     for (int j = 0; j < width; j++)
  71.                     {
  72.                         colorList.Add(bitmap.GetPixel(j,i));
  73.                     }
  74.                 }
  75.                
  76.                 // For each color, write color down and remove them from list
  77.                 Dictionary<Color, int> colorCount = new Dictionary<Color, int>();
  78.                 int parsedPixel = 0;
  79.                 while(colorList.Count > 0)
  80.                 {
  81.                     Color colorToSeek = colorList[0];
  82.                     int count = 0;
  83.                     for(int i = colorList.Count-1; i >=0; i--)
  84.                     {
  85.                         Color colorToCompare = colorList[i];
  86.                         if(colorToSeek == colorToCompare)
  87.                         {
  88.                             colorList.Remove(colorToCompare);
  89.                             count++;
  90.                            
  91.                             // Debugs progress to console
  92.                             parsedPixel++;
  93.                             Console.Clear();
  94.                             Console.WriteLine("Parsing file, may take a while");
  95.                             Console.WriteLine(parsedPixel + " / " + size + " ( " + (parsedPixel * 100 / size) + "% ) " );
  96.                         }
  97.                     }
  98.                    
  99.                     // Store colours and number of occurrences
  100.                     colorCount.Add(colorToSeek, count);
  101.                 }
  102.                
  103.                 return colorCount;
  104.             }
  105.             catch
  106.             {
  107.                 throw new Exception("Parsing error");
  108.             };
  109.         }
  110.        
  111.         // Prints the output of CountHamas
  112.         public static void WriteColors(Dictionary<Color, int> colors, string fileName)
  113.         {
  114.             try
  115.             {
  116.                 StreamWriter file = new StreamWriter(fileName + ".txt", false);
  117.                 foreach(KeyValuePair<Color, int> keyValue in colors)
  118.                 {
  119.                     string line = keyValue.Key + ": \t\t" + keyValue.Value;
  120.                     file.WriteLine(line);
  121.                 }
  122.                 file.Close();
  123.             }
  124.             catch
  125.             {
  126.                 Console.WriteLine("Unable to print pixel outputs");
  127.             }
  128.            
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement