• 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
Branch: Drawing noise in Cocoa (preferably fast)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Branch: Drawing noise in Cocoa (preferably fast)


  • Subject: Branch: Drawing noise in Cocoa (preferably fast)
  • From: Ken Tozier <email@hidden>
  • Date: Fri, 17 Jun 2011 22:29:25 -0400

Keying off Stephen's idea of drawing noise quickly, I came up with a solution that I can use for my own purposes. It seems like splitting up large image manipulations for execution on different processor cores could be a good introduction to GCD and blocks. I read the intro to GCD docs but am still a little in the dark about the whole topic. For example, say I'm using the following method to generate 10,000 x 10,000 or 50,000 x 50,000 pixel images, how would I go about "blockifying" it for GCD?

#define RGB_BYTES_PER_PIXEL		3

- (CGImageRef) noiseImageRefWithBaseRed:(float) inRed
		green:(float) inGreen
		blue:(float) inBlue
		width:(NSUInteger) inWidth
		height:(NSUInteger) inHeight
{
	NSUInteger		resultSize	= RGB_BYTES_PER_PIXEL * inWidth * inHeight;

	// used to preserve the basic hue of the input color
	float				clipPercent	= 4.0/10;

	// convert input color floats to 8 bit ints
	unsigned char	*buffer		= (unsigned char *) malloc(resultSize),
					*pr			= buffer,
					*pg			= pr + 1,
					*pb			= pg + 1,
					*e			= pr + resultSize,
					r			= (inRed * 255),
					g			= (inGreen * 255),
					b			= (inBlue * 255),
					redOffset,
					greenOffset,
					blueOffset,
					noise;

	// seed the random function
	srandom(time(0));

	// To preserve the basic hue of the input color,
	// clip offsets of non-major colors by a fixed amount
	if ((r > g) && (r > b))
	{
		redOffset	= 0;
		greenOffset	= (r - g) - (r - g) * clipPercent;
		blueOffset	= (r - b) - (r - b) * clipPercent;
	}
	else if ((g > r) && (g > b))
	{
		greenOffset	= 0;
		redOffset	= (g - r) - (g - r) * clipPercent;
		blueOffset	= (g - b) - (g - b) * clipPercent;
	}
	else
	{
		blueOffset	= 0;
		redOffset	= (b - r) - (b - r) * clipPercent;
		greenOffset	= (b - g) - (b - g) * clipPercent;
	}


	NSLog(@"starting noise loop");
	while (pr < e)
	{
		noise	= random();

		*pr	= r + (noise % redOffset);		pr += RGB_BYTES_PER_PIXEL;
		*pg	= g + (noise % greenOffset);	pg += RGB_BYTES_PER_PIXEL;
		*pb	= b + (noise & blueOffset);		pb += RGB_BYTES_PER_PIXEL;
	}
	NSLog(@"noise loop complete");

	// create data provider
	CGDataProviderRef		imgDataProvider	= CGDataProviderCreateWithData(NULL, buffer, resultSize, NULL);
	CGColorSpaceRef		imgColorSpace	= CGColorSpaceCreateDeviceRGB();
	CGImageRef				img				= CGImageCreate (
																inWidth,
																inHeight,
																8,
																RGB_BYTES_PER_PIXEL * 8,
																RGB_BYTES_PER_PIXEL * inWidth,
																imgColorSpace,
																kCGImageAlphaNone,
																imgDataProvider,
																NULL,
																true,
																kCGRenderingIntentDefault
															);

	CFRelease(imgDataProvider);
	CFRelease(imgColorSpace);

	free(buffer);

	return img;
}_______________________________________________

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

  • Follow-Ups:
    • Re: Branch: Drawing noise in Cocoa (preferably fast)
      • From: Seth Willits <email@hidden>
  • Prev by Date: Re: Drawing noise in Cocoa (preferably fast)
  • Next by Date: Re: Drawing noise in Cocoa (preferably fast)
  • Previous by thread: Re: Weird Crash in
  • Next by thread: Re: Branch: Drawing noise in Cocoa (preferably fast)
  • Index(es):
    • Date
    • Thread