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
C scalar kernel 0.016007s
C SSE4.2 kernel 0.011155s
C AVX2 kernel 0.014575s
NumPy version 0.190392s
Old NumPy version 0.274065s
Method Comparison Speedup
NumPy -> scalar 91.5927%
NumPy -> SSE4.2 94.1409%
NumPy -> AVX2 92.3448%
Old np -> SSE4.2 95.9297%
Old np -> AVX2 94.6819%
C scalar -> SSE4.2 30.3086%
C scalar -> AVX2 8.9448%

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.0.tar.gz (14.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.0-cp313-cp313-win_amd64.whl (23.0 kB view details)

Uploaded CPython 3.13Windows x86-64

normal_grain_merge-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl (69.1 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (69.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.0-cp312-cp312-win_amd64.whl (23.0 kB view details)

Uploaded CPython 3.12Windows x86-64

normal_grain_merge-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl (69.1 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (69.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.0-cp311-cp311-win_amd64.whl (23.0 kB view details)

Uploaded CPython 3.11Windows x86-64

normal_grain_merge-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl (68.5 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (69.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.0-cp310-cp310-win_amd64.whl (23.0 kB view details)

Uploaded CPython 3.10Windows x86-64

normal_grain_merge-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl (67.8 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (68.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.0-cp39-cp39-win_amd64.whl (23.0 kB view details)

Uploaded CPython 3.9Windows x86-64

normal_grain_merge-0.1.0-cp39-cp39-musllinux_1_2_x86_64.whl (67.6 kB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (68.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.0-cp38-cp38-win_amd64.whl (22.9 kB view details)

Uploaded CPython 3.8Windows x86-64

normal_grain_merge-0.1.0-cp38-cp38-musllinux_1_2_x86_64.whl (69.0 kB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (69.9 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: normal_grain_merge-0.1.0.tar.gz
  • Upload date:
  • Size: 14.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.0.tar.gz
Algorithm Hash digest
SHA256 e26513b6e19b658cd0a353f068f96d3ff74436b89efde4daec64d0d2fdc2d9c1
MD5 58ad0e18adb902870939898a4a50a673
BLAKE2b-256 e3d79fb8c7a7f009423e45124a284c4d576bc014a5f54a7441e183b4dabd4bd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 09747eff160cd87ce0c8739c414459a3f2d38c072c6b429c038872cb9e388a8a
MD5 0e588aa7b0bf0cb4d05c81a1a8d25d17
BLAKE2b-256 d46dcf49fab2c6a4e553db51936353ea777c86098ae31139503a987296b89945

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5a844960e3f736598c810d3f3eb2c47703bb6103442ff3a469e172f6f7c4af6b
MD5 56852e07b122f4e777aeecfadf729c12
BLAKE2b-256 63348d540f18022e6fe2bf5cef414d77e8a31ce29e68ad098809429b9434fa7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4862e41b1a3ddf2b74ceaae7370fd02e737ab3b49cc91e5cfbf77f2994ecd868
MD5 fd6c17417397242e5bcc400c4f2fb97c
BLAKE2b-256 47ace3ef688c654b0378dbb1fec954caea9cb4768e6fe4ef10b70f6145f14da5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a66bab4d59dfa4b245b6e2fe9be668068f7efb3e5dfd7ef85db62d0363d3c98f
MD5 707f5a3096ab61b74c9c99d5684db0ce
BLAKE2b-256 8651d0f9172572b8bca4003c443efa57c97ec4a07d2919bd24dd06e673f60376

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a423ed2dcac9845bd91bfb173b5d926097b093db5ed87ac4a455ec7b5957b2d9
MD5 d798b256757372c7066f4870829bb6fa
BLAKE2b-256 418aedbade626f0edded31562f93777d44edbde61761ad674f76d2cc0ca2c27e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8f3fd578525229f9d7050dd0ea23e400442566b06f702a5df83e0ad25ab7a0c9
MD5 0cb9bea3a1d80c5ac574c749ac813fc5
BLAKE2b-256 0bf19efad46678b6729dad7af609772b9a2434228c31b41f7ff02dbfafac90ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3792b2036916e883d2e872a748aa060056c2b048057e390a3993e3cf91a99f8c
MD5 ec552a6ab56a51a40324d05b9518eb0e
BLAKE2b-256 ca492a8cdf51448fd4b3379277707e2b3ce92fdd533511c790c42967b3213c68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e5332e7dbc13cc3188cd642551e8de0b2d4a5b52329983b636fe3fc57e8925f5
MD5 4ccf4a0a7efa7a6ce9314c24c791ad13
BLAKE2b-256 7d4ef0840b0522cc27fe26f577c272528ba2d916e628f3ae398abd448c78c388

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dc67c1271e49d79c1ec352d2f1b4bda74744ccccc59a02e4c59692d0ffd0027f
MD5 6e88d2668e5511478ce467a3f70d772d
BLAKE2b-256 eb8832702d8186880f829c8110906a212d16817bb6fab73bafee7f84562cbd99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f7df1eddc688eca9c41504fedcc4a7e40eedaf42428fdd073149e79d89d403e6
MD5 8e8270d37ed6b61e53633fe6ef0d62e9
BLAKE2b-256 804ea420a7aa9fd593fd439c9c98b862de832aa65085aaed0004fa305b791c51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e1470257dfda8e846ee0a52c84c0d109346b9d808d9d9b9f63c8cc3c186291d6
MD5 94c177de416dabc714c42db2af858f67
BLAKE2b-256 b26237eb725e520f3deb47d9c8e8341dfdb5f38f7ebe3715dfab344171b37514

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7314660811c7bd2426a94fcd6cce286f5466f250eabd80161f4207684c2f36f8
MD5 55e1b5d46e916d882b3377be6b447b4c
BLAKE2b-256 ed36531f6b0dd4da27c770f6824237d207e7506a3d1510c4e073a67201442994

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3fb4eed6e2850b80f237d84c4b927a59f819b9c14b419f35ac92b443da237cf3
MD5 a5ce8c7821ff9b11a48fab907f88c1f9
BLAKE2b-256 ca43d721bbf2633b310bd034e8485fbd2d3e4bc5a9e582d162264d8237dad33c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c00726b4ed7e0e7d91973722ebf3ed1c43695c3c2fb92554ea495268d8c9c9f3
MD5 833c98418bc4acba8bb4c7e7c26563e2
BLAKE2b-256 bc4667a557ea5c99cc8d2607b1ff304958e2d32d154610411c651d27196ff6c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a25cf5c64bedb68ec3098a82c48d1e8ff2740cf0cf633ad799338a7204001d25
MD5 d76622275f3ada84188a42e8bc02d9d0
BLAKE2b-256 3fc8fcf4ff72cf774688bff7238d9c4174d3fbd4e75203083e2273a2ac5cdeed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 dd0b570c38bc91c2db55b390060a45155e0884d4f8f931219e48df0b7cd44191
MD5 198b94db93ee90b3e4a111a97482af3e
BLAKE2b-256 9135a163aca2dcb02cef3ce04ef11449f39a89537b18c6dc21d4eb496e6f9419

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 de8189538ac9d0f7e77b01363cc4005e3b0e0176aa493abb67a13f5ed6999fbc
MD5 421aade48a3cf8b00e4e2cbbfbc5b893
BLAKE2b-256 63699a6a07422ac799e9cac6f4e7c0fb52385ad1a3449ec5bf8e6745df8f750e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c213f9a7d0746fbe4c8739954512af44bc9d02a954dcd6fa04e9920fe07f1d47
MD5 c685a8f69e3b44fa2fdf4d84b61c1c96
BLAKE2b-256 bb21f9618ea3984b210e2dc328caecf19f938a417e3aeb55c0e77acedc23ffff

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