code:
/*
 *  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
 */

#include <libcaptcha.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stb_image.h>
#include <stb_image_write.h>

int lc_save_png(const char *filename, lc_bmp *bmp) {
  return stbi_write_png(filename, bmp->w, bmp->h,
     bmp->ch, bmp->buffer, bmp->w * bmp->ch);
}

lc_bmp * lc_load_png(const char *filename) {
  lc_bmp *img = malloc(sizeof(*img));
  uint8_t *tmp;

  if (!img) {
    errno = ENOMEM;
    return NULL;
  }

  img->type = LC_TYPE_BMP;
  /*
   * TODO:
   * An sdditional file checks to make a possible error message
   * more detailed and accurate.
   */
/*  img->buffer = stbi_load(filename, &img->w, &img->h, &img->ch, 3);*/
    tmp = stbi_load(filename, &img->w, &img->h, &img->ch, 3);
    img->buffer = malloc(img->ch * (img->w * img->h));
    bcopy(tmp, img->buffer, img->ch * (img->w * img->h));

    stbi_image_free(tmp);

  if (!img->buffer) {
    errno = ENOENT;
    free(img);
    return NULL;
  }

  return img;
}

lc_bmp * lc_crop_bmp(lc_bmp *src, int xo, int yo, int w, int h) {
  lc_bmp *bmp = malloc(sizeof(*bmp));

  if (!bmp) {
    errno = ENOMEM;
    return NULL;
  }

  /*
   * RGB is more than enough
   */
  bmp->buffer = malloc(3 * (w * h));

  if (!bmp->buffer) {
    errno = ENOMEM;
    free(bmp);
    return NULL;
  }

  bmp->type = LC_TYPE_BMP;
  bmp->w = w;
  bmp->h = h;
  bmp->ch = 3;

  for (int sy = 0, y = yo; y < (yo + h); y++, sy++) {
    for (int xy = 0, xs = 0, x = xo; x < (xo + w); x++, xy++) {
      const uint32_t it = (3 * (y * src->w + x));
      const stbi_uc *pixel = src->buffer + it;
      *(bmp->buffer + (3 * (sy * w + xy)))     = pixel[0];
      *(bmp->buffer + (3 * (sy * w + xy)) + 1) = pixel[1];
      *(bmp->buffer + (3 * (sy * w + xy)) + 2) = pixel[2];
    }
  }

  return bmp;
}

static lc_bmp * lc_create_bmp(const int width, const int height) {
  lc_bmp *bmp = malloc(sizeof(*bmp));

  if (!bmp) {
    errno = ENOMEM;
    return NULL;
  }

  bmp->buffer = calloc(sizeof(uint8_t), width * height);

  if (!bmp->buffer) {
    errno = ENOMEM;
    free(bmp);
    return NULL;
  }

  bmp->type = LC_TYPE_BMP;

  return bmp;
}

static void lc_place_glyph(lc_bmp *bmp, lc_bmpGlyph *glyph) {
  int xoff = glyph->x;
  int yoff = glyph->y;

  for (int y = 0; y < glyph->h; y++) {
    if (y >= bmp->h)
      break;
    for (int x = 0; x < glyph->w; x++) {
      if (x >= bmp->w)
        break;
      int c = *(glyph->buffer + (x + y * glyph->w));
      if (c != 0) {
        *(bmp->buffer + ((xoff + x) + (yoff + y) * bmp->w)) = c;
      }
    }
  }
}

lc_bmp * lc_create_image(lc_arrGlyph *arr) {
  lc_bmpGlyph *item;
  lc_bmp * bmp = lc_create_bmp(arr->w, arr->h);

  if (!bmp) {
    errno = ENOMEM;
    return NULL;
  }

  bmp->type = LC_TYPE_BMP;
  bmp->ch = 1;
  bmp->w = arr->w;
  bmp->h = arr->h;

  for (int i = 0; i < arr->size; i++) {
    item = arr->items[i];
    lc_place_glyph(bmp, item);
  }

  return bmp;
}


code:
/*
 *  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
 */

#include <config.h>
#include <stdio.h>
#include <libcaptcha.h>

#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>

