Sim-LIT  2.0
 All Classes Namespaces Files Functions Variables Typedefs
Images.h
Go to the documentation of this file.
1 
9 #ifndef CLASS_IMAGES
10 #define CLASS_IMAGES
11 
12 #include <string>
13 #include "DataBlock.h"
14 #include "DataType.h"
15 #include "CImg.h"
16 #include <cstddef>
17 #include <vector>
18 
19 using namespace std;
20 using namespace cimg_library;
21 
22 class Images
23 {
24 private:
25  DataBlock** matrix;
26  int w;
27  int h;
28  int type;
29  int wblock;
30  int hblock;
31 
32 public:
33  ~Images(){}
34 
35  Images(vector<DataType *> list, int nType, int nwidth, int nheight, int nwblock, int nhblock, bool show_data){
36  /*
37  * @brief Método Constructor de la Clase Images
38  * @param list Lista de Elementos con los cuales se creará la nueva Imagen
39  * @param nType Cantidad de canales que tendrá la Imagen
40  * @param nwidth Cantidad de Columnas que tendrá la nueva imagen
41  * @param nheight Cantidad de Filas que tendrá la nueva imagen
42  */
43  type=nType;
44  w=nwidth;
45  h=nheight;
46  wblock=nwblock;
47  hblock=nhblock;
48  if((w%wblock!=0 || h%hblock!=0) || wblock!=hblock){
49  wblock=1;
50  hblock=1;
51  }
52  matrix = new DataBlock*[h/hblock];
53  for (int i = 0; i < h/hblock; i=i+1)
54  {
55  matrix[i] = new DataBlock[w/wblock];
56  for (int j = 0; j < w/wblock; j=j+1)
57  {
58  int pos = (i*(int)(h/hblock))+j;
59  if(list.at(pos)!=NULL){
60  vector<Pixel> *e = (vector<Pixel>*)list.at(pos)->getExtras();
61  vector<Pixel> lblock(wblock*hblock);
62  for(int k=0;k<(wblock*hblock);k++){
63  int* content = e->at(k).getContent();
64  if(content[0]==0){
65  vector<int>channels;
66  for(int x=0;x<content[1];x++){
67  channels.push_back(content[2+x]);
68  }
69  Pixel transformed(channels, type);
70  lblock[k]=transformed;
71  }
72  else{
73  Pixel transformed;
74  lblock[k]=transformed;
75  }
76  }
77  DataBlock block(lblock, wblock,hblock,type);
78  matrix[i][j] = block;
79  }
80  }
81  }
82  if(show_data)
83  cout << "Image Generated...\n";
84  }
85 
86  Images(string url, int nwblock, int nhblock, bool show_data){
87  /*
88  * @brief Método Constructor de la clase Images
89  * @param url Fuente de origen desde donde se importará la Imagen
90  */
91  wblock=nwblock;
92  hblock=nhblock;
93  CImg<> img(url.c_str());
94  const CImg<unsigned char> R = img.get_shared_channel(0),
95  G = img.get_shared_channel(1),
96  B = img.get_shared_channel(2);
97  if (R==G && R==B) {
98  type = 1;
99  } else {
100  type=img.spectrum();
101  }
102  cout<<"TIPO: "<<type<<"\n";
103  w = img.width();
104  h = img.height();
105  if((w%wblock!=0 || h%hblock!=0) || wblock!=hblock){
106  wblock=1;
107  hblock=1;
108  }
109  //cout << "W:"<<w<<"---"<<"H:"<<h<<"\n";
110  matrix = new DataBlock*[(int)h/hblock];
111  for (int i = 0; i < h/hblock; i=i+1)
112  {
113  //cout << "img ciclo for 1: " << i << "\n";
114  matrix[i] = new DataBlock[(int)w/wblock];
115  for (int j = 0; j < w/wblock; j=j+1)
116  {
117  vector<Pixel> list(hblock*wblock);
118  for(int f=0;f<hblock;f++){
119  for(int c=0;c<wblock;c++){
120  vector<int> channels(type);
121  for(int x=0;x<type;x++){
122  channels[x]=(int)img(((j*wblock)+c),((i*hblock)+f),0,x);
123  }
124  Pixel aux(channels,type);
125  list[(f*wblock)+c]=aux;
126  }
127  }
128  DataBlock* aux = new DataBlock(list,wblock,hblock,type);
129  matrix[i][j] = *aux;
130  }
131  }
132  if(show_data)
133  cout << "Image Loaded...\n";
134  }
135 
136  int getWidthBlock(){
137  /*
138  @brief Método que devuelve la cantidad de Columnas de cada bloque
139  */
140  return wblock;
141  }
142 
143  int getHeightBlock(){
144  /*
145  @brief Método que devuelve la cantidad de Filas de cada Bloque
146  */
147  return hblock;
148  }
149 
150  int getWidth(){
151  /*
152  @brief Método que devuelve la cantidad de Columnas de la Imagen
153  */
154  return w;
155  }
156 
157  int getHeight(){
158  /*
159  @brief Método que devuelve la cantidad de Filas de la Imagen
160  */
161  return h;
162  }
163 
164  DataBlock** getMatrix(){
165  /*
166  @brief Método que devuelve la Matriz de Píxeles de la Imagen
167  */
168  return matrix;
169  }
170 
171  int getType(){
172  /*
173  @brief Método que devuelve la cantidad de canales que posee la Imagen
174  */
175  return type;
176  }
177 
178  vector<DataType *> toList(){
179  /*
180  @brief Método que devuelve en un Vector la matriz de DataBlockes de la Imagen
181  */
182  vector<DataType *> list;
183  for (int i = 0; i < h/hblock; i++)
184  {
185  for (int j = 0; j < w/wblock; j++)
186  {
187  DataBlock* aux = &matrix[i][j];
188  list.push_back(aux);
189  }
190  }
191  return list;
192  }
193 
194  void save(const char* destiny){
195  /*
196  @brief Método que Genera una Imagen en formato BMP de la Imagen
197  @param destiny Destino donde se guardará la Imagen
198  */
199  CImg<> img(w,h,1,type,0);
200  for (int i = 0; i < h/hblock; i++){
201  for (int j = 0; j < w/wblock; j++){
202  if(matrix[i][j].isValid()){
203  vector<Pixel> *e = (vector<Pixel>*)matrix[i][j].getExtras();
204  for(int r=0;r<hblock;r++){
205  for(int c=0;c<wblock;c++){
206  vector<int> v_rgb = (e->at((r*hblock)+c)).getChannels();
207  int* rgb = (int*)malloc(sizeof(int)*v_rgb.size());
208  for(int x=0;x<v_rgb.size();x++){
209  rgb[x]=v_rgb[x];
210  }
211  img.draw_point((j*wblock)+c,(i*hblock)+r,0,rgb);
212  }
213  }
214  }
215  }
216  }
217  img.normalize(0, 255);
218  img.save(destiny);
219  }
220 
221 private:
222  vector<Pixel> getPixelPerBlock(CImg<> img, int x, int y){
223  /*
224  @brief Método que importa los canales de un determinado DataBlock a la Imagen
225  @param img Imagen de la cual se extraerán los canales del DataBlock
226  @param x Nº de Columna del DataBlock
227  @param y Nº de Fila del DataBlock
228  */
229  vector<Pixel> list(hblock*wblock);
230  for(int i=0;i<hblock;i++){
231  for(int j=0;j<wblock;j++){
232  vector<int> channels(type);
233  for(int c=0;c<type;c++){
234  channels[c]=(int)img(((y*wblock)+j),((x*hblock)+i),0,c);
235  }
236  Pixel aux(channels,type);
237  list[(i*wblock)+j]=aux;
238  }
239  }
240  return list;
241  }
242 };
243 
244 #endif
Clase que representa un Bloque de Píxeles.
Definition: DataBlock.h:19
Clase que representa una Imagen.
Definition: Images.h:22
void * getExtras()
Definition: DataBlock.h:146
Clase que representa un Pixel de una imagen.
Definition: Pixel.h:17