-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
241 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,249 @@ | ||
diff '--color=auto' -uNr patch/celebi.cc materialyoucolor/quantize/celebi.cc | ||
--- patch/celebi.cc 2024-01-13 11:47:16.897315093 +0530 | ||
+++ materialyoucolor/quantize/celebi.cc 2023-12-29 11:13:04.097468166 +0530 | ||
@@ -14,47 +14,53 @@ | ||
* 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> | ||
+ | ||
+namespace python = pybind11; | ||
+ | ||
+// std::map<Argb, uint32_t> | ||
+std::map<uint32_t, uint32_t> QuantizeCelebi(const python::list& 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; | ||
- } | ||
+ python::list rgba_ = python::cast<python::list>(pixels[i]); | ||
+ uint32_t pixel = (python::cast<uint32_t>(rgba_[0]) << 16) | | ||
+ (python::cast<uint32_t>(rgba_[1]) << 8) | | ||
+ python::cast<uint32_t>(rgba_[2]); | ||
opaque_pixels.push_back(pixel); | ||
} | ||
|
||
- std::vector<Argb> wu_result = QuantizeWu(opaque_pixels, max_colors); | ||
- | ||
- QuantizerResult result = | ||
- QuantizeWsmeans(opaque_pixels, wu_result, max_colors); | ||
+ std::vector<material_color_utilities::Argb> wu_result = material_color_utilities::QuantizeWu(opaque_pixels, max_colors); | ||
|
||
- return result; | ||
+ material_color_utilities::QuantizerResult result = | ||
+ material_color_utilities::QuantizeWsmeans(opaque_pixels, wu_result, max_colors); | ||
+ | ||
+ return result.color_to_count; | ||
} | ||
|
||
-} // namespace material_color_utilities | ||
+ | ||
+ | ||
+PYBIND11_MODULE(celebi, m) { | ||
+ m.doc() = "QuantizeCelebi from cpp"; | ||
+ m.def("QuantizeCelebi", &QuantizeCelebi, "Get dominant colors"); | ||
+} | ||
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/__init__.py materialyoucolor/quantize/__init__.py | ||
--- patch/__init__.py 1970-01-01 05:30:00.000000000 +0530 | ||
+++ materialyoucolor/quantize/__init__.py 2023-12-29 15:47:38.879196659 +0530 | ||
@@ -0,0 +1 @@ | ||
+from .celebi import QuantizeCelebi | ||
Binary files patch/__pycache__/__init__.cpython-311.pyc and materialyoucolor/quantize/__pycache__/__init__.cpython-311.pyc differ | ||
diff '--color=auto' -uNr patch/test.py materialyoucolor/quantize/test.py | ||
--- patch/test.py 2024-01-09 17:45:53.672847982 +0530 | ||
+++ materialyoucolor/quantize/test.py 1970-01-01 05:30:00.000000000 +0530 | ||
@@ -1,29 +0,0 @@ | ||
-import requests | ||
-import os | ||
- | ||
-FOLDER = "." | ||
-PATCH_FILE = "quantizer_cpp.patch" | ||
-COMMIT = "1217346b9416e6e55c83c6e9295f6aed001e852e" | ||
-URL = ( | ||
- "https://raw.githubusercontent.com/material-foundation/" | ||
- "material-color-utilities/{}/cpp/".format(COMMIT) | ||
-) | ||
-FILES = list( | ||
- "quantize/" + __ | ||
- for __ in [ | ||
- "wu.h", | ||
- "wu.cc", | ||
- "wsmeans.h", | ||
- "wsmeans.cc", | ||
- "lab.h", | ||
- "lab.cc", | ||
- "celebi.h", | ||
- "celebi.cc", | ||
- ] | ||
-) + ["utils/utils.h", "utils/utils.cc"] | ||
-for file in FILES: | ||
- with open(os.path.join(FOLDER, os.path.basename(file)), "w") as write_buffer: | ||
- write_buffer.write(requests.get(URL + file).text) | ||
- write_buffer.close() | ||
- print("[Downloaded] : " + file) | ||
- | ||
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-09 17:49:21.391928858 +0530 | ||
--- patch/utils.cc 2024-01-13 11:47:17.760650558 +0530 | ||
+++ materialyoucolor/quantize/utils.cc 2024-01-12 20:50:51.270088994 +0530 | ||
@@ -25,7 +25,6 @@ | ||
@@ -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 <iomanip> | ||
#include <sstream> | ||
- | ||
-#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); | ||
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 { | ||
|