summaryrefslogtreecommitdiff
path: root/src/graphics/GPUFilter.cpp
blob: 36fe8118e0351a3e8e0c6311278137e4e341a256 (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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
//  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
//

#include "GPUFilter.h"
#include "Bitmap.h"

#include "VertexArray.h"
#include "ImagingProjection.h"
#include "GLContext.h"
#include "ShaderRegistry.h"
#include "Filterfliprgb.h"
#include "BitmapLoader.h"

#include "../base/ObjectCounter.h"
#include "../base/Exception.h"
#include "../base/MathHelper.h"

#include <iostream>
#include <string.h>

using namespace std;
using namespace boost;

namespace avg {

GPUFilter::GPUFilter(const string& sShaderID, bool bUseAlpha,
        bool bStandalone, unsigned numTextures, bool bMipmap)
    : m_bStandalone(bStandalone),
      m_NumTextures(numTextures),
      m_bMipmap(bMipmap),
      m_SrcSize(0,0),
      m_DestRect(0,0,0,0)
{
    m_PFSrc = BitmapLoader::get()->getDefaultPixelFormat(bUseAlpha);
    m_PFDest = m_PFSrc;
    createShader(sShaderID);

    m_pShader = avg::getShader(sShaderID);
    ObjectCounter::get()->incRef(&typeid(*this));
}

GPUFilter::GPUFilter(PixelFormat pfSrc, PixelFormat pfDest, bool bStandalone, 
        const string& sShaderID, unsigned numTextures, bool bMipmap)
    : m_PFSrc(pfSrc),
      m_PFDest(pfDest),
      m_bStandalone(bStandalone),
      m_NumTextures(numTextures),
      m_bMipmap(bMipmap),
      m_SrcSize(0,0),
      m_DestRect(0,0,0,0)
{
    createShader(sShaderID);
    m_pShader = avg::getShader(sShaderID);
    ObjectCounter::get()->incRef(&typeid(*this));
}

GPUFilter::~GPUFilter()
{
    ObjectCounter::get()->decRef(&typeid(*this));
}

BitmapPtr GPUFilter::apply(BitmapPtr pBmpSource)
{
    AVG_ASSERT(m_pSrcTex);
    AVG_ASSERT(!(m_pFBOs.empty()));
    m_pSrcMover->moveBmpToTexture(pBmpSource, *m_pSrcTex);
    apply(m_pSrcTex);
    BitmapPtr pFilteredBmp = m_pFBOs[0]->getImage();

    BitmapPtr pTmpBmp;
    if (pixelFormatIsBlueFirst(pFilteredBmp->getPixelFormat()) !=
            pixelFormatIsBlueFirst(pBmpSource->getPixelFormat()) &&
        pFilteredBmp->getBytesPerPixel() <= 4)
    {
        pTmpBmp = FilterFlipRGB().apply(pFilteredBmp);
    } else {
        pTmpBmp = pFilteredBmp;
    }

    BitmapPtr pDestBmp;
    if (pTmpBmp->getPixelFormat() != pBmpSource->getPixelFormat()) {
        pDestBmp = BitmapPtr(new Bitmap(m_DestRect.size(),
                pBmpSource->getPixelFormat()));
        pDestBmp->copyPixels(*pTmpBmp);
    } else {
        pDestBmp = pTmpBmp;
    }
    return pDestBmp;
}

void GPUFilter::apply(GLTexturePtr pSrcTex)
{
    m_pFBOs[0]->activate();
    applyOnGPU(pSrcTex);
    m_pFBOs[0]->copyToDestTexture();
}

GLTexturePtr GPUFilter::getDestTex(int i) const
{
    return m_pFBOs[i]->getTex();
}

BitmapPtr GPUFilter::getImage() const
{
    return m_pFBOs[0]->getImage();
}

FBOPtr GPUFilter::getFBO(int i)
{
    return m_pFBOs[i];
}

const IntRect& GPUFilter::getDestRect() const
{
    return m_DestRect;
}

const IntPoint& GPUFilter::getSrcSize() const
{
    return m_SrcSize;
}

FRect GPUFilter::getRelDestRect() const
{
    glm::vec2 srcSize(m_SrcSize);
    return FRect(m_DestRect.tl.x/srcSize.x, m_DestRect.tl.y/srcSize.y,
            m_DestRect.br.x/srcSize.x, m_DestRect.br.y/srcSize.y);
}

void GPUFilter::setDimensions(const IntPoint& srcSize)
{
    setDimensions(srcSize, IntRect(IntPoint(0,0), srcSize), GL_CLAMP_TO_EDGE);
}

void GPUFilter::setDimensions(const IntPoint& srcSize, const IntRect& destRect,
        unsigned texMode)
{
    bool bProjectionChanged = false;
    if (destRect != m_DestRect) {
        m_pFBOs.clear();
        for (unsigned i=0; i<m_NumTextures; ++i) {
            FBOPtr pFBO = FBOPtr(new FBO(destRect.size(), m_PFDest, 1, 1, false,
                    m_bMipmap));
            m_pFBOs.push_back(pFBO);
        }
        m_DestRect = destRect;
        bProjectionChanged = true;
    }
    if (m_bStandalone && srcSize != m_SrcSize) {
        m_pSrcTex = GLTexturePtr(new GLTexture(srcSize, m_PFSrc, false, 0, texMode, 
                texMode));
        m_pSrcMover = TextureMover::create(srcSize, m_PFSrc, GL_STREAM_DRAW);
        bProjectionChanged = true;
    }
    m_SrcSize = srcSize;
    if (bProjectionChanged) {
        m_pProjection = ImagingProjectionPtr(new ImagingProjection(srcSize, destRect));
    }
}
  
const OGLShaderPtr& GPUFilter::getShader() const
{
    return m_pShader;
}

void GPUFilter::draw(GLTexturePtr pTex)
{
    pTex->activate(GL_TEXTURE0);
    m_pProjection->draw(m_pShader);
}

void dumpKernel(int width, float* pKernel)
{
    cerr << "  Kernel width: " << width << endl;
    float sum = 0;
    for (int i = 0; i < width; ++i) {
        sum += pKernel[i];
        cerr << "  " << pKernel[i] << endl;
    }
    cerr << "Sum of coefficients: " << sum << endl;
}

int GPUFilter::getBlurKernelRadius(float stdDev) const
{
    return int(ceil(stdDev*3));
}

GLTexturePtr GPUFilter::calcBlurKernelTex(float stdDev, float opacity, bool bUseFloat)
        const
{
    AVG_ASSERT(opacity != -1);
    int kernelWidth;
    float* pKernel;
    if (stdDev == 0) {
        kernelWidth = 1;
        pKernel = new float[1];
        pKernel[0] = float(opacity);
    } else {
        float tempCoeffs[1024];
        int i=0;
        float coeff;
        do {
            coeff = float(exp(-i*i/(2*stdDev*stdDev))/sqrt(2*PI*stdDev*stdDev))
                    *float(opacity);
            tempCoeffs[i] = coeff;
            i++;
        } while (coeff > 0.003 && i < 1024);
        if (i > 1) {
            int kernelCenter = i - 2;
            kernelWidth = kernelCenter*2+1;
            pKernel = new float[kernelWidth];
            float sum = 0;
            for (int i = 0; i <= kernelCenter; ++i) {
                pKernel[kernelCenter+i] = tempCoeffs[i];
                sum += tempCoeffs[i];
                if (i != 0) {
                    pKernel[kernelCenter-i] = tempCoeffs[i];
                    sum += tempCoeffs[i];
                }
            }
            // Make sure the sum of coefficients is opacity despite the inaccuracies
            // introduced by using a kernel of finite size.
            for (int i = 0; i < kernelWidth; ++i) {
                pKernel[i] *= float(opacity)/sum;
            }
        } else {
            // Blur is so wide that all pixels would be black at 8-bit precision
            kernelWidth = 1;
            pKernel = new float[1];
            pKernel[0] = 0.;
        }
    }
//    dumpKernel(kernelWidth, pKernel);
    
    IntPoint size(kernelWidth, 1);
    PixelFormat pf;
    if (bUseFloat) {
        pf = R32G32B32A32F;
    } else {
        pf = I8;
    }
    GLTexturePtr pTex(new GLTexture(size, pf));
    TextureMoverPtr pKernelMover = TextureMover::create(size, pf, GL_STREAM_DRAW);
    BitmapPtr pBmp = pKernelMover->lock();
    void * pPixels = pBmp->getPixels();
    GLContext::checkError("GPUFilter::calcBlurKernelTex MapBuffer()");
    if (bUseFloat) {
        float * pCurFloat = (float*)pPixels;
        for (int i = 0; i < kernelWidth; ++i) {
            for (int j = 0; j < 4; ++j) {
                *pCurFloat = pKernel[i];
                ++pCurFloat;
            }
        }
    } else {
        unsigned char * pCurPixel = (unsigned char *)pPixels;
        for (int i = 0; i < kernelWidth; ++i) {
            *pCurPixel = (unsigned char)(pKernel[i]*255+0.5);
            ++pCurPixel;
        }
    }
    pKernelMover->unlock();
    pKernelMover->moveToTexture(*pTex);

    delete[] pKernel;
    return pTex;
}

}