예제 #1
0
        static public string[] convert_to_ASCII_art(byte[] greyscale_bytes, int width, int height)
        {
            Image_Sizes image_size = calculate_image_tier(width);

            if (image_tier == null)
            {
                image_tier = image_size;
            }

            if (initialized == null ||
                !initialized ||
                (initialized && image_size != image_tier))
            {
                initialize_ASCII_hashes(image_size);
            }

            // convert the byte array into a two dimensional array of bytes
            byte[][] grey_bytes = new byte[height][];
            int      byte_index = 0;

            for (int i = 0; i < height; i++)
            {
                grey_bytes[i] = new byte[width];
                for (int j = 0; j < width; j++)
                {
                    grey_bytes[i][j] = greyscale_bytes[byte_index++];
                }
            }

            // get the ByteGrid of the greyscale bytes
            int remaining_height;
            int remaining_width;

            ByteGrid[][] byte_grid_grid = create_byte_grid_grid(grey_bytes, out remaining_height, out remaining_width);

            // get the hashes for all the pixel grids
            uint[][] hashes = hash_byte_grid(byte_grid_grid);

            // convert all hashes to strings
            // ERROR TESTING
            // make it so that if an image is very small, it uses bare bones,
            // if it's small to x_large, use all hashes, but pass in the correct
            //    folder to take the ASCII pics from

            //string[] ASCII_lines = null;
            //if (image_tier == Image_Sizes.ICON)
            //{
            //   ASCII_lines = shade_ASCII(grey_bytes);
            //}
            //else
            //{
            //   ASCII_lines = all_hashes_to_ASCII(hashes);
            //}
            //string[] ASCII_lines = bare_bones_ASCII(grey_bytes);

            string[] ASCII_lines = shade_ASCII(grey_bytes);

            return(ASCII_lines);
        }
        public static string[] convert_to_ASCII_art(byte[] greyscale_bytes, int width, int height)
        {
            Image_Sizes image_size = calculate_image_tier(width);

             if (image_tier == null)
             {
            image_tier = image_size;
             }

             if(initialized == null
            || !initialized
            || (initialized && image_size != image_tier))
             {
            initialize_ASCII_hashes(image_size);
             }

             // convert the byte array into a two dimensional array of bytes
             byte[][] grey_bytes = new byte[height][];
             int byte_index = 0;
             for(int i = 0; i < height; i++){
            grey_bytes[i] = new byte[width];
            for(int j = 0; j < width; j++){
               grey_bytes[i][j] = greyscale_bytes[byte_index++];
            }
             }

             // get the ByteGrid of the greyscale bytes
             int remaining_height;
             int remaining_width;
             ByteGrid[][] byte_grid_grid = create_byte_grid_grid(grey_bytes, out remaining_height, out remaining_width);

             // get the hashes for all the pixel grids
             uint[][] hashes = hash_byte_grid(byte_grid_grid);

             // convert all hashes to strings
             // ERROR TESTING
             // make it so that if an image is very small, it uses bare bones,
             // if it's small to x_large, use all hashes, but pass in the correct
             //    folder to take the ASCII pics from

             //string[] ASCII_lines = null;
             //if (image_tier == Image_Sizes.ICON)
             //{
             //   ASCII_lines = shade_ASCII(grey_bytes);
             //}
             //else
             //{
             //   ASCII_lines = all_hashes_to_ASCII(hashes);
             //}
             //string[] ASCII_lines = bare_bones_ASCII(grey_bytes);

             string[] ASCII_lines = shade_ASCII(grey_bytes);

             return ASCII_lines;
        }
        private static Image_Sizes calculate_image_tier(int width)
        {
            Image_Sizes image_tier;

               if ((Image_Sizes)width < Image_Sizes.SMALL)
               {
                  image_tier = Image_Sizes.ICON;
               }
               else if ((Image_Sizes)width < Image_Sizes.MEDIUM)
               {
                  image_tier = Image_Sizes.SMALL;
               }
               else if ((Image_Sizes)width < Image_Sizes.LARGE)
               {
                  image_tier = Image_Sizes.MEDIUM;
               }
               else if ((Image_Sizes)width < Image_Sizes.X_LARGE)
               {
                  image_tier = Image_Sizes.LARGE;
               }
               else
               {
                  image_tier = Image_Sizes.X_LARGE;
               }

               return image_tier;
        }
        public static void initialize_ASCII_hashes(Image_Sizes image_size)
        {
            // get the folder
               StringBuilder path_builder = new StringBuilder(System.Environment.CurrentDirectory);

               if (image_size == Image_Sizes.SMALL)
               {
                  path_builder.Append("\\ASCII_Characters_Small\\");
               }
               else if (image_size == Image_Sizes.MEDIUM)
               {
                  path_builder.Append("\\ASCII_Characters_Medium\\");
               }
               else if (image_size == Image_Sizes.LARGE)
               {
                  path_builder.Append("\\ASCII_Characters_Large\\");
               }
               else if (image_size == Image_Sizes.X_LARGE)
               {
                  path_builder.Append("\\ASCII_Characters_X_Large\\");
               }
               else
               {
                  throw new ArgumentException("ASCII_Converter: Invalid image size for ASCII hash initialization.");
               }

               // record the image tier
               image_tier = image_size;

               ASCII_Characters = new char[] {'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*',
                  '+', ',', '-', '.', '/', '0', '1', '2', '3', '4',
                  '5', '6', '7', '8', '9', ':', ';', '<', '=', '>',
                  '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\',
                  ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f',
                  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                  'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                  ' ', '{', '|', '}'
               };
               string[] files = {"exclamation_mark", "quotation_mark", "hashtag", "dollar_sign", "percent", "ampersans", "apostrophe", "left_parenthesis", "right_parenthesis", "asterisk",
                                "plus_sign", "comma", "minus_sign", "period", "forward_slash", "zero", "one", "two", "three", "four",
                                "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less_than_symbol", "equals_sign", "greater_than_symbol",
                                "question_mark", "at_symbol", "A", "B", "C", "D", "E", "F", "G", "H",
                                "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                                "S", "T", "U", "V", "W", "X", "Y", "Z", "left_square_bracket", "backslash",
                                "right_square_bracket", "carrot", "underscore", "single_quote_mark", "lowercase_a", "lowercase_b", "lowercase_c", "lowercase_d", "lowercase_e", "lowercase_f",
                                "lowercase_g", "lowercase_h", "lowercase_i", "lowercase_j", "lowercase_k", "lowercase_l", "lowercase_m", "lowercase_n", "lowercase_o", "lowercase_p",
                                "lowercase_q", "lowercase_r", "lowercase_s", "lowercase_t", "lowercase_u", "lowercase_v", "lowercase_w", "lowercase_x", "lowercase_y", "lowercase_z",
                                "space", "left_bracket", "vertical_line", "right_bracket"
                                };
               ASCII_hashes = new uint[ASCII_Characters.Length];

               for(int i = 0; i < ASCII_Characters.Length; i++){
                  BinaryFileReader file_reader = new BinaryFileReader(files[i] + ".png", path_builder.ToString());
                  PNG_Decoder png_decoder = new PNG_Decoder(file_reader.file_data);
                  byte[] file_bytes = png_decoder.to_greyscale();

                  for(int byte_index = 0; byte_index < file_bytes.Length; byte_index++){
                     ASCII_hashes[i] = ASCII_hashes[i] << 1;
                     if (file_bytes[byte_index] < 100)
                     {
                        ASCII_hashes[i] += 1;
                     }
                     //ASCII_hashes[i] += file_bytes[byte_index];
                  }
               }

               initialized = true;
        }
