Click here to Skip to main content
15,888,273 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello!

I need to find a way in C# to parse a binary file which contains an image in raw data (no headers, metadata, compresssion, etc.) in a 2-2-2-2 format. This is 2 bits for each RGB color and 2 bits for opacity. I need to read it and display the resulting image. I am new to C# and have not been able to find a relevant answer in the posts, any ideas? I am using VS 2008 Prof edition.

AQ
Posted
Comments
Sergey Alexandrovich Kryukov 31-May-11 13:37pm    
Tag it! WPF or System.Drawing? There is no just "binary file". What format?
Basically imaging name space in the library you want to use; and you will get an answer. How come it's a problem?
--SA
ArmandoQ 31-May-11 14:06pm    
It is System.Drawing. This is a custom format and it contains 2 bits for each color (RGB) and 2 bits for opacity. I need to read each byte and convert it to a pixel that I can then draw on the screen. I know the image is 800 x 600 and I know the values for each color and Alpha channel.
Sergey Alexandrovich Kryukov 31-May-11 13:38pm    
If this is custom format -- tag your library you want to use. It's also easy to answer.
--SA
aidin Tajadod 31-May-11 13:59pm    
Do you mean byte or bit?!
ArmandoQ 31-May-11 14:08pm    
I have a BYTE that contains 2 BITS for each RGB color and 2 BITS for opacity. This is similar to 8 bpp format but we are using 2-2-2-2 coding.

If you are using System.Drawing then you may create a bitmap of the same size then use the SetPixel method (or, if performance matters, the LockBits one, see Bitmap.Lockbits[^] at MSDN). The straighforward transformation of the RGBA components is the simple multiplication by 64 85 (sorry 64 was plainly wrong :-O, see SAKryukov reply), but you may choose alternatives.
 
Share this answer
 
v2
Comments
ArmandoQ 31-May-11 14:24pm    
It looks like this is just what I needed. I will code something and post results if it works.
Sergey Alexandrovich Kryukov 31-May-11 19:33pm    
Basically, correct, but do not advise SetPixel (I'm afraid to imagine the performance in this case, hardly acceptable). Multiply by 64 is incorrect (surprise? count it! don't forget, 2 is not maximum but number of bits; yesotaso won't let me lie here, please see his comment :-). I voted 4 this time.

Please see my answer.
--SA
yesotaso 31-May-11 21:42pm    
With all the good and bad we are mere mortals sir :)
Sergey Alexandrovich Kryukov 31-May-11 21:46pm    
And some of us probably even head to hell... :-)
"Memento mori!" (But why you remember it right now? Everything goes all right so far... :-)
Thank you.
--SA
CPallini 1-Jun-11 3:13am    
Yes, 85 is the correct factor, thank you for pointing out.
First rule. Do not use GetPixel/SetPixel: it's too slow! You can get acceptable performance only when you do it all at once.

Do the following. Create empty bitmap of the class System.Drawing.Bitmap and use System.Drawing.Bitmap.LockBits method. For pixelFormat, use System.Drawing.Imaging.PixelFormat.Format32bppPArgb (precisely this one, this is what you minimally need). Fill in the value of each pixel from your 2-2-2-2 data. Convert your 2-bit components for each color and opacity to 8-bit, so you maximum value of 3 is mapped to 255: byte component8 = component2 * 85 (sic!).

The code sample on this page will give you the idea how to address pixels and move data: http://msdn.microsoft.com/en-us/library/5ey6h79d(v=VS.100).aspx[^]. Pay attention for the use of System.Runtime.InteropServices.Marshal.

That should be enough to solve your problem.

—SA
 
Share this answer
 
Comments
Manfred Rudolf Bihy 31-May-11 20:14pm    
Sorry SA! Me thinks you got something mixed up.

1 * 64 = 64
2 * 64 = 128
3 * 64 = 192
4 * 64 = 256

Please explain to a mere mortal why you chose 85. Maybe I'm missing out on something here.
Enlighten me! :)
yesotaso 31-May-11 20:52pm    
0 -> 0
1 -> 85
2 -> 170
3 -> 255 ?
Manfred Rudolf Bihy 31-May-11 21:08pm    
:doh:
Caught, convicted, hanged!
I do feel a little dumb just right now.
Sergey Alexandrovich Kryukov 31-May-11 21:27pm    
One lapsus by me, one by you... We mere mortals quit today :-) no big deal at all.
--SA
Manfred Rudolf Bihy 31-May-11 21:45pm    
True! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900