No Description

steganography.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /// \file
  2. #include "steganography.h"
  3. #include <QDebug>
  4. Steganography::Steganography()
  5. {
  6. }
  7. int cbtoi(char c){
  8. if(c == '0') return 0;
  9. else return 1 ;
  10. }
  11. string decToBin(int num, int pad)
  12. {
  13. int r = 0; // residue
  14. // residuo
  15. int q = -1; // quotient
  16. // cociente
  17. string b = ""; // string to containt the result
  18. // cadena de caracteres para contener el resultado
  19. while(q != 0)
  20. {
  21. r = num%2;
  22. q = num/2;
  23. num = q;
  24. if (r) b.insert(0, "1");
  25. else b.insert(0, "0");
  26. }
  27. // if letter has less than pad bits, insert 0s at the beginning until length = pad
  28. // si letter tiene menos que pad bits, insertar 0s al principio hasta que el
  29. // length = pad
  30. while(b.length() < (unsigned long) pad)
  31. b.insert(0, "0");
  32. return b;
  33. }
  34. string messageToBinaryString(string str){
  35. string binStr = "";
  36. int temp;
  37. //converting message to binary string
  38. //convirtiendo un mensaje a una cadena de caracteres binarios
  39. for(unsigned int i = 0; i<str.length(); i++)
  40. {
  41. temp = static_cast<int>(str[i]);
  42. binStr.append(decToBin(temp, bitsPerLetter));
  43. }
  44. return binStr ;
  45. }
  46. char binStringToChar(string str){
  47. int value = 0 ;
  48. for(unsigned int i = 0; i < str.length(); i++){
  49. value = value << 1 ;
  50. if(str[i] == '1') value = value | 1 ;
  51. }
  52. return static_cast<char>(value) ;
  53. }
  54. int createMask(int lbits){
  55. return (1 << lbits) - 1;
  56. }
  57. void EmbbedMessage(const QImage & origImage, QImage & newImage, string msg){
  58. string binMsg ; // To store the message in binary.
  59. // Para almacenar el mensaje en binario.
  60. unsigned long msg_index = 0 ; // Will be used to pass through binStr
  61. // Sera utilizado para pasar atraves de binStr
  62. int red, green, blue ; // Will store the integer representation of the colors.
  63. // Va a guardar la representacion entera de los colores
  64. int mask = createMask(1);
  65. QRgb p;
  66. binMsg = messageToBinaryString(msg);
  67. //~English
  68. // We will include this postfix, so that the decoder will
  69. // know when the embedded message ends. It consists of 1 char
  70. // whose ASCII is 0.
  71. //~Spanish
  72. // Vamos a incluir este sufijo, tal que el decodificador sepa
  73. // cuando el mensaje embedido termina. Este consiste de 1 caracter
  74. // cuyo ASCII es 0.
  75. binMsg.append(2*bitsPerLetter,'0');
  76. //for each pixel in image
  77. //por cada pixel.
  78. for(int i = 0; i < origImage.width(); i++)
  79. {
  80. for(int j = 0; j<origImage.height(); j++)
  81. {
  82. p = origImage.pixel(i,j);
  83. red = qRed(p);
  84. if(msg_index < binMsg.length()){
  85. // clear the least significant bit of the red value
  86. // limpia el bit menos significativo del valor rojo
  87. red &= ~mask;
  88. // embed the corresponding bits from the mssage
  89. // empotra el bit correspondiente del mensaje
  90. red = red | cbtoi(binMsg[msg_index]) ;
  91. msg_index += 1 ;
  92. }
  93. green = qGreen(p);
  94. if(msg_index < binMsg.length()){
  95. green &= ~mask;
  96. green = green | cbtoi(binMsg[msg_index]);
  97. msg_index += 1 ;
  98. }
  99. blue = qBlue(p);
  100. if(msg_index < binMsg.length()){
  101. blue &= ~mask;
  102. blue = blue | cbtoi(binMsg[msg_index]);
  103. msg_index += 1 ;
  104. }
  105. else{
  106. // When the message is over we stop the loops
  107. // Cuando el mensaje termina paramos los loops
  108. i = origImage.width() ;
  109. break ;
  110. }
  111. newImage.setPixel(i,j,qRgb(red, green, blue));
  112. }
  113. }
  114. }
  115. string binaryStringToMessage(string binStr){
  116. // YOUR CODE HERE
  117. }
  118. string ExtractMessage(const QImage &stegoImage){
  119. // YOUR CODE HERE
  120. return "Este es el mensaje cuando aun no has implementado la funcion";
  121. }