create program to create bitmap

Game development specific discussions.
BasicCoder2
Posts: 3205
Joined: Jan 01, 2009 7:03

create program to create bitmap

Postby BasicCoder2 » Feb 15, 2017 2:08

Using this program you can convert a bitmap into data statements in a program that will generate the bitmap.
The program can then be posted to the forum to allow the generation of a bitmap from data statements.
I have provided such programs to generate images for another program posted on the forum that uses them.
Probably the bitmap shouldn't be more than 32 pixels wide keeping in mind you will end up with long data statements.
Also the more colors the larger the resulting palette colors() generated.
Before running the program enter the file name of the bitmap to be converted to data statements and the file name of the resulting bitmap to be saved as. Also you can choose the name of the resulting program that will create the bitmap from the data statements.

Code: Select all

Const NULL As Any Ptr = 0
screenres 640,480,32

dim shared as string fileName,fileName2,progName
fileName = "thumbsUp.bmp"      ' <----- change to desired bitmap to load
fileName2 = "thumbsUp2.bmp"            ' <----- change to desired name of save
progName  = "createThumbsUp2.bas"    ' <----- change to desired name of program created

dim shared as any ptr image
dim shared as integer iWidth,iHeight


sub loadImage(file as string)
    dim as integer fileHnd = FreeFile()
    'get width and height of bitmap
    open file for binary as #fileHnd
    get #fileHnd, 19, iWidth
    get #fileHnd, 23, iHeight
    close #fileHnd
    'initialize pointers to pic.i bitmaps
    image = imagecreate(iWidth,iHeight,rgb(255,255,255))
    bload file,image  'bload bitmap pic.i
    If image = NULL Then
        Print "image creation failed!"
        Sleep
        End
    end if
end sub

loadImage(fileName)

'=========== NOW EXTRACT COLOR PALETTE INTO colors LIST =======
dim shared as ulong colors(100000)
dim shared as ulong v
dim shared as integer totalColors,found

for j as integer = 0 to iHeight-1
    for i as integer = 0 to iWidth - 1
        v = point(i,j,image)  'get a color
        found = 0
        if totalColors > 0 then 'search for color
            for i as integer = 0 to totalColors-1
                if colors(i)= v then  'color found
                    found = 1
                end if
            next i
        end if
       
        if found=0 then 'not found so add to list
            colors(totalColors) = v
            totalColors = totalColors + 1
        end if
    next i
next j
'=====================================================

dim as ubyte r,g,b

Open progName For Output As #1

print #1,"screenres 640,480,32"
print #1,"dim as any ptr image"
print #1,"image = imagecreate(";iWidth;",";iHeight;")"
print #1,"dim as ulong colors(";totalColors;")"

for i as integer = 0 to totalColors-1
    v = colors(i)
    r = v shr 16 and 255
    g = v shr 8 and 255
    b = v and 255
    print #1,"colors(";i;")=RGB(";r;",";g;",";b;")"
next i

print #1, "    dim as integer n"
print #1, "    for j as integer = 0 to ";iHeight-1
print #1, "        for i as integer = 0 to ";iWidth-1
print #1, "            read n"
print #1, "            pset image,(i,j),colors(n)"
print #1, "        next i"
print #1, "    next j"

print #1, "bsave ";chr(34);filename2;chr(34);",image"
print #1,

'==============  now make sprite data statements =======================

dim as integer c  'palette number
dim as integer charCount
dim as string  sn  'to remove leading space
    for j as integer = 0 to iHeight-1
        charCount = 0
        print #1, "DATA ";
        for i as integer = 0 to iWidth-1
            if i<>0 then print #1,",";
            v = point(i,j,image)
            'find color in list and get palette number c
            for n as integer = 0 to totalColors-1
                if v = colors(n) then
                    c=n
                end if
            next n
            sn = str(c)
            print #1,sn;
            charCount = charCount + len(sn)
            if charCount > 50 then
                charCount = 0
                print #1,"_"
            end if
        next i
        print #1,
    next j
    print #1,

Close #1
print
print "A program called createBitMap.bas has been created"
print "If you load and run this program it will create"
print "and save the image as a file called"; fileName2

