Autor Beitrag
nepleurepas
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Di 30.09.08 18:12 
Hallo,

ich habe folgendes, mir völlig unerklärliches problem, das ich nicht einmal besser zu betiteln vermag!
Folgende procedure convertiert Tbitmaps in 8bitgrayscales:
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:
procedure converttograyscale(Abitmap: Tbitmap);
type
  TLogPal = record
    lpal : TLogPalette;
    colorSpace : Array[0..255] of TPaletteEntry; // This allocate room to
                                                 // new palette colors
                                                 // since palPalEntry member of
                                                 // TLogPalette is declared as
                                                 // Array [0..0] of TPaletteEntry
  end;
var
  bmp : TBitmap;
  pal : TLogPal;
  iCount : integer;
begin
  // Create a 256 gray-scale palette
  pal.lpal.palVersion:=$300;
  pal.lpal.palNumEntries := 256;
  for iCount := 0 to 255 do
    with pal.lpal.palPalEntry[iCount] do
      begin
        peRed := iCount;
        peGreen := iCount;
        peBlue := iCount;
      end;

  // Create a temporary bitmap
  bmp := TBitmap.Create;
  try
    // Define bitmap as 8 bit color
    bmp.PixelFormat := pf8bit;

    // Define width and height
    bmp.Width := Abitmap.Width;
    bmp.Height := Abitmap.Height;

    // Create our new grayscale palette
    bmp.Palette := CreatePalette(pal.lpal);

      // Draw the image on bmp surface
    bmp.Canvas.Draw(0,0,Abitmap);
    Abitmap.Assign(bmp);
  finally
    // Free temp bitmap
    bmp.Free;
  end;
end;

Diese procedure funktioniert einwandfrei, wenn ich sie folgendemaßen aufrufe:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm2.Button1Click(Sender: TObject);
var i: integer;
var Abitmap: Tbitmap;
begin
Abitmap:= Tbitmap.create;
Abitmap.PixelFormat:= pf24bit;
if opendialog1.execute then Abitmap.LoadFromFile(opendialog1.FileName);
converttograyscale(Abitmap);
form2.Image1.picture.Bitmap:= Abitmap;
Abitmap.SaveToFile('c:gray.bmp');
end;


Wenn sie jedoch durch folgende procedure aufgerufen wird:
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:
procedure getfields;
var i, r, x, y: integer;
var c1: Tcolor;
var field: Tbitmap;
begin
c1:= rgb(10,10,0)
field:= Tbitmap.Create;
field.PixelFormat:= pf24bit;

for i:=1 to 10 do
 begin
 r:= 1;
 while (r<380) do
  begin
  x:= r*3;
  y:= r*4;
  if (c1=graph.canvas.pixels[x,y])
  then
    begin
    getfieldscorners(x,y,i); //gets the corners' coordinates
    field.Width:= field.Right -  field.Left +1;
    field.height:= field.Bottom - field.Top +1;
    field.Canvas.CopyRect(rect(0,0,(field.Width),(field.Height)),field);  //files the Trect in a new Tbitmap (field)
    setcolorbw(field,'field');
    form2.Image1.Picture.Bitmap:= field; //bis hierhin funzt alles wunderbar
    showmessage('1');
    converttograyscale(field);           // Aufruf von converttograyscale
    form2.Image1.Picture.bitmap:= field; //ab hier ist das bild nur müll
    showmessage('2');   
    r:= 380;
    end;


  inc(r);
  end;
 end;
field.Free;
end;

liefert die procedure ab dem zweiten Schleifendurchlauf (äußere for-Schleife) nur ein weißes bild, mit schwarzem streifen am linken, unteren und rechten bildrand! Das bild, das beim ersten durchlauf konvertiert und angezeigt wird, ist in ordnung! Eigentlich muss der fehler in der procedure converttograyscale liegen, da getfields ohne converttograyscale wunderbar funktioniert. So ist z.b. das bild, das vor der showmessage 1 angezeigt wird, völig in ordnung. Erst wenn das bild von converttograyscale bearbeitet wurde, wird das fehlerhafte bild vor der showmessage 2 angezeigt.
Seltsam ist eben, dass converttograyscale an sich ja funktioniert ( s.o.).
Also ich kann mir da absolut kein reim drauf machen...
Wäre für jede antwort sehr dankbar
acadam71
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Di 30.09.08 18:52 
ausblenden Delphi-Quelltext
1:
form2.Image1.Picture.bitmap:= field; //ab hier ist das bild nur müll					


