-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathx20.lua
More file actions
284 lines (221 loc) · 6.11 KB
/
Copy pathx20.lua
File metadata and controls
284 lines (221 loc) · 6.11 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
--[[
plimage demo
--]]
-- initialise Lua bindings for PLplot examples.
dofile("plplot_examples.lua")
XDIM = 260
YDIM = 220
dbg = 0
nosombrero = 0
nointeractive = 0
f_name=""
-- Transformation function
function mypltr(x, y)
local x0 = (stretch["xmin"] + stretch["xmax"])*0.5
local y0 = (stretch["ymin"] + stretch["ymax"])*0.5
local dy = (stretch["ymax"]-stretch["ymin"])*0.5
local tx = x0 + (x0-x)*(1 - stretch["stretch"]*math.cos((y-y0)/dy*math.pi*0.5))
local ty = y
return tx, ty
end
-- read image from file in binary ppm format
function read_img(fname)
-- naive grayscale binary ppm reading. If you know how to, improve it
local fp = io.open(fname, "rb")
if fp==nil then
return 1
end
-- version
local ver = fp:read("*line")
if ver~="P5" then -- I only understand this!
fp:close()
return 1
end
while fp:read(1)=="#" do
local com = fp:read("*line")
if com==nil then
fp:close()
return 1
end
end
fp:seek("cur", -1)
local w, h, num_col = fp:read("*number", "*number", "*number")
if w==nil or h==nil or num_col==nil then -- width, height, num colors
fp:close()
return 1
end
-- read the rest of the line (only EOL)
fp:read("*line")
local img = fp:read(w*h)
fp:close()
if string.len(img)~=(w*h) then
return 1
end
local imf = {}
for i = 1, w do
imf[i] = {}
for j = 1, h do
imf[i][j] = string.byte(img, (h-j)*w+i) -- flip image up-down
end
end
return 0, imf, w, h, num_col
end
-- save plot
function save_plot(fname)
local cur_strm = pl.gstrm() -- get current stream
local new_strm = pl.mkstrm() -- create a new one
pl.sdev("psc") -- new device type. Use a known existing driver
pl.sfnam(fname) -- file name
pl.cpstrm(cur_strm, 0) -- copy old stream parameters to new stream
pl.replot() -- do the save
pl.end1() -- close new device
pl.sstrm(cur_strm) -- and return to previous one
end
-- get selection square interactively
function get_clip(xi, xe, yi, ye)
return 0, xi, xe, yi, ye
end
-- set gray colormap
function gray_cmap(num_col)
local r = { 0, 1 }
local g = { 0, 1 }
local b = { 0, 1 }
local pos = { 0, 1 }
pl.scmap1n(num_col)
pl.scmap1l(1, pos, r, g, b)
end
x = {}
y = {}
z = {}
r = {}
img_f = {}
cgrid2 = {}
-- Parse and process command line arguments
pl.parseopts(arg, pl.PL_PARSE_FULL)
-- Initialize plplot
pl.init()
z={}
-- view image border pixels
if dbg~=0 then
pl.env(1, XDIM, 1, YDIM, 1, 1) -- no plot box
-- build a one pixel square border, for diagnostics
for i = 1, XDIM do
z[i] = {}
z[i][1] = 1 -- left
z[i][YDIM] = 1 -- right
end
for i = 1, YDIM do
z[1][i] = 1 -- top
z[XDIM][i] = 1 -- botton
end
pl.lab("...around a blue square."," ","A red border should appear...")
pl.image(z, 1, XDIM, 1, YDIM, 0, 0, 1, XDIM, 1, YDIM)
end
-- sombrero-like demo
if nosombrero==0 then
r = {}
pl.col0(2) -- draw a yellow plot box, useful for diagnostics! :(
pl.env(0, 2*math.pi, 0, 3*math.pi, 1, -1)
for i = 1, XDIM do
x[i] = (i-1)*2*math.pi/(XDIM-1)
end
for i = 1, YDIM do
y[i] = (i-1)*3*math.pi/(YDIM-1)
end
for i = 1, XDIM do
r[i] = {}
z[i] = {}
for j=1, YDIM do
r[i][j] = math.sqrt(x[i]^2+y[j]^2)+1e-3
z[i][j] = math.sin(r[i][j])/r[i][j]
end
end
pl.lab("No, an amplitude clipped \"sombrero\"", "", "Saturn?")
pl.ptex(2, 2, 3, 4, 0, "Transparent image")
pl.image(z, 0, 2*math.pi, 0, 3*math.pi, 0.05, 1, 0, 2*math.pi, 0, 3*math.pi)
-- save the plot
if f_name~="" then
save_plot(f_name)
end
end
-- read Chloe image
-- Note we try two different locations to cover the case where this
-- examples is being run from the test_c.sh script
status, img_f, width, height, num_col = read_img("Chloe.pgm")
if status~=0 then
status, img_f, width, height, num_col = read_img("../Chloe.pgm")
if status~=0 then
pl.abort("No such file")
pl.plend()
os.exit()
end
end
-- set gray colormap
gray_cmap(num_col)
-- display Chloe
pl.env(1, width, 1, height, 1, -1)
if nointeractive==0 then
pl.lab("Set and drag Button 1 to (re)set selection, Button 2 to finish."," ","Chloe...")
else
pl.lab(""," ","Chloe...")
end
pl.image(img_f, 1, width, 1, height, 0, 0, 1, width, 1, height)
-- selection/expansion demo
if nointeractive==0 then
xi = 25
xe = 130
yi = 235
ye = 125
status, xi, xe, yi, ye = get_clip(xi, xe, yi, ye)
if status~=0 then -- get selection rectangle
pl.plend()
os.exit()
end
pl.spause(0)
pl.adv(0)
-- display selection only
pl.image(img_f, 1, width, 1, height, 0, 0, xi, xe, ye, yi)
pl.spause(1)
-- zoom in selection
pl.env(xi, xe, ye, yi, 1, -1)
pl.image(img_f, 1, width, 1, height, 0, 0, xi, xe, ye, yi)
end
-- Base the dynamic range on the image contents.
img_max, img_min = pl.MinMax2dGrid(img_f)
-- Draw a saturated version of the original image. Only use the middle 50%
-- of the image's full dynamic range.
pl.col0(2)
pl.env(0, width, 0, height, 1, -1)
pl.lab("", "", "Reduced dynamic range image example")
pl.imagefr(img_f, 0, width, 0, height, 0, 0, img_min + img_max*0.25, img_max - img_max*0.25)
-- Draw a distorted version of the original image, showing its full dynamic range.
pl.env(0, width, 0, height, 1, -1)
pl.lab("", "", "Distorted image example")
stretch = {}
stretch["xmin"] = 0
stretch["xmax"] = width
stretch["ymin"] = 0
stretch["ymax"] = height
stretch["stretch"] = 0.5
-- In C / C++ the following would work, with plimagefr directly calling
-- mypltr. For compatibilty with other language bindings the same effect
-- can be achieved by generating the transformed grid first and then
-- using pltr2.
-- pl.imagefr(img_f, width, height, 0., width, 0., height, 0., 0., img_min, img_max, mypltr, (PLPointer) &stretch)
cgrid2 = {}
cgrid2["xg"] = {}
cgrid2["yg"] = {}
cgrid2["nx"] = width+1
cgrid2["ny"] = height+1
for i = 1, width+1 do
cgrid2["xg"][i] = {}
cgrid2["yg"][i] = {}
for j = 1, height+1 do
xx, yy = mypltr(i, j)
cgrid2["xg"][i][j] = xx
cgrid2["yg"][i][j] = yy
end
end
--pl.imagefr(img_f, 0, width, 0, height, 0, 0, img_min, img_max, "pltr2", cgrid2)
pl.imagefr(img_f, 0, width, 0, height, 0, 0, img_min, img_max, "mypltr")
pl.plend()