Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. public enum State
  2. {
  3. Hiding,
  4. FillingWithZeros
  5. };
  6.  
  7. public static Bitmap EmbedText(string text, Bitmap bmp)
  8. {
  9. // save to png, don't save to lossy image formats like jpeg
  10. State state = State.Hiding;
  11. int charIndex = 0;
  12. int charValue = 0;
  13. long pixelElementIndex = 0;
  14. int zeros = 0;
  15. int R = 0, G = 0, B = 0;
  16. for (int i = 0; i < bmp.Height; i++)
  17. {
  18. for (int j = 0; j < bmp.Width; j++)
  19. {
  20. Color pixel = bmp.GetPixel(j, i);
  21. // clear the least significant bit (LSB) from each pixel element
  22. R = pixel.R - pixel.R % 2;
  23. G = pixel.G - pixel.G % 2;
  24. B = pixel.B - pixel.B % 2;
  25. for (int n = 0; n < 3; n++)
  26. {
  27. if (pixelElementIndex % 8 == 0)
  28. {
  29. // we can say that it's finished when 8 zeros are added
  30. if (state == State.FillingWithZeros && zeros == 8)
  31. {
  32. if ((pixelElementIndex - 1) % 3 < 2)
  33. {
  34. bmp.SetPixel(j, i, Color.FromArgb(R, G, B));
  35. }
  36. return bmp;
  37. }
  38. if (charIndex >= text.Length)
  39. {
  40. state = State.FillingWithZeros;
  41. }
  42. else
  43. {
  44. charValue = text[charIndex++];
  45. }
  46. }
  47. if (pixelElementIndex % 3 == 0)
  48. {
  49. {
  50. if (state == State.Hiding)
  51. {
  52. R += charValue % 2;
  53. charValue /= 2;
  54. }
  55. }
  56. }
  57. else if (pixelElementIndex % 3 == 1)
  58. {
  59. {
  60. if (state == State.Hiding)
  61. {
  62. G += charValue % 2;
  63. charValue /= 2;
  64. }
  65. }
  66. }
  67. else if (pixelElementIndex % 3 == 2)
  68. {
  69. {
  70. if (state == State.Hiding)
  71. {
  72. B += charValue % 2;
  73. charValue /= 2;
  74. }
  75. bmp.SetPixel(j, i, Color.FromArgb(R, G, B));
  76. }
  77. }
  78. pixelElementIndex++;
  79. if (state == State.FillingWithZeros)
  80. {
  81. zeros++;
  82. }
  83. }
  84. }
  85. }
  86. return bmp;
  87. }
  88.  
  89. public static string ExtractText(Bitmap bmp)
  90. {
  91. int colorUnitIndex = 0;
  92. int charValue = 0;
  93. string extractedText = String.Empty;
  94. for (int i = 0; i < bmp.Height; i++)
  95. {
  96. for (int j = 0; j < bmp.Width; j++)
  97. {
  98. Color pixel = bmp.GetPixel(j, i);
  99. for (int n = 0; n < 3; n++)
  100. {
  101. if (colorUnitIndex % 3 == 0)
  102. {
  103. {
  104. charValue = charValue * 2 + pixel.R % 2;
  105. }
  106. }
  107. else if (colorUnitIndex % 3 == 1)
  108. {
  109. {
  110. charValue = charValue * 2 + pixel.G % 2;
  111. }
  112. }
  113. else if (colorUnitIndex % 3 == 2)
  114. {
  115. {
  116. charValue = charValue * 2 + pixel.B % 2;
  117. }
  118. }
  119. colorUnitIndex++;
  120. if (colorUnitIndex % 8 == 0)
  121. {
  122. charValue = ReverseBits(charValue);
  123.  
  124. // can only be 0 if it is the stop character (the 8 zeros)
  125. if (charValue == 0)
  126. {
  127. return extractedText;
  128. }
  129. char c = (char)charValue;
  130. extractedText += c.ToString();
  131. }
  132. }
  133. }
  134. }
  135. return extractedText;
  136. }
  137.  
  138. private static int ReverseBits(int n)
  139. {
  140. int result = 0;
  141. for (int i = 0; i < 8; i++)
  142. {
  143. result = result * 2 + n % 2;
  144. n /= 2;
  145. }
  146. return result;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement