create program to create bitmap

Game development specific discussions.
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 17:01

... am also thinking that when the palette contains less than 128 (or 64,32,16,8,4) color values, then a good space saver is to use just the right number of bits for the color index... why use a full byte (1-255) for a color index if there are only 4 colors which could be stored as a 2 bit variable, right?

Adds a little bit of complication, but easy enough, especially worth it for the space savings...
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: create program to create bitmap

Postby MrSwiss » May 27, 2017 17:13

Just some comments on your code snippet:

Code: Select all

dim as byte a(7)        ' 8 elements 0..7
dim as string is8bit(7) ' as above
dim as string to7bit(8) ' 9 elements 0..8 -- can't be we use 8/7 bit's
dim as string holder = ""

a(0) = &h01 ' 1
a(1) = &h02 ' 2
a(2) = &h04 ' 4
a(3) = &h08 ' 8
a(4) = &h10 ' 16
a(5) = &h20 ' 32
a(6) = &h40 ' 64
a(7) = &h80 ' 128
Add them up and you'll get: 255 (surprise!) ;-)) -- on a UByte that is ... aka: &hFF
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: create program to create bitmap

Postby MrSwiss » May 27, 2017 17:47

BTW: I'm still not sure, what you are really trying to do.

Just some ideas:
1) unless you want to complicate matters, stick to a U/Byte (per point)
2) you'd need some sort of a table (call it a header), to enable decryption, later

Example (only 4 colors):
0 = &hFF000000 ' black (all: full Alpha, opaque)
1 = &hFFFF0000 ' red
2 = &hFF0000FF ' blue
3 = &hFFFFFFFF ' white

Without that, the prog. doesn't know, what the *replacement* of the *code* is.
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 17:48

MrSwiss wrote:Just some comments on your code snippet:

Code: Select all

dim as byte a(7)        ' 8 elements 0..7
dim as string is8bit(7) ' as above
dim as string to7bit(8) ' 9 elements 0..8 -- can't be we use 8/7 bit's
dim as string holder = ""

a(0) = &h01 ' 1
a(1) = &h02 ' 2
a(2) = &h04 ' 4
a(3) = &h08 ' 8
a(4) = &h10 ' 16
a(5) = &h20 ' 32
a(6) = &h40 ' 64
a(7) = &h80 ' 128
Add them up and you'll get: 255 (surprise!) ;-)) -- on a UByte that is ... aka: &hFF


I updated the code in the post to reflect your desires, both ways work though...
The initialized values in a(0) - a(6) are just some arbitrary numbers to test with, didn't mean anything
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 18:04

MrSwiss wrote:BTW: I'm still not sure, what you are really trying to do.

I guess I am not very good at explaining....
Here is the problem: we want to use the forum to host and transfer graphical files. The problem is that the forum (as far as copy and pastable characters) can only show a subset of the ASCII character set. So each character (even though is actually 8 bits) can only transfer 7 bits of info (these are printable characters from ASC(35) - ASC(163) = 128 values per ascii character.

So, lets say we have a graphic file consisting of 2 pixels: RED RGB(255,0,0) and BLUE rgb(0,0,255).
The BMP file looks like this: FF00000000FF (hex) and this 111111110000000000000000000000000000000011111111 (bin)
We 'could' just use regular ascii numbers to transfer in a data statement like this:
DATA 255,0,0,0,0,255
...and that would work fine. In fact, that is the fundamental way BasicCoder has been doing it until now.
BUT, notice, such a statement is taking up 20 BYTES! of forum space. The informational portion takes up 15 bytes.

So, my method is to convert each of the 8 bit values into 7 bit values and use the resulting ascii characters to transfer the file:
DATA "5gzW#*x".....<---I made up this converted string
notice that only 7 bytes are needed for the information...instead of 15. If we could represent all 256 values in a byte as a single ascii character, then no special conversion would be necessary, but we can't.

I think my next test example will show it better what I am doing...

Just some ideas:
2) you'd need some sort of a table (call it a header), to enable decryption, later

Example (only 4 colors):
0 = &hFF000000 ' black (all: full Alpha, opaque)
1 = &hFFFF0000 ' red
2 = &hFF0000FF ' blue
3 = &hFFFFFFFF ' white

