Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////MAIN/////////////////////////////////////////////////////
- #define cimg_display 2 //May need to change it to 1 if on LINUX.
- #include "Imagen.h"
- #include <fstream>
- #include <cstdlib>
- #include <string>
- #include <math.h>
- #include <windows.h>
- #include <wingdi.h>
- #include "CImg.h"
- using namespace cimg_library;
- using namespace std;
- int main() {
- Imagen imagen;
- imagen.getData("prueba");
- int x=imagen.getSize("prueba");
- imagen.crearImg("prueba");
- cout << "El tamano es:" << x;
- return 0;
- }
- ///////////////////////////////////////////////////////Imagen.cpp/////////////////////////////////////////
- #include "Imagen.h"
- #include "CImg.h"
- Imagen::Imagen(){}
- Imagen::~Imagen(){}
- unsigned char* Imagen::getData(string entrada){
- buffer=new char[getSize(entrada)*3];
- ifstream arch_in("entrada", ios::binary);
- arch_in.seekg(0, ios::beg);
- arch_in.read(buffer, getSize(entrada)*3); //size es pixeles. Mult por 3 por RGB
- arch_in.close();
- data=reinterpret_cast<unsigned char*>(buffer);
- return data; //Devuelve puntero con los bytes en un buffer. Es un array de bytes
- }
- int Imagen::getSize(string entrada){
- streampos inicio,fin;
- ifstream arch_in ("entrada", ios::binary);
- inicio = arch_in.tellg(); // Posicion inicial del archivo
- arch_in.seekg (0, ios::end); // offset 0 hasta el final
- fin = arch_in.tellg(); // Posicion final del archivo
- size=(int)sqrt((fin-inicio)/3); //Divide entre 3, saca raiz y obtiene parte entera
- arch_in.close();
- return size;
- }
- void Imagen::crearImg(string entrada){
- CImg<unsigned char> img(getData(entrada),getSize(entrada),getSize(entrada),0,3,false); //Constructor. Takes the data as a pointer
- img.normalize(0, 255); //random try to fix it. Not relevant.
- img.save("Hello.png"); //Saves images but the files doesnt display anything
- }
- //////////////////////////////////////////////////////////////Imagen.h///////////////////////////////////////////////
- #ifndef IMAGEN_H
- #define IMAGEN_H
- #define cimg_display 2 // MAY NEED TO CHANGE IT 1 IF ON LINUX
- #include <string>
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- #include <math.h>
- #include <windows.h>
- #include <wingdi.h>
- #include "CImg.h"
- using namespace cimg_library;
- using namespace std;
- class Imagen{
- private:
- int size;
- string entrada;
- char* buffer;
- unsigned char* data;
- public:
- Imagen();
- ~Imagen();
- unsigned char* getData(string entrada);
- int getSize(string entrada);
- void crearImg(string entrada);
- };
- #endif /* IMAGEN_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement