SRL Archive - Help and Support

Resizing an image with CopyCanvas?


_ChArMz

2010-03-15 15:08:48

I am trying to resize a 250x250 jpg image to half that size (125x125). But, when resizing it, the image itself becomes distorted.

I already tried converting the jpg to a bmp, then resizing, but that does not change the outcome.

Run the script below, make sure the image jpg and script are in the same location.

Is it possible to fix this in SCAR?

caused

2010-03-15 15:18:31

It seems rather normal to me, since there is no certain filter applied that would add "anti aliasing" or such to the image.

You could import something from delphi through a .dll file though ;].

~caused

nielsie95

2010-03-15 15:44:34

Of course it becomes distorted :p

Sir R. M8gic1an

2010-03-15 15:51:47

I didn't check the script… but my brother is doing game enginering… There's quite a few complicated algorithms to properly resize an image. It won't be that easy :p

~RM

_ChArMz

2010-03-15 17:31:05

type TWa = array[0..1000] of longInt;
PWa = ^TWa;
var sx1,sy1,sx2,sy2 : single; //source field positions
py,pj : PWa;
x,y,i,j : word; //source,dest field pixels
destR,destG,destB : single; //destination colors
sR,sG,sB : byte; //source colors
destWidth, destheight : word;
f,fi2 : single; //factors
dx,dy,AP : single; //distance, area percentage
color : DWord;//longInt;
begin
with paintbox2 do
with canvas do fillrect(rect(0,0,width,height));
//
destwidth := strtoint(edit1.text);
f := sw / destwidth;
fi2 := 1/f;
fi2 := fi2*fi2;
destheight := trunc(bm1.height/f);
with bm2 do
begin
width := destwidth;
height := destheight;
end;
//---
for y := 0 to destheight-1 do //vertical destination pixels
begin
sy1 := f * y;
sy2 := sy1 + f;
py := bm2.ScanLine[y];
for x := 0 to destwidth-1 do //horizontal destination pixels
begin
sx1 := f * x;
sx2 := sx1 + f;
destR := 0; destG := 0; destB := 0; //clear colors
for j := floor(sy1) to ceiling(sy2)-1 do //vertical source pixels
begin
pj := bm1.scanline[j];
dy := 1;
if sy1 > j then begin
dy := dy-(sy1-j);
end;
if sy2 < j+1 then begin
dy := dy-(j+1-sy2);
end;
for i := floor(sx1) to ceiling(sx2)-1 do //horizontal source pixels
begin
dx := 1;
if sx1 > i then begin
dx := dx-(sx1-i);
end;
if sx2 < i+1 then begin
dx := dx-(i+1-sx2);
end;
color := pj^[i];
sB := color and $ff;
sG := (color shr 8) and $ff;
sR := (color shr 16) and $ff;
AP := dx*dy*fi2;
destR := destR + sR*AP;
destG := destG + sG*AP;
destB := destB + sB*AP;
end;//for i
end;//for j
color := trunc(destB);
color := color or (trunc(destG) shl 8);
color := color or (trunc(destR) shl 16);
py^[x] := color;
end;//for x
end;//for y
paintbox2.canvas.draw(0,0,bm2);
end;
{
procedure ResizeImage(Src, Dst: TBitmap);
type
TRGBArray = array[Word] of TRGBTriple;
pRGBArray = ^TRGBArray;

var
x, y: Integer;
xP, yP: Integer;
xP2, yP2: Integer;
SrcLine1, SrcLine2: pRGBArray;
t3: Integer;
z, z2, iz2: Integer;
DstLine: pRGBArray;
DstGap: Integer;
w1, w2, w3, w4: Integer;
begin
Src.PixelFormat := pf24Bit;
Dst.PixelFormat := pf24Bit;

if (Src.Width = Dst.Width) and (Src.Height = Dst.Height) then
Dst.Assign(Src)
else
begin
DstLine := Dst.ScanLine[0];
DstGap := Integer(Dst.ScanLine[1]) - Integer(DstLine);

xP2 := MulDiv(pred(Src.Width), $10000, Dst.Width);
yP2 := MulDiv(pred(Src.Height), $10000, Dst.Height);
yP := 0;

for y := 0 to pred(Dst.Height) do
begin
xP := 0;

SrcLine1 := Src.ScanLine[yP shr 16];

if (yP shr 16 < pred(Src.Height)) then
SrcLine2 := Src.ScanLine[succ(yP shr 16)]
else
SrcLine2 := Src.ScanLine[yP shr 16];

z2 := succ(yP and $FFFF);
iz2 := succ((not yp) and $FFFF);
for x := 0 to pred(Dst.Width) do
begin
t3 := xP shr 16;
z := xP and $FFFF;
w2 := MulDiv(z, iz2, $10000);
w1 := iz2 - w2;
w4 := MulDiv(z, z2, $10000);
w3 := z2 - w4;
DstLine[x].rgbtRed := (SrcLine1[t3].rgbtRed * w1 +
SrcLine1[t3 + 1].rgbtRed * w2 +
SrcLine2[t3].rgbtRed * w3 + SrcLine2[t3 + 1].rgbtRed * w4) shr 16;
DstLine[x].rgbtGreen :=
(SrcLine1[t3].rgbtGreen * w1 + SrcLine1[t3 + 1].rgbtGreen * w2 +

SrcLine2[t3].rgbtGreen * w3 + SrcLine2[t3 + 1].rgbtGreen * w4) shr 16;
DstLine[x].rgbtBlue := (SrcLine1[t3].rgbtBlue * w1 +
SrcLine1[t3 + 1].rgbtBlue * w2 +
SrcLine2[t3].rgbtBlue * w3 +
SrcLine2[t3 + 1].rgbtBlue * w4) shr 16;
Inc(xP, xP2);
end;
Inc(yP, yP2);
DstLine := pRGBArray(Integer(DstLine) + DstGap);
end;
end;
end;

found these two procedures on the internet…did not know this was this big of a process :D

im looking for speed so what would be the best algo to use?, i assume switching to DIBSection or bmbits will also increase speed over scanlines?

Rick

2010-03-15 17:34:16

I was messing with an Image Manipulation Plugin for scar. I could add JPEG resizing to it if you want.
http://villavu.com/forum/showthread.php?p=674174#post674174

Edit: need to refresh before posting

_ChArMz

2010-03-15 21:37:42

From: r!ch!e
I was messing with an Image Manipulation Plugin for scar. I could add JPEG resizing to it if you want.
http://villavu.com/forum/showthread.php?p=674174#post674174

Edit: need to refresh before posting


in the plugin you just have simple converter functions?…but if u have a resizin function, ill be more than happy to take a look

EDIT:

no hard algo needed, a simple Bitmap.Canvas.StretchDraw, works perfect :D
check out attachment if you want, process takes 1ms, it only works with bitmaps, i have to add jpg conversion