summaryrefslogtreecommitdiff
path: root/src/graphics/Filter3x3.h
blob: 0ec38817f1df80ea7a19782d5161b38b80e669db (plain)
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
//
//  libavg - Media Playback Engine.
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#ifndef _Filter3x3_H
#define _Filter3x3_H

#include "../api.h"
#include "Filter.h"

#include "Pixel8.h"
#include "Pixel24.h"
#include "Pixel32.h"

#include <iostream>

namespace avg {

// Filter that applies a 3x3 kernel to the bitmap.
class AVG_API Filter3x3 : public Filter
{
public:
    Filter3x3(float Mat[3][3]);
    virtual ~Filter3x3();
    virtual BitmapPtr apply(BitmapPtr pBmpSource);

private:
    template<class PIXEL>
    void convolveLine(const unsigned char * pSrc, unsigned char * pDest,
            int lineLen, int stride) const;
    float m_Mat[3][3];
};

template<class PIXEL>
void Filter3x3::convolveLine(const unsigned char * pSrc, unsigned char * pDest,
        int lineLen, int stride) const
{
    PIXEL * pSrcPixel = (PIXEL *)pSrc;
    PIXEL * pDestPixel = (PIXEL *)pDest;
    for (int x = 0; x < lineLen; ++x) {
        float newR = 0;
        float newG = 0;
        float newB = 0;

        for (int i = 0; i < 3; i++) {
            unsigned char * pLineStart = (unsigned char *)pSrcPixel+i*stride;
            for (int j = 0; j < 3; j++) {
                PIXEL SrcPixel = *((PIXEL *)pLineStart+j);
                newR += SrcPixel.getR()*m_Mat[i][j];
                newG += SrcPixel.getG()*m_Mat[i][j];
                newB += SrcPixel.getB()*m_Mat[i][j];
            }
        }
        *pDestPixel = PIXEL((unsigned char)newR, (unsigned char)newG,
                (unsigned char)newB);

        pSrcPixel++;
        pDestPixel++;
    }
}

}

#endif