sleep

Last edited by BasicCoder2 on May 24, 2017 23:02, edited 1 time in total.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: create program to create bitmap

Postby thesanman112 » May 22, 2017 14:49

BasicCoder2, wouldn't it be easier to just open the BITMAP in binary and write a .bas file containing all the data for bitmap?

ive been working on a program to do just that as well. I convert the binary to HEX, seems to save a little space, then write the hex data to a bas file as data statements.

Code: Select all

''convert bmp to .bas datafile

ScreenRes 800, 600, 32
'Dim myImage As Any Ptr = ImageCreate( 64, 64 )
'BLoad "ship64.bmp", myImage
'Put (10,10), myImage
''ImageDestroy( myImage )
'Dim As Integer w, h, bypp, pitch,size
'Dim As Any Ptr pixdata
'Dim As Integer success
' success = (ImageInfo(myimage, w, h, bypp, pitch, pixdata,size) = 0)
'print
'print w,h,size
'close 
'sleep
dim as integer f1=freefile
open"ship64.bmp" for binary as f1
dim as integer f2=freefile
open"shipcopy.bmp" for binary as f2
dim as integer f3=freefile
open "data.bas" for output as f3
print lof(f1)
dim as string ds,hd
dim as integer d,a,dcount
for a=1 to lof(f1)
get #f1, ,d
put #f2, ,d
ds=hex(d)
dcount=dcount+1
hd=hd+ds+","
if dcount>20 then dcount=1:hd=rtrim(hd,","):print #f3, "data "+hd:hd=""
'if a<50 then print a;d;"*";ds;"*";cint("&h" & ds) <-----cint converts hex back to binary.
next
close

Sleep

Boromir
Posts: 406
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: create program to create bitmap

Postby Boromir » May 23, 2017 0:06

thesanman112 wrote:BasicCoder2, wouldn't it be easier to just open the BITMAP in binary and write a .bas file containing all the data for bitmap?

ive been working on a program to do just that as well. I convert the binary to HEX, seems to save a little space, then write the hex data to a bas file as data statements.

The goal of his program is to store images in plain text so they can be copy&pasted to forums and such.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: create program to create bitmap

Postby thesanman112 » May 23, 2017 0:09

Boromir, i know the goal, i posted some code, have a go at it, it works nicely to creat a .bas file full of data statements to copy and paste into your programs.
If needed i can add more code to it to make a bmp from the data statements as well...
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: create program to create bitmap

Postby thesanman112 » May 23, 2017 0:16

Also...there is a command function that should allow a user to just drag a bitmap over the exe and it will just do the work without us/users having to make up filenames and such.
Boromir
Posts: 406
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: create program to create bitmap

Postby Boromir » May 23, 2017 0:18

thesanman112 wrote:Boromir, i know the goal, i posted some code, have a go at it, it works nicely to creat a .bas file full of data statements to copy and paste into your programs.
If needed i can add more code to it to make a bmp from the data statements as well...

Nice! At first I thought it was creating a binary file. I can see this as useful.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: create program to create bitmap

Postby thesanman112 » May 23, 2017 0:22

What basicCoders program does is defines all the values and such, he litterally has broken down or decphyered the bitmap for us, which TECHNICALLY would be needed to find the dimentions of the bitmap if your going to use the drag and drop COMMAND functions on an Ex. What i did was say forget that, and get the binary values smaller lengths because we are going to store it visually in a bas file so we want it as small as possible maybe...
leopardpm
Posts: 1597
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 23, 2017 16:01

thesanman112 wrote: I convert the binary to HEX, seems to save a little space, then write the hex data to a bas file as data statements.

converting to hex (represented by ascii characters) saves a bit of space.... to get the full use of the 'copiable/pastable/seeable' ascii character set I think you can utilize 7 of the 8 bits available but you need to weed out the ascii codes which do not show on screen or do weird things... make a lookup table for converting the codes. To maximize space use, I would first do RLE compression on the binary data THEN convert to ASCII for the data statements. Having a header with some info about the image (dimensions mostly, perhaps color depth (1,8,16,24,32)) allows you to put the maximum amount of ascii characters into the data statements so that you also reduce the total number of statements. Besides using better compression techniques, this should provide the best, smallest, and easiest method of transferring images via forum posts.
BasicCoder2
Posts: 3205
Joined: Jan 01, 2009 7:03

