Skip to main content

Fused normal and grain merge C extension

Project description

normal_grain_merge

This implements a combined version of the blend modes normal and grain merge. Grain merge is performed on s and t with the result normal-merged with b. Subscripts indicate channels, with alpha (α) channels broadcast to three channels.

$$ (((\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}) $$

Installation

pip install normal-grain-merge

Usage

import numpy as np
from normal_grain_merge import normal_grain_merge, KernelKind


# Example arrays
base = np.zeros((100, 100, 3), dtype=np.uint8)
texture = np.zeros((100, 100, 3), dtype=np.uint8)
skin = np.zeros((100, 100, 4), dtype=np.uint8)
im_alpha = np.zeros((100, 100), dtype=np.uint8)

result_scalar = normal_grain_merge(base, texture, skin, im_alpha, KernelKind.KERNEL_SCALAR.value)
print(result_scalar.shape, result_scalar.dtype)

There are three kernels implemented in this module as defined in KernelKind.

  • KERNEL_AUTO: Automatically chooses the kernel, preferring AVX2
  • KERNEL_SCALAR: Portable scalar implementation.
  • KERNEL_SSE42: SSE4.2 intrinsics kernel. Likely better on AMD CPUs.
  • KERNEL_AVX2: AVX2 intrinsics kernel. Likely better on Intel CPUs.

Parameters

All input matrices should have the same height and width.

base

RGB or RGBA, dropping the alpha channel if it exists. The base image for application.

texture

RGB or RGBA, applying the alpha if it exists. This is the texture to be applied.

skin

RGBA, the segmented portion of base to texture. The "skin" of the object the texture is to be applied to.

im_alpha

The alpha of parameter skin. This is mostly a holdover from the Python implementation to deal with NumPy.

kernel

One of KernelKind.

Performance

The entire reason for me writing this was NumPy being slow when this operation is in the hot path. So, I decided to write a SIMD version that does the type casting outside NumPy with only the intermediate values being in FP32.

How much of a speedup is this? All numbers are from a Ryzen 7 4800H running Ubuntu 24.04 and Python 3.12.3.

Method/Kernel Average Iteration Time (RGB) Average Iteration Time (RGBA)
C scalar kernel 0.016109s 0.016679s
C SSE4.2 kernel 0.002446s 0.002478s
C AVX2 kernel 0.002336s 0.002520s
NumPy version 0.160623s 0.258044s
Old NumPy version 0.248160s 0.232046s
Method Comparison Speedup (RGB) Speedup (RGBA)
NumPy -> scalar 89.9709% 93.5363%
NumPy -> SSE4.2 98.4769% 99.0397%
NumPy -> AVX2 98.5454% 99.0235%
Old np -> SSE4.2 99.0142% 98.9321%
Old np -> AVX2 99.0585% 98.9141%
C scalar -> SSE4.2 84.8135% 85.1437%
C scalar -> AVX2 85.4964% 84.8923%

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

normal_grain_merge-0.1.2.tar.gz (16.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

normal_grain_merge-0.1.2-cp313-cp313-win_amd64.whl (26.9 kB view details)

Uploaded CPython 3.13Windows x86-64

normal_grain_merge-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl (125.5 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.2-cp312-cp312-win_amd64.whl (26.9 kB view details)

Uploaded CPython 3.12Windows x86-64

normal_grain_merge-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl (125.4 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.2-cp311-cp311-win_amd64.whl (26.9 kB view details)

Uploaded CPython 3.11Windows x86-64

normal_grain_merge-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl (125.5 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (129.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.2-cp310-cp310-win_amd64.whl (26.9 kB view details)

Uploaded CPython 3.10Windows x86-64

normal_grain_merge-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl (124.7 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (128.8 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.2-cp39-cp39-win_amd64.whl (26.9 kB view details)

Uploaded CPython 3.9Windows x86-64

normal_grain_merge-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl (124.6 kB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (128.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.2-cp38-cp38-win_amd64.whl (26.8 kB view details)

Uploaded CPython 3.8Windows x86-64

normal_grain_merge-0.1.2-cp38-cp38-musllinux_1_2_x86_64.whl (126.0 kB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130.7 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file normal_grain_merge-0.1.2.tar.gz.

File metadata

  • Download URL: normal_grain_merge-0.1.2.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for normal_grain_merge-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e3825fa0c22c4f5569c76ebecadd42a5d85a07d3ce3f42db42f396d6b6e79baa
MD5 1d2587cb7c49c42ae5e257571efc0c20
BLAKE2b-256 0851d665dc92d1666d98a79ec3e97f8f35d5f07dbf67b8dfb4af5f335f94af8f

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0bc7cc2744ffb6ed4f10903fdd76ac39c5b9d7d014fe30edf34640f5616f4d46
MD5 915a4c0ab2f338c4a707f6c640fef07e
BLAKE2b-256 5c70ad8dfed159e8383e690dd79d3cf6f7d677527a09de7f1c62c2148e1f75f1

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0a3b7603659b8c84c48e9168e33b7d003d6e28a17f0f456ce7ef8e41751ab5e1
MD5 274d3519c7ad82e821775f4de4e8c403
BLAKE2b-256 4e3b95f481f027065ceb642cd96a3a20cff04b3116c33581feb53e87040f2320

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b565037db5194a6635c120c952fd2ac05fcae96ab0b929b56a6a44c65314661c
MD5 29e471713afeefb245a4898201d4a7cb
BLAKE2b-256 74f65521c440c25aeba448fd98a44b757b4f3a6529107817d77438d801fd823a

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fcdce6ab8ed66279dc1f7dbfebfd0762b098824256fec0c29767d9acc8ce9331
MD5 1192d645577b74f83e70c7946e2ebdd8
BLAKE2b-256 0dffa05a5ed4947dee1d42e949a6f7848e6c87c0c1a9ba4cc2be1db952923483

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 98434f9450437ed48744dbf1b1cedc282cb7001122d99f7a83b183df21a9e5bf
MD5 5dfd37a673afed74a11a8aa4d3b29e53
BLAKE2b-256 f612d75a6a9036e1956fdf1d2e040c74e73093520ff7e026ab6e91b3b40e15ae

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6a4395c348fa0eec6f41da5b4bd9ca46b91a2e8119003655e631b539af43c502
MD5 851d3a050055fad91bc4448774338646
BLAKE2b-256 81fbdf6f191ba68485eb40c9d2f75d58bc5c3e8025fd4b0a4d5d412046e54b1b

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b994860e068dca3b5fcc52354c70625c1f5326aae060ffb6e4342fef5f9b847d
MD5 2ee42bd1b999802875eb62c705f9f640
BLAKE2b-256 28ed6f72c87bb85025dea2a47d51681fcfa4568f185a7ec3607baae9a931314e

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 46b68b7800455115eef68c0083e498370c1329546a2e21ddb07e63b0f6bd6342
MD5 f77c4818215bad4114f8fbc5ae04c4e0
BLAKE2b-256 ee726018e8e645ffec9e4bbbb17e0e17638aec2b99c868434b2f014f41b7b9cf

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 12704cd6473938e60af87a60a6ab7291ced8162fe5a3672e252ffd5b9920cbd5
MD5 fc49226532f150df8b79c325c3fc9c1f
BLAKE2b-256 4b6e79873ec13ca8ea4a588154264c8f6ca6d8753205b126cee02877c8f92b79

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 eb6d2f51ecf1153f9273ede83458589ea984d3cba12b9f35fc345d0ac32a08dd
MD5 1c9540bea62d1b445cbee61e9dc19521
BLAKE2b-256 33f4e21331d233f17fbda1765abfb6cc51c3e2ac171394133261058949109140

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 77557ba1b2e738a5d15bc7d277bce76298160bb4801c2ab71a5062bf42dd8828
MD5 c609f7270304d01b7cde1a4275457327
BLAKE2b-256 cbe4dc008479b428de05a40aa0ee697b7439a09d5a12cf0231924159718d9c68

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 682b2d517b6a5a206ba1e521bcb291276b54a6da7f354152ad50b42fe73ea995
MD5 3ee62b2e8887897a41108d1bd4d31774
BLAKE2b-256 1fc03007b7d8c1d66686a0650a7d9d054e73512c1e36339541293e78a1c4a432

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d059295f634e8dd3a0fa37ab6b8fe8b3248ff2ff75a2c75b873b7b509d10a79f
MD5 f5e4030d855a6c1e507fbaf081b9f641
BLAKE2b-256 2f5833e2fa1a50aaf959a5df42db76e100f1ffef8cbd7d34c10e6815084aeaca

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e99c54fd25d180e1248908e2b5603401549348825c5955108c640b695a897778
MD5 2d0d9dded2d26860783814a28e521fd8
BLAKE2b-256 76a10083b37fe59935d72e9262279c6f91b83f43b9da0d580d11bb0f984f9726

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0b5704c9d128ac67eafee7c6d07298abaa5d1258646fdc6022fe5dfe9b09da67
MD5 938b159a2e358dca13c7b4c248baa1ad
BLAKE2b-256 ab9e01f5213ce2a8456d361924ca1e7b580a200f4d924ccc8e9cc28429de92ec

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 81c236231bd01b64f622c1dde78efecb03fc54ea6f7afc2a6144616876663582
MD5 6a0373d3e3f72649515b043f19fd3de0
BLAKE2b-256 20298f62176278ef9a38e9e2c069446eb6fd534674af4c7bab7b26f8dadbd560

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp38-cp38-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c06eecce92d670613712406f2adfe28a558c0580def1c21fb5aa5e453c66cb93
MD5 7d90c5d5f9861623a0eea35ed8bf20a8
BLAKE2b-256 04ca6ab08bca7531e74341689f82b5f7eb868b009e9986df418fb34ddd778297

See more details on using hashes here.

File details

Details for the file normal_grain_merge-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for normal_grain_merge-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 41f3d55248752509a2c93d34b0c59052874dd41e3ef814e2ee60e02a22362425
MD5 253e450ab48c7a15688c7f45465feba7
BLAKE2b-256 c25a2bec368300c708f953eee697120bcc9f9b7db068d3b32cba97e71e6b9403

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page