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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/***************************************************************************
* *
* This file is part of the Fotowall project, *
* http://www.enricoros.com/opensource/fotowall *
* *
* Copyright (C) 2009 by TANGUY Arnaud <arn.tanguy@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "CPixmap.h"
#include "Shared/GlowEffectWidget.h"
#include <QImage>
#include <cmath>
CPixmap::CPixmap() {
}
CPixmap::CPixmap(const QString &filePath) : QPixmap(filePath), m_filePath(filePath) {
}
CPixmap::CPixmap(const QImage &image) : QPixmap(QPixmap::fromImage(image)), m_image(image) {
}
CPixmap::CPixmap(const QPixmap &pixmap): QPixmap(pixmap){
}
void CPixmap::addEffect(const PictureEffect & effect) {
switch (effect.effect) {
case PictureEffect::ClearEffects:
clearEffects();
break;
case PictureEffect::FlipH:
toHFlip();
break;
case PictureEffect::FlipV:
toVFlip();
break;
case PictureEffect::InvertColors:
toInvertedColors();
break;
case PictureEffect::NVG:
toNVG();
break;
case PictureEffect::BlackAndWhite:
toBlackAndWhite();
break;
case PictureEffect::Glow:
toGlow((int)effect.param);
break;
case PictureEffect::Sepia:
toSepia();
break;
case PictureEffect::Opacity:
m_effects.push_back(PictureEffect(PictureEffect::Opacity, (qreal)effect.param));
break;
case PictureEffect::Crop:
toCropped(effect.rect);
break;
case PictureEffect::AutoBlend:
toAutoBlend(effect.param);
break;
}
}
QList<PictureEffect> CPixmap::effects() const {
return m_effects;
}
void CPixmap::clearEffects()
{
if (!m_image.isNull())
updateImage(m_image);
else if (!m_filePath.isEmpty())
load(m_filePath);
m_effects.clear();
}
void CPixmap::toNVG() {
m_effects.push_back(PictureEffect::NVG);
QImage img = this->toImage();
QImage dest(img.size(), QImage::Format_ARGB32);
const int w = img.width();
const int h = img.height();
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
const QRgb pixel = img.pixel(x, y);
unsigned int average = (qGreen(pixel) + qRed(pixel) + qBlue(pixel)) / 3;
dest.setPixel(x, y, qRgba(average, average, average, qAlpha(pixel)));
}
}
updateImage(dest);
}
void CPixmap::toInvertedColors()
{
// can't use invertPixels, because it gives artifacts
m_effects.push_back(PictureEffect::InvertColors);
const QImage img = this->toImage();
QImage inverted(img.size(), QImage::Format_ARGB32);
const int w = img.width();
const int h = img.height();
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
QRgb pixel = img.pixel(x, y);
pixel = qRgba(255 - qRed(pixel), 255 - qGreen(pixel), 255 - qBlue(pixel), qAlpha(pixel));
inverted.setPixel(x,y,pixel);
}
}
updateImage(inverted);
}
void CPixmap::toHFlip() {
m_effects.push_back(PictureEffect::FlipH);
QImage img = this->toImage().mirrored(true, false);
updateImage(img);
}
void CPixmap::toVFlip() {
m_effects.push_back(PictureEffect::FlipV);
QImage img = this->toImage().mirrored(false, true);
updateImage(img);
}
void CPixmap::toBlackAndWhite()
{
m_effects.push_back(PictureEffect::BlackAndWhite);
const QImage img = this->toImage();
QImage dest(img.size(), QImage::Format_ARGB32);
const int w = img.width();
const int h = img.height();
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
const QRgb pixel = img.pixel(x, y);
unsigned int average = (qGreen(pixel)+ qRed(pixel) + qBlue(pixel)) / 3;
if (average > 127)
average = 255;
else
average = 0;
dest.setPixel(x,y, qRgba(average, average, average, qAlpha(pixel)));
}
}
updateImage(dest);
}
void CPixmap::toGlow(int radius) {
m_effects.push_back(PictureEffect(PictureEffect::Glow, (qreal)radius));
QImage dest = GlowEffectWidget::glown(this->toImage(), radius);
updateImage(dest);
}
void CPixmap::toSepia()
{
m_effects.push_back(PictureEffect::Sepia);
const QImage img = this->toImage();
QImage dest(img.size(), QImage::Format_ARGB32);
const int w = img.width();
const int h = img.height();
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
QRgb pixel = img.pixel(x, y);
const unsigned int average = (qGreen(pixel) + qRed(pixel) + qBlue(pixel)) / 3;
const int red = average*1.176, green = average*0.837, blue = average*0.558;
pixel = qRgba((red <= 255) ? red : 255, (green <= 255) ? green : 255, (blue <= 255) ? blue : 255, qAlpha(pixel));
dest.setPixel(x,y,pixel);
}
}
updateImage(dest);
}
/*
void CPixmap::toLuminosity(int value) {
m_effects.push_back(...);
QImage img = this->toImage();
QImage dest(img.size(), img.format());
QColor pixel;
for(int x=0; x<img.width();x++) {
for (int y=0; y<img.height(); y++) {
pixel = img.pixel(x, y);
int green = pixel.green() + value;
int red = pixel.red() + value;
int blue = pixel.blue() + value;
if(green > 255) green = 255;
else if(green < 0) green = 0;
if(red > 255) red = 255;
else if(red < 0) red = 0;
if(blue > 255) blue = 255;
else if(blue <0) blue = 0;
pixel.setGreen(green);
pixel.setBlue(blue);
pixel.setRed(red);
dest.setPixel(x,y,pixel.rgb());
}
}
updateImage(dest);
}
*/
void CPixmap::toCropped(const QRect &cropRect)
{
if (cropRect.isNull()) return;
m_effects.push_back(PictureEffect(PictureEffect::Crop, PictureEffect::Crop, cropRect));
const QImage img = this->toImage().copy(cropRect);
updateImage(img);
}
void CPixmap::toAutoBlend(qreal strength)
{
m_effects.push_back(PictureEffect(PictureEffect::AutoBlend, strength));
const QImage img = this->toImage();
QImage dest(img.size(), QImage::Format_ARGB32);
const int w = img.width();
const int h = img.height();
// create a fast lookup-table for the 256 possible alpha levels {a = i^5^(2*strength - 1)}
int gammaValue[256];
qreal expIdx = pow((qreal)5, (strength * 2) - 1);
for (int i = 0; i < 256; i++) {
qreal unif = (qreal)i / 255.f;
gammaValue[i] = (int)(255.f * pow(unif, expIdx));
}
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
QRgb pixel = img.pixel(x, y);
const int gray = qGray(pixel);
const int alpha = gammaValue[gray];
pixel = qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), alpha);
dest.setPixel(x, y, pixel);
}
}
updateImage(dest);
}
void CPixmap::updateImage(const QImage & newImage)
{
QImage copyImage = m_image;
QString copyFilePath = m_filePath;
QList<PictureEffect> copyEffects = m_effects;
*this = fromImage(newImage);
m_image = copyImage;
m_filePath = copyFilePath;
m_effects = copyEffects;
}
|