# Bit shuffle Operators for making collor

• ### Question

• Hi I work on graduate  project , I have some function for drawing picture from some sensor,  I don t understand how function works, can someone help me with understanding them
``` Private Function RGB565To32ARGBV3(ByVal rgb16 As UShort) As Int32
Dim rgb32 As Integer
Dim tmp As UShort
rgb32 = &HFF
tmp = rgb16 >> 11 And &H1F
tmp = (tmp * &HFF) / &H1F
rgb32 = (rgb32 << 8) + (tmp And &HFF)
tmp = rgb16 >> 5 And &H3F
tmp = (tmp * &HFF) / &H3F
rgb32 = (rgb32 << 8) + (tmp And &HFF)
tmp = rgb16 And &H1F
tmp = (tmp * &HFF) / &H1F
rgb32 = (rgb32 << 8) + (tmp And &HFF)
Return rgb32
End Function

Private Function YCbCr2RGB_v2(ByVal YCbCr As Byte) As Color
Dim R, G, B As Integer
Dim Y, Cb, Cr As Byte
Dim result As Color
'R = Y + 1.403 * (Cr - 0.5)
'G = Y - 0.344 * (Cr - 0.5) - 0.714 * (Cb - 0.5)
'B = Y + 1.773 * (Cb - 0.5)
Y = YCbCr >> 4
Cb = (YCbCr >> 2) And &H2
Cr = YCbCr And &H2
R = CInt((Y + 1.1398373983739838 * Cr))
G = CInt((Y - 0.39465170435897035 * Cb - 0.58059860666749763 * Cr) + 32)
B = CInt((Y + 2.0321100917431192 * Cr) * 16)
'Debug.WriteLine(Y.ToString & ":" & Cb.ToString & ":" & Cr.ToString & " <--> " & R.ToString & ":" & G.ToString & ":" & B.ToString)
result = Color.FromArgb(R, G, B)
Return result
End Function

Private Function YCbCr2RGB_v3(ByVal YCbCr As Byte) As Color
Dim R, G, B As Integer
Dim Y, Cb, Cr As Byte
Dim result As Color
'R = Y + 1.403 * (Cr - 0.5)
'G = Y - 0.344 * (Cr - 0.5) - 0.714 * (Cb - 0.5)
'B = Y + 1.773 * (Cb - 0.5)
Y = YCbCr And &HF0
Cb = (YCbCr << 4) And &HC0
Cr = (YCbCr << 6) And &HC0

'Debug.WriteLine(Y.ToString & ":" & Cb.ToString & ":" & Cr.ToString & " <--> " & R.ToString & ":" & G.ToString & ":" & B.ToString)
result = Color.FromArgb(Y, Cb, Cr)
Return result
End Function```

Wednesday, December 13, 2017 9:34 PM

### All replies

• What have you done so far? What is it exactly you think the functions do? What do you want them to do?

We cant say much without knowing what your sensor is and what you do with it. What is the result? A scanned image?

Perhaps you use these functions to convert the sensor data to color data for a bitmap? Is that what you want to do?

Wednesday, December 13, 2017 11:11 PM
• Hi,

to understand the functions you need to understand the color-spaces used. Go wiki and read a bit (thats the best advice I can give ...)

[in the first method you convert a rgb565 color to a standard 32 bit rgb value, so you have all 16 bits of a short (0 to 4, 5 to 10, 11 to 15 (zero based) or 1 based: 1 to 5, 6 to 11, 12 to 16 for each r, g, b channel) where the old value is stored, therefore the shifts and tests]

Regards,

Thorsten

Wednesday, December 13, 2017 11:14 PM
• I don t understand how function works, can someone help me with understanding them

Break that code down so that each line of code is a single operation.  For instance:

tmp = (tmp * &HFF) / &H1F

becomes

tmp = tmp * &HFF
tmp = tmp / &H1FThen you can use the debugger to single step through the code watching the variable values, so you can see why those operations create the results that you need.

