Fandom

Scratchpad

PLE:Unidad6Ejercicio19

215,647pages 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:         Unidad6Ejercicio19.cpp
Descripción:      Modifica 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 transformaBN(pixel *img, pixel *img2, unsigned int w, unsigned int h);
void satura(pixel *img, pixel *img2, unsigned int w, unsigned int h);
void volteaV(pixel *img, pixel *img2, unsigned int w, unsigned int h);
void volteaH(pixel *img, pixel *img2, unsigned int w, unsigned int h);
void escribeCabecera(ofstream &f, cabeceraBMP &c);
void escribePropiedades(ofstream &f, propiedadesBMP &p);
void escribeImagen(ofstream &f, pixel *img, unsigned int tam);
char menu();

const char BN = 'b';
const char SATURA = 's';

void main() {
        cabeceraBMP cBMP;
        propiedadesBMP pBMP;
        pixel *imagen, *imagen2;
        unsigned int t;
        char nombreOri[80], nombreDes[80], op;

        cout << "Introduzca el nombre del fichero origen ";
        cin >> nombreOri;
        cout << endl << "Introduzca el nombre del fichero destino ";
        cin >> nombreDes;

        ifstream fe(nombreOri, ios::in | ios::binary);
        ofstream fs(nombreDes, ios::out | ios::binary);

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

                op = menu();
                switch(op)
                {
                        case 'b':       transformaBN(imagen,imagen2,pBMP.width, pBMP.height);
                                        break;
                        case 's':       satura(imagen,imagen2,pBMP.width, pBMP.height);
                                        break;
                        case 'v':       volteaV(imagen,imagen2,pBMP.width, pBMP.height);
                                        break;
                        case 'h':       volteaH(imagen,imagen2,pBMP.width, pBMP.height);
                                        break;

                }
                escribeCabecera(fs,cBMP);
                escribePropiedades(fs,pBMP);
                escribeImagen(fs,imagen2,pBMP.imagesize / 3);

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

char menu()
{
        char opc;
        cout << "MENU DE OPCIONES" << endl;
        cout << "b - Covierte la imagen en banco y negro" << endl;
        cout << "s - Satura la imagen" << endl;
        cout << "v - Voltea Verticalmente la imagen" << endl;
        cout << "h - Voltea Horizontalmente la imagen" << endl;
        cout << "Elija una opcion: ";
        cin >> opc;
        return opc;
}

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 escribeImagen(ofstream &f, pixel *img, unsigned int tam)
{
        for(unsigned int i = 0; i < tam; i++)
                f.write(reinterpret_cast<char*>(img + i), sizeof(*img));
}

void transformaBN(pixel *img, pixel *img2, unsigned int w, unsigned int h)
{
        unsigned int i, j;
        float colorR, colorG, colorB, colorBN;
        for(i = 0; i < h; i++)
        {
                for(j = 0; j < w; j++)
                {
                        colorR = img[i * w + j].byteR;
                        colorG = img[i * w + j].byteG;
                        colorB = img[i * w + j].byteB;
                        colorBN = 0.299 * colorR + 0.587 * colorG + 0.114 * colorB;
                        img2[i * w + j].byteR = colorBN;
                        img2[i * w + j].byteG = colorBN;
                        img2[i * w + j].byteB = colorBN;
                }
        }
}

void satura(pixel *img, pixel *img2, unsigned int w, unsigned int h)
{
        unsigned int i, j;
        unsigned char umbral = 128;
        float colorR, colorG, colorB;
        for(i = 0; i < h; i++)
        {
                for(j = 0; j < w; j++)
                {
                        // Saturamos el rojo
                        if(img[i * w + j].byteR > umbral)
                                img2[i * w + j].byteR = 255;
                        else
                                img2[i * w + j].byteR = 0;

                        // Saturamos el Verde
                        if(img[i * w + j].byteG > umbral)
                                img2[i * w + j].byteG = 255;
                        else
                                img2[i * w + j].byteG = 0;

                        // Saturamos el Azul
                        if(img[i * w + j].byteB > umbral)
                                img2[i * w + j].byteB = 255;
                        else
                                img2[i * w + j].byteB = 0;
                }
        }
}

void volteaV(pixel *img, pixel *img2, unsigned int w, unsigned int h)
{
        unsigned int i, j, j2;
        for(i = 0; i < h; i++)
        {
                for(j = 0, j2= w - 1; j < w; j++, j2--)
                {
                        img2[i * w + j] = img[i * w + j2];
                }
        }
}

void volteaH(pixel *img, pixel *img2, unsigned int w, unsigned int h)
{
        unsigned int i, i2, j;
        for(i = 0, i2 = h - 1; i < h; i++, i2--)
        {
                for(j = 0; j < w; j++)
                {
                        img2[i * w + j] = img[i2 * w + j];
                }
        }
}

Also on Fandom

Random wikia