Blame simple/neural/convertsyms.c

56d550
56d550
#include <stdio.h></stdio.h>
56d550
#include <stdlib.h></stdlib.h>
56d550
#include <string.h></string.h>
56d550
56d550
56d550
int main() {
56d550
  int sizeX = 28;
56d550
  int sizeY = 28;
56d550
  int border = 2;
56d550
  int outSizeX = 22;
56d550
  int outSizeY = 22;
56d550
  const char *filename = "data/symbols-data.bin";
56d550
  const char *outFilename = "data/output/symbols22-data.bin";
56d550
56d550
56d550
  printf("read data from '%s'\n", filename);
56d550
  FILE *f = fopen(filename, "rb");
56d550
  if (!f)
56d550
    return printf("cannot open file\n"), 1;
56d550
  fseek(f, 0, SEEK_END);
56d550
  size_t fs = ftell(f);
56d550
  fseek(f, 0, SEEK_SET);
56d550
56d550
  size_t testSize = sizeX*sizeY + 1;
56d550
  int count = fs/testSize;
56d550
  if (!count)
56d550
    return printf("file is lesser minimal size\n"), fclose(f), 1;
56d550
56d550
  unsigned char *data = calloc(testSize, count);
56d550
  if (!fread(data, testSize*count, 1, f))
56d550
    return printf("cannot read"), free(data), fclose(f), 1;
56d550
  fclose(f);
56d550
56d550
  printf("write converted data to '%s'\n", outFilename);
56d550
  f = fopen(outFilename, "wb");
56d550
  if (!f)
56d550
    return printf("cannot open file\n"), 1;
56d550
56d550
  unsigned char *img = calloc(outSizeX*outSizeY+1, 1);
56d550
  int sx = sizeX - 2*border - 1, sy = sizeY - 2*border - 1;
56d550
  int osx = outSizeX - 2*border - 1, osy = outSizeY - 2*border - 1;
56d550
  for(int i = 0; i < count; ++i) {
56d550
    unsigned char *in = data + testSize*i;
56d550
    for(int y = 0; y <= osy; ++y) {
56d550
      int y0 = y*sy;
56d550
      int py = y0%osy;
56d550
      y0 /= osy;
56d550
      int y1 = y0 < sy ? y0 : sy;
56d550
      for(int x = 0; x <= osx; ++x) {
56d550
        int x0 = x*sx;
56d550
        int px = x0%osx;
56d550
        x0 /= osx;
56d550
        int x1 = x0 < sx ? x0 : sx-1;
56d550
56d550
        int p00 = in[(border+y0)*sizeX + x0];
56d550
        int p01 = in[(border+y0)*sizeX + x1];
56d550
        int p0 = p00*(osx-px) + p01*px;
56d550
56d550
        int p10 = in[(border+y1)*sizeX + x0];
56d550
        int p11 = in[(border+y1)*sizeX + x1];
56d550
        int p1 = p10*(osx-px) + p11*px;
56d550
56d550
        int p = p0*(osy-py) + p1*py;
56d550
        p = (p + osx*osy/2)/(osx*osy);
56d550
56d550
        img[(y+border)*outSizeX + x] = (unsigned int)p1;
56d550
      }
56d550
    }
56d550
    img[outSizeX*outSizeY] = in[sizeX*sizeY];
56d550
    if (!fwrite(img, outSizeX*outSizeY+1, 1, f))
56d550
      return printf("cannot write\n"), free(data), free(img), fclose(f), 1;
56d550
  }
56d550
  free(data);
56d550
  free(img);
56d550
56d550
  fclose(f);
56d550
56d550
  return printf("done\n");
56d550
  return 0;
56d550
}