예제 #5
0
        static public void initialize_ASCII_hashes(Image_Sizes image_size)
        {
            // get the folder
            StringBuilder path_builder = new StringBuilder(System.Environment.CurrentDirectory);

            if (image_size == Image_Sizes.SMALL)
            {
                path_builder.Append("\\ASCII_Characters_Small\\");
            }
            else if (image_size == Image_Sizes.MEDIUM)
            {
                path_builder.Append("\\ASCII_Characters_Medium\\");
            }
            else if (image_size == Image_Sizes.LARGE)
            {
                path_builder.Append("\\ASCII_Characters_Large\\");
            }
            else if (image_size == Image_Sizes.X_LARGE)
            {
                path_builder.Append("\\ASCII_Characters_X_Large\\");
            }
            else
            {
                throw new ArgumentException("ASCII_Converter: Invalid image size for ASCII hash initialization.");
            }

            // record the image tier
            image_tier = image_size;

            ASCII_Characters = new char[] { '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*',
                                            '+', ',', '-', '.', '/', '0', '1', '2', '3', '4',
                                            '5', '6', '7', '8', '9', ':', ';', '<', '=', '>',
                                            '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                                            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\',
                                            ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f',
                                            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                                            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                                            ' ', '{', '|', '}' };
            string[] files = { "exclamation_mark",     "quotation_mark", "hashtag",       "dollar_sign",       "percent",       "ampersans",   "apostrophe",  "left_parenthesis", "right_parenthesis",   "asterisk",
                               "plus_sign",            "comma",          "minus_sign",    "period",            "forward_slash", "zero",        "one",         "two",              "three",               "four",
                               "five",                 "six",            "seven",         "eight",             "nine",          "colon",       "semicolon",   "less_than_symbol", "equals_sign",         "greater_than_symbol",
                               "question_mark",        "at_symbol",      "A",             "B",                 "C",             "D",           "E",           "F",                "G",                   "H",
                               "I",                    "J",              "K",             "L",                 "M",             "N",           "O",           "P",                "Q",                   "R",
                               "S",                    "T",              "U",             "V",                 "W",             "X",           "Y",           "Z",                "left_square_bracket", "backslash",
                               "right_square_bracket", "carrot",         "underscore",    "single_quote_mark", "lowercase_a",   "lowercase_b", "lowercase_c", "lowercase_d",      "lowercase_e",         "lowercase_f",
                               "lowercase_g",          "lowercase_h",    "lowercase_i",   "lowercase_j",       "lowercase_k",   "lowercase_l", "lowercase_m", "lowercase_n",      "lowercase_o",         "lowercase_p",
                               "lowercase_q",          "lowercase_r",    "lowercase_s",   "lowercase_t",       "lowercase_u",   "lowercase_v", "lowercase_w", "lowercase_x",      "lowercase_y",         "lowercase_z",
                               "space",                "left_bracket",   "vertical_line", "right_bracket" };
            ASCII_hashes = new uint[ASCII_Characters.Length];

            for (int i = 0; i < ASCII_Characters.Length; i++)
            {
                BinaryFileReader file_reader = new BinaryFileReader(files[i] + ".png", path_builder.ToString());
                PNG_Decoder      png_decoder = new PNG_Decoder(file_reader.file_data);
                byte[]           file_bytes  = png_decoder.to_greyscale();

                for (int byte_index = 0; byte_index < file_bytes.Length; byte_index++)
                {
                    ASCII_hashes[i] = ASCII_hashes[i] << 1;
                    if (file_bytes[byte_index] < 100)
                    {
                        ASCII_hashes[i] += 1;
                    }
                    //ASCII_hashes[i] += file_bytes[byte_index];
                }
            }

            initialized = true;
        }