How to create a pointer to Timage?

Started by Baggey, December 18, 2021, 21:53:10

Previous topic - Next topic

Baggey

So, Im trying to create a TempGlyph:TImage how do i create a pointer to it? Yes im lost! and the help system is even more lost :-[

Anyone know what im doing WRONG?

Code (blitzmax) Select

       Graphics 800,600       
       
        Local _String:String="45"
        Local TempGlyph:TImage=CreateImage(8,16)
        'TempGlyph=GlyphImage[Character]
        Local TempPtr:TImage Ptr
        TempPtr=TempGlyph
        'Print "Pointer = "+TempPtr

        ' I would expect the first 8 bits to be changed to 45 Decimal or 00101101 Binary
        TempPtr=_String.ToInt
       
        DrawImage(TempGlyph,300,300)

        Repeat Until KeyHit(KEY_SPACE) Or AppTerminate()
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 24GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

Midimaster

This cannot work this way...

Keep always in mind that each pixel in a TPixMap or a TImage is not a single BIT (as you try) but a 4BYTE-sequence.
Means a glyph line like BIN=01001101 converts in a TPixMap to :
00 00 00 00  FF FF FF FF 00 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF 00 00 00 00 FF FF FF FF


better to see:
pixel1 (0) = 00 00 00 00
pixel2 (1) = FF FF FF FF
pixel3 (0) = 00 00 00 00
pixel4 (0) = 00 00 00 00
pixel5 (1) = FF FF FF FF
pixel6 (1) = FF FF FF FF
pixel7 (0) = 00 00 00 00
pixel6 (1) = FF FF FF FF


Why? Each pixel needs 4 bytes for the complete color information. The 4 bytes representing A-R-G-B.
A "No Glyph-Pixel-Bit" result in 4 Bytes 00 00 00 00.
A "Is Glyph-Pixel-Bit" result in 4 Byte FF FF FF FF.

Inside each TImage is a TPixmap that contains the pixel informations. So the classical way is to examine the BITS in the Glyph line and if yes write the corresponding pixel. This can be done with TPixmap commands:

Code (BlitzMax) Select
SuperStrict
Graphics 400,300
Local Image:TImage=CreateImage(8,16)

Local line%
Repeat
Cls
If MouseHit(1)
Image = Exchange(Image,line,45)
line  = line+1
EndIf
DrawText "TImage here: [  ]",40,100
DrawImage Image,155,100
DrawText "Hit the mouse to add glyph lines to TImage!",40,50
Flip
Until AppTerminate()

Function Exchange:TImage(Img:TImage, y%, Value:Int)
If y>15 Return img
Local pix:TPixmap=LockImage(Img)
Local bit%=128
For Local i%=0 To 7
If value & bit
pix.WritePixel i,y,$FFFFFFFF
Else
pix.WritePixel i,y,0
EndIf
bit = bit Shr 1
Next
UnlockImage img
Return Img
End Function


It is not neccessary to accelerate this with pointers. 10.000 calls of Exchange() are still below 1msec.


But I can show you how to do this with Pointers. For this you need a 4 bytes long pointer called INT PTR!
A Int Ptr jumps alway 4 bytes ahead.
Code (BlitzMax) Select
Function Exchange_B:TImage(Img:TImage, y%, Value:Int)
If y>15 Return img
Local pix:TPixmap=LockImage(Img)
Local bit%=128
Local Pointer:Int Ptr = Int Ptr(pix.pixels)
Local Pitch% = pix.pitch/4
For Local i%=0 To 7
If value & bit
Pointer[i+y*Pitch] = $FFFFFFFF
Else
Pointer[i+y*Pitch] = 0
EndIf
bit = bit Shr 1
Next
UnlockImage img
Return Img
End Function

This is 10 times faster. Now 100.000 calls of Exchange_B() are still below 1msec.
...back from Egypt

Baggey

#2
As always straight to the point! Thanks again :D

When you declare your Variables  Function Exchange:TImage(Img:TImage, y%, Value:Int) What does the % Actually Mean/Do.

Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 24GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

Midimaster

 ;D this is because I'm old-school...

I really try to change it, but when I write code very fast... it happens.

In former times we did not define variables by appending the type:
global A:Int= 5
but by using ShortCuts...
global A%= 5
The "%" means the same like ":INT".

There were some more:

A% = 5     ' means A is type Integer (was 16bit-Integer in the age of 8bit computer's)
B# = 5.0   ' means B is type Floting point
C$ = "5"   ' means C is type String


BlitzMax still accepts this old school appendices.

A% = 5     ' defined as A is type 32bit Integer
' equals...
A:Int = 5

B# = 5.0   ' defined as B is type 32bitFloting point
' equals...
B:FLOAT = 5.0

C$ = "5"   ' defined as C is type String
' equals...
C:STRING = "5"

...back from Egypt

Baggey

Ive created my function following your HELP!

Code (blitzmax) Select

    '
    '
    Function AlterGlyphImage(Character:Int, Row:Int, Value:Int)
        '
        ' We must deccrease Row by one This is todo with Event
        Row:-1
        '
        If Row>15 Return 'img
            Local pix:TPixmap=LockImage(GlyphImage[Character])
            Local bit:Int=128
                    For Local i:Int=0 To 7
                            If value & bit
                                    pix.WritePixel i,Row,$FFFFFFFF
                            Else
                                    pix.WritePixel i,Row,0
                            EndIf
                            bit = bit Shr 1
                    Next
            UnlockImage GlyphImage[Character]
        '
    End Function
    '
    '


I Didnt pass the "img:TImage" Within the function header.
I suppose this is a pointer to the image it's self. Is there any benifits for doing it this way? As i think ive done it Global'y which still works?  :-X

Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 24GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!