Skip to content

Commit 6822f51

Browse files
committed
new build system
1 parent dfeaa7b commit 6822f51

20 files changed

Lines changed: 9404 additions & 930 deletions

.github/workflows/cibuildwheel.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ jobs:
3434
CIBW_BEFORE_TEST: >
3535
pip install -r {project}/requirements.txt -r {project}/requirements-dev.txt
3636
CIBW_TEST_COMMAND: >
37-
curl -L -o test_image.jpg "https://raw.githubusercontent.com/mohammadimtiazz/standard-test-images-for-Image-Processing/refs/heads/master/standard_test_images/tulips.png" &&
37+
python -c "from PIL import Image; Image.new('RGB', (128, 128), (70, 120, 180)).save('test_image.jpg')" &&
3838
python {project}/tests/test_all.py --image test_image.jpg --quality 1 --method pillow --tonal-spot &&
3939
python {project}/tests/test_all.py --image test_image.jpg --quality 1 --method cpp --tonal-spot
4040
CIBW_BEFORE_BUILD: >
41-
pip install setuptools wheel
42-
CIBW_ENVIRONMENT: >
43-
PURE_PYTHON=False
41+
pip install -U pip setuptools wheel pybind11
4442
4543
- uses: actions/upload-artifact@v4
4644
with:

