/*
 *  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;
}

