1 #include "../../../include/freenet/captcha/simplecaptcha.h"
\r
2 #include "../../../include/freenet/captcha/easybmp/EasyBMP.h"
\r
3 #include "../../../include/freenet/captcha/easybmp/EasyBMP_Font.h"
\r
4 #include "../../../include/freenet/captcha/easybmp/EasyBMP_Geometry.h"
\r
12 void SimpleCaptcha::Generate()
\r
17 RGBApixel lettercols[5];
\r
18 std::string puzzlestring;
\r
19 std::string tempfilename=GenerateRandomString(10);
\r
20 tempfilename+=".bmp";
\r
22 puzzlestring=GenerateRandomString(5);
\r
24 m_solution.insert(m_solution.begin(),puzzlestring.begin(),puzzlestring.end());
\r
26 bmp.SetSize(bmpwidth,bmpheight);
\r
28 // first draw some random lines around the bitmap
\r
29 int numlines=(rand()%10)+10;
\r
30 for(int i=0; i<numlines; i++)
\r
33 int x1=rand()%bmpwidth;
\r
34 int y1=rand()%bmpheight;
\r
35 int x2=rand()%bmpwidth;
\r
36 int y2=rand()%bmpwidth;
\r
37 // keep the colors light
\r
38 pixel.Red=100+(rand()%150);
\r
39 pixel.Green=100+(rand()%150);
\r
40 pixel.Blue=100+(rand()%150);
\r
41 DrawAALine(bmp,x1,y1,x2,y2,pixel);
\r
44 // draw some random arcs
\r
45 int numarcs=(rand()%10)+10;
\r
46 for(int i=0; i<numarcs; i++)
\r
49 int x1=rand()%bmpwidth;
\r
50 int y1=rand()%bmpwidth;
\r
51 int radius=rand()%(bmpheight/2);
\r
52 double startangle=(double)(rand()%360)*(3.14159/180);
\r
53 double endangle=(double)(rand()%360)*(3.14159/180);
\r
54 pixel.Red=100+(rand()%150);
\r
55 pixel.Green=100+(rand()%150);
\r
56 DrawArc(bmp,x1,y1,radius,startangle,endangle,pixel);
\r
59 for(int i=0; i<5; i++)
\r
61 // keep the colors dark
\r
62 lettercols[i].Red=(rand()%200);
\r
63 lettercols[i].Green=(rand()%200);
\r
64 lettercols[i].Blue=(rand()%200);
\r
65 // draw a line with the letter col
\r
66 DrawAALine(bmp,rand()%bmpwidth,rand()%bmpheight,rand()%bmpwidth,rand()%bmpheight,lettercols[i]);
\r
69 // print puzzle onto bitmap
\r
70 for(int i=0; i<5; i++)
\r
72 PrintLetter(bmp,puzzlestring[i],5+(i*20)+(rand()%11)-5,10+(rand()%10),20,lettercols[i]);
\r
75 /* TOO dificult to solve with this
\r
76 // skew image left/right
\r
78 for(int yy=0; yy<bmpheight; yy++)
\r
81 skew=skew+(double)((rand()%11)-5)/20.0;
\r
83 for(xx=0; xx<skew; xx++)
\r
88 bmp.SetPixel(xx,yy,pixel);
\r
92 for(xx=0; xx<bmpwidth+skew; xx++)
\r
94 pixel=bmp.GetPixel(xx-skew,yy);
\r
95 bmp.SetPixel(xx,yy,pixel);
\r
100 for(xx=bmpwidth-1; xx>skew; xx--)
\r
102 pixel=bmp.GetPixel(xx-skew,yy);
\r
103 bmp.SetPixel(xx,yy,pixel);
\r
106 for(xx=bmpwidth+skew; xx<bmpwidth; xx++)
\r
111 bmp.SetPixel(xx,yy,pixel);
\r
116 // generate noise for each pixel
\r
117 for(int yy=0; yy<bmpheight; yy++)
\r
119 for(int xx=0; xx<bmpwidth; xx++)
\r
121 RGBApixel pixel=bmp.GetPixel(xx,yy);
\r
122 int tempred=pixel.Red+(rand()%21)-10;
\r
123 int tempgreen=pixel.Green+(rand()%21)-10;
\r
124 int tempblue=pixel.Blue+(rand()%21)-10;
\r
126 tempred < 0 ? tempred=0 : false;
\r
127 tempred > 255 ? tempred=255 : false;
\r
128 tempgreen < 0 ? tempgreen=0 : false;
\r
129 tempgreen > 255 ? tempgreen=255 : false;
\r
130 tempblue < 0 ? tempblue=0 : false;
\r
131 tempblue > 255 ? tempblue=255 : false;
\r
134 pixel.Green=tempgreen;
\r
135 pixel.Blue=tempblue;
\r
137 bmp.SetPixel(xx,yy,pixel);
\r
141 bmp.WriteToFile(tempfilename.c_str());
\r
142 ReadPuzzleData(tempfilename);
\r
143 unlink(tempfilename.c_str());
\r
146 const std::string SimpleCaptcha::GenerateRandomString(const int len)
\r
148 // no l,1 O,0 because they look too much alike
\r
149 static std::string chars="abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
\r
150 std::string temp="";
\r
151 for(int i=0; i<len; i++)
\r
153 temp+=chars[rand()%chars.size()];
\r
158 const bool SimpleCaptcha::GetPuzzle(std::vector<unsigned char> &puzzle)
\r
164 const bool SimpleCaptcha::GetSolution(std::vector<unsigned char> &solution)
\r
166 solution=m_solution;
\r
170 void SimpleCaptcha::ReadPuzzleData(const std::string &filename)
\r
173 FILE *infile=fopen(filename.c_str(),"rb");
\r
176 fseek(infile,0,SEEK_END);
\r
177 filelen=ftell(infile);
\r
178 fseek(infile,0,SEEK_SET);
\r
179 m_puzzle.resize(filelen);
\r
180 fread(&m_puzzle[0],1,filelen,infile);
\r