Saturday, September 27, 2008

Brighten Photo

Brighten Photo 

In this post, a simple function is presented to brighten/darken photos. In this function I use GDI+ and write code in unsafe mode (for efficiency).

using System.Drawing;
using System.Drawing.Imaging;
using GRBIF._CONST;
using System;

namespace IntelliPhoto
{

// Description: A simple GDI+ function for brightening
// Author: G. R. Roosta
// License: Free To Use (No Restriction)

public class BasicFilter
{

public static void Brighten(Bitmap bitmap, int brightness, string newFullFileName)
{
Brighten(bitmap, brightness);
bitmap.Save(newFullFileName);
return;
}

public static void Brighten(Bitmap bitmap, int brightness) // -255 <= brightness <= 255
{
if (brightness < -255 brightness > 255)
throw new Exception("Invalid 'brightness' value!");
BitmapData bmpData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmpData.Stride;
System.IntPtr Scan0 = bmpData.Scan0;
unsafe
{
byte* ptr = (byte*)(void*)Scan0;
int offset = stride - bitmap.Width * 3;
int netWidth = bitmap.Width * 3;
for (int y = 0; y <>
{
for (int x = 0; x <>
{
int c = ptr[0] + brightness;
if (c > 255)
c = 255;
else if (c <>
c = 0;

ptr[0] = (byte)c;
++ptr;
}
ptr += offset;
}
}

bitmap.UnlockBits(bmpData);
return;
}
}
}

Example:


Original Image (taken by Digital Camera: Sony DSC-S650)





Brightened with brightness=+40 using IntelliPhoto.BasicFilter.Brigten




Brightened with brightness=+80 using IntelliPhoto.BasicFilter.Brigten





Brightened with brightness=-40 using IntelliPhoto.BasicFilter.Brigten






Brightened with brightness=-80 using IntelliPhoto.BasicFilter.Brigten

No comments:

Post a Comment