How to use the mask color with a bmp image on freebasic?

New to FreeBASIC? Post your questions here.
Cpcdos
Posts: 204
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

How to use the mask color with a bmp image on freebasic?

Postby Cpcdos » Mar 06, 2013 19:12

Hello World!
I want to know how to use the mask color freebasic BUT with a BMP image with magenta (RGB 255, 0, 255)
for delete this background color when i put a BMP image
Is it possible?
Because , i unable to use the alpha channel ..

Thank. :)
counting_pine
Site Admin
Posts: 6026
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to use the mask color with a bmp image on freebasic?

Postby counting_pine » Mar 07, 2013 20:30

Hi,
If you are using a full-colour graphics mode, then you can mask out magenta in PUT using the TRANS modifier. See /wiki/KeyPgTransGfx for an example.
Otherwise (for palette modes) the mask colour is always the 0th index, whatever colour that is.
Cpcdos
Posts: 204
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: How to use the mask color with a bmp image on freebasic?

Postby Cpcdos » Mar 09, 2013 10:16

Thank you for your reply
but, i have always a problem
i cannot mask out magenta with a BMP image loaded..
i tested with this code (any image with magenta [255,0,255] )

Code: Select all

declare function bmp_load( ByRef filename As Const String ) As Any Ptr
Const NULL As Any Ptr = 0
ScreenRes 320, 200, 16

'' set up an image with the mask color as the background.
Dim img As Any Ptr = ImageCreate( 32, 32, RGB(255, 0, 255) )

img = bmp_load("img.bmp" )
If img = NULL Then
    Print "bmp_load failed"
Else
    Put (10, 10), img , Trans
    ImageDestroy( img )
End If
'' free the image memory
ImageDestroy img
Sleep

