PLE:Unidad6Ejercicio18

/* Programa:        Unidad6Ejercicio18.cpp Descripción:     Muestra los datos de la cabecera de un fichero BMP. using namespace std;
 * 1) include
 * 2) include
 * 3) include
 * 1) include

struct cabeceraBMP { char Signature[2];             /* Signatura = "BM"             */ unsigned int FileSize;         /* Tamaño Fichero               */ unsigned int Reserved;         /* Reservado                    */ unsigned int DataOffset;       /* Desplazamiento de los datos  */ };

struct propiedadesBMP { unsigned int size;              /* Header size in bytes      */ int width, height;              /* Width and height of image */ unsigned short int planes;      /* Number of colour planes   */ unsigned short int bits;        /* Bits per pixel            */ unsigned int compression;       /* Compression type          */ unsigned int imagesize;         /* Image size in bytes       */ int xresolution, yresolution;   /* Pixels per meter          */ unsigned int ncolours;          /* Number of colours         */ unsigned int importantcolours;  /* Important colours         */ };

struct pixel { unsigned char byteR, byteG, byteB; };

void leeCabecera(ifstream &f, cabeceraBMP &c); void leePropiedades(ifstream &f, propiedadesBMP &p); void leeImagen(ifstream &f, pixel *img, unsigned int tam); void muestraDatosBMP(cabeceraBMP cBMP, propiedadesBMP pBMP);

void main { cabeceraBMP cBMP; propiedadesBMP pBMP; pixel *imagen; char nombre[80]; ifstream fe;

cout << "Introduzca el nombre del fichero origen "; cin >> nombre;

fe.open(nombre, ios::in | ios::binary);

if(fe.is_open) {               leeCabecera(fe,cBMP); leePropiedades(fe,pBMP); imagen = new pixel[pBMP.imagesize / 3]; leeImagen(fe,imagen,pBMP.imagesize / 3); muestraDatosBMP(cBMP, pBMP);

fe.close; }       else cout << "Error: El fichero no existe "; cin.get; }

void leeCabecera(ifstream &f, cabeceraBMP &c) {       f.read(reinterpret_cast(&c.Signature[0]), sizeof(c.Signature[0])); f.read(reinterpret_cast(&c.Signature[1]), sizeof(c.Signature[1])); f.read(reinterpret_cast(&c.FileSize), sizeof(c.FileSize)); f.read(reinterpret_cast(&c.Reserved), sizeof(c.Reserved)); f.read(reinterpret_cast(&c.DataOffset), sizeof(c.DataOffset)); }

void leePropiedades(ifstream &f, propiedadesBMP &p) {       f.read(reinterpret_cast(&p.size), sizeof(p.size)); f.read(reinterpret_cast(&p.width), sizeof(p.width)); f.read(reinterpret_cast(&p.height), sizeof(p.height)); f.read(reinterpret_cast(&p.planes), sizeof(p.planes)); f.read(reinterpret_cast(&p.bits), sizeof(p.bits)); f.read(reinterpret_cast(&p.compression), sizeof(p.compression)); f.read(reinterpret_cast(&p.imagesize), sizeof(p.imagesize)); f.read(reinterpret_cast(&p.xresolution), sizeof(p.xresolution)); f.read(reinterpret_cast(&p.yresolution), sizeof(p.yresolution)); f.read(reinterpret_cast(&p.ncolours), sizeof(p.ncolours)); f.read(reinterpret_cast(&p.importantcolours), sizeof(p.importantcolours)); if(p.imagesize == 0) // La imagen esta comprimida p.imagesize = p.width * p.height * p.bits / 8; }

void leeImagen(ifstream &f, pixel *img, unsigned int tam) {       for(unsigned int i = 0; i < tam; i++) f.read(reinterpret_cast<char*>(img + i), sizeof(*img)); }

void escribeCabecera(ofstream &f, cabeceraBMP &c) {       f.write(reinterpret_cast<char*>(&c.Signature[0]), sizeof(c.Signature[0])); f.write(reinterpret_cast<char*>(&c.Signature[1]), sizeof(c.Signature[1])); f.write(reinterpret_cast<char*>(&c.FileSize), sizeof(c.FileSize)); f.write(reinterpret_cast<char*>(&c.Reserved), sizeof(c.Reserved)); f.write(reinterpret_cast<char*>(&c.DataOffset), sizeof(c.DataOffset)); }

void escribePropiedades(ofstream &f, propiedadesBMP &p) {       f.write(reinterpret_cast<char*>(&p.size), sizeof(p.size)); f.write(reinterpret_cast<char*>(&p.width), sizeof(p.width)); f.write(reinterpret_cast<char*>(&p.height), sizeof(p.height)); f.write(reinterpret_cast<char*>(&p.planes), sizeof(p.planes)); f.write(reinterpret_cast<char*>(&p.bits), sizeof(p.bits)); f.write(reinterpret_cast<char*>(&p.compression), sizeof(p.compression)); f.write(reinterpret_cast<char*>(&p.imagesize), sizeof(p.imagesize)); f.write(reinterpret_cast<char*>(&p.xresolution), sizeof(p.xresolution)); f.write(reinterpret_cast<char*>(&p.yresolution), sizeof(p.yresolution)); f.write(reinterpret_cast<char*>(&p.ncolours), sizeof(p.ncolours)); f.write(reinterpret_cast<char*>(&p.importantcolours), sizeof(p.importantcolours)); }

void muestraDatosBMP(cabeceraBMP cBMP, propiedadesBMP pBMP) {       cout << "Cabecera BMP " << endl << endl; cout << "Sgnatura = " << cBMP.Signature[0] << cBMP.Signature[1] << endl; cout << "Tam.Fichero = " << cBMP.FileSize << endl; cout << "Reservado = " << cBMP.Reserved << endl; cout << "Deplz. datos = " << cBMP.DataOffset << endl << endl;

cout << "Propiedades BMP " << endl << endl; cout << "Tam. = " << pBMP.size << endl; cout << "Ancho = " << pBMP.width << endl; cout << "Alto = " << pBMP.height << endl; cout << "Planos = " << pBMP.planes << endl; cout << "Bits = " << pBMP.bits << endl; cout << "Compresion = " << pBMP.compression << endl; cout << "Tam. Imagen = " << pBMP.imagesize << endl; cout << "Resolucion X = " << pBMP.xresolution << endl; cout << "Resolucion Y = " << pBMP.yresolution << endl; cout << "Num. Colores = " << pBMP.ncolours << endl; cout << "Num. Col. Import. = " << pBMP.importantcolours << endl; }