Solved

SuperSpeed access to pixels by DirectX

Posted on 2004-04-25
7
505 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

13 Experts available now in Live!

Get 1:1 Help Now