I see that it's possible to draw shapes to an alpha channel, but is it possible to draw images to an alpha channel?
Basically I need to be able to transfer either the red,green or blue channel of an image to the the alpha channel of another image.
Or to be even more clear - I'm looking to create a utility that will transfer a specular map stored in a texture to the alpha channel of a another texture. Unity 3D users will understand why.
I know that I could set each alpha pixel using Plot(), but that's way too slow. So unless there's a way to draw images into the alpha channel then I fear my only solution will be to access the image buffer directly... Ugh.. scary..
I had already tried this approach but it seems that only the alpha channel from the source image is copied to the alpha channel of the destination image. So in my situation the alpha channel in the source images doesn't contain any data at all. Only the red, green or blue channels contain the data that I want to transfer to the alpha channel. Doesn't really matter which one as they all contain the exact same data.
InitSprite()
OpenWindow(0,0,0,640,480,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
LoadSprite(0,#PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
IW = SpriteWidth(0) - 1 : IH = SpriteHeight(0) - 1
StartTime = ElapsedMilliseconds()
StartDrawing(SpriteOutput(0))
For x=0 To IW
For y=0 To IH
Plot(x,y,Point(x,y))
Next
Next
StopDrawing()
MessageRequester("PLOT()","Time: " + Str(ElapsedMilliseconds() - StartTime) + " Ms",64)
StartTime = ElapsedMilliseconds()
StartDrawing(SpriteOutput(0))
For x=0 To IW
For y=0 To IH
Box(x,y,1,1,Point(x,y))
Next
Next
StopDrawing()
MessageRequester("BOX()","Time: " + Str(ElapsedMilliseconds() - StartTime) + " Ms",64)
Fred wrote:Please note than since 4.40, Plot() isn't that slow anymore, so it could worth a try.
It's still very slow and rather useless. Use Box() instead (can be used outside "drawing area" as well).
@FluidByte: Interesting idea. My comparisons came up roughly 159x faster with Box() than with Plot(). I never would have guessed it. Thanks for the heads up.
OpenWindow(0,0,0,640,480,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StartTime = ElapsedMilliseconds()
StartDrawing(WindowOutput(0))
For x=0 To 100
For y=0 To 100
; Plot(x,y,Point(x,y))
Box(x,y,1,1,Point(x,y))
Next
Next
StopDrawing()
MessageRequester("PLOT()","Time: " + Str(ElapsedMilliseconds() - StartTime) + " Ms",64)
StartTime = ElapsedMilliseconds()
StartDrawing(WindowOutput(0))
For x=0 To 100
For y=0 To 100
Box(x,y,1,1,Point(x,y))
Next
Next
StopDrawing()
MessageRequester("BOX()","Time: " + Str(ElapsedMilliseconds() - StartTime) + " Ms",64)
Last edited by kernadec on Tue Jul 13, 2010 7:47 am, edited 1 time in total.
Fred wrote:Please note than since 4.40, Plot() isn't that slow anymore, so it could worth a try.
I actually did it with Plot() before asking on the forum, the process was too slow. Plot would be okay if I was working with small images, but I'm dealing with images as large as 4096*4096.
I never really assumed that it was Purebasic's plot that was slow, but rather that it was the process of reading and writing individual pixels with the CPU that was slow.
Edit:
When I first tried it with Plot() I was using an older version of Purebasic. Just tried it again with the latest version and it's great!!
Last edited by starax on Tue Jul 13, 2010 8:40 am, edited 1 time in total.
InitSprite()
OpenWindow(0,0,0,640,480,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
LoadSprite(0,#PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
IW = SpriteWidth(0) - 1 : IH = SpriteHeight(0) - 1
StartTime = ElapsedMilliseconds()
StartDrawing(SpriteOutput(0))
For x=0 To IW
For y=0 To IH
Plot(x,y,#Red)
Next
Next
StopDrawing()
MessageRequester("PLOT()","Time: " + Str(ElapsedMilliseconds() - StartTime) + " Ms",64)
StartTime = ElapsedMilliseconds()
StartDrawing(SpriteOutput(0))
For x=0 To IW
For y=0 To IH
Box(x,y,1,1,#Red)
Next
Next
StopDrawing()
MessageRequester("BOX()","Time: " + Str(ElapsedMilliseconds() - StartTime) + " Ms",64)
The reason Plot() is so slow is actually the use of Point()
Without this command Plot() is faster than Box():
I'm confused ...
Yeah strange... Point() must not like sprites. It seems fine with images. Plot(x,y,Point(x,y)) is faster than Box(x,y,1,1,Point(x,y)) when drawing into a image.
InitSprite()
OpenWindow(0,0,0,640,480,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
CreateSprite(0,512,512)
IW = SpriteWidth(0) - 1 : IH = SpriteHeight(0) - 1
StartDrawing(SpriteOutput(0))
For x=0 To IW
For y=0 To IH
Plot(x,y,Point(1,1))
Next
Next
StopDrawing()
It just manages to complete before crashing if the sprites dimensions are reduced to 128*128
starax wrote:
I fear my only solution will be to access the image buffer directly... Ugh.. scary..
Dont be afraid about directly accessing the image buffer. PB comes with nativ support for that, and it's not that hard if you done it just once.
Here is a very simple example that copies the red channel of the source image to the alpha channel of the destination image by directly accessing the image buffers. It's only a example to show you that it's not that scary actualy.