 |
 |
Scaling Image Data
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status:
Offline
|
|
Hello,
I was wondering if anyone knows if the Cocoa API has any methods for scaling NSImage data. I know that it can scale the display of the image data but I can't find anything that will scale the actual data. I might have overlooked something, though.
Seems like a tedious thing to do if someone else has already done it.
Thanks,
Jeff.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSImage *bigImage, *littleImage;
bigImage = [NSImage imageNamed:<font color = orange>@"SomeBigImageInMyAppBundle"</font>];
littleImage = [[NSImage alloc] initWithSize:NSMakeSize(<font color = blue>32</font>, <font color = blue>32</font>)];
[littleImage lockFocus];
[bigImage drawInRect:NSMakeRect(<font color = blue>0</font>, <font color = blue>0</font>, <font color = blue>32</font>, <font color = blue>32</font>) operation:NSCompositeCopy fraction:<font color = blue>1.0</font>];
[littleImage unlockFocus];
</font>[/code]
This code is right off the top of my head, so it may not compile when copied/pasted.
If you want to save the finished iamge, call <font face = "courier">[littleImage TIFFRepresentation]</font> to get an NSData you can write to a file. Also, you may want to mess with the current NSGraphicsContext's image interpolation factor after <font face = "courier">[littleImage lockFocus]</font> so you get good scaling.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Ahhh, this is beautiful! I've been using the quicktime API for this and hating it (quicktime code is really nasty after you've gotten used to objective-c yumminess).
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2001
Status:
Offline
|
|
So that doesn't quite compile, it needs an extra argument, fromRect. My question is, how can I make an NSRect object which has the dimensions of the source image?
I'm sorry to be a pain, but I've wanted to be able to do this for a while and this got me excited. The information I need is surely contained in the NSSize object obtained from [bigImage size], but I don't know how to access it since it is a Java class! Or is it? I'm obviously new to objective-c, and seem to be quite confused.
Thanks to anyone who decides to help, and to Rick already!
[ 03-19-2002: Message edited by: kongtomorrow ]
[ 03-19-2002: Message edited by: kongtomorrow ]
[ 03-19-2002: Message edited by: kongtomorrow ]
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2001
Status:
Offline
|
|
I got what I wanted to work, but from inspection the scaling seems to be a simple resampling of the image. Does anyone know if a more sophisticated scaling algorithm is already implemented somewhere in the frameworks in a way we can get at it?
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
NSSize is a C structure declared in <Foundation/NSGeometry.h>
Here's the declaration:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
typedef struct _NSSize {
float width; <font color = brown>/* should never be negative */</font>
float height; <font color = brown>/* should never be negative */</font>
} NSSize;
</font>[/code]
Assuming you have a NSSize called mySize, you could access width like this:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
myFloat = mySize.width;
</font>[/code]
If you have an NSRect that you want to know the width of you could do
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
myFloat = myRect.size.width;
</font>[/code]
or
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
myFloat = NSWidth(myRect);
</font>[/code]
All of of this and more can be found at /System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h
I'm not exactly sure where you got the idea this was a java class, but I've heard stranger ideas. 
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
oops, yeah, I left out the fromRect parameter. But you don't need to come up with the dimensions of your image there... if you pass in NSZeroRect it will automatically use the entire image. (If you pass in anything else, it lets you grab a portion of the image.)
See NSGraphicsContext.h for how to change the image interpolation setting (which you should do immediately after locking focus on littleImage) if you'd like better smoothing of the scaled image.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2001
Status:
Offline
|
|
Thanks a lot guys, I'll get the hang of this eventually.  And Rick I see you mentioned how to get better scaling. Sorry for making you repeat yourself.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
Sorry for making you repeat yourself.
That's okay, I don't mind. Oh, and did I mention you could get better image scaling by calling methods on NSGraphicsContext? 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|