Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bitmaptools.arrayblit fix: make x2,y2 optional and update docstring #10257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025

Conversation

FoamyGuy
Copy link
Collaborator

This function's docstrings don't match its behavior in two ways.

  1. Currently if you pass -1 for x2 or y2 as specified in the docstring it will raise an exception ValueError: x2 must be 0-80

We could update the validation to accept -1 but this same validation helper in used in multiple other places and I'm not sure if all of them share the same support for and meaning of -1 value.

In any case the current code already provides None as a value that does make it get taken as the width and/or height of the image. I think there is no need to have two different argument values that lead to that same behavior. So I've just updated the docstring to indicate None instead of -1.

  1. if x2 and y2 are omitted as the docstrings say they can be the code raises an exception TypeError: 'x2' argument required. This is resolved by removing the ARG_REQUIRED flag from for if_required2 argument to the validation helper call.

Tested on a pyportal with this code:

import math

import bitmaptools
import supervisor
import displayio
import ulab.numpy as np

# Make the display context. Change size if you want
display = supervisor.runtime.display
display.auto_refresh = False

# Make the display context
main_group = displayio.Group(scale=4)
display.root_group = main_group

bmp = displayio.Bitmap(320//4, 240//4, 16)
palette = displayio.Palette(16)
palette[0] = 0x000000
palette[1] = 0xffffff
palette[2] = 0xff0000
palette[3] = 0x00ff00
palette[4] = 0x0000ff
palette[5] = 0xffff00
palette[6] = 0x00ffff
palette[7] = 0xff00ff

bmp.fill(3)

tg = displayio.TileGrid(bmp, pixel_shader=palette)
main_group.append(tg)
rows = []
for y in range(bmp.height):
    for x in range(bmp.width):
        rows.extend([(y*bmp.width + x + y) % 8])
array_2d = np.array(rows, dtype=np.uint8)
bitmaptools.arrayblit(bmp, array_2d)
while True:
    pass

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt tannewt merged commit 2693c85 into adafruit:main Apr 16, 2025
513 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants