diff --git a/pixelflut.py b/pixelflut.py index 1f3c0d6..c2b31fd 100755 --- a/pixelflut.py +++ b/pixelflut.py @@ -3,19 +3,16 @@ import socket import PIL from PIL import Image, ImageColor import argparse -import threading +import multiprocessing +import random +from pprint import pprint from sys import exit sizex = 800 sizey = 600 -XSPLIT = 5 -YSPLIT = 5 - threads = [] -dirtybox = (0,0,sizex,sizey) - old_framebuffer = Image.new("RGBA", (sizex, sizey), None) framebuffer = Image.new("RGBA", (sizex, sizey), (0,0,0,0)) @@ -31,15 +28,29 @@ effect = parser.add_mutually_exclusive_group(required=False) effect.add_argument("--repeat", action="store_true") effect.add_argument("--animate", type=int, const=5, nargs='?', metavar="STEPSIZE") parser.add_argument("--bounce", action="store_true") +parser.add_argument("--debug", action="store_true") +size = parser.add_mutually_exclusive_group(required=False) +size.add_argument("--size", default=[800, 600], type=int, nargs=2) +size.add_argument("--autodetect", action="store_true") +parser.add_argument("--threads", type=int, default="5") args = parser.parse_args() +pprint(args) + +sizex = args.size[0] +sizey = args.size[1] + +dirtybox = (0,0,sizex,sizey) + +XSPLIT = args.threads +YSPLIT = args.threads + if args.bounce and not args.animate: print("--bounce requires --animate") exit(1) - -class DrawThread(threading.Thread): +class DrawThread(multiprocessing.Process): def run(self): xfrom = self._kwargs["xfrom"] yfrom = self._kwargs["yfrom"] @@ -51,11 +62,18 @@ class DrawThread(threading.Thread): buf = bytearray() global framebuffer - for x in range(xfrom,xto): - for y in range(yfrom,yto): - if framebuffer.getpixel((x,y))[3] != 0: - value = b"%02X%02X%02X%02X"%framebuffer.getpixel((x,y)) - buf += b"PX %d %d %b\n"%(x+args.xoffset,y+args.yoffset,value) + if args.debug: + for x in range(xfrom,xto): + for y in range(yfrom,yto): + if framebuffer.getpixel((x,y))[3] != 0: + value = b"%02X%02X%02X%02X"%self._kwargs["color"] + buf += b"PX %d %d %b\n"%(x+args.xoffset,y+args.yoffset,value) + else: + for x in range(xfrom,xto): + for y in range(yfrom,yto): + if framebuffer.getpixel((x,y))[3] != 0: + value = b"%02X%02X%02X%02X"%framebuffer.getpixel((x,y)) + buf += b"PX %d %d %b\n"%(x+args.xoffset,y+args.yoffset,value) s.send(buf) while args.repeat: @@ -109,7 +127,11 @@ def draw(): for xchunk in xchunks: for ychunk in ychunks: - threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1]})) + if args.debug: + color = (random.randint(0,255), random.randint(0,255), random.randint(0,255), 255) + threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1], "color": color})) + else: + threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1]})) for thread in threads: thread.start()