Skip to content

Commit

Permalink
a working prototype with libgd
Browse files Browse the repository at this point in the history
  • Loading branch information
sinofool committed Oct 12, 2013
1 parent fc07509 commit 584495c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
.DS_Store

# Compiled Object files
*.slo
*.lo
Expand All @@ -11,3 +13,7 @@
*.lai
*.la
*.a

# Eclipse project and workspaces
.metadata
.project
25 changes: 25 additions & 0 deletions Makefile
@@ -0,0 +1,25 @@

CPP=g++
LIBGD_HOME=$(HOME)/code/build/libgd

CXXFLAGS=-I$(LIBGD_HOME)/include
LDFLAGS=-L$(LIBGD_HOME)/lib -lgd

.PHONY: all
.PHONY: clean
.PHONY: run

all: ownCAPTCHA
echo "Welcome to ownCAPTCHA"

clean:
rm ownCAPTCHA

run:
./ownCAPTCHA

*.o: *.cpp
$(CPP) -c -g -o $@ $(CXXFLAGS) $<

ownCAPTCHA: src/main/cpp/main.cpp
$(CPP) -g -o $@ $(CXXFLAGS) $(LDFLAGS) $^
1 change: 1 addition & 0 deletions README.md
@@ -1,2 +1,3 @@
ownCAPTCHA
==========
CAPTCHA is a common comp
73 changes: 73 additions & 0 deletions src/main/cpp/captcha.hpp
@@ -0,0 +1,73 @@
#include <gd.h>
#include <string>
#include <stdio.h>

struct rgb {
rgb(uint8_t red, uint8_t green, uint8_t blue) : r(red), g(green), b(blue) {};
uint8_t r;
uint8_t g;
uint8_t b;
};

class background {
public:
background(int width, int height, rgb rgb) :
_width(width), _height(height), _rgb(rgb) {}

template<typename T> background* merge(T old) {
this->_img = gdImageCreateTrueColor(_width, _height);
int bg = gdImageColorResolve(_img, _rgb.r, _rgb.g, _rgb.b);
gdImageFill(_img, 0, 0, bg);
return this;
};
gdImagePtr img() {
return _img;
}
private:
gdImagePtr _img;
int _width;
int _height;
rgb _rgb;
};

class text {
public:
text(const std::string& text, const std::string& font, double size, double angle, int x, int y, rgb rgb) :
_text(text), _font(font), _size(size), _angle(angle), _x(x), _y(y), _rgb(rgb) {};
template<typename T> text* merge(T old) {
this->_img = old->img();
int brect[8];
int color = gdImageColorResolve(_img, _rgb.r, _rgb.g, _rgb.b);
gdImageStringFT(_img, &brect[0],
color, const_cast<char*>(_font.c_str()), _size,
_angle, _x, _y, const_cast<char*>(_text.c_str()));
return this;
};
gdImagePtr img() {
return _img;
}
private:
std::string _text;
std::string _font;
double _size;
double _angle;
int _x;
int _y;
rgb _rgb;
gdImagePtr _img;
};

class save {
public:
save(const std::string& filename) :
_filename(filename) {};
template<typename T> save* merge(T old) {
FILE* f = fopen(_filename.c_str(), "wb");
gdImagePng(old->img(), f);
fclose(f);
gdImageDestroy(old->img());
return NULL;
};
private:
std::string _filename;
};
Empty file added src/main/cpp/distorting.h
Empty file.
14 changes: 14 additions & 0 deletions src/main/cpp/main.cpp
@@ -0,0 +1,14 @@
#include <iostream>

#include "captcha.hpp"

int main() {
std::cout << "Welcome to ownCAPTCHA" << std::endl;

background bg(300, 200, rgb(0x00, 0xF0, 0x00));
text text("Hello", "./au.ttf", 40, 1, 150, 100, rgb(0x00, 0x00, 0xF0));
save save("background.png");
save.merge(text.merge(bg.merge(NULL)));

return 0;
}
Empty file added src/main/cpp/noise.h
Empty file.

0 comments on commit 584495c

Please sign in to comment.