#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 load_image_data(char* f_name);
void mesh(void);
u_char image[Y_SIZE][X_SIZE];
int main()
{
char f_name[256];
int i;
for (i = 0; i < 10; i++) {
sprintf_s(f_name, _countof(f_name), "std%d.pgm", i);
load_image_data(f_name);
mesh();
}
return 0;
}
void mesh(void)
{
int fv_mesh[DIM] = {0};
int ix, iy;
int k;
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) {
return;
}
// P5
fgets(buf
, sizeof buf
, fp
); if (buf[0] != 'P' || buf[1] != '5') {
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
); }
}
}
/*
std0.pgm
ff ff d4 92 79 ac fd ff
ff e2 84 fe fd ad b8 ff
ff 7a c5 ff ff f5 4a ff
ff 61 c5 ff ff f5 31 ff
ff 61 c5 ff ff f5 31 ff
ff 65 c5 ff ff f5 35 ff
ff d0 90 ff ff c3 9d ff
ff ff ba 85 b1 a9 fa ff
std1.pgm
ff ff f0 8b b0 ff ff ff
ff ff b9 4d a5 ff ff ff
ff ff ff 7d a5 ff ff ff
ff ff ff 7d a5 ff ff ff
ff ff ff 7d a5 ff ff ff
ff ff ff 7d a5 ff ff ff
ff ff ff 7d a5 ff ff ff
ff ff c2 56 72 cf ff ff
std2.pgm
ff ff d4 92 79 82 f7 ff
ff e2 b3 fe fd 72 b5 ff
ff d1 2c 9d ff 9a 91 ff
ff fa 98 c1 f8 41 e7 ff
ff ff ff f8 76 b7 ff ff
ff ff fa 9c b7 fe fb ff
ff f8 99 ef ff e9 a4 ff
ff 81 1a 49 49 2c 91 ff
std3.pgm
ff fb 82 88 94 b6 fd ff
ff d2 5a fe fd 34 ef ff
ff e1 96 ff fe 54 f4 ff
ff ff d9 97 91 d3 ff ff
ff ff ff ff e9 5d dd ff
ff ac 76 ff ff 9b 91 ff
ff 70 97 ff ff 8d 9d ff
ff de 8f b1 97 77 fa ff
std4.pgm
ff ff ff ff dc 88 ff ff
ff ff ff fa 5e 63 ff ff
ff ff fe a8 ac 63 ff ff
ff ff d0 c0 c1 63 ff ff
ff f3 a6 f6 c1 63 ff ff
ff ab 81 a5 7c 40 b0 ff
ff f5 ef ef b4 5d f1 ff
ff ff ff d7 86 45 bb ff
std5.pgm
ff d6 47 8d 8d 9a f8 ff
ff d1 79 9d 9d eb ff ff
ff d1 c4 fd fd ff ff ff
ff c5 75 99 98 aa fd ff
ff 87 d3 ff ff 76 b2 ff
ff c1 bf ff ff 9b 91 ff
ff 61 69 ff ff 8d 9d ff
ff be 8f b1 aa 84 fa ff
std6.pgm
ff ff f7 78 91 93 f1 ff
ff f6 93 f0 ff 55 94 ff
ff a8 81 ff ff ce e6 ff
ff 61 ba 9e 97 aa fd ff
ff 61 7a f6 ff 8c 9a ff
ff 65 c5 ff ff f5 31 ff
ff d0 be ff ff e9 6e ff
ff ff bb ae b1 82 d8 ff
std7.pgm
ff d6 2d 31 31 2e c8 ff
ff 94 c9 f7 f7 91 f8 ff
ff b7 ff ff ce c4 ff ff
ff ff ff fc 79 ea ff ff
ff ff ff e5 56 ff ff ff
ff ff fe 8a a5 ff ff ff
ff ff fd 1e a5 ff ff ff
ff ff fd 1f c0 ff ff ff
std8.pgm
ff ff d4 92 91 99 f1 ff
ff e2 72 fe ff 97 94 ff
ff d1 3b f4 ff 9e b3 ff
ff fa 72 22 7d a8 fd ff
ff f3 b2 a3 61 30 dd ff
ff 73 b3 ff ff d3 41 ff
ff 70 b6 ff ff e9 3e ff
ff f1 64 a7 b1 82 d8 ff
std9.pgm
ff fb a9 92 91 bb fd ff
ff be 9b fe ff d5 bf ff
ff 61 c5 ff ff f5 4a ff
ff 9e 8c fc ff c4 2b ff
ff ff 94 8e 97 bb 31 ff
ff fd f5 fe ff f1 5c ff
ff d3 1d eb ff 97 c7 ff
ff f1 88 b1 aa 84 fa ff
*/
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgWF9TSVpFCTMyCiNkZWZpbmUgWV9TSVpFCTMyCiNkZWZpbmUgWF9CTE9DSwk0CiNkZWZpbmUgWV9CTE9DSwk0CiNkZWZpbmUgRElNCSgoWV9TSVpFIC8gWV9CTE9DSykgKiAoWF9TSVpFIC8gWF9CTE9DSykpCgp0eXBlZGVmIHVuc2lnbmVkIGNoYXIgdV9jaGFyOwoKdm9pZCBsb2FkX2ltYWdlX2RhdGEoY2hhciogZl9uYW1lKTsKdm9pZCBtZXNoKHZvaWQpOwoKdV9jaGFyCWltYWdlW1lfU0laRV1bWF9TSVpFXTsKCmludCBtYWluKCkKewoJY2hhcglmX25hbWVbMjU2XTsKCWludAlpOwoKCWZvciAoaSA9IDA7IGkgPCAxMDsgaSsrKSB7CgkJc3ByaW50Zl9zKGZfbmFtZSwgX2NvdW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