This repository has been archived by the owner on Apr 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpBrush.cpp
95 lines (83 loc) · 3.22 KB
/
ImpBrush.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
//
// ImpBrush.cpp
//
// The implementation of virtual brush. All the other brushes inherit from it.
//
#include "ImpBrush.h"
#include "impressionistDoc.h"
#include "impressionistUI.h"
#include <cstring>
// Static class member initializations
int ImpBrush::c_nBrushCount = 0;
ImpBrush **ImpBrush::c_pBrushes = nullptr;
const short ImpBrush::gaussian_filter[3][3] = {
{1, 2, 1}, {2, 4, 2}, {1, 2, 1}
};
ImpBrush::ImpBrush(ImpressionistDoc *pDoc, const char *name)
: m_pDoc(pDoc), m_pBrushName(name) {
// ImpBrush **brush_list = new ImpBrush *[c_nBrushCount + 1];
// FOR_EACH_BRUSH(i) { brush_list[i] = c_pBrushes[i]; }
// brush_list[c_nBrushCount++] = this;
// delete[] c_pBrushes;
// c_pBrushes = brush_list;
}
//---------------------------------------------------
// Return m_pDoc, which connects the UI and brushes
//---------------------------------------------------
ImpressionistDoc *ImpBrush::GetDocument(void) { return m_pDoc; }
//---------------------------------------------------
// Return the name of the current brush
//---------------------------------------------------
const char *ImpBrush::BrushName(void) { return m_pBrushName; }
//----------------------------------------------------
// Set the color to paint with to the color at source,
// which is the coord at the original window to sample
// the color from
//----------------------------------------------------
void ImpBrush::SetColor(const Point source) {
ImpressionistDoc *pDoc = GetDocument();
GLubyte color[4];
Point valid_source = pDoc->clip(source);
auto pixel = pDoc->m_pUI->m_origView->original_img(valid_source.y, valid_source.x);
color[0] = get<0>(pixel);
color[1] = get<1>(pixel);
color[2] = get<2>(pixel);
int colorBlending = pDoc->getColorBlending();
if (colorBlending == 1) {
vector<double> rgb = pDoc->getUserColor();
color[0] *= rgb[0];
color[1] *= rgb[1];
color[2] *= rgb[2];
}
const float alpha = pDoc->getAlpha();
color[3] = (GLubyte)(alpha * 255.0f);
glColor4ubv(color);
}
void ImpBrush::filter(const short filter[][3], const int divisor, const int dim, const Point source, std::vector<int>& color) {
// two for-loops for convolution
// change the contents of color as necessary
// for out-of-bounds, use the value of the border
short offset = (dim - 1) / 2;
ImpressionistDoc* pDoc = GetDocument();
int r = 0, g = 0, b = 0;
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
int x = source.x - i + offset;
int y = source.y - i + offset;
if (x < 0) x = 0;
else if (x >= pDoc->m_nPaintWidth) x = pDoc->m_nPaintWidth - 1;
if (y < 0) y = 0;
else if (y >= pDoc->m_nPaintHeight) y = pDoc->m_nPaintHeight - 1;
auto pixel = pDoc->m_pUI->m_origView->original_img(y, x);
r += get<0>(pixel) * filter[i][j];
g += get<1>(pixel) * filter[i][j];
b += get<2>(pixel) * filter[i][j];
}
}
color[0] = r / divisor;
color[1] = g / divisor;
color[2] = b / divisor;
}
void ImpBrush::set_brush(int index, ImpBrush *b) {
ImpBrush::c_pBrushes[index] = b;
}