Solved

Read 10 bit data by c#

Posted on 2006-06-10
14
462 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
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
 
LVL 10

Accepted Solution

by:
_Katka_ earned 500 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

9 Experts available now in Live!

Get 1:1 Help Now