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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to