void VizCamera::paintEvent()

in roboschool/cpp-household/render-glwidget.cpp [477:553]


void VizCamera::paintEvent(QPaintEvent* ev)
{
	QPainter p(this);
	p.fillRect(ev->rect(), QColor(QRgb(0xFFFFFF)));
	boost::shared_ptr<Household::Camera> camera = cref.lock();
	if (!camera) return;
	int w = camera->camera_res_w;
	int h = camera->camera_res_h;
	int aux_w = camera->camera_aux_w;
	int aux_h = camera->camera_aux_h;
	int SCALE = 2;

	// rgb
	QImage img_rgb(w, h, QImage::Format_RGB32);
	img_rgb.fill(QColor(QRgb(0xFFFFFF)));
	for (int y=0; y<h; y++) {
		uchar* u = img_rgb.scanLine(y);
		uint8_t* src = (uint8_t*) &camera->camera_rgb[3*w*y];
		for (int x=0; x<w; x++) {
			u[4*x + 2] = src[3*x + 0];
			u[4*x + 1] = src[3*x + 1];
			u[4*x + 0] = src[3*x + 2];
		}
	}
	p.drawImage( QRect(MARGIN, MARGIN, w*SCALE, h*SCALE), img_rgb);

	// depth
	QImage img_aux(aux_w, aux_h, QImage::Format_RGB32);
	img_aux.fill(0xE0E0E0);
	static bool inited;
	static uint32_t palette[1024];
	static uint32_t mpalette[256];
	const int palette_size = sizeof(palette)/sizeof(palette[0]);
	if (!inited) {
		float palette_size1 = 1.0 / palette_size;
		for (int c=0; c<palette_size; c++) {
			QColor t;
			t.setHsvF(c*palette_size1, 1, 1);
			//t.setHsvF(c*palette_size1, 1, 0.7 + 0.3*sin(c*palette_size1*30));
			palette[c] = t.rgb();
		}
		for (int c=0; c<int(sizeof(mpalette)/sizeof(palette[0])); c++) {
			uint32_t color = 0;
			if (c & METACLASS_FLOOR)     color |= 0x0000FF;
			if (c & METACLASS_WALL)      color |= 0x000080;
			if (c & METACLASS_MYSELF)    color |= 0x400000;
			if (c & METACLASS_FURNITURE) color |= 0x008800;
			if (c & METACLASS_HANDLE)    color |= 0xFF8800;
			if (c & METACLASS_ITEM)      color |= 0xFF0000;
			mpalette[c] = color;
		}
	}
	if (!camera->camera_depth.empty())
	for (int y=0; y<aux_h; y++) {
		uchar* u = img_aux.scanLine(y);
		float* src = (float*) &camera->camera_depth[4*aux_w*y];
		uint8_t* msk = (uint8_t*) &camera->camera_depth_mask[aux_w*y];
		for (int x=0; x<aux_w; x++) {
			int ind = int(src[x] * 1024);
			(uint32_t&) u[4*x] = msk[x] ? palette[ind & (palette_size-1)] : 0x000000;
		}
	}
	p.drawImage( QRect(MARGIN + SCALE*w + MARGIN, MARGIN, w*SCALE, h*SCALE), img_aux);

	// metaclass
	if (!camera->camera_labeling.empty())
	for (int y=0; y<aux_h; y++) {
		uchar* u = img_aux.scanLine(y);
		uint8_t* src = (uint8_t*) &camera->camera_labeling[aux_w*y];
		uint8_t* msk = (uint8_t*) &camera->camera_labeling_mask[aux_w*y];
		for (int x=0; x<aux_w; x++) {
			(uint32_t&) u[4*x] = msk[x]*mpalette[src[x]];
		}
	}
	p.drawImage( QRect(MARGIN + SCALE*w + MARGIN + SCALE*w + MARGIN, MARGIN, w*SCALE, h*SCALE), img_aux);
	setWindowTitle(QString("RGB %1x%2, AUX %3x%4") . arg(w) . arg(h) . arg(camera->camera_aux_w) . arg(camera->camera_aux_h));
}