Axiom.Media.Image.ApplyGamma C# (CSharp) Method

ApplyGamma() public static method

Variant of ApplyGamma that operates on an unmanaged chunk of memory
public static ApplyGamma ( IntPtr bufPtr, float gamma, int size, int bpp ) : void
bufPtr System.IntPtr
gamma float
size int
bpp int
return void
		public static void ApplyGamma( IntPtr bufPtr, float gamma, int size, int bpp )
		{
			if ( gamma == 1.0f )
				return;

			//NB only 24/32-bit supported
			if ( bpp != 24 && bpp != 32 )
				return;

			int stride = bpp >> 3;
			unsafe
			{
				byte* srcBytes = (byte*)bufPtr.ToPointer();

				for ( int i = 0, j = size / stride, p = 0; i < j; i++, p += stride )
				{
					float r, g, b;

					r = (float)srcBytes[ p + 0 ];
					g = (float)srcBytes[ p + 1 ];
					b = (float)srcBytes[ p + 2 ];

					r = r * gamma;
					g = g * gamma;
					b = b * gamma;

					float scale = 1.0f, tmp;

					if ( r > 255.0f && ( tmp = ( 255.0f / r ) ) < scale )
						scale = tmp;
					if ( g > 255.0f && ( tmp = ( 255.0f / g ) ) < scale )
						scale = tmp;
					if ( b > 255.0f && ( tmp = ( 255.0f / b ) ) < scale )
						scale = tmp;

					r *= scale;
					g *= scale;
					b *= scale;

					srcBytes[ p + 0 ] = (byte)r;
					srcBytes[ p + 1 ] = (byte)g;
					srcBytes[ p + 2 ] = (byte)b;
				}
			}
		}

Same methods

Image::ApplyGamma ( byte buffer, float gamma, int size, int bpp ) : void