How to Replace RGB(0, 0, 0) to Other Color by DrawImage()?
How to Replace RGB(0, 0, 0) to Other Color by DrawImage()?
How to Replace RGB(0, 0, 0) to Other Color by DrawImage() ?
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
Well, one way is to find the pixels that are RGB(0,0,0) using Point(x,y), then changing them to the colour required using Plot(x,y,MyColour). So, If you have an image that is 100 x 100, something like this:
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()
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
Thank You, but I need read thousands of pictures, so I'm afraid I have no efficiency.
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
@ts-soft: Looks like it should be Plot(y, x..[] based on your loops. It's matterless here as w and h are the same but as soon as they aren't there will be a crash.
BERESHEIT
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
Uh ? Why ? Looks ok to me.netmaestro wrote:@ts-soft: Looks like it should be Plot(y, x..[] based on your loops.
"Have you tried turning it off and on again ?"
A little PureBasic review
A little PureBasic review
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
Because x is used for the outer loop and Plot() is xloc,yloc. See if this works without using Plot(y,x):
I don't think I've lost my mind on this (but it is possible) 
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

BERESHEIT
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
ehm... didn't you swap ImageWidth/ImageHeight ? 
As long as the ranges for the loops are correct, you can even swap the two loops (inner <-> outer) without consequences.

As long as the ranges for the loops are correct, you can even swap the two loops (inner <-> outer) without consequences.
"Have you tried turning it off and on again ?"
A little PureBasic review
A little PureBasic review
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
i have mild allergies to using Point/Plot over every pixel so have to use direct buffer manipulation but i ♥ it 
This version supports 24 & 32bit images, doesn't matter if ReversedY as not relevant to this procedure. For 32bit pics it ignores the Alpha channel when comparing colors but preserves its original value (Alpha channel shouldn't change). Only fast addition and bitwise operations are used in the main loop, cos that's how we roll!
FindColor/ReplaceColor are expected to be in the same BGRA/RGBA order as the image (DrawingBufferPixelFormat()) - I think it's best that the program establishes that byte order, not this function, because chances are it already knows and therefore would be a wasteful doubleup. But anyway here's a conversion if needed!

This version supports 24 & 32bit images, doesn't matter if ReversedY as not relevant to this procedure. For 32bit pics it ignores the Alpha channel when comparing colors but preserves its original value (Alpha channel shouldn't change). Only fast addition and bitwise operations are used in the main loop, cos that's how we roll!
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
- Michael Vogel
- Addict
- Posts: 2797
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: How to Replace RGB(0, 0, 0) to Other Color by DrawImage(
Just to add a code version including the DrawImage command... (please ignore the DummyImage procedure at the beginning, it's only to create an image for testing)
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