-
Notifications
You must be signed in to change notification settings - Fork 1
/
FPPixelContainer.py
100 lines (83 loc) · 3.68 KB
/
FPPixelContainer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import FreeCAD
import numpy as np
from array import array
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
def convertToRGBAArray(RGBint):
Blue = RGBint & 255
Green = (RGBint >> 8) & 255
Red = (RGBint >> 16) & 255
return (Red, Green, Blue, 0xff)
class PixelContainer:
NUM_COLOR_COMPONENTS = 4
def __init__(self, resolutionX, resolutionY):
self.image = Image.new(mode="RGBX", size=(resolutionX, resolutionY))
self.draw = ImageDraw.Draw(self.image)
self.font = ImageFont.truetype("truetype/ttf-bitstream-vera/VeraIt.ttf", 32)
self.modified = False
def _colToTup(self, color):
return (color.r, color.g, color.b, color.a)
def clear(self, color):
self.modified = True
if not color:
col = (0, 0, 0, 255)
else:
col = self._colToTup(color)
(x,y) = self.image.size
self.image.paste( (col[0], col[1], col[2], col[3]), [0, 0, x, y] )
def setPixel_ARGB32(self, pixel):
self.modified = True
self.image.putpixel((pixel.pos.x, pixel.pos.y), convertToRGBAArray(pixel.rgba))
# interpret subwindow as closed interval on both sides
def setSubWindowPixels_ARGB32(self, subWindowData):
self.modified = True
xmin = min(subWindowData.p1.x, subWindowData.p2.x)
xmax = max(subWindowData.p1.x, subWindowData.p2.x)
ymin = min(subWindowData.p1.y, subWindowData.p2.y)
ymax = max(subWindowData.p1.y, subWindowData.p2.y)
#FreeCAD.Console.PrintMessage("Type is" + str(type(subWindowData.rgbaStream)) + "\n")
#0xRRGGBBff
buf = array('I')
for rgba32 in subWindowData.rgbaStream:
#FreeCAD.Console.PrintMessage("r" + str( (rgba32 >> 24) & 0xff ) +
# "g" + str( (rgba32 >> 16) & 0xff ) +
# "b" + str( (rgba32 >> 8) & 0xff ) + "\n")
buf.append(rgba32)
assert(buf.itemsize == 4)
im = Image.frombuffer("RGBX", (xmax - xmin + 1, ymax - ymin + 1), bytes(buf), 'raw', "BGRX", 0, 1)
self.image.paste(im, (xmin, ymin))
def drawRectangle(self, rectangle):
self.modified = True
col = self._colToTup(rectangle.pixelColor)
fill = None
if rectangle.filled:
fill = col
self.draw.rectangle( [ (rectangle.p1.x, rectangle.p1.y), (rectangle.p2.x, rectangle.p2.y) ], fill = fill, outline = col )
def drawLine(self, lineData):
self.modified = True
col = self._colToTup(lineData.pixelColor)
self.draw.line([ (lineData.p1.x, lineData.p1.y), (lineData.p2.x, lineData.p2.y) ], fill = col)
def toString(self):
return self.image.transpose(Image.FLIP_TOP_BOTTOM).tobytes()
def setActiveFont(self, fontData):
try:
self.font = ImageFont.truetype(fontData.path, fontData.size)
except IOError:
FreeCAD.Console.PrintError("Could not open font file: " + fontData.path + "\n")
self.font = ImageFont.truetype("truetype/ttf-bitstream-vera/VeraIt.ttf", fontData.size)
def drawText(self, textData):
self.modified = True
col = self._colToTup(textData.color)
self.draw.text((textData.pos.x, textData.pos.y), textData.text, font = self.font, fill=col)
def getTextSize(self, txt, fontData):
if fontData.path:
font = ImageFont.truetype(fontData.path, fontData.size)
return self.draw.textsize(text = txt, font = font)
else:
return self.draw.textsize(text = txt, font = self.font)
def dirty(self):
if self.modified:
self.modified = False
return True
return False