PlaneShift

Development => PlaneShift Mods => Topic started by: Donari Tyndale on March 26, 2014, 08:18:43 am

Title: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 08:18:43 am
So I was playing around a little and made a tool that takes my .bmp picture and converts it into a map that is useable by Planeshift. Here's some examples:

(http://i60.tinypic.com/wrymop.jpg)

(http://i62.tinypic.com/25tvixg.jpg)

You can download the file here (http://speedy.sh/QX35Z/BMPtoXML.exe). It looks like this:

(http://i60.tinypic.com/2e5vbc8.jpg)

First, load a .bmp image. "Canvas Boundary" gives the image a frame, "Scale" scales your image and "Brush Tip" determines which character is used to draw your image. The first image was done using "." with scale 2, the second "*" with scale 3. You'll need to experiment around.

Oh, and the colour white is set to transparent, meaning whatever portion of your image is blank white will be ignored by the converter. Keep in mind that there is a character limit of ~200 painted pixels per map, set by the game (Talad should totally increase that so we can import better drawings).

If you want to do a black and white painting, you're in luck, because that is easier: The best size is around 200x200 pixels, paint whatever you want in black, leave the rest white. Load the image, simply check the box "Charcoal" and hit convert. Scale has no effect in this setting.

For those who feel like complaining that this doesn't work on their operating system/whatever, here's the source code. I have no idea of coding and whatever you might need to get this working on your machine.

Code: [Select]

Imports System
Imports System.IO
Imports System.Text



Public Class Form1
    Dim img As Bitmap


    Function I2H(int As Integer) As String
        Dim str As String
        str = Hex(int)
        If Len(str) = 1 Then
            str = "0" + str
        End If
        Return str
    End Function


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "BMP to XML by Donari"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            img = Image.FromFile(OpenFileDialog1.FileName)
            PictureBox1.Image = img

        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim x As Integer
        Dim y As Integer
        Dim cx As Integer
        Dim cy As Integer
        Dim out As String
        Dim pt As String
        Dim col As Color
        Dim white As Integer
        Dim scale As Integer
        scale = CInt(TextBox1.Text)
        white = Color.White.ToArgb()
        y = img.Height
        x = img.Width
        cx = CInt(TextBox3.Text)
        cy = CInt(TextBox2.Text)
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            out = SaveFileDialog1.FileName
        End If
        Dim fs As FileStream = File.Create(out)
        Dim info As Byte()

        If Not CheckBox1.Checked Then
            info = New UTF8Encoding(True).GetBytes("<pages><page l=""236"" t=""161"" w=""" + CStr(x * scale + 2 * cx) + """ h=""" + CStr(scale * y + 2 * cy) + """>")
            fs.Write(info, 0, info.Length)
            For i = 1 To x - 1
                For j = 1 To y - 1
                    col = img.GetPixel(i, j)
                    If col.ToArgb() <> white Then
                        pt = "<tx x=""" + CStr(i * scale + cx) + """ y=""" + CStr(j * scale + cy) + """t=""" + TextBox4.Text + """ col=""" + I2H(col.R) + I2H(col.G) + I2H(col.B) + """/>"
                        info = New UTF8Encoding(True).GetBytes(pt)
                        fs.Write(info, 0, info.Length)
                    End If
                Next
            Next
        Else
            info = New UTF8Encoding(True).GetBytes("<pages><page l=""236"" t=""161"" w=""" + CStr(x * 4 + 2 * cx) + """ h=""" + CStr(3 * y + 2 * cy) + """>")
            fs.Write(info, 0, info.Length)
            For j = 1 To y - 1
                pt = "<tx x=""" + CStr(1 + cx) + """ y=""" + CStr(j * 3 + cy) + """t="""
                For i = 1 To x - 1
                    col = img.GetPixel(i, j)
                    If col.ToArgb() <> white Then
                        pt = pt + "''"
                    Else
                        pt = pt + " "
                    End If
                Next
                pt = pt + """ col=""0""/>"
                info = New UTF8Encoding(True).GetBytes(pt)
                fs.Write(info, 0, info.Length)
            Next
        End If
        info = New UTF8Encoding(True).GetBytes("</page></pages>")
        fs.Write(info, 0, info.Length)
        fs.Close()
    End Sub


End Class
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: bilbous on March 26, 2014, 08:57:57 am
is that java?
looks really great
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: LigH on March 26, 2014, 09:02:11 am
"Dim" looks like Visual Basic.

I doubt this is a very useful tool; but I am curious to see it used for reasonable purposes.

In general, keep in mind that the size of the XML document is very limited, it must not exceed some threshold less than 64 KB, I believe...
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: bilbous on March 26, 2014, 09:05:10 am
well it is an exe so must be for windows.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 09:05:38 am
Yeah I did that with visual basic express 2013. And LigH, the size is not the problem. It's the item limit per xml that kills the resolution. One pixel painted=one item. So you can draw ~200 pixels per map.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: bilbous on March 26, 2014, 09:49:56 am
 8) I can't wait to start uploading furry porn, I'll make a mint!

Perhaps you should build in a font switcher.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Dannae on March 26, 2014, 09:55:03 am
As a player/character who likes to make map art for use in game and sometimes commissioned in character to do so, this has peaked my interest. Although it doesn't seem capable to create very precise pictures it is still interesting. I cannot experiment with it currently because I have Macs and have not bought Windows to put on any of them. If there were a way to combine this with Derula's script, http://www.hydlaaplaza.com/smf/index.php?topic=35557.0 (http://www.hydlaaplaza.com/smf/index.php?topic=35557.0), that would be even more interesting.

If you are unfamiliar with my chars. map art and what I've done with Derula's script, some samples are here: http://www.hydlaaplaza.com/smf/index.php?topic=39367.30 (http://www.hydlaaplaza.com/smf/index.php?topic=39367.30)

I've recently begun experimenting with creating map art that "appears" to have color fills.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Gilrond on March 26, 2014, 11:56:36 am
As Dannae noted, there is Derula's tool which converts SVG to PS map XML. Since we are talking about vector formats, that's way more useful. And there are existing powerful tools which perform some kind of raster -> vector conversion to make an SVG from a raster file. So really while this tool is a good exercise, it's not an optimal method.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 11:59:47 am
I really don't even know what SVG means. I just want to draw and convert.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 12:52:11 pm
Also. This is quite suboptimal.

(http://i61.tinypic.com/ax0l8n.jpg)
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: LigH on March 26, 2014, 01:10:00 pm
Scalable Vector Graphics.

The opposite of bitmaps.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Gilrond on March 26, 2014, 02:00:26 pm
I really don't even know what SVG means. I just want to draw andconvert.

Graphics can be represented in raster or vector format:

https://en.wikipedia.org/wiki/Raster_graphics
https://en.wikipedia.org/wiki/Vector_graphics

In short, raster image is what you call a bitmap - a dumb matrix of pixels of various colors. Vector image on the other hand mathematically defines various primitives such as curves, polygons and whatever else to describe the image. A common format for vector graphics is SVG: https://en.wikipedia.org/wiki/Svg

PlaneShift uses its own vector format, the "map" (which also uses XML, like the SVG by the way). Converting between raster and vector formats is not a strictly defined procedure as you might have guessed, since they are inherently very different. But there are already many tools for doing raster -> vector conversions, so there is no point to reinvent the wheel from a practical standpoint, but of course it's good for educational purposes.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 02:10:21 pm
So here's some new version:

(http://i61.tinypic.com/20shma0.jpg)
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 02:14:41 pm
... so there is no point to reinvent the wheel from a practical standpoint, but of course it's good for educational purposes.
So mista. How do I do that, then? Turn my bitmap into a PS image. Without reading anything about dumb vectors and other stuff that sounds like university level maths.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Gilrond on March 26, 2014, 02:56:03 pm
So mista. How do I do that, then? Turn my bitmap into a PS image. Without reading anything about dumb vectors and other stuff that sounds like university level maths.

I'd say, you can use something to convert bitmap to SVG first. For example: http://wiki.inkscape.org/wiki/index.php/Tools#Vectorize.2Ftrace
There are other tools you can find. Try searching for "vectorize bitmap to svg".

After you got the SVG, you might want to play around with it to minimize it since PS XML has limited capabilities (read about its limitations first: http://www.hydlaaplaza.com/smf/index.php?topic=35557.0). Same Inkscape is good for that. Then use Derula's tool mentioned above to convert the resulting SVG to PS map.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: LigH on March 26, 2014, 03:08:34 pm
To say it bold: Your approach is mainly a waste of space. You are not even getting close to art like Dannae's drawings.

The elements of vector drawings are primitives like: line, curve, ellipse, rectangle... descriptions of whole shapes or parts of their appearance. And for convenience, text or symbols at positions.

They are not: dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot ...
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 04:54:53 pm
You're missing the point. I don't want lines, bezier curves and whatever. I want pixels.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 05:34:13 pm
I'm calling this victory.
(http://i57.tinypic.com/11udvz8.jpg)
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: LigH on March 26, 2014, 06:00:56 pm
Well, fine, experiment succeeded. But don't make me buy that map. It will probably slow down the game each time I look at it?  :-\
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 06:24:40 pm
It's not much bigger than normal books. ~20k characters.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: Donari Tyndale on March 26, 2014, 07:06:20 pm
And for those who are interested how this works: It takes your .bmp and for each non white pixel writes "''" (Two 's), for each white pixel it writes " ".
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: derula on September 28, 2014, 04:13:51 pm
I'm calling this victory.

There wasn't even a battle.
You can't claim a victory when one of the contestants hasn't even shown up.
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: derula on September 29, 2014, 03:42:21 pm
I override your victory claim with this proof-of-concept (http://www.mediafire.com/download/t80s7cum6ab19vn/bmp2xml.zip). (Being a proof-of-concept, it comes without any instructions, but basically: install ruby & rmagick; replace test.png with any png of your liking; execute convert.rb. If it dies with "too bad so sad," it means that the object limit is exceeded. Try shrinking the bitmap, or reducing the number of colors.)

(http://i.imgur.com/xfxv5Vy.png)
Title: Re: BMP to XML (Aka importing drawings into PS)
Post by: derula on September 29, 2014, 03:47:18 pm
I have been informed by the wonderful person who drew that picture and took that screenshot for me that I should have given them credit. They also informed me that only n00bs double post. For my misdemeanor, I sincerely apologize. It will not happen ever again.