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.016076s
C SSE4.2 kernel 0.007300s
C AVX2 kernel 0.007113s
NumPy version 0.169621s
Old NumPy version 0.254648s
Method Comparison Speedup
NumPy -> scalar 90.5223%
NumPy -> SSE4.2 95.6965%
NumPy -> AVX2 95.8063%
Old np -> SSE4.2 97.1334%
Old np -> AVX2 97.2066%
C scalar -> SSE4.2 54.5933%
C scalar -> AVX2 55.7525%

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.1.tar.gz (14.8 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.1-cp313-cp313-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.13Windows x86-64

normal_grain_merge-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl (73.5 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (74.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.1-cp312-cp312-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.12Windows x86-64

normal_grain_merge-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl (73.4 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (74.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.1-cp311-cp311-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.11Windows x86-64

normal_grain_merge-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl (73.0 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (74.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.1-cp310-cp310-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.10Windows x86-64

normal_grain_merge-0.1.1-cp310-cp310-musllinux_1_2_x86_64.whl (72.2 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (73.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.1-cp39-cp39-win_amd64.whl (23.5 kB view details)

Uploaded CPython 3.9Windows x86-64

normal_grain_merge-0.1.1-cp39-cp39-musllinux_1_2_x86_64.whl (72.0 kB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (73.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.1.1-cp38-cp38-win_amd64.whl (23.4 kB view details)

Uploaded CPython 3.8Windows x86-64

normal_grain_merge-0.1.1-cp38-cp38-musllinux_1_2_x86_64.whl (73.5 kB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

normal_grain_merge-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (75.9 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: normal_grain_merge-0.1.1.tar.gz
  • Upload date:
  • Size: 14.8 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.1.tar.gz
Algorithm Hash digest
SHA256 528b8c2d400d209bc3241350ee715d53072472349c49b79390b3713c4603a793
MD5 c1b36e2051c9f74bfa90d655a18fe975
BLAKE2b-256 dc1f678a395f6842a605a1d2aed0b6512ec3c7c544da4406158ff8f7928b087c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0868621f2316445e444ab5df96178c387e01900de869394b3467b8d90b55d5db
MD5 206abbfd4f8dbb9cb16f0d85f1628adf
BLAKE2b-256 4ae640a838fa5bd10cab42fa2d60ee855ba983de3228472b633b991ec941534f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3193c3e9810465cd3af75cd99283faaa88b24688dd2faaf6339b84f34783ee00
MD5 bf81c13ec002ff2f3ba04448b1c7abe7
BLAKE2b-256 bc9ddfcd56eace0ab96ae9f0495795c98d1f1c596e00b9e4b6695066c61056ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 61c33759db9aa30511f41874ab67e6e2e66ae0e8be56f8ba0e60a7c53ffbfcc1
MD5 06456431f01251754781b55e0e78efe6
BLAKE2b-256 13d10a1694f11736fec3ba6a7691f93bfbf653e9460bdefd6b0d287df8bd5162

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 99a3a199cb702fe30c093ba381d37bd12630fa2ae1c3bcfc4a75185b620701aa
MD5 442ed687998bf0a98eebc8e3c8bfa81f
BLAKE2b-256 fc6ae6618d4e385bfcc961e6b3ad864edfbb7efb0002c9f497a8a9f3e1f4dddb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ef8cd651b12120a203efcdd4adcce2c30f8e507785d2f1cfaaa1443fd6716626
MD5 9aa8eb9d33b07cc27f791b991e2e9496
BLAKE2b-256 4e8a018dd54186dbfe8f83ed2645f0ab595ff40a99a9c737cce3154b660508ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d55d5ae14b3724503d596910220a194abefb61e7068cff1b73ed10579aea1469
MD5 9407e5220bd9b3869dcc660f75ae686d
BLAKE2b-256 98d4ef07780ab756e70a49e58ab0619ac2644dc7b7b528b66ad6894352c8f147

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 258c03bf317d50dacfc6c76718aa84bc0553f2b068e424b854ca85a36a0d20ae
MD5 f79d9544c7cbe0a513b701e4683860bd
BLAKE2b-256 606258ac2a9ff037f85baaa8c2f60320e1219c00e75c0c783a42fc2412ca71af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3cb5fb30a3389cccd53be8053ce40f8efb5a3b2142d2d13b3f45e37cd0a448f6
MD5 198632aac8d0c45cc3897217c98fbbda
BLAKE2b-256 53b2221cc6636d44839f6945518cdbd2623e03766ad8eb1eb4384d4f86abf872

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1fda7d1efba27683b759ffb4e54edfb59b3ba741e440d49dd7037ec5963ad165
MD5 f1a6e2989e3845ad8416fd56d5d8778c
BLAKE2b-256 18a61420024895d6c8ccb71285d67f96dfbba433791b4299e1f003aa914332c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e7f3593f5b87d42ec51078e8e99d24c7901a85f327bb7966bfbbc389ebbd3ddb
MD5 0c46e898fddf7eca35260656350662da
BLAKE2b-256 666e3742562a5052483a4fdef46829292808b04459be88ec9226101189537ea3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3bab959fb28071784ad904518169970ba8d8b298dbdfb9685e6c93f21a2f915f
MD5 c91b9625af8f924ae0ade59ed91614cf
BLAKE2b-256 aede0da657d866bef40f766f90a6831078a83ba418466e94267df34995ac7a5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1cfdca4a773624fa715acd191c961a5ef4b2197b1f5af373fca974fb36815692
MD5 1cb251bdffc5aa0069084e7ca6981a95
BLAKE2b-256 882a8d5407f6c3a526193131e8dd92a3d40b524c1cdd78502da214b55e40c23e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 739ac368d292a1421a633c5e2b32c3e12430e6cff271e25bb72392341a6db77c
MD5 126cc79f7572e23947c2e6ee52e9c6d5
BLAKE2b-256 fe9a37411a2a4740363f793e50fc6f48a96c5e31c132eb8bd672884c871d1570

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2b43861176150cda88de470ead6a0dcf8f528287458de9d0d963e1fa92c95553
MD5 2ad8a3bd68562ad92cc344d425a63f0a
BLAKE2b-256 31740f461233f88f43010c582d51697953e0402f13fafb6cbb2f4f94a8920d20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 84e183085b1088fc36e674233162ce96116d72de8a90f61388def309d91e9162
MD5 9823eb93d9ca219500ae4fd1193d1ce9
BLAKE2b-256 45f1c7f48764a2d3ebeb2803babf16176b23cca426cba530491db7da6898dee7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 67a962055bc36708d4b6de92df61be8f43dcec3ce6a05b6393f973e151dc6217
MD5 7759fefb4ae1d55cb926738cf2478c70
BLAKE2b-256 242100741751d5a8101e459cf8caf64e49782e5498f7386a107c0b215ef87c8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 35fb0c8883ecfbc0e9d5348c124c2dcedbc74cedb26761197120bc0874982e2f
MD5 83334428e668f4886591b42c22fee370
BLAKE2b-256 4fb61275d3f0b684777bffebc8eae9aa5afe8f95e2ab3675328df72250d9dd62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5bf692de0f02b85185db1cf49286468915133f98b77a6883013bc201ab6f2ecb
MD5 496792c52317bc9c11f7d20faee3d010
BLAKE2b-256 6cdc30f9bcebbed20c9e3d279579a3780d323294033af3149edb52e3dd6de47f

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