[quoted text, click to view] > Ideally I'd like an example of a 1bppindexed file being converted to 24
> or 32bppnon-indexed file.
You did not specify a language. Here are two versions in c# and vb:
public static void unpack1bpp(System.Drawing.Bitmap srcBitmap,
System.Drawing.Bitmap dstBitmap)
{
System.Drawing.Imaging.BitmapData bmDataSrc = srcBitmap.LockBits(new
Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
srcBitmap.PixelFormat);
System.Drawing.Imaging.BitmapData bmDataDst = dstBitmap.LockBits(new
Rectangle(0, 0, dstBitmap.Width, dstBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
dstBitmap.PixelFormat);
for(int i = 0; i < bmDataSrc.Height; i++)
{
// Find the byte on which this scanline begins
IntPtr pByte = (IntPtr)(bmDataSrc.Scan0.ToInt32() + i *
bmDataSrc.Stride);
IntPtr pdwBits = (IntPtr)(bmDataDst.Scan0.ToInt32() + i *
bmDataDst.Stride);
// Loop through one aligned scanline
int l = 0;
for (int j = 0; j < bmDataSrc.Stride; j++)
{
for (int k = 0; k < 8; k++) // packed as 8 pixels per byte
{
// locate the bit
int BitNumber = 7 - (k % 8);
// get the color index
byte pixel =
System.Runtime.InteropServices.Marshal.ReadByte((IntPtr)(pByte.ToInt32() +
j));
int Index = 0;
if (((pixel >> BitNumber) & 0x03) > 0)
Index = 1;
else
Index = 0;
// look up the color in the palette and write it to the
destination bitmap
System.Drawing.Color color =
srcBitmap.Palette.Entries[Index];
System.Runtime.InteropServices.Marshal.WriteInt32((IntPtr)(pdwBits.ToInt32()
+ (l * 4)), color.ToArgb());
l += 1;
}
}
}
srcBitmap.UnlockBits(bmDataSrc);
dstBitmap.UnlockBits(bmDataDst);
}
Public Sub unpack1bpp(ByVal srcBitmap As Bitmap, ByVal dstBitmap As Bitmap)
Dim bmDataSrc As System.Drawing.Imaging.BitmapData = _
srcBitmap.LockBits(New Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height),
_
System.Drawing.Imaging.ImageLockMode.ReadOnly,
_
srcBitmap.PixelFormat)
Dim bmDataDst As System.Drawing.Imaging.BitmapData = _
dstBitmap.LockBits(New Rectangle(0, 0, dstBitmap.Width,
dstBitmap.Height), _
System.Drawing.Imaging.ImageLockMode.ReadWrite,
_
dstBitmap.PixelFormat)
For i As Integer = 0 To (bmDataSrc.Height - 1) Step 1
' Find the byte on which this scanline begins
Dim pByte As IntPtr = CType((bmDataSrc.Scan0.ToInt32() + i *
bmDataSrc.Stride), IntPtr)
Dim pdwBits As IntPtr = CType((bmDataDst.Scan0.ToInt32() + i *
bmDataDst.Stride), IntPtr)
' Loop through one aligned scanline
Dim l As Integer = 0
For j As Integer = 0 To (bmDataSrc.Stride - 1) Step 1
For k As Integer = 0 To 7 Step 1 ' packed as 8 pixels per byte
' locate the bit
Dim BitNumber As Integer = 7 - (k Mod 8)
' get the color index
Dim pixel As Byte =
System.Runtime.InteropServices.Marshal.ReadByte(CType((pByte.ToInt32() + j),
IntPtr))
Dim Index As Integer = 0
If ((pixel >> BitNumber) And &H3) > 0 Then
Index = 1
Else
Index = 0
End If
' look up the color in the palette and write it to the
destination bitmap
Dim color As System.Drawing.Color =
srcBitmap.Palette.Entries(Index)
System.Runtime.InteropServices.Marshal.WriteInt32(CType((pdwBits.ToInt32()
+ (l * 4)), IntPtr), color.ToArgb())
l += 1
Next k
Next j
Next i
srcBitmap.UnlockBits(bmDataSrc)
dstBitmap.UnlockBits(bmDataDst)
End Sub