Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1406
  • Last Modified:

Read 10 bit binary data by c#

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
obeser
Asked:
obeser
  • 13
  • 10
1 Solution
 
AgariciCommented:
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
 
AgariciCommented:
found this link:http://www.codersource.net/csharp_image_Processing.aspx

seems to be what you need.

hth,
A.
0
 
obeserAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AgariciCommented:
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
 
AgariciCommented:
ah, 'byte' inside the while loops should be 'imgPtr'
0
 
obeserAuthor Commented:
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
 
AgariciCommented:
yep... hope it will work :)
0
 
obeserAuthor Commented:
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
 
AgariciCommented:
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
 
obeserAuthor Commented:
Ok i will try it.
0
 
obeserAuthor Commented:
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
 
obeserAuthor Commented:
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
 
AgariciCommented:
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
 
AgariciCommented:
anyway, you can do both and compare performances
0
 
obeserAuthor Commented:
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
 
AgariciCommented:
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
 
obeserAuthor Commented:
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
 
AgariciCommented:
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
 
obeserAuthor Commented:
So therefore in order to convert byte[] inpubuffer to short[] outpubuffer would you please revise your code.
And i will accept the answer
0
 
AgariciCommented:
i think it's ok to accept any of my posts if the code you wrote based on my suggestion works
0
 
AgariciCommented:
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
 
obeserAuthor Commented:
Because you do not try to understand the problem. You offer a solution and you insist on it.
0
 
AgariciCommented:
whatever.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 13
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now