Discussion:
Splitting RGB() values into Red, Green, Blue Integer components
(too old to reply)
lateropatero
2005-11-16 14:05:55 UTC
Permalink
Hi folks,

I'm something of a PB newcomer, so I'm sorry if I'm missing something
obvious here. I've written a proggram that loads a bitmap from a path
and reads RGB values from individual pixels in that Bitmap using GET
PIXEL. I can't figure out how to break the result of the GET PIXEL
operation up into its individual Red, Green, Blue values, so I can
assign them to individual variables like Redvar, Greenvar, Bluevar and
perform math operations with them before recomposing the processed
RGB() and writing it back to the pixel. Done it in other programming
languages but I can't figure out the syntax to get R, G, B as
individual integers from the GET PIXEL result in Powerbasic.

Also tried the DIB way as suggested in the helpfile example:

---------------------------------------------------------------------------------------------
' Change all red pixels to blue

LOCAL PixelPtr AS LONG PTR
GRAPHIC GET BITS TO bmp$
xsize& = CVL(bmp$,1)
ysize& = CVL(bmp$,5)
PixelPtr = STRPTR(bmp$) + 8
FOR i& = 1 TO xsize& * ysize&
IF @PixelPtr = BGR(%red) THEN @PixelPtr = BGR(%blue)
INCR PixelPtr
NEXT
GRAPHIC SET BITS bmp$

---------------------------------------------------------------------------------------------
-
Works fine (and fast!) but again, how do I go from the result
@PixelPtr to individual Red, Green, Blue values that I can assign to my
Redvar, Greenvar, Bluevar etc variables as 0 - 255 Integer values? The
reverse is a no brainer (e.g. RGB(Redvar, Greenvar, Bluevar).

Any help or pointers would be much appreciated. =}
Wilko
2005-11-16 19:38:42 UTC
Permalink
Post by lateropatero
Hi folks,
I'm something of a PB newcomer, so I'm sorry if I'm missing something
obvious here. I've written a proggram that loads a bitmap from a path
and reads RGB values from individual pixels in that Bitmap using GET
PIXEL. I can't figure out how to break the result of the GET PIXEL
operation up into its individual Red, Green, Blue values, so I can
assign them to individual variables like Redvar, Greenvar, Bluevar and
perform math operations with them before recomposing the processed
RGB() and writing it back to the pixel. Done it in other programming
languages but I can't figure out the syntax to get R, G, B as
individual integers from the GET PIXEL result in Powerbasic.
---------------------------------------------------------------------------------------------
' Change all red pixels to blue
LOCAL PixelPtr AS LONG PTR
GRAPHIC GET BITS TO bmp$
xsize& = CVL(bmp$,1)
ysize& = CVL(bmp$,5)
PixelPtr = STRPTR(bmp$) + 8
FOR i& = 1 TO xsize& * ysize&
INCR PixelPtr
NEXT
GRAPHIC SET BITS bmp$
---------------------------------------------------------------------------------------------
-
Works fine (and fast!) but again, how do I go from the result
@PixelPtr to individual Red, Green, Blue values that I can assign to my
Redvar, Greenvar, Bluevar etc variables as 0 - 255 Integer values? The
reverse is a no brainer (e.g. RGB(Redvar, Greenvar, Bluevar).
Any help or pointers would be much appreciated. =}
I think you need to divide by 255 in the right way. Don't know exactly
(who does?) but e.g. Blue may be mod (RGB / 255), green (RGB\255) or
something like that. If this does not help, let us know.

Wilko
Peter Manders
2005-11-16 20:45:27 UTC
Permalink
On 16 Nov 2005 06:05:55 -0800, "lateropatero"
Post by lateropatero
Hi folks,
I'm something of a PB newcomer, so I'm sorry if I'm missing something
obvious here. I've written a proggram that loads a bitmap from a path
and reads RGB values from individual pixels in that Bitmap using GET
PIXEL. I can't figure out how to break the result of the GET PIXEL
operation up into its individual Red, Green, Blue values, so I can
assign them to individual variables like Redvar, Greenvar, Bluevar and
perform math operations with them before recomposing the processed
RGB() and writing it back to the pixel. Done it in other programming
languages but I can't figure out the syntax to get R, G, B as
individual integers from the GET PIXEL result in Powerbasic.
---------------------------------------------------------------------------------------------
' Change all red pixels to blue
LOCAL PixelPtr AS LONG PTR
GRAPHIC GET BITS TO bmp$
xsize& = CVL(bmp$,1)
ysize& = CVL(bmp$,5)
PixelPtr = STRPTR(bmp$) + 8
FOR i& = 1 TO xsize& * ysize&
INCR PixelPtr
NEXT
GRAPHIC SET BITS bmp$
---------------------------------------------------------------------------------------------
-
Works fine (and fast!) but again, how do I go from the result
@PixelPtr to individual Red, Green, Blue values that I can assign to my
Redvar, Greenvar, Bluevar etc variables as 0 - 255 Integer values? The
reverse is a no brainer (e.g. RGB(Redvar, Greenvar, Bluevar).
Any help or pointers would be much appreciated. =}
At the start of your program:

#INCLUDE "WIN32API.INC"

Then try:

LOCAl rgbq AS RGBQUAD

FOR i& = 1 TO xsize& * ysize&
rgbq = @PixelPtr
INCR PixelPtr
'color components are in:
blue = rgbq.rgbBlue
green = rgbq.rgbGreen
red = rgbq.rgbRed
NEXT
--
Peter Manders.

The Entire Physical Universe, Including This Message, May One Day
Collapse Back into an Infinitesimally Small Space. Should Another
Universe Subsequently Re-emerge, the Existence of This Message in
That Universe Cannot Be Guaranteed.
Judson McClendon
2005-11-17 14:22:19 UTC
Permalink
Post by lateropatero
I'm something of a PB newcomer, so I'm sorry if I'm missing something
obvious here. I've written a proggram that loads a bitmap from a path
and reads RGB values from individual pixels in that Bitmap using GET
PIXEL. I can't figure out how to break the result of the GET PIXEL
operation up into its individual Red, Green, Blue values, so I can
assign them to individual variables like Redvar, Greenvar, Bluevar and
perform math operations with them before recomposing the processed
RGB() and writing it back to the pixel. Done it in other programming
languages but I can't figure out the syntax to get R, G, B as
individual integers from the GET PIXEL result in Powerbasic.
Red = PixelColor& AND &HFF
Green = (PixelColor& \ &H100) AND &HFF
Blue = (PixelColor& \ &H10000) AND &HFF
--
Judson McClendon ***@sunvaley0.com (remove zero)
Sun Valley Systems http://sunvaley.com
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."
Loading...