-
Notifications
You must be signed in to change notification settings - Fork 6
/
quantizer_cpp.patch
278 lines (233 loc) · 8.17 KB
/
quantizer_cpp.patch
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
diff '--color=auto' -uNr patch/celebi.h materialyoucolor/quantize/celebi.h
--- patch/celebi.h 2024-01-13 11:47:16.477314056 +0530
+++ materialyoucolor/quantize/celebi.h 2023-12-29 11:08:43.363374804 +0530
@@ -22,8 +22,8 @@
#include <vector>
-#include "cpp/quantize/wsmeans.h"
-#include "cpp/utils/utils.h"
+#include "wsmeans.h"
+#include "utils.h"
namespace material_color_utilities {
diff '--color=auto' -uNr patch/lab.cc materialyoucolor/quantize/lab.cc
--- patch/lab.cc 2024-01-13 11:47:16.030646286 +0530
+++ materialyoucolor/quantize/lab.cc 2023-12-29 11:08:43.363374804 +0530
@@ -14,11 +14,11 @@
* limitations under the License.
*/
-#include "cpp/quantize/lab.h"
+#include "lab.h"
#include <math.h>
-#include "cpp/utils/utils.h"
+#include "utils.h"
namespace material_color_utilities {
diff '--color=auto' -uNr patch/lab.h materialyoucolor/quantize/lab.h
--- patch/lab.h 2024-01-13 11:47:15.627311957 +0530
+++ materialyoucolor/quantize/lab.h 2023-12-29 11:08:43.363374804 +0530
@@ -28,7 +28,7 @@
#include <unordered_set>
#include <vector>
-#include "cpp/utils/utils.h"
+#include "utils.h"
namespace material_color_utilities {
diff '--color=auto' -uNr patch/utils.cc materialyoucolor/quantize/utils.cc
--- patch/utils.cc 2024-01-13 11:47:17.760650558 +0530
+++ materialyoucolor/quantize/utils.cc 2024-01-13 12:49:58.022437349 +0530
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "cpp/utils/utils.h"
+#include "utils.h"
#include <math.h>
@@ -23,8 +23,8 @@
#include <cstdint>
#include <cstdio>
#include <string>
-
-#include "absl/strings/str_cat.h"
+#include <iomanip>
+#include <sstream>
namespace material_color_utilities {
@@ -140,7 +140,7 @@
// Converts a color in ARGB format to a hexadecimal string in lowercase.
//
// For instance: hex_from_argb(0xff012345) == "ff012345"
-std::string HexFromArgb(Argb argb) { return absl::StrCat(absl::Hex(argb)); }
+std::string hex_from_argb(int color) { return std::stringstream() << std::hex << std::setw(8) << std::setfill('0') << color, std::stringstream().str().substr(2); }
Argb IntFromLstar(const double lstar) {
double y = YFromLstar(lstar);
@@ -172,6 +172,8 @@
input.a * matrix[1][0] + input.b * matrix[1][1] + input.c * matrix[1][2];
double c =
input.a * matrix[2][0] + input.b * matrix[2][1] + input.c * matrix[2][2];
- return (Vec3){a, b, c};
+ // Fix build on windows
+ Vec3 __ = {a, b, c};
+ return __;
}
} // namespace material_color_utilities
diff '--color=auto' -uNr patch/wsmeans.cc materialyoucolor/quantize/wsmeans.cc
--- patch/wsmeans.cc 2024-01-13 11:47:15.197310895 +0530
+++ materialyoucolor/quantize/wsmeans.cc 2024-01-09 17:35:33.361759926 +0530
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "cpp/quantize/wsmeans.h"
+#include "wsmeans.h"
#include <algorithm>
#include <cmath>
@@ -27,8 +27,8 @@
#include <unordered_set>
#include <vector>
-#include "absl/container/flat_hash_map.h"
-#include "cpp/quantize/lab.h"
+// #include "absl/container/flat_hash_map.h"
+#include "lab.h"
constexpr int kMaxIterations = 100;
constexpr double kMinDeltaE = 3.0;
@@ -64,7 +64,8 @@
}
uint32_t pixel_count = input_pixels.size();
- absl::flat_hash_map<Argb, int> pixel_to_count;
+ // absl::flat_hash_map<Argb, int> pixel_to_count;
+ std::unordered_map<Argb, int> pixel_to_count;
std::vector<uint32_t> pixels;
pixels.reserve(pixel_count);
std::vector<Lab> points;
@@ -75,7 +76,8 @@
// std::unordered_map 10.2 ms
// absl::btree_map 9.0 ms
// absl::flat_hash_map 8.0 ms
- absl::flat_hash_map<Argb, int>::iterator it = pixel_to_count.find(pixel);
+ // absl::flat_hash_map<Argb, int>::iterator it = pixel_to_count.find(pixel);
+ std::unordered_map<Argb, int>::iterator it = pixel_to_count.find(pixel);
if (it != pixel_to_count.end()) {
it->second++;
diff '--color=auto' -uNr patch/wsmeans.h materialyoucolor/quantize/wsmeans.h
--- patch/wsmeans.h 2024-01-13 11:47:14.800643248 +0530
+++ materialyoucolor/quantize/wsmeans.h 2023-12-29 11:08:43.370041494 +0530
@@ -21,7 +21,7 @@
#include <map>
#include <vector>
-#include "cpp/utils/utils.h"
+#include "utils.h"
namespace material_color_utilities {
diff '--color=auto' -uNr patch/wu.cc materialyoucolor/quantize/wu.cc
--- patch/wu.cc 2024-01-13 11:47:14.367308844 +0530
+++ materialyoucolor/quantize/wu.cc 2023-12-29 11:08:43.370041494 +0530
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "cpp/quantize/wu.h"
+#include "wu.h"
#include <stdlib.h>
@@ -23,7 +23,7 @@
#include <cstdio>
#include <vector>
-#include "cpp/utils/utils.h"
+#include "utils.h"
namespace material_color_utilities {
diff '--color=auto' -uNr patch/wu.h materialyoucolor/quantize/wu.h
--- patch/wu.h 2024-01-13 11:47:13.933974439 +0530
+++ materialyoucolor/quantize/wu.h 2023-12-29 11:08:43.370041494 +0530
@@ -21,7 +21,7 @@
#include <vector>
-#include "cpp/utils/utils.h"
+#include "utils.h"
namespace material_color_utilities {
--- patch/celebi.cc 2024-03-25 23:22:50.123427023 +0530
+++ materialyoucolor/quantize/celebi.cc 2024-03-25 23:20:03.277959473 +0530
@@ -14,47 +14,78 @@
* limitations under the License.
*/
-#include "cpp/quantize/celebi.h"
+#include "celebi.h"
#include <cstddef>
+#include <cstdint>
#include <cstdio>
#include <cstdlib>
+#include <iostream>
#include <vector>
-#include "cpp/quantize/wsmeans.h"
-#include "cpp/quantize/wu.h"
-#include "cpp/utils/utils.h"
-
-namespace material_color_utilities {
-
-QuantizerResult QuantizeCelebi(const std::vector<Argb>& pixels,
- uint16_t max_colors) {
- if (max_colors == 0 || pixels.empty()) {
- return QuantizerResult();
- }
-
+#include "wsmeans.h"
+#include "wu.h"
+#include "utils.h"
+#include "pybind11/pybind11.h"
+#include "pybind11/stl.h"
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+
+namespace python = pybind11;
+
+// std::map<Argb, uint32_t>
+std::map<uint32_t, uint32_t> QuantizeCelebi(const std::vector<std::vector<int>>& pixels,
+ int max_colors) {
if (max_colors > 256) {
max_colors = 256;
}
-
int pixel_count = pixels.size();
- std::vector<Argb> opaque_pixels;
+ std::vector<material_color_utilities::Argb> opaque_pixels;
opaque_pixels.reserve(pixel_count);
for (int i = 0; i < pixel_count; i++) {
- int pixel = pixels[i];
- if (!IsOpaque(pixel)) {
- continue;
- }
+ uint32_t pixel = (pixels[i][0] << 16) |
+ (pixels[i][1] << 8) |
+ (pixels[i][2]);
+ //if (pixels[i].size() > 3 && pixels[i][3] == 255)
opaque_pixels.push_back(pixel);
}
- std::vector<Argb> wu_result = QuantizeWu(opaque_pixels, max_colors);
+ std::vector<material_color_utilities::Argb> wu_result = material_color_utilities::QuantizeWu(
+ opaque_pixels, max_colors);
- QuantizerResult result =
- QuantizeWsmeans(opaque_pixels, wu_result, max_colors);
+ material_color_utilities::QuantizerResult result =
+ material_color_utilities::QuantizeWsmeans(opaque_pixels, wu_result, max_colors);
+
+ return result.color_to_count;
+}
+
- return result;
+std::vector<std::vector<int>> StbLoadImage(const char* image_path) {
+ int width, height, channels;
+ std::vector<std::vector<int>> pixel_array = {};
+ // Load the actual image
+ unsigned char* pixel_result = stbi_load(image_path, &width, &height, &channels, 4);
+ if (!pixel_result) {return pixel_array;}
+
+ pixel_array.reserve(width * height);
+ unsigned char* pixel_position;
+
+ for (int y = 0; y < height; ++y) {
+ for (int x = 0; x < width; ++x) {
+ pixel_position = pixel_result + (x + y * width) * 4;
+ std::vector<int> current_color = {
+ pixel_position[0], pixel_position[1], pixel_position[2]};
+ if (channels > 3) {current_color.push_back(pixel_position[3]);}
+ pixel_array.push_back(current_color);
+ }
+ }
+ stbi_image_free(pixel_result);
+ return pixel_array;
}
-} // namespace material_color_utilities
+PYBIND11_MODULE(celebi, m) {
+ m.doc() = "Functions from cpp backend";
+ m.def("QuantizeCelebi", &QuantizeCelebi, "Get dominant colors");
+ m.def("StbLoadImage", &StbLoadImage, "Get pixel array");
+}