portfolio/color-space/cvtab.cpp
2026-08-29 23:20:33 +02:00

148 lines
3.9 KiB
C++

#include "cvtab.h"
#define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))
#define MAX(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
cvTab::cvTab(QWidget *parent) :
QTabWidget(parent), _image(NULL)//,_model(NULL)
{
this->setParent(parent);
_Onglet();
// connect(this , SIGNAL(currentChanged(int)),this,SLOT(_currentChangedSlot(int)));
}
void cvTab::paintEvent(QPaintEvent* e){
if(!_image)
return;
QWidget *w=NULL;
if(currentIndex()==0){
//QPainter painter(w);
//QImage I=_image->copy();
// When no image has been loaded, there's nothing to draw.
w = widget(0);
QPainter painter(this);
painter.drawImage(QPoint(10, 30), *_image);
// w->setPixmap(QPixmap::fromImage(*_image));
//resize(_image->size().height()+10,_image->size().width()+10);
//adjustSize();
}
if(currentIndex()==1){
int hR[256],hG[256],hB[256];
calculHist(_image , hR,hG,hB);
// w =widget(1);
QPainter painter(this);
painter.setWindow(-10,-10,300,300);
QLineF axe_horiz(0, 30, 0, 170);
QLineF axe_verti(-30, 0, -30, 140);
painter.setFont(QFont("Calibri", 4));
painter.drawText(-2,30,"0");
painter.drawText(-3,148+30,"255");
painter.save();
// painter.rotate(90);
painter.drawLine(axe_horiz);
//painter.restore();
painter.save();
painter.rotate(-90);
painter.drawLine(axe_verti);
painter.restore();
painter.save();
painter.rotate(-90);
QPen penR(QColor("#FF0000"), 1, Qt::DotLine, Qt::FlatCap, Qt::RoundJoin);
QPen penG(QColor("#33FF00"), 1, Qt::DotLine, Qt::FlatCap, Qt::RoundJoin);
// painter.setPen(penHLines);
QPen penB(QColor("#0000FF"), 1, Qt::DotLine, Qt::FlatCap, Qt::RoundJoin);
//painter.setPen(penR);
//recher des du max
double maxR=0,maxG=0,maxB=0;
for (int i = 0; i < 256; i++)
{
maxR= MAX(maxR,hR[i]);
maxG= MAX(maxG,hG[i]);
maxB= MAX(maxB,hB[i]);
}
for (int i = 0; i < 256; i++)
{
painter.setPen(penR);
painter.drawPoint(-i-30,hR[i]/maxR *170 );
painter.setPen(penB);
painter.drawPoint(-i-30,hB[i]/maxB *170 );
painter.setPen(penG);
painter.drawPoint(-i-30,hG[i]/maxG *170 );
}
painter.restore();
painter.save();
painter.rotate(90);
painter.save();
//resize(_image->size().height()+10,_image->size().width()+10);
//adjustSize();
}
if(currentIndex()==2){
if(_image)
Histogramme_3D._setImg(_image);
}
show();
QWidget::paintEvent(e);
}
void cvTab::calculHist(QImage* image , int* histR,int*histG,int* histB)
{
int i;
if(image->allGray())
return;
/* initialisation de l'histogramme a 0*/
for(i = 0; i < 256;i++){
histR[i]=0;
histG[i]=0;
histB[i]=0;
}
/* calcule des valeur de l'histogramme */
for(int x = 0; x < image->width(); x++)
for(int y=0; y< image->height();y++){
histR[qRed(image->pixel(x,y))]++;
histG[qGreen(image->pixel(x,y))]++;
histB[qBlue(image->pixel(x,y))]++;
}
}
void cvTab::setImg(QImage* Img){
if(Img)
_image=Img;
}
//Slot that is called when Tab selection changes
void cvTab::_currentChangedSlot(int index)
{
if(currentIndex()==index)
return;
currentWidget()->hide();
setCurrentIndex(index);
currentWidget()->show();
// update();
}
void cvTab::_Onglet()
{
addTab(&Visuel,tr("Visuel"));
addTab(&Histograme_2D,tr("Histograme 2D"));
addTab(&Histogramme_3D,tr("Histogramme 3D"));
//setCurrentWidget(ajout);
connect(this,SIGNAL(currentChanged(int)), this,SLOT(_currentChangedSlot(int))) ;
}