diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 57ac779081..ec26eab98e 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -664,7 +664,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_readinto_obj, 0, bitmaptools_readinto); //| """The Floyd-Stenberg dither""" //| MAKE_ENUM_VALUE(bitmaptools_dither_algorithm_type, dither_algorithm, Atkinson, DITHER_ALGORITHM_ATKINSON); -MAKE_ENUM_VALUE(bitmaptools_dither_algorithm_type, dither_algorithm, FloydStenberg, DITHER_ALGORITHM_ATKINSON); +MAKE_ENUM_VALUE(bitmaptools_dither_algorithm_type, dither_algorithm, FloydStenberg, DITHER_ALGORITHM_FLOYD_STENBERG); MAKE_ENUM_MAP(bitmaptools_dither_algorithm) { MAKE_ENUM_MAP_ENTRY(dither_algorithm, Atkinson), diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 80b4a38964..6b5def2904 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -749,9 +749,9 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi int x1 = x + info->terms[i].dx; int dy = info->terms[i].dy; - rows[dy][x1] = ((info->terms[i].dl * err) >> 8) + rows[dy][x1]; + rows[dy][x1] = ((info->terms[i].dl * err) / 256) + rows[dy][x1]; } - err = err * info->dl >> 8; + err = (err * info->dl) / 256; } write_pixels(dest_bitmap, y, out); @@ -761,13 +761,13 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi rows[1] = rows[2]; rows[2] = tmp; - fill_row(source_bitmap, swap, rows[2], y + 2, info->mx); - y++; if (y == height) { break; } + fill_row(source_bitmap, swap, rows[2], y + 2, info->mx); + // Serpentine dither. Going right-to-left... for (int x = width; x--;) { int16_t pixel_in = rows[0][x] + err; @@ -779,9 +779,9 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi int x1 = x - info->terms[i].dx; int dy = info->terms[i].dy; - rows[dy][x1] = ((info->terms[i].dl * err) >> 8) + rows[dy][x1]; + rows[dy][x1] = ((info->terms[i].dl * err) / 256) + rows[dy][x1]; } - err = err * info->dl >> 8; + err = (err * info->dl) / 256; } write_pixels(dest_bitmap, y, out); @@ -790,7 +790,7 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi rows[1] = rows[2]; rows[2] = tmp; - fill_row(source_bitmap, swap, rows[2], y + 2, info->mx); + fill_row(source_bitmap, swap, rows[2], y + 3, info->mx); } displayio_area_t a = { 0, 0, width, height, NULL }; diff --git a/tests/circuitpython-manual/_bmp16.py b/tests/circuitpython-manual/bitmaptools/_bmp16.py similarity index 100% rename from tests/circuitpython-manual/_bmp16.py rename to tests/circuitpython-manual/bitmaptools/_bmp16.py diff --git a/tests/circuitpython-manual/blend.py b/tests/circuitpython-manual/bitmaptools/blend.py similarity index 100% rename from tests/circuitpython-manual/blend.py rename to tests/circuitpython-manual/bitmaptools/blend.py diff --git a/tests/circuitpython-manual/blinka16.bmp b/tests/circuitpython-manual/bitmaptools/blinka16.bmp similarity index 100% rename from tests/circuitpython-manual/blinka16.bmp rename to tests/circuitpython-manual/bitmaptools/blinka16.bmp diff --git a/tests/circuitpython-manual/bitmaptools/dither-atkinson.bmp b/tests/circuitpython-manual/bitmaptools/dither-atkinson.bmp new file mode 100644 index 0000000000..1829b8e7ae Binary files /dev/null and b/tests/circuitpython-manual/bitmaptools/dither-atkinson.bmp differ diff --git a/tests/circuitpython-manual/bitmaptools/dither-floydstenberg.bmp b/tests/circuitpython-manual/bitmaptools/dither-floydstenberg.bmp new file mode 100644 index 0000000000..e7481b0406 Binary files /dev/null and b/tests/circuitpython-manual/bitmaptools/dither-floydstenberg.bmp differ diff --git a/tests/circuitpython-manual/bitmaptools/dither.py b/tests/circuitpython-manual/bitmaptools/dither.py new file mode 100644 index 0000000000..13d0a5be6d --- /dev/null +++ b/tests/circuitpython-manual/bitmaptools/dither.py @@ -0,0 +1,18 @@ +import bitmaptools +import displayio +import _bmp16 + +if "/" in __file__: + here = __file__.rsplit("/", 1)[0] +else: + here = "." + +c = displayio.Colorspace.BGR565 + +b1 = _bmp16.loadbmp16(here + "/minerva16.bmp") +b3 = displayio.Bitmap(320, 240, 65536) + +bitmaptools.dither(b3, b1, c) +_bmp16.writebmp16(f"dither-atkinson.bmp", b3) +bitmaptools.dither(b3, b1, c, bitmaptools.DitherAlgorithm.FloydStenberg) +_bmp16.writebmp16(f"dither-floydstenberg.bmp", b3) diff --git a/tests/circuitpython-manual/bitmaptools/ellipse.bmp b/tests/circuitpython-manual/bitmaptools/ellipse.bmp new file mode 100644 index 0000000000..c048cb17c7 Binary files /dev/null and b/tests/circuitpython-manual/bitmaptools/ellipse.bmp differ diff --git a/tests/circuitpython-manual/minerva16.bmp b/tests/circuitpython-manual/bitmaptools/minerva16.bmp similarity index 100% rename from tests/circuitpython-manual/minerva16.bmp rename to tests/circuitpython-manual/bitmaptools/minerva16.bmp diff --git a/tests/circuitpython-manual/dither.py b/tests/circuitpython-manual/dither.py deleted file mode 100644 index 49a63c458c..0000000000 --- a/tests/circuitpython-manual/dither.py +++ /dev/null @@ -1,31 +0,0 @@ -import bitmaptools -import displayio -import _bmp16 - -if "/" in __file__: - here = __file__.rsplit("/", 1)[0] -else: - here = "." - -c = displayio.Colorspace.BGR565 - -b1 = _bmp16.loadbmp16(here + "/minerva16.bmp") -b3 = displayio.Bitmap(320, 240, 65536) - -for i in ( - 0, - 1 / 64, - 3 / 64, - 3 / 32, - 3 / 16, - 0.5, - 1 - 3 / 16, - 1 - 3 / 32, - 1 - 3 / 64, - 1 - 1 / 64, - 1, -): - bitmaptools.dither(b3, b1, c) - _bmp16.writebmp16(f"dither-atkinson.bmp", b3) - bitmaptools.dither(b3, b1, c, bitmaptools.DitherAlgorithm.FloydStenberg) - _bmp16.writebmp16(f"dither-floydstenberg.bmp", b3)