codice:
void scambia(unsigned char *a, unsigned char *b)
{
char aux;
aux = *a;
*a = *b;
*b = aux;
return;
}
void dim_imm(FILE *f, int *r, int *c)
{
rewind(f);
fseek(f, 18, SEEK_CUR);
fread(c, sizeof(int), 1, f);
fread(r, sizeof(int), 1, f);
rewind(f);
return;
}
unsigned char **alloca(int r, int c)
{
int i;
unsigned char **m;
m = malloc(r*sizeof(char*));
for(i = 0; i < r; i++)
m[i] = malloc(c*sizeof(char));
return m;
}
void dealloca(unsigned char **m, int r)
{
int i;
for(i = 0; i < r; i++)
free(m[i]);
free(m);
return;
}
void carica_mat(unsigned char **m, int r, int c, FILE *f)
{
int i, j;
fseek(f, -(r*c*sizeof(char)), SEEK_END);
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
fread(&m[i][j], sizeof(char),1,f);
return;
}
void copia_header(FILE *s, FILE *d)
{
int i, offset;
char aux;
rewind(s);
fseek(s, 10, SEEK_CUR);
fread(&offset, sizeof(int), 1, s);
rewind(s);
for(i = 0; i < offset; i += 8)
{
fread(&aux, sizeof(char),1,s);
fwrite(&aux, sizeof(char), 1, d);
}
rewind(d);
fseek(d, offset, SEEK_CUR);
return;
}
void capovolgi_imm(FILE *s, char *n)
{
int i, j;
char **mat;
char str[80];
int a, l;
FILE *d;
str[0] = '\0';
dim_imm(s, &a, &l);
mat = alloca(a, l);
carica_mat(mat, a, l , s);
for(i = 0; i < (a/2); i++)
for(j = 0; j < l; j++)
scambia(&mat[i][j], &mat[a-i-1][j]);
strcat(str,"(Capovolta) ");
strcat(str,n);
d = fopen(str, "wb");
copia_header(s,d);
for(i = 0; i < a; i++)
{
for(j = 0; j < l; j++)
fwrite(&mat[i][j], sizeof(char), 1, d);
}
return;
}