-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlurBrush.cpp
146 lines (106 loc) · 3.99 KB
/
BlurBrush.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
//
// BlurBrush.cpp
//
// The implementation of Blur Brush. It is a kind of ImpBrush. All your brush implementations
// will look like the file with the different GL primitive calls.
//
#include "impressionistDoc.h"
#include "impressionistUI.h"
#include "BlurBrush.h"
#include<iostream>
extern float frand();
BlurBrush::BlurBrush(ImpressionistDoc* pDoc, char* name) :
ImpBrush(pDoc, name)
{
filter[0] = 0.0625; filter[1] = 0.125; filter[2] = 0.0625;
filter[3] = 0.125; filter[4] = 0.25; filter[5] = 0.125;
filter[6] = 0.0625; filter[7] = 0.125; filter[8] = 0.0625;
/*
filter[0][0] = 0.00000067; filter[0][1] = 0.00002292; filter[0][2] = 0.00019117; filter[0][3] = 0.00038771; filter[0][4] = 0.00019117; filter[0][5] = 0.00002292; filter[0][6] = 0.00000067;
filter[1][0] = 0.00002292; filter[1][1] = 0.00078634; filter[1][2] = 0.00655965; filter[1][3] = 0.01330373; filter[1][4] = 0.00655965; filter[1][5] = 0.00078633; filter[1][6] = 0.00002292;
filter[2][0] = 0.00019117; filter[2][1] = 0.00655965; filter[2][2] = 0.05472157; filter[2][3] = 0.11098164; filter[2][4] = 0.05472157; filter[2][5] = 0.00655965; filter[2][6] = 0.00019117;
filter[3][0] = 0.00038771; filter[3][1] = 0.01330373; filter[3][2] = 0.11098164; filter[3][3] = 0.22508352; filter[3][4] = 0.11098164; filter[3][5] = 0.01330373; filter[3][6] = 0.00038771;
filter[4][0] = 0.00019117; filter[4][1] = 0.00655965; filter[4][2] = 0.05472157; filter[4][3] = 0.11098164; filter[4][4] = 0.05472157; filter[4][5] = 0.00655965; filter[4][6] = 0.00019117;
filter[5][0] = 0.00002292; filter[5][1] = 0.00078634; filter[5][2] = 0.00655965; filter[5][3] = 0.01330373; filter[5][4] = 0.00655965; filter[5][5] = 0.00078633; filter[5][6] = 0.00002292;
filter[6][0] = 0.00000067; filter[6][1] = 0.00002292; filter[6][2] = 0.00019117; filter[6][3] = 0.00038771; filter[6][4] = 0.00019117; filter[6][5] = 0.00002292; filter[6][6] = 0.00000067;
*/
}
void BlurBrush::BrushBegin(const Point source, const Point target)
{
ImpressionistDoc* pDoc = GetDocument();
ImpressionistUI* dlg = pDoc->m_pUI;
int size = pDoc->getSize();
if (!dlg->getEnableAutoDraw())
saveState();
glPointSize((float)size);
BrushMove(source, target);
}
void BlurBrush::BrushMove(const Point source, const Point target)
{
ImpressionistDoc* pDoc = GetDocument();
ImpressionistUI* dlg = pDoc->m_pUI;
if (pDoc == NULL) {
printf("BlurBrush::BrushMove document is NULL\n");
return;
}
int x = target.x;
int y = target.y;
//store the pixels
float* buffer = new float[9 * 9 * 3];
memset(buffer, 0, 9 * 9 * 3);
int startCol = dlg->m_paintView->getStartCol();
int startRow = dlg->m_paintView->getStartRow() + dlg->m_mainWindow->h() - dlg->m_paintView->getDrawHeight() - 25;
//int startRow = 0;
int endCol = dlg->m_paintView->getEndCol();
int endRow = startRow + dlg->m_paintView->getDrawHeight();
if (x < startCol) {
x = startCol + 4;
}
if (x >= endCol) {
x = endCol - 4;
}
if (y <= startRow) {
y = startRow + 4;
}
if (y >= endRow) {
y = endRow - 4;
}
for (int Y = -4; Y < 5; Y++) {
for (int X = -4; X < 5; X++) {
applyFilter(x + X, y + Y, buffer, ((X + 4) + (Y + 4) * 9) * 3);
}
}
glPointSize(1);
glBegin(GL_POINTS);
for (int i = 0; i < 9 * 9; i++) {
glColor3ub((GLubyte)buffer[3 * i], (GLubyte)buffer[3 * i + 1], (GLubyte)buffer[3 * i + 2]);
int dx = i % 9;
int dy = i / 9;
glVertex2d(x + dx - 4, y + dy - 4);
}
glEnd();
delete[]buffer;
}
void BlurBrush::BrushEnd(const Point source, const Point target)
{
// do nothing so far
}
char* BlurBrush::BrushName(void) {
return ImpBrush::BrushName();
}
void BlurBrush::applyFilter(int x, int y, float*buffer, int index) {
ImpressionistDoc* pDoc = GetDocument();
for (int color = 0; color < 3; color++) {
float result = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
result += ((float)(*(pDoc->GetOriginalPixel(x + i, y + j) + color)))* filter[(i + 1) * 3 + j + 1];
}
}
if (result > 255)
result = 255;
if (result < 0)
result = 0;
buffer[index + color] = result;
}
}