Euphoria
image_to_text.cc
Go to the documentation of this file.
1 #include "core/image_to_text.h"
2 
3 #include <iostream>
4 
5 #include "core/image.h"
6 #include "core/palette.h"
7 #include "base/stringbuilder.h"
8 
9 
10 namespace eu::core
11 {
12  Table<char>
13  from_image_to_string_table(const Image& img, const std::vector<ImageMapAction>& map)
14  {
15  auto pal = DynamicPalette::create_empty("");
16  for(const auto m: map)
17  {
18  pal.colors.push_back(m.from_color);
19  }
20 
22  (
23  img.width,
24  img.height,
25  ' '
26  );
27  ret.set_all
28  (
29  [&pal, &map, &img](int x, int y)
30  {
31  const auto p = img.get_pixel(x, y);
32  const auto index = pal.to_palette().get_index_closest(Rgbi{p.r, p.g, p.b});
33  return map[index].to;
34  }
35  );
36 
37  return ret;
38  }
39 
40  Table<char>
42  (
43  const Image& img,
44  const std::vector<ImageMapAction>& map, char missing
45  )
46  {
47  auto find_match = [&](const Rgbi& c) -> char
48  {
49  for(const auto& m: map)
50  {
51  if(m.from_color == c)
52  {
53  return m.to;
54  }
55  }
56 
57  return missing;
58  };
59  auto ret = Table<char>::from_width_height(img.width, img.height, ' ');
60  ret.set_all([&](int x, int y) {
61  const auto p = img.get_pixel(x, y);
62  const auto c = Rgbi {p.r, p.g, p.b};
63  const auto r = find_match(c);
64  return r;
65  });
66 
67  return ret;
68  }
69 
70  Table<char>
71  from_image_to_string_table(const Image& img, bool shorter, Grayscale grayscale)
72  {
73  auto ret = Table<char>::from_width_height(img.width, img.height, ' ');
74  ret.set_all([shorter, &img, grayscale](int x, int y) {
75  // http://paulbourke.net/dataformats/asciiart/
76  const std::string characters = shorter
77  ? "@%#*+=-:. "
78  : "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
79  ;
80  const auto inverted_color = to_rgb
81  (
82  make_grayscale(img.get_pixel(x, y), grayscale)
83  ).r;
84  const auto p = 1.0f - inverted_color;
85  const auto index = floor_to_int(p * static_cast<float>(characters.size() - 1) );
86  return characters[index];
87  });
88 
89  return ret;
90  }
91 
92  std::vector<std::string>
93  to_strings(const Table<char>& table)
94  {
95  std::vector<std::string> ret;
96 
97  for(int y = 0; y < table.get_height(); y += 1)
98  {
99  StringBuilder ss;
100  for(int x = 0; x < table.get_width(); x += 1)
101  {
102  ss.add_char(table[{x, y}]);
103  }
104  ret.insert(ret.begin(), ss.to_string());
105  }
106 
107  return ret;
108  }
109 
110 }
std::vector< std::string > to_strings(const Table< char > &table)
Table< char > from_image_to_string_table_exact(const Image &img, const std::vector< ImageMapAction > &map, char missing)
Table< char > from_image_to_string_table(const Image &img, const std::vector< ImageMapAction > &map)
Rgbai make_grayscale(Rgbai c, Grayscale grayscale)
Definition: imagefilters.cc:23
Rgb to_rgb(const Rgbi &c)
Definition: rgb.cc:229
std::vector< T > map(const std::vector< F > &fs, C convert)
Definition: functional.h:56
int floor_to_int(float v)
Definition: numeric.cc:49
float r
Definition: rgb.h:63
Definition: rgb.h:26
std::string to_string()
Complete the builder and return the resulting string.
StringBuilder & add_char(char c)
static DynamicPalette create_empty(const std::string &name)
Create a empty palette with a name.
Definition: palette.cc:83
Rgbai get_pixel(int x, int y) const
Definition: image.cc:137
int height
Definition: image.h:33
Idx get_height() const
Definition: table.h:158
Idx get_width() const
Definition: table.h:153
static Table from_width_height(Idx width, Idx height, T d=T())
Definition: table.h:23