Re: how does one resize images to be used for textures?
Re: how does one resize images to be used for textures?
- Subject: Re: how does one resize images to be used for textures?
- From: Peter Ammon <email@hidden>
- Date: Thu, 05 Jul 2001 14:27:00 -0700
on 7/5/01 9:16 AM, email@hidden at email@hidden wrote:
>
I'm trying to load images of arbitrary dimensions, to be used as OpenGL
>
textures. The textures have to have dimensions of powers of 2. Is there
>
a way to resize these things? The setSize method of NSImageRep does not
>
seem to do the job. My code is below. It works on images that already
>
have dimensions as powers of two.
>
>
thanks in advance,
>
Alex
>
>
>
NSBitmapImageRep *myimage;
>
NSSize mysize;
>
myimage = [NSBitmapImageRep
>
imageRepWithContentsOfFile:@"worlds.bmp"];
>
if(myimage == nil) printf("NIL IMAGE!!!");
>
mysize.width = 64;
>
mysize.height = 64;
>
[myimage setSize: mysize];
>
data = [myimage bitmapData];
>
>
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
>
glGenTextures(1, &texName);
>
glBindTexture(GL_TEXTURE_2D, texName);
>
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
>
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
>
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
>
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
>
>
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
>
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, [myimage size].width,
>
[myimage size].height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
setSize affects the size when it's rendered. It doesn't change the bitmap
data (if it did, it could be lossy).
Here's how I'd try to do it. This code is untested, so I don't know if it
works or not.
NSImage* originalImage=[[NSImage alloc]
initWithConentsOfFile:@"worlds.bmp"];
NSImage* newImage=[[NSImage alloc] initWithSize:NSMakeSize(64, 64)];
NSBitmapImageRep* bitmap=[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:64
pixelsHigh:64
bitsPerSample:CHAR_BIT
samplesPerPixel:3
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0];
[newImage addRepresentation:bitmap];
[newImage lockFocus];
[originalImage drawInRect:NSMakeRect(0, 0, 64, 64)
fromRect:NSMakeRect(0, 0, [originalImage size].width, [originalImage
size].height)
operation:NSCompositeCopy
fraction:1.0];
[newImage unlockFocus];
Now bitmap should contain a 64x64 version of the image, which you can use to
generate the texture.
Hope this helps.
-Peter