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 Windows 11 and Python 3.12.4.

Method/Kernel Average Iteration Time
C scalar kernel 0.019565s
C SSE4.2 kernel 0.013705s
C AVX2 kernel 0.016842s
NumPy version 0.228098s
Old NumPy version 0.350554s
Method Comparison Speedup
NumPy -> scalar 91.4227%
NumPy -> SSE4.2 93.9915%
NumPy -> AVX2 92.6165%
Old np -> SSE4.2 96.0904%
C scalar -> SSE4.2 29.9487%
C scalar -> AVX2 13.9183%

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.0.2.tar.gz (14.4 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.0.2-cp313-cp313-win_amd64.whl (22.9 kB view details)

Uploaded CPython 3.13Windows x86-64

normal_grain_merge-0.0.2-cp313-cp313-musllinux_1_2_x86_64.whl (73.9 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

normal_grain_merge-0.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (73.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.0.2-cp312-cp312-win_amd64.whl (22.9 kB view details)

Uploaded CPython 3.12Windows x86-64

normal_grain_merge-0.0.2-cp312-cp312-musllinux_1_2_x86_64.whl (73.8 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

normal_grain_merge-0.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (73.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.0.2-cp311-cp311-win_amd64.whl (22.9 kB view details)

Uploaded CPython 3.11Windows x86-64

normal_grain_merge-0.0.2-cp311-cp311-musllinux_1_2_x86_64.whl (72.5 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

normal_grain_merge-0.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (72.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.0.2-cp310-cp310-win_amd64.whl (22.9 kB view details)

Uploaded CPython 3.10Windows x86-64

normal_grain_merge-0.0.2-cp310-cp310-musllinux_1_2_x86_64.whl (71.7 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

normal_grain_merge-0.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (72.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.0.2-cp39-cp39-win_amd64.whl (22.8 kB view details)

Uploaded CPython 3.9Windows x86-64

normal_grain_merge-0.0.2-cp39-cp39-musllinux_1_2_x86_64.whl (71.5 kB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

normal_grain_merge-0.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (71.8 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

normal_grain_merge-0.0.2-cp38-cp38-win_amd64.whl (22.7 kB view details)

Uploaded CPython 3.8Windows x86-64

normal_grain_merge-0.0.2-cp38-cp38-musllinux_1_2_x86_64.whl (72.9 kB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

normal_grain_merge-0.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (73.9 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

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

File hashes

Hashes for normal_grain_merge-0.0.2.tar.gz
Algorithm Hash digest
SHA256 55bec1c091a60cb12b1e31e3f2396ca1c2d95ba9804ab874a55fedf0a8092a06
MD5 514d524654bc2fe4bff77c542074e595
BLAKE2b-256 e087b738f2ae99dea78ff7b40c8cda1dd52afe80a2332eaeddb91bd5d6df6ccc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1a8a4ae1e7fd42c95b1db5214cd1c820a22e8ff3cc5a7a21a058d8e3ba4f44f6
MD5 3dd7d01035db9cf24ae2459d47c5fd28
BLAKE2b-256 121640902148b966c0e48d19470c6b7e37b35f1b3b355339b64d8afaf6c04439

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 22d38ef298b4080ea07fa9856189cbd15eeb2aadb8dc15c44fcd3e30a94ecb2c
MD5 fd7ddd2c662f4a92529eb7abb792f31d
BLAKE2b-256 c9e13f2e6e8b6226cf5cb375b354de5d986b5245a995747ddf2be0d7f7f411e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78a3148baeaac309f1d69d6f1432a2983660e43a097b0917f9cc856ab7739b9d
MD5 b7c9c9313dd337de0d0cfc9619f69390
BLAKE2b-256 ab310127c5b28063c6c749cb16199abbf360dd3d1333a00f4c8e791df19e5692

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f910df0c4ca7cb655ad326ee852abbc4f0abd59dd7cdfe4326710f35fb7627a7
MD5 85cfad046a744f50202841fe1e6f5895
BLAKE2b-256 020a5f55430211d4cf19012a2fd2eb094ab0a545b33644d4605d99815f5db829

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3ee872c8de2f5a46b3d1af805821bbe489e35883a4be17898f48f1c533349e96
MD5 daf94d4aff29781d000c08cba3788045
BLAKE2b-256 4e7997a1530c3b1d0f29f0dd470e1fcba1102c7f7ad2645f0bdeb930a5f1b22c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a38b5144325f6e62df337297b079061cf69bfa19f9d15a47e17fecb261db364b
MD5 989276ad4e8f0cfa21a9b14580bdad05
BLAKE2b-256 9f7d3160a3b8a0fd3ea21fda7f46ed94bff58d52d6c1109a67cbb5722e599daa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3e8cfc7caf069ccde8e7f24d584a7d6aab6e6ae669ce75c8ddd80ef8e8f80c0e
MD5 33bb94db43892939ab5c9ece3bed23fc
BLAKE2b-256 5b91b5adf168f255b40c177e600686715923d0d85b93b2e7ff54ac6088ffc997

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c3ac480e9c698cdf5aa53048ace57bdb62c1888bec438269aab6271e13e43cda
MD5 1353c84758e14ce24e03cce2df7720a1
BLAKE2b-256 21de2e0748bd9d888c1d6d78365df358240790138eb7e06408d39c4a74db9e7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 714a42d46b645b2b6bb10a5dcbd4d56265f51dd178e49a93981f215581c7c319
MD5 b1c934377c2b6c8a4b51e976cea078e9
BLAKE2b-256 2bea42830b601f2cedfdd440a412324d55052ee88670e4a51b030747062b919b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 115cf16ba1da1c80db0991e4f023603be0eacb70ef71a2b2ec3d87e2f53fe3d0
MD5 68716eca82583e00919d04bfbf9cf3bd
BLAKE2b-256 b5ffc484535e8a46f350947e59297db6992cf4b77037ff26c8f432796c421e61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5958bb279868edaee4de1f162ec46caafbf8c174af2d0db61c0b406a199cfb89
MD5 c6ae895bc37da0bace4f1b80b114cd87
BLAKE2b-256 52cd0e84ba454c28e78c89b35be61aa61ddf3508498d9a0941c763aea4bead31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2d3f67c5254e7ce863edf77e6a134cd54896ce9080251941959aabe033ee89d8
MD5 0a7af4683285d864c578ec8af05dab0e
BLAKE2b-256 bb0b1fb2e1c3fd36133a8db4cfeeeba46b736d7bc6c50de50e1831b886fc39e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 daa69eb1b7f8208984b5f6093da82475a8297f75cfe4b5e19235d4efd5ea9af0
MD5 8278d906217f1fccf36a8eb174d56317
BLAKE2b-256 245a6a0f2effb0becbf654df24dfe88bb59704d3f86de0495b629f18d2c55bb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ccdef98d34bd1d67335e0bd7a1eab26cc87042a6804a266021367f28db5f4acf
MD5 44896d99d0681cce85838ed09f7cf917
BLAKE2b-256 2e0857f9820d2510dce1326df863971ee341e9b80d88a6f0392c30b82d8934bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9da742ed62ab1e8664213ce91d13200cc3288c63ed6489919de20926872d59ba
MD5 b9a9b6a6ef27f44072f93511dd747378
BLAKE2b-256 29b7cf756fae827b53987f4141ac94d9098180196a5cc1d7b9e15bc7a14d5b5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 7ec0c6effaa156ca7e0d2b0001d486c43a7186314799d80f5a82c0f5c1142b92
MD5 bf7511cd7505802ecd9a6bd84c2d949c
BLAKE2b-256 031a54c329c8a046cabcfe6b4d333feb36e21e9485e238c1f19e19a60e82b237

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 982f032105eda8ce30a2496b4ea3289b64a1a16758e090550e3566c3ecfb0556
MD5 02c776f58218648a9322813fa83ecc5e
BLAKE2b-256 e97ee53bf019620b9fd72575c4a7443a40a53652a0bc5c9956d3a0a2939685f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for normal_grain_merge-0.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ba90cef9b6fd6ffbd0514077b04df4d57c4cdc3eecc17e7415f6c737369dec76
MD5 b14395ce3e4485e89de20b63a7f11096
BLAKE2b-256 e6717a7f5ce399df5c2870e0900a337e69d1d08dbcac5303b8cd443149ca4689

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