colormap.cpp 815 Bytes
#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;
}