-
Notifications
You must be signed in to change notification settings - Fork 1
/
cg_cli.py
121 lines (112 loc) · 4.58 KB
/
cg_cli.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import os
import cg_algorithms as alg
import numpy as np
from PIL import Image
if __name__ == '__main__':
input_file = sys.argv[1]
output_dir = sys.argv[2]
os.makedirs(output_dir, exist_ok=True)
item_dict = {}
pen_color = np.zeros(3, np.uint8)
width = 0
height = 0
with open(input_file, 'r') as fp:
line = fp.readline()
while line:
line = line.strip().split(' ')
if line[0] == 'resetCanvas':
width = int(line[1])
height = int(line[2])
item_dict.clear()
elif line[0] == 'saveCanvas':
save_name = line[1]
canvas = np.zeros([height, width, 3], np.uint8)
canvas.fill(255)
for item_type, p_list, algorithm, color in item_dict.values():
if item_type == 'line':
pixels = alg.draw_line(p_list, algorithm)
for x, y in pixels:
canvas[y, x] = color
elif item_type == 'polygon':
pixels = alg.draw_polygon(p_list, algorithm)
for x, y in pixels:
canvas[y, x] = color
elif item_type == 'ellipse':
pixels = alg.draw_ellipse(p_list)
for x, y in pixels:
canvas[y, x] = color
elif item_type == 'curve':
pixels = alg.draw_curve(p_list, algorithm)
for x, y in pixels:
canvas[y, x] = color
Image.fromarray(canvas).save(os.path.join(output_dir, save_name + '.bmp'), 'bmp')
elif line[0] == 'setColor':
pen_color[0] = int(line[1])
pen_color[1] = int(line[2])
pen_color[2] = int(line[3])
elif line[0] == 'drawLine':
item_id = line[1]
x0 = int(line[2])
y0 = int(line[3])
x1 = int(line[4])
y1 = int(line[5])
algorithm = line[6]
item_dict[item_id] = ['line', [[x0, y0], [x1, y1]], algorithm, np.array(pen_color)]
elif line[0] == 'drawPolygon':
item_id = line[1]
p_list = []
n=len(line)
for i in range(1, int(n/2)):
p_list.append((int(line[2*i]),int(line[2*i+1])))
algorithm=line[n-1]
item_dict[item_id] = ['polygon', p_list, algorithm, np.array(pen_color)]
elif line[0] == 'drawEllipse':
item_id = line[1]
x0 = int(line[2])
y0 = int(line[3])
x1 = int(line[4])
y1 = int(line[5])
item_dict[item_id] = ['ellipse', [[x0, y0], [x1, y1]], None, np.array(pen_color)]
elif line[0] == 'drawCurve':
item_id = line[1]
p_list = []
n=len(line)
for i in range(1, int(n/2)):
p_list.append((int(line[2*i]),int(line[2*i+1])))
algorithm=line[n-1]
item_dict[item_id] = ['curve', p_list, algorithm, np.array(pen_color)]
elif line[0] == 'translate':
item_id = line[1]
dx = int(line[2])
dy = int(line[3])
p_list = item_dict[item_id][1]
p_list = alg.translate(p_list, dx, dy)
item_dict[item_id][1] = p_list
elif line[0] == 'rotate':
item_id = line[1]
x = int(line[2])
y = int(line[3])
r = int(line[4])
p_list = item_dict[item_id][1]
p_list = alg.rotate(p_list, x, y, r)
item_dict[item_id][1] = p_list
elif line[0] == 'scale':
item_id = line[1]
x = int(line[2])
y = int(line[3])
s = float(line[4])
p_list = item_dict[item_id][1]
item_dict[item_id][1] = alg.scale(p_list, x, y, s)
elif line[0] == 'clip':
item_id = line[1]
x0 = int(line[2])
y0 = int(line[3])
x1 = int(line[4])
y1 = int(line[5])
algorithm = line[6]
p_list = item_dict[item_id][1]
item_dict[item_id][1] = alg.clip(p_list, x0, y0, x1, y1, algorithm)
line = fp.readline()