Be a Supporter!

Super-Duper Image processing fun

  • 746 Views
  • 3 Replies
New Topic Respond to this Topic
Sir-Davey
Sir-Davey
  • Member since: Jul. 9, 2001
  • Offline.
Forum Stats
Supporter
Level 19
Blank Slate
Super-Duper Image processing fun 2005-03-01 12:31:25 Reply

Well i just finished an algorythm that can shrink an image smoothly (using bicubic average). Basically it creates a grid of the bigger image, then for each grid block, it calculates the average pixel color of it.
I wrote this in delphi, and shrinking an image from 1024x768 to a 320x240 image only takes 220 milliseconds on a P4 2.5GhZ. Here's my function

function TForm1.ShrinkImage(Source:TBitmap;ResWidth:Integer;ResHeight:Integer;Smooth
Shrink:Boolean): HDC;
var
ResH: HWND;
SrcBMP,ResBMP: TBitmap;
SLRes,SLSrc,SLAVG : pRGBTripleArray;
resX,resY,AVGi,AVGj,srcX,srcY,AVGr,AVGb,AVGg,AVGamnt: Integer;
WCoef,HCoef: Real;
begin
try
//Create our bitmaps
SrcBMP:=TBitmap.Create;
ResBMP:=TBitmap.Create;

//Get correct widths and heights and pixel resolutions
SrcBMP.Width:=Source.Width;
SrcBMP.Height:=Source.Height;
SrcBMP.PixelFormat:=pf24Bit;

ResBMP.Width:=ResWidth;
ResBMP.Height:=ResHeight;
ResBMP.PixelFormat:=pf24Bit;

//Copy our Source bitmap to our own source.
BitBlt(SrcBMP.Canvas.Handle,0,0,Source.Width,Source.Height,Source.Canvas.Ha
ndle,0,0,SRCCOPY);

//Make the division to know the shrinking "coefficient" and how much we have to inc() srcX for resX
WCoef:=SrcBMP.Width/ResBMP.Width;
HCoef:=SrcBMP.Height/ResBMP.Height;

//start doing pixel work
for resY:=0 to ResBMP.Height-1 do begin
srcY:=floor(resY*Hcoef);
SLRes:= ResBMP.ScanLine[resY];
SLSrc:= SrcBMP.ScanLine[srcY];
for resX:=0 to ResBMP.Width-1 do begin
srcX:=abs(floor(resX*Wcoef));
//Pixel averaging
//Get how many pixels need to be averaged for each chunk
if WCoef>1 then begin
if SmoothShrink then begin
AVGr:=0;
AVGg:=0;
AVGb:=0;
AVGamnt:=0;
SLAVG:=nil;
for AVGj:=floor(HCoef*-1/2) to floor(HCoef/2) do begin
if (AVGj<>0) then begin
SLAVG:=SrcBMP.Scanline[abs(srcY+AVGj)];
end
else begin
SLAVG:=SLSrc;
end;
for AVGi:=floor(WCoef*-1/2) to floor(WCoef/2) do begin
inc(AVGr,SLAVG[srcX+AVGi].rgbtRed);
inc(AVGg,SLAVG[srcX+AVGi].rgbtGreen);
inc(AVGb,SLAVG[srcX+AVGi].rgbtBlue);
inc(AVGamnt,1);
end;
end;
//calculate the average
AVGr:=round(AVGr/AVGamnt);
AVGg:=round(AVGg/AVGamnt);
AVGb:=round(AVGb/AVGamnt);
SLRes[resX].rgbtBlue:=AVGb;
SLRes[resX].rgbtGreen:=AVGg;
SLRes[resX].rgbtRed:=AVGr;
end;
end
else begin
SLRes[resX].rgbtBlue:=SLSrc[srcX].rgbtBlue;
SLRes[resX].rgbtGreen:=SLSrc[srcX].rgbtGreen;
SLRes[resX].rgbtRed:=SLSrc[srcX].rgbtRed;
end;
end;
end;

//and....asign our result!!
Result:=ResBMP.Canvas.Handle;

finally
//Free our bitmaps
SrcBMP.Free;
//ResBMP.Free;
end;
end;

Sorry for any wrapping NG might do


BBS Signature
Sir-Davey
Sir-Davey
  • Member since: Jul. 9, 2001
  • Offline.
Forum Stats
Supporter
Level 19
Blank Slate
Response to Super-Duper Image processing fun 2005-03-01 12:32:39 Reply

Oh I forgot to say: Post any image processing functions you might've done in the past, in whatever programming language you want.


BBS Signature
Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to Super-Duper Image processing fun 2005-03-01 17:29:14 Reply

nice dude. I don't know delphi so i can't really understand that but im thinking of making something that manipulates images in VB later on myself.

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Super-Duper Image processing fun 2005-03-01 21:55:54 Reply

Hmm... I'm going to try and make that in C++.... Hopefully I can get it in a week. I doubt it though because I'm going to be busy....