-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f66a32
commit 80778f8
Showing
7 changed files
with
218 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.egg-info | ||
__pycache__ | ||
monolens/_version.py | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from monolens import util | ||
import numpy as np | ||
import numba as nb | ||
|
||
src = np.empty((2073600, 4), np.uint8) | ||
dst = np.empty((2073600, 4), np.uint8) | ||
cb = util.cb_full[0] | ||
a, r, g, b = util.argb | ||
|
||
|
||
def _colorblindness_np(d, s, cb): | ||
d[:, r] = cb[0, 0] * s[:, r] + cb[0, 1] * s[:, g] + cb[0, 2] * s[:, b] | ||
d[:, g] = cb[1, 0] * s[:, r] + cb[1, 1] * s[:, g] + cb[1, 2] * s[:, b] | ||
d[:, b] = cb[2, 0] * s[:, r] + cb[2, 1] * s[:, g] + cb[2, 2] * s[:, b] | ||
|
||
|
||
@nb.njit | ||
def _colorblindness_nb(d, s, cb): | ||
for i, p in enumerate(s): | ||
d[i, r] = cb[0, 0] * p[r] + cb[0, 1] * p[g] + cb[0, 2] * p[b] | ||
d[i, g] = cb[1, 0] * p[r] + cb[1, 1] * p[g] + cb[1, 2] * p[b] | ||
d[i, b] = cb[2, 0] * p[r] + cb[2, 1] * p[g] + cb[2, 2] * p[b] | ||
|
||
|
||
@nb.njit(parallel=True) | ||
def _colorblindness_nbp(d, s, cb): | ||
for i in nb.prange(len(s)): | ||
p = s[i] | ||
d[i, r] = cb[0, 0] * p[r] + cb[0, 1] * p[g] + cb[0, 2] * p[b] | ||
d[i, g] = cb[1, 0] * p[r] + cb[1, 1] * p[g] + cb[1, 2] * p[b] | ||
d[i, b] = cb[2, 0] * p[r] + cb[2, 1] * p[g] + cb[2, 2] * p[b] | ||
|
||
|
||
def test_colorblindness_np(benchmark): | ||
benchmark(lambda: _colorblindness_np(dst, src, cb)) | ||
|
||
|
||
def test_colorblindness_nb(benchmark): | ||
benchmark(lambda: _colorblindness_nb(dst, src, cb)) | ||
|
||
|
||
def test_colorblindness_nbp(benchmark): | ||
benchmark(lambda: _colorblindness_nbp(dst, src, cb)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,100 @@ | ||
import os | ||
import sys | ||
from PySide6 import QtGui | ||
import numpy as np | ||
import numba as nb | ||
|
||
DEBUG = int(os.environ.get("DEBUG", "0")) | ||
|
||
if sys.byteorder == "little": | ||
argb = (3, 2, 1, 0) | ||
else: | ||
argb = (0, 1, 2, 3) | ||
|
||
# matrix values from colorblind package | ||
cb_lms = np.array( | ||
[ | ||
# Protanopia (red weakness) | ||
[[0, 0.90822864, 0.008192], [0, 1, 0], [0, 0, 1]], | ||
# Deuteranopia (green weakness) | ||
[[1, 0, 0], [1.10104433, 0, -0.00901975], [0, 0, 1]], | ||
# Tritanopia (blue weakness) | ||
[[1, 0, 0], [0, 1, 0], [-0.15773032, 1.19465634, 0]], | ||
], | ||
) | ||
rgb2lms = np.array( | ||
[ | ||
[0.3904725, 0.54990437, 0.00890159], | ||
[0.07092586, 0.96310739, 0.00135809], | ||
[0.02314268, 0.12801221, 0.93605194], | ||
], | ||
) | ||
lms2rgb = np.linalg.inv(rgb2lms) | ||
cb_full = [np.linalg.multi_dot((lms2rgb, cbi, rgb2lms)) for cbi in cb_lms] | ||
|
||
|
||
@nb.njit(cache=True) | ||
def clip(x, xmin, xmax): | ||
if x < xmin: | ||
return xmin | ||
return min(x, xmax) | ||
|
||
|
||
class QImageArrayInterface: | ||
__slots__ = ("__array_interface__",) | ||
|
||
def __init__(self, image): | ||
format = image.format() | ||
assert format == QtGui.QImage.Format_RGB32 | ||
|
||
self.__array_interface__ = { | ||
"shape": (image.width() * image.height(), 4), | ||
"typestr": "|u1", | ||
"data": image.bits(), | ||
"version": 3, | ||
} | ||
|
||
|
||
def qimage_array_view(image): | ||
return np.asarray(QImageArrayInterface(image)) | ||
|
||
|
||
@nb.njit(parallel=True, cache=True) | ||
def _grayscale(d, s, argb): | ||
a, r, g, b = argb | ||
for i in nb.prange(len(s)): | ||
sr = s[i, r] | ||
sg = s[i, g] | ||
sb = s[i, b] | ||
c = clip(0.299 * sr + 0.587 * sg + 0.114 * sb, 0, 255) | ||
d[i, r] = c | ||
d[i, g] = c | ||
d[i, b] = c | ||
|
||
|
||
def grayscale(dest, source): | ||
s = qimage_array_view(source) | ||
d = qimage_array_view(dest) | ||
_grayscale(d, s, argb) | ||
|
||
|
||
@nb.njit(parallel=True, cache=True) | ||
def _colorblindness(d, s, cb, argb): | ||
a, r, g, b = argb | ||
for i in nb.prange(len(s)): | ||
sr = s[i, r] | ||
sg = s[i, g] | ||
sb = s[i, b] | ||
dr = cb[0, 0] * sr + cb[0, 1] * sg + cb[0, 2] * sb | ||
dg = cb[1, 0] * sr + cb[1, 1] * sg + cb[1, 2] * sb | ||
db = cb[2, 0] * sr + cb[2, 1] * sg + cb[2, 2] * sb | ||
d[i, r] = clip(dr, 0, 255) | ||
d[i, g] = clip(dg, 0, 255) | ||
d[i, b] = clip(db, 0, 255) | ||
|
||
|
||
def colorblindness(dest, source, type): | ||
s = qimage_array_view(source) | ||
d = qimage_array_view(dest) | ||
cb = cb_full[type] | ||
_colorblindness(d, s, cb, argb) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ name = monolens | |
version = attr: monolens._version.version | ||
author = Hans Dembinski | ||
author_email = [email protected] | ||
description = Show part of your screen in 8-bit grayscale | ||
description = View part of your screen in monochrome colors or in simulated protanopia, deuteranopia, or tritanopia | ||
long_description = file: README.md | ||
long_description_content_type = text/markdown | ||
url = https://github.com/hdembinski/monolens | ||
|
@@ -15,10 +15,12 @@ classifiers = | |
Operating System :: OS Independent | ||
|
||
[options] | ||
package_dir = | ||
packages = monolens | ||
python_requires = >=3.6 | ||
install_requires = pyside6 | ||
install_requires = | ||
pyside6 | ||
numpy | ||
numba | ||
|
||
[options.entry_points] | ||
console_scripts = | ||
|