Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Scaling Image Data

Scaling Image Data
Thread Tools
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Mar 18, 2002, 06:47 PM
 
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.
Spectral Class
"Shedding Light on Innovation"
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Mar 19, 2002, 12:15 AM
 
<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.
Rick Roe
icons.cx | weblog
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Mar 19, 2002, 12:33 AM
 
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
Reply With Quote
Mar 19, 2002, 03:49 AM
 
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
Reply With Quote
Mar 19, 2002, 11:50 AM
 
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
Reply With Quote
Mar 19, 2002, 10:40 PM
 
NSSize is a C structure declared in &lt;Foundation/NSGeometry.h&gt;
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
Reply With Quote
Mar 19, 2002, 10:42 PM
 
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.
Rick Roe
icons.cx | weblog
     
Fresh-Faced Recruit
Join Date: Dec 2001
Status: Offline
Reply With Quote
Mar 19, 2002, 11:13 PM
 
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
Reply With Quote
Mar 20, 2002, 03:07 AM
 
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?
Rick Roe
icons.cx | weblog
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2