/*
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  Author: g0tsu
 *  Email:  g0tsu at dnmx.0rg
 */

#ifndef _L_LIBCAPTCHA_H
#define _L_LIBCAPTCHA_H

#include <stdint.h>
#include <stddef.h>
#include <math.h>

#ifndef u_uint32_t
typedef uint32_t u_int32_t;
#endif

#define MAX(a,b) (((a)>(b))?(a):(b))

enum LC_TYPES {
  LC_TYPE_FONT = 0,
  LC_TYPE_GLYPH,
  LC_TYPE_ARR,
  LC_TYPE_BMP
};

typedef struct lc_fontBuffer {
  uint8_t type;
  uint8_t *buffer;
  void *info;
} lc_fontBuffer;

typedef struct lc_bmpGlyph {
  uint8_t type;
  uint8_t *buffer;
  int x;
  int y;
  int w;
  int h;
} lc_bmpGlyph;

typedef struct lc_arrGlyph {
  uint8_t type;
  lc_bmpGlyph **items; // never do it at home
  int size;
  int w;
  int h;
} lc_arrGlyph;

typedef struct lc_bmp {
  uint8_t type;
  uint8_t *buffer;
  int w;
  int h;
  int ch;
} lc_bmp;

lc_fontBuffer * lc_create_font(char *filename);
lc_bmpGlyph * lc_create_glyph(lc_fontBuffer *font,
    const int glyph, const int line_height);

void lc_free(void *obj);

lc_arrGlyph * lc_str_to_arr(lc_fontBuffer * font, char *str,
    const int line_height, const int delta);

lc_bmpGlyph * lc_rotate_glyph(lc_bmpGlyph *glyph, int angle);

lc_bmp * lc_create_image(lc_arrGlyph *arr);
int lc_random_bytes(void *bytes, size_t size);

/* randomizers */
void lc_randomize_arr_x(lc_arrGlyph *arr, int delta);
void lc_randomize_arr_y(lc_arrGlyph *arr, int delta);
lc_arrGlyph * lc_randomize_arr_rotation(lc_arrGlyph *arr, int delta);
void lc_recalculate_arr(lc_arrGlyph *arr);

int lc_save_png(const char *filename, lc_bmp *bmp);
lc_bmp * lc_load_png(const char *filename);
lc_bmp * lc_crop_bmp(lc_bmp *src, int x, int y, int w, int h);
lc_bmp * lc_pattern_apply(lc_bmp *pattern, lc_bmp *dst);
lc_bmp * lc_pattern_merge(lc_bmp *bg, lc_bmp *bmp, int x, int y);

#endif

