No Description

steganography.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // We will include this postfix, so that the decoder will
  68. // know when the embedded message ends. It consists of 1 char
  69. // whose ASCII is 0.
  70. // Vamos a incluir este sufijo, tal que el decodificador sepa
  71. // cuando el mensaje embedido termina. Este consiste de 1 caracter
  72. // cuyo ASCII es 0.
  73. binMsg.append(2*bitsPerLetter,'0');
  74. //for each pixel in image
  75. //por cada pixel.
  76. for(int i = 0; i < origImage.width(); i++)
  77. {
  78. for(int j = 0; j<origImage.height(); j++)
  79. {
  80. p = origImage.pixel(i,j);
  81. red = qRed(p);
  82. if(msg_index < binMsg.length()){
  83. // clear the least significant bit of the red value
  84. // limpia el bit menos significativo del valor rojo
  85. red &= ~mask;
  86. // embed the corresponding bits from the mssage
  87. // empotra el bit correspondiente del mensaje
  88. red = red | cbtoi(binMsg[msg_index]) ;
  89. msg_index += 1 ;
  90. }
  91. green = qGreen(p);
  92. if(msg_index < binMsg.length()){
  93. green &= ~mask;
  94. green = green | cbtoi(binMsg[msg_index]);
  95. msg_index += 1 ;
  96. }
  97. blue = qBlue(p);
  98. if(msg_index < binMsg.length()){
  99. blue &= ~mask;
  100. blue = blue | cbtoi(binMsg[msg_index]);
  101. msg_index += 1 ;
  102. }
  103. else{
  104. // When the message is over we stop the loops
  105. // Cuando el mensaje termina paramos los loops
  106. i = origImage.width() ;
  107. break ;
  108. }
  109. newImage.setPixel(i,j,qRgb(red, green, blue));
  110. }
  111. }
  112. }
  113. string binaryStringToMessage(string binStr){
  114. // YOUR CODE HERE
  115. }
  116. string ExtractMessage(const QImage &stegoImage){
  117. // YOUR CODE HERE
  118. return "Este el mensaje cuando aun no has implementado la funcion";
  119. }