here's some code I wrote to shrink an image using pixel-averaging (smooth resultant image).
Code:
// shrink image by a given ratio
function Shrink(ARatio : Real ; ABitmap : TBitmap) : TBitmap;
Var
Lx, Ly : integer;
LyBox, LxBox : integer;
TR, TG, TB : integer;
avR, avG, avB : integer;
LRowIn, LRowOut : pRGBTripleArray;
LBoxCount : integer;
LRatio : Real;
begin
LRatio := 1 / ARatio;
Result := TBitmap.Create;
Result.PixelFormat := pf24bit;
Result.Width := trunc(ABitmap.Width / LRatio);
Result.Height := trunc(ABitmap.Height / LRatio);
for Ly := 0 to Result.Height-1 do begin
LRowOut := Result.ScanLine[Ly];
for Lx := 0 to Result.Width-1 do begin
TR := 0; TG := 0; TB := 0;
LBoxCount := 0;
for LyBox := trunc(Ly*LRatio) to trunc((Ly+1)*LRatio) - 1 do begin
LRowIn := ABitmap.ScanLine[LyBox];
for LxBox := trunc(Lx*LRatio) to trunc((Lx+1)*LRatio) - 1 do begin
TR := TR + LRowIn[LxBox].rgbtRed;
TG := TG + LRowIn[LxBox].rgbtGreen;
TB := TB + LRowIn[LxBox].rgbtBlue;
Inc(LBoxCount);
end;
end;
avR := TR div LBoxCount;
avG := TG div LBoxCount;
avB := TB div LBoxCount;
LRowOut[Lx].rgbtBlue := avB;
LRowOut[Lx].rgbtGreen := avG;
LRowOut[Lx].rgbtRed := avR;
end;
end;
ABitmap.Free;
end;
Hope it helps
Peter
Bookmarks