Page 1 of 1

Loadimage() flag to obtain a 32 bits image from a 24b file

Posted: Thu Jun 09, 2011 10:11 am
by djes
Well, all is said in the title ;)

Loadimage() can load 24 bits images, but to alter them with other 32 bits images (applying an alpha text on it for example), you have to create a 32 bits image, load the 24 bits one, copy it on the 32 bits and free the 24 bits.

So it would be nice to have a flag to load the 24 bits image and put it directly into a 32 bits buffer (with alpha set or not).

Re: Loadimage() flag to obtain a 32 bits image from a 24b fi

Posted: Mon Aug 01, 2016 6:23 pm
by wilbert
I know this is an old request but it would be very useful to have this ability.

Re: Loadimage() flag to obtain a 32 bits image from a 24b fi

Posted: Tue Aug 02, 2016 9:41 pm
by netmaestro
Maybe it's useful, I'm not sure. But it isn't needed for the OP's stated purpose as a 24bit output is fine as a target for alphablended operations. Note the identical result of applying alphablended text on each target:

Code: Select all

;24 Bits target output:
CreateImage(0, 320, 60, 24, RGB(0,0,100))
StartVectorDrawing(ImageVectorOutput(0))
  VectorSourceLinearGradient(0, 0, 400, 0)
  VectorSourceGradientColor(RGBA(255, 0, 0, 164), 0.0)
  VectorSourceGradientColor(RGBA(0, 255, 0, 88), 0.5)
  VectorSourceGradientColor(RGBA(0, 0, 255, 40), 1.0)
  VectorFont(LoadFont(0, "Arial Black", 24))
  MovePathCursor(20,0)
  AddPathText("Hello World!")
  FillPath()
StopVectorDrawing()

; 32 Bits target output:
CreateImage(1, 320,60,32,RGB(0,0,100))
StartVectorDrawing(ImageVectorOutput(1))
  VectorSourceLinearGradient(0, 0, 400, 0)
  VectorSourceGradientColor(RGBA(255, 0, 0, 164), 0.0)
  VectorSourceGradientColor(RGBA(0, 255, 0, 88), 0.5)
  VectorSourceGradientColor(RGBA(0, 0, 255, 40), 1.0)
  VectorFont(LoadFont(0, "Arial Black", 24))
  MovePathCursor(20,0)
  AddPathText("Hello World!")
  FillPath()
StopVectorDrawing()
  
OpenWindow(0,0,0,320,240,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(0,0,20,0,0,ImageID(0))
ImageGadget(1,0,90,0,0,ImageID(1))
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Loadimage() flag to obtain a 32 bits image from a 24b fi

Posted: Wed Aug 03, 2016 5:24 am
by wilbert
netmaestro wrote:Maybe it's useful, I'm not sure. But it isn't needed for the OP's stated purpose as a 24bit output is fine as a target for alphablended operations. Note the identical result of applying alphablended text on each target:
You are right netmaestro, for a rendering target it doesn't make a difference.

In my case, it's about interacting with technologies that can improve speed like SSE2 or OpenCL.
A x86 CPU simply isn't built to move 24 bit integer values to/from memory.
Of course you can do the conversion yourself but that takes additional processing time and it probably is easy for Fred to create the image as 32 bit in the first place.
There already is an optional unused flag for LoadImage. This would be a nice case to start using it :)

Re: Loadimage() flag to obtain a 32 bits image from a 24b fi

Posted: Wed Aug 03, 2016 8:32 am
by djes
Note the date of my post... Actually my request concern was about bitmap images, I had to work with hundred of png files and I was not using the vector library : it was not created yet !
Anyway, by now I don't care so much...

Re: Loadimage() flag to obtain a 32 bits image from a 24b fi

Posted: Wed Aug 03, 2016 8:45 am
by Keya
djes wrote:Anyway, by now I don't care so much...
well I still do, so you stick by your Feature Request damnit! lol. Actually i was about to use this just a few days ago (wanted to load 24bit bmp into 32bit image and manually provide my own transparency rather than using PNGDecoder) as I knew LoadImage had Flags, but when i checked the docs the Flag parameter is currently just reserved - i wonder what for :)

netmaestro,
Very cool! but am wondering can your trick be used for just regular alphablending between two images or can it also be used for transparent background? (ie. even if you don't know what the current theme's background color is)

In the meantime i've been using this 24-to-32 variation of my image 32-to-24, simple and effective but less efficient and quite a bit more overhead than would be required by a LoadImage-only method due to requiring quite a few different gfx funcs and effectively two images:

Code: Select all

Procedure.i ConvertImage24to32(hImg24)    ;returns hImg32 (leaves hImg24 untouched, FreeImage it if you like)
  Protected width,height, hImg32
  If ImageDepth(hImg24) <> 24: ProcedureReturn 0: EndIf
  width = ImageWidth(hImg24)
  height = ImageHeight(hImg24)
  hImg32 = CreateImage(#PB_Any, width, height, 32)
  If hImg32 = 0: ProcedureReturn 0: EndIf  
  If StartDrawing(ImageOutput(hImg32)) = 0
    FreeImage(hImg32):   ProcedureReturn 0
  EndIf
  DrawingMode(#PB_2DDrawing_Default)
  DrawImage(ImageID(hImg24),0,0,width,height)
  StopDrawing()
  ProcedureReturn hImg32
EndProcedure