.gitignore

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
build
2-
patch
3-
dist
4-
*.egg*
5-
materialyoucolor/quantize
6-
__pycache__
1+
build/
2+
dist/
3+
*.egg*
4+
*.whl
5+
*.so
6+
*.pyd
7+
8+
__pycache__/
79
*.pyc
10+
811
image.jpg
12+
test_image.jpg
913
tests/compare*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Thanks :heart: to [@midn8hustlr](https://github.com/midn8hustlr) for this [AUR p
8484

8585
Ensure these lines in `buildozer.spec`:
8686
```python
87-
requirements = materialyoucolor==3.0.0
87+
requirements = materialyoucolor==3.0.2
8888
p4a.branch = develop
8989
```
9090

materialyoucolor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.1"
1+
__version__ = "3.0.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .celebi import QuantizeCelebi, ImageQuantizeCelebi
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "celebi.h"
18+
19+
#include <cstddef>
20+
#include <cstdint>
21+
#include <cstdio>
22+
#include <cstdlib>
23+
#include <iostream>
24+
#include <vector>
25+
26+
#include "wsmeans.h"
27+
#include "wu.h"
28+
#include "utils.h"
29+
#include "pybind11/pybind11.h"
30+
#include "pybind11/stl.h"
31+
#define STB_IMAGE_IMPLEMENTATION
32+
#include "stb_image.h"
33+
34+
namespace python = pybind11;
35+
36+
// std::map<Argb, uint32_t>
37+
std::map<uint32_t, uint32_t> QuantizeCelebi(const std::vector<std::vector<int>>& pixels,
38+
int max_colors) {
39+
if (max_colors > 256) {
40+
max_colors = 256;
41+
}
42+
int pixel_count = pixels.size();
43+
44+
std::vector<material_color_utilities::Argb> opaque_pixels;
45+
opaque_pixels.reserve(pixel_count);
46+
for (int i = 0; i < pixel_count; i++) {
47+
uint32_t pixel = (pixels[i][0] << 16) |
48+
(pixels[i][1] << 8) |
49+
(pixels[i][2]);
50+
//if (pixels[i].size() > 3 && pixels[i][3] == 255)
51+
opaque_pixels.push_back(pixel);
52+
}
53+
54+
std::vector<material_color_utilities::Argb> wu_result = material_color_utilities::QuantizeWu(
55+
opaque_pixels, max_colors);
56+
57+
material_color_utilities::QuantizerResult result =
58+
material_color_utilities::QuantizeWsmeans(opaque_pixels, wu_result, max_colors);
59+
60+
return result.color_to_count;
61+
}
62+
63+
64+
std::map<uint32_t, uint32_t> ImageQuantizeCelebi(const char* image_path, const int quality, int max_colors) {
65+
int width, height, channels;
66+
std::vector<std::vector<int>> pixel_array = {};
67+
unsigned char* pixel_result = stbi_load(image_path, &width, &height, &channels, 4);
68+
if (!pixel_result) {return QuantizeCelebi(pixel_array, max_colors);}
69+
pixel_array.reserve( (width * height) / quality );
70+
unsigned char* pixel_position;
71+
int _quality = quality;
72+
for (int y = 0; y < height; ++y) {
73+
for (int x = 0; x < width; x = x+_quality) {
74+
pixel_position = pixel_result + (x + y * width) * 4;
75+
std::vector<int> current_color = {
76+
pixel_position[0], pixel_position[1], pixel_position[2]};
77+
if (channels > 3) {current_color.push_back(pixel_position[3]);}
78+
pixel_array.push_back(current_color);
79+
if (_quality < quality) {_quality = quality;}
80+
}
81+
if (y % 2 == 0) {
82+
_quality = quality / 2;
83+
} else {_quality = quality;}
84+
}
85+
stbi_image_free(pixel_result);
86+
return QuantizeCelebi(pixel_array, max_colors);
87+
}
88+
89+
90+
PYBIND11_MODULE(celebi, m) {
91+
m.doc() = "Functions from cpp backend";
92+
m.def("QuantizeCelebi", &QuantizeCelebi, "Get dominant colors");
93+
m.def("ImageQuantizeCelebi", &ImageQuantizeCelebi, "Get pixel array");
94+
}

materialyoucolor/quantize/celebi.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CPP_QUANTIZE_CELEBI_H_
18+
#define CPP_QUANTIZE_CELEBI_H_
19+
20+
#include <stdint.h>
21+
#include <stdlib.h>
22+
23+
#include <vector>
24+
25+
#include "wsmeans.h"
26+
#include "utils.h"
27+
28+
namespace material_color_utilities {
29+
30+
QuantizerResult QuantizeCelebi(const std::vector<Argb>& pixels,
31+
uint16_t max_colors);
32+
33+
} // namespace material_color_utilities
34+
35+
#endif // CPP_QUANTIZE_CELEBI_H_

materialyoucolor/quantize/lab.cc

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "lab.h"
18+
19+
#include <math.h>
20+
21+
#include "utils.h"
22+
23+
namespace material_color_utilities {
24+
25+
Argb IntFromLab(const Lab lab) {
26+
double e = 216.0 / 24389.0;
27+
double kappa = 24389.0 / 27.0;
28+
double ke = 8.0;
29+
30+
double fy = (lab.l + 16.0) / 116.0;
31+
double fx = (lab.a / 500.0) + fy;
32+
double fz = fy - (lab.b / 200.0);
33+
double fx3 = fx * fx * fx;
34+
double x_normalized = (fx3 > e) ? fx3 : (116.0 * fx - 16.0) / kappa;
35+
double y_normalized = (lab.l > ke) ? fy * fy * fy : (lab.l / kappa);
36+
double fz3 = fz * fz * fz;
37+
double z_normalized = (fz3 > e) ? fz3 : (116.0 * fz - 16.0) / kappa;
38+
double x = x_normalized * kWhitePointD65[0];
39+
double y = y_normalized * kWhitePointD65[1];
40+
double z = z_normalized * kWhitePointD65[2];
41+
42+
// intFromXyz
43+
double rL = 3.2406 * x - 1.5372 * y - 0.4986 * z;
44+
double gL = -0.9689 * x + 1.8758 * y + 0.0415 * z;
45+
double bL = 0.0557 * x - 0.2040 * y + 1.0570 * z;
46+
47+
int red = Delinearized(rL);
48+
int green = Delinearized(gL);
49+
int blue = Delinearized(bL);
50+
51+
return ArgbFromRgb(red, green, blue);
52+
}
53+
54+
Lab LabFromInt(const Argb argb) {
55+
int red = (argb & 0x00ff0000) >> 16;
56+
int green = (argb & 0x0000ff00) >> 8;
57+
int blue = (argb & 0x000000ff);
58+
double red_l = Linearized(red);
59+
double green_l = Linearized(green);
60+
double blue_l = Linearized(blue);
61+
double x = 0.41233895 * red_l + 0.35762064 * green_l + 0.18051042 * blue_l;
62+
double y = 0.2126 * red_l + 0.7152 * green_l + 0.0722 * blue_l;
63+
double z = 0.01932141 * red_l + 0.11916382 * green_l + 0.95034478 * blue_l;
64+
double y_normalized = y / kWhitePointD65[1];
65+
double e = 216.0 / 24389.0;
66+
double kappa = 24389.0 / 27.0;
67+
double fy;
68+
if (y_normalized > e) {
69+
fy = pow(y_normalized, 1.0 / 3.0);
70+
} else {
71+
fy = (kappa * y_normalized + 16) / 116;
72+
}
73+
74+
double x_normalized = x / kWhitePointD65[0];
75+
double fx;
76+
if (x_normalized > e) {
77+
fx = pow(x_normalized, 1.0 / 3.0);
78+
} else {
79+
fx = (kappa * x_normalized + 16) / 116;
80+
}
81+
82+
double z_normalized = z / kWhitePointD65[2];
83+
double fz;
84+
if (z_normalized > e) {
85+
fz = pow(z_normalized, 1.0 / 3.0);
86+
} else {
87+
fz = (kappa * z_normalized + 16) / 116;
88+
}
89+
90+
double l = 116.0 * fy - 16;
91+
double a = 500.0 * (fx - fy);
92+
double b = 200.0 * (fy - fz);
93+
return {l, a, b};
94+
}
95+
96+
} // namespace material_color_utilities

materialyoucolor/quantize/lab.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CPP_QUANTIZE_LAB_H_
18+
#define CPP_QUANTIZE_LAB_H_
19+
20+
#include <algorithm>
21+
#include <cmath>
22+
#include <cstdint>
23+
#include <cstdlib>
24+
#include <map>
25+
#include <set>
26+
#include <string>
27+
#include <unordered_map>
28+
#include <unordered_set>
29+
#include <vector>
30+
31+
#include "utils.h"
32+
33+
namespace material_color_utilities {
34+
35+
struct Lab {
36+
double l = 0.0;
37+
double a = 0.0;
38+
double b = 0.0;
39+
40+
double DeltaE(const Lab& lab) {
41+
double d_l = l - lab.l;
42+
double d_a = a - lab.a;
43+
double d_b = b - lab.b;
44+
return (d_l * d_l) + (d_a * d_a) + (d_b * d_b);
45+
}
46+
47+
std::string ToString() {
48+
return "Lab: L* " + std::to_string(l) + " a* " + std::to_string(a) +
49+
" b* " + std::to_string(b);
50+
}
51+
};
52+
53+
Argb IntFromLab(const Lab lab);
54+
Lab LabFromInt(const Argb argb);
55+
56+
} // namespace material_color_utilities
57+
#endif // CPP_QUANTIZE_LAB_H_

0 commit comments

Comments
 (0)