-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmap.py
59 lines (47 loc) · 1.56 KB
/
bitmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#
# (C) Copyright 2024 Pavel Tisnovsky
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Pavel Tisnovsky
#
from typing import List, Tuple
from pygame import Surface
def compute_min_max(
bitmap: List[List[float]], width: int, height: int
) -> Tuple[float, float]:
# initial limits for pixel values normalization
min = float("inf")
max = float("-inf")
# retrieve minimal and maximal pixel values
for j in range(height):
for i in range(width):
z = bitmap[j][i]
if max < z:
max = z
if min > z:
min = z
return min, max
def create_bitmap(width: int, height: int) -> List[List[float]]:
return [[0 for x in range(width)] for y in range(height)]
def draw_bitmap(bitmap: List[List[float]], surface: Surface, max_factor: float) -> None:
print("contrast adjustment")
width, height = surface.get_size()
# retrieve minimal and maximal pixel values
min, max = compute_min_max(bitmap, width, height)
max *= max_factor
k = 255.0 / (max - min)
# contrast change
for y in range(height):
for x in range(width):
f = float(bitmap[y][x])
f -= min
f *= k
if f > 255.0:
f = 255
i = int(f) & 255
surface.set_at((x, y), i + (i << 8) + (i << 16))