Re: Drawing noise in Cocoa (preferably fast)
Re: Drawing noise in Cocoa (preferably fast)
- Subject: Re: Drawing noise in Cocoa (preferably fast)
- From: Ken Tozier <email@hidden>
- Date: Wed, 15 Jun 2011 00:30:51 -0400
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@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
_______________________________________________
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