-1

I am playing around with colors in C# and I wanted to know how to get a color value from a 16-bit byte array. Here is a code bellow where I use a 32-bit byte array.

                var colorArray = new Color[b.Length/4];                
                for (var i = 0; i < b.Length; i += 4)
                {
                    var color = Color.FromArgb(b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
                    colorArray[i / 4] = color;
                }
  • What is the packing of your 16-bit format? Is it B5G6R5? or B5G5R5A1? or are you talking about B16G16R16? There's barely enough info in your question to attempt an answer. What part are you having trouble with? – Wyck Apr 14 '20 at 17:24
  • Have you read the Wiki article (https://en.wikipedia.org/wiki/BMP_file_format)? – jdweng Apr 14 '20 at 17:27
  • The format is rgb555 – TheBalanceHammer Apr 14 '20 at 17:35
  • So you need help accessing the bits? Do you know how to do bit manipulation? e.g.: `(data[0] >> 5) & 0x1F` Again, what part are you having trouble with? – Wyck Apr 14 '20 at 18:01
  • I suppose it is bit manipulation. I have a 16-bit byte array and I want to get a color value from 16-bits and store it in a color variable and then add that color variable to an array. – TheBalanceHammer Apr 14 '20 at 18:17
  • So I want to know hotw to get 16-bit rgb color values and add them to the color.FromArgb function. My bigger project is too read a 16-bit image and then use it's bits to draw a circle. And I am using a bitmap.setPiexl method to accomplish this task. And for that reason I need color values. Also, sorry I don't usually work with images or bitmaps. I just thought it would be a fun challenge I guess – TheBalanceHammer Apr 14 '20 at 18:24

1 Answers1

0

You basically shift the most significant bits of each field to the right place in its new format and mask off any missing bits to zero. Green is split across two bytes. This would be easier if the array were an array of 16 bit ints, but if it's in bytes, then the bit manipulation for a pair of bytes is roughly like this.

+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
|               byte1                 |             byte0             |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
| -  |          Red           |       Green       |       Blue        |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+

B8 = (byte0 << 3) & 0xF8;
G8 = ((byte1 << 6) & 0xC0) || ((byte0 >> 2) & 0x38);
R8 = (byte1 << 1) & 0xF8;

B8G8R8 = B8 | (G8 << 8) || (R8 << 16);
Wyck
  • 10,311
  • 6
  • 39
  • 60