Without that, the prog. doesn't know, what the *replacement* of the *code* is.

yes, there will be a header AND a color palette incorporated into the file. The header will contain information regarding the dimensions of the graphic, what compression was used, and the palette index bit size (as I posted a few posts back about reduced bit counts for color index numbers). This will make it easier for different versions of the utility to determine what is needed to convert & decompress the file.
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 18:38

I think this question will help get you on the same wave length...

What is the shortest string of characters that you can copy and paste into the forum to represent 56 bytes of 8 bit data?

My way does it using 64 bytes of modified ascii characters....

Pretending that the data in the 56 characters are the values 0 to 55, then here is the converted string:

#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ

here are all the ascii characters representing the numbers from 0 to 127
#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢

the character '#' represents the decimal value 0
the character 'A' represents the decimal value 30
etc... all the way to 127 if need be...

decrypted, they will result in the original 56 binary bytes of 8 bits each...
Last edited by leopardpm on May 27, 2017 18:41, edited 1 time in total.
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: create program to create bitmap

Postby MrSwiss » May 27, 2017 18:39

leopardpm wrote:printable characters from ASC(35) - ASC(163) = 128 values per ascii character
No, what you probably mean is: "we have a range of 128" (aka: the size of the palette, is = 128 Color's), correct?
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 18:48

MrSwiss wrote:
leopardpm wrote:printable characters from ASC(35) - ASC(163) = 128 values per ascii character
No, what you probably mean is: "we have a range of 128" (aka: the size of the palette, is = 128 Color's), correct?

forget about the palette for right now, focus on how many bits of information are able to be represented.... a normal ascii character can be the whole gamut from 0-255... but some of those are 'unprintable' and represent other things that cannot be copied and pasted in the forum. So, I am using each ascii character to represent a value of 0 to 127 (7 bits of info)

here are the characters:
#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢

their actual ascii codes are from 35 to 163 (i think). SO, in one forum character, we can represent a value from 0 to 127

Just as hex notation means each character represents a decimal value from 0 to 15. "FF" is two ascii characters whereas "255" is 3 ascii characters in the forum, so if we used hex notaion, the resulting ascii characters to represent bytes would take up less space then if we used decimal notation. My method just is more compact than hex notation and instead of each character being 0-15, each character is 0-127... the trick is to convert from 8 bit data bytes into 7 bit data bytes.... which is what I was showing in the above post...

whew! this is hard for me to explain, but I think it will make sense to you now...
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: create program to create bitmap

Postby MrSwiss » May 27, 2017 19:37

As far as I'm concerned, the whole thing is far simpler, than you make it to be.

The range of colors, "printable characters in CP 437" are: 33 to 254
(32 = Space, and 255 = "empty")
Using the CP switching code of mine, the utility can simply work with CP 437.
It is only internally switched and, switched back to *original*, before quitting.
This means: the users CP setting isn't affected at all (whatever CP is used).

The only thing left to do, is offsetting the String-Characters by 33, end of story.

The easiest way to do that, is IMO, with String [index] giving ASCII, then add/
subtract the 33, depending on en-/de-crypt ...
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 20:14

MrSwiss wrote:As far as I'm concerned, the whole thing is far simpler, than you make it to be.

The range of colors, "printable characters in CP 437" are: 33 to 254
(32 = Space, and 255 = "empty")
Using the CP switching code of mine, the utility can simply work with CP 437.
It is only internally switched and, switched back to *original*, before quitting.
This means: the users CP setting isn't affected at all (whatever CP is used).

The only thing left to do, is offsetting the String-Characters by 33, end of story.

The easiest way to do that, is IMO, with String [index] giving ASCII, then add/
subtract the 33, depending on en-/de-crypt ...

not a clue as to what a 'CP' is... or why it is needed.

from what I can understand above, each ascii character can represent a value between 33 and 254 = 222 individual values.... not an even number of bits so unless some very convoluted trick is used, then really only 7 bits (128 values) will be useful. That does not address the issue of converting from a series of 8 bit values into a series of 7 bit values, which is where I am in the testing programs I posted.

For instance, using your CP stuff, how many forum characters would it take you to represent 56 bytes, full 8 bit bytes, of information?

wait a sec, I think I see the issue you are having in understanding where I am coming from...
you said "The range of colors, "printable characters in CP 437" are: 33 to 254"... I am not talking colors here, I am talking raw data, 8 bit bytes. Doesn't matter what that data represents (either an RGB pixel, or a palette index, or a number, or whatever). RGB is just 24 bits of information... the question is to how to represent the information in the forum.... if each forum character can represent 7 bits, then it will take 4 characters to represent an RGB pixel color - I don't think that the CP stuff improves upon that, from what I gleen from it, you are talking about how the ascii character is shown on the screen... which doesn't matter as long as at least 128 ascii values are able to be shown for copy/paste, which the standard ascii does fine, no need to switch to another representation UNLESS that representation shows 0- 255, which would be an improvement of 1 bit (about 14%) in size reduction of the total # of characters.

I think I will just go ahead and do it, would be far easier than trying to explain...LOL!
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: create program to create bitmap

Postby MrSwiss » May 27, 2017 20:34

leopardpm wrote:not a clue as to what a 'CP' is... or why it is needed.
CP = code page.
You'd know by now, if you'd read my previous posts, in this thread. (since you've
just married = I'll forgive you, for once ;-)) , congratulations (even if late).)

The basic idea with the range(0 to 221) is to use it as INDEX into a ULong array,
holding the Colors representation: 163 = bright grey/blue (just an example)
You are never going to use true 7bit anyway, this just confuses the real issue.

What is the target?
1) Reduction of size (here: from 32bit to 8bit)
2) lossless (compression)
3) no DATA statements (String probably)
4) must be "printable" in Forum
... maybe forgot something important? ...
Last edited by MrSwiss on May 27, 2017 20:49, edited 1 time in total.
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 20:45

