// mesh_3b.c

#include <stdio.h>
#include <stdlib.h>

#define X_SIZE	32
#define Y_SIZE	32
#define X_BLOCK	4
#define Y_BLOCK	4
#define DIM	((Y_SIZE / Y_BLOCK) * (X_SIZE / X_BLOCK))

typedef unsigned char u_char;

void pattern(int n);
double simple_similarity(int* fv_mesh_a, int* fv_mesh_b);
void mesh(int* fv_mesh);
void load_image_data(char* f_name);

u_char	image[Y_SIZE][X_SIZE];

int main()
{
	int	i;

	for (i = 0; i < 10; i++) {
		pattern(i);
	}
	return 0;
}

void pattern(int n)
{
	char	f_name[256];
	int	fv_mesh_a[DIM];
	int	fv_mesh_b[DIM];
	int	i, imax;
	double	s, smax;

	sprintf_s(f_name, _countof(f_name), "num%d.pgm", n);
	load_image_data(f_name);
	printf("\n%s\n", f_name);
	mesh(fv_mesh_a);

	for (i = 0; i < 10; i++) {
		sprintf_s(f_name, _countof(f_name), "std%d.pgm", i);
		load_image_data(f_name);
		mesh(fv_mesh_b);
		s = simple_similarity(fv_mesh_a, fv_mesh_b);
		printf("%d %f\n", i, s);
		if (i == 0 || smax < s) {
			imax = i;
			smax = s;
		}
	}
	printf("識別結果：%d %f\n", imax, smax);
}

double simple_similarity(int* fv_mesh_a, int* fv_mesh_b)
{
	double	s;
	int	k;

	s = 0.0;
	for (k = 0; k < DIM; k++) {
		s += 1.0 - abs(fv_mesh_a[k] - fv_mesh_b[k]) / 127.5;
	}
	return s;
}

void mesh(int* fv_mesh)
{
	int	ix, iy;
	int	k;

	for (k = 0; k < DIM; k++) {
		fv_mesh[k] = 0;
	}
	for (iy = 0; iy < Y_SIZE; iy++) {
		for (ix = 0; ix < X_SIZE; ix++) {
			k = (iy / Y_BLOCK) * (X_SIZE / X_BLOCK) + (ix / X_BLOCK);
			fv_mesh[k] += image[iy][ix];
		}
	}
	for (k = 0; k < DIM; k++) {
		fv_mesh[k] /= X_BLOCK * Y_BLOCK;
	}

	for (iy = 0; iy < Y_SIZE / Y_BLOCK; iy++) {
		for (ix = 0; ix < X_SIZE / X_BLOCK; ix++) {
			k = iy * (X_SIZE / X_BLOCK) + ix;
		}
	}
}

void load_image_data(char* f_name)
{
	FILE*	fp;
	char	buf[640];
	errno_t	err;
	int	x_size, y_size;
	int	max_gray;
	int	ix, iy;

	err = fopen_s(&fp, f_name, "rb");
	if (err) {
		fprintf(stderr, "fopen_s\n");
		return;
	}

	// P5
	fgets(buf, sizeof buf, fp);
	if (buf[0] != 'P' || buf[1] != '5') {
		fprintf(stderr, "not P5\n");
		return;
	}

	// x y
	x_size = y_size = 0;
	while (x_size == 0) {
		fgets(buf, sizeof buf, fp);
		if (buf[0] == '#') continue;
		sscanf_s(buf, "%d%d", &x_size, &y_size);
	}

	// max
	max_gray = 0;
	while (max_gray == 0) {
		fgets(buf, sizeof buf, fp);
		if (buf[0] == '#') continue;
		sscanf_s(buf, "%d", &max_gray);
	}

	for (iy = 0; iy < y_size; iy++) {
		for (ix = 0; ix < x_size; ix++) {
			image[iy][ix] = fgetc(fp);
		}
	}

	fclose(fp);
}

/*
num0.pgm
0 61.168627
1 34.188235
2 38.603922
3 43.003922
4 32.376471
5 44.737255
6 50.368627
7 34.839216
8 42.572549
9 51.419608
識別結果：0 61.168627

num1.pgm
0 34.376471
1 61.529412
2 38.996078
3 37.043137
4 40.580392
5 37.223529
6 35.513725
7 46.023529
8 33.898039
9 35.498039
識別結果：1 61.529412

num2.pgm
0 37.137255
1 37.200000
2 60.188235
3 41.247059
4 37.976471
5 36.376471
6 39.952941
7 37.976471
8 42.494118
9 38.792157
識別結果：2 60.188235

num3.pgm
0 41.294118
1 34.972549
2 40.643137
3 60.211765
4 42.039216
5 50.713725
6 46.321569
7 35.749020
8 48.141176
9 41.443137
識別結果：3 60.211765

num4.pgm
0 32.494118
1 41.701961
2 37.898039
3 42.784314
4 60.031373
5 38.650980
6 38.603922
7 36.345098
8 36.674510
9 32.486275
識別結果：4 60.031373

num5.pgm
0 41.984314
1 34.501961
2 34.901961
3 50.000000
4 37.223529
5 59.074510
6 45.098039
7 33.396078
8 44.015686
9 42.619608
識別結果：5 59.074510

num6.pgm
0 49.231373
1 33.482353
2 40.266667
3 47.207843
4 37.662745
5 46.431373
6 59.623529
7 34.556863
8 49.976471
9 43.152941
識別結果：6 59.623529

num7.pgm
0 31.411765
1 43.458824
2 36.768627
3 34.909804
4 33.772549
5 32.988235
6 31.952941
7 59.686275
8 31.341176
9 31.670588
識別結果：7 59.686275

num8.pgm
0 41.443137
1 31.780392
2 43.788235
3 49.694118
4 37.011765
5 44.305882
6 48.823529
7 32.886275
8 59.411765
9 42.062745
識別結果：8 59.411765

num9.pgm
0 51.521569
1 34.094118
2 39.905882
3 42.360784
4 31.231373
5 44.266667
6 42.917647
7 34.054902
8 43.043137
9 60.125490
識別結果：9 60.125490
*/
