Skip to content

Commit 147ea14

Browse files
committed
quantize: optimize c++ color method
1 parent 121b1cb commit 147ea14

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

quantizer_cpp.patch

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ diff '--color=auto' -uNr patch/wu.h materialyoucolor/quantize/wu.h
171171

172172
namespace material_color_utilities {
173173

174-
--- patch/celebi.cc 2024-03-25 23:22:50.123427023 +0530
175-
+++ materialyoucolor/quantize/celebi.cc 2024-03-25 23:20:03.277959473 +0530
176-
@@ -14,47 +14,78 @@
174+
--- materialyoucolor/quantize/celebi.cc 2025-01-09 12:10:34.733592839 +0530
175+
+++ patch/celebi.cc 2025-01-09 11:57:06.090837655 +0530
176+
@@ -14,47 +14,81 @@
177177
* limitations under the License.
178178
*/
179179

@@ -244,35 +244,38 @@ diff '--color=auto' -uNr patch/wu.h materialyoucolor/quantize/wu.h
244244
+
245245
+ return result.color_to_count;
246246
+}
247-
+
248247

249248
- return result;
250-
+std::vector<std::vector<int>> StbLoadImage(const char* image_path) {
249+
+
250+
+std::map<uint32_t, uint32_t> ImageQuantizeCelebi(const char* image_path, const int quality, int max_colors) {
251251
+ int width, height, channels;
252252
+ std::vector<std::vector<int>> pixel_array = {};
253-
+ // Load the actual image
254253
+ unsigned char* pixel_result = stbi_load(image_path, &width, &height, &channels, 4);
255-
+ if (!pixel_result) {return pixel_array;}
256-
+
257-
+ pixel_array.reserve(width * height);
254+
+ if (!pixel_result) {return QuantizeCelebi(pixel_array, max_colors);}
255+
+ pixel_array.reserve( (width * height) / quality );
258256
+ unsigned char* pixel_position;
259-
+
257+
+ int _quality = quality;
260258
+ for (int y = 0; y < height; ++y) {
261-
+ for (int x = 0; x < width; ++x) {
259+
+ for (int x = 0; x < width; x = x+_quality) {
262260
+ pixel_position = pixel_result + (x + y * width) * 4;
263261
+ std::vector<int> current_color = {
264262
+ pixel_position[0], pixel_position[1], pixel_position[2]};
265263
+ if (channels > 3) {current_color.push_back(pixel_position[3]);}
266264
+ pixel_array.push_back(current_color);
265+
+ if (_quality < quality) {_quality = quality;}
267266
+ }
267+
+ if (y % 2 == 0) {
268+
+ _quality = quality / 2;
269+
+ } else {_quality = quality;}
268270
+ }
269271
+ stbi_image_free(pixel_result);
270-
+ return pixel_array;
272+
+ return QuantizeCelebi(pixel_array, max_colors);
271273
}
272274

273275
-} // namespace material_color_utilities
276+
+
274277
+PYBIND11_MODULE(celebi, m) {
275278
+ m.doc() = "Functions from cpp backend";
276279
+ m.def("QuantizeCelebi", &QuantizeCelebi, "Get dominant colors");
277-
+ m.def("StbLoadImage", &StbLoadImage, "Get pixel array");
280+
+ m.def("ImageQuantizeCelebi", &ImageQuantizeCelebi, "Get pixel array");
278281
+}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ def download_files(base_url, folder, file_map):
635635

636636
# write __init__.py
637637
with open(os.path.join(MCU_FOLDER, "__init__.py"), "w") as file:
638-
file.write("from .celebi import QuantizeCelebi, StbLoadImage")
638+
file.write("from .celebi import QuantizeCelebi, ImageQuantizeCelebi")
639639
file.close()
640640

641641
if should_apply:

tests/test_all.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import os
44
import requests
55
from timeit import default_timer
6+
import gc
67
import sys
8+
import time
79
from materialyoucolor.utils.color_utils import rgba_from_argb
8-
from materialyoucolor.quantize import QuantizeCelebi, StbLoadImage
10+
from materialyoucolor.quantize import QuantizeCelebi, ImageQuantizeCelebi
911
from materialyoucolor.score.score import Score
1012
from materialyoucolor.hct import Hct
1113
from materialyoucolor.dynamiccolor.material_dynamic_colors import MaterialDynamicColors
@@ -39,27 +41,28 @@
3941
print("Downloaded: ", FILENAME, os.path.exists(FILENAME))
4042

4143
console = Console()
44+
quality = int(sys.argv[2])
4245

46+
########### PILLOW METHOD #############
4347
start = default_timer()
4448
image = Image.open(FILENAME)
45-
4649
pixel_len = image.width * image.height
4750
image_data = image.getdata()
48-
pixel_array = [image_data[_] for _ in range(0, pixel_len, int(sys.argv[2]))]
49-
end = default_timer()
50-
print("File open took [pillow]: ", end - start, "secs")
51-
start = default_timer()
52-
pixel_array = StbLoadImage(FILENAME)
51+
# start = default_timer()
52+
colors = QuantizeCelebi([image_data[i] for i in range(0, pixel_len, quality)], MAX_COLOR)
5353
end = default_timer()
54-
print("File open took [stb_image]: ", end - start, "secs")
54+
print(f"Color[pillow] generation took {end-start:.4f} secs")
55+
##############################
5556

57+
########## C++ Method ##########
5658
start = default_timer()
57-
colors = QuantizeCelebi(pixel_array, MAX_COLOR)
58-
selected = Score.score(colors)
59+
# loading using c++ method
60+
colors = ImageQuantizeCelebi(FILENAME, quality, MAX_COLOR)
5961
end = default_timer()
62+
print(f"Color[stb_image] generation took {end-start:.4f} secs")
63+
######################
6064

61-
print("Color generation took : ", end - start, "secs\n")
62-
65+
selected = Score.score(colors)
6366

6467
if os.name == "nt":
6568
# UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to <undefined>
@@ -91,6 +94,8 @@
9194
)
9295
console.print(st)
9396

97+
exit()
98+
9499
def print_scheme(scheme_function, name):
95100
print()
96101
schemes = [scheme_function(rgb) for rgb in selected]

0 commit comments

Comments
 (0)