Advertisement
Guest User

Image program

a guest
May 16th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. /////////////////////////////////////////MAIN/////////////////////////////////////////////////////
  2.  
  3. #define cimg_display 2 //May need to change it to 1 if on LINUX.
  4. #include "Imagen.h"
  5. #include <fstream>
  6. #include <cstdlib>
  7. #include <string>
  8. #include <math.h>
  9. #include <windows.h>
  10. #include <wingdi.h>
  11. #include "CImg.h"
  12. using namespace cimg_library;
  13. using namespace std;
  14.  
  15. int main() {
  16.  
  17.     Imagen imagen;
  18.     imagen.getData("prueba");
  19.     int x=imagen.getSize("prueba");
  20.     imagen.crearImg("prueba");
  21.    
  22.     cout << "El tamano es:" << x;
  23.     return 0;
  24. }
  25.  
  26. ///////////////////////////////////////////////////////Imagen.cpp/////////////////////////////////////////
  27.  
  28. #include "Imagen.h"
  29. #include "CImg.h"
  30.  
  31.  
  32. Imagen::Imagen(){}
  33.  
  34. Imagen::~Imagen(){}
  35.  
  36. unsigned char* Imagen::getData(string entrada){
  37.  
  38.  
  39.     buffer=new char[getSize(entrada)*3];
  40.     ifstream arch_in("entrada", ios::binary);
  41.     arch_in.seekg(0, ios::beg);
  42.     arch_in.read(buffer, getSize(entrada)*3); //size es pixeles. Mult por 3 por RGB
  43.     arch_in.close();
  44.     data=reinterpret_cast<unsigned char*>(buffer);
  45.     return data;             //Devuelve puntero con los bytes en un buffer. Es un array de bytes
  46. }
  47.  
  48. int Imagen::getSize(string entrada){
  49.  
  50.     streampos inicio,fin;
  51.     ifstream arch_in ("entrada", ios::binary);
  52.     inicio = arch_in.tellg();                   // Posicion inicial del archivo
  53.     arch_in.seekg (0, ios::end);   // offset 0 hasta el final
  54.     fin = arch_in.tellg();                    // Posicion final del archivo
  55.     size=(int)sqrt((fin-inicio)/3);    //Divide entre 3, saca raiz y obtiene parte entera
  56.     arch_in.close();
  57.     return size;
  58.  
  59. }
  60.  
  61. void Imagen::crearImg(string entrada){
  62.  
  63.     CImg<unsigned char> img(getData(entrada),getSize(entrada),getSize(entrada),0,3,false); //Constructor. Takes the data as a pointer
  64.      img.normalize(0, 255); //random try to fix it. Not relevant.
  65.      img.save("Hello.png"); //Saves images but the files doesnt display anything
  66. }
  67.  
  68. //////////////////////////////////////////////////////////////Imagen.h///////////////////////////////////////////////
  69.  
  70. #ifndef IMAGEN_H
  71. #define IMAGEN_H
  72. #define cimg_display 2 // MAY NEED TO CHANGE IT 1 IF ON LINUX
  73. #include <string>
  74. #include <iostream>
  75. #include <cstdlib>
  76. #include <fstream>
  77. #include <math.h>
  78. #include <windows.h>
  79. #include <wingdi.h>
  80. #include "CImg.h"
  81. using namespace cimg_library;
  82.  
  83. using namespace std;
  84.  
  85. class Imagen{
  86.  
  87. private:
  88.  
  89.     int size;
  90.     string entrada;
  91.     char* buffer;
  92.     unsigned char* data;
  93.  
  94. public:
  95.  
  96.     Imagen();
  97.     ~Imagen();
  98.     unsigned char* getData(string entrada);
  99.     int getSize(string entrada);
  100.     void crearImg(string entrada);
  101.  
  102. };
  103. #endif /* IMAGEN_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement