FreeImage and Displaying Images *SOLVED*

New to FreeBASIC? Post your questions here.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

FreeImage and Displaying Images *SOLVED*

Postby datwill310 » Apr 04, 2017 18:04

I have managed to correctly link the FreeImage DLL to my program. I have a 256x256 test image stored in PNG format. I am able to load, unload, and access various information about this image, such as dimensions, formats, and pixel sizes (it's the furthest I've ever gone to incorporating PNG support in any of my apps :happy-tearful-face:).

However, I can't for the love of me display that image on the screen.

I first tried to work with the examples given in the latest documentation for the library, and that only produced a few dozen random pixels (using direct memory access using FI's methods). Then I tried the pixel colour retrieval method, and that isn't displaying ANYTHING.

Here is my code (for my second method):

Code: Select all

#include "freeimage.bi"

screen 20

dim as FIBITMAP ptr imgtest = _FreeImage_Load(_FreeImage_GetFileType("assets\img\1.png"), "assets\img\1.png")

for y as uinteger = 0 to _FreeImage_GetHeight(imgtest)-1
   for x as uinteger = 0 to _FreeImage_GetWidth(imgtest)-1
      dim as RGBQUAD col
      _FreeImage_GetPixelColor(imgtest, x, y, @col)
      pset (x, y), rgba(col.rgbRed, col.rgbGreen, col.rgbBlue, col.rgbReserved)
   next
next

sleep
_FreeImage_Unload(imgtest)


I have debugged the values of GetPixelColor, the col. members, and rgba(), they all produce valid values. But when put together, it doesn't work at all!

A note: I have had to prepend underscores to all the symbols because when I constructed the input library for the DLL, pexport and dlltool seemed to do this. BTW, do any end-users of apps using FreeImage (as an example) require the input library to run the software or is it only for compilation?

I feel like I'm spamming the forum a bit.

Also I would've posted in the Libraries section, but posts here and in General in my experience seem to get answered to a lot more quickly/get answered to at all :).
Last edited by datwill310 on Apr 09, 2017 13:30, edited 1 time in total.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: FreeImage and Displaying Images

Postby datwill310 » Apr 05, 2017 13:20


Not yet; I will have a good look! Very helpful to convert FIBITMAP images to FB images! Will get back to you once I test it.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: FreeImage and Displaying Images

Postby datwill310 » Apr 05, 2017 14:16


It works perfectly! Thanks to you and cha0s!
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeImage and Displaying Images

Postby MrSwiss » Apr 05, 2017 18:59

datwill310 wrote:Also I would've posted in the Libraries section, but posts here and in General in my experience seem to get answered to a lot more quickly/get answered to at all :).
I don't agree with your reasoning, because IMHO, the differences are as follows:
- since there are plenty different lib's
- since no one on this forum uses *all* of them (if any at all)
- therefore experienced users (with any lib, specified) are few
those are the reasons, that there are fewer answers, as well as it might take longer, to get one.

In short: ask library related questions, in the right place (Libraries Sub-Forum).
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: FreeImage and Displaying Images

Postby datwill310 » Apr 06, 2017 1:19

datwill310 wrote:

It works perfectly! Thanks to you and cha0s!

Actually, I think I've come across an issue with images containing alpha. They just render as black boxes.
fxm
Posts: 10046
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeImage and Displaying Images

Postby fxm » Apr 06, 2017 6:11

Are you well using:
Screen mode , depth , num_pages , FB.GFX_SHAPED_WINDOW
or
ScreenRes width, height , depth , num_pages , FB.GFX_SHAPED_WINDOW
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: FreeImage and Displaying Images

Postby datwill310 » Apr 06, 2017 11:55

fxm wrote:Are you well using:
Screen mode , depth , num_pages , FB.GFX_SHAPED_WINDOW
or
ScreenRes width, height , depth , num_pages , FB.GFX_SHAPED_WINDOW

I am not using the num_pages parameter, and I am also not using the GFX_SHAPED_WINDOW flag. However, I'll test out your suggestions!
fxm
Posts: 10046
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeImage and Displaying Images

Postby fxm » Apr 06, 2017 12:42

It is only mandatory to use the GFX_SHAPED_WINDOW flag.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: FreeImage and Displaying Images

Postby datwill310 » Apr 06, 2017 13:52

fxm wrote:It is only mandatory to use the GFX_SHAPED_WINDOW flag.

I implemented this flag, and the images still render as black squares wherever alpha is used.
fxm
Posts: 10046
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeImage and Displaying Images

Postby fxm » Apr 06, 2017 14:02

For higher depths than 8pp, the transparent color, when GFX_SHAPED_WINDOW flag is activated, is identified by looking just at the red (255), green (0) and blue (255) components of the color value.
Therefore for 32 bpp depth, available working colors for transparent mode are RGBA(255, 0, 255, alpha), with alpha blending not working because GFX_ALPHA_PRIMITIVES flag must be not activated for transparent mode.
(GFX_ALPHA_PRIMITIVES flag overwriting transparent mode effect requested by GFX_SHAPED_WINDOW flag)
Last edited by fxm on Apr 08, 2017 6:45, edited 5 times in total.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: FreeImage and Displaying Images

Postby datwill310 » Apr 06, 2017 14:05

fxm wrote:For higher depths than 8pp, the transparent color is identified by looking just at the red (255), green (0) and blue (255) components of the color value.
Therefore for 32 bpp depth, available working colors for transparent mode are RGBA(255, 0, 255, alpha).

Ah yes! This works. However, I would like to be able to draw alpha without shaping the actual window. How can I achieve this?
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeImage and Displaying Images

Postby MrSwiss » Apr 07, 2017 19:19

datwill310 wrote:How can I achieve this?
A little excerpt from *fbGFX.bi*:

Code: Select all

   const as integer GFX_NULL                = -1      , _
                GFX_WINDOWED            = &h00      , _
                GFX_FULLSCREEN            = &h01      , _
                GFX_OPENGL               = &h02      , _
                GFX_NO_SWITCH            = &h04      , _
                GFX_NO_FRAME            = &h08      , _
                GFX_SHAPED_WINDOW         = &h10      , _
                GFX_ALWAYS_ON_TOP         = &h20      , _
                GFX_ALPHA_PRIMITIVES      = &h40      , _    ' <-- used for alpha-stuff only
                GFX_HIGH_PRIORITY         = &h80
   '' OpenGL options
   const as integer GFX_STENCIL_BUFFER         = &h10000   , _
                GFX_ACCUMULATION_BUFFER   = &h20000   , _
                GFX_MULTISAMPLE         = &h40000
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: FreeImage and Displaying Images

Postby datwill310 » Apr 07, 2017 20:59

MrSwiss wrote:
datwill310 wrote:How can I achieve this?
A little excerpt from *fbGFX.bi*:

Code: Select all

   const as integer GFX_NULL                = -1      , _
                GFX_WINDOWED            = &h00      , _
                GFX_FULLSCREEN            = &h01      , _
                GFX_OPENGL               = &h02      , _
                GFX_NO_SWITCH            = &h04      , _
                GFX_NO_FRAME            = &h08      , _
                GFX_SHAPED_WINDOW         = &h10      , _
                GFX_ALWAYS_ON_TOP         = &h20      , _
                GFX_ALPHA_PRIMITIVES      = &h40      , _    ' <-- used for alpha-stuff only
                GFX_HIGH_PRIORITY         = &h80
   '' OpenGL options
   const as integer GFX_STENCIL_BUFFER         = &h10000   , _
                GFX_ACCUMULATION_BUFFER   = &h20000   , _
                GFX_MULTISAMPLE         = &h40000

Hi MrSwiss,

OK yes, I will post in the correct topics next time ;)

