How to Replace RGB(0, 0, 0) to Other Color by DrawImage()?
Posted: Sat Sep 07, 2013 1:01 am
How to Replace RGB(0, 0, 0) to Other Color by DrawImage() ?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
StartDrawing(ImageOutput(#MyImage))
For x = 0 To 99
For y = 0 To 99
iColour = Point(x,y)
If(iColour = RGB(0,0,0) : Plot(x,y,RGB(255,255,255)) : EndIf
Next y
Next x
StopDrawing()
Code: Select all
Procedure FilterCallback(x, y, SourceColor, DestColor)
If SourceColor = RGB(0, 0, 0) : SourceColor = RGB(255, 255, 255) : EndIf
ProcedureReturn SourceColor
EndProcedure
CreateImage(0, 100, 100)
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_CustomFilter)
CustomFilterCallback(@FilterCallback())
For x = 0 To 99
For y = 0 To 99
If y % 2
Plot(x, y, RGB(0, 0, 0))
Else
Plot(x, y, RGB(128, 128, 128))
EndIf
Next
Next
StopDrawing()
OpenWindow(0, #PB_Ignore, #PB_Ignore, 200, 200, "")
ImageGadget(0, 50, 50, 100, 100, ImageID(0))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Uh ? Why ? Looks ok to me.netmaestro wrote:@ts-soft: Looks like it should be Plot(y, x..[] based on your loops.
Code: Select all
Procedure FilterCallback(x, y, SourceColor, DestColor)
If SourceColor = RGB(0, 0, 0) : SourceColor = RGB(255, 255, 255) : EndIf
ProcedureReturn SourceColor
EndProcedure
CreateImage(0, 200, 100)
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_CustomFilter)
CustomFilterCallback(@FilterCallback())
For x = 0 To ImageHeight(0)-1
For y = 0 To ImageWidth(0)-1
If y % 2
Plot(x, y, RGB(0, 0, 0))
Else
Plot(x, y, RGB(128, 128, 128))
EndIf
Next
Next
StopDrawing()
OpenWindow(0, #PB_Ignore, #PB_Ignore, 200, 200, "")
ImageGadget(0, 50, 50, 100, 100, ImageID(0))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Select all
Procedure ReplaceColor(hImg, width, height, FindColor.l, ReplaceColor.l, bytesperpixel) ;bytesperpixel=3 or 4 (24/32bit)
Protected x,y, *pixel.Long, steprow, FindColorOnly = FindColor & $00FFFFFF
If hImg
If StartDrawing(ImageOutput(hImg))
*pixel = DrawingBuffer()
steprow = (DrawingBufferPitch() - (width * bytesperpixel))
For y = 1 To height
For x = 1 To width
If (*pixel\l & $00FFFFFF) = FindColorOnly
*pixel\l = ReplaceColor | (*pixel\l & $FF000000)
EndIf
*pixel + bytesperpixel
Next x
*pixel + steprow
Next y
StopDrawing()
EndIf
EndIf
EndProcedure
Code: Select all
Procedure RGBAtoBGRA(pixel) ;and vice versa
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
!mov rax, [p.v_pixel]
CompilerElse
!mov eax, [p.v_pixel]
CompilerEndIf
!bswap eax
!ror eax, 8 ;restore Alpha
ProcedureReturn ;eax/rax
EndProcedure
Code: Select all
#SwapBlackColor=1
Procedure DummyImage(id,x=0,y=0)
Protected i,z
#VoxMin=320 : #VoxMax=640 : #VoyMin=240 : #VoyMax=480
If x=0 : x=#VoxMin+Random(#VoxMax-#VoxMin) : EndIf
If y=0 : y=#VoyMin+Random(#VoyMax-#VoyMin) : EndIf
z=CreateImage(id,x,y)
If z
If id=#PB_Any
id=z
EndIf
StartDrawing(ImageOutput(id))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For i=0 To x>>3
Circle(Random(x),Random(y),Random(x>>4),$60000000|Random(#White))
Next i
StopDrawing()
ProcedureReturn z
EndIf
ProcedureReturn #Null
EndProcedure
Procedure FilterCallback(x,y,SourceColor,DestColor)
If SourceColor&$FFFFFF
ProcedureReturn SourceColor
Else
ProcedureReturn #Red
EndIf
EndProcedure
OpenWindow(0, 0, 0, 800,600, "-", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ImageGadget(1,0,0,800,600,0)
CreateImage(1,800,600)
DummyImage(2,640,400)
StartDrawing(ImageOutput(1))
If #SwapBlackColor
DrawingMode(#PB_2DDrawing_CustomFilter)
CustomFilterCallback(@FilterCallback())
EndIf
DrawImage(ImageID(2),80,100)
StopDrawing()
SetGadgetState(1,ImageID(1))
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow