morph: accommodate fractional add values · csamuelson/circuitpython@7e1c05e · GitHub
Skip to content

Commit 7e1c05e

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

4 files changed

Lines changed: 14 additions & 16 deletions

File tree

shared-bindings/bitmapfilter/__init__.c

Lines changed: 3 additions & 3 deletions

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

0 commit comments

Comments
 (0)