Autor Beitrag
Fighter#1
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 787

Win XP, Ubuntu 8.04
Turbo Delphi 2006, Delphi 2005 Pe, Delphi 5 Pe, Netbeans 6.1, Eclipse, Microsoft VisualC#, Dev C++, PHP, HTML, CSS
BeitragVerfasst: Sa 07.01.06 15:42 
Ich habe schon den Thread Einzelne Pixels mit Scanline . . . durchgelesen aber inhaltlich find ich da nicht viel.
Kann mir jemand sagen wie ich aus einem Image die Pixel finde die einen bestimmten Farbwert,
der einfachheit halber schwarz haben.
Mit Pixels[x,y] kann ich das aber wie geht das mit Scanlines???


Moderiert von user profile iconChristian S.: Topic aus Dateizugriff verschoben am Sa 07.01.2006 um 15:50
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Sa 07.01.06 15:58 
Hallo,
erstelle dir ein einzeiliges Bitmap und experimentiere.
Scanline ist ein Zeiger auf array[0..BitMap.width] of tFarbe;
tFarbe =f(PixelFormat Property) siehe mal dies
www.delphipraxis.net/topic49118.html

Gruss Horst
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Sa 07.01.06 21:14 
Kleines Beispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
var
 p0, p: ^DWord;
 w,h,x,y: Integer;
begin
  w := 128; // breite
  h := 128; // höhe
  Image1.Picture.Bitmap.Width := w;
  Image1.Picture.Bitmap.Height := h;
  Image1.Picture.Bitmap.PixelFormat := pf32Bit;
  p0 := Image1.Picture.Bitmap.ScanLine[h-1];
  for x := 0 to w-1 do
   for y := 0 to h-1 do
   begin
     p := p0; // Startadresse für Bild
     inc(p, x+(h-y-1)*w); // Berechnung der Adresse für Pixel (x,y)

     if x*x+y*y < 128*128 then // falls innerhalb Kreis mit Radius 128
      p^ := $FF0000 else // rot einfärben
      p^ := $00FF00; // grün einfärben
   end;
  Image1.Invalidate; // bei Gelegenheit neu zeichnen
end;
Lossy eX
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1048
Erhaltene Danke: 4



BeitragVerfasst: So 08.01.06 17:14 
Als kleiner Optimierungsfanatiker würde ich empfehlen die Schleifen geringfügig anders aufbauan. Vor allem da man nicht immer davon ausgehen kann (sollte), dass die letzte Zeile als erstes im Speicher liegt. Und dann muss man auch nich für jedes Pixel die Position zu berechnen.

In etwa so.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
var
 p: ^DWord;
 w,h,x,y: Integer;
begin
  w := 128; // breite
  h := 128; // höhe
  Image1.Picture.Bitmap.Width := w;
  Image1.Picture.Bitmap.Height := h;
  Image1.Picture.Bitmap.PixelFormat := pf32Bit;
  for y := 0 to h-1 do begin
    p := Image1.Picture.Bitmap.ScanLine[y]; // Zeilenanfang von Bitmap holen
    for x := 0 to w-1 do begin
      if x*x+y*y < 128*128 then // falls innerhalb Kreis mit Radius 128
        p^ := $FF0000 // rot einfärben
      else
        p^ := $00FF00; // grün einfärben
      inc(p); Auf nächstes Pixel setzen.
    end;
  end;
  Image1.Invalidate; // bei Gelegenheit neu zeichnen
end;

_________________
Nur die Menschheit ist arrogant genug, um zu glauben sie sei die einzige intelligente Lebensform im All. Wo nicht mal das nachhaltig bewiesen wurde.
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: So 08.01.06 17:36 
Hallo,

fuer Prozessoren die nicht gerne Multiplizieren ;-)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
var
 p: ^DWord;
 w,h,x,x2,dx2,y,y2,r2: Integer;
begin
  w := 128; // breite
  h := 128; // höhe
  Image1.Picture.Bitmap.Width := w;
  Image1.Picture.Bitmap.Height := h;
  Image1.Picture.Bitmap.PixelFormat := pf32Bit;
  r2 := 128*128;
  for y := 0 to h-1 do
    begin
    p := Image1.Picture.Bitmap.ScanLine[y]; // Zeilenanfang von Bitmap holen
    y2:= r2-y*y;
    dx2:=1;
    x2:=0;
    for x := 0 to w-1 do begin
      if x2>y2 then // falls innerhalb Kreis mit Radius 128
        p^ := $FF0000 // rot einfärben
      else
        p^ := $00FF00; // grün einfärben
      inc(x2,dx2);
      inc(dx2,2);//x^2 = Summe der ersten n ungeraden Zahlen
      inc(p); Auf nächstes Pixel setzen.
    end;
  end;
  Image1.Invalidate; // bei Gelegenheit neu zeichnen
end;


