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