[Extension] Icon image to Lua string

Discuss your Lua playlist, album art and interface scripts.
mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

[Extension] Icon image to Lua string

Postby mederi » 15 Jan 2015 14:39

VLC Extension script with a dialog box for converting of small images to Lua strings:

Code: Select all

icon_path = "d:\\icon.png" -- png or jpg function descriptor() return { title = "Icon image to Lua string", icon = icon_string, } end function activate() Create_dialog() end function deactivate() end function meta_changed() end function close() vlc.deactivate() end function Create_dialog() d = vlc.dialog(descriptor().title) text_input_path = d:add_text_input(icon_path,1,1,4,1) d:add_button("Load icon", click_Load_icon, 1,2,1,1) spin = d:add_spin_icon(2, 2, 1, 1) html_Lua_string = d:add_html("Load PNG or JPG file (the smaller file, the shorter string). Recommended image resolution: 32x32. Then select and copy the resulting string.",1,3,4,1) end function click_Load_icon() local file = io.open(text_input_path:get_text(), "rb") if file==nil then html_Lua_string:set_text("No file loaded! Check the path and try again.") return false end -- Show progress spin:animate() d:update() local data_string="" while true do data = file:read(1) if data==nil or file:seek()>=50000 then break end data_string=data_string.."\\"..string.byte(data) vlc.keep_alive() end file:close() html_Lua_string:set_text(data_string) -- spin:stop() end icon_string = "\137\80\78\71\13\10\26\10\0\0\0\13\73\72\68\82\0\0\0\32\0\0\0\32\4\3\0\0\0\129\84\103\199\0\0\0\48\80\76\84\69\255\1\1\255\255\129\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\52\196\80\139\0\0\0\72\73\68\65\84\120\156\133\209\193\13\0\32\8\67\209\174\192\254\203\122\49\68\62\69\122\50\47\169\138\74\123\162\198\65\171\116\168\13\11\181\111\33\11\51\228\129\51\220\194\23\180\192\179\240\16\220\148\32\94\140\16\28\142\128\23\239\128\127\234\176\229\0\4\101\15\3\166\210\191\4\0\0\0\0\73\69\78\68\174\66\96\130"
How to use an icon in your VLC Extension script: https://forum.videolan.org/viewtopic.ph ... 60#p415560

roland1
Blank Cone
Blank Cone
Posts: 44
Joined: 01 May 2014 16:49

Re: [Extension] Icon image to Lua string

Postby roland1 » 18 Jan 2015 22:11

This

Code: Select all

local data_string="" while true do data = file:read(1) if data==nil or file:seek()>=50000 then break end data_string=data_string.."\\"..string.byte(data) vlc.keep_alive() end
is sth Lua 5.1 really don't like (massive string interning). But also Lua 5.2 (in vlc, at least) seems to like this

Code: Select all

local data_string = file:read"*a":gsub(".", function(m) return ("\\%d"):format(m:byte()) end)
more, e.g. 32x32 and 256x256:
(2k) 6x faster
(50k) 20x faster
More generally, the idiom to concat a big bunch of strings is, at least was (5.1), to collect them in a table (as sequence) and to apply table.concat at the end.

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

Re: [Extension] Icon image to Lua string

Postby mederi » 19 Jan 2015 18:44

Thank you for the better solution. It is really faster on my old computer: 15 s vs. 22 s (50 kB). For recommended small icons (32x32; 2 kB) the advantage is negligible. There is still Lua 5.1 in VLC-win32.

roland1
Blank Cone
Blank Cone
Posts: 44
Joined: 01 May 2014 16:49

Re: [Extension] Icon image to Lua string

Postby roland1 » 19 Jan 2015 20:30

My fault to give a vague description.
6x and 20x faster is only with respect to the posted code-lines, looped in a modified version of your Extension, durations of the loops measured with os.time.
Absolute values:
size repititions duration1 duration2
------------------------------------
2k---2000--------2s--------12s
50k--400---------7s--------?
50k--40----------1s--------15s

This is not a problem of Your extension (+recommendation), but it'd be for extensions transforming many, or even worse, large, icons at runtime (256x256: 0.02s vs 0.38s per icon). IMO this is worth noting in this forum, being one of the very few resources regarding scripting VLC in Lua. But, Yes, I've heard about premature optimization:)


Return to “Scripting VLC in lua”

Who is online

Users browsing this forum: No registered users and 7 guests