summaryrefslogtreecommitdiff
path: root/src/imaging/FilterDistortion.cpp
blob: 08b55bbbd987dd8b86946c0e670ac093bf9ec220 (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
83
//
//  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
//
//  Original author of this file is igor@c-base.org.
//

#include "FilterDistortion.h"

#include <iostream>
#include <math.h>

using namespace std;

namespace avg {

FilterDistortion::FilterDistortion(const IntPoint& srcSize,
        CoordTransformerPtr pTransformer)
    : m_SrcSize(srcSize),
      m_pTransformer(pTransformer) 
{
    // We use the same dimensions for both of src and dest and just crop.
    // for each pixel at (x,y) in the dest, m_pMap[x][y] contains an IntPoint that gives 
    // the coords in the src Bitmap. 
    m_pMap = new IntPoint[m_SrcSize.y*m_SrcSize.x];
    for (int y = 0; y < m_SrcSize.y; ++y) {
        for (int x = 0; x < m_SrcSize.x; ++x) {
            glm::dvec2 tmp = m_pTransformer->inverse_transform_point(glm::dvec2(x,y));
            IntPoint tmp2(int(tmp.x+0.5),int(tmp.y+0.5));
            if (tmp2.x < m_SrcSize.x && tmp2.y < m_SrcSize.y &&
                    tmp2.x >= 0 && tmp2.y >= 0)
            {
                m_pMap[y*m_SrcSize.x+x] = tmp2;
            } else {
                m_pMap[y*m_SrcSize.x+x] = IntPoint(0,0);
            }
        }
    }
}

FilterDistortion::~FilterDistortion()
{
    delete[] m_pMap;
}

BitmapPtr FilterDistortion::apply(BitmapPtr pBmpSource)
{
    BitmapPtr pDestBmp = BitmapPtr(new Bitmap(m_SrcSize, I8));
    unsigned char* pDestLine = pDestBmp->getPixels();
    unsigned char* pSrcPixels = pBmpSource->getPixels();
    unsigned char* pDestPixel = pDestLine;
    int destStride = pDestBmp->getStride();
    int srcStride = pBmpSource->getStride();
    IntPoint * pMapPos = m_pMap;
    for (int y = 0; y < m_SrcSize.y; ++y) {
        for(int x = 0; x < m_SrcSize.x; ++x) {
            *pDestPixel = pSrcPixels[pMapPos->x + srcStride*pMapPos->y];
            pDestPixel++;
            pMapPos++;
        }
        pDestLine+=destStride;
        pDestPixel = pDestLine;
    }
    return pDestBmp;
}

}