MrSwiss wrote:
leopardpm wrote:not a clue as to what a 'CP' is... or why it is needed.
CP = code page.
You'd know by now, if you'd read my previous posts, in this thread.

The basic idea with the range(0 to 221) is to use it as INDEX into a ULong array,
holding the Colors representation: 163 = bright grey/blue (just an example)
You are never going to use true 7bit anyway, this just confuses the real issue.

What is the target?
1) Reduction of size (here: from 32bit to 8bit)
2) lossless (compression)
3) must be "printable" in Forum
... maybe forgot something important? ...

yes, you are just talking about palettizing the image, a form of compression - hence your statement "reduction in size (here: from 32bit to 8bit)". Palettizing and all other compression are a different issue than what I am talking about.... OK, starting from a basic BMP RGB image with a variety of colors of pixels, AFTER you have palettized and/or compressed it, you still end up with a series of 8 bit bytes which represent the image in some form, right? What I am addressing is how to get all those bits into the forum most effectively..... and stuffing them into 7 bits/character is about the best/easiest to do.

here is your palette:
01 = RED &hFF0000
02 = BLUE &h0000FF

here is your image:
First byte is a palette index to RED, Second byte points to BLUE, then RED again:
010201

that is 6 characters in the forum to represent the image, I do that in 6 bits of 1 character....
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: create program to create bitmap

Postby MrSwiss » May 27, 2017 20:53

6 characters in the forum
This means: 6 U/Bytes = 48 bits

A Char in the Forum = 1 UByte (a String is an array of UByte, simplified)
Last edited by MrSwiss on May 27, 2017 21:08, edited 1 time in total.
dodicat
Posts: 6012
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: create program to create bitmap

Postby dodicat » May 27, 2017 21:07

Numbers to base 128 have a length of five digits representing the ulong color.
- and, as leopardpm suggested, codes between 35 and 163 look readable in Ides and the forum code slot.
I can generate the base 128 characters in this range, and can simulate a retrieval from the forum (hopefully)

Code: Select all

 

Dim Shared ADDQmod(0 To 19) As Ubyte
Dim Shared ADDbool(0 To 19) As Ubyte
For z As Integer=0 To 19
    ADDQmod(z)=(z Mod 10+48)
    ADDbool(z)=(-(10<=z))
