/* A pure code for converting xpm to matrix data (N*M)
*
* usage:
* cp xxx.xpm xpm.xpm && gcc *.c -O3
* ./a.out maxval
*
* date: 2022.08.18
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xpm.xpm"
};
typedef struct t_code
{
char code[6];
float val;
} t_code;
int main(int argc, char *argv[])
{
static float maxVal = 4.46, minVal = 0;
int i, j, k;
char *token = NULL;
char *str = NULL;
int nx, ny, nc, ncode = 1;
struct t_code *code = NULL;
FILE *fp = NULL;
if (argc == 2)
{
maxVal = (float)atof(argv[1]);
}
else if (argc == 3)
{
minVal = (float)atof(argv[1]);
maxVal = (float)atof(argv[2]);
}
else
{
fprintf(stderr, "Error! Missing input parameters\n");
getchar();
exit(1);
}
fprintf(stderr, "MinVal = %f, MaxVal = %f\n", minVal, maxVal);
str = strdup(gromacs_xpm[0]);
sscanf(str, "%d %d %d %d", &nx, &ny, &nc, &ncode);
free(str); str = NULL;
fprintf(stderr, "DIM: %d x %d; ncolor= %d; ncode= %d\n", ny, nx, nc, ncode);
code = (t_code *)malloc(sizeof(t_code)*nc);
for (i = 0; i < nc; i++)
{
strncpy(code[i].code, gromacs_xpm[i+1], ncode);
code[i].code[ncode] = '\0';
code[i].val = (maxVal-minVal)/(nc-1) * i;
}
/* read matrix and write new matrix data */
fp = fopen("Matrix.dat", "w");
for (i = ny-1; i >= 0; i--)
{
for (j = 0; j < nx; j++)
{
for (k = 0; k < nc; k++)
{
char *p = &gromacs_xpm[i+nc+1][ncode*j];
if (!strncmp(code[k].code, p, ncode))
{
fprintf(fp, "%6.2f ", code[k].val);
break;
}
}
}
fprintf(fp, "\n");
}
free(code); code = NULL;
fclose(fp);
fprintf(stderr, "\nDone! Matrix data written to Matrix.dat\n");
return 0;
}