ejercicio con un BMP en c

oskrok
05 de Junio del 2005
hola este es mi primer mensaje y soy bastante noob, tengo que hacer un programa en c que convierta un bmp a escala de grises i por ahora solo consigo leer la cabecera del bmp, lo que no tengo ni idea es como sacar los valores rgb de cada pixel. Os pongo lo que levo echo:

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

typedef struct descripcion{
unsigned int tam;
int alto,ancho;
unsigned short int planos;
unsigned short int bits;
unsigned int compresion;
unsigned int timaxe;
int resolucionX,resolucionY;
unsigned int cores;
unsigned int coresI;
}descripcion;

typedef struct cabeceira{
char signature[2];
unsigned int filesize;
unsigned int reserved;
unsigned int dataoffset;
}cabeceira;

typedef struct bmp{
descripcion foto;
cabeceira arch;
}bmp;

void getCabeceira (bmp *p,char * nome, FILE * f);
void getPropiedades ( bmp * p, char * nome, FILE * f);


int main(){
FILE *f;
bmp *p;
char *nome,buf[121];
p=(bmp *)malloc(sizeof(bmp));
printf("Introducca o nome do archivo: ");
gets(buf);
nome=(char *)malloc((strlen(buf)+1)*sizeof(char));
strcpy(nome,buf);
getCabeceira(p,nome,f);
getPropiedades(p,nome,f);
system("pause");
return 0;
}



void getCabeceira(bmp *p,char * nome, FILE * f){
f=fopen(nome,"r+b");
fseek(f,0,SEEK_SET);
fread(&p->arch.signature,sizeof(char),1,f);
fseek(f,2,SEEK_SET);
fread(&p->arch.filesize,sizeof(unsigned int),1,f);
fseek(f,6,SEEK_SET);
fread(&p->arch.reserved,sizeof(unsigned int),1,f);
fseek(f,10,SEEK_SET);
fread(&p->arch.dataoffset,sizeof(unsigned int),1,f);
fclose(f);
return;
}

void getPropiedades(bmp *p, char *nome, FILE *f){
f=fopen(nome,"r+b");
fseek(f,14,SEEK_SET);
fread(&p->foto.tam,sizeof(unsigned int),1,f);
fseek(f,18,SEEK_SET);
fread(&p->foto.ancho,sizeof(int),1,f);
fseek(f,22,SEEK_SET);
fread(&p->foto.alto,sizeof(int),1,f);
fseek(f,24,SEEK_SET);
fread(&p->foto.planos,sizeof(unsigned short int),1,f);
fseek(f,26,SEEK_SET);
fread(&p->foto.bits,sizeof(unsigned short int),1,f);
fseek(f,30,SEEK_SET);
fread(&p->foto.compresion,sizeof(unsigned int),1,f);
fseek(f,34,SEEK_SET);
fread(&p->foto.timaxe,sizeof(unsigned int),1,f);
fseek(f,38,SEEK_SET);
fread(&p->foto.resolucionX,sizeof(int),1,f);
fseek(f,42,SEEK_SET);
fread(&p->foto.resolucionY,sizeof(int),1,f);
fseek(f,46,SEEK_SET);
fread(&p->foto.cores,sizeof(unsigned int),1,f);
fseek(f,50,SEEK_SET);
fread(&p->foto.coresI,sizeof(unsigned int),1,f);
fclose(f);
}