summaryrefslogtreecommitdiff
path: root/test/lua
diff options
context:
space:
mode:
authorscuri <scuri>2008-10-17 06:10:15 +0000
committerscuri <scuri>2008-10-17 06:10:15 +0000
commit5a422aba704c375a307a902bafe658342e209906 (patch)
tree5005011e086bb863d8fb587ad3319bbec59b2447 /test/lua
First commit - moving from LuaForge to SourceForge
Diffstat (limited to 'test/lua')
-rw-r--r--test/lua/analyze.lua28
-rw-r--r--test/lua/capture.lua63
-rw-r--r--test/lua/error.lua10
-rw-r--r--test/lua/fft.lua17
-rw-r--r--test/lua/flower.jpgbin0 -> 17915 bytes
-rw-r--r--test/lua/index.lua18
-rw-r--r--test/lua/info.lua149
-rw-r--r--test/lua/lena.jpgbin0 -> 7145 bytes
-rw-r--r--test/lua/palette.lua6
-rw-r--r--test/lua/process.lua50
-rw-r--r--test/lua/process_new.lua44
-rw-r--r--test/lua/render.lua50
-rw-r--r--test/lua/render_cd.lua15
-rw-r--r--test/lua/screencapture.lua13
-rw-r--r--test/lua/show_flower.wlua31
-rw-r--r--test/lua/view.wlua158
16 files changed, 652 insertions, 0 deletions
diff --git a/test/lua/analyze.lua b/test/lua/analyze.lua
new file mode 100644
index 0000000..7f06496
--- /dev/null
+++ b/test/lua/analyze.lua
@@ -0,0 +1,28 @@
+require"imlua"
+require"imlua_process"
+
+local filename = "lena.jpg"
+
+local image = im.FileImageLoad(filename)
+local gray = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType())
+local binary = im.ImageCreate(image:Width(), image:Height(), im.BINARY, image:DataType())
+local region = im.ImageCreate(image:Width(), image:Height(), im.GRAY, im.USHORT)
+
+-- make it grayscale
+im.ConvertColorSpace(image, gray)
+gray:Save("lena_gray.jpg", "JPEG")
+
+-- make it binary
+im.ProcessSliceThreshold(gray, binary, 0, 128)
+binary:Save("lena_binary.jpg", "JPEG")
+
+local count = im.AnalyzeFindRegions(binary, region, 4, 1)
+print("regions: ", count)
+
+local region2 = im.ImageCreate(image:Width(), image:Height(), im.GRAY, im.BYTE)
+im.ConvertDataType(region, region2, 0, 0, 0, 0)
+
+local region3 = im.ImageCreate(image:Width(), image:Height(), im.MAP, im.BYTE)
+im.ConvertColorSpace(region2, region3)
+region3:SetPalette(im.PaletteHighContrast(), 256)
+region3:Save("lena_region.gif", "GIF")
diff --git a/test/lua/capture.lua b/test/lua/capture.lua
new file mode 100644
index 0000000..491518a
--- /dev/null
+++ b/test/lua/capture.lua
@@ -0,0 +1,63 @@
+require"imlua"
+require"imlua_capture"
+
+im.VideoCaptureReloadDevices()
+
+print("--- Devices ---")
+local n = im.VideoCaptureDeviceCount()
+
+for i = 0, n - 1 do
+ desc = im.VideoCaptureDeviceDesc(i)
+ print(desc)
+end
+
+local vc = im.VideoCaptureCreate()
+print("connect: ", vc:Connect(0))
+print()
+
+print("--- Dialogs ---")
+
+local dc = vc:DialogCount()
+for i = 0, dc - 1 do
+ desc = vc:DialogDesc(i)
+ print(i, desc)
+ vc:ShowDialog(i)
+end
+print()
+
+
+print("--- Formats ---")
+
+local fc = vc:FormatCount()
+for i = 0, fc - 1 do
+ local success, width, height, desc = vc:GetFormat(i)
+ print(i, string.format("%dx%d", width, height), desc)
+end
+print()
+
+print("--- Image Size ---")
+local width, height = vc:GetImageSize()
+print(width, height)
+print()
+
+print("--- Attributes ---")
+attribs = vc:GetAttributeList()
+for i, name in ipairs(attribs) do
+ local error, percent = vc:GetAttribute(name)
+ if error == 0 then percent = "get error" end
+ print(i, name, percent)
+end
+--vc:SetAttribute("FlipVertical", 1)
+--vc:SetAttribute("FlipHorizontal", 1)
+print()
+
+print("--- Capture ---")
+local image = im.ImageCreate(width, height, im.RGB, im.BYTE)
+local res = vc:Live(1)
+if (res > 0) then
+ print("grabbing frame")
+ print(vc:Frame(image, 3000))
+end
+image:Save("capture.jpg", "JPEG")
+
+vc:Disconnect()
diff --git a/test/lua/error.lua b/test/lua/error.lua
new file mode 100644
index 0000000..f8c71a8
--- /dev/null
+++ b/test/lua/error.lua
@@ -0,0 +1,10 @@
+require"imlua"
+
+local filename = "lena.jpg"
+local image = im.FileImageLoad(filename)
+local image2 = im.ImageCreate(image:Width(), image:Height(), im.GRAY, im.USHORT)
+
+-- Both calls will signal an error because of incompatible parameters
+
+--im.ConvertDataType(image, image2, im.CPX_REAL, im.GAMMA_LINEAR, 0, im.CAST_MINMAX)
+im.ConvertColorSpace(image, image2, im.CPX_REAL, im.GAMMA_LINEAR, 0, im.CAST_MINMAX)
diff --git a/test/lua/fft.lua b/test/lua/fft.lua
new file mode 100644
index 0000000..3589635
--- /dev/null
+++ b/test/lua/fft.lua
@@ -0,0 +1,17 @@
+require"imlua"
+require"imlua_process"
+require"imlua_fftw"
+
+local filename = "lena.jpg"
+local image = im.FileImageLoad(filename)
+
+local complex = im.ImageCreate(image:Width(), image:Height(), image:ColorSpace(), im.CFLOAT)
+im.ProcessFFT(image, complex)
+
+local c = complex[0][5][10] -- component=0(Red), y = 5 x =10
+print(c[1], c[2])
+
+complex[0][5][10] = { 2*c[1], c[2]/2 }
+
+local c = complex[0][5][10]
+print(c[1], c[2])
diff --git a/test/lua/flower.jpg b/test/lua/flower.jpg
new file mode 100644
index 0000000..2b9dbf1
--- /dev/null
+++ b/test/lua/flower.jpg
Binary files differ
diff --git a/test/lua/index.lua b/test/lua/index.lua
new file mode 100644
index 0000000..1c4c1e4
--- /dev/null
+++ b/test/lua/index.lua
@@ -0,0 +1,18 @@
+require"imlua"
+
+local filename = "lena.jpg"
+local image = im.FileImageLoad(filename)
+
+local r = image[0]
+local g = image[1]
+local b = image[2]
+
+for row = 0, image:Height() - 1, 10 do
+ for column = 0, image:Width() - 1, 10 do
+ r[row][column] = 0
+ g[row][column] = 0
+ b[row][column] = 0
+ end
+end
+
+image:Save("lena_indexing.bmp", "BMP")
diff --git a/test/lua/info.lua b/test/lua/info.lua
new file mode 100644
index 0000000..5b4b151
--- /dev/null
+++ b/test/lua/info.lua
@@ -0,0 +1,149 @@
+require"imlua"
+require"lfs"
+
+function PrintError(error)
+ local msg = {}
+ msg[im.ERR_OPEN] = "Error Opening File."
+ msg[im.ERR_MEM] = "Insuficient memory."
+ msg[im.ERR_ACCESS] = "Error Accessing File."
+ msg[im.ERR_DATA] = "Image type not Suported."
+ msg[im.ERR_FORMAT] = "Invalid Format."
+ msg[im.ERR_COMPRESS] = "Invalid or unsupported compression."
+
+ if msg[error] then
+ print(msg[error])
+ else
+ print("Unknown Error.")
+ end
+end
+
+function FindZero(data)
+ if (not data) then return false end
+ for i = 1, table.getn(data) do
+ if data[i] == 0 then
+ return true
+ end
+ end
+ return false
+end
+
+function AttribData2Str(data, data_type)
+ local data_str
+
+ if data_type == im.BYTE then
+ data_str = string.format("%3d", data[1])
+ elseif data_type == im.USHORT then
+ data_str = string.format("%5d", data[1])
+ elseif data_type == im.INT then
+ data_str = string.format("%5d", data[1])
+ elseif data_type == im.FLOAT then
+ data_str = string.format("%5.2f", data[1])
+ elseif data_type == im.CFLOAT then
+ data_str = string.format("%5.2f, %5.2f", data[1], data[2])
+ end
+
+ return data_str
+end
+
+function GetSizeDesc(size)
+ local size_desc
+
+ if size < 1024 then
+ size_desc = "b"
+ else
+ size = size / 1024
+
+ if size < 1024 then
+ size_desc = "Kb"
+ else
+ size = size / 1024
+ size_desc = "Mb"
+ end
+ end
+
+ return size, size_desc
+end
+
+function FileSize(file_name)
+ if lfs then
+ local attr = lfs.attributes(file_name)
+ return attr.size
+ else
+ return 0
+ end
+end
+
+function PrintImageInfo(file_name)
+ print("IM Info")
+ print(string.format(" File Name:\n %s", file_name))
+
+ local ifile, error = im.FileOpen(file_name)
+ if not ifile then
+ PrintError(error)
+ return nil
+ end
+
+ local file_size = FileSize(file_name)
+
+ print(string.format(" File Size: %.2f %s", GetSizeDesc(file_size)))
+
+ local format, compression, image_count = ifile:GetInfo()
+
+ local error, format_desc = im.FormatInfo(format)
+ print(string.format(" Format: %s - %s", format, format_desc))
+ print(string.format(" Compression: %s", compression))
+ print(string.format(" Image Count: %d", image_count))
+ for i = 1, image_count do
+ local error, width, height, color_mode, data_type = ifile:ReadImageInfo(i-1)
+ if width == nil then
+ PrintError(height)
+ ifile:Close()
+ return nil
+ end
+
+ print(string.format(" Image #%d", i))
+ print(string.format(" Width: %d", width))
+ print(string.format(" Height: %d", height))
+ print(string.format(" Color Space: %s", im.ColorModeSpaceName(color_mode)))
+ print(string.format(" Has Alpha: %s", im.ColorModeHasAlpha(color_mode) and "Yes" or "No"))
+ print(string.format(" Is Packed: %s", im.ColorModeIsPacked(color_mode) and "Yes" or "No"))
+ print(string.format(" Is Top Down: %s", im.ColorModeIsTopDown(color_mode) and "Yes" or "No"))
+ print(string.format(" Data Type: %s", im.DataTypeName(data_type)))
+
+ local image_size = im.ImageDataSize(width, height, color_mode, data_type)
+ print(string.format(" Data Size: %.2f %s", GetSizeDesc(image_size)))
+
+ local attrib_list = ifile:GetAttributeList()
+ for a = 1, table.getn(attrib_list) do
+ if a == 1 then
+ print(" Attributes:")
+ end
+
+ local attrib_data, attrib_data_type = ifile:GetAttribute(attrib_list[a])
+
+ if table.getn(attrib_data) == 1 then
+ print(string.format(" %s: %s", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type)))
+ elseif attrib_data_type == im.BYTE and FindZero(attrib_data) then
+ attrib_data = ifile:GetAttribute(attrib_list[a], true)
+ print(string.format(" %s: %s", attrib_list[a], attrib_data))
+ else
+ print(string.format(" %s: %s ...", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type)))
+ end
+ end
+ end
+
+ ifile:Close()
+end
+
+function main(arg)
+ if (not arg or table.getn(arg) < 1) then
+ print("Invalid number of arguments.")
+ return nil
+ end
+
+ PrintImageInfo(arg[1])
+ return 1
+end
+
+--main(arg)
+PrintImageInfo("lena.jpg")
diff --git a/test/lua/lena.jpg b/test/lua/lena.jpg
new file mode 100644
index 0000000..b6bad61
--- /dev/null
+++ b/test/lua/lena.jpg
Binary files differ
diff --git a/test/lua/palette.lua b/test/lua/palette.lua
new file mode 100644
index 0000000..5ec7168
--- /dev/null
+++ b/test/lua/palette.lua
@@ -0,0 +1,6 @@
+require"imlua"
+
+local impal = im.PaletteHotIron()
+print(impal)
+print(im.ColorDecode(impal[1]))
+
diff --git a/test/lua/process.lua b/test/lua/process.lua
new file mode 100644
index 0000000..5a32586
--- /dev/null
+++ b/test/lua/process.lua
@@ -0,0 +1,50 @@
+require"imlua"
+require"imlua_process"
+
+function save_histogram (hist, filename, format)
+ local height = 200 -- altura da imagem
+ local max = math.max(unpack(hist)) -- pega o maior valor do histograma
+ local n = table.getn(hist) + 1 -- zero-based
+ local image = im.ImageCreate(n, height, im.GRAY, im.BYTE) -- cria a imagem
+ local white = 255
+ local black = 0
+
+ local render = function (x, y, d, param)
+ local v = hist[x] / max
+ local h = v * height
+ if y <= h then return black end
+ return white
+ end
+
+ im.ProcessRenderOp(image, render, "histogram", {}, 0)
+ image:Save(filename, format)
+end
+
+local filename = "lena.jpg"
+
+local image = im.FileImageLoad(filename)
+
+save_histogram(im.CalcHistogram(image, 0, 0), "lena_histogram_R.gif", "GIF")
+save_histogram(im.CalcHistogram(image, 1, 0), "lena_histogram_G.gif", "GIF")
+save_histogram(im.CalcHistogram(image, 2, 0), "lena_histogram_B.gif", "GIF")
+save_histogram(im.CalcGrayHistogram(image, 0), "lena_histogram_gray.gif", "GIF")
+
+local r = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType())
+local g = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType())
+local b = im.ImageCreate(image:Width(), image:Height(), im.GRAY, image:DataType())
+im.ProcessSplitComponents(image, { r, g, b})
+r:Save("lena_r.jpg", "JPEG")
+g:Save("lena_g.jpg", "JPEG")
+b:Save("lena_b.jpg", "JPEG")
+
+local rgb = image:Clone()
+im.ProcessMergeComponents({r, g, b}, rgb)
+rgb:Save("lena_rgb.jpg", "JPEG")
+
+local replace = image:Duplicate()
+im.ProcessReplaceColor(image, replace, { 146, 93, 145 }, { 255, 0, 255 })
+replace:Save("lena_replace.jpg", "JPEG")
+
+local bitmask = image:Duplicate()
+im.ProcessBitMask(image, bitmask, "01111010", im.BIT_XOR)
+replace:Save("lena_bitmask.jpg", "JPEG")
diff --git a/test/lua/process_new.lua b/test/lua/process_new.lua
new file mode 100644
index 0000000..93ebcce
--- /dev/null
+++ b/test/lua/process_new.lua
@@ -0,0 +1,44 @@
+require"imlua"
+require"imlua_process"
+
+function save_histogram (hist, filename, format)
+ local height = 200 -- altura da imagem
+ local max = math.max(unpack(hist)) -- pega o maior valor do histograma
+ local n = table.getn(hist) + 1 -- zero-based
+ local image = im.ImageCreate(n, height, im.GRAY, im.BYTE) -- cria a imagem
+ local white = 255
+ local black = 0
+
+ local render = function (x, y, d, param)
+ local v = hist[x] / max
+ local h = v * height
+ if y <= h then return black end
+ return white
+ end
+
+ im.ProcessRenderOp(image, render, "histogram", {}, 0)
+ image:Save(filename, format)
+end
+
+local filename = "lena.jpg"
+
+local image = im.FileImageLoad(filename)
+
+save_histogram(im.CalcHistogram(image, 0, 0), "lena_histogram_R.gif", "GIF")
+save_histogram(im.CalcHistogram(image, 1, 0), "lena_histogram_G.gif", "GIF")
+save_histogram(im.CalcHistogram(image, 2, 0), "lena_histogram_B.gif", "GIF")
+save_histogram(im.CalcGrayHistogram(image, 0), "lena_histogram_gray.gif", "GIF")
+
+local r, g, b = im.ProcessSplitComponentsNew(image)
+r:Save("lena_r.jpg", "JPEG")
+g:Save("lena_g.jpg", "JPEG")
+b:Save("lena_b.jpg", "JPEG")
+
+local rgb = im.ProcessMergeComponentsNew({r, g, b})
+rgb:Save("lena_rgb.jpg", "JPEG")
+
+local replace = im.ProcessReplaceColorNew(image, { 146, 93, 145 }, { 255, 0, 255 })
+replace:Save("lena_replace.jpg", "JPEG")
+
+local bitmask = im.ProcessBitMaskNew(image, "01111010", im.BIT_XOR)
+replace:Save("lena_bitmask.jpg", "JPEG")
diff --git a/test/lua/render.lua b/test/lua/render.lua
new file mode 100644
index 0000000..b57f906
--- /dev/null
+++ b/test/lua/render.lua
@@ -0,0 +1,50 @@
+require"imlua"
+require"imlua_process"
+
+local image = im.ImageCreate(500, 500, im.RGB, im.BYTE)
+
+im.ProcessRenderRandomNoise(image)
+image:Save("render_noise.bmp", "BMP")
+
+im.ProcessRenderConstant(image, { 128.0, 0.0, 255.0 })
+image:Save("render_constant.bmp", "BMP")
+
+im.ProcessRenderWheel(image, 100, 200)
+image:Save("render_wheel.bmp", "BMP")
+
+im.ProcessRenderTent(image, 300, 200)
+image:Save("render_tent.bmp", "BMP")
+
+im.ProcessRenderRamp(image, 0, 500, 0)
+image:Save("render_ramp.bmp", "BMP")
+
+im.ProcessRenderBox(image, 200, 200)
+image:Save("render_box.bmp", "BMP")
+
+im.ProcessRenderSinc(image, 100.0, 100.0)
+image:Save("render_sinc.bmp", "BMP")
+
+im.ProcessRenderGaussian(image, 100.0)
+image:Save("render_gaussian.bmp", "BMP")
+
+im.ProcessRenderLapOfGaussian(image, 100.0)
+image:Save("render_lapofgaussian.bmp", "BMP")
+
+im.ProcessRenderCosine(image, 100.0, 100.0)
+image:Save("render_cosine.bmp", "BMP")
+
+im.ProcessRenderGrid(image, 100.0, 100.0)
+image:Save("render_grid.bmp", "BMP")
+
+im.ProcessRenderChessboard(image, 100.0, 100.0)
+image:Save("render_chess.bmp", "BMP")
+
+im.ProcessRenderCone(image, 200)
+image:Save("render_cone.bmp", "BMP")
+
+local render_func = function (x, y, d, param)
+ return math.mod(x + y, 256)
+end
+
+im.ProcessRenderOp(image, render_func, "test", {}, 0)
+image:Save("render_func.bmp", "BMP")
diff --git a/test/lua/render_cd.lua b/test/lua/render_cd.lua
new file mode 100644
index 0000000..532f906
--- /dev/null
+++ b/test/lua/render_cd.lua
@@ -0,0 +1,15 @@
+require"imlua"
+require"cdlua"
+require"imlua_cd"
+
+local image = im.ImageCreate(500, 500, im.RGB, im.BYTE)
+local canvas = image:cdCreateCanvas() -- Creates a CD_IMAGERGB canvas
+
+canvas:Activate()
+canvas:Clear()
+canvas:Font("Times", cd.BOLD, 24)
+canvas:Text(100, 100, "Test")
+canvas:Line(0,0,100,100)
+canvas:Kill()
+
+image:Save("new.bmp", "BMP")
diff --git a/test/lua/screencapture.lua b/test/lua/screencapture.lua
new file mode 100644
index 0000000..5b1a93d
--- /dev/null
+++ b/test/lua/screencapture.lua
@@ -0,0 +1,13 @@
+require"imlua"
+require"cdlua"
+require"imlua_cd"
+
+local canvas = cd.CreateCanvas(cd.NATIVEWINDOW, nil)
+canvas:Activate()
+local w, h = canvas:GetSize()
+local image = im.ImageCreate(w, h, im.RGB, im.BYTE)
+image:cdCanvasGetImage(canvas, 0, 0)
+error = image:Save("screencapture.jpg", "JPEG")
+image:Destroy()
+if (error) then print("error = "..error) end
+
diff --git a/test/lua/show_flower.wlua b/test/lua/show_flower.wlua
new file mode 100644
index 0000000..a3803d7
--- /dev/null
+++ b/test/lua/show_flower.wlua
@@ -0,0 +1,31 @@
+require"imlua"
+require"iuplua"
+require"cdlua"
+require"cdluaiup"
+require"imlua_cd"
+
+image = im.FileImageLoad("flower.jpg") -- directly load the image at index 0. it will open and close the file
+cnv = iup.canvas{rastersize = image:Width().."x"..image:Height(), border = "NO"}
+cnv.image = image -- store the new image in the IUP canvas as an attribute
+
+function cnv:map_cb() -- the CD canvas can only be created when the IUP canvas is mapped
+ self.canvas = cd.CreateCanvas(cd.IUP, self)
+end
+
+function cnv:action() -- called everytime the IUP canvas needs to be repainted
+ self.canvas:Activate()
+ self.canvas:Clear()
+ self.image:cdCanvasPutImageRect(self.canvas, 0, 0, 0, 0, 0, 0, 0, 0) -- use default values
+end
+
+dlg = iup.dialog{cnv}
+
+function dlg:close_cb()
+ cnv.image:Destroy()
+ cnv.canvas:Kill()
+ self:destroy()
+ return iup.IGNORE -- because we destroy the dialog
+end
+
+dlg:show()
+iup.MainLoop()
diff --git a/test/lua/view.wlua b/test/lua/view.wlua
new file mode 100644
index 0000000..9eb9eed
--- /dev/null
+++ b/test/lua/view.wlua
@@ -0,0 +1,158 @@
+require"imlua"
+require"iuplua"
+require"cdlua"
+require"cdluaiup"
+require"imlua_cd"
+
+function PrintError(func, error)
+ local msg = {}
+ msg[im.ERR_OPEN] = "Error Opening File."
+ msg[im.ERR_MEM] = "Insuficient memory."
+ msg[im.ERR_ACCESS] = "Error Accessing File."
+ msg[im.ERR_DATA] = "Image type not Suported."
+ msg[im.ERR_FORMAT] = "Invalid Format."
+ msg[im.ERR_COMPRESS] = "Invalid or unsupported compression."
+
+ if msg[error] then
+ print(func..": "..msg[error])
+ else
+ print("Unknown Error.")
+ end
+end
+
+function LoadImage(file_name)
+ local image
+ local ifile, error = im.FileOpen(file_name)
+ if not ifile then
+ PrintError("open", error)
+ return
+ end
+
+ -- load the first image in the file.
+ -- force the image to be converted to a bitmap
+ image, error = ifile:LoadBitmap()
+ if not image then
+ PrintError("load", error)
+ return
+ end
+
+ ifile:Close()
+ return image
+end
+
+
+dlg = nil -- only one dlg
+
+function ShowImage(file_name)
+
+ local image = LoadImage(file_name)
+ if not image then
+ return false
+ end
+
+ if dlg then
+ local old_canvas = dlg.canvas
+ local old_image = dlg.image
+
+ if old_canvas ~= nil then old_canvas:Kill() end
+ if old_image ~= nil then old_image:Destroy() end
+
+ iup.Destroy(dlg)
+ end
+
+ cnv = iup.canvas{}
+
+ function cnv:action()
+ local canvas = dlg.canvas
+ local image = dlg.image
+
+ if (not canvas) then return end
+
+ canvas:Activate()
+ canvas:Clear()
+ image:cdCanvasPutImageRect(canvas, 0, 0, image:Width(), image:Height(), 0, 0, 0, 0)
+ canvas:Flush()
+
+ return iup.DEFAULT
+ end
+
+ function cnv:button_cb()
+ local file_name = "*.*"
+ local error
+
+ file_name, error = iup.GetFile(file_name)
+ if error ~= 0 then
+ return iup.DEFAULT
+ end
+
+ ShowImage(file_name)
+ return iup.DEFAULT
+ end
+
+
+ -- Set the Canvas inicial size (IUP will retain this value).
+ cnv.rastersize = string.format("%dx%d", image:Width(), image:Height())
+
+ dlg = iup.dialog{cnv}
+ dlg.title = file_name
+
+ function dlg:close_cb()
+ local canvas = self.canvas
+ local image = self.image
+
+ if canvas then canvas:Kill() end
+ if image then image:Destroy() end
+
+ return iup.CLOSE
+ end
+
+ dlg:show()
+
+ canvas = cd.CreateCanvas(cd.IUP, cnv)
+ assert(canvas)
+
+ dlg.canvas = canvas
+ dlg.image = image
+
+ cnv:action()
+
+ return true
+end
+
+function main(arg)
+ local file_name = "*.*"
+ local error
+
+ -- Try to get a file name from the command line.
+ if (arg == nil or table.getn(arg) < 2) then
+ file_name, error = iup.GetFile(file_name)
+ if error ~= 0 then
+ return true
+ end
+ else
+ file_name = arg[1]
+ end
+
+ if not ShowImage(file_name) then
+ local Try = true
+ -- If ShowImage returns an error I will try to read another image.
+ -- I can give up on File Open dlg choosing "Cancel".
+ while Try do
+ file_name = "*.*"
+
+ file_name, error = iup.GetFile(file_name)
+ if error ~= 0 then
+ return true
+ end
+
+ if ShowImage(file_name) then
+ Try = false
+ end
+ end
+ end
+
+ iup.MainLoop()
+ return true
+end
+
+main(arg)