1.1 diff -r 5edfbd3e7a46 -r 1204ebf9340d PTdecode/CImg-1.3.0/examples/image2ascii.cpp 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/PTdecode/CImg-1.3.0/examples/image2ascii.cpp Mon Aug 03 14:09:20 2009 +0100 1.4 @@ -0,0 +1,164 @@ 1.5 +/* 1.6 + # 1.7 + # File : image2ascii.cpp 1.8 + # ( C++ source file ) 1.9 + # 1.10 + # Description : A basic image to ASCII-art converter. 1.11 + # This file is a part of the CImg Library project. 1.12 + # ( http://cimg.sourceforge.net ) 1.13 + # 1.14 + # Copyright : David Tschumperle 1.15 + # ( http://www.greyc.ensicaen.fr/~dtschump/ ) 1.16 + # 1.17 + # License : CeCILL v2.0 1.18 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.19 + # 1.20 + # This software is governed by the CeCILL license under French law and 1.21 + # abiding by the rules of distribution of free software. You can use, 1.22 + # modify and/ or redistribute the software under the terms of the CeCILL 1.23 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.24 + # "http://www.cecill.info". 1.25 + # 1.26 + # As a counterpart to the access to the source code and rights to copy, 1.27 + # modify and redistribute granted by the license, users are provided only 1.28 + # with a limited warranty and the software's author, the holder of the 1.29 + # economic rights, and the successive licensors have only limited 1.30 + # liability. 1.31 + # 1.32 + # In this respect, the user's attention is drawn to the risks associated 1.33 + # with loading, using, modifying and/or developing or reproducing the 1.34 + # software by the user in light of its specific status of free software, 1.35 + # that may mean that it is complicated to manipulate, and that also 1.36 + # therefore means that it is reserved for developers and experienced 1.37 + # professionals having in-depth computer knowledge. Users are therefore 1.38 + # encouraged to load and test the software's suitability as regards their 1.39 + # requirements in conditions enabling the security of their systems and/or 1.40 + # data to be ensured and, more generally, to use and operate it in the 1.41 + # same conditions as regards security. 1.42 + # 1.43 + # The fact that you are presently reading this means that you have had 1.44 + # knowledge of the CeCILL license and that you accept its terms. 1.45 + # 1.46 +*/ 1.47 + 1.48 +// Tell CImg not to use display capabilities. 1.49 +#undef cimg_display 1.50 +#define cimg_display 0 1.51 +#include "CImg.h" 1.52 +using namespace cimg_library; 1.53 + 1.54 +// The lines below are necessary when using a non-standard compiler as visualcpp6. 1.55 +#ifdef cimg_use_visualcpp6 1.56 +#define std 1.57 +#endif 1.58 +#ifdef min 1.59 +#undef min 1.60 +#undef max 1.61 +#endif 1.62 + 1.63 +/*--------------------------- 1.64 + 1.65 + Main procedure 1.66 + 1.67 + --------------------------*/ 1.68 +int main(int argc,char **argv) { 1.69 + cimg_usage("A simple image to ASCII-art converter.\n\nUsage : image2ascii [options] image"); 1.70 + 1.71 + // Read command line parameters 1.72 + const char *geom = cimg_option("-g","79x40","Output size"); 1.73 + const int alphabet = cimg_option("-a",0,"Alphabet type (0=full, 1=numbers, 2=letters, 3=signs, 4=minimal"); 1.74 + const bool invert = cimg_option("-invert",false,"Invert image intensities"); 1.75 + const float contour = (float)cimg_option("-contour",0.0f,"Use image contours higher than specified threshold"); 1.76 + const float blur = (float)cimg_option("-blur",0.8f,"Image pre-blur"); 1.77 + const float sigma = (float)cimg_option("-sigma",1.5f,"Font pre-blur"); 1.78 + const char *file_i = cimg_argument1(0,"-invert"); 1.79 + int w = 79, h = 40; 1.80 + std::sscanf(geom,"%d%*c%d",&w,&h); 1.81 + if (cimg_option("-h",false,0)) std::exit(0); 1.82 + 1.83 + // Init fonts 1.84 + const CImgList<> font_full = CImgList<>::font(11,false); 1.85 + const int fw = font_full['A'].dimx(), fh = font_full['A'].dimy(); 1.86 + CImgList<> font, font_blur; 1.87 + CImgList<unsigned char> font_code; 1.88 + 1.89 + switch (alphabet) { 1.90 + case 1: { 1.91 + font_code.insert(CImg<>::vector(' ')); 1.92 + for (unsigned char l='0'; l<='9'; l++) font_code.insert(CImg<>::vector(l)); 1.93 + } break; 1.94 + case 2: { 1.95 + font_code.insert(CImg<>::vector(' ')); 1.96 + for (unsigned char l='A'; l<='Z'; l++) font_code.insert(CImg<>::vector(l)); 1.97 + } break; 1.98 + case 3: { 1.99 + font_code.insert(CImg<>::vector(' ')); 1.100 + font_code.insert(CImg<>::vector('-')); 1.101 + font_code.insert(CImg<>::vector('_')); 1.102 + font_code.insert(CImg<>::vector('|')); 1.103 + font_code.insert(CImg<>::vector('/')); 1.104 + font_code.insert(CImg<>::vector('\\')); 1.105 + font_code.insert(CImg<>::vector('+')); 1.106 + font_code.insert(CImg<>::vector('.')); 1.107 + font_code.insert(CImg<>::vector('*')); 1.108 + font_code.insert(CImg<>::vector('=')); 1.109 + font_code.insert(CImg<>::vector(']')); 1.110 + font_code.insert(CImg<>::vector('[')); 1.111 + font_code.insert(CImg<>::vector('(')); 1.112 + font_code.insert(CImg<>::vector(')')); 1.113 + font_code.insert(CImg<>::vector('{')); 1.114 + font_code.insert(CImg<>::vector('}')); 1.115 + font_code.insert(CImg<>::vector('"')); 1.116 + font_code.insert(CImg<>::vector('!')); 1.117 + font_code.insert(CImg<>::vector('$')); 1.118 + } break; 1.119 + case 4: { 1.120 + font_code.insert(CImg<>::vector(' ')); 1.121 + font_code.insert(CImg<>::vector('.')); 1.122 + font_code.insert(CImg<>::vector('/')); 1.123 + font_code.insert(CImg<>::vector('\\')); 1.124 + font_code.insert(CImg<>::vector('_')); 1.125 + font_code.insert(CImg<>::vector('_')); 1.126 + font_code.insert(CImg<>::vector('|')); 1.127 + } break; 1.128 + default: { for (unsigned char l=' '; l<='~'; l++) font_code.insert(CImg<>::vector(l)); } break; 1.129 + } 1.130 + cimglist_for(font_code,l) { 1.131 + font.insert(font_full(font_code[l](0))); 1.132 + font_blur.insert(font[l].get_resize(fw,fh,1,1).blur(sigma).normalize(0,255)); 1.133 + } 1.134 + 1.135 + // Init images 1.136 + CImg<> img; 1.137 + if (!file_i) { float white[3] = { 255,255,255 }; img.assign().draw_text(0,0," CImg\nRocks !",white); } 1.138 + else img.assign(file_i); 1.139 + img.pointwise_norm().resize(fw*w,fh*h); 1.140 + if (blur) img.blur(blur); 1.141 + if (contour>0) { 1.142 + CImgList<> grad = img.get_gradient("xy",4); 1.143 + img = (grad[0].pow(2) + grad[1].pow(2)).sqrt().normalize(0,100).threshold(contour); 1.144 + } 1.145 + img.normalize(0,255); 1.146 + if (invert) img = 255.0f-img; 1.147 + CImg<unsigned char> dest(w,h,1,1,0); 1.148 + 1.149 + // Render ASCII-art image, using a simple correlation method. 1.150 + CImg<> neigh; 1.151 + cimg_forY(dest,y) { cimg_forX(dest,x) { 1.152 + neigh = img.get_crop(x*fw,y*fh,(x+1)*fw,(y+1)*fh); 1.153 + float scoremin = 2e28f; 1.154 + unsigned int best = 0; 1.155 + cimglist_for(font_code,l) { 1.156 + const CImg<>& letter = font_blur[l]; 1.157 + const float score = (float)((letter-neigh).pow(2).sum()); 1.158 + if (score<scoremin) { scoremin = score; best = l; } 1.159 + } 1.160 + dest(x,y) = best; 1.161 + std::fprintf(stdout,"%c",font_code[dest(x,y)](0)); 1.162 + } 1.163 + std::fprintf(stdout,"\n"); 1.164 + } 1.165 + 1.166 + std::exit(0); 1.167 + return 0; 1.168 +}