PTdecode/CImg-1.3.0/examples/image2ascii.cpp

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