• 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
Re: Faster way to test pixels?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Faster way to test pixels?


  • Subject: Re: Faster way to test pixels?
  • From: Ken Thomases <email@hidden>
  • Date: Sun, 25 May 2008 01:10:50 -0500

On May 25, 2008, at 12:55 AM, Graham Cox wrote:

Trying to squeeze a bit more speed out of certain bottlenecks in my code. Here's one of them:

			NSBitmapImageRep* bits = [self pathBitmapInRect:ir];

			// if any pixels in this bitmap are set, we have a hit.

			int		x, y;
			unsigned	pixel;

			for( y = 0; y < ir.size.height; ++y )
			{
				for( x = 0; x < ir.size.width; ++x )
				{
					[bits getPixel:&pixel atX:x y:y];

					if ( pixel < 255 )
					{
						hit = YES;
						goto endOfLoop;
					}
				}
			}

			endOfLoop:


The <bits> object is a grayscale bitmap where the background is painted white and any set pixels are painted black. I just need to know if there are *any* black pixels in the image. <ir> is an NSRect which can be any size. The imaging method (not shown) accounts for the origin of this rect, so the test scans from 0,0. The purpose is to test whether a given rect intersects a rendered object (path), not just test a point against it.


Is there any faster way of doing this than just iterating over the pixels like this? I can't think of anything but someone might have a bright idea.

Well, one approach would be to optimize away the method call overhead using its IMP. <http://developer.apple.com/documentation/Performance/Conceptual/CodeSpeed/Articles/CriticalCode.html#//apple_ref/doc/uid/20001871-98344 >


Another approach would be to ask "bits" for direct access to its bitmap data (using -bitmapData) and scanning that directly, avoiding even a function call per pixel.

Lastly, you should consider using the vImage component of the Accelerate framework for the most highly optimized implementation you could hope for. <http://developer.apple.com/documentation/Performance/Conceptual/vImage/index.html > I think you want the vImageHistogramCalculation_Planar8 function <http://developer.apple.com/documentation/Performance/Reference/vImage_histogram/Reference/reference.html#//apple_ref/doc/uid/TP40005491-CH210-vImageHistogramCalculation_Planar8 >.

Cheers,
Ken
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Faster way to test pixels? (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Scope in an interface?
  • Next by Date: How to Delay, Wait, Pause...
  • Previous by thread: Faster way to test pixels?
  • Next by thread: Re: Faster way to test pixels?
  • Index(es):
    • Date
    • Thread