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: Brendan Younger <email@hidden>
- Date: Thu, 5 Jul 2001 14:03:02 -0500
Alex, this is from a previous post by David Trevas.
- (void)scalePicture
{
NSBitmapImageRep *myBitmapRep;
NSData *inData, *outData;
NSSize desiredSize;
NSImage *myImage;
float desiredWidth = 212.0, desiredHeight = 156.0;
desiredSize.width = desiredWidth;
desiredSize.height = desiredHeight;
inData = [[NSData alloc]
initWithContentsOfFile:@"/Users/myname/Documents/OriginalPicture.tiff"];
//Needs to be released
if(!inData) return;
myBitmap = [NSBitmapImageRep imageRepWith
Data:picData];
//Automatically auto-released
if(!myBitmap) return;
myImage = [[NSImage alloc] initWithSize:desiredSize]; //Needs to
be released
if(!myImage) return;
[myImage setScalesWhenResized:YES];
[myImage addRepresentation:myBitmap];
[myImage lockFocusOnRepresentation:myBitmap];
outData = [myImage TIFFRepresentation]; //Automatically auto-released
[outData writeToFile:@"/Users/myname/Documents/NewPicture.tiff"
atomically:NO];
[myImage unlockFocus];
[myImage release];
[inData release];
}
1. Set the dimensions you want your new picture to be.
2. Get the data of the Original Picture as an NSData object.
3. Create an NSBitmapImageRep from that data. This image
rep(resentation) is the original size of the Original Picture.
4. Create an NSImage using the initWithSize: method using the desired
size. This creates essentially an empty box waiting for you to pour
something in it. Notice that most of the other init methods with
NSImage use some data to get started.
5. Set the "ScalesWhenResized" flag to YES so that when you pour the
image rep into the image, it will scale itself to the image's size.
6. Add the NSBitmapImageRep to the NSImage.
7. Lock the focus on the new representation. NSImage maintains an
array of representations, so you have to tell it the one you want to
focus on at any given time.
8. Create the output data by taking the TIFF representation of the
NSImage. Since you focused on the bitmap image, it is the one that gets
copied into data.
9. Write the data to the new file.
10. Unlock the focus on the bitmap rep.
11. Release objects created by an alloc-init sequence.
Looking through this, it seems to be a bit much, but play with it to get
it down to the minimum needed. I've had success with:
image = [[NSImage alloc] initWith
Data:[anImage TIFFRepresentation]];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(16.0, 16.0)];
Of course this doesn't do everything you need, but it works as far a
scaling goes.
Brendan Younger