Vielleicht mal mit assign versuchen
ausblenden Delphi-Quelltext
1:
form2.Image1.Picture.bitmap.Assign(field);					


Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Di 30.09.08 19:04 
bringt leider nichts :-(.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 01.10.08 20:27 
TImage handhabt intern eine eigene TBitmap-Instanz. Diese allein mit Assign oder durch Canvas.Draw dieses internen Bitmaps bearbeiten.

Zudem ist dein Code insofern unsauber, dass man Konvertierungen nur wenn zwingend notwendig inline ausführt und ansonsten eine entsprechende (neue) Instanz als Ergebnis zurückgibt.

Was soll eigentlich die CopyRect in Zeile 23?

Bitmaps zuweisen (und auch jegliche anderen Instanzen) IMMER mit Assign!!! (Siehe Oben).

P.S.: Keine Threadwerbung, auch nicht per PN!

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Mi 01.10.08 21:48 
user profile iconBenBE hat folgendes geschrieben Zum zitierten Posting springen:
TImage handhabt intern eine eigene TBitmap-Instanz. Diese allein mit Assign oder durch Canvas.Draw dieses internen Bitmaps bearbeiten.


Hier spielst du wahrscheinlich auf die Zeilen 25 und 28 an? Das hat mir ja acadam71 auch geraten. Aber wie gesagt, dadurch hat sich keine Veränderung eingestellt. Habs mir jetzt aber hinter die Ohren geschrieben, also die Zuweisung durch assign.

Zitat:

Zudem ist dein Code insofern unsauber, dass man Konvertierungen nur wenn zwingend notwendig inline ausführt und ansonsten eine entsprechende (neue) Instanz als Ergebnis zurückgibt.


Meinst du damit, dass die procedure converttograyscale zwei Parameter besitzen sollte? Ungefähr so?
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:
procedure converttograyscale(Abitmap, Bbitmap: Tbitmap);
type
  TLogPal = record
    lpal : TLogPalette;
    colorSpace : Array[0..255] of TPaletteEntry; // This allocate room to
                                                 // new palette colors
                                                 // since palPalEntry member of
                                                 // TLogPalette is declared as
                                                 // Array [0..0] of TPaletteEntry
  end;
var
  bmp : TBitmap;
  pal : TLogPal;
  iCount : integer;
begin
  // Create a 256 gray-scale palette
  pal.lpal.palVersion:=$300;
  pal.lpal.palNumEntries := 256;
  for iCount := 0 to 255 do
    with pal.lpal.palPalEntry[iCount] do
      begin
        peRed := iCount;
        peGreen := iCount;
        peBlue := iCount;
      end;

  // Create a temporary bitmap
  bmp := TBitmap.Create;
  try
    // Define bitmap as 8 bit color
    bmp.PixelFormat := pf8bit;

    // Define width and height
    bmp.Width := Abitmap.Width;
    bmp.Height := Abitmap.Height;

    // Create our new grayscale palette
    bmp.Palette := CreatePalette(pal.lpal);

      // Draw the image on bmp surface
    bmp.Canvas.Draw(0,0,Abitmap);
    Bbitmap.Assign(bmp);
  finally
    // Free temp bitmap
    bmp.Free;
  end;
end;

Jau, das wars wohl. Jetzt macht converttograyscale keine Zicken mehr!
Zitat:

Was soll eigentlich die CopyRect in Zeile 23?

Ja, die is so natürlich schwachsinn. Das field am ende der zeile müsste eigentlich ein einen anderen Namen haben. Ich hab paar namen der variablen geändert, da diese vllt sonst den sinn meines programmes offenbart hätten. Nicht dass dieser illegal ist, aber ich will die idee einfach für mich behalten..noch. Dabei ist mir wohl ein fehler unterlaufen...sorry

Auf jeden fall Danke!

Zitat:

P.S.: Keine Threadwerbung, auch nicht per PN!

ich dachte man kann ja mal höflich fragen...
Aber werds nicht mehr machen!