Solved

Read 10 bit binary data by c#

Posted on 2006-06-12
23
1,270 Views
Last Modified: 2008-01-09
Hi,
I am trying to read a 3712x3712 grid data satellite image by c#.
The each pixel value is written 10 bit. I have to read the value of each pixel.
Cheers,



0
Comment
Question by:obeser
  • 13
  • 10
23 Comments
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
you have to load the image into a Bitmap object. then use lockbits to get the bitmapdata.
from bitmapdata the Scan0 property gives you the address of raw bitmap data (pixel bytes).

hth,
A.
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
found this link:http://www.codersource.net/csharp_image_Processing.aspx

seems to be what you need.

hth,
A.
0
 

Author Comment

by:obeser
Comment Utility
hi
i have seen the article on codesource before.
But it does not work for my case. I have to write a bitreader. and read 10 bit from file or buffer and will convert it to int16.

cheers
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
scan0 gives you access to image bytes. it your image format uses 10 bits per pixel it means that you need to extract those 10 bits from the mem.

something like this:
(but this code is written directly here and untested, so do not expect it to work or even compile. take it more as a pseudocode algorithm)

unsafe
{
   byte offset = 0;
   byte* imgPtr = ( byte* )( data.Scan0 );
   int height = 0;
   int width = 0;
   while( height < data.Height )
   {
      width = 0;
      while( width < data.Width )
      {
         offset = 8;
         while( offset > 0 )
         {
            onePixel = *byte;
            onePixel <<= offset;
            onePixel |= ( *(byte+1) >> (8-10+offset) );

            // do something with onPixel

            byte++;offset = 10-offset;
         }
         width ++;
       }
     
      height ++;
   }
}

hth,
A.
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
ah, 'byte' inside the while loops should be 'imgPtr'
0
 

Author Comment

by:obeser
Comment Utility
So the true code is as follows. is it
unsafe
{
   byte offset = 0;
   byte* imgPtr = ( byte* )( data.Scan0 );
   int height = 0;
   int width = 0;
   while( height < data.Height )
   {
      width = 0;
      while( width < data.Width )
      {
         offset = 8;
         while( offset > 0 )
         {
            onePixel = *imgPtr ;
            onePixel <<= offset;
            onePixel |= ( *(imgPtr +1) >> (8-10+offset) );

            // do something with onPixel

            imgPtr ++;offset = 10-offset;
         }
         width ++;
       }
     
      height ++;
   }
}
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
yep... hope it will work :)
0
 

Author Comment

by:obeser
Comment Utility
here in this code. where did you get 10 bit and convert it to 16 bit int value.
I could not catch it. You play with bytes. But i think we should play with bits.
Also i do not ant to use BitMap class.
Instead i want to write a binarybitreader. And Convert the readbits into desired type.
cheers,
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
onePixel is your 16 bit value
and i extract 10 bits from the byte array pointed by imgPtr and fill them into onePixel.
as you said, i play with bytes.
the problem is: since i have a byte array and i need 10 bit value, i have to use 8 bits from the first byte and 2 from the second to build the first 10 bit value.
then, for the second 10 bit value i use the remaining 6 bits of the second byte in the array and 4 bits from the third byte... and so on...
it is a 4 step operation until the offset is 0 again and you build your 5th 10 bit value using 8 bits from the 6th byte in array and 2 from the 7th.
the byte array i am speaking of is imgPtr - the image raw bytes.
0
 

Author Comment

by:obeser
Comment Utility
Ok i will try it.
0
 

Author Comment

by:obeser
Comment Utility
I have just noticed that in your code at
while (offset > 0) loop never end. Because the initial value is 8 and
offset=10-offset
at first offset=2
then offset=8
then offset=2
then offset=8 and go on.
Offset is always greater then 0. Therefore while loop never end.

Isnt it?

Cheers Ozgur
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:obeser
Comment Utility
And assume you have a byte array

byte[] inputbuffer of length 37120 bits namely 4640 bytes.

I want to extract 3712 "10 bit"  value from this byte array and load into int16[] intvalues array of length 3712.