Gruss Horst
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 08.01.06 17:38 
Die Speicheradresse habe ich nur deshalb immer neu berechnet, da das die Frage von Fighter#1 beantwortet. Ohne direkte Adressierung würde er nicht wissen, wie man das macht.
Ich kann das ganze auch in eine einzige for-Schleife packen, wenn ich will, aber das bringt niemand etwas...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
 p: ^DWord;
 w,h,x: Integer;
begin
  w := 128; // breite
  h := 128; // höhe
  Image1.Picture.Bitmap.Width := w;
  Image1.Picture.Bitmap.Height := h;
  Image1.Picture.Bitmap.PixelFormat := pf32Bit;
  p := Image1.Picture.Bitmap.ScanLine[h-1];
  for x := 0 to w*h-1 do
  begin
    p^ := DWord(sqr(x mod 128) + sqr(127-x div 128) < 128*128)*$FFFF00 xor $00FF00;
    inc(p);
  end;
  Image1.Invalidate;
end;

Und wenn man es besonders schnell wollte würde es wieder ein bisschen anders aussehen...
Der Kreis war auch nur ein Beispiel; die einte Multiplikation wegzuoptimieren macht den Code nur unlesbar...

Wegen dem bottom-up DIB. Wenn du es sauber willst, müsstest du zuerst überprüfen ob die Höhe im Bitmapheader positiv oder negativ ist und entsprechend die Formel anpassen. Aber so wie die Bitmaps in Delphi erzeugt werden sind es immer bottom-up DIBs. Übrigens: Mit der Funktion BytesPerScanline könnte man übrigens auch rausfinden, wieviele Bytes ein Scanline umfasst. (Aber wieder: Bei 32-bit Bitmaps sind die Pixel eh schon 4-Byte aligned).
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mo 09.01.06 10:44 
Hallo,

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
const
  BREITE = 400;
procedure TForm1.Button2Click(Sender: TObject);
//Zeichnet einen Kreis mit Radius r mit Mittelpunkt x0,y0
const
  Farbe1 = $00FF0000;
  Farbe2 = $00F0F0F0;
var
 TF,T0,T1 : int64;
 p: ^DWord;
 w,h,x0,y0,x,x2,dx2,y,y2,dy2,r,r2,Farbe,XORFarbe,k: Integer;

begin
  w := BREITE; // breite
  h := BREITE; // höhe
  Image1.Picture.Bitmap.Width := w;
  Image1.Picture.Bitmap.Height := h;
  Image1.Picture.Bitmap.PixelFormat := pf32Bit;

  QueryperformanceCounter(T0);
  y0 := h div 3;
  x0 := w*2 div 3;
  r  := Breite div 3;
  r2 := sqr(r);
For k := 1 to 1000 do
  begin
  y2 := y0*y0+r;//+r zum runden auf halben Pixel
  dy2 := 2*(0-y0)+1;
  y2 := r2-y2;
  XORFarbe := Farbe1 XOR Farbe2;
  for y := 0 to h-1 do
    begin
    p := Image1.Picture.Bitmap.ScanLine[y]; // Zeilenanfang von Bitmap holen
    dx2:=2*(0-x0)+1;;
    x2:= x0*x0+r;
    for x := 0 to w-1 do
      begin
      If X2 < y2 then
         p^ := Farbe1
      else
         p^ := Farbe2;
      inc(x2,dx2);
      inc(dx2,2);
      inc(p); //Auf nächstes Pixel setzen.
      end;
    dec(y2,dy2);
    inc(dy2,2);
  end;
end;
  QueryperformanceCounter(T1);
  QueryperformanceFrequency(TF);
  Image1.Invalidate; // bei Gelegenheit neu zeichnen
  Button1.Caption:= Format('%f',[(T1-t0)/Tf]);
end;


Ich habe mal mein Programm mit 400x400 Punkten getestet.
Bei meinem Athlon64 bringt das Einsparen der Multiplikation fast nichts <1%.
p := Image1.Picture.Bitmap.ScanLine[h-1];
Nur einmal statt 400 mal spart 10%.

Aber deine Version mit div mod ist 890%(gut ein AhtlonXP ist bei div,mod wesentlich schneller) langsamer und so
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
p := Image1.Picture.Bitmap.ScanLine[h-1];
  For y := 0 to 400-1 do
    for x := 0 to 400-1 do
      begin
      p^ := DWord(x*x+y*y<400*400)*$FFFF00 xor $00FF00;
      inc(p);
      end;

das ist ein Quentchen ~2% schneller als meine Version mit if.
In Zahlen:
1000 Bilder a 400x400 Punkte(640.000 byte) in 0.90 bis 0.92 Sekunden das sind 700 Mb/s ~ 10 Cpu-Takte pro Punkt.
Ein Schleifedurchlauf nur mit inc(p) dauert 5 CPU Takte.

Wer schliesst das Thema?

Gruss Horst