normal-grain-merge 0.0.0__tar.gz → 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of normal-grain-merge might be problematic. Click here for more details.
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/PKG-INFO +102 -78
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/README.md +24 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge/normal_grain_merge.pyi +28 -28
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/PKG-INFO +102 -78
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/pyproject.toml +31 -31
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/setup.cfg +4 -4
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/setup.py +8 -4
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/LICENSE +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge/__init__.py +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge/kernel_kind.py +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge/normal_grain_merge.c +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/SOURCES.txt +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/dependency_links.txt +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/requires.txt +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/top_level.txt +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/tests/test_ngm.py +0 -0
- {normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/tests/test_speed.py +0 -0
|
@@ -1,78 +1,102 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: normal_grain_merge
|
|
3
|
-
Version: 0.0.
|
|
4
|
-
Summary: Fused normal and grain merge C extension
|
|
5
|
-
Author: Samuel Howard
|
|
6
|
-
License
|
|
7
|
-
Project-URL: Homepage, https://github.com/samhaswon/normal_grain_merge
|
|
8
|
-
Project-URL: Bug Tracker, https://github.com/samhaswon/normal_grain_merge/issues
|
|
9
|
-
Keywords: image,processing
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: C
|
|
12
|
-
Classifier: Operating System :: OS Independent
|
|
13
|
-
Requires-Python: >=3.
|
|
14
|
-
Description-Content-Type: text/markdown
|
|
15
|
-
License-File: LICENSE
|
|
16
|
-
Requires-Dist: numpy<2.3.0
|
|
17
|
-
Dynamic: license-file
|
|
18
|
-
|
|
19
|
-
# normal_grain_merge
|
|
20
|
-
|
|
21
|
-
This implements a combined version of the blend modes normal and grain merge.
|
|
22
|
-
Grain merge is performed on *s* and *t* with the result normal-merged with *b*.
|
|
23
|
-
Subscripts indicate channels, with alpha (α) channels broadcast to three channels.
|
|
24
|
-
|
|
25
|
-
$$
|
|
26
|
-
(((\mathrm{t_{rgb}} + \mathrm{s_{rgb}} - 0.5) * \mathrm{t_\alpha} + \mathrm{t_{rgb}} * (1 - \mathrm{t_\alpha})) * (1 - 0.3) + \mathrm{s_{rgb}} * 0.3) * \mathrm{t_\alpha} + \mathrm{b_{rgb}} * (1 - \mathrm{t_\alpha})
|
|
27
|
-
$$
|
|
28
|
-
|
|
29
|
-
## Usage
|
|
30
|
-
```py
|
|
31
|
-
import numpy as np
|
|
32
|
-
from normal_grain_merge import normal_grain_merge, KernelKind
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
# Example arrays
|
|
36
|
-
base = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
37
|
-
texture = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
38
|
-
skin = np.zeros((100, 100, 4), dtype=np.uint8)
|
|
39
|
-
im_alpha = np.zeros((100, 100), dtype=np.uint8)
|
|
40
|
-
|
|
41
|
-
result_scalar = normal_grain_merge(base, texture, skin, im_alpha, KernelKind.KERNEL_SCALAR.value)
|
|
42
|
-
print(result_scalar.shape, result_scalar.dtype)
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
There are three kernels implemented in this module as defined in `KernelKind`.
|
|
46
|
-
|
|
47
|
-
- `KERNEL_AUTO`: Automatically chooses the kernel, preferring AVX2
|
|
48
|
-
- `KERNEL_SCALAR`: Portable scalar implementation.
|
|
49
|
-
- `KERNEL_SSE42`: SSE4.2 intrinsics kernel. Likely better on AMD CPUs.
|
|
50
|
-
- `KERNEL_AVX2`: AVX2 intrinsics kernel. Likely better on Intel CPUs.
|
|
51
|
-
|
|
52
|
-
### Parameters
|
|
53
|
-
|
|
54
|
-
All input matrices should have the same height and width.
|
|
55
|
-
|
|
56
|
-
#### `base`
|
|
57
|
-
|
|
58
|
-
RGB or RGBA, dropping the alpha channel if it exists.
|
|
59
|
-
The base image for application.
|
|
60
|
-
|
|
61
|
-
#### `texture`
|
|
62
|
-
|
|
63
|
-
RGB or RGBA, applying the alpha if it exists.
|
|
64
|
-
This is the texture to be applied.
|
|
65
|
-
|
|
66
|
-
#### `skin`
|
|
67
|
-
|
|
68
|
-
RGBA, the segmented portion of base to texture.
|
|
69
|
-
The "skin" of the object the texture is to be applied to.
|
|
70
|
-
|
|
71
|
-
#### `im_alpha`
|
|
72
|
-
|
|
73
|
-
The alpha of parameter `skin`.
|
|
74
|
-
This is mostly a holdover from the Python implementation to deal with NumPy.
|
|
75
|
-
|
|
76
|
-
#### `kernel`
|
|
77
|
-
|
|
78
|
-
One of `KernelKind`.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: normal_grain_merge
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Fused normal and grain merge C extension
|
|
5
|
+
Author: Samuel Howard
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/samhaswon/normal_grain_merge
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/samhaswon/normal_grain_merge/issues
|
|
9
|
+
Keywords: image,processing
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: C
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: numpy<2.3.0
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# normal_grain_merge
|
|
20
|
+
|
|
21
|
+
This implements a combined version of the blend modes normal and grain merge.
|
|
22
|
+
Grain merge is performed on *s* and *t* with the result normal-merged with *b*.
|
|
23
|
+
Subscripts indicate channels, with alpha (α) channels broadcast to three channels.
|
|
24
|
+
|
|
25
|
+
$$
|
|
26
|
+
(((\mathrm{t_{rgb}} + \mathrm{s_{rgb}} - 0.5) * \mathrm{t_\alpha} + \mathrm{t_{rgb}} * (1 - \mathrm{t_\alpha})) * (1 - 0.3) + \mathrm{s_{rgb}} * 0.3) * \mathrm{t_\alpha} + \mathrm{b_{rgb}} * (1 - \mathrm{t_\alpha})
|
|
27
|
+
$$
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
```py
|
|
31
|
+
import numpy as np
|
|
32
|
+
from normal_grain_merge import normal_grain_merge, KernelKind
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# Example arrays
|
|
36
|
+
base = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
37
|
+
texture = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
38
|
+
skin = np.zeros((100, 100, 4), dtype=np.uint8)
|
|
39
|
+
im_alpha = np.zeros((100, 100), dtype=np.uint8)
|
|
40
|
+
|
|
41
|
+
result_scalar = normal_grain_merge(base, texture, skin, im_alpha, KernelKind.KERNEL_SCALAR.value)
|
|
42
|
+
print(result_scalar.shape, result_scalar.dtype)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
There are three kernels implemented in this module as defined in `KernelKind`.
|
|
46
|
+
|
|
47
|
+
- `KERNEL_AUTO`: Automatically chooses the kernel, preferring AVX2
|
|
48
|
+
- `KERNEL_SCALAR`: Portable scalar implementation.
|
|
49
|
+
- `KERNEL_SSE42`: SSE4.2 intrinsics kernel. Likely better on AMD CPUs.
|
|
50
|
+
- `KERNEL_AVX2`: AVX2 intrinsics kernel. Likely better on Intel CPUs.
|
|
51
|
+
|
|
52
|
+
### Parameters
|
|
53
|
+
|
|
54
|
+
All input matrices should have the same height and width.
|
|
55
|
+
|
|
56
|
+
#### `base`
|
|
57
|
+
|
|
58
|
+
RGB or RGBA, dropping the alpha channel if it exists.
|
|
59
|
+
The base image for application.
|
|
60
|
+
|
|
61
|
+
#### `texture`
|
|
62
|
+
|
|
63
|
+
RGB or RGBA, applying the alpha if it exists.
|
|
64
|
+
This is the texture to be applied.
|
|
65
|
+
|
|
66
|
+
#### `skin`
|
|
67
|
+
|
|
68
|
+
RGBA, the segmented portion of base to texture.
|
|
69
|
+
The "skin" of the object the texture is to be applied to.
|
|
70
|
+
|
|
71
|
+
#### `im_alpha`
|
|
72
|
+
|
|
73
|
+
The alpha of parameter `skin`.
|
|
74
|
+
This is mostly a holdover from the Python implementation to deal with NumPy.
|
|
75
|
+
|
|
76
|
+
#### `kernel`
|
|
77
|
+
|
|
78
|
+
One of `KernelKind`.
|
|
79
|
+
|
|
80
|
+
## Performance
|
|
81
|
+
|
|
82
|
+
The entire reason for me writing this was NumPy being slow when this operation is in the hot path.
|
|
83
|
+
So, I decided to write a SIMD version that does the type casting outside NumPy with only the intermediate values being in FP32.
|
|
84
|
+
|
|
85
|
+
How much of a speedup is this? All numbers are from a Ryzen 7 4800H running Windows 11 and Python 3.12.4.
|
|
86
|
+
|
|
87
|
+
| Method/Kernel | Average Iteration Time |
|
|
88
|
+
|-------------------|------------------------|
|
|
89
|
+
| C scalar kernel | 0.019565s |
|
|
90
|
+
| C SSE4.2 kernel | 0.013705s |
|
|
91
|
+
| C AVX2 kernel | 0.016842s |
|
|
92
|
+
| NumPy version | 0.228098s |
|
|
93
|
+
| Old NumPy version | 0.350554s |
|
|
94
|
+
|
|
95
|
+
| Method Comparison | Speedup |
|
|
96
|
+
|--------------------|----------|
|
|
97
|
+
| NumPy -> scalar | 91.4227% |
|
|
98
|
+
| NumPy -> SSE4.2 | 93.9915% |
|
|
99
|
+
| NumPy -> AVX2 | 92.6165% |
|
|
100
|
+
| Old np -> SSE4.2 | 96.0904% |
|
|
101
|
+
| C scalar -> SSE4.2 | 29.9487% |
|
|
102
|
+
| C scalar -> AVX2 | 13.9183% |
|
|
@@ -58,3 +58,27 @@ This is mostly a holdover from the Python implementation to deal with NumPy.
|
|
|
58
58
|
#### `kernel`
|
|
59
59
|
|
|
60
60
|
One of `KernelKind`.
|
|
61
|
+
|
|
62
|
+
## Performance
|
|
63
|
+
|
|
64
|
+
The entire reason for me writing this was NumPy being slow when this operation is in the hot path.
|
|
65
|
+
So, I decided to write a SIMD version that does the type casting outside NumPy with only the intermediate values being in FP32.
|
|
66
|
+
|
|
67
|
+
How much of a speedup is this? All numbers are from a Ryzen 7 4800H running Windows 11 and Python 3.12.4.
|
|
68
|
+
|
|
69
|
+
| Method/Kernel | Average Iteration Time |
|
|
70
|
+
|-------------------|------------------------|
|
|
71
|
+
| C scalar kernel | 0.019565s |
|
|
72
|
+
| C SSE4.2 kernel | 0.013705s |
|
|
73
|
+
| C AVX2 kernel | 0.016842s |
|
|
74
|
+
| NumPy version | 0.228098s |
|
|
75
|
+
| Old NumPy version | 0.350554s |
|
|
76
|
+
|
|
77
|
+
| Method Comparison | Speedup |
|
|
78
|
+
|--------------------|----------|
|
|
79
|
+
| NumPy -> scalar | 91.4227% |
|
|
80
|
+
| NumPy -> SSE4.2 | 93.9915% |
|
|
81
|
+
| NumPy -> AVX2 | 92.6165% |
|
|
82
|
+
| Old np -> SSE4.2 | 96.0904% |
|
|
83
|
+
| C scalar -> SSE4.2 | 29.9487% |
|
|
84
|
+
| C scalar -> AVX2 | 13.9183% |
|
{normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge/normal_grain_merge.pyi
RENAMED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
# ngm/normal_grain_merge.pyi
|
|
2
|
-
from typing import Literal
|
|
3
|
-
import numpy as np
|
|
4
|
-
from .kernel_kind import KernelKind
|
|
5
|
-
|
|
6
|
-
def normal_grain_merge(
|
|
7
|
-
base: np.ndarray,
|
|
8
|
-
texture: np.ndarray,
|
|
9
|
-
skin: np.ndarray,
|
|
10
|
-
im_alpha: np.ndarray,
|
|
11
|
-
kernel: Literal["auto", "scalar", "sse42", "avx2"] | KernelKind = "auto",
|
|
12
|
-
) -> np.ndarray:
|
|
13
|
-
"""
|
|
14
|
-
Performs a combined merge: grain merge of skin and texture,
|
|
15
|
-
then a normal merge of that result on base.
|
|
16
|
-
Channel ordering doesn't matter as long as it is consistent.
|
|
17
|
-
:param base: The base RGB image.
|
|
18
|
-
:param texture: The texture, either RGB or RGBA.
|
|
19
|
-
:param skin: The RGBA skin cutout.
|
|
20
|
-
:param im_alpha: The alpha from the cutout.
|
|
21
|
-
:param kernel: Which kernel to use.
|
|
22
|
-
The `auto` kernel chooses between avx2 and sse4.2 when compiled with gcc and uses `scaler` on Windows.
|
|
23
|
-
The `scalar` kernel is a portable implementation that relies on the compiler for SIMD.
|
|
24
|
-
The `sse42` kernel uses SSE4.2 intrinsics.
|
|
25
|
-
The `avx2` kernel uses AVX2 intrinsics.
|
|
26
|
-
:return: RGB np.ndarray.
|
|
27
|
-
"""
|
|
28
|
-
...
|
|
1
|
+
# ngm/normal_grain_merge.pyi
|
|
2
|
+
from typing import Literal
|
|
3
|
+
import numpy as np
|
|
4
|
+
from .kernel_kind import KernelKind
|
|
5
|
+
|
|
6
|
+
def normal_grain_merge(
|
|
7
|
+
base: np.ndarray,
|
|
8
|
+
texture: np.ndarray,
|
|
9
|
+
skin: np.ndarray,
|
|
10
|
+
im_alpha: np.ndarray,
|
|
11
|
+
kernel: Literal["auto", "scalar", "sse42", "avx2"] | KernelKind = "auto",
|
|
12
|
+
) -> np.ndarray:
|
|
13
|
+
"""
|
|
14
|
+
Performs a combined merge: grain merge of skin and texture,
|
|
15
|
+
then a normal merge of that result on base.
|
|
16
|
+
Channel ordering doesn't matter as long as it is consistent.
|
|
17
|
+
:param base: The base RGB image.
|
|
18
|
+
:param texture: The texture, either RGB or RGBA.
|
|
19
|
+
:param skin: The RGBA skin cutout.
|
|
20
|
+
:param im_alpha: The alpha from the cutout.
|
|
21
|
+
:param kernel: Which kernel to use.
|
|
22
|
+
The `auto` kernel chooses between avx2 and sse4.2 when compiled with gcc and uses `scaler` on Windows.
|
|
23
|
+
The `scalar` kernel is a portable implementation that relies on the compiler for SIMD.
|
|
24
|
+
The `sse42` kernel uses SSE4.2 intrinsics.
|
|
25
|
+
The `avx2` kernel uses AVX2 intrinsics.
|
|
26
|
+
:return: RGB np.ndarray.
|
|
27
|
+
"""
|
|
28
|
+
...
|
|
@@ -1,78 +1,102 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: normal_grain_merge
|
|
3
|
-
Version: 0.0.
|
|
4
|
-
Summary: Fused normal and grain merge C extension
|
|
5
|
-
Author: Samuel Howard
|
|
6
|
-
License
|
|
7
|
-
Project-URL: Homepage, https://github.com/samhaswon/normal_grain_merge
|
|
8
|
-
Project-URL: Bug Tracker, https://github.com/samhaswon/normal_grain_merge/issues
|
|
9
|
-
Keywords: image,processing
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: C
|
|
12
|
-
Classifier: Operating System :: OS Independent
|
|
13
|
-
Requires-Python: >=3.
|
|
14
|
-
Description-Content-Type: text/markdown
|
|
15
|
-
License-File: LICENSE
|
|
16
|
-
Requires-Dist: numpy<2.3.0
|
|
17
|
-
Dynamic: license-file
|
|
18
|
-
|
|
19
|
-
# normal_grain_merge
|
|
20
|
-
|
|
21
|
-
This implements a combined version of the blend modes normal and grain merge.
|
|
22
|
-
Grain merge is performed on *s* and *t* with the result normal-merged with *b*.
|
|
23
|
-
Subscripts indicate channels, with alpha (α) channels broadcast to three channels.
|
|
24
|
-
|
|
25
|
-
$$
|
|
26
|
-
(((\mathrm{t_{rgb}} + \mathrm{s_{rgb}} - 0.5) * \mathrm{t_\alpha} + \mathrm{t_{rgb}} * (1 - \mathrm{t_\alpha})) * (1 - 0.3) + \mathrm{s_{rgb}} * 0.3) * \mathrm{t_\alpha} + \mathrm{b_{rgb}} * (1 - \mathrm{t_\alpha})
|
|
27
|
-
$$
|
|
28
|
-
|
|
29
|
-
## Usage
|
|
30
|
-
```py
|
|
31
|
-
import numpy as np
|
|
32
|
-
from normal_grain_merge import normal_grain_merge, KernelKind
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
# Example arrays
|
|
36
|
-
base = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
37
|
-
texture = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
38
|
-
skin = np.zeros((100, 100, 4), dtype=np.uint8)
|
|
39
|
-
im_alpha = np.zeros((100, 100), dtype=np.uint8)
|
|
40
|
-
|
|
41
|
-
result_scalar = normal_grain_merge(base, texture, skin, im_alpha, KernelKind.KERNEL_SCALAR.value)
|
|
42
|
-
print(result_scalar.shape, result_scalar.dtype)
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
There are three kernels implemented in this module as defined in `KernelKind`.
|
|
46
|
-
|
|
47
|
-
- `KERNEL_AUTO`: Automatically chooses the kernel, preferring AVX2
|
|
48
|
-
- `KERNEL_SCALAR`: Portable scalar implementation.
|
|
49
|
-
- `KERNEL_SSE42`: SSE4.2 intrinsics kernel. Likely better on AMD CPUs.
|
|
50
|
-
- `KERNEL_AVX2`: AVX2 intrinsics kernel. Likely better on Intel CPUs.
|
|
51
|
-
|
|
52
|
-
### Parameters
|
|
53
|
-
|
|
54
|
-
All input matrices should have the same height and width.
|
|
55
|
-
|
|
56
|
-
#### `base`
|
|
57
|
-
|
|
58
|
-
RGB or RGBA, dropping the alpha channel if it exists.
|
|
59
|
-
The base image for application.
|
|
60
|
-
|
|
61
|
-
#### `texture`
|
|
62
|
-
|
|
63
|
-
RGB or RGBA, applying the alpha if it exists.
|
|
64
|
-
This is the texture to be applied.
|
|
65
|
-
|
|
66
|
-
#### `skin`
|
|
67
|
-
|
|
68
|
-
RGBA, the segmented portion of base to texture.
|
|
69
|
-
The "skin" of the object the texture is to be applied to.
|
|
70
|
-
|
|
71
|
-
#### `im_alpha`
|
|
72
|
-
|
|
73
|
-
The alpha of parameter `skin`.
|
|
74
|
-
This is mostly a holdover from the Python implementation to deal with NumPy.
|
|
75
|
-
|
|
76
|
-
#### `kernel`
|
|
77
|
-
|
|
78
|
-
One of `KernelKind`.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: normal_grain_merge
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Fused normal and grain merge C extension
|
|
5
|
+
Author: Samuel Howard
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/samhaswon/normal_grain_merge
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/samhaswon/normal_grain_merge/issues
|
|
9
|
+
Keywords: image,processing
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: C
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: numpy<2.3.0
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# normal_grain_merge
|
|
20
|
+
|
|
21
|
+
This implements a combined version of the blend modes normal and grain merge.
|
|
22
|
+
Grain merge is performed on *s* and *t* with the result normal-merged with *b*.
|
|
23
|
+
Subscripts indicate channels, with alpha (α) channels broadcast to three channels.
|
|
24
|
+
|
|
25
|
+
$$
|
|
26
|
+
(((\mathrm{t_{rgb}} + \mathrm{s_{rgb}} - 0.5) * \mathrm{t_\alpha} + \mathrm{t_{rgb}} * (1 - \mathrm{t_\alpha})) * (1 - 0.3) + \mathrm{s_{rgb}} * 0.3) * \mathrm{t_\alpha} + \mathrm{b_{rgb}} * (1 - \mathrm{t_\alpha})
|
|
27
|
+
$$
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
```py
|
|
31
|
+
import numpy as np
|
|
32
|
+
from normal_grain_merge import normal_grain_merge, KernelKind
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# Example arrays
|
|
36
|
+
base = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
37
|
+
texture = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
38
|
+
skin = np.zeros((100, 100, 4), dtype=np.uint8)
|
|
39
|
+
im_alpha = np.zeros((100, 100), dtype=np.uint8)
|
|
40
|
+
|
|
41
|
+
result_scalar = normal_grain_merge(base, texture, skin, im_alpha, KernelKind.KERNEL_SCALAR.value)
|
|
42
|
+
print(result_scalar.shape, result_scalar.dtype)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
There are three kernels implemented in this module as defined in `KernelKind`.
|
|
46
|
+
|
|
47
|
+
- `KERNEL_AUTO`: Automatically chooses the kernel, preferring AVX2
|
|
48
|
+
- `KERNEL_SCALAR`: Portable scalar implementation.
|
|
49
|
+
- `KERNEL_SSE42`: SSE4.2 intrinsics kernel. Likely better on AMD CPUs.
|
|
50
|
+
- `KERNEL_AVX2`: AVX2 intrinsics kernel. Likely better on Intel CPUs.
|
|
51
|
+
|
|
52
|
+
### Parameters
|
|
53
|
+
|
|
54
|
+
All input matrices should have the same height and width.
|
|
55
|
+
|
|
56
|
+
#### `base`
|
|
57
|
+
|
|
58
|
+
RGB or RGBA, dropping the alpha channel if it exists.
|
|
59
|
+
The base image for application.
|
|
60
|
+
|
|
61
|
+
#### `texture`
|
|
62
|
+
|
|
63
|
+
RGB or RGBA, applying the alpha if it exists.
|
|
64
|
+
This is the texture to be applied.
|
|
65
|
+
|
|
66
|
+
#### `skin`
|
|
67
|
+
|
|
68
|
+
RGBA, the segmented portion of base to texture.
|
|
69
|
+
The "skin" of the object the texture is to be applied to.
|
|
70
|
+
|
|
71
|
+
#### `im_alpha`
|
|
72
|
+
|
|
73
|
+
The alpha of parameter `skin`.
|
|
74
|
+
This is mostly a holdover from the Python implementation to deal with NumPy.
|
|
75
|
+
|
|
76
|
+
#### `kernel`
|
|
77
|
+
|
|
78
|
+
One of `KernelKind`.
|
|
79
|
+
|
|
80
|
+
## Performance
|
|
81
|
+
|
|
82
|
+
The entire reason for me writing this was NumPy being slow when this operation is in the hot path.
|
|
83
|
+
So, I decided to write a SIMD version that does the type casting outside NumPy with only the intermediate values being in FP32.
|
|
84
|
+
|
|
85
|
+
How much of a speedup is this? All numbers are from a Ryzen 7 4800H running Windows 11 and Python 3.12.4.
|
|
86
|
+
|
|
87
|
+
| Method/Kernel | Average Iteration Time |
|
|
88
|
+
|-------------------|------------------------|
|
|
89
|
+
| C scalar kernel | 0.019565s |
|
|
90
|
+
| C SSE4.2 kernel | 0.013705s |
|
|
91
|
+
| C AVX2 kernel | 0.016842s |
|
|
92
|
+
| NumPy version | 0.228098s |
|
|
93
|
+
| Old NumPy version | 0.350554s |
|
|
94
|
+
|
|
95
|
+
| Method Comparison | Speedup |
|
|
96
|
+
|--------------------|----------|
|
|
97
|
+
| NumPy -> scalar | 91.4227% |
|
|
98
|
+
| NumPy -> SSE4.2 | 93.9915% |
|
|
99
|
+
| NumPy -> AVX2 | 92.6165% |
|
|
100
|
+
| Old np -> SSE4.2 | 96.0904% |
|
|
101
|
+
| C scalar -> SSE4.2 | 29.9487% |
|
|
102
|
+
| C scalar -> AVX2 | 13.9183% |
|
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
[build-system]
|
|
2
|
-
requires = [
|
|
3
|
-
"setuptools>=64",
|
|
4
|
-
"wheel",
|
|
5
|
-
"numpy<2.3.0" # Follows opencv-python (cv2)
|
|
6
|
-
]
|
|
7
|
-
build-backend = "setuptools.build_meta"
|
|
8
|
-
|
|
9
|
-
[project]
|
|
10
|
-
name = "normal_grain_merge"
|
|
11
|
-
version = "0.0.
|
|
12
|
-
description = "Fused normal and grain merge C extension"
|
|
13
|
-
readme = "README.md"
|
|
14
|
-
authors = [
|
|
15
|
-
{ name = "Samuel Howard" }
|
|
16
|
-
]
|
|
17
|
-
dependencies = ["numpy<2.3.0"]
|
|
18
|
-
requires-python = ">=3.
|
|
19
|
-
license = "MIT"
|
|
20
|
-
classifiers = [
|
|
21
|
-
"Programming Language :: Python :: 3",
|
|
22
|
-
"Programming Language :: C",
|
|
23
|
-
"Operating System :: OS Independent",
|
|
24
|
-
]
|
|
25
|
-
keywords = [
|
|
26
|
-
"image",
|
|
27
|
-
"processing"
|
|
28
|
-
]
|
|
29
|
-
[project.urls]
|
|
30
|
-
"Homepage" = "https://github.com/samhaswon/normal_grain_merge"
|
|
31
|
-
"Bug Tracker" = "https://github.com/samhaswon/normal_grain_merge/issues"
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = [
|
|
3
|
+
"setuptools>=64",
|
|
4
|
+
"wheel",
|
|
5
|
+
"numpy<2.3.0" # Follows opencv-python (cv2)
|
|
6
|
+
]
|
|
7
|
+
build-backend = "setuptools.build_meta"
|
|
8
|
+
|
|
9
|
+
[project]
|
|
10
|
+
name = "normal_grain_merge"
|
|
11
|
+
version = "0.0.1"
|
|
12
|
+
description = "Fused normal and grain merge C extension"
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
authors = [
|
|
15
|
+
{ name = "Samuel Howard" }
|
|
16
|
+
]
|
|
17
|
+
dependencies = ["numpy<2.3.0"]
|
|
18
|
+
requires-python = ">=3.8"
|
|
19
|
+
license = { text = "MIT" }
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: C",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
]
|
|
25
|
+
keywords = [
|
|
26
|
+
"image",
|
|
27
|
+
"processing"
|
|
28
|
+
]
|
|
29
|
+
[project.urls]
|
|
30
|
+
"Homepage" = "https://github.com/samhaswon/normal_grain_merge"
|
|
31
|
+
"Bug Tracker" = "https://github.com/samhaswon/normal_grain_merge/issues"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[egg_info]
|
|
2
|
-
tag_build =
|
|
3
|
-
tag_date = 0
|
|
4
|
-
|
|
1
|
+
[egg_info]
|
|
2
|
+
tag_build =
|
|
3
|
+
tag_date = 0
|
|
4
|
+
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
+
import platform
|
|
1
2
|
from setuptools import setup, Extension
|
|
2
|
-
import numpy
|
|
3
3
|
import sys
|
|
4
|
+
import numpy
|
|
4
5
|
|
|
5
|
-
extra_compile_args = []
|
|
6
6
|
|
|
7
|
+
arch = platform.machine().lower()
|
|
7
8
|
extra_compile_args = []
|
|
9
|
+
|
|
8
10
|
if sys.platform == "win32":
|
|
9
11
|
extra_compile_args += ["/O2", "/arch:AVX2", "/Qpar"] # enables AVX/AVX2; SSE4.2 implied
|
|
12
|
+
elif "arm" in arch or "aarch64" in arch:
|
|
13
|
+
extra_compile_args += ["-O3"]
|
|
10
14
|
else:
|
|
11
|
-
extra_compile_args += ["-O3", "-march=x86-64
|
|
15
|
+
extra_compile_args += ["-O3", "-march=x86-64", "-mavx2", "-msse4.2"]
|
|
12
16
|
|
|
13
17
|
module = Extension(
|
|
14
18
|
"normal_grain_merge.normal_grain_merge",
|
|
@@ -19,7 +23,7 @@ module = Extension(
|
|
|
19
23
|
|
|
20
24
|
setup(
|
|
21
25
|
name="normal_grain_merge",
|
|
22
|
-
version="0.0.
|
|
26
|
+
version="0.0.1",
|
|
23
27
|
description="Normal grain merge C extension",
|
|
24
28
|
ext_modules=[module],
|
|
25
29
|
packages=["normal_grain_merge"],
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge/normal_grain_merge.c
RENAMED
|
File without changes
|
{normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/requires.txt
RENAMED
|
File without changes
|
{normal_grain_merge-0.0.0 → normal_grain_merge-0.0.1}/normal_grain_merge.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|