Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pixel RGB

Posted on 2000-05-16
10
Medium Priority
?
545 Views
Last Modified: 2010-04-04
I need to read value of pixel color
Canvas.Pixels[x,y]
and to retrive info about red green and blue so I can manipulate with this values and after that I need to put this togather and to set Pixel to new color with canvas.pixels[x,y] := new value;

I have C/Java code but this dont work in Delphi beacouse oprenads are not equal.
C++/Java code

    q= Canvas->Pixels[x][y];
//  red=(q >>16) & 0xff ;//| (q<<16);
  z= z ? 0 : ((q>>0) & 0xff);//red
  z2= z2 ? 0:((q>>8) & 0xff);//green
  z3= z3 ? 0:((q>>16) & 0xff);//blue

0
Comment
Question by:xtvca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 6

Accepted Solution

by:
edey earned 120 total points
ID: 2814942
from delphi3.hlp:

If you specify TColor as a specific 4-byte hexadecimal number instead of using the constants defined in the Graphics unit, the low three bytes represent RGB color intensities for blue, green, and red, respectively. The value $00FF0000 represents full-intensity, pure blue, $0000FF00 is pure green, and $000000FF is pure red. $00000000 is black and $00FFFFFF is white.


hence you could try:

var
 r,g,b : byte;
 c : integer;
begin
 c : my_canvas.pixels[some_x,some_y];
 b := c shr 16;
 g := c shr 8;
 r := c;
end;


GL
Mike
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 2814962
The pixel property is very slow. Are you reading the pixels of a bitmap? Then use scanline instead.
0
 
LVL 2

Expert Comment

by:brainware
ID: 2815119
FastDIB is a hmm good DIB Class with easy and fast access to Pixels -
http://www.jps.net/gfody
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:edey
ID: 2815432
or you can try this:

var
 r,g,b : byte;
 c : integer;
 p : PByteArray;
begin
 p := my_bitmap.scanline[some_y];
 c := p[some_x];
 b := c shr 16;
 g := c shr 8;
 r := c;
end;


this is much faster, esp. if you arange to check pixels all on one scanline, ex.

var
 r,g,b : byte;
 c,ix,iy : integer;
 p : PByteArray;
 buf : TBitmap;
begin
 buf := TBitmap.create;
 buf.pixelFormat := pf24bit;
 buf.width := clientWidth;
 buf.height := clientHeight;
 for iy := 0 to buf.height-1 do
 begin
  p := buf.scanline[iy];
  for ix := 0 to buf.width-1 do
  begin
   p[ix*3] := ix;  //blue
   p[ix*3+1] := ix;//green
   p[ix*3+2] := ix;//red
  end;
 canvas.draw(0,0,buf)//draws a bunch a grey lines to the form
end;


GL
Mike

0
 
LVL 1

Expert Comment

by:flooder
ID: 2816434
The best option I think is to use the shr option proposed by edey it is the fastest.
There is a easier way to do it that work just as well but is slower you can use windows api functions
r,g,b:byte;
k:tcolor;

r:=GetRValue(Canvas.Pixels[x,y]
)
GetGValue
GetBValue
 and
k:=rgb(r,g,b);
Canvas.Pixels[x,y]:=rgb(r,g,b);

I thinks this api's is in the Windows.pas unit.


0
 

Author Comment

by:xtvca
ID: 2816510
Thanks for great response  !
I will accept edey answer beacouse he was first.

Thnks & Regards
Vedran
0
 

Author Comment

by:xtvca
ID: 2816515
P.S. how to put this r,g,b back to integer which represent TColor value ?

0
 
LVL 1

Expert Comment

by:flooder
ID: 2817381
use the rgb function
RGB(Value1,Value2,Value3)
I am not sure in what order the colours is i think first red than green,blue
the result is a tcolor value
0
 
LVL 6

Expert Comment

by:edey
ID: 2817644
Glad to be of service :)

if you need to recreate the TColor (see above for a 24 bit alternative) simply try the reverse, so if

b := c shr 16;
g := c shr 8;
r := c;

then

c := r + (g shl 8) + (b shl 16)


GL
Mike
0
 

Author Comment

by:xtvca
ID: 2818610
Thanks again edey & flooder
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question