normal-grain-merge 0.1.3__cp311-cp311-win_amd64.whl
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.
- normal_grain_merge/__init__.py +2 -0
- normal_grain_merge/kernel_kind.py +8 -0
- normal_grain_merge/normal_grain_merge.c +1331 -0
- normal_grain_merge/normal_grain_merge.cp311-win_amd64.pyd +0 -0
- normal_grain_merge/normal_grain_merge.pyi +28 -0
- normal_grain_merge-0.1.3.dist-info/METADATA +109 -0
- normal_grain_merge-0.1.3.dist-info/RECORD +10 -0
- normal_grain_merge-0.1.3.dist-info/WHEEL +5 -0
- normal_grain_merge-0.1.3.dist-info/licenses/LICENSE +21 -0
- normal_grain_merge-0.1.3.dist-info/top_level.txt +1 -0
|
Binary file
|
|
@@ -0,0 +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 RGB 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
|
+
...
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: normal_grain_merge
|
|
3
|
+
Version: 0.1.3
|
|
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
|
|
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
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
pip install normal-grain-merge
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
```py
|
|
37
|
+
import numpy as np
|
|
38
|
+
from normal_grain_merge import normal_grain_merge, KernelKind
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# Example arrays
|
|
42
|
+
base = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
43
|
+
texture = np.zeros((100, 100, 3), dtype=np.uint8)
|
|
44
|
+
skin = np.zeros((100, 100, 4), dtype=np.uint8)
|
|
45
|
+
im_alpha = np.zeros((100, 100), dtype=np.uint8)
|
|
46
|
+
|
|
47
|
+
result_scalar = normal_grain_merge(base, texture, skin, im_alpha, KernelKind.KERNEL_SCALAR.value)
|
|
48
|
+
print(result_scalar.shape, result_scalar.dtype)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
There are three kernels implemented in this module as defined in `KernelKind`.
|
|
52
|
+
|
|
53
|
+
- `KERNEL_AUTO`: Automatically chooses the kernel, preferring AVX2
|
|
54
|
+
- `KERNEL_SCALAR`: Portable scalar implementation.
|
|
55
|
+
- `KERNEL_SSE42`: SSE4.2 intrinsics kernel. Likely better on AMD CPUs.
|
|
56
|
+
- `KERNEL_AVX2`: AVX2 intrinsics kernel. Likely better on Intel CPUs.
|
|
57
|
+
|
|
58
|
+
### Parameters
|
|
59
|
+
|
|
60
|
+
All input matrices should have the same height and width.
|
|
61
|
+
|
|
62
|
+
#### `base`
|
|
63
|
+
|
|
64
|
+
RGB or RGBA, dropping the alpha channel if it exists.
|
|
65
|
+
The base image for application.
|
|
66
|
+
|
|
67
|
+
#### `texture`
|
|
68
|
+
|
|
69
|
+
RGB or RGBA, applying the alpha if it exists.
|
|
70
|
+
This is the texture to be applied.
|
|
71
|
+
|
|
72
|
+
#### `skin`
|
|
73
|
+
|
|
74
|
+
RGBA, the segmented portion of base to texture.
|
|
75
|
+
The "skin" of the object the texture is to be applied to.
|
|
76
|
+
|
|
77
|
+
#### `im_alpha`
|
|
78
|
+
|
|
79
|
+
The alpha of parameter `skin`.
|
|
80
|
+
This is mostly a holdover from the Python implementation to deal with NumPy.
|
|
81
|
+
|
|
82
|
+
#### `kernel`
|
|
83
|
+
|
|
84
|
+
One of `KernelKind`.
|
|
85
|
+
|
|
86
|
+
## Performance
|
|
87
|
+
|
|
88
|
+
The entire reason for me writing this was NumPy being slow when this operation is in the hot path.
|
|
89
|
+
So, I decided to write a SIMD version that does the type casting outside NumPy with only the intermediate values being in FP32.
|
|
90
|
+
|
|
91
|
+
How much of a speedup is this? All numbers are from a Ryzen 7 4800H running Ubuntu 24.04 and Python 3.12.3.
|
|
92
|
+
|
|
93
|
+
| Method/Kernel | Average Iteration Time (RGB) | Average Iteration Time (RGBA) |
|
|
94
|
+
|-------------------|------------------------------|-------------------------------|
|
|
95
|
+
| C scalar kernel | 0.016109s | 0.016679s |
|
|
96
|
+
| C SSE4.2 kernel | 0.002446s | 0.002478s |
|
|
97
|
+
| C AVX2 kernel | 0.002336s | 0.002520s |
|
|
98
|
+
| NumPy version | 0.160623s | 0.258044s |
|
|
99
|
+
| Old NumPy version | 0.248160s | 0.232046s |
|
|
100
|
+
|
|
101
|
+
| Method Comparison | Speedup (RGB) | Speedup (RGBA) |
|
|
102
|
+
|--------------------|---------------|----------------|
|
|
103
|
+
| NumPy -> scalar | 89.9709% | 93.5363% |
|
|
104
|
+
| NumPy -> SSE4.2 | 98.4769% | 99.0397% |
|
|
105
|
+
| NumPy -> AVX2 | 98.5454% | 99.0235% |
|
|
106
|
+
| Old np -> SSE4.2 | 99.0142% | 98.9321% |
|
|
107
|
+
| Old np -> AVX2 | 99.0585% | 98.9141% |
|
|
108
|
+
| C scalar -> SSE4.2 | 84.8135% | 85.1437% |
|
|
109
|
+
| C scalar -> AVX2 | 85.4964% | 84.8923% |
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
normal_grain_merge/__init__.py,sha256=Roc1wQ7_13LG_Z3Bd82zhk8wn7R1BrcO63fCdsvnnJU,89
|
|
2
|
+
normal_grain_merge/kernel_kind.py,sha256=3cP4WRQSG9ZZeHsrXpXJ5Kcc8wABsmRSgex0rwRT8K4,162
|
|
3
|
+
normal_grain_merge/normal_grain_merge.c,sha256=FtrCLe8ajPrd9mSFLloFwKaxfYgjTKKOo5nvSpu2lcQ,56968
|
|
4
|
+
normal_grain_merge/normal_grain_merge.cp311-win_amd64.pyd,sha256=C3ko9jsBFj9DgW_Zm0HzrwKGBSv0lzPdSdtemFoV4c0,28672
|
|
5
|
+
normal_grain_merge/normal_grain_merge.pyi,sha256=HXa55A0wdcmzPpJzi7qgJws5y2q_uGjdJZQXzTkw9vc,1089
|
|
6
|
+
normal_grain_merge-0.1.3.dist-info/licenses/LICENSE,sha256=qbUDFP46iOpV1ouBhpqjX-kS_cCVMHgrLBNcdTlq7Qc,1089
|
|
7
|
+
normal_grain_merge-0.1.3.dist-info/METADATA,sha256=Cml2gzBTWhL3Fua_ftXqdBFXPw76ej6Dwk3GXnnkmDU,4055
|
|
8
|
+
normal_grain_merge-0.1.3.dist-info/WHEEL,sha256=bVDps4tOb0nHmbMU4uK7h9NZsZJmyTqcRbrvK3TZLY4,102
|
|
9
|
+
normal_grain_merge-0.1.3.dist-info/top_level.txt,sha256=jfUAUKWrxBshHvZ0xTu3uF5VJsUpbWp5NkxUj8OXqu8,19
|
|
10
|
+
normal_grain_merge-0.1.3.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Samuel Howard
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
normal_grain_merge
|