#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <gd.h>
#include "gdfonts.h"
#include "gdfontt.h"
#include "gdfontl.h"
#include "gdfontl.h"
#include "gdfontmb.h"


/*
	dynamic bit compressor

	initial header:
		int32 version
		int32 error check method
			no error check
			per set crc
			md5sum end result
			other gcrypt options
		int32 set size (bytes uncompressed)

	setX header:
		char bits (bits per member)
	setX data:
		optional crc/error
		members

1 byte
	256 combinations
8 - 64 bits


1

2 - 16

3

4

5

6

7

8



baseline offset
find median of set values
find max bits needed for offset value
store base, bit size, array of offsets


*/

void Usage(void){
	printf("dbc [-v/-h] [-c] [-d] [-i] [filenames ... ]\n");

}

//const uint8_t byte_limits[8] = {2, 4, 8, 16, 32, 64, 128, 256};
const uint16_t byte_limits[8] = {3, 5, 9, 17, 33, 65, 129, 257};
const double diff_limits[11] = {1, 2, 4, 8, 16, 32, 48, 64, 128, 192, 256};

int main(int argc, char **argv){
	FILE *fp1;
	FILE *out;
	gdImagePtr im;
	im = gdImageCreate(356,356);
    //int white = gdImageColorAllocate(im, 255, 255, 255);
    int black = gdImageColorAllocate(im, 0, 0, 0);
	int green = gdImageColorAllocate(im, 0, 255, 0);
/*
    int red = gdImageColorAllocate(im, 255, 0, 0);
    int blue = gdImageColorAllocate(im, 0, 0, 255);
    int yellow = gdImageColorAllocate(im, 255, 255, 0);
    int orange = gdImageColorAllocate(im, 255, 128, 0);
    int purple = gdImageColorAllocate(im, 255, 0, 255);
    int brown = gdImageColorAllocate(im, 128, 64, 0);
    int grey = gdImageColorAllocate(im, 128, 128, 128);
*/
	uint8_t eight_b[8];
	//uint8_t eight_t[8];
	double percents[256]; //= {0,0,0,0,0,0,0,0,0,0,0};
	double diff = 0;
	double temp1 = 0;
	double temp2 = 0;
	uint8_t last_b = 0;
	uint64_t tallies[256];
	size_t tbr = 0;
	//double tbs = 0;
	//uint8_t sbr = 0;
	uint32_t n1 = 0;
	//uint8_t n2 = 0;
	char label[12];

	//(void)memset(tallies, 
//printf("here1a\n");
	for(n1=0; n1<256; n1++){
		tallies[(size_t)n1]=0;
		percents[(size_t)n1]=0;
	}
//printf("here1b\n");
	fp1 = fopen(argv[1], "rb");
	if(!fp1){
		printf("Error opening file: %s\n", argv[1]);
		return 1;
	}
//printf("here2\n");
	while(fread((void *)eight_b, 1, 1, fp1) == 1){
		tbr++;
		diff = (double)last_b - eight_b[0];
		//tallies[n2]++;
		if(diff < 0)
			diff *= -1;
		//for(n1=0; n1<256; n1++){
		//	if(diff == n1){
		//		percents[n1]++;
		//		break;
		//	}
		//}
		percents[(size_t)diff]++;
		last_b = eight_b[0];
		tallies[last_b]++;
//if( (tbr % 1000) == 0)
//	printf("%zu\n", tbr);
	}


	fclose(fp1);

	printf("\tread %zu bytes\n", tbr);
	//printf("\tpotential %.0lf bytes\n", tbs/8);
	//printf("byte values relation tally\n");
	//for(n1=0; n1<11; n1++){
	//	printf("\n%.0lf: %.02lf%% (%.0lf)\n", diff_limits[n1], percents[n1]/tbr*100, percents[n1]);
	//}
	strcpy(label, "totals");
	gdImageString(im, gdFontMediumBold,   178   ,   26        , (unsigned char *)label , black);
	gdImageLine(im, 50, 26,50,126, black);
	gdImageLine(im, 50, 126,306,126, black);
	strcpy(label, "diffs");
	gdImageString(im, gdFontMediumBold,   178   ,   152        , (unsigned char *)label , black);
	gdImageLine(im, 50, 152,50,252, black);
	gdImageLine(im, 50, 252,306,252, black);
	for(n1=0; n1<256; n1++){
		temp1 = tallies[n1];
		temp2 = tbr*100;
		temp1 /= temp2;
		gdImageLine(im, n1+50, 126-temp1 ,n1+50,126, green);
		temp1 = percents[n1];
		temp2 = tbr*100;
		temp1 /= temp2;
		gdImageLine(im, n1+50, 252-temp1,n1+50,252, green);
	}
	out = fopen("output.png", "w");
    gdImageInterlace(im, 1);
    gdImagePng(im, out);
    fclose(out);
    gdImageDestroy(im);

	return 0;
}