PDA

View Full Version : how to cut an image to small?



yqs2002
12-18-2002, 11:55 PM
I want to texturemap an image of 3450*5678 pixel ,but the max matrix of texturemap in my pc is 1024*1024,how can i solve it? is cutting map is a good way? how to do it?
thanks for any help.

mdog1234
12-19-2002, 12:25 AM
Yeah cut it or shrink it. All texture maps have to be square and a power of 2

MickeyMouse
12-19-2002, 01:32 AM
I recommend you shrink it
Pseudocode in case...



for i := 0 to 1023 do begin
for j := 0 to 1023 do begin
_i := (i * 3450) div 1023;
_j := (j * 5678) div 1023;
final_texture[i][j] := your_texture[_i][_j];
end;
end;

Hope you don't mind my Pascal code http://www.opengl.org/discussion_boards/ubb/smile.gif

Deiussum
12-19-2002, 05:54 AM
Originally posted by mdog1234:
Yeah cut it or shrink it. All texture maps have to be square and a power of 2

You're right about the power of 2, but not about them needing to be square. As long as both dimensions are a power of 2, you can have rectangular textures. (e.g. 256x128)

mdog1234
12-19-2002, 09:23 PM
oh ok, thats good to know

12-20-2002, 07:45 AM
I copy pasted parts of my imagelibrary that does the trick http://www.opengl.org/discussion_boards/ubb/smile.gif


inline unsigned int toPowerOf2(unsigned int w) { // Returns maximum power of 2 value that is smaller than or equals w
unsigned int p=1;
while (p<=w) { p<<=1; }
return p>>1;
}

void Image: http://www.opengl.org/discussion_boards/ubb/tongue.gifower2() // Scale down to nearest 2^n x 2^m size.
{
int p2_width = toPowerOf2(getWidth());
int p2_height = toPowerOf2(getHeight());
if (p2_width==getWidth() && p2_height==getHeight())
return;

//printf("Scaling image to power of 2\n");

Image p2img(p2_width,p2_height,getComponents());
int x,y;
for (x=0; x<p2_width; x++)
for (y=0; y<p2_height; y++)
{
int x2 = getWidth()*x / p2_width;
int y2 = getHeight()*y / p2_height;
p2img.setPixel(x, y, getPixel(x2,y2));
}
*this = p2img;
}
void Image::halve() // Halve image
{
Image half(getWidth()>>1,getHeight()>>1,getComponents());
int x,x2,y,y2;
for (x=0,x2=0; x2<getWidth(); x++, x2+=2)
for (y=0,y2=0; y2<getHeight(); y++, y2+=2)
half.setPixel(x, y, getPixel(x2,y2));
*this = half;
}


Image img("fname.png");
img.power2();
while (img.getWidth()>1024 &#0124; &#0124; img.getHeight()>1024) img.halve();

MickeyMouse
12-21-2002, 03:12 AM
Actually our codes could be optimized a bit http://www.opengl.org/discussion_boards/ubb/biggrin.gif.
This would probably save you a lot of time:




i_step := (3450 << 16) div 1023;
j_step := (5678 << 16) div 1023;

_i := 0;
for i := 0 to 1023 do begin
_j := 0;
for j := 0 to 1023 do begin
_i := i + i_step;
_j := j + j_step;
final_texture[i][j] := your_texture[_i >> 16][_j >> 16];
end;
end;


Just for fun http://www.opengl.org/discussion_boards/ubb/smile.gif