Root/nanonote-files/example-files/data/Examples/lua-plplot-examples/x20.lua

1--[[ $Id: x20.lua 9535 2009-02-17 10:14:04Z smekal $
2
3    plimage demo
4--]]
5
6-- initialise Lua bindings for PLplot examples.
7dofile("plplot_examples.lua")
8
9XDIM = 260
10YDIM = 220
11
12dbg = 0
13nosombrero = 0
14nointeractive = 0
15f_name=""
16
17
18-- Transformation function
19function mypltr(x, y)
20  local x0 = (stretch["xmin"] + stretch["xmax"])*0.5
21  local y0 = (stretch["ymin"] + stretch["ymax"])*0.5
22  local dy = (stretch["ymax"]-stretch["ymin"])*0.5
23  local tx = x0 + (x0-x)*(1 - stretch["stretch"]*math.cos((y-y0)/dy*math.pi*0.5))
24  local ty = y
25    
26  return tx, ty
27end
28
29
30-- read image from file in binary ppm format
31function read_img(fname)
32  -- naive grayscale binary ppm reading. If you know how to, improve it
33  local fp = io.open(fname, "rb")
34  if fp==nil then
35    return 1
36  end
37
38  -- version
39  local ver = fp:read("*line")
40  
41  if ver~="P5" then -- I only understand this!
42    fp:close()
43    return 1
44  end
45
46  while fp:read(1)=="#" do
47    local com = fp:read("*line")
48    if com==nil then
49      fp:close()
50      return 1
51    end
52  end
53  fp:seek("cur", -1)
54  
55  local w, h, num_col = fp:read("*number", "*number", "*number")
56  if w==nil or h==nil or num_col==nil then -- width, height, num colors
57    fp:close()
58    return 1
59  end
60  
61  -- read the rest of the line (only EOL)
62  fp:read("*line")
63
64  local img = fp:read(w*h)
65  fp:close()
66  if string.len(img)~=(w*h) then
67    return 1
68  end
69
70  local imf = {}
71
72  for i = 1, w do
73    imf[i] = {}
74    for j = 1, h do
75      imf[i][j] = string.byte(img, (h-j)*w+i) -- flip image up-down
76    end
77  end
78
79  return 0, imf, w, h, num_col
80end
81
82
83-- save plot
84function save_plot(fname)
85  local cur_strm = pl.gstrm() -- get current stream
86  local new_strm = pl.mkstrm() -- create a new one
87    
88  pl.sdev("psc") -- new device type. Use a known existing driver
89  pl.sfnam(fname) -- file name
90
91  pl.cpstrm(cur_strm, 0) -- copy old stream parameters to new stream
92  pl.replot() -- do the save
93  pl.end1() -- close new device
94
95  pl.sstrm(cur_strm) -- and return to previous one
96end
97
98
99-- get selection square interactively
100function get_clip(xi, xe, yi, ye)
101  return 0, xi, xe, yi, ye
102end
103
104
105-- set gray colormap
106function gray_cmap(num_col)
107  local r = { 0, 1 }
108  local g = { 0, 1 }
109  local b = { 0, 1 }
110  local pos = { 0, 1 }
111
112  pl.scmap1n(num_col)
113  pl.scmap1l(1, pos, r, g, b)
114end
115
116
117x = {}
118y = {}
119z = {}
120r = {}
121img_f = {}
122cgrid2 = {}
123
124  
125-- Bugs in plimage():
126-- + at high magnifications, the left and right edge are ragged, try
127-- ./x20c -dev xwin -wplt 0.3,0.3,0.6,0.6 -ori 0.5
128     
129-- Bugs in x20c.c:
130-- + if the window is resized after a selection is made on "lena", when
131  --making a new selection the old one will re-appear.
132  
133
134-- Parse and process command line arguments
135pl.parseopts(arg, pl.PL_PARSE_FULL)
136
137-- Initialize plplot
138pl.init()
139
140z={}
141
142-- view image border pixels
143if dbg~=0 then
144  pl.env(1, XDIM, 1, YDIM, 1, 1) -- no plot box
145  
146  -- build a one pixel square border, for diagnostics
147  for i = 1, XDIM do
148    z[i] = {}
149    z[i][1] = 1 -- left
150    z[i][YDIM] = 1 -- right
151  end
152
153  for i = 1, YDIM do
154    z[1][i] = 1 -- top
155    z[XDIM][i] = 1 -- botton
156  end
157
158  pl.lab("...around a blue square."," ","A red border should appear...")
159
160  pl.image(z, 1, XDIM, 1, YDIM, 0, 0, 1, XDIM, 1, YDIM)
161end
162
163-- sombrero-like demo
164if nosombrero==0 then
165  r = {}
166  pl.col0(2) -- draw a yellow plot box, useful for diagnostics! :(
167  pl.env(0, 2*math.pi, 0, 3*math.pi, 1, -1)
168
169  for i = 1, XDIM do
170    x[i] = (i-1)*2*math.pi/(XDIM-1)
171  end
172  for i = 1, YDIM do
173    y[i] = (i-1)*3*math.pi/(YDIM-1)
174  end
175
176  for i = 1, XDIM do
177    r[i] = {}
178    z[i] = {}
179    for j=1, YDIM do
180      r[i][j] = math.sqrt(x[i]^2+y[j]^2)+1e-3
181      z[i][j] = math.sin(r[i][j])/r[i][j]
182    end
183  end
184
185  pl.lab("No, an amplitude clipped \"sombrero\"", "", "Saturn?")
186  pl.ptex(2, 2, 3, 4, 0, "Transparent image")
187  pl.image(z, 0, 2*math.pi, 0, 3*math.pi, 0.05, 1, 0, 2*math.pi, 0, 3*math.pi)
188
189  -- save the plot
190  if f_name~="" then
191    save_plot(f_name)
192  end
193end
194
195-- read Lena image
196-- Note we try two different locations to cover the case where this
197-- examples is being run from the test_c.sh script
198status, img_f, width, height, num_col = read_img("lena.pgm")
199if status~=0 then
200  status, img_f, width, height, num_col = read_img("../lena.pgm")
201  if status~=0 then
202    pl.abort("No such file")
203    pl.plend()
204    os.exit()
205  end
206end
207
208-- set gray colormap
209gray_cmap(num_col)
210
211-- display Lena
212pl.env(1, width, 1, height, 1, -1)
213
214if nointeractive==0 then
215  pl.lab("Set and drag Button 1 to (re)set selection, Button 2 to finish."," ","Lena...")
216else
217  pl.lab(""," ","Lena...")
218end
219
220pl.image(img_f, 1, width, 1, height, 0, 0, 1, width, 1, height)
221
222-- selection/expansion demo
223if nointeractive==0 then
224  xi = 200
225  xe = 330
226  yi = 280
227  ye = 220
228
229  status, xi, xe, yi, ye = get_clip(xi, xe, yi, ye)
230  if status~=0 then -- get selection rectangle
231    pl.plend()
232    os.exit()
233  end
234
235  pl.spause(0)
236  pl.adv(0)
237
238  -- display selection only
239  pl.image(img_f, 1, width, 1, height, 0, 0, xi, xe, ye, yi)
240
241  pl.spause(1)
242
243  -- zoom in selection
244  pl.env(xi, xe, ye, yi, 1, -1)
245  pl.image(img_f, 1, width, 1, height, 0, 0, xi, xe, ye, yi)
246end
247
248-- Base the dynamic range on the image contents.
249img_max, img_min = pl.MinMax2dGrid(img_f)
250
251-- Draw a saturated version of the original image. Only use the middle 50%
252-- of the image's full dynamic range.
253pl.col0(2)
254pl.env(0, width, 0, height, 1, -1)
255pl.lab("", "", "Reduced dynamic range image example")
256pl.imagefr(img_f, 0, width, 0, height, 0, 0, img_min + img_max*0.25, img_max - img_max*0.25)
257
258-- Draw a distorted version of the original image, showing its full dynamic range.
259pl.env(0, width, 0, height, 1, -1)
260pl.lab("", "", "Distorted image example")
261
262stretch = {}
263stretch["xmin"] = 0
264stretch["xmax"] = width
265stretch["ymin"] = 0
266stretch["ymax"] = height
267stretch["stretch"] = 0.5
268
269-- In C / C++ the following would work, with plimagefr directly calling
270-- mypltr. For compatibilty with other language bindings the same effect
271-- can be achieved by generating the transformed grid first and then
272-- using pltr2.
273-- pl.imagefr(img_f, width, height, 0., width, 0., height, 0., 0., img_min, img_max, mypltr, (PLPointer) &stretch)
274
275cgrid2 = {}
276cgrid2["xg"] = {}
277cgrid2["yg"] = {}
278cgrid2["nx"] = width+1
279cgrid2["ny"] = height+1
280
281for i = 1, width+1 do
282  cgrid2["xg"][i] = {}
283  cgrid2["yg"][i] = {}
284  for j = 1, height+1 do
285    xx, yy = mypltr(i, j)
286    cgrid2["xg"][i][j] = xx
287    cgrid2["yg"][i][j] = yy
288  end
289end
290    
291--pl.imagefr(img_f, 0, width, 0, height, 0, 0, img_min, img_max, "pltr2", cgrid2)
292pl.imagefr(img_f, 0, width, 0, height, 0, 0, img_min, img_max, "mypltr")
293
294pl.plend()
295

Archive Download this file



interactive