This is a typical situation where some assembler can be usefull. Of course, always {$ifdef} the assembler code and put a Pascal version in the {$else} part to not hurt portabilty.

[pascal]
if frmGenerate.SizePower2 then
begin
w:=imgout.width;
h:=imgout.height;
asm
mov ebx,w
bsr ecx,ebx
mov ebx,1
inc ecx
shl ebx,ecx
mov w,ebx

mov ebx,h
bsr ecx,ebx
mov ebx,1
inc ecx
shl ebx,ecx
mov h,ebx
end;
end;
[/pascal]