Skip to content

Instantly share code, notes, and snippets.

@lukehorvat
Last active December 11, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukehorvat/4537745 to your computer and use it in GitHub Desktop.
Save lukehorvat/4537745 to your computer and use it in GitHub Desktop.
An example of concurrent image brightness normalization in C#.
//determine the brightness range of this thread's image strip
Tuple brightnessRange = GetImagePartBrightnessRange(image, startX, startY, finishX, finishY);
//update the global brightness range of the entire image
lock (_lock)
{
minBrightness = Math.Min(minBrightness, brightnessRange.Item1);
maxBrightness = Math.Max(maxBrightness, brightnessRange.Item2);
}
//wait at the barrier until all image strips have been assessed
barrier.Wait();
//now that the brightness range of the entire image is known, normalize the brightness of the image strip
NormalizeImagePart(image, startX, startY, finishX, finishY, minBrightness, maxBrightness);
//raise event so that event listeners are notified that the image strip has been normalized
ImagePartNormalized(this, new ImagePartModifiedEventArgs(startX, startY, finishX, finishY));
bool isLastThread = barrier.Wait();
//if this thread is the last one past the barrier, then we
//can safely assume all image strips have been normalized
if (isLastThread)
{
//raise event so that event listeners are notified that the normalization is complete
ImageNormalizationCompleted(this, EventArgs.Empty);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment