def nb_read_uint12(raw_data, width): """ Convert packed 12bit data (3 bytes per 2 pixels) into unpacked 16bit data (2 bytes per pixel). `raw_data` is a 2D numpy array with the raw frame data of dimensions ``(nrows, stride)``, where ``stride`` is the size of one row in bytes. `width` is the size of the resulting row in pixels; if it is 0, assumed to be maximal possible size. Funcation semantics is identical to :func:`read_uint12`, but it is implemented with Numba to speed up calculations. """ h, s = raw_data.shape if width == 0: width = (s * 2) // 3 out = np.empty((h, width), dtype=nb.uint16) chwidth = width // 2 for i in range(h): for j in range(chwidth): fst_uint8 = nb.uint16(raw_data[i, j * 3]) mid_uint8 = nb.uint16(raw_data[i, j * 3 + 1]) lst_uint8 = nb.uint16(raw_data[i, j * 3 + 2]) out[i, j * 2] = (fst_uint8 << 4) | (mid_uint8 & 0x0F) out[i, j * 2 + 1] = (mid_uint8 >> 4) | (lst_uint8 << 4) if width % 2 == 1: fst_uint8 = nb.uint16(raw_data[i, chwidth * 3]) mid_uint8 = nb.uint16(raw_data[i, chwidth * 3 + 1]) out[i, width - 1] = (fst_uint8 << 4) | (mid_uint8 & 0x0F) return out
def buffered_bounded_lemire_uint16(bitgen, rng, bcnt, buf): """ Generates a random unsigned 16 bit integer bounded within a given interval using Lemire's rejection. The buffer acts as storage for a 32 bit integer drawn from the associated BitGenerator so that multiple integers of smaller bitsize can be generated from a single draw of the BitGenerator. """ # Note: `rng` should not be 0xFFFF. When this happens `rng_excl` becomes # zero. rng_excl = uint16(rng) + uint16(1) assert (rng != 0xFFFF) # Generate a scaled random number. n, bcnt, buf = buffered_uint16(bitgen, bcnt, buf) m = uint32(n * rng_excl) # Rejection sampling to remove any bias leftover = m & 0xFFFF if (leftover < rng_excl): # `rng_excl` is a simple upper bound for `threshold`. threshold = ((uint16(UINT16_MAX) - rng) % rng_excl) while (leftover < threshold): n, bcnt, buf = buffered_uint16(bitgen, bcnt, buf) m = uint32(n * rng_excl) leftover = m & 0xFFFF return m >> 16, bcnt, buf
def numba_decompress_blocks(input, block_size, last_block_size, block_ends, output): num_blocks = len(block_ends) for p in numba.prange(num_blocks): if p == 0: i = numba.uint64(0) else: i = numba.uint64(block_ends[p - numba.uint(1)]) block_end = numba.uint64(block_ends[p]) j = numba.uint64(block_size * p) if (p == (num_blocks - numba.uint8(1))): end = j + numba.uint64(last_block_size) else: end = j + numba.uint64(block_size) while ((j < end) and (i < block_end)): t1 = numba.uint16((input[i] & 0xF0) >> 4) t2 = numba.uint16((input[i] & 0x0F) + 4) i += numba.uint8(1) if (t1 == 15): while input[i] == 255: t1 += numba.uint8(input[i]) i += numba.uint8(1) t1 += numba.uint8(input[i]) i += numba.uint8(1) for n in range(t1): output[j] = input[i] i += numba.uint8(1) j += numba.uint8(1) if (j >= end): break off = numba.uint16(input[i]) + (numba.uint16(input[i + 1]) << 8) i += numba.uint8(2) if (t2 == 19): while input[i] == 255: t2 += numba.uint8(input[i]) i += numba.uint8(1) t2 += numba.uint8(input[i]) i += numba.uint8(1) for n in range(t2): output[j] = output[j - off] j += numba.uint8(1)
def rational2_2(image_array, topleft, xstride, ystride, lambd, julia, rot): y, x = cuda.grid(2) if x < image_array.shape[1] and y < image_array.shape[0]: z = complex128(topleft + x * xstride - 1j * y * ystride) * exp( 1j * rot) i = 0 while i < 1000 and z.real * z.real + z.imag * z.imag < 4: z = z * z - lambd / (z * z) + julia i += 1 k = real_log(float64(i)) / real_log(999.0) norm = z.real * z.real + z.imag * z.imag if norm > 4: l = uint16(real_log(z.real * z.real + z.imag * z.imag - 4) * 200) # if l>255: l = 255 image_array[y, x] = uint16(255 * k) * 256 - l
def buffered_uint16(bitgen, bcnt, buf): if not bcnt: buf = next_uint32(bitgen) bcnt = 1 else: buf >>= 16 bcnt -= 1 return uint16(buf), bcnt, buf
def unpack_10b(input, tone_map, output, offset=0, stride=1): # 10 bits data => 4 points packed into 5 bytes for block in numba.prange(len(input) // 5): i = block * 5 val0 = (numba.uint16(input[i+0] & 0xFF) << 2) + (numba.uint16(input[i+1]) >> 6) val1 = (numba.uint16(input[i+1] & 0x3F) << 4) + (numba.uint16(input[i+2]) >> 4) val2 = (numba.uint16(input[i+2] & 0x0F) << 6) + (numba.uint16(input[i+3]) >> 2) val3 = (numba.uint16(input[i+3] & 0x03) << 8) + (numba.uint16(input[i+4]) >> 0) j = offset + (block * 4 * stride) output[j ] = tone_map[val0] output[j + stride] = tone_map[val1] output[j + 2*stride] = tone_map[val2] output[j + 3*stride] = tone_map[val3]
from math import gcd from queue import Empty from dataclasses import asdict, is_dataclass from enum import Enum import ctypes import numpy as np from numba import vectorize, uint16 @vectorize([uint16(uint16, uint16)]) def neg_dif(x, y): """ Parameters ---------- x : y : Returns ------- """ if y < x: return x - y else: return 0 def lcm(a, b): """Return lowest common multiple.""" return a * b // gcd(a, b)
from numba import njit, uint16, complex64, float32, boolean from PIL import Image MAX_ITER = 100 def get_color(c): return (c, c, c) @njit(boolean(float32, float32)) def inside_cardioid(x0, y0): return (x0 - 0.25)**2 + y0**2 < (0.5 - 0.5 * cos(atan2(y0, x0 - 0.25)))**2 @njit(uint16(complex64, uint16)) def iteration(c, max_iter): z = 0 for i in range(1, max_iter): if z.real**2 + z.imag**2 > 4: return i z = z**2 + c return 0 @njit(uint16(complex64, uint16)) def mandelbrot(c, max_iter): if inside_cardioid(c.real, c.imag): return 0 return iteration(c, max_iter)