Fandom

Scratchpad

PLE:Unidad6Ejercicio18

215,902pages on
this wiki
Add New Page
Discuss this page0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

/*
Programa:         Unidad6Ejercicio18.cpp
Descripción:      Muestra los datos de la cabecera de un fichero BMP.
*/
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

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<char*>(&c.Signature[0]), sizeof(c.Signature[0]));
        f.read(reinterpret_cast<char*>(&c.Signature[1]), sizeof(c.Signature[1]));
        f.read(reinterpret_cast<char*>(&c.FileSize), sizeof(c.FileSize));
        f.read(reinterpret_cast<char*>(&c.Reserved), sizeof(c.Reserved));
        f.read(reinterpret_cast<char*>(&c.DataOffset), sizeof(c.DataOffset));
}

void leePropiedades(ifstream &f, propiedadesBMP &p)
{
        f.read(reinterpret_cast<char*>(&p.size), sizeof(p.size));
        f.read(reinterpret_cast<char*>(&p.width), sizeof(p.width));
        f.read(reinterpret_cast<char*>(&p.height), sizeof(p.height));
        f.read(reinterpret_cast<char*>(&p.planes), sizeof(p.planes));
        f.read(reinterpret_cast<char*>(&p.bits), sizeof(p.bits));
        f.read(reinterpret_cast<char*>(&p.compression), sizeof(p.compression));
        f.read(reinterpret_cast<char*>(&p.imagesize), sizeof(p.imagesize));
        f.read(reinterpret_cast<char*>(&p.xresolution), sizeof(p.xresolution));
        f.read(reinterpret_cast<char*>(&p.yresolution), sizeof(p.yresolution));
        f.read(reinterpret_cast<char*>(&p.ncolours), sizeof(p.ncolours));
        f.read(reinterpret_cast<char*>(&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;
}

Also on Fandom

Random wikia