 |
 |
Cocoa: Compositing Images
|
 |
|
 |
|
Junior Member
Join Date: Nov 2001
Location: Indiana
Status:
Offline
|
|
I have two NSImages complete with transparency, etc, and I would like to 'put them on top of each other.' It seems that this should be fairly simple to do with NSImage constants such as "NSCompositeDestinationOver" used in conjuction with functions like "compositeToPoint :operation:"
But, alas, I'm a dumbass and can't figure out how to do it. How do I use TWO DIFFERENT IMAGES? Do I have to have both my images in the SAME IMAGE and then muck around witht he NSPoint to fake having two seperate images or what?
Help me, or else. 
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Originally posted by Sawtooth G4:
<STRONG>I have two NSImages complete with transparency, etc, and I would like to 'put them on top of each other.' It seems that this should be fairly simple to do with NSImage constants such as "NSCompositeDestinationOver" used in conjuction with functions like "compositeToPoint :operation:"
But, alas, I'm a dumbass and can't figure out how to do it. How do I use TWO DIFFERENT IMAGES? Do I have to have both my images in the SAME IMAGE and then muck around witht he NSPoint to fake having two seperate images or what?
Help me, or else.  </STRONG>
Are you trying to composite them on the display, or are you trying to create a third image composed of the two others?
The way I've done it so far for display purposes is subclassing NSView, and in the drawRect: method of my subclass, I have the lines:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
[backgroundImage compositeToPoint:NSZeroPoint
fromRect:NSMakeRect(<font color = blue>0.0</font>, <font color = blue>0.0</font>, backWidth, backHeight)
operation:NSCompositeCopy];
[foregroundImage compositeToPoint:NSMakePoint(foreX, foreY)
fromRect:NSMakeRect(<font color = blue>0.0</font>, <font color = blue>0.0</font>, foreWidth, foreHeight)
operation:NSCompositeSourceOver];
</font>[/code]
It copies the background to (0.0, 0.0) in the currently focused view (which is my custom view when drawRect: is being called) using width and height variables I've stored, then composites the foreground onto it with transparency using X, Y, width, and height variables I've stored.
Is this what you're looking for?
What I've been wondering lately is what the fastest method of drawing into a window is...I'm getting about 25fps copying two parts of a 512x512 PNG into a 512x512 view (trying to scroll it) and then compositing a 256x256 PNG with transparency on top of them. It seems to me that there must be a faster way of getting that to work. I read a comment from a while back on Omni's lists where a guy was seeing much better performance drawing NSBitmapRep's directly instead of though NSImage, but I haven't figured out how to do that yet.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2001
Location: Indiana
Status:
Offline
|
|
While that is somewhat helpful, Marshall, I'm actually trying to create a third image out of the other two, rather than just drawing the images onto the screen, one atop the other. Any idea how this would best be done?
---
Sig still broke.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Originally posted by Sawtooth G4:
<STRONG>While that is somewhat helpful, Marshall, I'm actually trying to create a third image out of the other two, rather than just drawing the images onto the screen, one atop the other. Any idea how this would best be done?
</STRONG>
Well, I'm not sure how it would best be done...still too new at Cocoa. But this may do what you need:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSImage *finalImage = [[NSImage alloc] initWithSize:[backgroundImage size]];
[finalImage lockFocus];
[backgroundImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
[foregroundImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
[finalImage unlockFocus];
</font>[/code]
I tried it out (setting an NSImageView to point to the completed image when finished) and it seems to work. Hope it helps.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2001
Location: Indiana
Status:
Offline
|
|
I got it working -- thanks a lot Marshall.
<runs back to ProjectBuilder after getting some more WildCherry Pepsi>
---
Yeah sig still broke. It'll be fixed next week when i go back to school.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
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
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|