2


In the world of PC's, a byte usually denotes a memory boundary alignment of 8 bits which the computer treats as a single unit. In mini and mainframe computers, longer sequences like 16 and 32 bits (referred to as full words and double words respectively) are also possible.

But in the remote sensing world, the sensors/detectors are used to measure changes in the intensity of radiation from the atmosphere and the Earth by using their own radiometric resolutions such as 6-bit, 10-bit, and 12-bit. Therefore, the remotely sensed raw data image captured by the ground station are always packed in the form of 6-bit, 10-bit, or 12-bit BCD streams. These data are stored scanline by scanline.

My job is to read this kind of scanline one by one form an opend binary file, and stored to an unsigned char source buffer for a while. For 6-bit stream, I must scale up to 8-bit (one byte), and for 10-bit or 12-bit stream, I will make them to be scaled unsigned short integer. Finally, I should get an unsigned char (for 6-bit) or unsigned short destination buffer.

Would you please show me how to do that? C/C++ code demo will be highly appreciated! Thank you.

10101010 01110101 00011001
|         |
-----------
 10-bit BCD

10101010 01110101 00011001
      |         |
      -----------
       10-bit BCD

10101010 01110101 01100110
       |          |
       ------------
         10-bit BCD
GoldenLee
  • 737
  • 2
  • 13
  • 28

1 Answers1

2

The usual way is to left shift by the difference in the number of bits between the input and the output, then add in the high order bits from the input to fill out the lower bits.

unsigned char eightbit = (sixbit << 2) | (sixbit >> 4);
unsigned short sixteenbit = (tenbit << 6) | (tenbit >> 4);
unsigned short sixteenbit = (twelvebit << 4) | (twelvebit >> 8);

There's an alternate approach for the lower bits that I haven't seen very often - fill them with noise. This masks some of the quantization error in the original sample.

unsigned char eightbit = (sixbit << 2) | (sixteenbitnoise >> 14);
unsigned short sixteenbit = (tenbit << 6) | (sixteenbitnoise >> 10);
unsigned short sixteenbit = (twelvebit << 4) | (sixteenbitnoise >> 12);
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Dear Mark, Thank you for your answer. How to extract one 6-bit/10bit/12bit from an unsigned char array? Let's take 10-bit split for example, apparently, 10-bit BCD can alternative span 2 bytes or 3 bytes. – GoldenLee Jun 17 '11 at 06:58