Solved

Read 10 bit binary data by c#

Posted on 2006-06-12
23
1,301 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
ID: 16892986
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
ID: 16892994
found this link:http://www.codersource.net/csharp_image_Processing.aspx

seems to be what you need.

hth,
A.
0
 

Author Comment

by:obeser
ID: 16893156
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 11

Expert Comment

by:Agarici
ID: 16893480
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
ID: 16893500
ah, 'byte' inside the while loops should be 'imgPtr'
0
 

Author Comment

by:obeser
ID: 16893565
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
ID: 16893575
yep... hope it will work :)
0
 

Author Comment

by:obeser
ID: 16893605
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
ID: 16893720
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
ID: 16893736
Ok i will try it.
0
 

Author Comment

by:obeser
ID: 16896632
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
 

Author Comment

by:obeser
ID: 16896683
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
ID: 16901134
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
ID: 16901177
anyway, you can do both and compare performances
0
 

Author Comment

by:obeser
ID: 16901506
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
ID: 16901597
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
ID: 16901772
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
ID: 16901826
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
ID: 16901894
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
ID: 16901955
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
ID: 16909446
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
ID: 16909528
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
ID: 16909532
whatever.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 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