#define STBI_NO_PNM
#define STBI_NO_PSD
#define STBI_NO_BMP
#define STBI_NO_GIF
#define STBI_NO_PIC
#define STBI_NO_TGA
/*#define STBI_NO_HDR*/
/*#define STBI_NO_JPEG*/
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

typedef struct lc_objtf {
  uint8_t type;
  void *field1;
  void *field2;
} lc_objtf;

static void lc_free_arr(lc_arrGlyph *arr) {
  for (int i = 0; i < arr->size; i++) {
    lc_free(arr->items[i]);
  }

  free(arr->items);
  free(arr);
}

void lc_free(void *obj) {
  lc_objtf *o = obj;
  if (o != NULL) {
    if (o->type == LC_TYPE_ARR) {
      lc_free_arr((lc_arrGlyph *)obj);
      return;
    }

    if (o->field1) {
      free(o->field1);
    }

    if (o->type == LC_TYPE_FONT &&
        o->field2) {
      free(o->field2);
    }

    free(o);
  }
}

repo_name: acetone/libcaptcha

license: mit


code:
/*
 *  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


code:
/*
 *  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
 */

#include <libcaptcha.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <stb_image.h>

static lc_bmp * lc_create_3ch_bmp(int w, int h) {
  lc_bmp *bmp = malloc(sizeof(*bmp));

  if (!bmp) {
    errno = ENOMEM;
    return NULL;
  }

  bmp->buffer = calloc(1, 3 * (w * h));

  if (!bmp->buffer) {
    errno = ENOMEM;
    free(bmp);
    return NULL;
  }

  bmp->type = LC_TYPE_BMP;
  bmp->w = w;
  bmp->h = h;
  bmp->ch = 3;

  return bmp;
}

lc_bmp * lc_pattern_merge(lc_bmp *bg, lc_bmp *src, int xo, int yo) {
  lc_bmp *bmp = lc_create_3ch_bmp(bg->w, bg->h);

  if (!bmp) {
    errno = ENOMEM;
    return NULL;
  }

  for (int y = 0; y < bmp->h; y++) {
    for (int x = 0; x < bmp->w; x++) {
      const uint32_t it = (3 * (y * bg->w + x));
      const stbi_uc *pixel = bg->buffer + it;
      *(bmp->buffer + (3 * (y * bmp->w + x)))     = pixel[0];
      *(bmp->buffer + (3 * (y * bmp->w + x)) + 1) = pixel[1];
      *(bmp->buffer + (3 * (y * bmp->w + x)) + 2) = pixel[2];
    }
  }

  for (int y = 0; y < bmp->h; y++) {
    for (int x = 0; x < bmp->w; x++) {
      if (x < src->w && y < src->h && *(src->buffer + (3 * (y * src->w + x))) != 0) {
        int ym = y + yo;
        int xm = x + xo;
        *(bmp->buffer + (3 * (ym * bmp->w + xm))) = *(src->buffer + (3 * (y * src->w + x)));
        *(bmp->buffer + (3 * (ym * bmp->w + xm)) + 1) = *(src->buffer + (3 * (y * src->w + x) + 1));
        *(bmp->buffer + (3 * (ym * bmp->w + xm)) + 2) = *(src->buffer + (3 * (y * src->w + x) + 2));
      }
    }
  }
  return bmp;
}

lc_bmp * lc_pattern_apply(lc_bmp *pattern, lc_bmp *src) {
  lc_bmp *bmp = lc_create_3ch_bmp(src->w, src->h);

  if (!bmp) {
    errno = ENOMEM;
    return NULL;
  }

  for (int y = 0; y < bmp->h; y++) {
    for (int x = 0; x < bmp->w; x++) {
      if (*(src->buffer + (y * bmp->w + x)) == 0) {
        continue;
      }

      const uint32_t it = (3 * (y * pattern->w + x));
      const stbi_uc *pixel = pattern->buffer + it;

      *(bmp->buffer + (3 * (y * bmp->w + x)))     = pixel[0];
      *(bmp->buffer + (3 * (y * bmp->w + x)) + 1) = pixel[1];
      *(bmp->buffer + (3 * (y * bmp->w + x)) + 2) = pixel[2];
    }
  }

  return bmp;
}