How can i achieve this. I think about converting byte array to bit array by

BitArray bits = new BitArray(inpubuffer);

Just an idea. I hope i achieved to describe my problem.

Cheers,
Ozgur
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
i told you i wrote the code directly into this page...

so, about the offset thing, you should modify the code like this:
 
     while( width < data.Width )
      {
         offset = 0;
         while( offset < 8 )
         {
            onePixel = *imgPtr ;
            onePixel <<= 8 - offset;
            onePixel |= ( *(imgPtr +1) >> (2+offset) );

            // do something with onPixel

            imgPtr ++;offset += 2;
         }
         width ++;
       }

you can use the bitarray but i think you will have low performaces
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
anyway, you can do both and compare performances
0
 

Author Comment

by:obeser
Comment Utility
By your help i wrote following routine to extract 3712 "16bit" value from 4640 bytes.
Is it OK? or would you please correct it?

Cheers,
Ozgur

public short[] Convert10BitTo16Bit(byte[] inputbuffer)
{
    byte offset = 0;
    byte imgPtr;
    short onepixel;
    //input buffer is 4640 bytes
     for(int i=0;i<=inputbuffer.Count;i++)
     {
         imgPtr=inputbuffer[i];
         offset = 0;
         while( offset < 8 )
         {
            onePixel = imgPtr ;
            onePixel <<= 8 - offset;
            onePixel |= ( *(imgPtr +1) >> (2+offset) );

            // do something with onPixel

           
            offset += 2;
         }
     }
}
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
basicaly it's ok, but:
where do you get the byte[] inputbuffer from?
because the bitmapdata.scan0 gives you a byte* not a managed byte[]

i would have written it like this:
(because from bitmapdata.scan0 you get a byte*and it is faster to just advance the byte pointer by doing imgptr++ then geting the byte with inputBuffer[i] )

public short[] Convert10BitTo16Bit(byte* inputbuffer, int nByteCount )
{
    byte offset = 0;
    byte* imgPtr;
    short onepixel;
    //input buffer is 4640 bytes
    imgPtr=inputbuffer;
    for(int i=0 ; i<nByteCount ; i++)
     {
        offset = 0;
         while( offset < 8 )
         {
            onePixel = imgPtr ;
            onePixel <<= 8 - offset;
            onePixel |= ( *(imgPtr +1) >> (2+offset) );

            // do something with onPixel

           
            offset += 2;
            imgPtr++;
         }
     }
}

hth,
A.
0
 

Author Comment

by:obeser
Comment Utility
Agaraci,
I have an satellite image. Composed of three part 1-Hedaer 2-Image Data and 3-Trailer

My aim is to read image data.

Image is composed of 3712x3712 pixel. But in every line 27 byte of line information is written.
Therefore i read every line of image from file with BinaryReader Class;
After reading hedaer my code is as follows

byte[] inputbuffer;
byte[] lineheader;
short[] outputbuffer;
for (int i=0;i<=3711;i++)
{
      lineheaer=BinaryReader.ReadBytes(27);
      inputbuffer=BinaryReader.ReadBytes(3712*10);
      //here i will use convertion and obtain 3712 length short array of pixel values
     outputbuffer=Convert10BitTo16Bit();
      //Write outpubuffer
     BinaryWriter.Write(outputbuffer);
}
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
i was under the impression that you have an image (like a jpg file) wich you load into a Bitmap object and so on

ok, in that case, no objections to your code.
0
 

Author Comment

by:obeser
Comment Utility
So therefore in order to convert byte[] inpubuffer to short[] outpubuffer would you please revise your code.
And i will accept the answer
0
 
LVL 11

Accepted Solution

by:
Agarici earned 500 total points
Comment Utility
i think it's ok to accept any of my posts if the code you wrote based on my suggestion works
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
why C ????
i have practically solved your problem and more, i did my best to make you understand the solution

i dont think this is fair.
0
 

Author Comment

by:obeser
Comment Utility
Because you do not try to understand the problem. You offer a solution and you insist on it.
0
 
LVL 11

Expert Comment

by:Agarici
Comment Utility
whatever.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now