-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageUtils.cpp
More file actions
71 lines (66 loc) · 2.6 KB
/
imageUtils.cpp
File metadata and controls
71 lines (66 loc) · 2.6 KB
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
// Copyright (c) 2017 Marek Dvoroznak
// Licensed under the MIT License.
#include "imageUtils.h"
#include <stack>
using namespace std;
using namespace Eigen;
void floodFill(Imguc &I, int startX, int startY, unsigned char targetColor, unsigned char replacementColor)
{
stack<pair<int,int>> S;
S.push(make_pair(startX,startY));
while (!S.empty()) {
const pair<int,int> &front = S.top();
const int x = front.first, y = front.second;
S.pop();
if (x < 0 || x >= I.w || y < 0 || y >= I.h) continue;
unsigned char &sample = I(x, y, 0);
if (targetColor == replacementColor || sample != targetColor) continue;
sample = replacementColor;
S.push(make_pair(x, y+1));
S.push(make_pair(x, y-1));
S.push(make_pair(x-1, y));
S.push(make_pair(x+1, y));
}
}
void floodFill(const Imguc &I, Imguc &O, int startX, int startY, unsigned char targetColor, unsigned char replacementColor)
{
stack<pair<int,int>> S;
S.push(make_pair(startX,startY));
while (!S.empty()) {
const pair<int,int> &front = S.top();
const int x = front.first, y = front.second;
S.pop();
if (x < 0 || x >= I.w || y < 0 || y >= I.h) continue;
const unsigned char &sampleI = I(x, y, 0);
unsigned char &sampleO = O(x, y, 0);
if (sampleI != targetColor || sampleO == replacementColor) continue;
// sampleI == targetColor && sampleO != replacementColor
sampleO = replacementColor;
S.push(make_pair(x, y+1));
S.push(make_pair(x, y-1));
S.push(make_pair(x-1, y));
S.push(make_pair(x+1, y));
}
}
void scanlineFillOutline(const Imguc &outline, Imguc &out, unsigned char outlineColor)
{
const Imguc &O = outline;
fora(y,1,out.h-1) {
bool inside = false;
unsigned char prev = O(0,0,0);
int stepUp = 0, stepDown = 0;
fora(x,1,out.w-1) {
const unsigned char sample = O(x,y,0);
if (sample != prev) {
if (sample == outlineColor) { stepUp = 0; stepDown = 0; }
if (sample == outlineColor && (O(x-1,y+1,0)==outlineColor || O(x,y+1,0)==outlineColor)) stepUp++; // start outline, step up
if (sample != outlineColor && (O(x-1,y-1,0)==outlineColor || O(x,y-1,0)==outlineColor)) stepUp++; // end outline, step up
if (sample == outlineColor && (O(x-1,y-1,0)==outlineColor || O(x,y-1,0)==outlineColor)) stepDown++; // start outline, step down
if (sample != outlineColor && (O(x-1,y+1,0)==outlineColor || O(x,y+1,0)==outlineColor)) stepDown++; // end outline, step down
if (sample != outlineColor && !(stepUp==stepDown && stepUp == 1)) inside = !inside;
}
if (inside) out(x,y,0) = outlineColor;
prev = sample;
}
}
}