'' file:///H:/FreeBASIC/doc/FreeBASIC/KeyPgBload.html
Function bmp_load( ByRef filename As Const String ) As Any Ptr
    Dim As Integer filenum, bmpwidth, bmpheight
    Dim As Any Ptr img

    '' open BMP file
    filenum = FreeFile()
    If Open( filename For Binary Access Read As #filenum ) <> 0 Then Return NULL

        '' retrieve BMP dimensions
        Get #filenum, 19, bmpwidth
        Get #filenum, 23, bmpheight

    Close #filenum

    '' create image with BMP dimensions
    img = ImageCreate( bmpwidth, Abs(bmpheight) )

    If img = NULL Then Return NULL

    '' load BMP file into image buffer
    If BLoad( filename, img ) <> 0 Then ImageDestroy( img ): Return NULL

    Return img
End Function
counting_pine
Site Admin
Posts: 6026
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to use the mask color with a bmp image on freebasic?

Postby counting_pine » Mar 12, 2013 0:57

Perhaps the colour is not exactly magenta.. Try using Hex(Point(x, y, img)); magenta pixels should have FF00FF for the last 6 digits.
By the way, the effects of using ImageDestroy on NULL are undefined, so you should only use it when bmp_load succeeds.
Cpcdos
Posts: 204
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: How to use the mask color with a bmp image on freebasic?

Postby Cpcdos » Mar 12, 2013 9:28

Hi,
HEX( Point(10, 10, img ))
make me " Error 41 : Variable required "
what do I do?
dafhi
Posts: 1225
Joined: Jun 04, 2005 9:51

Re: How to use the mask color with a bmp image on freebasic?

Postby dafhi » Mar 12, 2013 11:22

dim as uinteger valu = Hex( Point( 10,10, img ) ): print valu
Cpcdos
Posts: 204
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: How to use the mask color with a bmp image on freebasic?

Postby Cpcdos » Mar 12, 2013 16:10

On microsoft paint , i have assigned le magenta at 255 , 0 , 255
but on this freebasic program , i don't have FF00FF value ..
i just have 9CC742, That do ? :(

ps : i'm use "ScreenRes 320, 200, 16"
dafhi
Posts: 1225
Joined: Jun 04, 2005 9:51

Re: How to use the mask color with a bmp image on freebasic?

Postby dafhi » Mar 13, 2013 5:16

I believe, as counting_pine suggested, that the magenta in your bmp is off.

This might fix it

Code: Select all

Const NULL As Any Ptr = 0
Const Magenta565 = (31 shl 11) or 31

'' file:///H:/FreeBASIC/doc/FreeBASIC/KeyPgBload.html
Function bmp_load( ByRef filename As Const String ) As Any Ptr
    Dim As Integer filenum, bmpwidth, bmpheight
    Dim As Any Ptr img

    '' open BMP file
    filenum = FreeFile()
    If Open( filename For Binary Access Read As #filenum ) <> 0 Then Return NULL

        '' retrieve BMP dimensions
        Get #filenum, 19, bmpwidth
        Get #filenum, 23, bmpheight

    Close #filenum

    '' create image with BMP dimensions
    img = ImageCreate( bmpwidth, Abs(bmpheight) )

    If img = NULL Then Return NULL

    '' load BMP file into image buffer
    If BLoad( filename, img ) <> NULL Then ImageDestroy( img ): Return NULL

    Return img
End Function

Sub ColReplace16( byval img as any ptr, _OldCol as UInteger, NewCol as ushort = Magenta565 )
  if img = NULL then exit sub
  dim as any ptr pixels
  dim as integer wid,hgt,bypp,pitch, pitchBy2
  ImageInfo img, wid, hgt, bypp, pitch, pixels: pitchBy2 = pitch \ 2
 
  Union UnionARGB
    Type
      As UByte  B
      As UByte  G
      As UByte  R
      As UByte  A
    End Type
    As UInteger ARGB
  End Union

  dim as UnionARGB OldCol32: OldCol32.ARGB = _OldCol
 
  with OldCol32
  dim as ushort OldCol16 = ((.R Shr 3) Shl 11) or ((.G Shr 2) Shl 5) or .B Shr 3
  end with
 
  for y as short ptr = pixels to pixels + (hgt-1)*pitch step pitchBy2
    for x as short ptr = y to y + wid-1
      if *x = OldCol16 then *x = NewCol
    Next
  Next

End Sub

'' -------
''  Main
'' -------

ScreenRes 320, 200, 16

Dim As Any Ptr img = bmp_load("img.bmp" )
ColReplace16( img, point( 10, 10, img ), Magenta565 )

If img = NULL Then
    Print "bmp_load failed"
Else
    Put (10, 10), img , Trans
    ImageDestroy( img )
End If

Sleep
Last edited by dafhi on Mar 13, 2013 9:14, edited 1 time in total.
Cpcdos
Posts: 204
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: How to use the mask color with a bmp image on freebasic?

Postby Cpcdos » Mar 13, 2013 8:39

Thank you for your code, he work!
But i have 3 warning (see picture)
it's normal ?
Image
fxm
Posts: 8529
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: How to use the mask color with a bmp image on freebasic?

Postby fxm » Mar 13, 2013 9:10

Try to declare 'pixels' as a 'short ptr':

Code: Select all

  dim as integer wid,hgt,bypp,pitch,pitchBy2
  dim as short ptr pixels
dafhi
Posts: 1225
Joined: Jun 04, 2005 9:51

Re: How to use the mask color with a bmp image on freebasic?

Postby dafhi » Mar 13, 2013 9:17

sample updated. thanks
Cpcdos
Posts: 204
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: How to use the mask color with a bmp image on freebasic?

Postby Cpcdos » Mar 13, 2013 10:01

Thank you, it work ! :D

And last question (see picture)
why green color (RGB : 152,199,68 ) it's transparent ? :S

Image (zoom for good see)
dafhi
Posts: 1225
Joined: Jun 04, 2005 9:51

Re: How to use the mask color with a bmp image on freebasic?

Postby dafhi » Mar 13, 2013 10:25

google Pixelformer, a simple program which will show u the alpha channel
Cpcdos
Posts: 204
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: How to use the mask color with a bmp image on freebasic?

Postby Cpcdos » Mar 13, 2013 13:17

Okay :)
Thank you very much at all people for your help!

Bye
counting_pine
Site Admin
Posts: 6026
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to use the mask color with a bmp image on freebasic?

Postby counting_pine » Mar 13, 2013 15:51

Just to say, if we need to do direct memory manipulation then something's going wrong somewhere, either with your code or in FB. It should be pretty much as straightforward as the code in the Trans example above.

Could you switch to a 32-bit graphics mode and see if it works there, and also give me the Hex(Point(...)) value again in 32-bit mode?

Can you also confirm what version of FreeBASIC you're using?

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest