Beispiel #1
0
        static private uint byte_grid_to_pixel_hash(ByteGrid grid)
        {
            if (grid == null ||
                !grid.valid_grid)
            {
                throw new ArgumentNullException("ASCII_Converter: Null ByteGrid object passed when trying to hash the pixel value.");
            }

            uint hash = 0;

            // 2 bits per byte

            for (int i = 0; i < grid.height; i++)
            {
                for (int j = 0; j < grid.width; j++)
                {
                    hash = hash << 2;

                    GREYSCALE_CONVERSION_VALUES conv_val =
                        (GREYSCALE_CONVERSION_VALUES)grid.grid[i][j];

                    if (conv_val <= GREYSCALE_CONVERSION_VALUES.PURE_BLACK)
                    {
                        hash += 3;
                    }
                    else if (conv_val <= GREYSCALE_CONVERSION_VALUES.SEMI_BLACK)
                    {
                        hash += 2;
                    }
                    else if (conv_val <= GREYSCALE_CONVERSION_VALUES.GREY)
                    {
                        hash += 1;
                    }
                    else if (conv_val <= GREYSCALE_CONVERSION_VALUES.WHITE)
                    {
                        hash += 0;
                    }
                    else
                    {
                        throw new InvalidOperationException("ASCII_Converter: Invalid greyscale byte passed.");
                    }
                }
            }

            return(hash);
        }
        private static uint[][] hash_byte_grid(ByteGrid[][] byte_grid)
        {
            if (byte_grid == null
                  || (byte_grid.Length > 0
                      && byte_grid[0] == null))
               {
                  throw new ArgumentNullException("ASCII_Converter: Unable to create hash.");
               }
               // takes an n by n array of greyscale pixels
               // and converts them to a hash
               uint[][] hash_grid = new uint[byte_grid.Length][];

               for (int i = 0; i < byte_grid.Length; i++)
               {
                  hash_grid[i] = new uint[byte_grid[i].Length];

                  for (int j = 0; j < byte_grid[0].Length; j++)
                  {
                     hash_grid[i][j] = byte_grid_to_pixel_hash(byte_grid[i][j]);
                  }
               }

               return hash_grid;
        }
        private static ByteGrid[][] create_byte_grid_grid(byte[][] greyscale_bytes, out int remaining_height, out int remaining_width)
        {
            //if (!is_rectangular_2D_array(greyscale_bytes, 5, 5))
               //{
               //   remaining_height = 0;
               //   remaining_width = 0;
               //   return null;
               //}

               int data_height = greyscale_bytes.Length;
               int data_width = greyscale_bytes[0].Length;

               int hash_height = calculate_hash_height();
               int hash_width = calculate_hash_width();

               int result_height = data_height / hash_height;
               int result_width = data_width / hash_width;

               remaining_height = data_height % hash_height;
               remaining_width = data_width % hash_width;

               // account for indivisible picture dimensions
               if (remaining_height > 0)
               {
                  result_height++;
               }
               if (remaining_width > 0)
               {
                  result_width++;
               }

               // get the bytegrids
               List<ByteGrid> grid_list = new List<ByteGrid>();
               for(int grid_height_index = 0; grid_height_index < result_height; grid_height_index++){
                  for(int grid_width_index = 0; grid_width_index < result_width; grid_width_index++){
                     // in the correct grid area of the 2D byte array,
                     // now copy the appropriate bytes over to the
                     // temp byte[][], make a ByteGrid out of them, and
                     // push it into the list

                     // get the starting, top-left corner of the 2-D byte array you're going to convert to a ByteGrid
                     int byte_height_index = grid_height_index * hash_height;

                     byte[][] grid_arr = new byte[hash_height][];

                     for(int i = 0; i < hash_height; i++){
                        grid_arr[i] = new byte[hash_width];
                        int byte_width_index = grid_width_index * hash_width;

                        for(int j = 0; j < hash_width; j++){
                           if (byte_height_index >= greyscale_bytes.Length)
                           {
                              grid_arr[i][j] = 255; // make the unexisting bit white
                           }
                           else if (byte_width_index >= greyscale_bytes[byte_height_index].Length)
                           {
                              grid_arr[i][j] = 255; // make the unexisting bit white
                           }
                           else
                           {
                              grid_arr[i][j] = greyscale_bytes[byte_height_index][byte_width_index];
                           }
                           ++byte_width_index;
                        }
                        byte_height_index++;
                     }

                     grid_list.Add(new ByteGrid(grid_arr));
                  }
               }

               // push the bytegrids into the result array and return it.
               // prepare the return list
               ByteGrid[][] result = new ByteGrid[result_height][];
               for (int i = 0; i < result_height; i++)
               {
                  result[i] = new ByteGrid[result_width];
               }
               int result_height_index = 0;
               int result_width_index = 0;
               foreach (ByteGrid grid in grid_list)
               {
                  result[result_height_index][result_width_index] = grid;
                  ++result_width_index;
                  if (result_width_index % result_width == 0)
                  {
                     ++result_height_index;
                     result_width_index = 0;
                  }
               }

               return result;
        }
        private static uint byte_grid_to_pixel_hash(ByteGrid grid)
        {
            if(grid == null
                        || !grid.valid_grid){
                        throw new ArgumentNullException("ASCII_Converter: Null ByteGrid object passed when trying to hash the pixel value.");
                     }

                     uint hash = 0;
                     // 2 bits per byte

                     for(int i = 0; i < grid.height; i++){
                        for(int j = 0; j < grid.width; j++){
                           hash = hash << 2;

                           GREYSCALE_CONVERSION_VALUES conv_val =
                              (GREYSCALE_CONVERSION_VALUES)grid.grid[i][j];

                           if (conv_val <= GREYSCALE_CONVERSION_VALUES.PURE_BLACK)
                           {
                              hash += 3;
                           }
                           else if (conv_val <= GREYSCALE_CONVERSION_VALUES.SEMI_BLACK)
                           {
                              hash += 2;
                           }
                           else if (conv_val <= GREYSCALE_CONVERSION_VALUES.GREY)
                           {
                              hash += 1;
                           }
                           else if (conv_val <= GREYSCALE_CONVERSION_VALUES.WHITE)
                           {
                              hash += 0;
                           }
                           else
                           {
                              throw new InvalidOperationException("ASCII_Converter: Invalid greyscale byte passed.");
                           }
                        }
                     }

                     return hash;
        }
Beispiel #5
0
        static private ByteGrid[][] create_byte_grid_grid(byte[][] greyscale_bytes, out int remaining_height, out int remaining_width)
        {
            //if (!is_rectangular_2D_array(greyscale_bytes, 5, 5))
            //{
            //   remaining_height = 0;
            //   remaining_width = 0;
            //   return null;
            //}

            int data_height = greyscale_bytes.Length;
            int data_width  = greyscale_bytes[0].Length;

            int hash_height = calculate_hash_height();
            int hash_width  = calculate_hash_width();

            int result_height = data_height / hash_height;
            int result_width  = data_width / hash_width;

            remaining_height = data_height % hash_height;
            remaining_width  = data_width % hash_width;

            // account for indivisible picture dimensions
            if (remaining_height > 0)
            {
                result_height++;
            }
            if (remaining_width > 0)
            {
                result_width++;
            }

            // get the bytegrids
            List <ByteGrid> grid_list = new List <ByteGrid>();

            for (int grid_height_index = 0; grid_height_index < result_height; grid_height_index++)
            {
                for (int grid_width_index = 0; grid_width_index < result_width; grid_width_index++)
                {
                    // in the correct grid area of the 2D byte array,
                    // now copy the appropriate bytes over to the
                    // temp byte[][], make a ByteGrid out of them, and
                    // push it into the list

                    // get the starting, top-left corner of the 2-D byte array you're going to convert to a ByteGrid
                    int byte_height_index = grid_height_index * hash_height;

                    byte[][] grid_arr = new byte[hash_height][];

                    for (int i = 0; i < hash_height; i++)
                    {
                        grid_arr[i] = new byte[hash_width];
                        int byte_width_index = grid_width_index * hash_width;

                        for (int j = 0; j < hash_width; j++)
                        {
                            if (byte_height_index >= greyscale_bytes.Length)
                            {
                                grid_arr[i][j] = 255; // make the unexisting bit white
                            }
                            else if (byte_width_index >= greyscale_bytes[byte_height_index].Length)
                            {
                                grid_arr[i][j] = 255; // make the unexisting bit white
                            }
                            else
                            {
                                grid_arr[i][j] = greyscale_bytes[byte_height_index][byte_width_index];
                            }
                            ++byte_width_index;
                        }
                        byte_height_index++;
                    }

                    grid_list.Add(new ByteGrid(grid_arr));
                }
            }

            // push the bytegrids into the result array and return it.
            // prepare the return list
            ByteGrid[][] result = new ByteGrid[result_height][];
            for (int i = 0; i < result_height; i++)
            {
                result[i] = new ByteGrid[result_width];
            }
            int result_height_index = 0;
            int result_width_index  = 0;

            foreach (ByteGrid grid in grid_list)
            {
                result[result_height_index][result_width_index] = grid;
                ++result_width_index;
                if (result_width_index % result_width == 0)
                {
                    ++result_height_index;
                    result_width_index = 0;
                }
            }

            return(result);
        }