-
Notifications
You must be signed in to change notification settings - Fork 3
/
PatternBrush.cpp
165 lines (137 loc) · 3.43 KB
/
PatternBrush.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
#include "PatternBrush.h"
#include <filesystem>
#include <FL/fl_ask.H>
#include "CurvedBrush.h"
#include <FL/Fl_Native_File_Chooser.H>
volatile bool PatternBrush::isGenerating = false;
std::vector< ImageWrapper<GLubyte> > PatternBrush::patterns{};
std::vector< ImageWrapper<GLubyte> > PatternBrush::scaledPatterns{};
std::random_device PatternBrush::rd{};
std::default_random_engine PatternBrush::gen{rd()};
std::uniform_int_distribution<> PatternBrush::dis(1,10);
int PatternBrush::size = -1;
void PatternBrush::cb_set_patterns(Fl_Widget*, void*)
{
Fl_Native_File_Chooser chooser(Fl_Native_File_Chooser::BROWSE_DIRECTORY);
chooser.directory(std::filesystem::current_path().string().c_str());
chooser.show();
auto* directoryName = chooser.filename();
if (directoryName == nullptr)
{
fl_alert("No directory selected!");
}
else
{
setPatterns(directoryName);
}
}
void PatternBrush::setPatterns(const std::string& directoryName)
{
const std::filesystem::path patternDirectory(directoryName);
if (is_directory(patternDirectory))
{
patterns.clear();
for (auto& entry: std::filesystem::directory_iterator(patternDirectory))
{
if (entry.path().extension() == ".bmp")
{
//Entry should be an alpha map.
auto width = 0;
auto height = 0;
auto xxxxx = readBMP(entry.path().string().c_str(), width, height);
patterns.push_back({
xxxxx,
{width, height}
});
}
}
dis = std::uniform_int_distribution<>(0, patterns.size() - 1);
}
else
{
fl_alert("Selected path is not a directory.");
}
}
PatternBrush::PatternBrush(ImpressionistDoc* pDoc, char*): ImpBrush(pDoc)
{
}
void PatternBrush::BrushBegin(const Point source, const Point target)
{
const auto newSize = GetDocument()->m_pUI->getSize();
if (newSize != size)
{
isGenerating = true;
size = newSize;
//make scaled images
const Dim newDim = { size, size };
scaledPatterns.clear();
for (auto& w: patterns)
{
ImageWrapper<GLubyte> v{
new GLubyte[newDim.getLength()],
newDim
};
const auto scale = double(w.dim.width) / size;
v.eachPixel([&](GLubyte* rgbArray, const long x, const long y)
{
auto* t = w.getPixelPtr(x * scale, y * scale);
rgbArray[0] = t[0];
rgbArray[1] = t[1];
rgbArray[2] = t[2];
});
scaledPatterns.push_back(v);
}
isGenerating = false;
}
BrushMove(source, target);
}
void PatternBrush::BrushMove(const Point source, const Point target)
{
auto* docPtr = GetDocument();
if (docPtr == nullptr) {
__debugbreak();
return;
}
if (isGenerating)
{
return; //Scaled brushes still generating
}
if (source.x > docPtr->m_nPaintWidth || source.y < 0)
{
return;
}
if (step == 0)
{
//Make new pattern
const auto i = dis(gen);
if (i < 0 || i >= scaledPatterns.size()) return;
auto& alpha = scaledPatterns[i];
const int tX0 = target.x - alpha.dim.width / 2;
const int tY0 = target.y - alpha.dim.height / 2;
glPointSize(1.f);
glBegin(GL_POINTS);
{
alpha.eachPixel([&](const GLubyte* rgbArray, const long x, const long y)
{
const auto tX = tX0 + x;
const auto tY = tY0 + y;
if (tX < 0 || tX >= docPtr->m_nPaintWidth || tY < 0 || tY >= docPtr->m_nPaintHeight) return;
if (rgbArray[0] > 0)
{
docPtr->m_pUI->setAlpha(rgbArray[0] / 255.0);
SetColor(source);
glVertex2d(tX, tY);
}
});
}
glEnd();
step = alpha.dim.width / 4;
}
else
{
step--;
}
}
void PatternBrush::BrushEnd(const Point source, const Point target)
{
}