7 #include <iterator> // std::ostream_iterator
10 #include <iostream> // std::cout, std::fixed
21 typedef unsigned char byte;
27 return a - floor(a/n)*n;
30 inline int positive_modulo(int i, int n) {
31 return (i % n + n) % n;
34 void Bezout(int rkm2,int rkm1,int *res,int ukm1=0,int vkm1=1,int ukm2=1,int vkm2=0){
35 /*r(k-4)=r(k-3)*q(k-2)+r(k-2) <=> r(k-2)=a*u(k-2)+b*v(k-2)
38 r(k-3)=r(k-2)*q(k-1)+r(k-1) <=> r(k-1)=a*u(k-1)+b*v(k-1)
40 r(k-2)=r(k-1)*q(k)+r(k) <=> r(k)=a*u(k)+b*v(k) avec u(k)=u(k-2)-qk*u(k-1) et v(k)=v(k-2)-qk*v(k-1)*/
43 int qk=(rkm2-rk)/rkm1;
45 cout<<rkm1<<" = a*"<<ukm1<<"+b*"<<vkm1<<std::endl;
49 Bezout(rkm1,rk,res,ukm2-qk*ukm1,vkm2-qk*vkm1,ukm1,vkm1);
54 void saveFile(Row<byte> data, const char *fileName,long size_file=0) {
55 byte* vec=(byte*)data.memptr();
56 // cout<<"vec "<<d2.size()<<endl;
57 FILE* pFile = fopen (fileName, "wb");
59 size_file=data.size();
61 fwrite (vec , sizeof(byte), size_file, pFile);
65 /* vector<uint8_t> v =conv_to< vector<uint8_t> >::from(data);
66 ofstream outfile(fileName, ios::out | ofstream::binary);
68 copy(v.begin(), v.end(), ostream_iterator<uint8_t>(outfile));
74 Row<byte> readFile( const char *fileName) {
75 /*ifstream stream(fileName, ios::in | ios::binary | ios::ate);
76 int sizeFile=stream.tellg();
77 stream.seekg(0, ios::beg);
78 vector<uint8_t> vec((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
83 FILE* pFile = fopen (fileName, "rb");
84 fseek(pFile, 0L, SEEK_END);
85 int sz = ftell(pFile);
87 byte* vec=new byte[sz];
88 fread (vec , sizeof(byte), sz, pFile);
96 cout<<"vec ici"<<d2.size()<<endl;
103 convert_vec256_to_vec250(vector<uint8_t> vec256){
106 int num_val_greater = std::count_if(vec256.begin(), vec256.end(), [](int i){return i>=mm;});
107 vector<uint8_t> vec250;
108 vec250.reserve(vec256.size()+num_val_greater);
109 // cout<<"ICI "<<vec250.size()<<" "<<vec256.size()+num_val_greater<<endl;
112 for (int i=0; i<vec256.size(); i++) {
113 if(vec256[i]>=mm-1) {
114 vec250.push_back(mm-1);
115 vec250.push_back(vec256[i]-mm+1);
116 //vec250[l++]=(mm-1);
118 //vec250[l++]=vec256[i]-mm+1;
121 vec250.push_back(vec256[i]);
122 //vec250[l++]=vec256[i];
125 //cout<<"ICI "<<vec250.size()<<endl;
132 convert_vec250_to_vec256(vector<uint8_t> vec250){
135 vector<uint8_t> vec256;
136 vec256.reserve(vec250.size());
138 for (int i=0; i<vec250.size(); i++) {
139 if(vec250[i]==mm-1) {
140 vec256.push_back(vec250[++i]+mm-1);
143 vec256.push_back(vec250[i]);
147 // cout<<"ICI 2 "<<vec256.size()<<endl;
155 Mat<byte> readFullFile(int n, int k, long& sizeFile, int &lc) {
157 // ifstream stream("lena.png", ios::in | ios::binary | ios::ate);
158 ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate);
159 // ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate);
161 sizeFile=stream.tellg();
162 cout<<sizeFile<<endl;
163 stream.seekg(0, ios::beg);
167 //Used to convert the size (in long) in 8 bytes
171 long tmpSize=sizeFile;
172 for(int i=0;i<8;i++) {
174 // cout<<(int)t<<endl;
179 //we rebuild the size in order to check that the routine is correct
180 cout<<"rebuild"<<endl;
182 for(int i=8-1;i>=0;i--) {
187 cout<<(long)res<<endl;
191 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
192 cout<<"res contents "<<contents.size()<<endl;
193 cout<<"add size of file"<<endl;
194 // for(int i=0;i<8;i++)
195 // contents.insert(i,mysize[i]);
196 contents.insert (contents.begin(), mysize, mysize+8);
198 cout<<"start of the vector"<<endl;
200 cout<<(int)contents[i]<<" ";
205 cout<<"res contents "<<contents.size()<<endl;
206 vector<uint8_t> contents2=convert_vec256_to_vec250(contents);
207 cout<<"res contents2 "<<contents2.size()<<endl;
213 for (auto i = contents2.begin(); i != contents2.end(); ++i)
214 std::cout << (int)*i << ' ';
218 // vector<uint8_t> contents3=convert_vec250_to_vec256(contents2);
220 /*cout<<"LAST"<<endl;
221 for (auto i = contents3.begin(); i != contents3.end(); ++i)
222 cout << (int)*i << ' ';
226 if (std::equal(contents.begin(), contents.begin() + contents.size(), contents3.begin()))
227 cout << "success" << endl;
230 lc=ceil(contents2.size()/k);
231 cout<<"size lc "<<lc<<endl;
232 Mat<byte> matData(&contents2[0],1,contents2.size());
235 /* Row<byte> test(&contents[0],contents.size());
236 cout<<"test "<<size(test)<<endl;
237 saveFile(test, "lena3.png");
240 cout << "file size: " << contents2.size() << endl;
242 matData.reshape(k,lc);
243 cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
250 int main( int argc, char *argv[] ) {
268 S=readFullFile(n,k,sizeFile,lc);
272 arma_rng::set_seed(time(NULL));
273 //S=randi<Mat<byte>>(k,100000000);
275 cout<<"S "<<size(S)<<endl;
278 Mat<byte> G= randi<Mat<byte>>(n,k);
279 cout<<"G "<<size(G)<<endl;
282 Mat<int> C=conv_to<Mat<int>>::from(G)*conv_to<Mat<int>>::from(S);
284 cout<<"C "<<size(C)<<endl;
286 Mat<byte> C2=conv_to<Mat<byte>>::from(C);
290 for(int i=0;i<n;i++) {
292 ss <<"lena_"<<i<<".png";
293 string str = ss.str();
294 saveFile(C2.row(i), str.c_str());
304 //read k files among n
307 // for(int i=k-1;i>=0;i--) {
308 for(int i=0+off;i<k+off;i++) {
310 ss <<"lena_"<<i<<".png";
311 string str = ss.str();
313 Row<byte> d2=readFile(str.c_str());
314 C3.insert_rows(l,d2);
322 // Mat<int> Cs=C.rows(0,k-1);
323 Mat<int> Cs=conv_to<Mat<int>>::from(C3);
325 cout<<size(Cs)<<" "<<size(C3)<<endl;
326 cout<<"S "<<" "<<S<<endl;
330 Mat<int> Gs2=conv_to<Mat<int>>::from(G).rows(0,k-1);
331 mat Gs=conv_to<mat>::from(Gs2);
336 double determinant2=det(Gs);
337 int determinant=remainder(determinant2,mm);
338 determinant=positive_modulo(determinant,mm);
341 Bezout(determinant,mm,&r);
344 cout<<determinant<<" "<<mm<<" "<<r<<endl;
346 mat Gsi=round(inv(Gs)*det(Gs)*r);
352 mat SS2=mod(temp,mm);
353 Mat<byte> S2=conv_to<Mat<byte>>::from(SS2);
355 cout<<"S2 "<<S2<<endl;
357 cout<<max(S2-S,1)<<endl;
362 S2.reshape(1,S2.n_rows*S2.n_cols);
365 vector<uint8_t> res =conv_to< vector<uint8_t> >::from(S2.row(0));
366 cout<<"res size "<<res.size()<<endl;
368 cout<<"start of file"<<endl;
370 cout<<(int)res[i]<<" ";
374 vector<uint8_t> res2=convert_vec250_to_vec256(res);
375 cout<<"res2 size "<<res2.size()<<endl;
377 cout<<"get size"<<endl;
380 for(int i=0;i<8;i++) {
381 mysize[i]=res2.front();
382 res2.erase(res2.begin());
386 cout<<(int)mysize[i]<<endl;
388 cout<<"rebuild size"<<endl;
390 for(int i=8-1;i>=0;i--) {
392 size_file+=mysize[i];
395 cout<<"size file "<<(long)size_file<<endl;
396 saveFile(res2, "lena2.png",size_file);