1 /* This file is part of the KDE project
2  * Copyright (C) 2010 Carlos Licea <carlos@kdab.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "MonoFilterEffect.h"
21 #include <KoFilterEffectRenderContext.h>
22 
23 #include <QImage>
24 
MonoFilterEffect()25 MonoFilterEffect::MonoFilterEffect()
26 : KoFilterEffect(MonoFilterEffectId, "Mono Effect")
27 {
28 }
29 
~MonoFilterEffect()30 MonoFilterEffect::~MonoFilterEffect()
31 {
32 }
33 
save(KoXmlWriter &)34 void MonoFilterEffect::save(KoXmlWriter& /*writer*/)
35 {
36 }
37 
load(const KoXmlElement &,const KoFilterEffectLoadingContext &)38 bool MonoFilterEffect::load(const KoXmlElement& /*element*/, const KoFilterEffectLoadingContext& /*context*/)
39 {
40     return true;
41 }
42 
processImage(const QImage & image,const KoFilterEffectRenderContext & context) const43 QImage MonoFilterEffect::processImage(const QImage& image, const KoFilterEffectRenderContext& context) const
44 {
45     QImage result = image.convertToFormat(QImage::Format_ARGB32);
46     QRgb* pixel = reinterpret_cast<QRgb*>( result.bits() );
47     const int right = context.filterRegion().right();
48     const int bottom = context.filterRegion().bottom();
49     const int width = result.width();
50     for( int row = context.filterRegion().top(); row < bottom; ++row ) {
51         for( int col = context.filterRegion().left(); col < right; ++col ){
52             const QRgb currentPixel = pixel[row * width + col];
53             const int red = qRed(currentPixel);
54             const int green = qGreen(currentPixel);
55             const int blue = qBlue(currentPixel);
56             const int alpha = qAlpha(currentPixel);
57             const int monoValue = ( (red * 11 + green * 16 + blue * 5) / 32 ) / 127 * 255;
58             pixel[row * width + col] = qRgba(monoValue, monoValue, monoValue, alpha);
59         }
60     }
61     return result;
62 }
63 
64