1 /*
2  #
3  #  File        : image2ascii.cpp
4  #                ( C++ source file )
5  #
6  #  Description : A basic image to ASCII-art converter.
7  #                This file is a part of the CImg Library project.
8  #                ( http://cimg.eu )
9  #
10  #  Copyright   : David Tschumperlé
11  #                ( http://tschumperle.users.greyc.fr/ )
12  #
13  #  License     : CeCILL v2.0
14  #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
15  #
16  #  This software is governed by the CeCILL  license under French law and
17  #  abiding by the rules of distribution of free software.  You can  use,
18  #  modify and/ or redistribute the software under the terms of the CeCILL
19  #  license as circulated by CEA, CNRS and INRIA at the following URL
20  #  "http://www.cecill.info".
21  #
22  #  As a counterpart to the access to the source code and  rights to copy,
23  #  modify and redistribute granted by the license, users are provided only
24  #  with a limited warranty  and the software's author,  the holder of the
25  #  economic rights,  and the successive licensors  have only  limited
26  #  liability.
27  #
28  #  In this respect, the user's attention is drawn to the risks associated
29  #  with loading,  using,  modifying and/or developing or reproducing the
30  #  software by the user in light of its specific status of free software,
31  #  that may mean  that it is complicated to manipulate,  and  that  also
32  #  therefore means  that it is reserved for developers  and  experienced
33  #  professionals having in-depth computer knowledge. Users are therefore
34  #  encouraged to load and test the software's suitability as regards their
35  #  requirements in conditions enabling the security of their systems and/or
36  #  data to be ensured and,  more generally, to use and operate it in the
37  #  same conditions as regards security.
38  #
39  #  The fact that you are presently reading this means that you have had
40  #  knowledge of the CeCILL license and that you accept its terms.
41  #
42 */
43 
44 // Tell CImg not to use display capabilities.
45 #undef cimg_display
46 #define cimg_display 0
47 #include "CImg.h"
48 using namespace cimg_library;
49 
50 /*---------------------------
51 
52   Main procedure
53 
54   --------------------------*/
main(int argc,char ** argv)55 int main(int argc,char **argv) {
56   cimg_usage("A simple image to ASCII-art converter.\n\nUsage : image2ascii [options] image");
57 
58   // Read command line parameters
59   const char *const file_i = cimg_option("-i",(char*)0,"Input image");
60   const char *const geom = cimg_option("-g","79x40","Output size");
61   const int alphabet = cimg_option("-a",0,"Alphabet type (0=full, 1=numbers, 2=letters, 3=signs, 4=minimal");
62   const bool invert = cimg_option("-invert",false,"Invert image intensities");
63   const float contour = (float)cimg_option("-contour",0.0f,"Use image contours higher than specified threshold");
64   const float blur = (float)cimg_option("-blur",0.8f,"Image pre-blur");
65   const float sigma = (float)cimg_option("-sigma",10.0f,"Font pre-blur");
66 
67   int w = 79, h = 40;
68   std::sscanf(geom,"%d%*c%d",&w,&h);
69   if (cimg_option("-h",false,0)) std::exit(0);
70 
71   // Init fonts
72   CImgList<> font_full = CImgList<>::font(13,false);
73   font_full.remove(0,255);
74   const int fw = font_full[(int)'A'].width(), fh = font_full[(int)'A'].height();
75   CImgList<> font, font_blur;
76   CImgList<unsigned char> font_code;
77 
78   switch (alphabet) {
79   case 1: {
80     font_code.insert(CImg<>::vector(' '));
81     for (unsigned char l='0'; l<='9'; l++) font_code.insert(CImg<>::vector(l));
82   } break;
83   case 2: {
84     font_code.insert(CImg<>::vector(' '));
85     for (unsigned char l='A'; l<='Z'; l++) font_code.insert(CImg<>::vector(l));
86   } break;
87   case 3: {
88     font_code.insert(CImg<>::vector(' '));
89     font_code.insert(CImg<>::vector('-'));
90     font_code.insert(CImg<>::vector('_'));
91     font_code.insert(CImg<>::vector('|'));
92     font_code.insert(CImg<>::vector('/'));
93     font_code.insert(CImg<>::vector('\\'));
94     font_code.insert(CImg<>::vector('+'));
95     font_code.insert(CImg<>::vector('.'));
96     font_code.insert(CImg<>::vector('*'));
97     font_code.insert(CImg<>::vector('='));
98     font_code.insert(CImg<>::vector(']'));
99     font_code.insert(CImg<>::vector('['));
100     font_code.insert(CImg<>::vector('('));
101     font_code.insert(CImg<>::vector(')'));
102     font_code.insert(CImg<>::vector('{'));
103     font_code.insert(CImg<>::vector('}'));
104     font_code.insert(CImg<>::vector('"'));
105     font_code.insert(CImg<>::vector('!'));
106     font_code.insert(CImg<>::vector('$'));
107     } break;
108   case 4: {
109     font_code.insert(CImg<>::vector(' '));
110     font_code.insert(CImg<>::vector('.'));
111     font_code.insert(CImg<>::vector('/'));
112     font_code.insert(CImg<>::vector('\\'));
113     font_code.insert(CImg<>::vector('_'));
114     font_code.insert(CImg<>::vector('_'));
115     font_code.insert(CImg<>::vector('|'));
116     } break;
117   default: { for (unsigned char l=' '; l<='~'; l++) font_code.insert(CImg<>::vector(l)); } break;
118   }
119   cimglist_for(font_code,l) {
120     font.insert(font_full(font_code[l](0)));
121     font_blur.insert(font[l].get_resize(fw,fh,1,1).blur(sigma).normalize(0,255));
122   }
123 
124   // Init images
125   CImg<> img;
126   if (!file_i) { float white[3] = { 255,255,255 }; img.assign().draw_text(0,0," CImg\nRocks !",white); }
127   else img.assign(file_i);
128   img.norm().resize(fw*w,fh*h);
129   if (blur) img.blur(blur);
130   if (contour>0) {
131     CImgList<> grad = img.get_gradient("xy",4);
132     img = (grad[0].pow(2) + grad[1].pow(2)).sqrt().normalize(0,100).threshold(contour);
133   }
134   img.normalize(0,255);
135   if (invert) img = 255.0f - img;
136   CImg<unsigned char> dest(w,h,1,1,0);
137 
138   // Render ASCII-art image, using a simple correlation method.
139   CImg<> neigh;
140   cimg_forY(dest,y) { cimg_forX(dest,x) {
141     neigh = img.get_crop(x*fw,y*fh,(x + 1)*fw,(y + 1)*fh);
142     float scoremin = 2e28f;
143     unsigned int best = 0;
144     cimglist_for(font_code,l) {
145       const CImg<>& letter = font_blur[l];
146       const float score = (float)((letter - neigh).pow(2).sum());
147       if (score<scoremin) { scoremin = score; best = l; }
148     }
149     dest(x,y) = (unsigned char)best;
150     std::fprintf(stdout,"%c",font_code[dest(x,y)](0));
151   }
152   std::fprintf(stdout,"\n");
153   }
154 
155   std::exit(0);
156   return 0;
157 }
158