I need to convert an 8 bit indexed image into an 8 bit non-indexed
image.
This way would produce an non-indexed 32 bit image:
Bitmap nonindexedBitmap = new Bitmap(bitmap.Width, bitmap.Height,
PixelFormat.Format32bppRgb);
But I'm not able to find PixelFormat.Format8bppRgb. Why?
Regards,
Norbert
An indexed image has a palette. A non indexed image has discrete RGB
pixels. PixelFormat.Format8bppRgb is not possible.
> I need to convert an 8 bit indexed image into an 8 bit non-indexed
> image.
You may unpack your 8bpp indexed image into a 888RGB 24bpp image or a
8888RGB 32bpp image by using Lockbits.
See the following for a tutorial:
http://www.bobpowell.net/lockingbits.htm
"Norbert Pürringer" <thal...@graffiti.net> wrote in message
news:f0c6dea3-2d65-4cb8...@m3g2000hsc.googlegroups.com...
Are you sure? The result would be an image with only 4 (or 8) possible
shades for each of red, green and blue. If you had a blue fading sky it
would look very poor.
> This way would produce an non-indexed 32 bit image:
>
> Bitmap nonindexedBitmap = new Bitmap(bitmap.Width, bitmap.Height,
> PixelFormat.Format32bppRgb);
>
> But I'm not able to find PixelFormat.Format8bppRgb. Why?
Possibly because of the low quality image you would get I guess.
Michael
Is this a mobile format perhaps?
I would imagine that encoding such an image wouldn't be difficult unless
it used some wierd compression scheme also. You could write a
brute-force encoder if you knew the target format.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
I don't.
Michael
It appears that Norbert has lost interest. :-)
Michael
I need a way to convert it from an indexed image so I can apply a
watermark. Unless anyone can suggest how I might add a watermark to an
indexed tiff file?
Cheers for any help
Wes
*** Sent via Developersdex http://www.developersdex.com ***
To convert any indexed image, use Lockbits to access the image bits
directly.
Create a 24bpp or 32bpp destination bitmap.
For each source indexed image scanline, look up each pixel in the
palette(i.e., each pixel is an index into a palette of colors) and upack it
to your destination 24bpp or 32bpp bitmap. The palette provides you with a
distinct red, green and blue color for each index.
See Bob Powell's excellent examples for using Lockbits.
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
"Wes Lowe" <wes....@gmail.com> wrote in message
news:ez0g2DIo...@TK2MSFTNGP04.phx.gbl...
I've read through bob powells lockbits page and i feel like an idiot
because I don't know where to start implementing it.
If anyone can help simplify this that would be great, I'm really
struggling to make head nor tail of it.
Ideally I'd like an example of a 1bppindexed file being converted to 24
or 32bppnon-indexed file.
Once again, any help is appreciated, although I'm beginning to think I'm
getting in too deep here.
Thanks again.
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
I really appreciate your help, I can carry on with my project now.
Cheers again!
Michael,
Could you please help me out, how to convert Format32BppArgb image
into Format8BppIndexed image.
It is Urgent.
Thanks in Advance,
Vamsee...
There is much work to be done to code this from scratch.
Net 3.0 has support for converting bitmaps to indexed formats.
Use BitmapSource and BitmapPalette classes.
If you wish to stick with Net 2.0, you must create a palette from the
Format32BppArgb image.
There are many methods of color quantization including Octree, Wu, NeQuant,
MedianCut, etc.
You must also choose a threshold for the alpha channel conversion. For
example, the PNG format supports an alpha channel in a 8bpp image.
Once you have a palette, you need to reindex the image by choosing the
closest color in that palette that matches the original color in the
Format32BppArgb image. To get a nice looking image, you may need to dither
the image using Floyd-Steinberg or another dithering algorithm.
This requires using Lockbits to cycle through both images.
Here is the code for the Octree quantizer:
http://msdn.microsoft.com/en-us/library/aa479306.aspx
Bob Powell's website hase code to use Lockbits:
http://bobpowell.net/
There are other libraries that will do the work for you they include:
AFORGE
http://code.google.com/p/aforge/
IPLAB
http://code.google.com/p/iplab/
Hi Michael,
First of All, I am new to GDI / Graphics.
I am using .NET Framework 2.0. My Images are containing Black, White
and Gray colors.
Before sending request to you, I have gone through http://bobpowell.net/
That site contains code converting 32Bpp Images into 1Bpp and 4Bpp.
I got confused while reading 4Bpp conversion code, because what value
should be passed to colorIndex?
How to use Colorpalette? If you have code please help. The conversion
is single time conversion only for 800 records.
Thanks in Advance.
Vamsee...
Please read this article:
http://msdn.microsoft.com:80/en-us/library/aa479306.aspx
This article provides you with complete code and explanation for every step
in the process.