Solved

SuperSpeed access to pixels by DirectX

Posted on 2004-04-25
7
506 Views
Last Modified: 2010-04-05
Hi experts,
I want to get about 500 squares with different size from a bitmap and stretch them into 40 x 40 squares. and also READ and WRITE pixels very fast. count them and so (Access them snappy ;)
Can you give me some code to do it using DirectX because I need to know this fast method very fast too. Much faster than Scanline. at least converting a 320 x 240 bitmap to grayscale in less than 50 milliseconds (converting 100 times in 50 ms).
Look at this code:

var
bitmap:tbitmap;
p:pbytearray;
x,y,q,w,i:integer;
begin
bitmap:=tbitmap.Create;
bitmap.Assign(image1.Picture.Bitmap);
q:=gettickcount;
for i:=0 to 100 do begin
for x:=0 to bitmap.Height-1 do begin
p:=bitmap.ScanLine[x];
for y:=0 to bitmap.Width-1 do begin
if (p[y*3+2]>200) and (p[y*3+2]<255) then begin
p[y*3+0]:=255;
p[y*3+1]:=255;
p[y*3+2]:=255;
end;
end;
end;
end;
w:=gettickcount;
image1.Picture.Bitmap.Assign(bitmap);
showmessage:=(inttostr(w-q));
end;

In this code I looked for 200<redvalue<255 for 100 times. it takes about 32~47 milliseconds by an ATi Radeon 7000 64MB. and 450 milliseconds for 1000 scans.
I want it more than 50 times faster. DirectX can do this for sure. but how. I use DirectX to capture images from a ccd, now I have a bitmap assigned to the current frame. how can I access bitmap.pixels? (Please answer with codes, I don't want to be lazy but ...)
Thank U,

Comboy
0
Comment
Question by:Comboy
  • 2
7 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 10913545
As far as I know DirectX won't be much faster. But you can still optimize that code you posted. E.g. get rid of the "*3" and replace it by adding "3" after each pixel.

What is your final aim? Is it fast grayscaling?
0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 100 total points
ID: 10913702
You can also use this to optimize code:

type
  TLine = array [0..32767] of TRGBTriple;
  PLine = ^TLine;
var
  p : PLine; {not pbytearray}
..
for y := 0 to bitmap.Height - 1 do begin
  p := bitmap.ScanLine[y];
  for x := 0 to bitmap.Width - 1 do with p[x] do begin
    if (rgbtRed > 200) and (rgbtRed < 255) then begin
      rgbtRed   := 255;
      rgbtGreen := 255;
      rgbtBlue  := 255;
    end;
  end;
end;

p[y].rgbtRed, p[y].rgbtGreen, p[y].rgbtBlue will be the red, green and blue values of the pixel now (not p[y*3+0], p[y*3+1], p[y*3+2]).
0
 
LVL 1

Author Comment

by:Comboy
ID: 10916939
Hi,
to Madshi > Final aim is an object detection software.
to ZhaawZ > It's faster, but not fast enough :(

to Madshi > DirectX is not faster - this codes make the process 4 times faster, they're in GR32_Filters.pas

procedure ColorToGrayscale(Dst, Src: TBitmap32);
var
  I: Integer;
  D, S: PColor32;
begin
  CheckParams(Dst, Src);
  Dst.SetSize(Src.Width, Src.Height);
  D := @Dst.Bits[0];
  S := @Src.Bits[0];
  for I := 0 to Src.Width * Src.Height - 1 do
  begin
    D^ := Gray32(Intensity(S^));
    Inc(S); Inc(D);
  end;
  Dst.Changed;
end;

DirectDraw must be faster, if it's not then how can games be created, while moving in a scene, it's drawing many textures using many filters and rotating them, but it's still smooth.
0
 
LVL 20

Assisted Solution

by:Madshi
Madshi earned 100 total points
ID: 10917136
That code above is not much different to the scanline code, it's just a bit more optimated. That's why I said: "But you can still optimize that code you posted" in my first comment.

DirectX is mainly meant to DRAW graphics, not to read and analyze them. DirectX games are so fast, because the graphics card is optimized for games. It does most of the work, while the CPU only tells the graphics card what to do.

In contrast what you're doing has not much to do with games programming or with writing into the graphics card. You simply want to manipulate some pixels in a bitmap. The graphics card has nothing to do with that. It's a CPU and system RAM only operation. And so DirectX will not be able to speed it up much - if at all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now