Thursday, December 14, 2017 12:12 AM
• I don t understand how function works, can someone help me with understanding them

Break that code down so that each line of code is a single operation.  For instance:

tmp = (tmp * &HFF) / &H1F

becomes

tmp = tmp * &HFF
tmp = tmp / &H1FThen you can use the debugger to single step through the code watching the variable values, so you can see why those operations create the results that you need.

Ok then what's the purpose of the bit shift operator? By that I mean - what affect does that have on the result?

"A problem well stated is a problem half solved.” - Charles F. Kettering

Thursday, December 14, 2017 12:17 AM
•  It is not clear to me what you want us to help you to understand.

Do you want help understanding how to use these 3 functions in your vb.net code?

Do you want help understanding how the bitwise operators work?

Do you want help understanding the color spaces?

Maybe these links will help a little if you want to understand the bitwise operators.

Understand how bitwise operators work (C# and VB.NET examples)

Bitwise Operations in VB.NET

Using Bitwise Operators In VB

Maybe this link will help with understanding the YCbCr color space...

Wikipedia - YCbCr

Maybe these will give a little helpful info on the RGB565 (16bit High Color) format...

Wikipedia - High color

If you say it can`t be done then i`ll try it

Thursday, December 14, 2017 12:49 AM
• Ok then what's the purpose of the bit shift operator? By that I mean - what affect does that have on the result?

The first one

tmp = rgb16 >> 11

returns the high-order 5 bits as an integer.   As the input format is RGB565, OP should be able to connect that value to the colour reported for that pixel. The relationship to the output value format of 32ARGB is a little more obscure, because the A value is not provided from the input, and it is necessary to note that there are three successive
rgb32 << 8

But if it is taken one step at a time the process should be apparent.

Thursday, December 14, 2017 1:06 AM
•  second function use this function for drawing bit by bit fingerprint image, sensor scan fingerprint image. Yes I want to convert sensor data for bitmap, but I want to change some in this code to get better image
Thursday, December 14, 2017 8:35 AM
• I understand how to use this function but I want to change them, to get better image , but I don t know the bitwise operators work, and how it use to create some color . Thanks a lot for advice

Best  Regards

Thursday, December 14, 2017 8:45 AM
•  second function use this function for drawing bit by bit fingerprint image, sensor scan fingerprint image. Yes I want to convert sensor data for bitmap, but I want to change some in this code to get better image

Depending on the source of the image and which conversion you are using, you could try adjusting the weighting values for the color/luminance calculations.  That may have an effect in highlighting contrasting parts of the image.  The simplest change would be to add some NumericUpDown controls, and use the values of those controls instead of the existing constants.    Small changes will change the color balance - larger changes will produce false colour images that might suit your purpose.

Thursday, December 14, 2017 8:50 AM
• I use this function for drawing image

```   Private Sub DrawImage()
Dim imgPtr As UInteger
fpBitmap = New Bitmap(320, 240)
imgPtr = 0
For pY As Integer = 0 To fpBitmap.Height - 1
For pX As Integer = 0 To fpBitmap.Width - 1
' fpBitmap.SetPixel(pX, pY, Color.FromArgb(RGB565To32ARGBV3(CUShort(FP_Control1.imgBuff(imgPtr)))))'
'fpBitmap.SetPixel(pX, pY, YCbCr2RGB(FP_Control1.imgBuff(imgPtr)))
fpBitmap.SetPixel(pX, pY, YCbCr2RGB_v3(FP_Control1.imgBuff(imgPtr)))
imgPtr += 1
Next
Next

btSave.Image = fpBitmap
End Sub```

Saturday, December 16, 2017 10:31 AM
• I use this function for drawing image

Were you able to observe the change in the image when you adjusted the hue/luminance weighting values in YCbCr2RGB_v3?   What was the change that produced the best effect for highlighting the details in the image?

Saturday, December 16, 2017 11:47 AM