Is there any reason the noise has to be unique each time? Could you just 
generate a noise file and composite that over whatever you want? Unless you're 
doing something with cryptography, noise, is noise, is noise. It all basically 
looks the same so reusing a single file seems like it could serve your 
purposes...

On Jun 14, 2011, at 5:56 PM, Stephen Blinkhorn wrote:

> Hi all,
> 
> I'm looking to fill a large custom view with noise. The solution I have 
> arrived at so far is taking much too long to draw for it to be useable. Does 
> anyone know any better ways of achieving this in Cocoa? The basic idea is to 
> add an amount of noise to a base color pixel by pixel, for example:
> 
> // create base color
> NSColor *base_color = [NSColor colorWithCalibratedRed:0.25 green:0.3 
> blue:0.35 alpha:1.0]; //[style colorForKey:@"Section"];
> 
> // get color components
> CGFloat red, green, blue, alpha;
> [base_color getRed:&red green:&green blue:&blue alpha:&alpha];
> 
> // create rect and boundary variables
> NSRect noise_rect = NSMakeRect(0, 0, 1, 1);
> NSInteger width = [self bounds].size.width;
> NSInteger height = [self bounds].size.height;
> 
> // main loop
> NSInteger x_pos = 0;
> for(; x_pos<width; x_pos+=1)
> {
>       NSInteger y_pos = 0;
>       for(; y_pos<height; y_pos+=1)
>       {
>               // create noise (0..1 range)
>               float pixel_noise = (rand() % 1000 * 0.0001);
>               // add noise to color variables
>               [[NSColor colorWithCalibratedRed:red+pixel_noise 
> green:green+pixel_noise blue:blue+pixel_noise alpha:alpha] set];
>               // draw a 1X1 pixel rect
>               noise_rect.origin.x = x_pos;
>               noise_rect.origin.y = y_pos;
>               NSRectFill(noise_rect);
>       };
> };
> _______________________________________________
> 
> 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/kentozier%40comcast.net
> 
> This email sent to [email protected]

_______________________________________________

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