#include <pgm.h>
#include <math.h>
enum { N = 4, M = 4 };
typedef struct pgm_t {
int cols, rows;
gray maxval;
gray **pgm;
} pgm;
struct pgm_t load_pgm( const char *filename ) {
FILE
*fp
= fopen( filename
, "w" ); struct pgm_t pgm;
pgm.pgm = pgm_readpgm( fp, &pgm.cols, &pgm.rows, &pgm.maxval );
return pgm;
}
void store_pgm( const char *filename, const struct pgm_t *pgm ) {
FILE
*fp
= fopen( filename
, "w" ); pgm_writepgm( fp, pgm->pgm, pgm->cols, pgm->rows, pgm->maxval, 0 );
}
struct pgm_t mesh( const struct pgm_t *src, int w, int h ) {
struct pgm_t result;
int x, y;
double d = (double)(src->cols * src->rows)/w/h;
result.maxval = src->maxval;
result.cols = w;
result.rows = h;
result.pgm = pgm_allocarray( w, h );
for( x = 0; x < src->cols; ++x ) {
for( y = 0; y < src->rows; ++y ) {
result.pgm[x/w][y/h] += src->pgm[x][y];
}
}
for( x = 0; x < w; ++x ) {
for( y = 0; y < h; ++y ) {
result.pgm[x][y] /= d;
}
}
return result;
}
double calc_similarness( const struct pgm_t *a, const struct pgm_t *b ) {
double result = 0;
int x, y;
#if 0
assert( a
->maxval
== b
->maxval
); #endif
for( x = 0; x < a->cols; ++x ) {
for( y = 0; y < a->rows; ++y ) {
result
+= fabs( a
->pgm
[x
][y
] - b
->pgm
[x
][y
] ); }
}
return result / a->cols / a->rows;
}
int main( int argc, const char *argv[]) {
if( strcmp( argv
[0], "(1)" ) == 0 ) { struct pgm_t g = load_pgm( argv[1] );
struct pgm_t g2 = mesh( &g, N, M );
store_pgm( "result-1.pgm", &g2 );
}
else if( strcmp( argv
[0], "(2)" ) == 0 ) { struct pgm_t in = load_pgm( argv[1] );
struct pgm_t meshed = mesh( &in, N, M );
double max_similarness = 0.0;
int most_similar_number = -1;
int i;
for( i = 0; i < 10; ++i ) {
char buf[32];
struct pgm_t std_N = load_pgm( buf );
struct pgm_t meshed_N = mesh( &std_N, N, M );
double similarness = calc_similarness( &in, &std_N );
if( similarness > max_similarness ) {
max_similarness = similarness;
most_similar_number = i;
}
printf( "SIMILARITY TO #%i IS %f\n", i
, similarness
); }
printf( "MOST SIMILAR NUMBER IS: %d", most_similar_number
); }
else if( strcmp( argv
[0], "(3)" ) == 0 ) { }
}
I2luY2x1ZGUgPHBnbS5oPgojaW5jbHVkZSA8bWF0aC5oPgoKZW51bSB7IE4gPSA0LCBNID0gNCB9OwoKdHlwZWRlZiBzdHJ1Y3QgcGdtX3QgewogIGludCBjb2xzLCByb3dzOwogIGdyYXkgbWF4dmFsOwogIGdyYXkgKipwZ207Cn0gcGdtOwoKc3RydWN0IHBnbV90IGxvYWRfcGdtKCBjb25zdCBjaGFyICpmaWxlbmFtZSApIHsKICBGSUxFICpmcCA9IGZvcGVuKCBmaWxlbmFtZSwgInciICk7CiAgc3RydWN0IHBnbV90IHBnbTsKICBwZ20ucGdtID0gcGdtX3JlYWRwZ20oIGZwLCAmcGdtLmNvbHMsICZwZ20ucm93cywgJnBnbS5tYXh2YWwgKTsKICBmY2xvc2UoIGZwICk7CiAgcmV0dXJuIHBnbTsKfQoKdm9pZCBzdG9yZV9wZ20oIGNvbnN0IGNoYXIgKmZpbGVuYW1lLCBjb25zdCBzdHJ1Y3QgcGdtX3QgKnBnbSApIHsKICBGSUxFICpmcCA9IGZvcGVuKCBmaWxlbmFtZSwgInciICk7CiAgcGdtX3dyaXRlcGdtKCBmcCwgcGdtLT5wZ20sIHBnbS0+Y29scywgcGdtLT5yb3dzLCBwZ20tPm1heHZhbCwgMCApOwogIGZjbG9zZSggZnAgKTsKfQoKc3RydWN0IHBnbV90IG1lc2goIGNvbnN0IHN0cnVjdCBwZ21fdCAqc3JjLCBpbnQgdywgaW50IGggKSB7CiAgc3RydWN0IHBnbV90IHJlc3VsdDsKICBpbnQgeCwgeTsKICBkb3VibGUgZCA9IChkb3VibGUpKHNyYy0+Y29scyAqIHNyYy0+cm93cykvdy9oOwoKICByZXN1bHQubWF4dmFsID0gc3JjLT5tYXh2YWw7Ci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