achat levitra old
acheter levitra new
commander levitra mail
levitra belgique misc
levitra en france misc
levitra en ligne images
levitra generique pages
levitra moins cher the
levitra ordonnance mail
levitra pas cher mail
levitra pharmacie old
levitra prix wiki
levitra sans ordonnance all
medicament levitra pages


News Item: : Towards Steganography III: a class that manipulates RGB colors
(Category: Tech related)
Posted by yiangos
Wednesday 22 July 2009 - 01:14:00

In my two previous posts (here and here), I showed how to create two of the components needed for my steganography project. This post is about the part where we manipulate the least significant bits of the RGB components of a pixel.


This has two parts: A "getter" part, where we get the values of the least significant bits, and a "setter" part, where we change these bits so that they represent a particular number.

Each color component is actually a byte. So, getting the least significant bit of the R,G or B component of a pixel's color, is the same as getting the least significant bit of a byte. This is quite easy as an algorithm: simply grab the byte, and bitwise AND it with 1. It's pretty easy to see that a bitwise AND between 1 and any (positive) number results in 1 (if the number is odd) or 0 (if the number is even). In VB.NET, the result of a bitwise operation between two integers is an integer of the same type (in our case, a byte), so we need to further cast the result (1 or 0) to boolean. The function is as follows:
  1.    Protected Shared Function GetComponentLastBit(ByVal num As Byte) As Boolean
  2.         Return CBool(num And 1)
  3.     End Function


That's ok if we have the color component. However, we're more likely to have just the pixel's color. The following function comes in handy:
  1.    Protected Shared Function GetColorComponent(ByVal pixelColor As System.Drawing.Color, ByVal colorComponent As Integer) As Byte
  2.         Dim returnValue As Byte = 0
  3.         Select Case colorComponent
  4.             Case 0
  5.                 returnValue = pixelColor.R
  6.             Case 1
  7.                 returnValue = pixelColor.G
  8.             Case 2
  9.                 returnValue = pixelColor.B
  10.         End Select
  11.         Return returnValue
  12.     End Function


This simply gets a color, and returns one of its three components, according to the second parameter. For that parameter, 0 stands for Red, 1 stands for Green and 2 stands for Blue.

Putting things together, we finally get this function:
  1.    Public Shared Function GetNumFromColor(ByVal pixelColor As System.Drawing.Color) As Byte
  2.         Dim returnValue As Byte = 0
  3.         For i As Integer = 0 To 2
  4.             returnValue += CByte(GetComponentLastBit(GetColorComponent(pixelColor, i)) And 1) << i
  5.         Next
  6.         Return returnValue
  7.     End Function

A lot of things take place here. First of all, we get a color as a parameter. From the previous function, we've already mapped 0 to Red, 1 to Green and 2 to Blue. Therefore, we iterate over these three values, and so we're able to get the last bit of each color component by means of the GetComponentLastBit() method. This information is returned as a boolean. We cast the boolean as byte (a True value corresponds to 11111111, or 256, while a false value corresponds to 0) and grab its least significant bit (the "And 1" part). So now we have either 1 (for true) or 0 (for false). We need to place this value at the corresponding position of a 3-bit word. Since we've already chosen implicitly an order (0-Red, 1-Green, 2-Blue), we follow that for consistency, and assign the red bit to the least significant bit of the 3-bit word, the green bit to the middle one and the blue bit to the most significant bit od the 3-bit word. That part is taken care of by the left shift (<<). Adding the three values, we get a 3-bit word. In other words, we get a number from 0 to 7 inclusive.

Apparently, this function will also come in handy in the decryption process.

This sums up the "getter" part.

The "setter" part, has three equivalent functions. First, a function that toggles the last bit of a specific color component (i.e. of a byte):
  1.    Protected Shared Function ToggleLastBit(ByVal num As Byte) As Byte
  2.         If GetComponentLastBit(num) Then
  3.             Return num - 1
  4.         Else
  5.             Return num + 1
  6.         End If
  7.     End Function

Not much to note here. If the least significant bit (LSB) is 1, then the result of a toggle would be that the LSB becomes 0 and all other bits remain as they were. Changing the LSB from 1 to 0 without any other changes is the same as subtracting 1 from the original number. If the LSB is 0, then changing it to 1 without any other changes is the same as adding 1 to the original number. Since the LSB was 0, there will be no carry, so no other bit is affected and therefore an overflow can't occur either.

The previous function blindly toggles the LSB of any byte it receives as its parameter. To toggle on demand, we use the following function
  1.    Protected Shared Function SetColorComponent(ByVal num As Byte, ByVal setFlag As Boolean) As Byte
  2.         If setFlag Then
  3.             Return ToggleLastBit(num)
  4.         Else
  5.             Return num
  6.         End If
  7.     End Function

All this function does, is toggle the LSB if the flag (the second argument) is True. Now, what we need, is a way to set or unset this flag.

The flag we just mentioned, when set, states that "this particular component doesn't have its last bit set to the desired value, and needs to be toggled". In other words, this flag should be set when the corresponding bits of the 3-bit word we get from the "getter" part of the original color are different to the 3-bit word we need to store. This is easy to know. If we use XOR between the 3-bit word of the original and the 3-bit word we need to store, then the bits of the result that are 1 are the ones that correspond to the components that need their LSB toggled. So, putting it all together:
  1.    Public Shared Function SetNumToColor(ByVal pixelColor As System.Drawing.Color, ByVal number As Byte) As System.Drawing.Color
  2.         Dim colorNumber As Byte = GetNumFromColor(pixelColor)
  3.         If number < 8 And number >= 0 Then
  4.             'Find which components need toggling
  5.             colorNumber = colorNumber Xor number
  6.             'Colornumber is now a number from 0 to 7, where the bits equal to 1
  7.             'represent the color components that need to toggle their last bit, with red in the
  8.             'least significant bit and blue in the most significant bit
  9.             'To get the Red bit, we rightshift 0 times and then bitwise AND this number with 1.
  10.             'To get the Green bit, we rightshift 1 times and then bitwise AND this number with 1.
  11.             'To get the Blue bit, we rightshift 2 times and then bitwise AND this number with 1.
  12.             Return System.Drawing.Color.FromArgb( _
  13.                     pixelColor.A, _
  14.                     SetColorComponent(pixelColor.R, CBool(colorNumber >> 0 And 1)), _
  15.                     SetColorComponent(pixelColor.G, CBool(colorNumber >> 1 And 1)), _
  16.                     SetColorComponent(pixelColor.B, CBool(colorNumber >> 2 And 1)) _
  17.                     )
  18.         Else
  19.             Return pixelColor
  20.         End If
  21.     End Function

One thing to note here, is that in the resulting color, we use the original alpha channel unchanged. That means that we can use semi-transparent images without really any issues. The transparency will carry through unaffected.

This is the end of the "setter" part. The attached zip contains these functions as parts of a class in a separate project.




This news item is from Midnight blogging
( http://yiannis.vavouranakis.gr/myblog/news.php?extend.42 )