S
surturz
This article is helpful in creating 1bpp (Monochrome) GIFs using GDI+
http://support.microsoft.com/kb/319061
However, I'm using VB 2005 and am having trouble copying the pixels across.
The C code I'm having trouble converting follows:
unsafe
{
// Get the pointer to the image bits.
// This is the unsafe operation.
byte * pBits;
if (bitmapData.Stride > 0)
pBits = (byte *)pixels.ToPointer();
else
// If the Stide is negative, Scan0 points to the last
// scanline in the buffer. To normalize the loop, obtain
// a pointer to the front of the buffer that is located
// (Height-1) scanlines previous.
pBits = (byte *)pixels.ToPointer() + bitmapData.Stride*(Height-1);
uint stride = (uint)Math.Abs(bitmapData.Stride);
for ( uint row = 0; row < Height; ++row )
{
for ( uint col = 0; col < Width; ++col )
{
// Map palette indexes for a gray scale.
// If you use some other technique to color convert,
// put your favorite color reduction algorithm here.
Color pixel; // The source pixel.
// The destination pixel.
// The pointer to the color index byte of the
// destination; this real pointer causes this
// code to be considered unsafe.
byte * p8bppPixel = pBits + row*stride + col;
pixel = BmpCopy.GetPixel((int)col, (int)row);
// Use luminance/chrominance conversion to get grayscale.
// Basically, turn the image into black and white TV.
// Do not calculate Cr or Cb because you
// discard the color anyway.
// Y = Red * 0.299 + Green * 0.587 + Blue * 0.114
// This expression is best as integer math for performance,
// however, because GetPixel listed earlier is the slowest
// part of this loop, the expression is left as
// floating point for clarity.
double luminance = (pixel.R *0.299) +
(pixel.G *0.587) +
(pixel.B *0.114);
// Gray scale is an intensity map from black to white.
// Compute the index to the grayscale entry that
// approximates the luminance, and then round the index.
// Also, constrain the index choices by the number of
// colors to do, and then set that pixel's index to the
// byte value.
*p8bppPixel = (byte)(luminance * (nColors-1)/255 +0.5);
} /* end loop for col */
} /* end loop for row */
} /* end unsafe */
// To commit the changes, unlock the portion of the bitmap.
bitmap.UnlockBits(bitmapData);
http://support.microsoft.com/kb/319061
However, I'm using VB 2005 and am having trouble copying the pixels across.
The C code I'm having trouble converting follows:
unsafe
{
// Get the pointer to the image bits.
// This is the unsafe operation.
byte * pBits;
if (bitmapData.Stride > 0)
pBits = (byte *)pixels.ToPointer();
else
// If the Stide is negative, Scan0 points to the last
// scanline in the buffer. To normalize the loop, obtain
// a pointer to the front of the buffer that is located
// (Height-1) scanlines previous.
pBits = (byte *)pixels.ToPointer() + bitmapData.Stride*(Height-1);
uint stride = (uint)Math.Abs(bitmapData.Stride);
for ( uint row = 0; row < Height; ++row )
{
for ( uint col = 0; col < Width; ++col )
{
// Map palette indexes for a gray scale.
// If you use some other technique to color convert,
// put your favorite color reduction algorithm here.
Color pixel; // The source pixel.
// The destination pixel.
// The pointer to the color index byte of the
// destination; this real pointer causes this
// code to be considered unsafe.
byte * p8bppPixel = pBits + row*stride + col;
pixel = BmpCopy.GetPixel((int)col, (int)row);
// Use luminance/chrominance conversion to get grayscale.
// Basically, turn the image into black and white TV.
// Do not calculate Cr or Cb because you
// discard the color anyway.
// Y = Red * 0.299 + Green * 0.587 + Blue * 0.114
// This expression is best as integer math for performance,
// however, because GetPixel listed earlier is the slowest
// part of this loop, the expression is left as
// floating point for clarity.
double luminance = (pixel.R *0.299) +
(pixel.G *0.587) +
(pixel.B *0.114);
// Gray scale is an intensity map from black to white.
// Compute the index to the grayscale entry that
// approximates the luminance, and then round the index.
// Also, constrain the index choices by the number of
// colors to do, and then set that pixel's index to the
// byte value.
*p8bppPixel = (byte)(luminance * (nColors-1)/255 +0.5);
} /* end loop for col */
} /* end loop for row */
} /* end unsafe */
// To commit the changes, unlock the portion of the bitmap.
bitmap.UnlockBits(bitmapData);