Skip to content

Commit 7e1c05e

Browse files
committed
morph: accommodate fractional add values
1 parent e02c72b commit 7e1c05e

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

shared-bindings/bitmapfilter/__init__.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//| bitmap: displayio.Bitmap,
3636
//| weights: Sequence[int],
3737
//| mul: float = 1.0,
38-
//| add: int = 0,
38+
//| add: float = 0,
3939
//| mask: displayio.Bitmap | None = None,
4040
//| threshold=False,
4141
//| offset: int = 0,
@@ -112,7 +112,7 @@ STATIC mp_obj_t bitmapfilter_morph(size_t n_args, const mp_obj_t *pos_args, mp_m
112112
mask = MP_OBJ_TO_PTR(args[ARG_mask].u_obj);
113113
}
114114

115-
mp_int_t b = mp_obj_get_int(args[ARG_add].u_obj);
115+
mp_float_t b = mp_obj_get_float(args[ARG_add].u_obj);
116116

117117
mp_obj_t weights = args[ARG_weights].u_obj;
118118
mp_obj_t obj_len = mp_obj_len(weights);
@@ -137,7 +137,7 @@ STATIC mp_obj_t bitmapfilter_morph(size_t n_args, const mp_obj_t *pos_args, mp_m
137137

138138
mp_float_t m = args[ARG_mul].u_obj != mp_const_none ? mp_obj_get_float(args[ARG_mul].u_obj) : 1 / (mp_float_t)weight_sum;
139139

140-
shared_module_bitmapfilter_morph(bitmap, mask, sq_n_weights / 2, iweights, (float)m, b,
140+
shared_module_bitmapfilter_morph(bitmap, mask, sq_n_weights / 2, iweights, m, b,
141141
args[ARG_threshold].u_bool, args[ARG_offset].u_bool, args[ARG_invert].u_bool);
142142
return mp_const_none;
143143
}

shared-bindings/bitmapfilter/__init__.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ void shared_module_bitmapfilter_morph(
3333
displayio_bitmap_t *mask,
3434
const int ksize,
3535
const int *krn,
36-
const float m,
37-
const int b,
36+
const mp_float_t m,
37+
const mp_float_t b,
3838
bool threshold,
3939
int offset,
4040
bool invert);

shared-module/bitmapfilter/__init__.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,16 @@ void shared_module_bitmapfilter_morph(
140140
displayio_bitmap_t *mask,
141141
const int ksize,
142142
const int *krn,
143-
const float m,
144-
const int b,
143+
const mp_float_t m,
144+
const mp_float_t b,
145145
bool threshold,
146146
int offset,
147147
bool invert) {
148148

149149
int brows = ksize + 1;
150150

151151
const int32_t m_int = (int32_t)MICROPY_FLOAT_C_FUN(round)(65536 * m);
152+
const int32_t b_int = (int32_t)MICROPY_FLOAT_C_FUN(round)(65536 * COLOR_G6_MAX * b);
152153

153154
check_matching_details(bitmap, bitmap);
154155

@@ -169,7 +170,7 @@ void shared_module_bitmapfilter_morph(
169170
continue; // Short circuit.
170171

171172
}
172-
int32_t tmp, r_acc = 0, g_acc = 0, b_acc = 0, ptr = 0;
173+
int32_t r_acc = 0, g_acc = 0, b_acc = 0, ptr = 0;
173174

174175
if (x >= ksize && x < bitmap->width - ksize && y >= ksize && y < bitmap->height - ksize) {
175176
for (int j = -ksize; j <= ksize; j++) {
@@ -194,22 +195,19 @@ void shared_module_bitmapfilter_morph(
194195
}
195196
}
196197
}
197-
tmp = (r_acc * m_int) >> 16;
198-
r_acc = tmp + b;
198+
r_acc = (r_acc * m_int + b_int) >> 16;
199199
if (r_acc > COLOR_R5_MAX) {
200200
r_acc = COLOR_R5_MAX;
201201
} else if (r_acc < 0) {
202202
r_acc = 0;
203203
}
204-
tmp = (g_acc * m_int) >> 16;
205-
g_acc = tmp + b;
204+
g_acc = (g_acc * m_int + b_int * 2) >> 16;
206205
if (g_acc > COLOR_G6_MAX) {
207206
g_acc = COLOR_G6_MAX;
208207
} else if (g_acc < 0) {
209208
g_acc = 0;
210209
}
211-
tmp = (b_acc * m_int) >> 16;
212-
b_acc = tmp + b;
210+
b_acc = (b_acc * m_int + b_int) >> 16;
213211
if (b_acc > COLOR_B5_MAX) {
214212
b_acc = COLOR_B5_MAX;
215213
} else if (b_acc < 0) {

tests/circuitpython/bitmapfilter_morph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ def make_quadrant_bitmap():
3232
b = make_circle_bitmap()
3333
q = make_quadrant_bitmap()
3434
dump_bitmap(q)
35-
bitmapfilter.morph(b, mask=q, weights=blur, add=32)
35+
bitmapfilter.morph(b, mask=q, weights=blur, add=1 / 4)
3636
dump_bitmap(b)
3737

3838
# This is a kind of edge filter
3939
b = make_circle_bitmap()
40-
bitmapfilter.morph(b, weights=sharpen, threshold=True, add=8, invert=True)
40+
bitmapfilter.morph(b, weights=sharpen, threshold=True, add=0.125, invert=True)
4141
dump_bitmap(b)

0 commit comments

Comments
 (0)