1 // g++ -std=c++11 -O4 -msse2 -msse3 -msse4 -fopenmp -O3 test_mat2.cpp -o test_mat2 -I /home/couturie/tools/armadillo-6.200.5/include/ -lc -lm -lpthread -lgfortran -DMAX_STACK_ALLOC=2048 -Wall -m64 -DF_INTERFACE_GFORT -fPIC -DSMP_SERVER -DNO_WARMUP -DMAX_CPU_NUMBER=8 -DNO_AFFINITY -UCOMPLEX -DDOUBLE -I/home/couturie/tools/openblas/include -I/home/couturie/Downloads/OpenBLAS-0.2.15/ /home/couturie/tools/openblas/lib/libopenblas_haswellp-r0.2.15.a
10 #include <iterator> // std::ostream_iterator
13 #include <iostream> // std::cout, std::fixed
24 typedef unsigned char byte;
30 return a - floor(a/n)*n;
33 inline int positive_modulo(int i, int n) {
34 return (i % n + n) % n;
37 void Bezout(int rkm2,int rkm1,int *res,int ukm1=0,int vkm1=1,int ukm2=1,int vkm2=0){
38 /*r(k-4)=r(k-3)*q(k-2)+r(k-2) <=> r(k-2)=a*u(k-2)+b*v(k-2)
41 r(k-3)=r(k-2)*q(k-1)+r(k-1) <=> r(k-1)=a*u(k-1)+b*v(k-1)
43 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)*/
46 int qk=(rkm2-rk)/rkm1;
48 cout<<rkm1<<" = a*"<<ukm1<<"+b*"<<vkm1<<std::endl;
52 Bezout(rkm1,rk,res,ukm2-qk*ukm1,vkm2-qk*vkm1,ukm1,vkm1);
57 void saveFile(Row<byte> data, const char *fileName,long size_file=0) {
58 byte* vec=(byte*)data.memptr();
59 // cout<<"vec "<<d2.size()<<endl;
60 FILE* pFile = fopen (fileName, "wb");
62 size_file=data.size();
64 fwrite (vec , sizeof(byte), size_file, pFile);
68 /* vector<uint8_t> v =conv_to< vector<uint8_t> >::from(data);
69 ofstream outfile(fileName, ios::out | ofstream::binary);
71 copy(v.begin(), v.end(), ostream_iterator<uint8_t>(outfile));
77 Row<byte> readFile( const char *fileName) {
78 /*ifstream stream(fileName, ios::in | ios::binary | ios::ate);
79 int sizeFile=stream.tellg();
80 stream.seekg(0, ios::beg);
81 vector<uint8_t> vec((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
86 FILE* pFile = fopen (fileName, "rb");
87 fseek(pFile, 0L, SEEK_END);
88 int sz = ftell(pFile);
90 byte* vec=new byte[sz];
91 fread (vec , sizeof(byte), sz, pFile);
99 cout<<"vec ici"<<d2.size()<<endl;
106 convert_vec256_to_vec250(vector<uint8_t> vec256){
109 int num_val_greater = std::count_if(vec256.begin(), vec256.end(), [](int i){return i>=mm;});
110 vector<uint8_t> vec250;
111 vec250.reserve(vec256.size()+num_val_greater);
112 // cout<<"ICI "<<vec250.size()<<" "<<vec256.size()+num_val_greater<<endl;
115 for (int i=0; i<vec256.size(); i++) {
116 if(vec256[i]>=mm-1) {
117 vec250.push_back(mm-1);
118 vec250.push_back(vec256[i]-mm+1);
119 //vec250[l++]=(mm-1);
121 //vec250[l++]=vec256[i]-mm+1;
124 vec250.push_back(vec256[i]);
125 //vec250[l++]=vec256[i];
128 //cout<<"ICI "<<vec250.size()<<endl;
135 convert_vec250_to_vec256(vector<uint8_t> vec250){
138 vector<uint8_t> vec256;
139 vec256.reserve(vec250.size());
141 for (int i=0; i<vec250.size(); i++) {
142 if(vec250[i]==mm-1) {
143 vec256.push_back(vec250[++i]+mm-1);
146 vec256.push_back(vec250[i]);
150 // cout<<"ICI 2 "<<vec256.size()<<endl;
158 Mat<byte> readFullFile(int n, int k, long& sizeFile, int &lc) {
160 // ifstream stream("lena.png", ios::in | ios::binary | ios::ate);
161 ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate);
162 // ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate);
163 sizeFile=stream.tellg();
164 cout<<sizeFile<<endl;
165 stream.seekg(0, ios::beg);
171 long tmpSize=sizeFile;
172 for(int i=0;i<8;i++) {
174 // cout<<(int)t<<endl;
179 cout<<"rebuild"<<endl;
181 for(int i=8-1;i>=0;i--) {
186 cout<<(long)res<<endl;
190 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
191 cout<<"res contents "<<contents.size()<<endl;
192 cout<<"add size of file"<<endl;
193 // for(int i=0;i<8;i++)
194 // contents.insert(i,mysize[i]);
195 contents.insert (contents.begin(), mysize, mysize+8);
196 cout<<"res contents "<<contents.size()<<endl;
197 vector<uint8_t> contents2=convert_vec256_to_vec250(contents);
198 cout<<"res contents2 "<<contents2.size()<<endl;
204 for (auto i = contents2.begin(); i != contents2.end(); ++i)
205 std::cout << (int)*i << ' ';
209 // vector<uint8_t> contents3=convert_vec250_to_vec256(contents2);
211 /*cout<<"LAST"<<endl;
212 for (auto i = contents3.begin(); i != contents3.end(); ++i)
213 cout << (int)*i << ' ';
217 if (std::equal(contents.begin(), contents.begin() + contents.size(), contents3.begin()))
218 cout << "success" << endl;
221 lc=ceil(contents2.size()/k);
223 Mat<byte> matData(&contents2[0],1,contents2.size());
226 /* Row<byte> test(&contents[0],contents.size());
227 cout<<"test "<<size(test)<<endl;
228 saveFile(test, "lena3.png");
231 cout << "file size: " << contents2.size() << endl;
233 matData.reshape(k,lc);
234 cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
241 int main( int argc, char *argv[] ) {
262 S=readFullFile(n,k,sizeFile,lc);
266 arma_rng::set_seed(time(NULL));
267 //S=randi<Mat<byte>>(k,100000000);
269 cout<<"S "<<size(S)<<endl;
272 Mat<byte> G= randi<Mat<byte>>(n,k);
273 cout<<"G "<<size(G)<<endl;
276 Mat<int> C=conv_to<Mat<int>>::from(G)*conv_to<Mat<int>>::from(S);
278 cout<<"C "<<size(C)<<endl;
280 Mat<byte> C2=conv_to<Mat<byte>>::from(C);
284 for(int i=0;i<n;i++) {
286 ss <<"lena_"<<i<<".png";
287 string str = ss.str();
288 saveFile(C2.row(i), str.c_str());
296 //read k files among n
297 for(int i=0;i<k;i++) {
299 ss <<"lena_"<<i<<".png";
300 string str = ss.str();
302 Row<byte> d2=readFile(str.c_str());
303 C3.insert_rows(i,d2);
310 // Mat<int> Cs=C.rows(0,k-1);
311 Mat<int> Cs=conv_to<Mat<int>>::from(C3);
313 cout<<size(Cs)<<" "<<size(C3)<<endl;
317 Mat<int> Gs2=conv_to<Mat<int>>::from(G).rows(0,k-1);
318 mat Gs=conv_to<mat>::from(Gs2);
323 double determinant2=det(Gs);
324 int determinant=remainder(determinant2,mm);
325 determinant=positive_modulo(determinant,mm);
328 Bezout(determinant,mm,&r);
331 cout<<determinant<<" "<<mm<<" "<<r<<endl;
333 mat Gsi=round(inv(Gs)*det(Gs)*r);
340 mat SS2=mod(temp,mm);
341 Mat<byte> S2=conv_to<Mat<byte>>::from(SS2);
344 // cout<<"max"<<endl;
345 // cout<<max(S2-S,1)<<endl;
350 S2.reshape(1,S2.n_rows*S2.n_cols);
353 vector<uint8_t> res =conv_to< vector<uint8_t> >::from(S2.row(0));
354 cout<<"res size "<<res.size()<<endl;
355 vector<uint8_t> res2=convert_vec250_to_vec256(res);
356 cout<<"res2 size "<<res2.size()<<endl;
358 cout<<"get size"<<endl;
361 for(int i=0;i<8;i++) {
362 mysize[i]=res2.front();
363 res2.erase(res2.begin());
367 cout<<(int)mysize[i]<<endl;
369 cout<<"rebuild size"<<endl;
371 for(int i=8-1;i>=0;i--) {
373 size_file+=mysize[i];
376 cout<<"size file "<<(long)size_file<<endl;
377 saveFile(res2, "lena2.png",size_file);