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