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
Code:
function fraction(maxnew, maxold, val: single): single;
begin
if maxnew = 0 then
Result := 0
else
Result := (val * maxold) / maxnew;
end;
if (width and (width - 1) <> 0) or (height and (height - 1) <> 0) then // POT check
if NPOT = false then
begin
// pixel resize the image.
Ow:= width;
Oh:= height;
width:= trunc(power(2, trunc(Log2(width ))));
height:= trunc(power(2, trunc(Log2(height))));
POTstream:= Tfile.Create;
POTstream.setnewsize(Width * Height * 4);
for x:= 0 to Width-1 do
for y:= 0 to height-1 do begin
Move(
pointer(integer(pData) + (trunc(fraction(height, oh, y)) * ow + trunc(fraction(width, ow, x))) * 4)^,
pointer(integer(POTstream.memory) + (y * (width) + x) * 4)^,
4);
end;
// set new data to use
size:= width * height * 4;
pData:= POTstream.memory;
end;
Bookmarks