Yes, I have tried using this flag, but my graphics commands act differently with that flag on:
- Cls doesn't seem to work correctly, but with sGUI particularly: such that:
- The default sGUI background doesn't show up at all after clearly: it just shows what I intended to remove from the screen.
- The backgrounds for all controls are completely see through.
- Text is completely invisible.
- Before I draw sGUI controls, the screen doesn't clear at all, even though I have used cls, and it works without the flag.
- Alpha STILL renders black on the screen: I'm thinking the issue lies within the PNG -> fb.IMAGE conversion functions I got from cha0s in the above link.

Would like to pin-down what the issue is: I need alpha channel support or else the game is going to look very unfinished ;) with boxes around everything. Also isn't there a way in which I can use pink (i.e. 255, 0, 255) as a transparent colour but without reshaping the window?

Thanks again.
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FreeImage and Displaying Images

Postby MrSwiss » Apr 08, 2017 11:25

Hi datwill310,

I've no idea how (if at all) sGUI is influencing on Alpha etc. But in many cases Alpha on
it's own produces not really what one might expect.
Below a Union is used to be able to access every single Color-Channel: (aa, rr, gg, bb).
The combination of the same manipulation on Alpha- + Red-Channel seems to get the
expected result, just *uncomment* the red in the code, to check it out:

Code: Select all

' AlphaChannelWithUnion.bas -- 2017-04-08, by MrSwiss

Union colour
    As ULong clr    ' color
    Type
        As UByte a  ' alpha
        As UByte r  ' red
        As UByte g  ' green
        As UByte b  ' blue
    End Type
End Union

Dim As colour   c1, c2, c3
c1.clr = &hFF000000 ' black
c2.clr = &hFFFFFFFF ' white
c3.clr = &hFFFF0000 ' red

' ===== MAIN =====
ScreenRes(640, 480, 32,, 64)        ' GFX_ALPHA_PRIMITIVES = &h40
Color(c1.clr, c2.clr) : Cls

' size rectangle: screen size less 10 pixels border
Dim As Any Ptr IMG = ImageCreate(620, 460, &h00FF00FF, 32)

For x As UInteger = 0 To 619        ' width of rectangle
    For y As UInteger = 0 To 459    ' height ...
        Var chan = x * y Mod 256    ' initialize new channel value
        c3.a = chan                 ' modify alpha channel (0 to 255)
        ' with alpha alone, one might not get the desired effect! try red too
        'c3.r = chan                 ' modify red   channel (0 to 255)
        PSet IMG, (x, y), c3.clr    ' write a pixel to IMG
    Next
Next

Put (10, 10), IMG, Trans            ' IMG centered, to screen
ImageDestroy(IMG) : IMG = 0         ' clean up: release mem / zero ptr

Sleep
' ===== END-MAIN ===== ' ----- EOF -----

A little something about posting: plse. don't copy the whole post (comments, code and all),
just the section, you're referring to. This saves a lot of resources of the forum.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 9 guests