Next z
Dim shared As integer _Mod(0 To 99),_Div(0 To 99)
For z As Integer=0 To 99:_Mod(z)=(z Mod 10+48):_Div(z)=z\10:Next
function Qmult(a as string,b as string) as string
      var flag=0,la = len(a),lb = len(b)
      If Len(b)>Len(a) Then flag=1:swap a,b:swap la,lb
dim as ubyte n,carry,ai
    var c =string(la+lb,"0")
    for i as integer =la-1 to 0 step -1
        carry=0:ai=a[i]-48
        for j as integer =lb-1 to 0 step -1
          n = ai * (b[j]-48) + (c[i+j+1]-48) + carry
              carry =_Div(n):c[i+j+1]=_Mod(n)
            next j
        c[i]+=carry
    next i
 if flag then swap a,b
   return ltrim(c,"0")
end function
Function plus(_num1 As String,_num2 As String) As String
    var _flag=0,n_=0
    Dim As Ubyte addup=Any,addcarry=Any
    #macro finish()
    answer=Ltrim(answer,"0")
    If _flag=1 Then Swap _num2,_num1
    Return answer
    #endmacro
    If Len(_num2)>Len(_num1) Then
        Swap _num2,_num1
        _flag=1
        Endif
        var diff=Len(_num1)-Len(_num2)
        var answer="0"+_num1
        addcarry=0
        For n_=Len(_num1)-1 To diff Step -1
            addup=_num2[n_-diff]+_num1[n_]-96
            answer[n_+1]=ADDQmod(addup+addcarry)
            addcarry=ADDbool(addup+addcarry)
        Next n_
        If addcarry=0 Then
            finish()
        End If
        If n_=-1 Then
            answer[0]=addcarry+48
            finish()
            Endif
            For n_=n_ To 0 Step -1
                addup=_num1[n_]-48
                answer[n_+1]=ADDQmod(addup+addcarry)
                addcarry=ADDbool(addup+addcarry)
                If addcarry=0 Then Exit For
            Next n_
            answer[0]=addcarry+48
            finish()
        End Function
       




 Function ConvertToBase10(Number As String) As String
            Dim As String sum
            sum=Str(Asc(Left(Number,1))-35)
            For n As Integer=2 To Len(Number)
                sum=plus(Qmult(sum,"128"),Str((Number[n-1]-35)) )
            Next n
           ' print
            Return sum
 End Function
 
#define R(c) ( c  Shr 16 And 255 )
#define G(c) (  c  Shr  8 And 255 )
#define B(c) (  c         And 255 )

 Screen 20,32
 Dim As String s
 Dim As ULong b10
 For n As Long=1 To 50
    Read s
    b10=ValULng(ConvertToBase10(s))
    Color b10
    Print "RGB(";R(b10);G(b10);B(b10);")"
 Next
 sleep
 Data _
"2¡ŽyU",_
"2¢aW|",_
"2›hƒa",_
"2 ‚n¡",_
"2¡Bez",_
"2žsu",_
"2›47Š",_
"2¢'=4",_
"2‚s",_
"2 œb4",_
"28?0",_
"2ž ;V",_
"2¢l¡”",_
"2›K–",_
"2¡–`h",_
"2 “hD",_
"2¢7y~",_
"2¡>ŸX",_
"2›_ƒ",_
"2œNŠi",_
"2žžlš",_
"2¢¢]‡",_
"2žbh3",_
"2¢P>]",_
"2 _‹+",_
"2›[mŠ",_
"2*8",_
"2¢d-k",_
"2 G",_
"2¢_yb",_
"2y@v",_
"2¢Hi<",_
"2 ‘+/",_
"2žH†",_
"2 gBF",_
"2¡CW;",_
"2 U:f",_
"2œU'8",_
"2 […K",_
"2}.)",_
"2$¢D",_
"2ž#gN",_
"2ŸŽu¡",_
"2|‡L",_
"2œT{‡",_
"2›uR™",_
"2¡EsŒ",_
"2 š6‚",_
"2›sJe",_
"2›?†„"
 
leopardpm
Posts: 1792
Joined: Feb 28, 2009 20:58

Re: create program to create bitmap

Postby leopardpm » May 27, 2017 21:20

exactly DOdicat!

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 1 guest