1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include <stdlib.h>
#include <stdio.h>
#include <cv.h> // open cv general include file
#include <highgui.h> // open cv GUI include file
#include <iostream> // standard C++ I/O
#include <vector>
using namespace cv; // OpenCV API is in the C++ "cv" namespace
using namespace std;
unsigned long hashe (unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
{
hash = ((hash << 5) + hash) + c; // hash * 33 + c
}
return hash;
}
int main( int argc, char** argv )
{
Mat grayscale, encoded_swap, encoded_message, decoded_message; // image object
int key, flag=0, n=0, swap, l=0, k=0;
bool keepProcessing = true;;
vector<int> params; // file saving compression parameters
const string windowName1 = "decoded_message"; // window name
//const string windowName2 = "decoded_message"; // window name
unsigned char * password = new unsigned char[128] ;
vector <Point2i> pl;
cout << "Enter your password.\n";
cin >> password;
if ((argc == 4) && !(grayscale= imread (argv[1], CV_LOAD_IMAGE_GRAYSCALE)).empty() && !(encoded_swap= imread (argv[2], CV_LOAD_IMAGE_GRAYSCALE)).empty() )
{
if ( (grayscale.rows == encoded_swap.rows) && (grayscale.cols == encoded_swap.cols))
{
cout << "The two images are the same size.\n";
int hv = hashe (password);
RNG r(hv);
int row = encoded_swap.rows;
int col = encoded_swap.cols;
int n = row*col;
vector<int> swap_tab;
vector<int> encoded_message_pixels;
Mat encoded_s = encoded_swap.clone();
Mat grays = grayscale.clone();
encoded_message = encoded_s - grays;
decoded_message = encoded_message.clone();
namedWindow(windowName1, 0);
// we initialise the decoded_message
for (int i=0; i<row; i++)
{
for (int j=0; j<col; j++)
{
decoded_message.at<uchar>(i,j) = 0;
}
}
// we get the encoded message
for (int i=0; i<row; i++)
{
for (int j=0; j<col; j++)
{
if(encoded_message.at<uchar>(i,j) == 1)
{
encoded_message.at<uchar>(i,j) = 0;
}
else
{
encoded_message.at<uchar>(i,j) = 255;
}
}
}
// we get the list of swaping
for(int s=0; s<col*row; s++)
{
swap_tab.push_back(r(col*row));
}
// We write out the list of swaping
cout << "The 10 first swaping numbers are:\n";
for(int h=0; h<10; h++)
{
cout << swap_tab[h] << " ";
}
cout << "\n" ;
// we put the encoded message in an array for easier manipulation
for (int i=0; i<row; i++)
{
for (int j=0; j< col; j++)
{
encoded_message_pixels.push_back(encoded_message.at<uchar>(i,j));
}
}
// we write out the 100 first elements of the encoded_message_pixels before swaping back
cout << "The 100 first encoded message pixels before swaping back are:\n";
for (int i=0; i<100; i++)
{
cout << encoded_message_pixels[i] << " ";
}
cout << "\n" ;
// decoding of the message we swap the pixels backward the list! C'EST ICI QUE SA BUGUE: CE CODE NE FAIT STRICTEMENT RIEN, IL NE MODIFIE PAS LE CONTENU DE encoded_message_pixels
for (int h=col*row-1; h=0; h--)
{
int ss;
ss = encoded_message_pixels[swap_tab.at(h)];
encoded_message_pixels[swap_tab.at(h)] = encoded_message_pixels[h];
encoded_message_pixels[h] = ss;
}
//we write out the 100 first elements of the encoded_message_pixels after swaping back ET ICI ON OBTIENT LA MEME LISTE, QU'AVANT JE NE COMPRENDS RIEN, SUREMENT UNE ERREUR DANS L'INITIALISATION DU VECTEUR, JE SUIS DEBUTANT...
cout << "The 100 first encoded message pixels after swaping back are:\n";
for (int i=0; i<100; i++)
{
cout << encoded_message_pixels[i] << " ";
}
cout << "\n" ;
// we get the decoded message
for (int i=0; i<row; i++)
{
for (int j=0; j< col; j++)
{
decoded_message.at<uchar>(i,j) = encoded_message_pixels[l++];
}
}
// display the decoded message in window
imshow(windowName1, decoded_message );
//save out decoded message to file decoded_message.png
params.push_back(CV_IMWRITE_PNG_COMPRESSION);
params.push_back(95);
imwrite(argv[3], decoded_message, params);
while (keepProcessing)
{
// start event processing loop (very important, in fact essential for GUI)
key=waitKey(20);
// get any keyboard input given by the user and process it
if (key == 'x')
{
// if user presses "x" then exit
std::cout << "Keyboard exit requested : exiting now - bye!" << std::endl;
keepProcessing = false;
}
}
// all OK : main returns 0
return 0;
}
else
{
return -1;
}
}
// not OK : main returns -1
return -1;
} |
Partager