-
Notifications
You must be signed in to change notification settings - Fork 1
/
GaussianBlur.cpp
228 lines (213 loc) · 6.7 KB
/
GaussianBlur.cpp
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
#include "GaussianBlur.h"
#include <SDL2/SDL.h>
#include <stdexcept>
#include "SdlTools.h"
#include "tools.h"
using namespace std;
/// Surface spiegeln
void FlipSurface(SDL_Surface* pSurface, bool aFlipHorizontal)
{
int bpp=SDL_BITSPERPIXEL(pSurface->format->format);
if (bpp!=32)
throw runtime_error(strprintf("Invalid pixel format for FlipSurface: %s. Bpp: %d",SDL_GetPixelFormatName(pSurface->format->format),bpp));
if (!aFlipHorizontal)
{
// Spiegelung an der horizontalen(!) Achse. Eigentlich ist die Bezeichnung verwirrend, aber kompatibel mit der libSDL
unsigned char* pBuffer=new unsigned char[pSurface->pitch];
for (int y=0; y<pSurface->h/2; y++)
{
// temporäre Kopie einer Zeile in den Puffer
memcpy(pBuffer,(unsigned char *)(pSurface->pixels)+(pSurface->h-y-1)*pSurface->pitch,pSurface->pitch);
// von oben nach unten Kopieren
memcpy((unsigned char *)(pSurface->pixels)+(pSurface->h-y-1)*pSurface->pitch,(unsigned char *)(pSurface->pixels)+y*pSurface->pitch,pSurface->pitch);
// Zum Schluss Kopie von unten nach oben kopieren
memcpy((unsigned char *)(pSurface->pixels)+y*pSurface->pitch,pBuffer,pSurface->pitch);
}
delete[] pBuffer;
}
else
{
Uint32 Buf;
// Spiegelung an der vertikalen Achse
for (int y=0; y<pSurface->h; y++)
{
unsigned char *pB=(unsigned char *)(pSurface->pixels)+y*pSurface->pitch;
Uint32 *pStart=(Uint32*)pB;
for (int x=0; x<pSurface->w/2; x++)
{
Buf=pStart[pSurface->w-x-1];
pStart[pSurface->w-x-1]=pStart[x];
pStart[x]=Buf;
}
}
}
}
/// wendet den Blur-Effekt auf eine Textur an
void BlurTexture(SDL_Texture* pTexture, unsigned int r)
{
int CurrentTextureWidth=0;
int CurrentTextureHeight=0;
unsigned char * aPixelsCurrent=NULL;
int mPitchCurrent=0;
Uint32 mFormat=SDL_PIXELFORMAT_UNKNOWN;
check_error_sdl(SDL_QueryTexture(pTexture, &mFormat, NULL, &CurrentTextureWidth, &CurrentTextureHeight),"SDL_QueryTexture");
check_error_sdl(SDL_LockTexture(pTexture,NULL, (void**)&aPixelsCurrent, &mPitchCurrent),"SDL_LockTexture");
BlurRGBA(CurrentTextureWidth, CurrentTextureHeight, mPitchCurrent, aPixelsCurrent, mFormat, r);
SDL_UnlockTexture(pTexture);
}
void BlurSurface(SDL_Surface* pSurface, unsigned int r)
{
BlurRGBA(pSurface->w, pSurface->h, pSurface->pitch, (unsigned char*)pSurface->pixels, pSurface->format->format, r);
}
void BlurRGBA(int aWidth, int aHeight, int aPitch, unsigned char * pPixels, Uint32 aPixelFormat, int r, bool sw)
{
// Kein Blur, falls der Blur-Radius größer als die Höhe des Bildes ist.
if (aHeight / 2 <= r)
{
r = aHeight % 2 == 0 ? aHeight / 2 - 1 : aHeight / 2;
}
if (r<2)
return;
if (aWidth / 2 <= r)
{
r = aWidth % 2 == 0 ? aWidth / 2 - 1 : aWidth / 2;
}
if (r<2)
return;
const int NumBytesCurrent=aPitch*aHeight;
// Aufteilung in einzelne Farbkanäle
const int ChannelSize=NumBytesCurrent/4;
unsigned char * rc=new unsigned char[ChannelSize+r];
unsigned char * gc=new unsigned char[ChannelSize+r];
unsigned char * bc=new unsigned char[ChannelSize+r];
unsigned char * rc_d=new unsigned char[ChannelSize+r];
unsigned char * gc_d=new unsigned char[ChannelSize+r];
unsigned char * bc_d=new unsigned char[ChannelSize+r];
int cc=0;
for (int i=0; i<NumBytesCurrent; i+=4)
{
bc[cc]=pPixels[i];
gc[cc]=pPixels[i+1];
rc[cc]=pPixels[i+2];
bc_d[cc]=bc[cc];
gc_d[cc]=gc[cc];
rc_d[cc]=rc[cc];
cc++;
}
gaussBlur(bc,bc_d,ChannelSize,aWidth,aHeight,r);
gaussBlur(gc,gc_d,ChannelSize,aWidth,aHeight,r);
gaussBlur(rc,rc_d,ChannelSize,aWidth,aHeight,r);
cc=0;
for (int i=0; i<NumBytesCurrent; i+=4)
{
if (sw)
{
double in=min(255.0,0.3*rc_d[cc]+0.59*gc_d[cc]+0.11*bc_d[cc])*0.8;
unsigned char i_i=(unsigned char)(in);
pPixels[i]=i_i;
pPixels[i+1]=i_i;
pPixels[i+2]=i_i;
cc++;
}
else
{
pPixels[i]=bc_d[cc];
pPixels[i+1]=gc_d[cc];
pPixels[i+2]=rc_d[cc];
cc++;
}
}
delete[] rc;
delete[] gc;
delete[] bc;
delete[] rc_d;
delete[] gc_d;
delete[] bc_d;
}
/// Einen Farbkanal blurren
void gaussBlur (unsigned char *scl, unsigned char* tcl, unsigned int scl_length, unsigned int w, unsigned int h, unsigned int r)
{
vector<unsigned int> bxs = boxesForGauss(r, 3);
boxBlur (scl, tcl, scl_length, w, h, (bxs[0]-1)/2);
boxBlur (tcl, scl, scl_length, w, h, (bxs[1]-1)/2);
boxBlur (scl, tcl, scl_length, w, h, (bxs[2]-1)/2);
}
vector<unsigned int> boxesForGauss(unsigned int sigma, unsigned int n) // standard deviation, number of boxes
{
double wIdeal = sqrt((12*sigma*sigma/n)+1); // Ideal averaging filter width
unsigned int wl = floor(wIdeal);
if(wl%2==0)
wl--;
int wu = wl+2;
double mIdeal = (12*sigma*sigma - n*wl*wl - 4*n*wl - 3*n)/(-4*wl - 4);
unsigned int m = (unsigned int) (mIdeal+0.5);
vector<unsigned int> sizes;
for(unsigned int i=0; i<n; i++)
sizes.push_back(i<m?wl:wu);
return sizes;
}
void boxBlur (unsigned char *scl, unsigned char *tcl, unsigned int scl_length, unsigned int w, unsigned int h, unsigned int r)
{
memcpy(tcl,scl,scl_length);
boxBlurH(tcl, scl, w, h, r);
boxBlurT(scl, tcl, w, h, r);
}
void boxBlurH (unsigned char *scl, unsigned char *tcl, unsigned int w, unsigned int h, unsigned int r)
{
double iarr = 1 / double(r+r+1);
for(unsigned int i=0; i<h; i++)
{
unsigned int ti = i*w, li = ti, ri = ti+r;
unsigned int fv = scl[ti], lv = scl[ti+w-1], val = (r+1)*fv;
for(unsigned int j=0; j<r; j++)
val += scl[ti+j];
for(unsigned int j=0; j<=r; j++)
{
val += scl[ri++] - fv;
tcl[ti++] = (unsigned char)(val*iarr+0.5);
}
for(unsigned int j=r+1; j<w-r; j++)
{
val += scl[ri++] - scl[li++];
tcl[ti++] = (unsigned char)(val*iarr+0.5);
}
for(unsigned int j=w-r; j<w; j++)
{
val += lv - scl[li++];
tcl[ti++] = (unsigned char)(val*iarr+0.5);
}
}
}
void boxBlurT (unsigned char *scl, unsigned char *tcl, unsigned int w, unsigned int h, unsigned int r)
{
double iarr = 1 / double(r+r+1);
for(unsigned int i=0; i<w; i++)
{
unsigned int ti = i, li = ti, ri = ti+r*w;
unsigned int fv = scl[ti], lv = scl[ti+w*(h-1)], val = (r+1)*fv;
for(unsigned int j=0; j<r; j++)
val += scl[ti+j*w];
for(unsigned int j=0; j<=r; j++)
{
val += scl[ri] - fv;
tcl[ti] = (unsigned char)(val*iarr+0.5);
ri+=w;
ti+=w;
}
for(unsigned int j=r+1; j<h-r; j++)
{
val += scl[ri] - scl[li];
tcl[ti] = (unsigned char)(val*iarr+0.5);
li+=w;
ri+=w;
ti+=w;
}
for(unsigned int j=h-r; j<h; j++)
{
val += lv - scl[li];
tcl[ti] = (unsigned char)(val*iarr+0.5);
li+=w;
ti+=w;
}
}
}