PDA

View Full Version : Resizing a bitmap...



Gadget
14-05-2003, 02:39 PM
Resizing a bitmap...

Anyone know of a good way to resize a bitmap? I mean an algorithm that produces a smooth looking resized image, unlike stretch draw!

Maybe if I get the 'average' pixel colour per block of pixels that I want to resize?? It doesn't have to look great, just better than stretchdraw!

Alimonster
20-05-2003, 07:58 AM
Either GraphicsEx.pas (http://www.delphi-gems.com/Graphics.php#GraphicEx) by Mike Lischke or Graphics32 (http://www.g32.org/graphics32/) contains good resampling filters (like Lanczos3, Mitchell, etc). I can't remember what's in them, though, so have a look through the source code yourself.

Gadget
28-05-2003, 09:30 AM
Thankyou :)

Yet again I am in debt to you!

Gadget
06-06-2003, 02:27 PM
Yeah, I know I am replying to myself... But this may help someone else with the same question:-

http://www.torry.net/vcl/graphics/bitmap/resample.zip

peterbone
11-06-2003, 01:50 PM
here's some code I wrote to shrink an image using pixel-averaging (smooth resultant image).


// 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