?
Solved

Read 10 bit data by c#

Posted on 2006-06-10
14
Medium Priority
?
473 Views
Last Modified: 2008-01-16
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
Comment
Question by:obeser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 5

Expert Comment

by:aaronfeng
ID: 16879229
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
 
LVL 2

Expert Comment

by:HeidarV
ID: 16879780
use System.Text.Encoding.BigEndianUnicode.GetChars()
0
 

Author Comment

by:obeser
ID: 16879965
It is a seviri native image. "use System.Text.Encoding.BigEndianUnicode.GetChars()" does not work for this case.
0
Independent Software Vendors: 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!

 
LVL 10

Accepted Solution

by:
_Katka_ earned 2000 total points
ID: 16881435
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
 

Author Comment

by:obeser
ID: 16890439
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
 
LVL 10

Expert Comment

by:_Katka_
ID: 16891779
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
 
LVL 10

Expert Comment

by:_Katka_
ID: 16892338
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
 

Author Comment

by:obeser
ID: 16896835
Would you please explain what this line do

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

cheers,
ozgur
0
 

Author Comment

by:obeser
ID: 16896910
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
 
LVL 10

Expert Comment

by:_Katka_
ID: 16901442
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
 

Author Comment

by:obeser
ID: 16901511
it is 13 mb.

How can i send it to you
0
 

Author Comment

by:obeser
ID: 16901557
if you can give an upload adress i can upload it
0
 
LVL 10

Expert Comment

by:_Katka_
ID: 16903066
Just send it by mail :)

drahokoupilu@telpro.cz

regards,
Kate
0
 

Author Comment

by:obeser
ID: 16904487
Katka many many thanks. I have done it by another way.
Cheers,
Ozgur
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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