티스토리 뷰
my Programing/etc..
Convert From 24-bit Color Bitmap To 8-bit grayscale Bitmap
Gandawon 2008. 10. 23. 15:23static void Main(string[] args)
{
Bitmap bmp = new Bitmap("d:/tmp/screenshot.png");
Bitmap gray;
Stopwatch sw = new Stopwatch();
sw.Start();
gray = ColorToGrayscale(bmp);
sw.Stop();
gray.Save("d:/tmp/screenshot_gray.png");
Console.WriteLine(sw.Elapsed.TotalMilliseconds);
Console.ReadKey();
}
/// <summary>
/// Converts a bitmap into an 8-bit grayscale bitmap
/// </summary>
public static Bitmap ColorToGrayscale(Bitmap bmp)
{
int w = bmp.Width,
h = bmp.Height,
r, ic, oc, bmpStride, outputStride, bytesPerPixel;
PixelFormat pfIn = bmp.PixelFormat;
ColorPalette palette;
Bitmap output;
BitmapData bmpData, outputData;
//Create the new bitmap
output = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
//Build a grayscale color Palette
palette = output.Palette;
for (int i = 0; i < 256; i++)
{
Color tmp = Color.FromArgb(255, i, i, i);
palette.Entries[ i ] = Color.FromArgb(255, i, i, i);
}
output.Palette = palette;
//No need to convert formats if already in 8 bit
if (pfIn == PixelFormat.Format8bppIndexed)
{
output = (Bitmap)bmp.Clone();
//Make sure the palette is a grayscale palette and not some other
//8-bit indexed palette
output.Palette = palette;
return output;
}
//Get the number of bytes per pixel
switch (pfIn)
{
case PixelFormat.Format24bppRgb: bytesPerPixel = 3; break;
case PixelFormat.Format32bppArgb: bytesPerPixel = 4; break;
case PixelFormat.Format32bppRgb: bytesPerPixel = 4; break;
default: throw new InvalidOperationException("Image format not supported");
}
//Lock the images
bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly,
pfIn);
outputData = output.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly,
PixelFormat.Format8bppIndexed);
bmpStride = bmpData.Stride;
outputStride = outputData.Stride;
//Traverse each pixel of the image
unsafe
{
byte* bmpPtr = (byte*)bmpData.Scan0.ToPointer(),
outputPtr = (byte*)outputData.Scan0.ToPointer();
if (bytesPerPixel == 3)
{
//Convert the pixel to it's luminance using the formula:
// L = .299*R + .587*G + .114*B
//Note that ic is the input column and oc is the output column
for (r = 0; r < h; r++)
for (ic = oc = 0; oc < w; ic += 3, ++oc)
outputPtr[r * outputStride + oc] = (byte)(int)
(0.299f * bmpPtr[r * bmpStride + ic] +
0.587f * bmpPtr[r * bmpStride + ic + 1] +
0.114f * bmpPtr[r * bmpStride + ic + 2]);
}
else //bytesPerPixel == 4
{
//Convert the pixel to it's luminance using the formula:
// L = alpha * (.299*R + .587*G + .114*B)
//Note that ic is the input column and oc is the output column
for (r = 0; r < h; r++)
for (ic = oc = 0; oc < w; ic += 4, ++oc)
outputPtr[r * outputStride + oc] = (byte)(int)
((bmpPtr[r * bmpStride + ic] / 255.0f) *
(0.299f * bmpPtr[r * bmpStride + ic + 1] +
0.587f * bmpPtr[r * bmpStride + ic + 2] +
0.114f * bmpPtr[r * bmpStride + ic + 3]));
}
}
//Unlock the images
bmp.UnlockBits(bmpData);
output.UnlockBits(outputData);
return output;
}
{
Bitmap bmp = new Bitmap("d:/tmp/screenshot.png");
Bitmap gray;
Stopwatch sw = new Stopwatch();
sw.Start();
gray = ColorToGrayscale(bmp);
sw.Stop();
gray.Save("d:/tmp/screenshot_gray.png");
Console.WriteLine(sw.Elapsed.TotalMilliseconds);
Console.ReadKey();
}
/// <summary>
/// Converts a bitmap into an 8-bit grayscale bitmap
/// </summary>
public static Bitmap ColorToGrayscale(Bitmap bmp)
{
int w = bmp.Width,
h = bmp.Height,
r, ic, oc, bmpStride, outputStride, bytesPerPixel;
PixelFormat pfIn = bmp.PixelFormat;
ColorPalette palette;
Bitmap output;
BitmapData bmpData, outputData;
//Create the new bitmap
output = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
//Build a grayscale color Palette
palette = output.Palette;
for (int i = 0; i < 256; i++)
{
Color tmp = Color.FromArgb(255, i, i, i);
palette.Entries[ i ] = Color.FromArgb(255, i, i, i);
}
output.Palette = palette;
//No need to convert formats if already in 8 bit
if (pfIn == PixelFormat.Format8bppIndexed)
{
output = (Bitmap)bmp.Clone();
//Make sure the palette is a grayscale palette and not some other
//8-bit indexed palette
output.Palette = palette;
return output;
}
//Get the number of bytes per pixel
switch (pfIn)
{
case PixelFormat.Format24bppRgb: bytesPerPixel = 3; break;
case PixelFormat.Format32bppArgb: bytesPerPixel = 4; break;
case PixelFormat.Format32bppRgb: bytesPerPixel = 4; break;
default: throw new InvalidOperationException("Image format not supported");
}
//Lock the images
bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly,
pfIn);
outputData = output.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly,
PixelFormat.Format8bppIndexed);
bmpStride = bmpData.Stride;
outputStride = outputData.Stride;
//Traverse each pixel of the image
unsafe
{
byte* bmpPtr = (byte*)bmpData.Scan0.ToPointer(),
outputPtr = (byte*)outputData.Scan0.ToPointer();
if (bytesPerPixel == 3)
{
//Convert the pixel to it's luminance using the formula:
// L = .299*R + .587*G + .114*B
//Note that ic is the input column and oc is the output column
for (r = 0; r < h; r++)
for (ic = oc = 0; oc < w; ic += 3, ++oc)
outputPtr[r * outputStride + oc] = (byte)(int)
(0.299f * bmpPtr[r * bmpStride + ic] +
0.587f * bmpPtr[r * bmpStride + ic + 1] +
0.114f * bmpPtr[r * bmpStride + ic + 2]);
}
else //bytesPerPixel == 4
{
//Convert the pixel to it's luminance using the formula:
// L = alpha * (.299*R + .587*G + .114*B)
//Note that ic is the input column and oc is the output column
for (r = 0; r < h; r++)
for (ic = oc = 0; oc < w; ic += 4, ++oc)
outputPtr[r * outputStride + oc] = (byte)(int)
((bmpPtr[r * bmpStride + ic] / 255.0f) *
(0.299f * bmpPtr[r * bmpStride + ic + 1] +
0.587f * bmpPtr[r * bmpStride + ic + 2] +
0.114f * bmpPtr[r * bmpStride + ic + 3]));
}
}
//Unlock the images
bmp.UnlockBits(bmpData);
output.UnlockBits(outputData);
return output;
}
'my Programing > etc..' 카테고리의 다른 글
32bitmap To 8bit PNG 만들기 삽질중! (0) | 2008.10.28 |
---|---|
BITMAP 구조. (0) | 2008.10.25 |
Flash action script (0) | 2008.08.26 |
Java Virtual Machine (0) | 2008.08.23 |
HandsetProperty.getSystemProperty(); (0) | 2008.05.22 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- kext
- 두바이
- VirtualBox
- BigbusTour
- java
- java vm
- iPhone
- XBOX360
- Cydia
- Lee Chung Yong
- ALC888
- awn
- WeatherWidget
- theme
- euro2008
- iOS4.0
- LockInfo
- ubuntu
- 청용
- kext helper
- 한글2008
- suave
- 우분투
- 테일즈 오브 베스페리아
- MaskFilterEffect
- Mac
- 아이폰
- J2ME
- 10.5.8
- 기어스오브워2
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함