Wed, 05 Aug 2009 15:00:54 +0100
small changes to hexdump code to stop a gcc warning on platforms where sizeof(int) != sizeof(int*) e.g. x86_64
philpem@5 | 1 | /* |
philpem@5 | 2 | # |
philpem@5 | 3 | # File : captcha.cpp |
philpem@5 | 4 | # ( C++ source file ) |
philpem@5 | 5 | # |
philpem@5 | 6 | # Description : Captcha images generator. |
philpem@5 | 7 | # This file is a part of the CImg Library project. |
philpem@5 | 8 | # ( http://cimg.sourceforge.net ) |
philpem@5 | 9 | # |
philpem@5 | 10 | # Copyright : David Tschumperle |
philpem@5 | 11 | # ( http://www.greyc.ensicaen.fr/~dtschump/ ) |
philpem@5 | 12 | # |
philpem@5 | 13 | # License : CeCILL v2.0 |
philpem@5 | 14 | # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) |
philpem@5 | 15 | # |
philpem@5 | 16 | # This software is governed by the CeCILL license under French law and |
philpem@5 | 17 | # abiding by the rules of distribution of free software. You can use, |
philpem@5 | 18 | # modify and/ or redistribute the software under the terms of the CeCILL |
philpem@5 | 19 | # license as circulated by CEA, CNRS and INRIA at the following URL |
philpem@5 | 20 | # "http://www.cecill.info". |
philpem@5 | 21 | # |
philpem@5 | 22 | # As a counterpart to the access to the source code and rights to copy, |
philpem@5 | 23 | # modify and redistribute granted by the license, users are provided only |
philpem@5 | 24 | # with a limited warranty and the software's author, the holder of the |
philpem@5 | 25 | # economic rights, and the successive licensors have only limited |
philpem@5 | 26 | # liability. |
philpem@5 | 27 | # |
philpem@5 | 28 | # In this respect, the user's attention is drawn to the risks associated |
philpem@5 | 29 | # with loading, using, modifying and/or developing or reproducing the |
philpem@5 | 30 | # software by the user in light of its specific status of free software, |
philpem@5 | 31 | # that may mean that it is complicated to manipulate, and that also |
philpem@5 | 32 | # therefore means that it is reserved for developers and experienced |
philpem@5 | 33 | # professionals having in-depth computer knowledge. Users are therefore |
philpem@5 | 34 | # encouraged to load and test the software's suitability as regards their |
philpem@5 | 35 | # requirements in conditions enabling the security of their systems and/or |
philpem@5 | 36 | # data to be ensured and, more generally, to use and operate it in the |
philpem@5 | 37 | # same conditions as regards security. |
philpem@5 | 38 | # |
philpem@5 | 39 | # The fact that you are presently reading this means that you have had |
philpem@5 | 40 | # knowledge of the CeCILL license and that you accept its terms. |
philpem@5 | 41 | # |
philpem@5 | 42 | */ |
philpem@5 | 43 | |
philpem@5 | 44 | #ifndef cimg_debug |
philpem@5 | 45 | #define cimg_debug 1 |
philpem@5 | 46 | #endif |
philpem@5 | 47 | #include "CImg.h" |
philpem@5 | 48 | using namespace cimg_library; |
philpem@5 | 49 | |
philpem@5 | 50 | // The lines below are necessary when using a non-standard compiler such as visualcpp6. |
philpem@5 | 51 | #ifdef cimg_use_visualcpp6 |
philpem@5 | 52 | #define std |
philpem@5 | 53 | #endif |
philpem@5 | 54 | #ifdef min |
philpem@5 | 55 | #undef min |
philpem@5 | 56 | #undef max |
philpem@5 | 57 | #endif |
philpem@5 | 58 | |
philpem@5 | 59 | // Start main procedure |
philpem@5 | 60 | //---------------------- |
philpem@5 | 61 | int main(int argc,char **argv) { |
philpem@5 | 62 | |
philpem@5 | 63 | // Read command line parameters |
philpem@5 | 64 | //------------------------------ |
philpem@5 | 65 | cimg_usage("Captcha generator"); |
philpem@5 | 66 | const char *file_o = cimg_option("-o",(char*)0,"Output image file"); |
philpem@5 | 67 | const bool add_border = cimg_option("-b",true,"Add border to captcha image"); |
philpem@5 | 68 | const bool visu = cimg_option("-visu",true,"Enable visualization if no output file"); |
philpem@5 | 69 | |
philpem@5 | 70 | // Generate captcha text (6 char max). |
philpem@5 | 71 | //------------------------------------ |
philpem@5 | 72 | const char *predef_words[] = { |
philpem@5 | 73 | "aarrgh", "abacas", "abacus", "abakas", "abamps", "abased", "abaser", "abases", "abasia", "abated", "abater", "abates", "abatis", "abator", |
philpem@5 | 74 | "baobab", "barbal", "barbed", "barbel", "barber", "barbes", "barbet", "barbie", "barbut", "barcas", "barded", "bardes", "bardic", "barege", |
philpem@5 | 75 | "cavies", "cavils", "caving", "cavity", "cavort", "cawing", "cayman", "cayuse", "ceased", "ceases", "cebids", "ceboid", "cecity", "cedarn", |
philpem@5 | 76 | "dicast", "dicers", "dicier", "dicing", "dicker", "dickey", "dickie", "dicots", "dictum", "didact", "diddle", "diddly", "didies", "didoes", |
philpem@5 | 77 | "emails", "embalm", "embank", "embark", "embars", "embays", "embeds", "embers", "emblem", "embody", "emboli", "emboly", "embosk", "emboss", |
philpem@5 | 78 | "fluffy", "fluids", "fluish", "fluked", "flukes", "flukey", "flumed", "flumes", "flumps", "flunks", "flunky", "fluors", "flurry", "fluted", |
philpem@5 | 79 | "genome", "genoms", "genres", "genros", "gentes", "gentil", "gentle", "gently", "gentry", "geodes", "geodic", "geoids", "gerahs", "gerbil", |
philpem@5 | 80 | "hotter", "hottie", "houdah", "hounds", "houris", "hourly", "housed", "housel", "houser", "houses", "hovels", "hovers", "howdah", "howdie", |
philpem@5 | 81 | "inland", "inlays", "inlets", "inlier", "inmate", "inmesh", "inmost", "innage", "innate", "inners", "inning", "inpour", "inputs", "inroad", |
philpem@5 | 82 | "joypop", "jubbah", "jubhah", "jubile", "judder", "judged", "judger", "judges", "judoka", "jugate", "jugful", "jugged", "juggle", "jugula", |
philpem@5 | 83 | "knifer", "knifes", "knight", "knives", "knobby", "knocks", "knolls", "knolly", "knosps", "knotty", "knouts", "knower", "knowns", "knubby", |
philpem@5 | 84 | "legate", "legato", "legend", "legers", "legged", "leggin", "legion", "legist", "legits", "legman", "legmen", "legong", "legume", "lehuas", |
philpem@5 | 85 | "mammal", "mammas", "mammee", "mammer", "mammet", "mammey", "mammie", "mammon", "mamzer", "manage", "manana", "manats", "manche", "manege", |
philpem@5 | 86 | "nihils", "nilgai", "nilgau", "nilled", "nimble", "nimbly", "nimbus", "nimmed", "nimrod", "ninety", "ninjas", "ninons", "ninths", "niobic", |
philpem@5 | 87 | "offish", "offkey", "offset", "oftest", "ogdoad", "oghams", "ogival", "ogives", "oglers", "ogling", "ogress", "ogrish", "ogrism", "ohmage", |
philpem@5 | 88 | "papaws", "papaya", "papers", "papery", "pappus", "papula", "papule", "papyri", "parade", "paramo", "parang", "paraph", "parcel", "pardah", |
philpem@5 | 89 | "quasar", "quatre", "quaver", "qubits", "qubyte", "queans", "queasy", "queazy", "queens", "queers", "quelea", "quells", "quench", "querns", |
philpem@5 | 90 | "raised", "raiser", "raises", "raisin", "raitas", "rajahs", "rakees", "rakers", "raking", "rakish", "rallye", "ralphs", "ramada", "ramate", |
philpem@5 | 91 | "savory", "savour", "savoys", "sawers", "sawfly", "sawing", "sawlog", "sawney", "sawyer", "saxony", "sayeds", "sayers", "sayest", "sayids", |
philpem@5 | 92 | "tondos", "toneme", "toners", "tongas", "tonged", "tonger", "tongue", "tonics", "tonier", "toning", "tonish", "tonlet", "tonner", "tonnes", |
philpem@5 | 93 | "uredia", "uredos", "ureide", "uremia", "uremic", "ureter", "uretic", "urgent", "urgers", "urging", "urials", "urinal", "urines", "uropod", |
philpem@5 | 94 | "villus", "vimina", "vinals", "vincas", "vineal", "vinery", "vinier", "vinify", "vining", "vinous", "vinyls", "violas", "violet", "violin", |
philpem@5 | 95 | "webfed", "weblog", "wechts", "wedded", "wedder", "wedeln", "wedels", "wedged", "wedges", "wedgie", "weeded", "weeder", "weekly", "weened", |
philpem@5 | 96 | "xystoi", "xystos", "xystus", "yabber", "yabbie", "yachts", "yacked", "yaffed", "yagers", "yahoos", "yairds", "yakked", "yakker", "yakuza", |
philpem@5 | 97 | "zigged", "zigzag", "zillah", "zinced", "zincic", "zincky", "zinebs", "zinged", "zinger", "zinnia", "zipped", "zipper", "zirams", "zircon" }; |
philpem@5 | 98 | cimg::srand(); |
philpem@5 | 99 | const char *const captcha_text = predef_words[(int)std::rand()%(sizeof(predef_words)/7)]; |
philpem@5 | 100 | |
philpem@5 | 101 | // Create captcha image |
philpem@5 | 102 | //---------------------- |
philpem@5 | 103 | |
philpem@5 | 104 | // Write colored and distorted text |
philpem@5 | 105 | CImg<unsigned char> captcha(256,64,1,3,0), color(3); |
philpem@5 | 106 | char letter[2] = { 0 }; |
philpem@5 | 107 | for (unsigned int k=0; k<6; ++k) { |
philpem@5 | 108 | CImg<unsigned char> tmp; |
philpem@5 | 109 | *letter = captcha_text[k]; |
philpem@5 | 110 | if (*letter) { |
philpem@5 | 111 | { cimg_forX(color,i) color[i] = (unsigned char)(128+(std::rand()%127)); } |
philpem@5 | 112 | tmp.draw_text((int)(2+8*cimg::rand()), |
philpem@5 | 113 | (int)(12*cimg::rand()), |
philpem@5 | 114 | letter,color.ptr(),0,1,std::rand()%2?38:57); |
philpem@5 | 115 | const unsigned int dir = std::rand()%4, wph = tmp.dimx()+tmp.dimy(); |
philpem@5 | 116 | { cimg_forXYV(tmp,x,y,v) { |
philpem@5 | 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)); |
philpem@5 | 118 | tmp(x,y,v) = (unsigned char)cimg::max(0.0f,cimg::min(255.0f,1.5f*tmp(x,y,v)*val/wph)); |
philpem@5 | 119 | }} |
philpem@5 | 120 | if (std::rand()%2) tmp = (tmp.get_dilate(3)-=tmp); |
philpem@5 | 121 | tmp.blur((float)cimg::rand()*0.8f).normalize(0,255); |
philpem@5 | 122 | const float sin_offset = (float)cimg::crand()*3, sin_freq = (float)cimg::crand()/7; |
philpem@5 | 123 | { cimg_forYV(captcha,y,v) captcha.get_shared_line(y,0,v).translate((int)(4*std::cos(y*sin_freq+sin_offset))); } |
philpem@5 | 124 | captcha.draw_image(6+40*k,tmp); |
philpem@5 | 125 | } |
philpem@5 | 126 | } |
philpem@5 | 127 | |
philpem@5 | 128 | // Add geometric and random noise |
philpem@5 | 129 | CImg<unsigned char> copy = (+captcha).fill(0); |
philpem@5 | 130 | for (unsigned int l=0; l<3; ++l) { |
philpem@5 | 131 | if (l) copy.blur(0.5f).normalize(0,148); |
philpem@5 | 132 | for (unsigned int k=0; k<10; ++k) { |
philpem@5 | 133 | cimg_forX(color,i) color[i] = (unsigned char)(128+cimg::rand()*127); |
philpem@5 | 134 | if (cimg::rand()<0.5f) copy.draw_circle((int)(cimg::rand()*captcha.dimx()), |
philpem@5 | 135 | (int)(cimg::rand()*captcha.dimy()), |
philpem@5 | 136 | (int)(cimg::rand()*30), |
philpem@5 | 137 | color,0.6f,~0U); |
philpem@5 | 138 | else copy.draw_line((int)(cimg::rand()*captcha.dimx()), |
philpem@5 | 139 | (int)(cimg::rand()*captcha.dimy()), |
philpem@5 | 140 | (int)(cimg::rand()*captcha.dimx()), |
philpem@5 | 141 | (int)(cimg::rand()*captcha.dimy()), |
philpem@5 | 142 | color,0.6f); |
philpem@5 | 143 | } |
philpem@5 | 144 | } |
philpem@5 | 145 | captcha|=copy; |
philpem@5 | 146 | captcha.noise(10,2); |
philpem@5 | 147 | |
philpem@5 | 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); |
philpem@5 | 149 | captcha = (+captcha).fill(255) - captcha; |
philpem@5 | 150 | |
philpem@5 | 151 | // Write output image and captcha text |
philpem@5 | 152 | //------------------------------------- |
philpem@5 | 153 | std::printf("%s\n",captcha_text); |
philpem@5 | 154 | if (file_o) captcha.save(file_o); |
philpem@5 | 155 | else if (visu) { |
philpem@5 | 156 | CImgDisplay disp(CImg<unsigned char>(512,128,1,3,180).draw_image(128,32,captcha),captcha_text,0); |
philpem@5 | 157 | while (!disp.is_closed && !disp.key) { disp.wait(); if (disp.is_resized) disp.resize(disp); } |
philpem@5 | 158 | } |
philpem@5 | 159 | return 0; |
philpem@5 | 160 | } |
philpem@5 | 161 |