1 #include "../../../include/freenet/captcha/alternatecaptcha2.h"
\r
3 #include <Poco/Path.h>
\r
4 #include <Poco/DirectoryIterator.h>
\r
9 #ifdef ALTERNATE_CAPTCHA
\r
11 AlternateCaptcha2::AlternateCaptcha2()
\r
13 AlternateCaptchaFonts fonts;
\r
14 m_fonts=fonts.Fonts();
\r
17 void AlternateCaptcha2::Generate()
\r
19 std::string puzzlestring=GenerateRandomString(6);
\r
20 std::vector<int> fontnums(puzzlestring.size(),0);
\r
25 FreeImage::Bitmap bigbmp(300,300,32);
\r
26 bigbmp.SetTransparent();
\r
28 // get total width of text
\r
30 for(int i=0; i<puzzlestring.size(); i++)
\r
32 fontnums[i]=rand()%m_fonts.size();
\r
33 textwidth+=m_fonts[fontnums[i]].Char(puzzlestring[i]).Width();
\r
40 white.rgbReserved=255;
\r
41 int numlines=(rand()%3)+2;
\r
42 for(int i=0; i<numlines; i++)
\r
45 int y1=(rand()%100)+100;
\r
46 int x2=(rand()%150)+150;
\r
47 int y2=(rand()%100)+100;
\r
48 bigbmp.Line(x1,y1,x2,y2,white);
\r
49 bigbmp.Line(x1+1,y1,x2+1,y2,white);
\r
52 // draw the text on the bigbmp centered
\r
53 int currentx=150-(textwidth/2);
\r
54 for(int i=0; i<puzzlestring.size(); i++)
\r
56 FreeImage::Bitmap charbmp=m_fonts[fontnums[i]].Char(puzzlestring[i]);
\r
57 bigbmp.BlitTrans(charbmp,currentx,150-(charbmp.Height()/2),0,0,charbmp.Width(),charbmp.Height());
\r
58 currentx+=m_fonts[fontnums[i]].Char(puzzlestring[i]).Width();//+1;
\r
61 // rotate and skew the big bitmap a few times
\r
64 int numrots=(rand()%4)+4;
\r
65 for(int i=0; i<numrots; i++)
\r
67 thisrot=((rand()%180)-90);
\r
68 bigbmp.Rotate(-lastrot+thisrot,0,0,150,150);
\r
69 int offset=rand()%10000;
\r
70 float freq=3.0+(float(rand()%7000)/1000.0);
\r
71 float amp=1.0+(float(rand()%1000)/500.0);
\r
72 for(int y=0; y<bigbmp.Height(); y++)
\r
74 double shift=sin((double)(y+offset)/freq)*amp;
\r
75 bigbmp.HorizontalOffset(y,shift);
\r
80 // rotate the big bitmap back to (almost) horizontal
\r
81 bigbmp.Rotate(-lastrot+((rand()%10)-5),0,0,150,150);
\r
83 // grab the center of the big bitmap as the final bitmap
\r
84 FreeImage::Bitmap bmp(110,50,32);
\r
85 bmp.SetTransparent();
\r
86 bmp.Blit(bigbmp,0,0,95,125,110,50,255);
\r
88 m_solution.insert(m_solution.end(),puzzlestring.begin(),puzzlestring.end());
\r
89 bmp.SaveToMemory("bmp",m_puzzle);
\r
93 const std::string AlternateCaptcha2::GenerateRandomString(const int len)
\r
95 // no l,1 O,0 because they look too much alike
\r
96 static std::string chars="abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789@#";
\r
97 std::string temp="";
\r
98 for(int i=0; i<len; i++)
\r
100 temp+=chars[rand()%chars.size()];
\r
105 const bool AlternateCaptcha2::GetPuzzle(std::vector<unsigned char> &puzzle)
\r
111 const bool AlternateCaptcha2::GetSolution(std::vector<unsigned char> &solution)
\r
113 solution=m_solution;
\r
117 #endif // ALTERNATE_CAPTCHA
\r