Author Topic: BMP to XML (Aka importing drawings into PS)  (Read 1126 times)

Donari Tyndale

  • Hydlaa Notable
  • *
  • Posts: 748
    • View Profile
BMP to XML (Aka importing drawings into PS)
« 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:




You can download the file here. It looks like this:



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
« Last Edit: March 26, 2014, 06:30:14 pm by Donari Tyndale »

bilbous

  • Guest
Re: BMP to XML (Aka importing drawings into PS)
« Reply #1 on: March 26, 2014, 08:57:57 am »
is that java?
looks really great

LigH

  • Forum Legend
  • *
  • Posts: 7096
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #2 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...
« Last Edit: March 26, 2014, 09:11:18 am by LigH »

Gag Harmond
Knight and Ambassador
The Royal House of Purrty

bilbous

  • Guest
Re: BMP to XML (Aka importing drawings into PS)
« Reply #3 on: March 26, 2014, 09:05:10 am »
well it is an exe so must be for windows.

Donari Tyndale

  • Hydlaa Notable
  • *
  • Posts: 748
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #4 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.

bilbous

  • Guest
Re: BMP to XML (Aka importing drawings into PS)
« Reply #5 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.
« Last Edit: March 26, 2014, 09:52:41 am by bilbous »

Dannae

  • Hydlaa Citizen
  • *
  • Posts: 250
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #6 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, 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

I've recently begun experimenting with creating map art that "appears" to have color fills.

Gilrond

  • Hydlaa Notable
  • *
  • Posts: 764
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #7 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.
« Last Edit: March 26, 2014, 11:58:26 am by Gilrond »

Donari Tyndale

  • Hydlaa Notable
  • *
  • Posts: 748
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #8 on: March 26, 2014, 11:59:47 am »
I really don't even know what SVG means. I just want to draw and convert.

Donari Tyndale

  • Hydlaa Notable
  • *
  • Posts: 748
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #9 on: March 26, 2014, 12:52:11 pm »
Also. This is quite suboptimal.


LigH

  • Forum Legend
  • *
  • Posts: 7096
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #10 on: March 26, 2014, 01:10:00 pm »
Scalable Vector Graphics.

The opposite of bitmaps.

Gag Harmond
Knight and Ambassador
The Royal House of Purrty

Gilrond

  • Hydlaa Notable
  • *
  • Posts: 764
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #11 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.

Donari Tyndale

  • Hydlaa Notable
  • *
  • Posts: 748
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #12 on: March 26, 2014, 02:10:21 pm »
So here's some new version:


Donari Tyndale

  • Hydlaa Notable
  • *
  • Posts: 748
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #13 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.

Gilrond

  • Hydlaa Notable
  • *
  • Posts: 764
    • View Profile
Re: BMP to XML (Aka importing drawings into PS)
« Reply #14 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.
« Last Edit: March 26, 2014, 02:59:13 pm by Gilrond »