Autor Beitrag
Nemo90
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 01.03.08 00:49 
Hallo ich möchte in Delphi eine BMP-Datei verwischen (blurren).
Dazu habe ich folgendes Programm geschrieben:

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:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,JPEG, ComCtrls;

type MyThread=class(TThread)
    procedure Execute;override;
  private
    procedure DoBlur;
  public
  end;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    ProgressBar1: TProgressBar;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
    t:MyThread;
    function Blur(bmp:TBitmap):TBitmap;
    function RGBtoColor(r,g,b:Byte):TColor;
    procedure GetRGB(var R:Byte;var B:Byte;var G:Byte;cl:TColor);
  public
    { Public-Deklarationen }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then Image1.Picture.LoadFromFile(OpenDialog1.FileName);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if SaveDialog1.Execute then Image1.Picture.SaveToFile(SaveDialog1.FileName);
end;

function TForm1.Blur(bmp:TBitmap):TBitmap;
var i,j,x,y:Integer;
R,G,B:Byte;
rges,gges,bges:Integer;
newbmp:TBitmap;
proz,anz,ges:Real;
begin
  proz:=0;
  newbmp:=TBitmap.Create;
  newbmp.Height:=bmp.Height-10;
  newbmp.Width:=bmp.Width-10;
  ges:=newbmp.Height*newbmp.Width;
  anz:=0;
  for x:=5 to bmp.Width-5 do
  for y:=5 to bmp.Height-5 do
  begin
    rges:=0;
    bges:=0;
    gges:=0;
    for i:=x-5 to x+5 do
    for j:=y-5 to y+5 do
    begin
      GetRGB(R,G,B,bmp.Canvas.Pixels[i,j]);
      rges:=rges+R;
      bges:=bges+B;
      gges:=gges+G;
    end;
    rges:=TRUNC(rges/100);
    bges:=TRUNC(bges/100);
    gges:=TRUNC(gges/100);
    newbmp.Canvas.Pixels[x-5,y-5]:=RGBtoColor(rges,gges,bges);
    anz:=anz+1;
    proz:=anz/ges;
    ProgressBar1.Position:=Round(100*proz);
  end;
  ProgressBar1.Position:=0;
  newbmp.Transparent:=true;
  result:=newbmp;
end;

procedure TForm1.GetRGB(var R:Byte;var B:Byte;var G:Byte;cl:TColor);
begin
  R := cl and $FF;
  G := (cl shr 8) and $FF;
  B := (cl shr 16) and $FF;
end;

procedure MyThread.Execute;
begin
  Synchronize(DoBlur);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  t:=MyThread.Create(true);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  t.Execute;
end;

procedure MyThread.DoBlur;
begin
  Form1.Image1.Picture.Bitmap:=Form1.Blur(Form1.Image1.Picture.Bitmap);
end;

function TForm1.RGBtoColor(r,g,b:Byte):TColor;
begin
  result:=$00000000 or r;
  result:=result or (g shl 8);
  result:=result or (b shl 16);
end;

end.


Allerdings macht das Programm unabsichtlich Alphablending.
Weiss jemand wo der Fehler liegt?
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: Sa 01.03.08 01:18 
Rechnen wir doch einfach mal ...

ausblenden Delphi-Quelltext
1:
(x+5)-(x-5)+1 = 11					


Das noch in Y-Richtung, ergibt 11*11 = 121 Pixel. Du teilst aber durch 100, damit erhält man:

121/100 = 1,21fache Helligkeit

Oder was meinst Du mit Alphablending???

Ferner ein kleiner Hinweis: Die VCL bietet eine Funktion ColorToRGB und eine Funktion RGBToColor an. Diese solltest Du nutzen, da ansonsten Spezialfarben wie clWindow nicht korrekt behandelt werden (hatte das Problem mal bei einem Gradienten-Zeichen-Algo).

Auch solltest Du statt Trunc(X / 100) das ganze als x div 100 schreiben.

_________________
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.
Nemo90 Threadstarter
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 01.03.08 18:10 
Super danke!

Funktioniert jetzt alles.
Hab außerdem in der GetRGB Funktion B und G vertauscht deswegen kamen auch bissel komische Farben am Anfang raus.

Läuft jetzt alles perfekt.

mfg Nemo90