void readWindowTest( uint64_t doff, const char* enc, size_t hbeg, size_t hend, size_t pre, size_t post, const std::vector<byte>& data, const std::vector<int32_t>& ecp, const std::vector<size_t>& eoff) { const LG_Window inner{doff + hbeg, doff + hend}; int32_t* chars = nullptr; size_t* offsets = nullptr; size_t clen; LG_Error* err = nullptr; const unsigned int abad = lg_read_window( reinterpret_cast<const char*>(data.data()), reinterpret_cast<const char*>(data.data()) + data.size(), doff, &inner, enc, pre, post, &chars, &offsets, &clen, &err ); if (err) { const std::string msg(err->Message); lg_free_error(err); throw std::runtime_error(msg); } std::unique_ptr<int32_t[],void(*)(int32_t*)> pchars( chars, &lg_free_window_characters ); std::unique_ptr<size_t[],void(*)(size_t*)> poff( offsets, &lg_free_window_offsets ); const size_t ebad = std::count_if( ecp.begin(), ecp.end()-1, [](int32_t v){ return v < 0; } ); SCOPE_ASSERT_EQUAL(ebad, abad); std::vector<int32_t> acp(chars, chars+clen); SCOPE_ASSERT_EQUAL(ecp, acp); std::vector<size_t> aoff(offsets, offsets+clen); SCOPE_ASSERT_EQUAL(eoff, aoff); }
WindowAckSizeMsgPtr RtmpParser::parseWindowAckSizeMsg(RtmpMsgHeaderPtr& mh) { WindowAckSizeMsgPtr acp(new WindowAckSizeMsg()); ReadBufferPtr readBuffer(new ReadBuffer(mh->length)); readBuffer->appendData(mh->body, mh->length); acp->windowAckSize = readBuffer->read<int32_t>(ReadBuffer::BIG); return acp; }
/* replaces the chars with special meaning with the associated data from the player info struct */ static char *generatetextfromlabel(widget *item) { char *text = malloc(MAX_LABELSIZE); char tmp[MAX_LABELSIZE]; if(!item) { free(text); return NULL; } strcpy(text, item->label); if(item->type == tySlabel) return text; stringreplace(text, "$1", "%.2i:%.2i:%.2i", guiInfo.ElapsedTime / 3600, (guiInfo.ElapsedTime / 60) % 60, guiInfo.ElapsedTime % 60); stringreplace(text, "$2", "%.4i:%.2i", guiInfo.ElapsedTime / 60, guiInfo.ElapsedTime % 60); stringreplace(text, "$3", "%.2i", guiInfo.ElapsedTime / 3600); stringreplace(text, "$4", "%.2i", (guiInfo.ElapsedTime / 60) % 60); stringreplace(text, "$5", "%.2i", guiInfo.ElapsedTime % 60); stringreplace(text, "$6", "%.2i:%.2i:%.2i", guiInfo.RunningTime / 3600, (guiInfo.RunningTime / 60) % 60, guiInfo.RunningTime % 60); stringreplace(text, "$7", "%.4i:%.2i", guiInfo.RunningTime / 60, guiInfo.RunningTime % 60); stringreplace(text, "$8", "%i:%.2i:%.2i", guiInfo.ElapsedTime / 3600, (guiInfo.ElapsedTime / 60) % 60, guiInfo.ElapsedTime % 60); stringreplace(text, "$v", "%3.2f%%", guiInfo.Volume); stringreplace(text, "$V", "%3.1f", guiInfo.Volume); stringreplace(text, "$U", "%3.0f", guiInfo.Volume); stringreplace(text, "$b", "%3.2f%%", guiInfo.Balance); stringreplace(text, "$B", "%3.1f", guiInfo.Balance); stringreplace(text, "$D", "%3.0f", guiInfo.Balance); stringreplace(text, "$t", "%.2i", guiInfo.Track); stringreplace(text, "$o", "%s", acp(TranslateFilename(0, tmp, sizeof(tmp)))); stringreplace(text, "$O", "%s", acp(TranslateFilename(4, tmp, sizeof(tmp)))); stringreplace(text, "$x", "%i", guiInfo.VideoWidth); stringreplace(text, "$y", "%i", guiInfo.VideoHeight); stringreplace(text, "$C", "%s", guiInfo.sh_video ? codecname : ""); stringreplace(text, "$$", "$"); if(guiInfo.Playing == GUI_STOP) { stringreplace(text, "$P", "s"); stringreplace(text, "$s", "s"); } else if(guiInfo.Playing == GUI_PLAY) { stringreplace(text, "$P", "p"); stringreplace(text, "$p", "p"); } else if(guiInfo.Playing == GUI_PAUSE) { stringreplace(text, "$P", "e"); stringreplace(text, "$e", "e"); } if(guiInfo.AudioChannels == 0) stringreplace(text, "$a", "n"); else if(guiInfo.AudioChannels == 1) stringreplace(text, "$a", "m"); else if(guiInfo.AudioChannels == 2) stringreplace(text, "$a", (guiInfo.AudioPassthrough ? "r" : "t")); else stringreplace(text, "$a", "r"); if(guiInfo.StreamType == STREAMTYPE_FILE) stringreplace(text, "$T", "f"); else if(guiInfo.StreamType == STREAMTYPE_DVD || guiInfo.StreamType == STREAMTYPE_DVDNAV) stringreplace(text, "$T", "d"); else if(guiInfo.StreamType == STREAMTYPE_STREAM) stringreplace(text, "$T", "u"); else stringreplace(text, "$T", " "); stringreplace(text, "$f", acp(TranslateFilename(1, tmp, sizeof(tmp)))); stringreplace(text, "$F", acp(TranslateFilename(2, tmp, sizeof(tmp)))); return text; }
void sa_corana::evolve(population &pop) const { // Let's store some useful variables. const problem::base &prob = pop.problem(); const problem::base::size_type D = prob.get_dimension(), prob_i_dimension = prob.get_i_dimension(), prob_c_dimension = prob.get_c_dimension(), prob_f_dimension = prob.get_f_dimension(); const decision_vector &lb = prob.get_lb(), &ub = prob.get_ub(); const population::size_type NP = pop.size(); const problem::base::size_type Dc = D - prob_i_dimension; //We perform some checks to determine wether the problem/population are suitable for sa_corana if ( Dc == 0 ) { pagmo_throw(value_error,"There is no continuous part in the problem decision vector for sa_corana to optimise"); } if ( prob_c_dimension != 0 ) { pagmo_throw(value_error,"The problem is not box constrained and sa_corana is not suitable to solve it"); } if ( prob_f_dimension != 1 ) { pagmo_throw(value_error,"The problem is not single objective and sa_corana is not suitable to solve it"); } //Determines the number of temperature adjustment for the annealing procedure const size_t n_T = m_niter / (m_step_adj * m_bin_size * Dc); // Get out if there is nothing to do. if (NP == 0 || m_niter == 0) { return; } if (n_T == 0) { pagmo_throw(value_error,"n_T is zero, increase niter"); } //Starting point is the best individual const int bestidx = pop.get_best_idx(); const decision_vector &x0 = pop.get_individual(bestidx).cur_x; const fitness_vector &fit0 = pop.get_individual(bestidx).cur_f; //Determines the coefficient to dcrease the temperature const double Tcoeff = std::pow(m_Tf/m_Ts,1.0/(double)(n_T)); //Stores the current and new points decision_vector xNEW = x0, xOLD = xNEW; fitness_vector fNEW = fit0, fOLD = fNEW; //Stores the adaptive steps of each component (integer part included but not used) decision_vector step(D,m_range); //Stores the number of accepted points per component (integer part included but not used) std::vector<int> acp(D,0) ; double ratio = 0, currentT = m_Ts, probab = 0; //Main SA loops for (size_t jter = 0; jter < n_T; ++jter) { for (int mter = 0; mter < m_step_adj; ++mter) { for (int kter = 0; kter < m_bin_size; ++kter) { size_t nter = boost::uniform_int<int>(0,Dc-1)(m_urng); for (size_t numb = 0; numb < Dc ; ++numb) { nter = (nter + 1) % Dc; //We modify the current point actsol by mutating its nter component within //a step that we will later adapt xNEW[nter] = xOLD[nter] + boost::uniform_real<double>(-1,1)(m_drng) * step[nter] * (ub[nter]-lb[nter]); // If new solution produced is infeasible ignore it if ((xNEW[nter] > ub[nter]) || (xNEW[nter] < lb[nter])) { xNEW[nter]=xOLD[nter]; continue; } //And we valuate the objective function for the new point prob.objfun(fNEW,xNEW); // We decide wether to accept or discard the point if (prob.compare_fitness(fNEW,fOLD) ) { //accept xOLD[nter] = xNEW[nter]; fOLD = fNEW; acp[nter]++; //Increase the number of accepted values } else { //test it with Boltzmann to decide the acceptance probab = exp ( - fabs(fOLD[0] - fNEW[0] ) / currentT ); // we compare prob with a random probability. if (probab > m_drng()) { xOLD[nter] = xNEW[nter]; fOLD = fNEW; acp[nter]++; //Increase the number of accepted values } else { xNEW[nter] = xOLD[nter]; } } // end if } // end for(nter = 0; ... } // end for(kter = 0; ... // adjust the step (adaptively) for (size_t iter = 0; iter < Dc; ++iter) { ratio = (double)acp[iter]/(double)m_bin_size; acp[iter] = 0; //reset the counter if (ratio > .6) { //too many acceptances, increase the step by a factor 3 maximum step[iter] = step [iter] * (1 + 2 *(ratio - .6)/.4); } else { if (ratio < .4) { //too few acceptance, decrease the step by a factor 3 maximum step [iter]= step [iter] / (1 + 2 * ((.4 - ratio)/.4)); }; }; //And if it becomes too large, reset it to its initial value if ( step[iter] > m_range ) { step [iter] = m_range; }; } } // Cooling schedule currentT *= Tcoeff; } if ( prob.compare_fitness(fOLD,fit0) ){ pop.set_x(bestidx,xOLD); //new evaluation is possible here...... std::transform(xOLD.begin(), xOLD.end(), pop.get_individual(bestidx).cur_x.begin(), xOLD.begin(),std::minus<double>()); pop.set_v(bestidx,xOLD); } }
int main ( int argc, char *argv[] ) { // Loop variables int i=0, j=0, k=0, l=0; // The number of faces and their size depend of the database of faces int n=0,m=0; // size of faces int N = 0; // number of faces char **originFacesPath = NULL; Image *originFaces = NULL; Image *eigenFaces = NULL; Image faceAverage; // ACP elements : // we use array of doubles double** R = NULL; // Matrice d'image (une ligne = un visage) double** transposedR = NULL; // Matrice d'image transposée double* uR = NULL; // Vecteur moyen double** XXT = NULL; // Eigen elements double* eigenValues = NULL; // the same for XXT and XTX double** eigenVectorsV = NULL; // eigen vectors of XXT double** eigenVectorsU = NULL; // eigen vectors of XTX // Face projetction on the new space int q; // dimension of the new space double** tabCoefficients = NULL; double** eigenProjections = NULL; Image *faceProjections = NULL; // Output pictures char output[OUTPUT_SIZE]; if(argc <= 1){ fprintf(stderr, "Usage : %s image1 image2 ...\n",argv[0]); exit (EXIT_FAILURE); } N = argc - 1; originFacesPath = parseArgv(argv,N); printf("Faces importation... "); // Importation des N visages sources (matrix nxm) originFaces = importFaces(originFacesPath,N); free(originFacesPath); free(originFacesPath[0]); originFacesPath = NULL; n = originFaces[0].nbLignes; m = originFaces[0].nbColonnes; // Conversion des visages en vecteurs lignes dans R // (première colonne de la matrice puis seconde etc. = m colonnes qui se suivent de n pixels) // R = {I1,I2, ... IN}T avec N le nombre de visages (cela donne une matrice de N * (n*m)) // Un visage dans R correspond à une ligne R = facesConversion(originFaces,N); for(i=0 ; i<N ; i++){ originFaces[i].t2D = imageDesallocation(originFaces[i].t2D); } free(originFaces); printf("Success!\n"); // If it is a center ACP we have to find the average face uR uR = averageFace(R,N,(n*m)); // then, for a center ACP, we transform R in a center matrix : R <- R - uR centerFacesMatrix(R,uR,N,(n*m)); matrixExport(uR,n,m,"averageFace.pgm"); // dim(XXT) = N*N != dim(XTX) = (n*m) * (n*m) [BEAUCOUP] // Donc on fait l'ACP sur XXT puis on récupère les composantes principales (CP) de XTX grâce aux formules : // va = CP de l'axe a de XXT et ua = CP de l'axe A de XTX // ua = va * XT * 1/sqrt(lambdaA) avec lambdaA la valeur propre de l'axe A (eigenValues of XXT are equal to the eigenValues of XTX) // a. XXT transposedR = transposeMatrix(R,N,(n*m)); XXT = multiplyMatrix(R,N,(n*m),transposedR,(n*m),N); // R = matrixDesallocation(R); // b. Valeurs propres et vecteurs propres if((eigenValues = malloc(N*sizeof(double)))==NULL){ perror("Memory allocation error\n"); exit(EXIT_FAILURE); } eigenVectorsV = memoryAllocation(N,N); printf("ACP... "); acp(XXT,N,eigenValues,eigenVectorsV); XXT = matrixDesallocation(XXT); // c. Now we have to recovery the principal components of XTX eigenVectorsU = memoryAllocation(n*m,n*m); // /!\ we must consider the number of eigen values != 0 // There are as many eigen values !=0 in XXT as in XTX // but the size of these matrix is different // so the loop stop before n*m and even N because here N < n*m for(j=0 ; j<N ; j++){ // we multiply the transposedR matrix by the XXT eigen vector of the jth column. for(i=0 ; i<n*m ; i++){ double sum = 0; for(k=0 ; k<N ; k++){ sum += transposedR[i][k]*eigenVectorsV[k][j]; } eigenVectorsU[i][j]=sum/sqrt(eigenValues[j]); } } free(eigenValues); transposedR = matrixDesallocation(transposedR); eigenVectorsV = matrixDesallocation(eigenVectorsV); if ((eigenFaces = malloc(N*sizeof(Image))) == NULL){ perror("Memory allocation error\n"); exit(EXIT_FAILURE); } for(i=0 ; i< N ; i++){ eigenFaces[i].nbLignes = n; eigenFaces[i].nbColonnes = m; eigenFaces[i].t2D = imageAllocation(n,m); } //inverseFacesConversion(eigenFaces,eigenVectorsU,N,n,m); for(k=0 ; k<N ; k++){ for(i=0 ; i<n ; i++){ for(j=0 ; j<m ; j++){ // /!\ Why a and b ?! without values are too low double a = 110; double b = 7000; if(k==0) printf("%g\t",eigenVectorsU[j*n+i][k]); eigenFaces[k].t2D[i][j] = (Pixel) (abs((int) (a+eigenVectorsU[j*n+i][k]*b))); } if(k==0) printf("\n"); } } // eigenVectorsU = matrixDesallocation(eigenVectorsU); /* for(k=0 ; k<N ; k++){ sprintf(output,"eigen_faces/eigen_face%d.pgm",k); matrixExport(eigenVectorsU[][k],n,m,output); }*/ printf("Success!\n"); printf("# Writing eigen faces ...\n"); for(i = 0 ; i < N ; i++){ sprintf(output,"img/eigen_faces/eigen_face%d.pgm",i); ecrireImage (eigenFaces[i], output); } // desallocation memoire for(i=0 ; i<N ; i++){ eigenFaces[i].t2D = imageDesallocation(eigenFaces[i].t2D); } printf("# Face projection ...\n"); // face projection q = N - 1; // each line contains the coefficients of the face in the new q-dimension-space tabCoefficients = memoryAllocation(N,q); eigenProjections = memoryAllocation(N,n*m); for(i=0 ; i<N ; i++){ projection(R[i], eigenVectorsU, uR, n, m, q, tabCoefficients[i], eigenProjections[i]); } // We search the origin face which is the closest of the face to project search_face("img/toProject.pgm", eigenVectorsU, uR, n, m, q, N, tabCoefficients); tabCoefficients = matrixDesallocation(tabCoefficients); eigenVectorsU = matrixDesallocation(eigenVectorsU); R = matrixDesallocation(R); free(uR); uR = NULL; // Now we have to create faceProjection with the eigenProjections /* if((faceProjections=malloc(N*sizeof(Image)))==NULL){ perror("Memory allocation error !"); exit(EXIT_FAILURE); } for(i=0 ; i< N ; i++){ faceProjections[i].nbLignes = n; faceProjections[i].nbColonnes = m; faceProjections[i].t2D = imageAllocation(n,m); } for(k=0 ; k<N ; k++){ for(i=0 ; i<n ; i++){ for(j=0 ; j<m ; j++){ faceProjections[k].t2D[i][j] = (Pixel) eigenProjections[k][j*n+i]; } } }*/ for(i=0 ; i<N ; i++){ sprintf(output,"projections/projection%d.pgm",i); matrixExport(eigenProjections[i],n,m,output); } eigenProjections = matrixDesallocation(eigenProjections); /*printf("# Writing projection faces ...\n"); for(i = 0 ; i < N ; i++){ sprintf(output,"img/projections/projection%d.pgm",i); ecrireImage (faceProjections[i], output); } // Memory desallocation for(i=0 ; i<N ; i++){ faceProjections[i].t2D = imageDesallocation(faceProjections[i].t2D); }*/ return EXIT_SUCCESS; }