Re: Drawing noise in Cocoa (preferably fast)
Re: Drawing noise in Cocoa (preferably fast)
- Subject: Re: Drawing noise in Cocoa (preferably fast)
- From: Stephen Blinkhorn <email@hidden>
- Date: Wed, 15 Jun 2011 12:32:19 -0600
Thanks Ken, will test this out.
Stephen
On 14 Jun 2011, at 23:44, Ken Tozier wrote:
I didn't test this (basically cut and pasted from some existing code
I have) but it should run plenty fast
#define RGB_BYTES_PER_PIXEL 3
- (NSBitmapImageRep *) noiseImageRepWithBaseRed:(float) inRed
green:(float) inGreen
blue:(float) inBlue
width:(NSUInteger) inWidth
height:(NSUInteger) inHeight
{
NSUInteger resultSize = RGB_BYTES_PER_PIXEL * width * height;
// allocate buffer, init pointers and convert input floats to 8 bit
ints
unsigned char *buffer = (unsigned char *) calloc(resultSize, 1),
*pr = buffer,
*pg = pr + 1,
*pb = pg + 1,
*e = buffer + resultSize - RGB_BYTES_PER_PIXEL;
r = inRed * 255,
g = inGreen * 255,
b = inBlue * 255,
noise;
// it looked from your code that you only wanted the random
// number to contribute 10% to the color, so if you pre-divide
// the byte max (255) by 10 here, that's one less calculation
// needed inside the loop
float byteMax = 26;
// seed the random function
srandom(time(0));
// add random value to source rgb values
while (pr < e)
{
noise = ((float)random() * byteMax);
// add noise to r,g,b values, save in buffer and increment pointer
to next slot
*pr = r + noise; pr += RGB_BYTES_PER_PIXEL;
*pg = g + noise; pg += RGB_BYTES_PER_PIXEL;
*pb = b + noise; pb += RGB_BYTES_PER_PIXEL;
}
// now create an image rep with your raw byte array
NSBitmapImageRep *result = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: &buffer
pixelsWide: inWidth
pixelsHigh: inHeight
bitsPerSample: 8
samplesPerPixel: RGB_BYTES_PER_PIXEL
hasAlpha: NO
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: RGB_BYTES_PER_PIXEL * width
bitsPerPixel: RGB_BYTES_PER_PIXEL * 8] autorelease];
NSLog(@"result: %@", result);
// clean up
free(buffer);
return result;
}
On Jun 14, 2011, at 6:20 PM, Stephen Blinkhorn wrote:
Thanks for all the Core Image tips. Completely missed that somehow.
At the moment I'm drawing a lot of stuff into CGLayers to save
redrawing the same thing over and over. I see CIImage provides
imageWithCGLayer: so this could be very fruitful.
Thanks!
Stephen
On 14 Jun 2011, at 16:07, Quincey Morris wrote:
On Jun 14, 2011, at 14:56, Stephen Blinkhorn wrote:
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:
Have you looked into using Core Image, e.g. the CIRandomGenerator
filter composited onto an image filled with the base color?
For something trivial (in Core Image terms) like this, there's a
fairly steep learning curve, but the point of Core Image *is*
performance.
_______________________________________________
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
_______________________________________________
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