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

Win XP

BeitragVerfasst: Do 18.09.08 22:00 
Hallo,

habe folgende procedure zur Umwandlung in eine 8bitgrayscale gefunden:
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:
55:
procedure TForm1.Button1Click(Sender: TObject);
type
  TLogPal = record
    lpal : TLogPalette;
    colorSpace : Array[0..255of 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
  // Loads the image
  Image1.Picture.LoadFromFile('my_picture.bmp');

  // 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 := Image1.Picture.Width;
    bmp.Height := Image1.Picture.Height;

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


    // Draw the image on bmp surface
    bmp.Canvas.Draw(0,0,Image1.Picture.Graphic);


    // Save the new 8bit file
    bmp.SaveToFile('my_picture-8bit.bmp');

  finally
    // Free temp bitmap
    bmp.Free;
  end;
end;


diese funktioniert auch soweit. Nun wollte ich sie in mein programm einbauen, jedoch, ohne den Umweg über ein TImage:
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:
procedure converttograyscale(var test: Tbitmap);
type
  TLogPal = record
    lpal : TLogPalette;
    colorSpace : Array[0..255of 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 := test.Width;
    bmp.Height := test.Height;

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

      // Draw the image on bmp surface
    bmp.Canvas.Draw(0,0,test);
    test:= bmp;




  finally
    // Free temp bitmap
    bmp.Free;
  end;
end;


Nun stürzt das Programm in der Zeile
ausblenden Delphi-Quelltext
1:
test:= bmp;					

ab. Woran liegt dies?

Danke für Antworten
Stefan
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Do 18.09.08 22:02 
Welche Fehlermeldung??
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Do 18.09.08 22:05 
user profile iconBoldar hat folgendes geschrieben:
Welche Fehlermeldung??

"Access violation at adress 0041F27D in module project1.exe"
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 18.09.08 22:11 
Moin!

Ähm, das sind Objekte, da kann keine einfache Zuweisung machen... :? Probier das mal so:
user profile iconnepleurepas hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure converttograyscale(ABitmap: TBitmap);
//...
begin
  //...
  // Draw the image on bmp surface
  bmp.Canvas.Draw(0,0,ABitmap);
  ABitmap.Assign(bmp);
  //...
Und das var beim Parameter kannst du dir auch sparen, TBitmap ist eh ein Zeiger. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Do 18.09.08 22:22 
awright, da hab ich wohl wieder was fundamentales gelernt. Ne Frage:
Bei www.delphi-treff.de/...zugriff-auf-objekte/

geht doch  auto1:= auto2 auch. Und das sind doch eigentlich auch Objekte?

---Moderiert von user profile iconNarses: Beiträge zusammengefasst---

user profile iconNarses hat folgendes geschrieben:
Ähm, das sind Objekte, da kann keine einfache Zuweisung machen... :? Probier das mal so:

Das ändert leider nichts an der Fehlermeldung :-(.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 18.09.08 22:43 
Moin!

user profile iconnepleurepas hat folgendes geschrieben:
Ne Frage:
Bei www.delphi-treff.de/...zugriff-auf-objekte/

geht doch  auto1:= auto2 auch. Und das sind doch eigentlich auch Objekte?
Klar kann man die Zeiger kopieren, das ist auch nicht das Problem. ;) Das Problem kriegst du erst, wenn du bmp freigibst, weil du dann nämlich ein freigegebenes Objekt ablieferst, das gibt dann auf die Finger. :idea: :D

user profile iconnepleurepas hat folgendes geschrieben:
Das ändert leider nichts an der Fehlermeldung :-(.
Grade ausprobiert, die Prozedur läuft einwandfrei. ;)

Der Fehler muss woanders liegen. :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
nepleurepas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164

Win XP

BeitragVerfasst: Do 18.09.08 22:52 
aja habs, danke. Nur n tippfehler.