merci de ton attention
voici le code entier de la procedure, elle est tres longue et getmem est juste au debut.
procedure TConvolution.ButtonApplyClick(Sender: TObject);
var
PTemp1, PImg1, PTemp, PImg, PFilt, PFilt1 : ^integer;
Scan : PByteArray;
x, y, posFil, posImg, posTemp, x1, y1, C, filterWi,
filterHe, min , max, s : integer;
SFilter: array of integer;
begin
{Shows how to allocate memory dynamically -
3 because we want red, green and blue,
then the number of pixels,
then the size of each thing (real) we want}
//storage of the calculated values of pixel
GetMem(PTemp, 3*MyImage.Width*MyImage.Height*Sizeof(Integer));
//Storage of the values of pixel of the Image
GetMem(PImg, 3*MyImage.Width*MyImage.Height*Sizeof(Integer));
min := 0;
max := 0;
Screen.Cursor := crHourGlass; // Let user know we're busy...
{initialise the filter}
filterWi:=Matrice.ColCount ;
filterHe:=Matrice.RowCount ;
s:= round(filterHe/2 - 0.5); // to know how many pixel not used on the side
SetLength(Sfilter,(filterWi*filterHe)); //set the size of the array
for y:=0 to filterHe-1 do begin
for x:=0 to filterWi-1 do begin // loads filter values
posFil:=y*filterWi+x ; // from matrice
Sfilter[posFil] :=StrToInt(Matrice.Cells[x,y]);
end;
end;
PFilt := @Sfilter[0];
//-------------store the pixel value in the memory--------------//
{Shows how to access each colour component}
for y:=0 to MyImage.Height-1 do
begin
Scan:=MyImage.ScanLine[y];
for x:=0 to MyImage.Width-1 do
begin
posImg:=(y*MyImage.Width + x)*3;
PImg1:=PImg;
inc(PImg1,posImg);
PImg1^:=Scan[x*3]; {blue pixel}
inc(PImg1);
PImg1^:=Scan[x*3+1]; {green pixel}
inc(PImg1);
PImg1^:=Scan[x*3+2] {red pixel}
end;
end;
//----------------Apply the filter----------------------//
//manage the position in the memory for image and the temporary file.
for y:=0 to MyImage.Height - filterHe do
begin
for x:=0 to MyImage.Width - filterWi do
begin
posImg:=(y*MyImage.Width+x)*3;
posTemp:=((y*MyImage.Width+x)+ (MyImage.Width+ s))*3;
PImg1:=PImg;
PTemp1:= PTemp;
inc(PImg1,posImg);
{the line on sides is not used,
s1 is the pixels on the side not used}
inc(PTemp1,posTemp);
/// do this part for each color blue (C = 0), green(C=1), red(C=2)
for C:=0 to 2 do
begin
PTemp1^:= 0;
//manage the position in the filter
for y1:=0 to filterHe-1 do
begin
for x1:=0 to filterWi-1 do
begin
posFil:=y1*filterWi+x1 ;
Pfilt1:=Pfilt;
inc(Pfilt1,posFil);
inc(PImg1,posFil + C);
PTemp1^:= PTemp1^+ PImg1^ * Pfilt1^;
// record min and max intensities
if PTemp1^< min then
min := PTemp1^;
if PTemp1^> max then
min := PTemp1^;
end;
end;
inc(PTemp1);
end;
end;
end;
//-----------------Calculate the new intensity of the pixel-------------//
// do this part for each color blue (C = 0), green(C=1), red(C=2)
for C:=0 to 2 do
begin
for y:=0 to MyImage.Height - filterHe do
begin
for x:=0 to MyImage.Width - filterWi do
begin
posImg:=(y*MyImage.Width+x)*3 + C;
PosTemp:=((MyImage.Width + s)+(y*MyImage.Width+x))*3 + C;
PImg1:=PImg;
PTemp1:=(PTemp);
inc(PImg1,posImg);
inc(PTemp1,posTemp);
PImg1^ :=(PTemp1^ - min)* 255;
end;
end;
end;
//----------------DISPLAY THE NEW VALUE-----------------------//
for y:=0 to MyImage.Height-1 do begin
Scan:=MyImage.ScanLine[y];
for x:=0 to MyImage.Width-1 do begin
posImg:=(y*MyImage.Width+x)*3;
PImg1:=PImg;
inc(PImg1,posImg);
Scan[x*3]:= round(PImg1^/(max-min)); {blue pixel}
inc(PImg1);
Scan[x*3+1]:= round(PImg1^/(max-min)); {green pixel}
inc(PImg1);
Scan[x*3+2]:= round(PImg1^/(max-min)); {red pixel}
end;
end;
{Free the memory}
Dispose(PTemp);
Dispose(PImg);
{redraw the image after its been changed}
ImgPro.Image1.Canvas.Draw(0,0,MyImage);
Screen.Cursor := crDefault;
end;