[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SuperSpeed access to pixels by DirectX

Posted on 2004-04-25
7
Medium Priority
?
519 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
4 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 400 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

829 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