• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Efficient way to composite large images?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Efficient way to composite large images?


  • Subject: Efficient way to composite large images?
  • From: "Matt Ball" <email@hidden>
  • Date: Sun, 12 Mar 2006 17:44:27 -0500

I'm currently working on a simple drawing app with support for layers.
Each layer is an NSObject subclass with several properties, including
"image," which stores the image represented by that layer. I composite
them together in my view like so:

- (void)drawRect:(NSRect)rect {
	[[self transparentPattern] drawInRect:[self frame]
fromRect:NSMakeRect(0,0,[[self transparentPattern] size].width,[[self
transparentPattern] size].height) operation:NSCompositeSourceOver
fraction:1.0];

	[[self cachedImage] drawInRect:[self frame]
fromRect:NSMakeRect(0,0,[[self cachedImage] size].width, [[self
cachedImage] size].height) operation:NSCompositeSourceOver
fraction:1.0];
}

- (NSImage *)cachedImage {
	if(_cachedImage == nil) {
		[self refreshCachedImage];
	}
	return _cachedImage;
}

- (void)refreshCachedImage {
	if(_cachedImage) {
		[_cachedImage release];
	}
	_cachedImage = [[NSImage alloc] initWithSize:[self frame].size];
	[_cachedImage lockFocus];
	[self drawLayerImagesFromArray:[self layersArray]];
	[_cachedImage unlockFocus];
	[self setNeedsDisplay:YES];
}

----------
Whenever a layer changes, I call -refreshCachedImage to create a new
display image. This works well for speeding up window resizing, etc,
but when I move a layer, like so, it slows down very much when working
with large images (700x700 or larger):

- (void)mouseDragged:(NSEvent *)theEvent
{
	NSOutlineView *outlineView = [[[[NSApp delegate]
theLayersPaletteController] layersController] outlineView];
	MBLayer *selectedLayer = [outlineView itemAtRow:[outlineView selectedRow]];
	NSImage *layerImage = [selectedLayer keyValue:@"image"];

	NSPoint newOrigin = [theEvent locationInWindow];

	if([self currentTool] == MBMoveTool && layerImage != nil &&
![[selectedLayer keyValue:@"locked"] boolValue] && [[selectedLayer
keyValue:@"visible"] boolValue]) {
		int xOffset = newOrigin.x - oldOrigin.x;
		int yOffset = oldOrigin.y - newOrigin.y;

		NSImage *newLayerImage = [[NSImage alloc] initWithSize:[layerImage size]];
		[newLayerImage lockFocus];
		[layerImage compositeToPoint:NSMakePoint(xOffset,yOffset)
operation:NSCompositeSourceOver fraction:1.0];
		[newLayerImage unlockFocus];
		[selectedLayer setValue:newLayerImage forKey:@"image"];
		[layerImage release];
	}
	oldOrigin = newOrigin;
	[self refreshCachedImage];
}

----------
Does anyone know of a more efficient way to composite my layers
together? The slowdown when working with large images is pretty
unbearable.

Any help would be appreciated,
Matt Ball
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Efficient way to composite large images?
      • From: Ricky Sharp <email@hidden>
  • Prev by Date: Re: lockFocus and unlockFocus not really working at drag-time???
  • Next by Date: Re: Core Data Metadata
  • Previous by thread: Re: lockFocus and unlockFocus not really working at drag-time???
  • Next by thread: Re: Efficient way to composite large images?
  • Index(es):
    • Date
    • Thread