PDA

View Full Version : pixel resize algorythm inside!



JernejL
17-11-2006, 09:25 PM
A while ago i decided to implement fallback mechanism for non-power of 2 texture loading on cards that don't support power of two textures, so i made this pixel resize code to deal with it, it will scale-down the image to fit next lowest power of 2 size. it is supposed to work with RGBA images.

I also had a idea, to implement a routine to pad the images to next pot dimension, render it scaled with filtering with opengl to next POT dimension, read back from framebuffer, and re-upload as a POT texture ;)




function fraction(maxnew, maxold, val: single): single;
begin
if maxnew = 0 then
Result := 0
else
Result := (val * maxold) / maxnew;
end;

if &#40;width and &#40;width - 1&#41; <> 0&#41; or &#40;height and &#40;height - 1&#41; <> 0&#41; then // POT check
if NPOT = false then
begin

// pixel resize the image.

Ow&#58;= width;
Oh&#58;= height;

width&#58;= trunc&#40;power&#40;2, trunc&#40;Log2&#40;width &#41;&#41;&#41;&#41;;
height&#58;= trunc&#40;power&#40;2, trunc&#40;Log2&#40;height&#41;&#41;&#41;&#41;;

POTstream&#58;= Tfile.Create;
POTstream.setnewsize&#40;Width * Height * 4&#41;;

for x&#58;= 0 to Width-1 do
for y&#58;= 0 to height-1 do begin

Move&#40;
pointer&#40;integer&#40;pData&#41; + &#40;trunc&#40;fraction&#40;height, oh, y&#41;&#41; * ow + trunc&#40;fraction&#40;width, ow, x&#41;&#41;&#41; * 4&#41;^,
pointer&#40;integer&#40;POTstream.memory&#41; + &#40;y * &#40;width&#41; + x&#41; * 4&#41;^,
4&#41;;
end;

// set new data to use
size&#58;= width * height * 4;
pData&#58;= POTstream.memory;

end;