Re: create program to create bitmap

Postby BasicCoder2 » May 23, 2017 21:41

The program was meant for small images with a reduced color set.
The program makes a palette of colors and the data statements hold palette numbers.
I don't see how hex numbers representing the rgb value for every pixel can be smaller in character length and I think you need to add the &H to those numbers in the data statements if you want to READ them.
A palette number can be as little as one character 0 to 9 or two characters 10 to 99. Really for larger images with many colors I don't think there is any practical way to reduce them to a size that can be known to be small enough to be posted as data statements.
.
leopardpm
Posts: 1597
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 23, 2017 23:11

BasicCoder2 wrote:I don't see how hex numbers representing the rgb value for every pixel can be smaller in character length and I think you need to add the &H to those numbers in the data statements if you want to READ them.

you do not put the '&H' in the data statement, only in the conversion routine (if you want to do it via that method, or you could just make a quick lookup table to convert the ascii code of the digit into a decimal number

Code: Select all

data "1B41E06056"


read in the string, converting it to decimal... this means that a 3 digit number (like 255) would only take 2 digits in the data statement ( FF ). But hex is inefficient, better to use as much of the ascii character set as possible:

A - Z = 26 numbers
a - z = 26 numbers
0 - 9 = 10 numbers
etc

I believe ascii codes from 32 through 125 give valid characters which are easily 'cut and pasteable' into the forum, and I think codes from 128 to 165 also. This gives 93 + 37 = 130 total numbers which can be represented by a single ASCII character. 7 bits = 128 values, so 1 character can easily store 7 full bits of data. This means 10 pixels of RGBA data (40 bytes of raw binary data, 320 bits) could be represented by a string of 47 characters in a data statement - pretty good!

Of course you are palettizing the images, which is a form of compression, but still encoding into ascii will reduce the total characters in all the data statements significantly
BasicCoder2
Posts: 3205
Joined: Jan 01, 2009 7:03

Re: create program to create bitmap

Postby BasicCoder2 » May 23, 2017 23:21

leopardpm wrote:you do not put the '&H' in the data statement,


Code: Select all

data "1B41E06056"


Two quote characters or &H I see no difference unless you mean two quotes for a whole string of characters as I have done in other examples using characters instead of decimal numbers but that is again making a palette first and assigning a character to a palette number.
.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: create program to create bitmap

Postby thesanman112 » May 24, 2017 0:30

Also....dont know for sure but we should be able to just input the character string from file and use mki to convert, just not sure about building data statements out of input'd from file character strings....anyways, it would use 4 characters long instead of 8 hex characters.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: create program to create bitmap

Postby thesanman112 » May 24, 2017 0:33

Leopardpm....im not sure if all characters will write to the data statements? Its easy enough to make the integer value using mki with the data input from file.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: create program to create bitmap

Postby thesanman112 » May 24, 2017 0:56

Basiccoder2's method is pretty good at being small enough, but its based on 4 or 5 colors, i wonder if we could modify that method and make 3 data fields in the data statements to cover the rgb values, so a 64x64 image would have 3 , 64x64 data statement builds.
leopardpm
Posts: 1597
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 24, 2017 6:42

ok fine, ya'll are forcing me to actually code it up, huh? drats, I have been avoiding doing this just because I ultimately feel it is easier just to link to a hosted image than to first convert it then have others have to unconvert it... but it is an interesting exercise, right? lol... getting married on Thursday so I don't expect much coding over the next few days though....

Leopardpm....im not sure if all characters will write to the data statements?

They don't. Of the entire ASCII set, I think I found about 190+ characters that actually print out... so I only expect to use 128 characters (7 bits of info).

Two quote characters or &H I see no difference unless you mean two quotes for a whole string of characters as I have done in other examples

yes... a quote at beginning and end of the entire string of perhaps 200 characters per DATA statement...unless quote characters are not required for putting string data into a DATA statement.

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 2 guests