Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
image_processor/Distortion.cpp at main · sinfillo/image_processor · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
sinfillo
/
image_processor
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
image_processor
/
Distortion.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
151 lines (145 loc) · 6.65 KB
main
Breadcrumbs
image_processor
/
Distortion.cpp
Copy path
Top
File metadata and controls
Code
Blame
151 lines (145 loc) · 6.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
//
Created by Дарья Фомина on 26.03.2023.
//
#
include
"
Distortion.h
"
Distortion::Distortion
() {
}
Image
Distortion::Apply
(
const
Image& base_image) {
cnt_iters_ =
std::min
(cnt_iters_, base_image.
GetWidth
());
Image ans = base_image;
//
clock_t dptime = 0;
//
clock_t pathtime = 0;
while
(cnt_iters_-- >
0
) {
std::cerr << cnt_iters_ <<
"
iterations remain
"
<< std::endl;
std::vector<std::vector<
double
>>
all_dist
(ans.
GetHeight
(), std::vector<
double
>(ans.
GetWidth
(),
INF
));
//
std::vector<std::vector<char>> prev(ans.GetHeight(), std::vector<char>(ans.GetWidth(), '!'));
//
std::set<Info> que;
//
std::priority_queue<Info, std::vector<Info>, std::greater<>> que;
//
auto start_time = clock();
for
(
size_t
x =
0
; x < ans.
GetWidth
(); ++x) {
//
que.insert(Info{.x_coord_ = x, .y_coord_ = ans.GetHeight() - 1, .dist_ = 0});
//
que.emplace(Info{.x_coord_ = x, .y_coord_ = ans.GetHeight() - 1, .dist_ = 0});
all_dist[ans.
GetHeight
() -
1
][x] =
0
;
//
prev[ans.GetHeight() - 1][x] = '.';
}
/*
while (!que.empty()) {
// auto cur = que.begin();
auto cur = *que.begin();
if (cur.y_coord_ == 0) {
break;
}
que.erase(que.begin());
// que.pop();
for (ssize_t dx = -1; dx <= 1; ++dx) {
ssize_t want_x = dx + cur.x_coord_;
ssize_t want_y = cur.y_coord_ - 1;
if (want_x < 0 || want_x >= ans.GetWidth() || want_y < 0 || want_y >= ans.GetHeight()) {
continue;
}
double weight = Dist(ans.GetColor(want_x, want_y), ans.GetColor(cur.x_coord_, cur.y_coord_));
if (all_dist[want_y][want_x] > all_dist[cur.y_coord_][cur.x_coord_] + weight) {
que.erase(Info{.x_coord_ = static_cast<size_t>(want_x), .y_coord_ = static_cast<size_t>(want_y),
.dist_ = all_dist[want_y][want_x]}); all_dist[want_y][want_x] = all_dist[cur.y_coord_][cur.x_coord_] + weight;
que.emplace(Info{.x_coord_ = static_cast<size_t>(want_x), .y_coord_ = static_cast<size_t>(want_y),
.dist_ = all_dist[want_y][want_x]});
// que.insert(Info{.x_coord_ = static_cast<size_t>(want_x), .y_coord_ = static_cast<size_t>(want_y),
.dist_ = all_dist[want_y][want_x]}); if (dx == -1) { prev[want_y][want_x] = '0'; } else if (dx == 0) {
prev[want_y][want_x] = '1';
} else {
prev[want_y][want_x] = '2';
}
}
}
}
*/
if
(ans.
GetHeight
() >
1
) {
for
(
ssize_t
y =
static_cast
<
ssize_t
>(ans.
GetHeight
()) -
1
; y >=
1
; --y) {
for
(
ssize_t
x =
0
; x <
static_cast
<
ssize_t
>(ans.
GetWidth
()); ++x) {
for
(
ssize_t
dx = -
1
; dx <=
1
; ++dx) {
ssize_t
want_x = dx + x;
ssize_t
want_y = y -
1
;
if
(want_x <
0
|| want_x >= ans.
GetWidth
() || want_y <
0
|| want_y >= ans.
GetHeight
()) {
continue
;
}
double
weight =
Dist
(ans.
GetColor
(want_x, want_y), ans.
GetColor
(x, y));
if
(all_dist[want_y][want_x] > all_dist[y][x] + weight) {
all_dist[want_y][want_x] = all_dist[y][x] + weight;
/*
if (dx == -1) {
prev[want_y][want_x] = '0';
} else if (dx == 0) {
prev[want_y][want_x] = '1';
} else {
prev[want_y][want_x] = '2';
}
*/
}
}
}
}
}
//
dptime += clock() - start_time;
//
start_time = clock();
ssize_t
x_min = -
1
;
double
dist_min =
INF
;
for
(
ssize_t
x =
0
; x < ans.
GetWidth
(); ++x) {
if
(x_min == -
1
|| dist_min > all_dist[
0
][x]) {
x_min = x;
dist_min = all_dist[
0
][x_min];
}
}
ssize_t
cur_y =
0
;
std::vector<std::pair<
size_t
,
size_t
>> need_erase;
while
(cur_y != ans.
GetHeight
()) {
//
std::cerr << x_min << " " << cur_y << std::endl;
//
ans.EraseColor(x_min, cur_y);
need_erase.
push_back
({
static_cast
<
size_t
>(x_min),
static_cast
<
size_t
>(cur_y)});
ssize_t
want_y = cur_y +
1
;
if
(want_y == ans.
GetHeight
()) {
break
;
}
for
(
ssize_t
dx = -
1
; dx <=
1
; ++dx) {
ssize_t
want_x = x_min + dx;
//
std::cout << want_x << " ";
if
(want_x >=
0
&& want_x < ans.
GetWidth
() &&
all_dist[want_y][want_x] +
Dist
(ans.
GetColor
(x_min, cur_y), ans.
GetColor
(want_x, want_y)) ==
all_dist[cur_y][x_min]) {
x_min = want_x;
cur_y =
static_cast
<
ssize_t
>(want_y);
break
;
}
}
//
std::cout << std::endl;
}
for
(
const
auto
& elem : need_erase) {
ans.
EraseColor
(elem.
first
, elem.
second
);
}
ans.
SetWidth
(ans.
GetWidth
() -
1
);
//
pathtime += clock() - start_time;
}
const
int64_t
padding_amount =
static_cast
<
int64_t
>((
4
- ((ans.
GetWidth
() *
3
) %
4
)) %
4
);
const
size_t
file_size =
FILE_HEADER_SIZE
+
INFORMATION_HEADER_SIZE
+ ans.
GetWidth
() * ans.
GetHeight
() *
3
+
padding_amount * ans.
GetHeight
();
ans.
SetFileSize
(file_size);
/*
std::cerr << "Time spent on calculating dp: " << (double)dptime / CLOCKS_PER_SEC << std::endl
<< "Time spent on erasing path: " << (double)pathtime / CLOCKS_PER_SEC << std::endl;
*/
return
ans;
}
double
Distortion::Dist
(
const
Image::Color& a,
const
Image::Color& b) {
double
dist_r = a.
r_
- b.
r_
;
double
dist_g = a.
g_
- b.
g_
;
double
dist_b = a.
b_
- b.
b_
;
//
return std::abs(dist_r) + std::abs(dist_g) + std::abs(dist_b);
return
std::sqrt
(dist_r * dist_r + dist_g * dist_g + dist_b * dist_b);
}
Distortion::Distortion
(
const
size_t
cnt_iters) : cnt_iters_(cnt_iters) {
}
bool
Distortion::Info::
operator
<(
const
Info& other)
const
{
if
(other.
dist_
== dist_) {
if
(other.
x_coord_
== x_coord_) {
return
y_coord_ < other.
y_coord_
;
}
return
x_coord_ < other.
x_coord_
;
}
return
dist_ < other.
dist_
;
}
bool
Distortion::Info::
operator
>(
const
Distortion::Info& other)
const
{
return
other < *
this
;
}
You can’t perform that action at this time.