PTdecode/CImg-1.3.0/examples/captcha.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/captcha.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,161 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : captcha.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : Captcha images generator.
    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 +#ifndef cimg_debug
    1.48 +#define cimg_debug 1
    1.49 +#endif
    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 such 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 +// Start main procedure
    1.63 +//----------------------
    1.64 +int main(int argc,char **argv) {
    1.65 +
    1.66 +  // Read command line parameters
    1.67 +  //------------------------------
    1.68 +  cimg_usage("Captcha generator");
    1.69 +  const char *file_o       = cimg_option("-o",(char*)0,"Output image file");
    1.70 +  const bool add_border    = cimg_option("-b",true,"Add border to captcha image");
    1.71 +  const bool visu          = cimg_option("-visu",true,"Enable visualization if no output file");
    1.72 +
    1.73 +  // Generate captcha text (6 char max).
    1.74 +  //------------------------------------
    1.75 +  const char *predef_words[] = {
    1.76 +    "aarrgh", "abacas", "abacus", "abakas", "abamps", "abased", "abaser", "abases", "abasia", "abated", "abater", "abates", "abatis", "abator",
    1.77 +    "baobab", "barbal", "barbed", "barbel", "barber", "barbes", "barbet", "barbie", "barbut", "barcas", "barded", "bardes", "bardic", "barege",
    1.78 +    "cavies", "cavils", "caving", "cavity", "cavort", "cawing", "cayman", "cayuse", "ceased", "ceases", "cebids", "ceboid", "cecity", "cedarn",
    1.79 +    "dicast", "dicers", "dicier", "dicing", "dicker", "dickey", "dickie", "dicots", "dictum", "didact", "diddle", "diddly", "didies", "didoes",
    1.80 +    "emails", "embalm", "embank", "embark", "embars", "embays", "embeds", "embers", "emblem", "embody", "emboli", "emboly", "embosk", "emboss",
    1.81 +    "fluffy", "fluids", "fluish", "fluked", "flukes", "flukey", "flumed", "flumes", "flumps", "flunks", "flunky", "fluors", "flurry", "fluted",
    1.82 +    "genome", "genoms", "genres", "genros", "gentes", "gentil", "gentle", "gently", "gentry", "geodes", "geodic", "geoids", "gerahs", "gerbil",
    1.83 +    "hotter", "hottie", "houdah", "hounds", "houris", "hourly", "housed", "housel", "houser", "houses", "hovels", "hovers", "howdah", "howdie",
    1.84 +    "inland", "inlays", "inlets", "inlier", "inmate", "inmesh", "inmost", "innage", "innate", "inners", "inning", "inpour", "inputs", "inroad",
    1.85 +    "joypop", "jubbah", "jubhah", "jubile", "judder", "judged", "judger", "judges", "judoka", "jugate", "jugful", "jugged", "juggle", "jugula",
    1.86 +    "knifer", "knifes", "knight", "knives", "knobby", "knocks", "knolls", "knolly", "knosps", "knotty", "knouts", "knower", "knowns", "knubby",
    1.87 +    "legate", "legato", "legend", "legers", "legged", "leggin", "legion", "legist", "legits", "legman", "legmen", "legong", "legume", "lehuas",
    1.88 +    "mammal", "mammas", "mammee", "mammer", "mammet", "mammey", "mammie", "mammon", "mamzer", "manage", "manana", "manats", "manche", "manege",
    1.89 +    "nihils", "nilgai", "nilgau", "nilled", "nimble", "nimbly", "nimbus", "nimmed", "nimrod", "ninety", "ninjas", "ninons", "ninths", "niobic",
    1.90 +    "offish", "offkey", "offset", "oftest", "ogdoad", "oghams", "ogival", "ogives", "oglers", "ogling", "ogress", "ogrish", "ogrism", "ohmage",
    1.91 +    "papaws", "papaya", "papers", "papery", "pappus", "papula", "papule", "papyri", "parade", "paramo", "parang", "paraph", "parcel", "pardah",
    1.92 +    "quasar", "quatre", "quaver", "qubits", "qubyte", "queans", "queasy", "queazy", "queens", "queers", "quelea", "quells", "quench", "querns",
    1.93 +    "raised", "raiser", "raises", "raisin", "raitas", "rajahs", "rakees", "rakers", "raking", "rakish", "rallye", "ralphs", "ramada", "ramate",
    1.94 +    "savory", "savour", "savoys", "sawers", "sawfly", "sawing", "sawlog", "sawney", "sawyer", "saxony", "sayeds", "sayers", "sayest", "sayids",
    1.95 +    "tondos", "toneme", "toners", "tongas", "tonged", "tonger", "tongue", "tonics", "tonier", "toning", "tonish", "tonlet", "tonner", "tonnes",
    1.96 +    "uredia", "uredos", "ureide", "uremia", "uremic", "ureter", "uretic", "urgent", "urgers", "urging", "urials", "urinal", "urines", "uropod",
    1.97 +    "villus", "vimina", "vinals", "vincas", "vineal", "vinery", "vinier", "vinify", "vining", "vinous", "vinyls", "violas", "violet", "violin",
    1.98 +    "webfed", "weblog", "wechts", "wedded", "wedder", "wedeln", "wedels", "wedged", "wedges", "wedgie", "weeded", "weeder", "weekly", "weened",
    1.99 +    "xystoi", "xystos", "xystus", "yabber", "yabbie", "yachts", "yacked", "yaffed", "yagers", "yahoos", "yairds", "yakked", "yakker", "yakuza",
   1.100 +    "zigged", "zigzag", "zillah", "zinced", "zincic", "zincky", "zinebs", "zinged", "zinger", "zinnia", "zipped", "zipper", "zirams", "zircon" };
   1.101 +  cimg::srand();
   1.102 +  const char *const captcha_text = predef_words[(int)std::rand()%(sizeof(predef_words)/7)];
   1.103 +
   1.104 +  // Create captcha image
   1.105 +  //----------------------
   1.106 +
   1.107 +  // Write colored and distorted text
   1.108 +  CImg<unsigned char> captcha(256,64,1,3,0), color(3);
   1.109 +  char letter[2] = { 0 };
   1.110 +  for (unsigned int k=0; k<6; ++k) {
   1.111 +    CImg<unsigned char> tmp;
   1.112 +    *letter = captcha_text[k];
   1.113 +    if (*letter) {
   1.114 +      { cimg_forX(color,i) color[i] = (unsigned char)(128+(std::rand()%127)); }
   1.115 +      tmp.draw_text((int)(2+8*cimg::rand()),
   1.116 +                    (int)(12*cimg::rand()),
   1.117 +                    letter,color.ptr(),0,1,std::rand()%2?38:57);
   1.118 +      const unsigned int dir = std::rand()%4, wph = tmp.dimx()+tmp.dimy();
   1.119 +      { cimg_forXYV(tmp,x,y,v) {
   1.120 +        const int val = dir==0?x+y:(dir==1?x+tmp.dimy()-y:(dir==2?y+tmp.dimx()-x:tmp.dimx()-x+tmp.dimy()-y));
   1.121 +        tmp(x,y,v) = (unsigned char)cimg::max(0.0f,cimg::min(255.0f,1.5f*tmp(x,y,v)*val/wph));
   1.122 +      }}
   1.123 +      if (std::rand()%2) tmp = (tmp.get_dilate(3)-=tmp);
   1.124 +      tmp.blur((float)cimg::rand()*0.8f).normalize(0,255);
   1.125 +      const float sin_offset = (float)cimg::crand()*3, sin_freq = (float)cimg::crand()/7;
   1.126 +      { cimg_forYV(captcha,y,v) captcha.get_shared_line(y,0,v).translate((int)(4*std::cos(y*sin_freq+sin_offset))); }
   1.127 +      captcha.draw_image(6+40*k,tmp);
   1.128 +    }
   1.129 +  }
   1.130 +
   1.131 +  // Add geometric and random noise
   1.132 +  CImg<unsigned char> copy = (+captcha).fill(0);
   1.133 +  for (unsigned int l=0; l<3; ++l) {
   1.134 +    if (l) copy.blur(0.5f).normalize(0,148);
   1.135 +    for (unsigned int k=0; k<10; ++k) {
   1.136 +      cimg_forX(color,i) color[i] = (unsigned char)(128+cimg::rand()*127);
   1.137 +      if (cimg::rand()<0.5f) copy.draw_circle((int)(cimg::rand()*captcha.dimx()),
   1.138 +                                              (int)(cimg::rand()*captcha.dimy()),
   1.139 +                                              (int)(cimg::rand()*30),
   1.140 +                                              color,0.6f,~0U);
   1.141 +      else copy.draw_line((int)(cimg::rand()*captcha.dimx()),
   1.142 +                          (int)(cimg::rand()*captcha.dimy()),
   1.143 +                          (int)(cimg::rand()*captcha.dimx()),
   1.144 +                          (int)(cimg::rand()*captcha.dimy()),
   1.145 +                          color,0.6f);
   1.146 +    }
   1.147 +  }
   1.148 +  captcha|=copy;
   1.149 +  captcha.noise(10,2);
   1.150 +
   1.151 +  if (add_border) captcha.draw_rectangle(0,0,captcha.dimx()-1,captcha.dimy()-1,CImg<unsigned char>::vector(255,255,255),1.0f,~0U);
   1.152 +  captcha = (+captcha).fill(255) - captcha;
   1.153 +
   1.154 +  // Write output image and captcha text
   1.155 +  //-------------------------------------
   1.156 +  std::printf("%s\n",captcha_text);
   1.157 +  if (file_o) captcha.save(file_o);
   1.158 +  else if (visu) {
   1.159 +    CImgDisplay disp(CImg<unsigned char>(512,128,1,3,180).draw_image(128,32,captcha),captcha_text,0);
   1.160 +    while (!disp.is_closed && !disp.key) { disp.wait(); if (disp.is_resized) disp.resize(disp); }
   1.161 +  }
   1.162 +  return 0;
   1.163 +}
   1.164 +