"""Desktop image lab for the OLED Endless Landscape detour.

Lesson: https://mdm508.github.io/post/the-endless-landscape/#q6-encode-and-preview
Run ON YOUR COMPUTER with desktop Python, not on the Pico.
Install Pillow in that interpreter: python -m pip install Pillow
The separate Pico lab needs ssd1306.py in the same folder ON THE PICO.

Only the import and readiness message run. Uncomment the supplied helper,
write your two functions, then uncomment one test block at a time.
Use opaque black/white artwork, one still PNG, height divisible by eight.
The final landscape must be 64 high and at least 128 wide.
"""

from PIL import Image

print("Desktop image lab ready. Read Q6; uncomment the helper and add your code.")

# SUPPLIED file-opening/validation helper. Uncomment it as a complete block.
# It deliberately rejects transparency and grey/color pixels instead of
# silently choosing a threshold or changing your artwork.
# def read_artwork(filename):
#     with Image.open(filename) as original:
#         image = original.convert("RGBA")
#     width, height = image.size
#     if width <= 0 or height <= 0 or height % 8:
#         raise ValueError("Height must be a positive multiple of eight")
#     allowed = ((0, 0, 0, 255), (255, 255, 255, 255))
#     for y in range(height):
#         for x in range(width):
#             if image.getpixel((x, y)) not in allowed:
#                 raise ValueError("Use opaque black and white pixels only")
#     return image

# YOUR IMPLEMENTATIONS. Consult the page/bit mapping from Phase 3.
# def convert_image(filename):
#     """Return (bytearray, width, height) in MONO_VLSB order."""
#     pass
#
# def preview_converted(data, width, height):
#     """Return an RGB Pillow image decoded from the bytes; validate size."""
#     pass

# TEST 1: predict the six bytes BEFORE writing down the expected answer.
# tiny = Image.new("RGBA", (3, 16), (0, 0, 0, 255))
# for point in ((0, 0), (1, 7), (2, 8), (0, 15)):
#     tiny.putpixel(point, (255, 255, 255, 255))
# tiny.save("tiny-test.png")
# data, width, height = convert_image("tiny-test.png")
# print("Tiny bytes:", [hex(value) for value in data])
# Add an assertion against your hand-calculated six bytes.
# decoded = preview_converted(data, width, height)
# assert decoded.tobytes() == tiny.convert("RGB").tobytes()

# TEST 2: run after independent known-byte checks pass.
# data, width, height = convert_image("landscape.png")
# if height != 64 or width < 128:
#     raise ValueError("Use a world at least 128 wide and exactly 64 high")
# decoded = preview_converted(data, width, height)
# assert decoded.tobytes() == read_artwork("landscape.png").convert("RGB").tobytes()
# with open("landscape.bin", "wb") as file:
#     file.write(data)
# decoded.resize((width * 3, height * 3), Image.Resampling.NEAREST).save("landscape-preview.png")
# print("World width:", width, "Bytes:", len(data))
# Upload only landscape.bin to the Pico for this project. Set WORLD_WIDTH
# there to the printed width. Keep PNG/preview and this lab on your computer.
