Bitmap to SVG converter (grid mode)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Posts: 113
Joined: Nov 19, 2012 19:58

Bitmap to SVG converter (grid mode)

Postby Pitto » May 15, 2016 21:45


This small program converts a 24bpp BMP file 24bpp to a SVG grid file – It is not all my own bag :)
I've merged some code of the following topics:
how to get an image's dimensions? [solved!]
Get a pixels color in an image

The goal was to convert small bitmap to vector graphic for enhance some features of the pixel-art

Here's an example:

Code: Select all

'Simple utility to convert a 24 Bpp Bitmap file to a SVG file
'based on topics
'how to get an image's dimensions? [solved!]
'Get a pixels color in an image
'to run it: "bmp2svg filename.bmp"
#include ""

Type bitmap_header Field = 1
    bfType          As Ushort
    bfsize          As Uinteger
    bfReserved1     As Ushort
    bfReserved2     As Ushort
    bfOffBits       As Uinteger
    biSize          As Uinteger
    biWidth         As Uinteger
    biHeight        As Uinteger
    biPlanes        As Ushort
    biBitCount      As Ushort
    biCompression   As Uinteger
    biSizeImage     As Uinteger
    biXPelsPerMeter As Uinteger
    biYPelsPerMeter As Uinteger
    biClrUsed       As Uinteger
    biClrImportant  As Uinteger
End Type

Dim bmp_header As bitmap_header
dim as integer mx,my,mb           'mouse variables
dim as integer imgW,imgH          'size of image you will be loading
dim as any ptr myImage            'a pointer to your bitmap image
dim as integer x, y               'image processing coords
dim as uinteger r,b,g,p           'red,green,blue values of pixel p
dim as string file_name

'check if specified file exists
if __FB_ARGC__ > 1 then
   file_name = str(Command(1))
   if FileExists(file_name)  then
      print "file doesn't exists"
   end if
   print "input a valid bmp file"
   print "bmp2svg filename.bmp"
end if

Open file_name For Binary As #1
    Get #1,,bmp_header
Close #1

Print "width: " + str(bmp_header.biWidth) +"; Height: " + str(bmp_header.biHeight)
print "file will be created as " + file_name + ".svg"

screenres 640,480,32              'must use before imagecreate()

imgW = bmp_header.biWidth                        'width
imgH = bmp_header.biHeight                       'height

myImage = imagecreate(imgW,imgH)  'create memory buffer the size of your image
bload file_name,myImage         'copy from file to bitmap

put (0,0),myImage,pset            'display your image

Dim ff As UByte
ff = FreeFile
Open file_name + ".svg" For output As #ff

Print #ff, "<?xml version='1.0' standalone='no'?>"
Print #ff, "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' ''>"
Print #ff, "<svg width='" + str(imgW) + "mm' height='" + str(imgH) + "mm' version='1.1' xmlns=''>"
Print #ff, "<desc>Artwork made with a Freebasic SVG converter</desc>"

for x = 0 to bmp_header.biWidth - 1
   for y = 0 to bmp_header.biHeight - 1
      p = point(x, y,myImage)  'get pixel value at coordinate x, y
      r = p shr 16 and 255
      g = p shr 8 and 255
      b = p and 255
      Print #ff, "<rect x='" + str(x) + "mm' y='" + str(y) + "mm' width='1mm' height='1mm' fill='rgb("+str(r)+","+str(g)+"," + str(b) + ")'/>"
   next y
next x

Print #ff, "</svg>"
Close #ff

    getmouse mx,my,,mb
    p = point(mx,my,myImage)  'get pixel value at coordinate mx,my
    r = p shr 16 and 255
    g = p shr 8 and 255
    b = p and 255
    locate 20,1
    print "RGB: "+str(r)+", "+str(g)+", " + str(b) + "             "
    print "press ESC to exit"
    sleep 2 ' <--------------------important
loop until multikey(&H01)  'exit on esc key pressed
imagedestroy(myImage)  'clean up

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests