forked from evgeny-nadymov/telegram-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtils.cpp
More file actions
executable file
·175 lines (145 loc) · 4.08 KB
/
Copy pathImageUtils.cpp
File metadata and controls
executable file
·175 lines (145 loc) · 4.08 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
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
166
167
168
169
170
171
172
173
174
175
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
#include "pch.h"
#include "ImageUtils.h"
#include <time.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
using namespace TelegramClient_WebP;
ImageUtils::ImageUtils()
{
}
#define SIZE_FULL 8100
static inline uint64_t get_colors(const uint8_t *p) {
return p[0] + (p[1] << 16) + ((uint64_t)p[2] << 32);
}
static void fastBlur(int width, int height, int s, void *pixels, int radius) {
uint8_t *pix = (uint8_t *)pixels;
const int w = width;
const int h = height;
const int stride = s;
const int r1 = radius + 1;
const int div = radius * 2 + 1;
int shift;
if (radius == 1) {
shift = 2;
}
else if (radius == 3) {
shift = 4;
}
else if (radius == 7) {
shift = 6;
}
else if (radius == 15) {
shift = 8;
}
else {
return;
}
if (radius > 15 || div >= w || div >= h || w * h > SIZE_FULL) {
return;
}
static uint64_t rgb[SIZE_FULL];
int x, y, i;
int yw = 0;
const int we = w - r1;
for (y = 0; y < h; y++) {
uint64_t cur = get_colors(&pix[yw]);
uint64_t rgballsum = -radius * cur;
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
for (i = 1; i <= radius; i++) {
uint64_t cur = get_colors(&pix[yw + i * 4]);
rgbsum += cur * (r1 - i);
rgballsum += cur;
}
x = 0;
#define update(start, middle, end) \
rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FF; \
\
rgballsum += get_colors (&pix[yw + (start) * 4]) - \
2 * get_colors (&pix[yw + (middle) * 4]) + \
get_colors (&pix[yw + (end) * 4]); \
rgbsum += rgballsum; \
x++; \
while (x < r1) {
update(0, x, x + r1);
}
while (x < we) {
update(x - r1, x, x + r1);
}
while (x < w) {
update(x - r1, x, w - 1);
}
#undef update
yw += stride;
}
const int he = h - r1;
for (x = 0; x < w; x++) {
uint64_t rgballsum = -radius * rgb[x];
uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
for (i = 1; i <= radius; i++) {
rgbsum += rgb[i * w + x] * (r1 - i);
rgballsum += rgb[i * w + x];
}
y = 0;
int yi = x * 4;
#define update(start, middle, end) \
int64_t res = rgbsum >> shift; \
pix[yi] = res; \
pix[yi + 1] = res >> 16; \
pix[yi + 2] = res >> 32; \
\
rgballsum += rgb[x + (start) * w] - \
2 * rgb[x + (middle) * w] + \
rgb[x + (end) * w]; \
rgbsum += rgballsum; \
y++; \
yi += stride;
while (y < r1) {
update(0, y, y + r1);
}
while (y < he) {
update(y - r1, y, y + r1);
}
while (y < h) {
update(y - r1, y, h - 1);
}
#undef update
}
}
Platform::Array<uint8>^ ImageUtils::FastBlur(int width, int height, int stride, const Platform::Array<uint8>^ pixels){
auto pix = new uint8_t[pixels->Length];
for (int i = 0; i < pixels->Length; i++) {
pix[i] = pixels[i];
}
fastBlur(width, height, stride, pix, 3);
Platform::Array<uint8>^ returnPixels = ref new Platform::Array<uint8>(pixels->Length);
for (int i = 0; i < pixels->Length; i++) {
returnPixels[i] = pix[i];
}
delete[] pix;
return returnPixels;
}
Platform::Array<uint8>^ ImageUtils::FastSecretBlur(int width, int height, int stride, const Platform::Array<uint8>^ pixels){
auto pix = new uint8_t[pixels->Length];
for (int i = 0; i < pixels->Length; i++) {
pix[i] = pixels[i];
}
fastBlur(width, height, stride, pix, 15);
Platform::Array<uint8>^ returnPixels = ref new Platform::Array<uint8>(pixels->Length);
for (int i = 0; i < pixels->Length; i++) {
returnPixels[i] = pix[i];
}
delete[] pix;
return returnPixels;
}