ID:1690821
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
I'm requesting a proc that converts /icon objects into a base64 text string. (Or alternatively, give the ability to save an icon to the filesystem)

Currently, the built-in savefile system already stores icons in base64 encoding, but in order to retreive that, you need to use ExportText, and parse the mark-up. But no one seems to have a parser, and thus it's not quite possible.

This is useful for a couple reasons. The first of which being that there's no built-in way to create icon files. Best one could do is story them inside of a savefile.
And second; in this way we can interact with other applications that accept base64 strings of images.


Alternatively, we could have a function that actually saves an icon file to the filesystem (and not just one that sends it to the user.
What's the difference between saving an icon file and sending it to a client to be saved? Who's saving it, and where?
Currently, if you create an icon at runtime, the only way to save that new icon is by saving it to a savefile. But there's no direct way to interact with other applications using that icon, for things like modifying it or sending it to an outside server.

For the most part, I'm suggesting a way to actually return the icon information in a usable format, such as an actual file, or a base64 string.
In response to RageFury33
You can save an /icon as a .dmi using ftp()...?
I think you can also do the same thing with browse_rsc(I,F) i nwhich I is the icon object and F is the file to save it to.

proc/SaveIcon(client/C, icon/I, F)
C << browse_rsc(I,F)

// example:
SaveIcon(client, icon('someicon.dmi'), "someicon.png")
Does this help you out? What it does is convert all pixels of the icon to a large string of hex codes using IconToHex(). To convert it back, we would use HexToIcon().
proc/IconToHex(icon/I, S="")
var/dx = I.Width()
var/dy = I.Height()
var/s = "[dx],[dy],"
for (var/i = 1 to dx)
for (var/j = 1 to dy)
var/ss = I.GetPixel(i,j,S)
if (ss)
s += "[ss],"
else
s += "_,"
return s

proc/HexToIcon(H)
var/list/l = TextToList(H,",")
var/icon/r = icon('Icon.dmi',"blank")
var/lx = text2num(l[1])
var/ly = text2num(l[2])
r.Scale(lx,ly)

for (var/i = 1 to lx)
for (var/j = 1 to ly)
var/u = ((i*lx)-lx) + j + 2
if (l[u] != "_")
r.DrawBox(l[u],i,j)

return r

proc/TextToList(T="NoText",S=",")
var/list/l = new()
var/w = ""
var/c = ""
for (var/i = 1 to length(T))
c = Char(T,i)
if ( (c == S) && (length(w)) )
l += w
w = ""
c = ""
else
w += c
if (length(w))
l += w
return l

proc/Char(T="NoText",N=1)
return ascii2text( text2ascii(T,N) )


You can save pretty much any icon you'd like. Careful though, because it only saves a static image, and not an actual DMI.

An example:
var/icon/i = icon('SomeIcon.dmi')
var/s = IconToHex(i)

// ...

var/icon/i = HexToIcon(s)
world << "\icon[i]"
If you just want to save the icon inside the project directory, you can also use fcopy():
fcopy(fcopy_rsc(myDynamicIcon),"out.dmi")


I can't remember when the fcopy_rsc() is necessary, though.

Also, I thought the savefile format already stored the icon as base-64, so if you actually wanted that you could probably store it in a savefile and then load it back as text (might have to trim off some markup at the start/end). Or use savefile.ExportText().