Skip to content

Instantly share code, notes, and snippets.

@janisozaur
Created March 28, 2011 00:10
Show Gist options
  • Save janisozaur/889785 to your computer and use it in GitHub Desktop.
Save janisozaur/889785 to your computer and use it in GitHub Desktop.
void Image::changeBrighten(int value)
{
// DOPISAĆ FUNKCJĘ która będzie przechowywać oryginalne parametry obrazka!!
QRgb pixel;
//na początek obsługa obrazków w skali szarości i mono (mniej niż 8 bitów)
if(QImage::Format_RGB32 > img->format())
{
QVector<QRgb> colorTab = img->colorTable();
for(int i = 0; i < colorTab.count(); i++)
{
pixel = colorTab.at(i);
pixel = qRgb(this->sum(qRed(pixel), 0, 255, value), this->sum(qGreen(pixel), 0, 255, value), this->sum(qBlue(pixel), 0, 255, value));
img->setColorTable(colorTab); // this is spartaaa
}
}else{
//tutaj można załatwić kolorowe obrazki
for(int i = 0; i < img->width(); i++)
{
for(int j = 0; j < img->height(); j++)
{
pixel = img->pixel(i, j);
img->setPixel(i, j, qRgb(this->sum(qRed(pixel), 0, 255, value), this->sum(qGreen(pixel), 0, 255, value), this->sum(qBlue(pixel), 0, 255, value)));
}
}
}
}
QRgb Image::sum(QRgb pix, int low, int high, int val)
{
pix = pix + val;
//qDebug()<<"wypisz kazdy pixel: "<<pix<<endl;
if(pix > high)
{
pix = 255;
}
return pix;
}
QRgb Image::distinct(QRgb pix, int low, int high, int val)
{
int temp = pix;
//pix = pix - val;
temp -= val;
if(temp < low)
{
temp = 0;
}
pix = temp;
return pix;
}
void Image::changeDarkness(int value)
{
QRgb pixel;
//na początek obsługa obrazków w skali szarości i mono (mniej niż 8 bitów)
if(QImage::Format_RGB32 > img->format())
{
QVector<QRgb> colorTab = img->colorTable();
for(int i = 0; i < colorTab.count(); i++)
{
pixel = colorTab.at(i);
pixel = qRgb(this->distinct(qRed(pixel), 0, 255, value), this->distinct(qGreen(pixel), 0, 255, value), this->distinct(qBlue(pixel), 0, 255, value));
img->setColorTable(colorTab);
}
}else{
//tutaj można załatwić kolorowe obrazki
for(int i = 0; i < img->width(); i++)
{
for(int j = 0; j < img->height(); j++)
{
pixel = img->pixel(i, j);
img->setPixel(i, j, qRgb(this->distinct(qRed(pixel), 0, 255, value), this->distinct(qGreen(pixel), 0, 255, value), this->distinct(qBlue(pixel), 0, 255, value)));
}
}
}
}
void Image::negative()
{
QRgb pixel;
if(QImage::Format_RGB32 > img->format())
{
QVector<QRgb> colorTab = img->colorTable();
for(int i = 0; i < colorTab.count(); i++)
{
pixel = colorTab.at(i);
pixel = qRgb(255 - qRed(pixel), 255 - qGreen(pixel), 255 - qBlue(pixel));
colorTab.replace(i, pixel);
}
img->setColorTable(colorTab);
}else{
for(int i = 0; i < img->width(); i++)
{
for(int j = 0; j < img->height(); j++)
{
pixel = img->pixel(i, j);
img->setPixel(i, j, qRgb(255 - qRed(pixel), 255 - qGreen(pixel), 255 - qBlue(pixel)));
}
}
}
}
// DO TEGO TRZEBA DOROBIĆ SUWAK
void Image::contrast(float a)
{
QRgb pixel;
if(QImage::Format_RGB32 > img->format())
{
QVector<QRgb> colorTab = img->colorTable();
for(int i = 0; i < colorTab.count(); i++)
{
pixel = colorTab.at(i);
pixel = qRgb(this->calcContrast(qRed(pixel), a), this->calcContrast(qGreen(pixel), a), this->calcContrast(qBlue(pixel), a));
colorTab.replace(i, pixel);
}
img->setColorTable(colorTab);
}else{
for(int i = 0; i < img->width(); i++)
{
for(int j = 0; j < img->height(); j++)
{
pixel = img->pixel(i, j);
img->setPixel(i, j, qRgb(this->calcContrast(qRed(pixel), a), this->calcContrast(qGreen(pixel), a), this->calcContrast(qBlue(pixel), a)));
}
}
}
}
QRgb Image::calcContrast(QRgb pix, float a)
{
int temp = pix;
if( (a * (temp - 127) + 127) > 255)
{
temp = 255;
}else if( (a * (temp - 127) + 127) < 0){
temp = 0;
}else{
temp = a * (temp - 127) + 127;
}
pix = temp;
return pix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment