PTdecode/CImg-1.3.0/examples/captcha.cpp

Wed, 05 Aug 2009 15:00:54 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 05 Aug 2009 15:00:54 +0100
changeset 12
96e1df9bd27c
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

small changes to hexdump code to stop a gcc warning on platforms where sizeof(int) != sizeof(int*) e.g. x86_64

     1 /*
     2  #
     3  #  File        : captcha.cpp
     4  #                ( C++ source file )
     5  #
     6  #  Description : Captcha images generator.
     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 #ifndef cimg_debug
    45 #define cimg_debug 1
    46 #endif
    47 #include "CImg.h"
    48 using namespace cimg_library;
    50 // The lines below are necessary when using a non-standard compiler such as visualcpp6.
    51 #ifdef cimg_use_visualcpp6
    52 #define std
    53 #endif
    54 #ifdef min
    55 #undef min
    56 #undef max
    57 #endif
    59 // Start main procedure
    60 //----------------------
    61 int main(int argc,char **argv) {
    63   // Read command line parameters
    64   //------------------------------
    65   cimg_usage("Captcha generator");
    66   const char *file_o       = cimg_option("-o",(char*)0,"Output image file");
    67   const bool add_border    = cimg_option("-b",true,"Add border to captcha image");
    68   const bool visu          = cimg_option("-visu",true,"Enable visualization if no output file");
    70   // Generate captcha text (6 char max).
    71   //------------------------------------
    72   const char *predef_words[] = {
    73     "aarrgh", "abacas", "abacus", "abakas", "abamps", "abased", "abaser", "abases", "abasia", "abated", "abater", "abates", "abatis", "abator",
    74     "baobab", "barbal", "barbed", "barbel", "barber", "barbes", "barbet", "barbie", "barbut", "barcas", "barded", "bardes", "bardic", "barege",
    75     "cavies", "cavils", "caving", "cavity", "cavort", "cawing", "cayman", "cayuse", "ceased", "ceases", "cebids", "ceboid", "cecity", "cedarn",
    76     "dicast", "dicers", "dicier", "dicing", "dicker", "dickey", "dickie", "dicots", "dictum", "didact", "diddle", "diddly", "didies", "didoes",
    77     "emails", "embalm", "embank", "embark", "embars", "embays", "embeds", "embers", "emblem", "embody", "emboli", "emboly", "embosk", "emboss",
    78     "fluffy", "fluids", "fluish", "fluked", "flukes", "flukey", "flumed", "flumes", "flumps", "flunks", "flunky", "fluors", "flurry", "fluted",
    79     "genome", "genoms", "genres", "genros", "gentes", "gentil", "gentle", "gently", "gentry", "geodes", "geodic", "geoids", "gerahs", "gerbil",
    80     "hotter", "hottie", "houdah", "hounds", "houris", "hourly", "housed", "housel", "houser", "houses", "hovels", "hovers", "howdah", "howdie",
    81     "inland", "inlays", "inlets", "inlier", "inmate", "inmesh", "inmost", "innage", "innate", "inners", "inning", "inpour", "inputs", "inroad",
    82     "joypop", "jubbah", "jubhah", "jubile", "judder", "judged", "judger", "judges", "judoka", "jugate", "jugful", "jugged", "juggle", "jugula",
    83     "knifer", "knifes", "knight", "knives", "knobby", "knocks", "knolls", "knolly", "knosps", "knotty", "knouts", "knower", "knowns", "knubby",
    84     "legate", "legato", "legend", "legers", "legged", "leggin", "legion", "legist", "legits", "legman", "legmen", "legong", "legume", "lehuas",
    85     "mammal", "mammas", "mammee", "mammer", "mammet", "mammey", "mammie", "mammon", "mamzer", "manage", "manana", "manats", "manche", "manege",
    86     "nihils", "nilgai", "nilgau", "nilled", "nimble", "nimbly", "nimbus", "nimmed", "nimrod", "ninety", "ninjas", "ninons", "ninths", "niobic",
    87     "offish", "offkey", "offset", "oftest", "ogdoad", "oghams", "ogival", "ogives", "oglers", "ogling", "ogress", "ogrish", "ogrism", "ohmage",
    88     "papaws", "papaya", "papers", "papery", "pappus", "papula", "papule", "papyri", "parade", "paramo", "parang", "paraph", "parcel", "pardah",
    89     "quasar", "quatre", "quaver", "qubits", "qubyte", "queans", "queasy", "queazy", "queens", "queers", "quelea", "quells", "quench", "querns",
    90     "raised", "raiser", "raises", "raisin", "raitas", "rajahs", "rakees", "rakers", "raking", "rakish", "rallye", "ralphs", "ramada", "ramate",
    91     "savory", "savour", "savoys", "sawers", "sawfly", "sawing", "sawlog", "sawney", "sawyer", "saxony", "sayeds", "sayers", "sayest", "sayids",
    92     "tondos", "toneme", "toners", "tongas", "tonged", "tonger", "tongue", "tonics", "tonier", "toning", "tonish", "tonlet", "tonner", "tonnes",
    93     "uredia", "uredos", "ureide", "uremia", "uremic", "ureter", "uretic", "urgent", "urgers", "urging", "urials", "urinal", "urines", "uropod",
    94     "villus", "vimina", "vinals", "vincas", "vineal", "vinery", "vinier", "vinify", "vining", "vinous", "vinyls", "violas", "violet", "violin",
    95     "webfed", "weblog", "wechts", "wedded", "wedder", "wedeln", "wedels", "wedged", "wedges", "wedgie", "weeded", "weeder", "weekly", "weened",
    96     "xystoi", "xystos", "xystus", "yabber", "yabbie", "yachts", "yacked", "yaffed", "yagers", "yahoos", "yairds", "yakked", "yakker", "yakuza",
    97     "zigged", "zigzag", "zillah", "zinced", "zincic", "zincky", "zinebs", "zinged", "zinger", "zinnia", "zipped", "zipper", "zirams", "zircon" };
    98   cimg::srand();
    99   const char *const captcha_text = predef_words[(int)std::rand()%(sizeof(predef_words)/7)];
   101   // Create captcha image
   102   //----------------------
   104   // Write colored and distorted text
   105   CImg<unsigned char> captcha(256,64,1,3,0), color(3);
   106   char letter[2] = { 0 };
   107   for (unsigned int k=0; k<6; ++k) {
   108     CImg<unsigned char> tmp;
   109     *letter = captcha_text[k];
   110     if (*letter) {
   111       { cimg_forX(color,i) color[i] = (unsigned char)(128+(std::rand()%127)); }
   112       tmp.draw_text((int)(2+8*cimg::rand()),
   113                     (int)(12*cimg::rand()),
   114                     letter,color.ptr(),0,1,std::rand()%2?38:57);
   115       const unsigned int dir = std::rand()%4, wph = tmp.dimx()+tmp.dimy();
   116       { cimg_forXYV(tmp,x,y,v) {
   117         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));
   118         tmp(x,y,v) = (unsigned char)cimg::max(0.0f,cimg::min(255.0f,1.5f*tmp(x,y,v)*val/wph));
   119       }}
   120       if (std::rand()%2) tmp = (tmp.get_dilate(3)-=tmp);
   121       tmp.blur((float)cimg::rand()*0.8f).normalize(0,255);
   122       const float sin_offset = (float)cimg::crand()*3, sin_freq = (float)cimg::crand()/7;
   123       { cimg_forYV(captcha,y,v) captcha.get_shared_line(y,0,v).translate((int)(4*std::cos(y*sin_freq+sin_offset))); }
   124       captcha.draw_image(6+40*k,tmp);
   125     }
   126   }
   128   // Add geometric and random noise
   129   CImg<unsigned char> copy = (+captcha).fill(0);
   130   for (unsigned int l=0; l<3; ++l) {
   131     if (l) copy.blur(0.5f).normalize(0,148);
   132     for (unsigned int k=0; k<10; ++k) {
   133       cimg_forX(color,i) color[i] = (unsigned char)(128+cimg::rand()*127);
   134       if (cimg::rand()<0.5f) copy.draw_circle((int)(cimg::rand()*captcha.dimx()),
   135                                               (int)(cimg::rand()*captcha.dimy()),
   136                                               (int)(cimg::rand()*30),
   137                                               color,0.6f,~0U);
   138       else copy.draw_line((int)(cimg::rand()*captcha.dimx()),
   139                           (int)(cimg::rand()*captcha.dimy()),
   140                           (int)(cimg::rand()*captcha.dimx()),
   141                           (int)(cimg::rand()*captcha.dimy()),
   142                           color,0.6f);
   143     }
   144   }
   145   captcha|=copy;
   146   captcha.noise(10,2);
   148   if (add_border) captcha.draw_rectangle(0,0,captcha.dimx()-1,captcha.dimy()-1,CImg<unsigned char>::vector(255,255,255),1.0f,~0U);
   149   captcha = (+captcha).fill(255) - captcha;
   151   // Write output image and captcha text
   152   //-------------------------------------
   153   std::printf("%s\n",captcha_text);
   154   if (file_o) captcha.save(file_o);
   155   else if (visu) {
   156     CImgDisplay disp(CImg<unsigned char>(512,128,1,3,180).draw_image(128,32,captcha),captcha_text,0);
   157     while (!disp.is_closed && !disp.key) { disp.wait(); if (disp.is_resized) disp.resize(disp); }
   158   }
   159   return 0;
   160 }