diff --git a/draw_12_rects_and_send.py b/draw_12_rects_and_send.py index 23171de..330d872 100644 --- a/draw_12_rects_and_send.py +++ b/draw_12_rects_and_send.py @@ -1,12 +1,14 @@ #!/usr/bin/env python3 """ -draw_12_rects_and_send.py +draw_12_rects_thick_outline.py -* 800 × 600 framebuffer, 32‑bit ARGB (A,R,G,B each 1 byte) -* 12 rectangles, aspect ratio width : height = 1 : 12 (tall, upright) -* rectangles are placed side‑by‑side, centred in the image -* the complete framebuffer is sent over a TCP connection to - a server listening on localhost:12345 +800 × 600 ARGB framebuffer. +12 tall rectangles (aspect 1 : 12) placed side‑by‑side with a gap. +Only the rectangle outline is drawn, the border thickness can be changed +with the constant LINE_THICKNESS (default = 3 pixels). + +The complete framebuffer is streamed to a TCP server listening on +localhost:12345 (payload is prefixed with a 4‑byte big‑endian length). """ import socket @@ -22,54 +24,79 @@ BPP = 4 # bytes per pixel (A,R,G,B) # ---------------------------------------------------------------------- # Geometry of the 12 rectangles # ---------------------------------------------------------------------- -# Height of a rectangle must fill the whole image (600 px) -# width = height / 12 → 600 / 12 = 50 px -RECT_HEIGHT = FB_HEIGHT // 2 -RECT_WIDTH = RECT_HEIGHT // 12 # 50 -SPACING = 20 +RECT_HEIGHT = 400 # fill the whole image vertically +RECT_WIDTH = RECT_HEIGHT // 12 # 600 / 12 = 50 px (maintains 1:12) -TOTAL_RECT_W = (11 * SPACING) + (RECT_WIDTH * 12) # 600 -X0_START = (FB_WIDTH - TOTAL_RECT_W) // 2 # centre the strip +SPACING = 10 # pixels between two neighbours +TOTAL_RECT_W = 12 * RECT_WIDTH + 11 * SPACING +X0_START = (FB_WIDTH - TOTAL_RECT_W) // 2 # centre the whole strip + +# ---------------------------------------------------------------------- +# Outline thickness (change this to whatever you need) +# ---------------------------------------------------------------------- +LINE_THICKNESS = 4 # >=1 ; thicker than 1 pixel # ---------------------------------------------------------------------- # Helper – write a pixel into the bytearray (no bounds check for speed) # ---------------------------------------------------------------------- -def set_pixel(buf: bytearray, x: int, y: int, color: tuple): - """Write an ARGB pixel at (x,y). - - `color` is a 4‑tuple (A,R,G,B) with each component 0‑255. - """ +def set_pixel(buf: bytearray, x: int, y: int, color: tuple) -> None: + """Write an ARGB pixel at (x, y). `color` = (A,R,G,B).""" offset = (y * FB_WIDTH + x) * BPP - buf[offset:offset+4] = bytes(color) # A,R,G,B order + buf[offset:offset + 4] = bytes(color) # A,R,G,B order + + +# ---------------------------------------------------------------------- +# Fill a rectangular region (used for thick edges) +# ---------------------------------------------------------------------- +def fill_rect(buf: bytearray, x0: int, y0: int, w: int, h: int, color: tuple) -> None: + """Write a solid w × h block of `color` starting at (x0, y0).""" + # Clip to the frame buffer – safety net for very thick lines at the border + x0 = max(0, x0) + y0 = max(0, y0) + w = min(w, FB_WIDTH - x0) + h = min(h, FB_HEIGHT - y0) + + row_bytes = w * BPP + pixel_bytes = bytes(color) + + for y in range(y0, y0 + h): + off = (y * FB_WIDTH + x0) * BPP + buf[off:off + row_bytes] = pixel_bytes * w # ---------------------------------------------------------------------- # Build the framebuffer # ---------------------------------------------------------------------- def build_framebuffer() -> bytearray: - """Create an ARGB buffer, fill it black and draw the 12 rectangles.""" + """Create an ARGB buffer, fill it black and draw the thick‑outline rectangles.""" + # 1️⃣ background = opaque black (A,R,G,B) fb = bytearray(FB_WIDTH * FB_HEIGHT * BPP) - - # ------------------------------------------------------------------ - # 1) background = solid black, fully opaque - # ------------------------------------------------------------------ - bg_color = (255, 0, 0, 0) # A,R,G,B - # Fill the whole buffer in one go (faster than per‑pixel loop) + bg_color = (255, 0, 0, 0) # opaque black fb[:] = bg_color * (FB_WIDTH * FB_HEIGHT) - # ------------------------------------------------------------------ - # 2) draw the 12 rectangles - # ------------------------------------------------------------------ + # 2️⃣ draw each rectangle outline for idx in range(12): - rect_color = (255, 255, 255, 255) # opaque + # ── colour palette (feel free to change) ─────────────────────── + rect_color = (255, 255, 255, 255) # opaque - x_start = X0_START + idx * (RECT_WIDTH + SPACING) - x_end = x_start + RECT_WIDTH # exclusive + # left‑most X coordinate of the *inner* rectangle (the border belongs to it) + x0 = X0_START + idx * (RECT_WIDTH + SPACING) + y0 = 100 + w = RECT_WIDTH + h = RECT_HEIGHT - for y in range(FB_HEIGHT): - # slice‑assignment is faster than per‑pixel calls - row_offset = (y * FB_WIDTH + x_start) * BPP - fb[row_offset:row_offset + RECT_WIDTH * BPP] = rect_color * RECT_WIDTH + # ── thick edges ────────────────────────────────────────────────── + # top edge + fill_rect(fb, x0, y0, w, LINE_THICKNESS, rect_color) + + # bottom edge + fill_rect(fb, x0, y0 + h - LINE_THICKNESS, w, LINE_THICKNESS, rect_color) + + # left edge + fill_rect(fb, x0, y0, LINE_THICKNESS, h, rect_color) + + # right edge + fill_rect(fb, x0 + w - LINE_THICKNESS, y0, LINE_THICKNESS, h, rect_color) return fb @@ -77,22 +104,24 @@ def build_framebuffer() -> bytearray: # ---------------------------------------------------------------------- # Send the buffer to the TCP server # ---------------------------------------------------------------------- -def send_framebuffer(buf: bytearray, host: str = "127.0.0.1", port: int = 12345): +def send_framebuffer(buf: bytearray, + host: str = "127.0.0.1", + port: int = 12345) -> None: """Open a TCP socket, connect, and stream the whole framebuffer.""" try: with socket.create_connection((host, port), timeout=5) as sock: - # Optional: prefix the payload with its length (big‑endian uint32) + # optional length prefix (many protocols expect it) length_prefix = len(buf).to_bytes(4, byteorder="big") sock.sendall(length_prefix + buf) - print(f"Sent {len(buf)} bytes to {host}:{port}") + print(f"✔ Sent {len(buf)} bytes to {host}:{port}") except Exception as exc: - print(f"Could not send framebuffer: {exc}", file=sys.stderr) + print(f"❌ Could not send framebuffer: {exc}", file=sys.stderr) # ---------------------------------------------------------------------- # Main entry point # ---------------------------------------------------------------------- -def main(): +def main() -> None: fb = build_framebuffer() send_framebuffer(fb)