Solved

Read 10 bit data by c#

Posted on 2006-06-10
14
468 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Reference issues in Visual Studio 3 41
Release Dynamically Allocated Memory in C# 3 38
"Emulate" TAB key when press Enter Key 3 49
Input parameteres to DragOver 2 20
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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