Blame view

cpp/colormap.cpp 815 Bytes
7006df5f   David Mayerich   reformat of direc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <qimage.h>
  #include <qcolor.h>
  #include <iostream>
  
  void qt_buffer2image(unsigned char* buffer, std::string filename, unsigned int x_size, unsigned int y_size)
  {
      //x_size = 256;
      //y_size = 256;
  	//create an image object
  	QImage image(x_size, y_size, QImage::Format_RGB32);
  	if(image.isNull())
  	{
          std::cout<<"Error creating QImage."<<std::endl;
          return;
      }
  
  	int i;
  	unsigned char r, g, b;
  	unsigned int x, y;
  	for(y=0; y<y_size; y++)
  		for(x=0; x<x_size; x++)
  		{
  			//calculate the 1D index
  			i = y * x_size + x;
  
  			r = buffer[i * 3 + 0];
  			g = buffer[i * 3 + 1];
  			b = buffer[i * 3 + 2];
  
  			//set the image pixel
  			QColor color(r, g, b);
  			image.setPixel(x, y, color.rgb());
  		}
  
  	if(!image.save(filename.c_str()))
          std::cout<<"Error saving QImage."<<std::endl;
  }