-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.pre
More file actions
136 lines (109 loc) · 3.49 KB
/
Copy pathinit.pre
File metadata and controls
136 lines (109 loc) · 3.49 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
! Image loading helpers for Prefix
! Backed by image/image.c.
!
! The returned tensor layout is [column][row][channel] (width, height, channel)
! in user code. Channels are ordered r, g, b, a and values are 0..255.
EXTEND(EXTENSION: win32)
EXTEND(EXTENSION: image)
IMPORT(path)
FUNC TNS LOAD(STR img_path){
STR ext = path.EXTNAME(img_path)
IF(EQ(ext, "png")){
RETURN(LOAD_PNG(img_path))
} ELSEIF (OR(EQ(ext, "jpg"),EQ(ext, "jpeg"))){
RETURN(LOAD_JPEG(img_path))
} ELSEIF (EQ(ext, "bmp")){
RETURN(LOAD_BMP(img_path))
} ELSE {
THROW("Unsupported image format: ",ext)
}
}
FUNC INT WIDTH(TNS img){
RETURN(TLEN(img, 0d1))
}
FUNC INT HEIGHT(TNS img){
RETURN(TLEN(img, 0d2))
}
FUNC INT CHANNELS(TNS img){
RETURN(TLEN(img, 0d3))
}
FUNC TNS R(TNS img){
RETURN(img[*, *, 0d1])
}
FUNC TNS G(TNS img){
RETURN(img[*, *, 0d2])
}
FUNC TNS B(TNS img){
RETURN(img[*, *, 0d3])
}
FUNC TNS A(TNS img){
RETURN(img[*, *, 0d4])
}
FUNC TNS PIXEL(TNS img, INT x, INT y){
RETURN(img[x, y, *])
}
FUNC INT PIXEL_R(TNS img, INT x, INT y){
RETURN(img[x, y, 0d1])
}
FUNC INT PIXEL_G(TNS img, INT x, INT y){
RETURN(img[x, y, 0d2])
}
FUNC INT PIXEL_B(TNS img, INT x, INT y){
RETURN(img[x, y, 0d3])
}
FUNC INT PIXEL_A(TNS img, INT x, INT y){
RETURN(img[x, y, 0d4])
}
FUNC TNS FLIP_V(TNS img){
RETURN(TFLIP(img, 0d1))
}
FUNC TNS FLIP_H(TNS img){
RETURN(TFLIP(img, 0d2))
}
FUNC TNS INVERT(TNS img){
TNS return = MSUB(TNS(SHAPE(img), 0xFF), img)
return[*, *, 0d4] = img[*, *, 0d4] ! Preserve alpha
POP(return)
}
FUNC TNS RECT(TNS img, INT x, INT y, INT width, INT height, TNS color, INT fill, INT thickness){
TNS pts = [ ^
[x, y], ^
[ADD(x, SUB(width, 0d1)), y], ^
[ADD(x, SUB(width, 0d1)), ADD(y, SUB(height, 0d1))], ^
[x, ADD(y, SUB(height, 0d1))], ^
[x, y] ^
]
RETURN(POLYGON(img, pts, color, fill, thickness))
}
FUNC TNS RECTANGLE(TNS img, INT x, INT y, INT width, INT height, TNS color, INT fill, INT thickness){
RETURN(RECT(img, x, y, width, height, color, fill, thickness))
}
FUNC TNS FILL_RECT(TNS img, INT x, INT y, INT width, INT height, TNS color){
RETURN(RECT(img, x, y, width, height, color, 0d1, 0d1))
}
FUNC TNS FILL_ELLIPSE(TNS img, TNS center, INT rx, INT ry, TNS color){
RETURN(ELLIPSE(img, center, rx, ry, color, 0d1, 0d1))
}
FUNC TNS SQUARE(TNS img, INT x, INT y, INT size, TNS color, INT fill, INT thickness){
RETURN(RECT(img, x, y, size, size, color, fill, thickness))
}
FUNC TNS CIRCLE(TNS img, TNS center, INT radius, TNS color, INT fill, INT thickness){
RETURN(ELLIPSE(img, center, radius, radius, color, fill, thickness))
}
FUNC TNS CROP(TNS img, TNS corners){
IF(NEQ(SHAPE(corners), [0d4, 0d2])){
THROW("CROP corners must be a 0d4 by 0d2 tensor: [[tl_x, tl_y], [tr_x, tr_y], [bl_x, bl_y], [br_x, br_y]]")
}
RETURN(img[MIN(corners[*, 0d1]):MAX(corners[*, 0d1]), MIN(corners[*, 0d2]):MAX(corners[*, 0d2]), *])
}
FUNC BOOL SHOW(TNS img){
! Save the image to the provided path and open it with the system default
! viewer on Windows. `img_path` is treated as the target file path.
STR img_path = "C:/Windows/Temp/tmp_img.png"
SAVE_PNG(img, img_path, 0d0)
! ShellExecuteW(hwnd, operation, file, params, dir, showcmd)
! Use NULL hwnd (0), operation "open", empty params and dir, showcmd=1
win32.WIN_CALL("shell32", "ShellExecuteW", "PSSSSI", "P", 0d0, "open", img_path, "", "", 0d1)
DEL(img_path)
RETURN(FALSE)
}