Commit 67295f60 authored by Inge Wallin's avatar Inge Wallin
Browse files

Fix image effects 'grayscale' and 'mono'.

The original author had forgotten to take the alpha channel into account.

BUG: 260434
parent 5341d8c8
/* This file is part of the KDE project
* Copyright (C) 2010 Carlos Licea <carlos@kdab.com>
* Copyright (C) 2012 Inge Wallin <inge@lysator.liu.se>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -17,9 +18,11 @@
* Boston, MA 02110-1301, USA.
*/
#include "GreyscaleFilterEffect.h"
#include <KoFilterEffectRenderContext.h>
GreyscaleFilterEffect::GreyscaleFilterEffect()
: KoFilterEffect(GreyscaleFilterEffectId, "Grayscale effect")
{
......@@ -40,19 +43,26 @@ bool GreyscaleFilterEffect::load(const KoXmlElement& /*element*/, const KoFilter
QImage GreyscaleFilterEffect::processImage(const QImage& image, const KoFilterEffectRenderContext& context) const
{
QImage result = image;
QRgb* pixel = reinterpret_cast<QRgb*>( result.bits() );
const int right = context.filterRegion().right();
QImage result = image.convertToFormat(QImage::Format_ARGB32);
const int bottom = context.filterRegion().bottom();
const int left = context.filterRegion().left();
const int right = context.filterRegion().right();
const int width = result.width();
for( int row = context.filterRegion().top(); row < bottom; ++row ) {
for( int col = context.filterRegion().left(); col < right; ++col ){
const QRgb currentPixel = pixel[row * width + col];
const int red = qRed(currentPixel);
const int green = qGreen(currentPixel);
const int blue = qBlue(currentPixel);
const QRgb *src = (const QRgb*)image.bits();
QRgb *dst = (QRgb*)result.bits();
for (int row = context.filterRegion().top(); row < bottom; ++row) {
for (int col = left; col < right; ++col) {
int index = row * width + col;
const QRgb &s = src[index];
const int red = qRed(s);
const int green = qGreen(s);
const int blue = qBlue(s);
const int alpha = qAlpha(s);
const int grayValue = (red * 11 + green * 16 + blue * 5) / 32;
pixel[row * width + col] = qRgb(grayValue, grayValue, grayValue);
dst[index] = qRgba(grayValue, grayValue, grayValue, alpha);
}
}
return result;
......
......@@ -40,7 +40,7 @@ bool MonoFilterEffect::load(const KoXmlElement& /*element*/, const KoFilterEffec
QImage MonoFilterEffect::processImage(const QImage& image, const KoFilterEffectRenderContext& context) const
{
QImage result = image;
QImage result = image.convertToFormat(QImage::Format_ARGB32);
QRgb* pixel = reinterpret_cast<QRgb*>( result.bits() );
const int right = context.filterRegion().right();
const int bottom = context.filterRegion().bottom();
......@@ -51,8 +51,9 @@ QImage MonoFilterEffect::processImage(const QImage& image, const KoFilterEffectR
const int red = qRed(currentPixel);
const int green = qGreen(currentPixel);
const int blue = qBlue(currentPixel);
const int alpha = qAlpha(currentPixel);
const int monoValue = ( (red * 11 + green * 16 + blue * 5) / 32 ) / 127 * 255;
pixel[row * width + col] = qRgb(monoValue, monoValue, monoValue);
pixel[row * width + col] = qRgba(monoValue, monoValue, monoValue, alpha);
}
}
return result;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment