PTdecode/CImg-1.3.0/examples/image2ascii.cpp

Mon, 03 Aug 2009 14:21:23 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 03 Aug 2009 14:21:23 +0100
changeset 6
a274aba0a9d3
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

added keepme for ptdecode obj directory

     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.sourceforge.net )
     9  #
    10  #  Copyright   : David Tschumperle
    11  #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    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 */
    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;
    50 // The lines below are necessary when using a non-standard compiler as visualcpp6.
    51 #ifdef cimg_use_visualcpp6
    52 #define std
    53 #endif
    54 #ifdef min
    55 #undef min
    56 #undef max
    57 #endif
    59 /*---------------------------
    61   Main procedure
    63   --------------------------*/
    64 int main(int argc,char **argv) {
    65   cimg_usage("A simple image to ASCII-art converter.\n\nUsage : image2ascii [options] image");
    67   // Read command line parameters
    68   const char *geom    = cimg_option("-g","79x40","Output size");
    69   const int alphabet  = cimg_option("-a",0,"Alphabet type (0=full, 1=numbers, 2=letters, 3=signs, 4=minimal");
    70   const bool invert   = cimg_option("-invert",false,"Invert image intensities");
    71   const float contour = (float)cimg_option("-contour",0.0f,"Use image contours higher than specified threshold");
    72   const float blur    = (float)cimg_option("-blur",0.8f,"Image pre-blur");
    73   const float sigma   = (float)cimg_option("-sigma",1.5f,"Font pre-blur");
    74   const char *file_i  = cimg_argument1(0,"-invert");
    75   int w = 79, h = 40;
    76   std::sscanf(geom,"%d%*c%d",&w,&h);
    77   if (cimg_option("-h",false,0)) std::exit(0);
    79   // Init fonts
    80   const CImgList<> font_full = CImgList<>::font(11,false);
    81   const int fw = font_full['A'].dimx(), fh = font_full['A'].dimy();
    82   CImgList<> font, font_blur;
    83   CImgList<unsigned char> font_code;
    85   switch (alphabet) {
    86   case 1: {
    87     font_code.insert(CImg<>::vector(' '));
    88     for (unsigned char l='0'; l<='9'; l++) font_code.insert(CImg<>::vector(l));
    89   } break;
    90   case 2: {
    91     font_code.insert(CImg<>::vector(' '));
    92     for (unsigned char l='A'; l<='Z'; l++) font_code.insert(CImg<>::vector(l));
    93   } break;
    94   case 3: {
    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     font_code.insert(CImg<>::vector('('));
   108     font_code.insert(CImg<>::vector(')'));
   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     } break;
   115   case 4: {
   116     font_code.insert(CImg<>::vector(' '));
   117     font_code.insert(CImg<>::vector('.'));
   118     font_code.insert(CImg<>::vector('/'));
   119     font_code.insert(CImg<>::vector('\\'));
   120     font_code.insert(CImg<>::vector('_'));
   121     font_code.insert(CImg<>::vector('_'));
   122     font_code.insert(CImg<>::vector('|'));
   123     } break;
   124   default: { for (unsigned char l=' '; l<='~'; l++) font_code.insert(CImg<>::vector(l)); } break;
   125   }
   126   cimglist_for(font_code,l) {
   127     font.insert(font_full(font_code[l](0)));
   128     font_blur.insert(font[l].get_resize(fw,fh,1,1).blur(sigma).normalize(0,255));
   129   }
   131   // Init images
   132   CImg<> img;
   133   if (!file_i) { float white[3] = { 255,255,255 }; img.assign().draw_text(0,0," CImg\nRocks !",white); }
   134   else img.assign(file_i);
   135   img.pointwise_norm().resize(fw*w,fh*h);
   136   if (blur) img.blur(blur);
   137   if (contour>0) {
   138     CImgList<> grad = img.get_gradient("xy",4);
   139     img = (grad[0].pow(2) + grad[1].pow(2)).sqrt().normalize(0,100).threshold(contour);
   140   }
   141   img.normalize(0,255);
   142   if (invert) img = 255.0f-img;
   143   CImg<unsigned char> dest(w,h,1,1,0);
   145   // Render ASCII-art image, using a simple correlation method.
   146   CImg<> neigh;
   147   cimg_forY(dest,y) { cimg_forX(dest,x) {
   148     neigh = img.get_crop(x*fw,y*fh,(x+1)*fw,(y+1)*fh);
   149     float scoremin = 2e28f;
   150     unsigned int best = 0;
   151     cimglist_for(font_code,l) {
   152       const CImg<>& letter = font_blur[l];
   153       const float score = (float)((letter-neigh).pow(2).sum());
   154       if (score<scoremin) { scoremin = score; best = l; }
   155     }
   156     dest(x,y) = best;
   157     std::fprintf(stdout,"%c",font_code[dest(x,y)](0));
   158   }
   159   std::fprintf(stdout,"\n");
   160   }
   162   std::exit(0);
   163   return 0;
   164 }