• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

Read 10 bit data by c#

I am trying to read an image data. of 3712x3712 pixel. Each pixel value is 10 bit and in big endian order.
I have to read each pixel value and convert them to 16 bit "int". how can i achieve it using c#
0
obeser
Asked:
obeser
1 Solution
 
aaronfengCommented:
Just out of curiosity what's the file format you are trying to read?  I'm have done a little bit of image processing in the past, but it was all byte based not bit.

Cheers,

Aaron
http://geekswithblogs.net/afeng
0
 
HeidarVCommented:
use System.Text.Encoding.BigEndianUnicode.GetChars()
0
 
obeserAuthor Commented:
It is a seviri native image. "use System.Text.Encoding.BigEndianUnicode.GetChars()" does not work for this case.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
_Katka_Commented:
Hi,

1) Endianess is not applied to this example because it's 10-bit number (thus b10...b1 in any case)
2) try to read all the data in buffer (or alternatively the LCM of 10 and 16 bits = 80 bits = 8 bytes per buffer)
3) make a function:

public Boolean GetBit(int bitIndex, List<Byte> bitField)
{
      return (bitField[bitIndex / 8] & (1 << (bitIndex % 8))) > 0;
}

4) go thru the buffer bits step by step:

public List<Int32> Convert10BitTo16Bit(List<Byte> inputBuffer)
{
      Int32 actualValue = 0;
      List<Int32> outputBuffer = new List<Int32>();

      for (int a = 0; a < yourBuffer.Length * 8; a++)
      {
            // every ten bits store the actual 16-bit value
            if (a != 0 && a % 10 == 0)
            {
                  outputBuffer.Add(actualValue);
                  actualValue = 0;    
            }

            // sum the current bit to our actual value
            if (GetBit(a, inputBuffer))
            {
                  actualValue += 1 << (9 - (a % 10));
            }
      }

      return outputBuffer;
}

Note: it's written just from a head.. so pardon some minor errors

regards,
Kate
0
 
obeserAuthor Commented:
Hi Kate I have graded your solution AA at first look. Because it seems to work. But when i tried to run it
i noticed that it does not work correctly.

1- it does not handle all the buffer you always got 1 member missing. that is
(int a = 0; a < yourBuffer.Length * 8) syntax it does not deal with last 10 bit
2-In conversion i think there is a problem.

if you revise please  your solution i would be happy.
Ozgur
0
 
_Katka_Commented:
Hi.. you're right.. the algo's missing the last value just modify it like this it should help:

public List<Int32> Convert10BitTo16Bit(List<Byte> inputBuffer)
{
      Int32 actualValue = 0;
      List<Int32> outputBuffer = new List<Int32>();

      for (int a = 0; a < yourBuffer.Length * 8; a++)
      {
            // every ten bits store the actual 16-bit value
            if (a != 0 && a % 10 == 0)
            {
                  outputBuffer.Add(actualValue);
                  actualValue = 0;    
            }

            // sum the current bit to our actual value
            if (GetBit(a, inputBuffer))
            {
                  actualValue += 1 << (9 - (a % 10));
            }
      }

      // save the last value
      outputBuffer.Add(actualValue);

      return outputBuffer;
}

regards,
Kate
0
 
_Katka_Commented:
Well I tried to put it in the VS and it worked well in this form:

using System;
using System.Collections.Generic;
using System.Text;

namespace BitConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Byte> field = new List<byte>();
            List<Int32> result = null;

            field.Add(255);
            field.Add(255);
            field.Add(255);
            field.Add(255);
            field.Add(255);
            field.Add(0);
            field.Add(0);
            field.Add(0);
            field.Add(0);
            field.Add(0);

            result = Convert10BitTo16Bit(field);
        }

        public static Boolean GetBit(int bitIndex, List<Byte> bitField)
        {
            return (bitField[bitIndex / 8] & (1 << (bitIndex % 8))) > 0;
        }

        public static List<Int32> Convert10BitTo16Bit(List<Byte> inputBuffer)
        {
            Int32 actualValue = 0;
            List<Int32> outputBuffer = new List<Int32>();

            for (int a = 0; a < inputBuffer.Count * 8; a++)
            {
                // every ten bits store the actual 16-bit value
                if (a != 0 && a % 10 == 0)
                {
                    outputBuffer.Add(actualValue);
                    actualValue = 0;
                }

                // sum the current bit to our actual value
                if (GetBit(a, inputBuffer))
                {
                    actualValue += 1 << (9 - (a % 10));
                }
            }

            // save the last value
            outputBuffer.Add(actualValue);

            return outputBuffer;
        }
    }
}

the result is 8 x Int32 field containing:

[1] - 1023
[2] - 1023
[3] - 1023
[4] - 1023
[5] - 0
[6] - 0
[7] - 0
[8] - 0

which is correct

regards,
Kate
0
 
obeserAuthor Commented:
Would you please explain what this line do

  actualValue += 1 << (9 - (a % 10));

cheers,
ozgur
0
 
obeserAuthor Commented:
I tried the code you supplied but could get the right image.
I am sure in my code that the wrong part is at converting 10 bit to 16 bit part.
In my case i have a data of 37120 bits namely 4640 bytes.
This length data contain 3712 "10 bit" pixel. I have to read each 10 bit pixel value and right it to a file.
In order to achive to write i have to convert it to int16 value. Beacuse .net does not support write 10 bit data.

I have tried the code you supplied but could not obtain the satellite image correctly.

Cheers,
Ozgur
0
 
_Katka_Commented:
actualValue += 1 << (9 - (a % 10));

this line will actually add the value of specific bit in order to get 16-bit value, thus:

a = 0 : 1 << (9 - (0 % 10)) : 1 << (9 - 0) : 1 << 9 : 512
a = 1 : 1 << (9 - (1 % 10)) : 1 << (9 - 1) : 1 << 8 : 256
a = 2 : 1 << (9 - (2 % 10)) : 1 << (9 - 2) : 1 << 7 : 128
a = 3 : 1 << (9 - (3 % 10)) : 1 << (9 - 3) : 1 << 6 : 64
..
a = 10 : 1 << (9 - (10 % 10)) : 1 << (9 - 0) : 1 << 9 : 512
a = 11 : 1 << (9 - (11 % 10)) : 1 << (9 - 1) : 1 << 8 : 256

and so on..

also would you mind to send me the picture.. ? I'm unable to determine if
the algo fits your needs and the image is displayed correctly..

regards,
Kate
0
 
obeserAuthor Commented:
it is 13 mb.

How can i send it to you
0
 
obeserAuthor Commented:
if you can give an upload adress i can upload it
0
 
_Katka_Commented:
Just send it by mail :)

drahokoupilu@telpro.cz

regards,
Kate
0
 
obeserAuthor Commented:
Katka many many thanks. I have done it by another way.
Cheers,
Ozgur
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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