void fft_fill()
{
  int i;
  double s=1/65535.0;
  read_pgm("/dev/shm/1.pgm",0);
  for(i=0;i<width[0]*height[0];i++){
    fft_in[0][i]=image[0][i]*s;
  }
  read_pgm("/dev/shm/2.pgm",1);
  for(i=0;i<width[1]*height[1];i++){
    fft_in[1][i]=image[1][i]*s;
  }
}
/* Assombrissement  d'une image pour pouvoir voir les effets de la normalisation */
void assombrir_image(char * cheminEntree, char * cheminSortie, int val)
{
	int y=0, x=0;

	//création de la future image assombrie
	image * image = read_pgm(cheminEntree);
    
#pragma omp parallel
    {
#pragma omp for
	for (y = 0; y < image->h; y++)
	{
#pragma omp for
		for (x = 0; x < image->w; x++)
		{
			//valeur du niveau de gris superieur à val => baisse le niveau de val 	
			if(image->img[y+x*(image->h)] > val)
			{
				image->img[y+x*(image->h)] = image->img[y+x*(image->h)] - val;
			}
			else //sinon : on met a 0 pour ne pas être en négatif
			{
				image->img[y+x*(image->h)] = 0;
			}
		}
	}
        
    }//end of parallel
	write_pgm(cheminSortie, image);
	return;
}
int main(int argc, char **argv){
int** picture;
char* name;
name = malloc(strlen(argv[0]));
picture = read_pgm(argv[1]);
printf("Value of picture: %d\n", picture[490][1]);
free(name);
return 0;
}
示例#4
0
Matrix<Color> read_img(std::string filename)
{
    std::size_t n = filename.size();
    std::string extension3 = filename.substr(n-3, n);
    std::string extension4 = filename.substr(n-4, n);
    if(!extension3.compare("bmp"))
    {
        return read_bmp(filename);
    }
    else if(!extension3.compare("gif"))
    {
        return read_gif(filename);
    }
    else if(!extension3.compare("ico"))
    {
        return read_ico(filename);
    }
    /*else if(!extension3.compare("jpg"))
    {
        return read_jpeg(filename);
    }*/
    else if(!extension3.compare("pcx"))
    {
        return read_pcx(filename);
    }
    else if(!extension3.compare("png"))
    {
        return read_png(filename);
    }
    else if(!extension3.compare("pbm"))
    {
        return bw2colorimage(read_pbm(filename));
    }
    else if(!extension3.compare("pgm"))
    {
        return gray2colorimage(read_pgm(filename));
    }
    else if(!extension3.compare("ppm"))
    {
        return read_ppm(filename);
    }
    else if(!extension3.compare("tga"))
    {
        return read_tga(filename);
    }
    else if(!extension4.compare("tiff"))
    {
        return read_tiff(filename);
    }
    else
    {
        return Matrix<Color>();
    }
}
void normalisation_image(char * chemin_entree, char * chemin_sortie)
{
	image * im = NULL;
	image * norma_train = NULL;
	int w=0,h=0,i=0;
	int taille_img=0;
	int pmin = 0, pmax=0;

	im = read_pgm(chemin_entree);

	w = im->w;
	h = im->h;
	taille_img = w*h;
	
	printf("\n  Creation d'une image normalisee: norma_train.pgm (h=%d,w=%d)...\n",h,w);
	norma_train = create_image(w,h,255);
	
	if( norma_train == NULL || chemin_entree == NULL || chemin_sortie == NULL)
		error("-- Problème normalisation -> pointeur NULL -- ");

	//On détermine les valeurs pmin et pmax de l'image de départ
#pragma omp parallel
    {
#pragma omp for
	for(i=0; i< taille_img; i++)
	{     
			//Si la valeur du niveau de gris est inférieur à pmin, il devient pmin
			if(im->img[i]< pmin)
				pmin=im->img[i];   
			//Si la valeur du niveau de gris est supérieur à pmax, il devient pmax
			else if(im->img[i] > pmax)
				pmax=im->img[i];
	}

	// On applique la formule de normalisation avec les pmin et pmax trouvé
#pragma omp for
	for( i=0 ; i < taille_img; i++)
	{
		norma_train->img[i] = 255*(im->img[i]-pmin)/(pmax-pmin);
	}

        
    }//end of parallel
	write_pgm(chemin_sortie,norma_train);
	
	return;
}
void print_image(const char *filename, int freq_min, int freq_max, int col_time_ms)
{
    char buf[2048];
    Context context;
    int width, height, max;
    const char *image = read_pgm(filename, &width, &height, &max);
    if (image == NULL) {
        printf("Error loading %s", filename);
        exit(1);
    }
    Pa_Initialize();
    context_init(&context, height, freq_min, freq_max, col_time_ms);
    start_playback(&context);

    for (int col = 0; col < width; col++) {
        for (int row = 0; row < height; row++) {
            buf[height - 1 - row] = image[row*width + col];
        }
        print_image_column(&context, buf, height, max);
    }
    stop_playback(&context);
    Pa_Terminate();
    context_dealloc(&context);
}
示例#7
0
int main(int argc, char *argv[])
{
  dungeon_t d;
  time_t seed;
  struct timeval tv;
  int32_t i;
  uint32_t do_load, do_save, do_seed, do_image, do_save_seed,
           do_save_image, do_place_pc;
  uint32_t long_arg;
  char *save_file;
  char *load_file;
  char *pgm_file;

  memset(&d, 0, sizeof (d));

  /* Default behavior: Seed with the time, generate a new dungeon, *
   * and don't write to disk.                                      */
  do_load = do_save = do_image = do_save_seed =
    do_save_image = do_place_pc = 0;
  do_seed = 1;
  save_file = load_file = NULL;
  d.max_monsters = MAX_MONSTERS;
  d.max_objects = MAX_OBJECTS;
  
  /* The project spec requires '--load' and '--save'.  It's common  *
   * to have short and long forms of most switches (assuming you    *
   * don't run out of letters).  For now, we've got plenty.  Long   *
   * forms use whole words and take two dashes.  Short forms use an *
    * abbreviation after a single dash.  We'll add '--rand' (to     *
   * specify a random seed), which will take an argument of it's    *
   * own, and we'll add short forms for all three commands, '-l',   *
   * '-s', and '-r', respectively.  We're also going to allow an    *
   * optional argument to load to allow us to load non-default save *
   * files.  No means to save to non-default locations, however.    *
   * And the final switch, '--image', allows me to create a dungeon *
   * from a PGM image, so that I was able to create those more      *
   * interesting test dungeons for you.                             */
 
 if (argc > 1) {
    for (i = 1, long_arg = 0; i < argc; i++, long_arg = 0) {
      if (argv[i][0] == '-') { /* All switches start with a dash */
        if (argv[i][1] == '-') {
          argv[i]++;    /* Make the argument have a single dash so we can */
          long_arg = 1; /* handle long and short args at the same place.  */
        }
        switch (argv[i][1]) {
        case 'r':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-rand")) ||
              argc < ++i + 1 /* No more arguments */ ||
              !sscanf(argv[i], "%lu", &seed) /* Argument is not an integer */) {
            usage(argv[0]);
          }
          do_seed = 0;
          break;
        case 'l':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-load"))) {
            usage(argv[0]);
          }
          do_load = 1;
          if ((argc > i + 1) && argv[i + 1][0] != '-') {
            /* There is another argument, and it's not a switch, so *
             * we'll treat it as a save file and try to load it.    */
            load_file = argv[++i];
          }
          break;
        case 's':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-save"))) {
            usage(argv[0]);
          }
          do_save = 1;
          if ((argc > i + 1) && argv[i + 1][0] != '-') {
            /* There is another argument, and it's not a switch, so *
             * we'll save to it.  If it is "seed", we'll save to    *
	     * <the current seed>.rlg327.  If it is "image", we'll  *
	     * save to <the current image>.rlg327.                  */
	    if (!strcmp(argv[++i], "seed")) {
	      do_save_seed = 1;
	      do_save_image = 0;
	    } else if (!strcmp(argv[i], "image")) {
	      do_save_image = 1;
	      do_save_seed = 0;
	    } else {
	      save_file = argv[i];
	    }
          }
          break;
        case 'i':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-image"))) {
            usage(argv[0]);
          }
          do_image = 1;
          if ((argc > i + 1) && argv[i + 1][0] != '-') {
            /* There is another argument, and it's not a switch, so *
             * we'll treat it as a save file and try to load it.    */
            pgm_file = argv[++i];
          }
          break;
        case 'n':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-nummon")) ||
              argc < ++i + 1 /* No more arguments */ ||
              !sscanf(argv[i], "%hu", &d.max_monsters)) {
            usage(argv[0]);
          }
          break;
        case 'p':
          /* PC placement makes no effort to avoid placing *
           * the PC inside solid rock.                     */
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-pc"))) {
            usage(argv[0]);
          }
          if ((d.PC->position[dim_y] = atoi(argv[++i])) < 1 ||
              d.PC->position[dim_y] > DUNGEON_Y - 2         ||
              (d.PC->position[dim_x] = atoi(argv[++i])) < 1 ||
              d.PC->position[dim_x] > DUNGEON_X - 2)         {
            fprintf(stderr, "Invalid PC position.\n");
            usage(argv[0]);
          }
          do_place_pc = 1;
          break;
        case 'o':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-objcount")) ||
              argc < ++i + 1 /* No more arguments */ ||
              !sscanf(argv[i], "%hu", &d.max_objects)) {
            usage(argv[0]);
          }
          break;
         default:
          usage(argv[0]);
        }
      } else { /* No dash */
        usage(argv[0]);
      }
    }
  }

  if (do_seed) {
    /* Allows me to generate more than one dungeon *
     * per second, as opposed to time().           */
    gettimeofday(&tv, NULL);
    seed = (tv.tv_usec ^ (tv.tv_sec << 20)) & 0xffffffff;
  }

  srand(seed);

  parse_descriptions(&d);
  io_init_terminal();
  init_dungeon(&d);

  if (do_load) {
    read_dungeon(&d, load_file);
  } else if (do_image) {
    read_pgm(&d, pgm_file);
  } else {
    gen_dungeon(&d);
  }

  config_pc(&d);
  gen_monsters(&d);
  gen_objects(&d);
  pc_observe_terrain(d.PC, &d);

  io_display(&d);
  io_queue_message("Seed is %u.", seed);
  while (pc_is_alive(&d) && dungeon_has_npcs(&d) && !d.quit) {
    do_moves(&d);
  }
  io_display(&d);

  io_reset_terminal();
  
  if (do_save) {
    if (do_save_seed) {
       /* 10 bytes for number, dot, extention and null terminator. */
      save_file = (char *) malloc(18);
      sprintf(save_file, "%ld.rlg327", seed);
    }
    if (do_save_image) {
      if (!pgm_file) {
	fprintf(stderr, "No image file was loaded.  Using default.\n");
	do_save_image = 0;
      } else {
	/* Extension of 3 characters longer than image extension + null. */
	save_file = (char *) malloc(strlen(pgm_file) + 4);
	strcpy(save_file, pgm_file);
	strcpy(strchr(save_file, '.') + 1, "rlg327");
      }
    }
    write_dungeon(&d, save_file);

    if (do_save_seed || do_save_image) {
      free(save_file);
    }
  }

  printf("%s", pc_is_alive(&d) ? victory : tombstone);
  printf("You defended your life in the face of %u deadly beasts.\n"
         "You avenged the cruel and untimely murders of %u "
         "peaceful dungeon residents.\n",
         d.PC->kills[kill_direct], d.PC->kills[kill_avenged]);

  if (pc_is_alive(&d)) {
    /* If the PC is dead, it's in the move heap and will get automatically *
     * deleted when the heap destructs.  In that case, we can't call       *
     * delete_pc(), because it will lead to a double delete.               */
    character_delete(d.PC);
  }

  delete_dungeon(&d);
  destroy_descriptions(&d);

  return 0;
}
示例#8
0
void hough(char* input, char* output){
    image* im=read_pgm(input);
    int RMAX =sqrt(pow(im->h, 2)+pow(im->w, 2));
    int **houghMatrix;
    houghMatrix=(int**)malloc(sizeof(int*)*WH);
    for (int i=0; i<WH; i++) {
        houghMatrix[i]=(int*)malloc(sizeof(int)*RMAX);
        for (int j=0; j<RMAX; j++) {
            houghMatrix[i][j]=0;
        }
    }
    
    double theta=PI/(2*WH);
    
    int max10[NUM_LINES]={0};
    int positioni[NUM_LINES];
    int positionj[NUM_LINES];

#pragma omp parallel
    {
#pragma omp for
    for (int i=0; i<im->h; i++) {
#pragma omp for
        for (int j=0; j<im->w; j++) {
            if (im->img[j*im->h+i]>THRESHOLD_HOUGH) {
                for (int l=0; l<WH; l++) {
                    int r=i*sin(theta*l)+j*cos(theta*l);
                    if (r<RMAX) {
                        houghMatrix[l][r]++;
                    }
                }
            }
        }
            //imSobelOut->img[j*h+i] =sobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
    }
#pragma omp for
    for (int i=0; i<WH; i++) {
        for (int j=0; j<RMAX; j++) {
            for (int k=0; k<NUM_LINES; k++) {
                if (max10[k]<houghMatrix[i][j]) {
                    for (int n=NUM_LINES-1; n>k; n--) {
                        max10[n]=max10[n-1];
                    }
                    max10[k]=houghMatrix[i][j];
                    positioni[k]=i;
                    positionj[k]=j;
                    break;
                }
            }
        }
    }
  /*
    for (int i=0; i<im->h; i++)
        for (int j=0; j<im->w; j++)
            im->img[j*im->h+i]=0;
    */
        
#pragma omp for
    int j=0;
    for (int k=0; k<NUM_LINES; k++) {
#pragma omp for
        for (int i=0; i<im->h; i++) {
            j=(positionj[k]-(i*sin(theta*positioni[k])))/cos(theta*positioni[k]);
            if (j<im->w && j>0) {
                im->img[j*im->h+i]=255;
            }
        }
    }
        
    }//end of parallel
    
    write_pgm(output, im);
    
}
示例#9
0
文件: main.c 项目: ludcila/CFD-Lab
int main(int argn, char** args){

	double **U, **V, **P, **F, **G, **RS;
	const char *szFileName = "dam_break.dat";
	double Re, UI, VI, PI, GX, GY, t_end, xlength, ylength, dt, dx, dy, alpha, omg, tau, eps, dt_value;
	double res = 0, t = 0, n = 0;
	int imax, jmax, itermax, it;

	
	/* Additional data structures for VOF */
	double **fluidFraction;
	double **fluidFraction_alt;
	int **flagField;
	double **dFdx, **dFdy;
	int **pic;
	char output_dirname[60];
	double epsilon = 1e-10;
	
	/* Read the program configuration file using read_parameters() */
	read_parameters(szFileName, &Re, &UI, &VI, &PI, &GX, &GY, &t_end, &xlength, &ylength, &dt, &dx, &dy, &imax, &jmax, &alpha, &omg, &tau, &itermax, &eps, &dt_value);        

	/* Set up the matrices (arrays) needed using the matrix() command */
	U = matrix(0, imax  , 0, jmax+1);
	V = matrix(0, imax+1, 0, jmax  );
	P = matrix(0, imax+1, 0, jmax+1);
	F = matrix(0, imax  , 0, jmax+1);
	G = matrix(0, imax+1, 0, jmax  );
	RS= matrix(0, imax+1, 0, jmax+1);
	flagField = imatrix(0, imax+1, 0, jmax+1);
	fluidFraction = matrix(0, imax+1, 0, jmax+1);
	fluidFraction_alt = matrix(0, imax+1, 0, jmax+1);
	dFdx = matrix(0, imax+1, 0, jmax+1);
	dFdy = matrix(0, imax+1, 0, jmax+1);


	/*create a directory*/
	strcpy(output_dirname, "dam_break/dam_break");
	mkdir("dam_break", 0777);
	
	/* Read pgm file with domain and initial setting */
	pic = read_pgm("dam_break.pgm");
	init_fluidFraction(pic, fluidFraction,fluidFraction_alt, imax, jmax);
	
	/* Assign initial values to u, v, p */
	init_uvp(UI, VI, PI, imax, jmax, U, V, P);

		/* Set valid values for the fluid fraction */
		adjust_fluidFraction(fluidFraction, flagField, epsilon, imax, jmax);

	while(t <= t_end){   
	
		/*Select δt*/
		calculate_dt(Re, tau, &dt, dx, dy, imax, jmax, U, V);
		
		/* Set boundary values for u and v */
/*		boundaryvalues(imax, jmax, U, V, flagField, dx, dy);
*/		
		/* Compute F(n) and G(n) */
		calculate_fg(Re, GX, GY, alpha, dt, dx, dy, imax, jmax, U, V, F, G, flagField);
		
		/* Compute the right-hand side rs of the pressure equation */
		calculate_rs(dt, dx, dy, imax, jmax, F, G, RS, flagField);



		
				
		
		/* Determine the orientation of the free surfaces */

		calculate_freeSurfaceOrientation(fluidFraction, flagField, dFdx, dFdy, dx, dy, imax, jmax);

		
		/* Perform SOR iterations */
		it=0;
		res = 1e6;
		while(it < itermax && res > eps){
			sor(omg, dx, dy, imax, jmax, P, RS, fluidFraction, flagField, dFdx, dFdy, &res);
			it++;
		}

		
		/* Compute u(n+1) and v(n+1) */
		calculate_uv(dt, dx, dy, imax, jmax, U, V, F, G, P, flagField);

		boundaryvalues(imax, jmax, U, V, flagField, dx, dy);
		
		/* Compute fluidFraction(n+1) */
		calculate_fluidFraction(fluidFraction,fluidFraction_alt, flagField, U, V, dFdx, dFdy, imax, jmax, dx, dy, dt);

		boundaryvalues(imax, jmax, U, V, flagField, dx, dy);
		/* Set valid values for the fluid fraction */
		adjust_fluidFraction(fluidFraction, flagField, epsilon, imax, jmax);

		boundaryvalues(imax, jmax, U, V, flagField, dx, dy);


		if((int)n % 10 == 0) {
			write_vtkFile(output_dirname, n, xlength, ylength, imax, jmax, dx, dy, U, V, P, fluidFraction, flagField);
		}
		
		/* Print out simulation time and whether SOR converged */
		printf("Time: %.4f", t);
		if(res > eps) {printf("\t*** Did not converge (res=%f, eps=%f)", res, eps);return -1;}
		printf("\n");
		
		t = t + dt;
		n++;
	
	}
	

	free_matrix(U , 0, imax  , 0, jmax+1);
	free_matrix(V , 0, imax+1, 0, jmax  );
	free_matrix(P , 0, imax+1, 0, jmax+1);
	free_matrix(F , 0, imax  , 0, jmax+1);
	free_matrix(G , 0, imax+1, 0, jmax  );
	free_matrix(RS, 0, imax+1, 0, jmax+1);
	
	return -1;
	
}
void initialiseFields( double *collideField, double *streamField, int *flagField, int * xlength, int *local_xlength, char *problem, char* pgmInput, int rank, int iProc, int jProc, int kProc )
{
    int iCoord, jCoord, kCoord, x, y, z, i;
#ifdef _ARBITRARYGEOMETRIES_
    int** pgmImage;
#endif // _ARBITRARYGEOMETRY_
    computePosition(iProc, jProc, kProc, &iCoord, &jCoord, &kCoord);
#ifdef _ARBITRARYGEOMETRIES_
    #pragma omp parallel private(x, y, z, i) shared(iCoord, jCoord, kCoord, collideField, flagField, streamField, xlength, problem, pgmInput, pgmImage, local_xlength, iProc, jProc, kProc)
#else
    #pragma omp parallel private(x, y, z, i) shared(iCoord, jCoord, kCoord, collideField, flagField, streamField, xlength, local_xlength, iProc, jProc, kProc)
#endif // _ARBITRARYGEOMETRY_
    {
        #pragma omp for schedule(static)
        for(x = 0; x < (local_xlength[0] + 2) * (local_xlength[1] + 2) * (local_xlength[2] + 2); x++)
        {
            flagField[x] = FLUID;
            for(i = 0; i < PARAMQ; i++)
                collideField[PARAMQ * x + i] = streamField[PARAMQ * x + i] = LATTICEWEIGHTS[i];
        }

        // independend of the problem first initialize all ghost cells as PARALLEL_BOUNDARY
        /* top boundary */
        #pragma omp for nowait schedule(static)
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] = PARALLEL_BOUNDARY;

        /* back boundary */
        #pragma omp for nowait schedule(static)
        for (y = 0; y <= local_xlength[1] + 1; y++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                flagField[y * (local_xlength[0] + 2) + x] = PARALLEL_BOUNDARY;

        /* bottom boundary */
        #pragma omp for nowait schedule(static)
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] = PARALLEL_BOUNDARY;

        /* left boundary */
        #pragma omp for nowait schedule(static)
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (y = 0; y <= local_xlength[1] + 1; y++)
                flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] = PARALLEL_BOUNDARY;

        /* right boundary */
        #pragma omp for nowait schedule(static)
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (y = 0; y <= local_xlength[1] + 1; y++)
                flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] = PARALLEL_BOUNDARY;

        /* front boundary, i.e. z = local_xlength + 1 */
        #pragma omp for schedule(static)
        for (y = 0; y <= local_xlength[1] + 1; y++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                flagField[(local_xlength[2] + 1) * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = PARALLEL_BOUNDARY;

        /** initialization of different scenarios */
#ifdef _ARBITRARYGEOMETRIES_
        if (!strcmp(problem, "drivenCavity"))
        {
#endif // _ARBITRARYGEOMETRY_
            /* front boundary, i.e. z = local_xlength + 1 */
            if (kCoord == kProc - 1)
            {
                #pragma omp for nowait schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[(local_xlength[2] + 1) * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = NO_SLIP;
            }

            /* back boundary */
            if (kCoord == 0)
            {
                #pragma omp for schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[y * (local_xlength[0] + 2) + x] = NO_SLIP;
            }

            /* left boundary */
            if (iCoord == 0)
            {
                #pragma omp for nowait schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] = NO_SLIP;
            }
            /* right boundary */
            if (iCoord == iProc - 1)
            {
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] = NO_SLIP;
            }
            /* bottom boundary */
            if (jCoord == 0)
            {
                #pragma omp for nowait schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)

                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] = NO_SLIP;
            }

            /* top boundary */
            if (jCoord == jProc - 1)
            {
                #pragma omp for nowait schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] = MOVING_WALL;
            }
#ifdef _ARBITRARYGEOMETRIES_
        }
        if (!strcmp(problem, "tiltedPlate"))
        {
            #pragma omp single
            {
                pgmImage = read_pgm(pgmInput);
            }
            #pragma omp for nowait schedule(static)
            for (z = 1; z <= local_xlength[2]; z++)
                for (y = 1; y <= local_xlength[1]; y++)
                    for (x = 1; x <= local_xlength[0]; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = !!pgmImage[(xlength[0] / iProc) * iCoord + x][(xlength[1] / jProc) * jCoord + y];


            /* front boundary, i.e. z = local_xlength + 1 */
            if (kCoord == kProc - 1)
                #pragma omp for nowait schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                    {
                        // check for obstacle in the cell in the inner part of the domain adjacent to the boundary (i.e. NO_SLIP)
                        // if there is an obstacle, make the boundary NO_SLIP instead of FREE_SLIP as well
                        if (pgmImage[(xlength[0] / iProc) * iCoord + x][(xlength[1] / jProc) * jCoord + y])
                            flagField[(local_xlength[2] + 1) * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = NO_SLIP;
                        else
                            flagField[(local_xlength[2] + 1) * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = FREE_SLIP;
                    }

            /* back boundary */
            if (kCoord == 0)
                #pragma omp for schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                    {
                        // check for obstacle in the cell in the inner part of the domain adjacent to the boundary
                        // if there is an obstacle, make the boundary NO_SLIP instead of FREE_SLIP as well
                        if (pgmImage[(xlength[0] / iProc) * iCoord + x][(xlength[1] / jProc) * jCoord + y])
                            flagField[y * (local_xlength[0] + 2) + x] = NO_SLIP;
                        else
                            flagField[y * (local_xlength[0] + 2) + x] = FREE_SLIP;
                    }


            /* left boundary */
            if (iCoord == 0)
                #pragma omp for nowait schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] = INFLOW;

            /* right boundary */
            if (iCoord == iProc - 1)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] = OUTFLOW;


            /* top boundary */
            if (jCoord == jProc - 1)
                #pragma omp for nowait schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] = NO_SLIP;

            /* bottom boundary */
            if (jCoord == 0)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] = NO_SLIP;


            // Adjust PARALLEL_BOUNDARY if the "adjacent" cell in the domain of the neighbouring process is an obstacle cell
            /* top boundary */
            #pragma omp for nowait schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (x = 0; x <= local_xlength[0] + 1; x++)
                    if (flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] == PARALLEL_BOUNDARY && pgmImage[(xlength[0] / iProc) * iCoord + x][(xlength[1] / jProc) * (jCoord + 1) +  1])
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] = NO_SLIP;

            /* bottom boundary */
            #pragma omp for schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (x = 0; x <= local_xlength[0] + 1; x++)
                    if (flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] == PARALLEL_BOUNDARY && pgmImage[(xlength[0] / iProc) * iCoord + x][(xlength[1] / jProc) * jCoord])
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] = NO_SLIP;

            /* left boundary */
            #pragma omp for nowait schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    if (flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] == PARALLEL_BOUNDARY && pgmImage[(xlength[0] / iProc) * iCoord][(xlength[1] / jProc) * jCoord + y])
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] = NO_SLIP;

            /* right boundary */
            #pragma omp for schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    if (flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] == PARALLEL_BOUNDARY && pgmImage[(xlength[0] / iProc) * (iCoord + 1) + 1][(xlength[1] / jProc) * jCoord + y])
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] = NO_SLIP;
            #pragma omp single
            {
                free_imatrix(pgmImage, 0, xlength[0] + 2, 0, xlength[1] + 2);
            }
        }
        if (!strcmp(problem, "flowStep"))
        {
            /* front boundary, i.e. z = local_xlength + 1 */
            if (kCoord == kProc - 1)
                #pragma omp for schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[(local_xlength[2] + 1) * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = NO_SLIP;

            /* back boundary */
            if (kCoord == 0)
                #pragma omp for schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[y * (local_xlength[0] + 2) + x] = NO_SLIP;

            /* left boundary */
            if (iCoord == 0)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] = INFLOW;

            /* right boundary */
            if (iCoord == iProc - 1)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] = OUTFLOW;

            /* top boundary */
            if (jCoord == jProc - 1)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] = NO_SLIP;

            /* bottom boundary */
            if (jCoord == 0)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] = NO_SLIP;


            /* step */
            // step height & width: jProc * local_xlength[1] / 2
            #pragma omp for schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (y = 1; y <= min(xlength[1] / 2 - jCoord * (xlength[1] / jProc), local_xlength[1] ) ; y++) /* integer division on purpose, half of the channel is blocked by step */
                    for (x = 1; x <= min(xlength[1] / 2 - iCoord * (xlength[0] / iProc), local_xlength[0] ); x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = NO_SLIP;

            // adjust the PARALLEL_BOUNDARY where "adjacent" cells in the neighbouring domain are NO_SLIP
            /* top boundary */
            #pragma omp for schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (x = 0; x <= local_xlength[0] + 1; x++)
                    if (x <= xlength[1] / 2 - iCoord * (xlength[0] / iProc) && xlength[1] / 2 - (jCoord + 1) * (xlength[1] / jProc) >= 1 && flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] == PARALLEL_BOUNDARY)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] = NO_SLIP;


            /* bottom boundary */
            #pragma omp for schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (x = 0; x <= local_xlength[0] + 1; x++)
                    if( x <= xlength[1] / 2 - iCoord * (xlength[0] / iProc) && 0 <= xlength[1] / 2 - jCoord * (xlength[1] / jProc) && flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] == PARALLEL_BOUNDARY)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] = NO_SLIP;

            /* left boundary */
            #pragma omp for schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    if( 0 <= xlength[1] / 2 - iCoord * (xlength[0] / iProc) && y <= xlength[1] / 2 - jCoord * (xlength[1] / jProc)  && flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] == PARALLEL_BOUNDARY)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] = NO_SLIP;

            /* right boundary */
            #pragma omp for schedule(static)
            for (z = 0; z <= local_xlength[2] + 1; z++)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    if( 1 <= xlength[1] / 2 - (iCoord + 1) * (xlength[0] / iProc) && y <= xlength[1] / 2 - jCoord * (xlength[1] / jProc)  && flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] == PARALLEL_BOUNDARY)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] = NO_SLIP;
        }
        if (!strcmp(problem, "planeShearFlow"))
        {

            /* front boundary, i.e. z = xlength + 1 */
            if (kCoord == kProc - 1)
                #pragma omp for schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[(local_xlength[2] + 1) * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x] = FREE_SLIP;

            /* back boundary */
            if (kCoord == 0)
                #pragma omp for schedule(static)
                for (y = 0; y <= local_xlength[1] + 1; y++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[y * (local_xlength[0] + 2) + x] = FREE_SLIP;

            /* left boundary */
            if (iCoord == 0)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2)] = PRESSURE_IN;

            /* right boundary */
            if (iCoord == iProc - 1)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (y = 0; y <= local_xlength[1] + 1; y++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + local_xlength[0] + 1] = OUTFLOW;

            /* top boundary */
            if (jCoord == jProc - 1)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + (local_xlength[1] + 1) * (local_xlength[0] + 2) + x] = NO_SLIP;

            /* bottom boundary */
            if (jCoord == 0)
                #pragma omp for schedule(static)
                for (z = 0; z <= local_xlength[2] + 1; z++)
                    for (x = 0; x <= local_xlength[0] + 1; x++)
                        flagField[z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + x] = NO_SLIP;
        }
#endif // _ARBITRARYGEOMETRY_
    } // end of parallel region

    /** debugging code: checking the flagField */
#ifdef DEBUG
    int * exactFlagField;
    exactFlagField = (int *) malloc( (size_t) sizeof( int ) * (xlength[0] + 2) *  (xlength[1] + 2) * (xlength[2] + 2));
    FILE *fp2 = NULL;
    unsigned int line2 = 0;
    int noOfReadEntries;
    int error2 = 0;
    char szFileName2[80];
    computePosition(iProc, jProc, kProc, &iCoord, &jCoord, &kCoord);
    sprintf( szFileName2, "Testdata/%s/flagField.dat", problem );
    fp2 = fopen(szFileName2,"r");
    if (fp2 != NULL)
    {
        for (line2 = 0; line2 < (xlength[0] + 2) *  (xlength[1] + 2) * (xlength[2] + 2); line2++)
        {
            noOfReadEntries = fscanf(fp2,"%d",&exactFlagField[line2]);
            if (noOfReadEntries != 1)
                continue;
        }

    }
    fclose(fp2);
    for (z = 1; z <= local_xlength[2]; z++)
        for (y = 1; y <= local_xlength[1]; y++)
            for(x = 1; x <= local_xlength[0]; x++)
                for (i = 0; i < PARAMQ; i++)
                    if (flagField[(z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x)] != exactFlagField[((z + kCoord * (xlength[2] / kProc)) * (xlength[0] + 2) * (xlength[1] + 2) + (y + jCoord * (xlength[1] / jProc)) * (xlength[0] + 2) + (x + iCoord * (xlength[0] / iProc)))])
                        error2 = 1;
    if (error2)
        printf("ERROR: Process %d has a different flagField at inner nodes.\n",rank);

    error2 = 0;
    // Check global boundaries as well
    if (iCoord == 0)
    {
        // Left boundary
        x = 0;
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (y = 0; y <= local_xlength[1] + 1; y++)
                if (flagField[(z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x)] != exactFlagField[((z + kCoord * (xlength[2] / kProc)) * (xlength[0] + 2) * (xlength[1] + 2) + (y + jCoord * (xlength[1] / jProc)) * (xlength[0] + 2) + (x + iCoord * (xlength[0] / iProc)))])
                    error2 = 1;

    }
    if (iCoord == iProc - 1)
    {
        // Right boundary
        x = local_xlength[0] + 1;
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (y = 0; y <= local_xlength[1] + 1; y++)
                if (flagField[(z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x)] != exactFlagField[((z + kCoord * (xlength[2] / kProc)) * (xlength[0] + 2) * (xlength[1] + 2) + (y + jCoord * (xlength[1] / jProc)) * (xlength[0] + 2) + (x + iCoord * (xlength[0] / iProc)))])
                    error2 = 1;
    }
    if (jCoord == 0)
    {
        // Bottom boundary
        y = 0;
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                if (flagField[(z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x)] != exactFlagField[((z + kCoord * (xlength[2] / kProc)) * (xlength[0] + 2) * (xlength[1] + 2) + (y + jCoord * (xlength[1] / jProc)) * (xlength[0] + 2) + (x + iCoord * (xlength[0] / iProc)))])
                    error2 = 1;

    }
    if (jCoord == jProc - 1)
    {
        // Top boundary
        y = local_xlength[1] + 1;
        for (z = 0; z <= local_xlength[2] + 1; z++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                if (flagField[(z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x)] != exactFlagField[((z + kCoord * (xlength[2] / kProc)) * (xlength[0] + 2) * (xlength[1] + 2) + (y + jCoord * (xlength[1] / jProc)) * (xlength[0] + 2) + (x + iCoord * (xlength[0] / iProc)))])
                    error2 = 1;
    }
    if (kCoord == 0)
    {
        // back boundary
        z = 0;
        for (y = 0; y <= local_xlength[1] + 1; y++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                if (flagField[(z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x)] != exactFlagField[((z + kCoord * (xlength[2] / kProc)) * (xlength[0] + 2) * (xlength[1] + 2) + (y + jCoord * (xlength[1] / jProc)) * (xlength[0] + 2) + (x + iCoord * (xlength[0] / iProc)))])
                    error2 = 1;

    }
    if (kCoord == kProc - 1)
    {
        // front boundary
        z = local_xlength[2] + 1;
        for (y = 0; y <= local_xlength[1] + 1; y++)
            for (x = 0; x <= local_xlength[0] + 1; x++)
                if (flagField[(z * (local_xlength[0] + 2) * (local_xlength[1] + 2) + y * (local_xlength[0] + 2) + x)] != exactFlagField[((z + kCoord * (xlength[2] / kProc)) * (xlength[0] + 2) * (xlength[1] + 2) + (y + jCoord * (xlength[1] / jProc)) * (xlength[0] + 2) + (x + iCoord * (xlength[0] / iProc)))])
                    error2 = 1;
    }

    if (error2)
        printf("ERROR: Process %d has a different flagField at global boundary.\n",rank);
    free(exactFlagField);
#endif
    /** debugging code end */
}
示例#11
0
文件: init.c 项目: EvaBr/CFD-Lab
/**
 * The integer array Flag is initialized to constants C_F for fluid cells and C_B
 * for obstacle cells as specified by the parameter problem.
 */
void init_flag(
		char *problem,
		int imax,
		int jmax,
		int kmax,
		//  double presDelta,
		int ***Flag,
		int wl,
		int wr,
		int wf,
		int wh,
		int wt,
		int wb
) {
	int i,j,k;
	//read the geometry
	int **Pic = read_pgm(problem);
	for (k=1; k<kmax+1; k++) {
		//initialisation to C_F and C_B  Flag[i][0][0]
		for (int j=1; j<jmax+1; j++){ //i is the line, j the column. so left, right is j-1,j+1, and north, south is i-1, i+1. up/down (bigger/smaller z) is -/+ (ymax+2)*k
			for (int i=1; i<imax+1; i++){ //in Pic, 1 is where it's fluid, and 0 where it's air, apart from that: different boundary cond.
				Flag[i][jmax+1-j][k] = createflag(Pic,i,j,k,imax,jmax,kmax);
			}
		} //da bo to delal more bit slika tk narjena, da ma vsaka 2D podslika okoliinokoli ghost boundary. (s poljubno boundary cifro, tj med 2 in 6)
	}

	//set outer boundary vortices, so you wont use them uninitialised. used getwallbit() bcs theyre on the edges of domain, so have only 2 or 3 neighbs, all boundary
	for (k=0; k<kmax+2; k++) {
		Flag[0][0][k] = getwallbit(wf);            //xz0
		Flag[0][jmax+1][k] = getwallbit(wh);       //xz1
		Flag[imax+1][0][k] = getwallbit(wf);       //xz0
		Flag[imax+1][jmax+1][k] = getwallbit(wh);  //xz1
	}
	for (j=0; j<jmax+2; j++) {
		Flag[0][j][0] = getwallbit(wb);            //xy0
		Flag[0][j][kmax+1] = getwallbit(wt);       //xy1
		Flag[imax+1][j][0] = getwallbit(wb);       //xy0
		Flag[imax+1][j][kmax+1] = getwallbit(wt);  //xy1
	}
	for (i=0; i<imax+2; i++) {
		Flag[i][0][0] = getwallbit(wf);            //xy0
		Flag[i][jmax+1][0] = getwallbit(wh);       //xy0
		Flag[i][0][kmax+1] = getwallbit(wf);       //xy1
		Flag[i][jmax+1][kmax+1] = getwallbit(wh);  //xy1
	}

	//set outer boundary flags - top and bottom:
	for (i=1; i<=imax; i++){
		for (j=1; j<=jmax; j++){
			if ((Flag[i][j][1]&pow2(2,12))==(Flag[i][j][1]&pow2(2,13))) {//if our only inner cell neighbour is a boundary(obstacle), we set this cell to the same kind of boundary.
				Flag[i][j][0] = getwallbit(0)/*3*(pow(2,10)+pow(2,8)+pow(2,6)+pow(2,4)+4+1)*/ + (Flag[i][j][1]&(pow2(2,12)*15)/*info bout the boundary at the beginning*/);
			} else { //Otherwise set it to what you read in the picture (<- remark: NO. we set it to what we read in the parameter file.). plus subtract/set the neighbouring cell, which seems to be water or air
				Flag[i][j][0] = (getwallbit(wb)-3) + (Flag[i][j][1]&3);
			}
			if ((Flag[i][j][kmax]&pow2(2,12))==(Flag[i][j][kmax]&pow2(2,13))) {//if our only inner cell neighbour is a boundary(obstacle), we set this cell to the same kind of bc.
				Flag[i][j][kmax+1] = getwallbit(0) + (Flag[i][j][kmax]&(pow2(2,12)*15)); /*info bout the boundary at the beginning*/
			} else { //Otherwise set it to what you read in the picture <- remark: NO. we set it to what we read in the parameter file.
				Flag[i][j][kmax+1] = (getwallbit(wt)-12) + (Flag[i][j][kmax]&12);
			}
		}
	}
	//set the outer boundary flags - right and left:
	for (j=1; j<=jmax; j++){
		for (k=1; k<=kmax; k++){
			if ((Flag[1][j][k]&pow2(2,12))==(Flag[1][j][k]&pow2(2,13))) {
				Flag[0][j][k] = getwallbit(0) + (Flag[1][j][k]&(pow2(2,12)*15));
			} else { //Otherwise set it to what you read in the picture <- remark: NO. we set it to what we read in the parameter file.
				Flag[0][j][k] = (getwallbit(wl)- 3*pow2(2,10)) + (Flag[1][j][k]& (3*pow2(2,10)));
			}
			if ((Flag[imax][j][k]&pow2(2,12))==(Flag[imax][j][k]&pow2(2,13))) {
				Flag[imax+1][j][k] = getwallbit(0) + (Flag[imax][j][k]&(pow2(2,12)*15));
			} else {
				Flag[imax+1][j][k] = (getwallbit(wr)-3*pow2(2,8)) + (Flag[imax][j][k]&(3*pow2(2,8)));
			}
		}
	}
	//set the outer boundary flags - front and back:
	for (i=1; i<=imax; i++){
		for (k=1; k<=kmax; k++){
			if ((Flag[i][1][k]&pow2(2,12))==(Flag[i][1][k]&pow2(2,13))) {
				Flag[i][0][k] = getwallbit(0) + (Flag[i][1][k]&(pow2(2,12)*15));
			} else {
				Flag[i][0][k] = (getwallbit(wf)-64*3) + (Flag[i][1][k]&(64*3));
			}
			if ((Flag[i][jmax][k]&pow2(2,12))==(Flag[i][jmax][k]&pow2(2,13))) {
				Flag[i][jmax+1][k] = getwallbit(0) + (Flag[i][jmax][k]&(pow2(2,12)*15));
			} else {
				Flag[i][jmax+1][k] = (getwallbit(wh)-16*3) + (Flag[i][jmax][k]&(16*3));
			}
		}
	}
/*
	for(i = 0; i <= imax+1; i++) {
		for(j = 0; j <= jmax+1; j++) {
			for(k = 0; k <= kmax+1; k++) {
				if(isfluid(Flag[i][j][k])){
					setcelltype(&Flag[i][jmax][k],FLUID);
				}
			}
		}
	}
	*/



	// take care of the case when pressure is given
	/*	for (i=0; i<=imax+1; i++){
		if (presDelta) {
			Flag[i][0] += 32;
			Flag[i][jmax+1] += 32;
		}
	}
	for (j=0; j<=jmax+1; j++){
		if (presDelta) {
			Flag[0][j] += 32;
			Flag[imax+1][j] += 32;
		}
	}*/
}
示例#12
0
int main(int argc, char* argv[]) {
	bool quit		= false;
	bool refresh	= true;
	FILE *f;
	char *filename;
	int  pdx, pdy;

	if (argc < 2) {
		puts("Usage: fbi16 [file]");
		exit(0);
	}
	else
		filename = argv[1];
	
	init();

	image = NULL;
	f = fopen(filename, "rb"); halt_on_error(filename);
	read_pgm(f);
	fclose(f);


	/* center horizontal */
	if (blocks < 640/8)
		sdx = (640/8 - blocks)/2;
	else
		sdx = 0;
	
	/* center verical */
	if (height < 480)
		sdy = (480 - height)/2;
	else
		sdy = 0;

	pdx = pdy = dx = dy = 0;
	while (!quit) {
		if (refresh || pdx != dx || pdy != dy) {
			show_image(dx, dy);
			pdx = dx;
			pdy = dy;
			refresh = false;
		}
		switch (getchar()) {
			case 'q':
			case 'Q':
				quit = true;
				break;

			/* scroll left */
			case 's':
				if (blocks > 640/8) {
					dx += 1;
					if (dx > (blocks - 640/8))
						dx = blocks - 640/8;
				}
				break;
			case 'S':
				if (blocks > 640/8) {
					dx += 2;
					if (dx > (blocks - 640/8))
						dx = blocks - 640/8;
				}
				break;

			/* scroll right */
			case 'a':
				if (blocks > 640/8) {
					dx -= 1;
					if (dx < 0) dx = 0;
				}
				break;
			case 'A':
				if (blocks > 640/8) {
					dx -= 2;
					if (dx < 0) dx = 0;
				}
				break;

			/* scroll down */
			case 'w':
				if (height > 480) {
					dy += 10;
					if (dy > (height - 480))
						dy = height - 480;
				}
				break;
			case 'W':
				if (height > 480) {
					dy += 20;
					if (dy > (height - 480))
						dy = height - 480;
				}
				break;
			
			/* scroll up */
			case 'z':
				if (height > 480) {
					dy -= 10;
					if (dy < 0) dy = 0;
				}
				break;
			case 'Z':
				if (height > 480) {
					dy -= 20;
					if (dy < 0) dy = 0;
				}
				break;

			/* refresh image */
			case '\n':
			case 'r':
			case 'R':
				refresh = true;
				break;

			/* invert image */ 
			case 'i':
			case 'I':
				invert_image();
				refresh = true;
				break;
		}
	}

	clean();
	return EXIT_SUCCESS;
}
void sobel(char * chemin_entree, char * chemin_sortie)
{
	image * im = NULL;
	image * img_sobel = NULL;
	//int w=0,h=0;
	int x=0,y=0;
	double norme_gradient;
	int gx,gy;
	
	im = read_pgm(chemin_entree);
	printf("\n* Creation de la sortie du filtre de Sobel: sobel.pgm (h=%d,w=%d)...\n",im->h,im->w);
	img_sobel = create_image(im->w,im->h,255);

	
	/* TO DO ! */
    
    int a1,a2,a3,b1,b2,b3,c1,c2,c3;
    a1=a2=a3=b1=b2=b3=c1=c2=c3=0;
    int h=im->h;
    int w=im->w;
    
    image* imSobelOut=create_image(im->w, im->h, im->colors);
    double SX[9]={0};
    double SY[9]={0};
    SX[0]=SX[6]=1/2;
    //SX[0]=SX[6]=0;
    SX[3]=2;
    //SX[3]=1/2;
    SX[2]=SX[8]=-1/2;
    //SX[2]=SX[8]=0;
    SX[5]=-2;
    //SX[5]=-1/2;
    SY[0]=SY[2]=1/2;
    //SY[0]=SY[2]=0;
    SY[1]=2;
    //SY[1]=1/2;
    SY[6]=SY[8]=-1/2;
    //SY[6]=SY[8]=0;
    SY[7]=-2;
    //SY[7]=-1/2;
    
    
    /*
     for (int i=0; i<im->h; i++) {
     for (int j=0; j<im->w; j++) {
     if (im->img[j*h+i]<120){
     im->img[j*h+i]=0;
     }
     else{
     imSobelOut->img[j*h+i]=255;
     }
     //imSobelOut->img[j*h+i] =sobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
     }
     }
     write_pgm("test.bmp", im);*/
    
#pragma omp parallel
    {
    
#pragma omp for nowait
    for (int j=1; j<im->w-1; j++) {
        a1=a2=a3=0;
        b1=im->img[(j-1)*h];
        b2=im->img[j*h];
        b3=im->img[(j+1)*h];
        c1=im->img[(j-1)*h+1];
        c2=im->img[j*h+1];
        c3=im->img[(j+1)*h+1];
        imSobelOut->img[j*h] = mySobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
    }
    
#pragma omp for nowait
    for (int j=1; j<im->w-1; j++) {
        int i=h-1;
        a1=im->img[(j-1)*h+(i-1)];
        a2=im->img[j*h+(i-1)];
        a3=im->img[(j+1)*h+(i-1)];
        b1=im->img[(j-1)*h+i];
        b2=im->img[j*h+i];
        b3=im->img[(j+1)*h+i];
        c1=c2=c3=0;
        imSobelOut->img[j*h+i] = mySobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
    }
    
    
#pragma omp for nowait
    for (int i=1; i<im->h-1; i++) {
        int j=0;
        a1=b1=c1=0;
        a2=im->img[j*h+(i-1)];
        a3=im->img[(j+1)*h+(i-1)];
        //b1=im->img[(j-1)*h+i];
        b2=im->img[j*h+i];
        b3=im->img[(j+1)*h+i];
        //c1=im->img[(j-1)*h+i+1];
        c2=im->img[j*h+i+1];
        c3=im->img[(j+1)*h+i+1];
        imSobelOut->img[j*h+i] = mySobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
    }
    
    
#pragma omp for nowait
    for (int i=1; i<im->h-1; i++) {
        int j=w-1;
        a3=b3=c3=0;
        a1=im->img[(j-1)*h+(i-1)];
        a2=im->img[j*h+(i-1)];
        //a3=im->img[(j+1)*h+(i-1)];
        b1=im->img[(j-1)*h+i];
        b2=im->img[j*h+i];
        //b3=im->img[(j+1)*h+i];
        c1=im->img[(j-1)*h+i+1];
        c2=im->img[j*h+i+1];
        //c3=im->img[(j+1)*h+i+1];
        imSobelOut->img[j*h+i] = mySobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
    }
    
        
#pragma omp for nowait
    for (int i=1; i<im->h-1; i++) {
        for (int j=1; j<im->w-1; j++) {
            a1=im->img[(j-1)*h+(i-1)];
            a2=im->img[j*h+(i-1)];
            a3=im->img[(j+1)*h+(i-1)];
            b1=im->img[(j-1)*h+i];
            b2=im->img[j*h+i];
            b3=im->img[(j+1)*h+i];
            c1=im->img[(j-1)*h+i+1];
            c2=im->img[j*h+i+1];
            c3=im->img[(j+1)*h+i+1];
            imSobelOut->img[j*h+i] =mySobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
        }
    }
    
        
#pragma omp for
    for (int i=0; i<im->h; i++) {
#pragma omp for
        for (int j=0; j<im->w; j++) {
            if (imSobelOut->img[j*h+i]<100){
                imSobelOut->img[j*h+i]=0;
            }
            else{
                imSobelOut->img[j*h+i]=255;
            }
            //imSobelOut->img[j*h+i] =sobelFiltre(a1, a2, a3, b1, b2, b3, c1, c2, c3, SX, SY);
        }
    }
    
    //write_pgm("sobelOuptFile.bmp", imSobelOut);
    

    }//end of parallel
    
	write_pgm(chemin_sortie,imSobelOut);

	return;
}
示例#14
0
FloatImage::FloatImage(char *filename)
{
  read_pgm (filename);
}
示例#15
0
int main(int argc, char* argv[]) 
{
  int max_disparity = DEFAULT_MAX_DISPARITY, min_disparity = DEFAULT_MIN_DISPARITY, 
    x_window_size = DEFAULT_WINDOW_SIZE, y_window_size = DEFAULT_WINDOW_SIZE, 
    x_tx_win_size = DEFAULT_TX_WINDOW_SIZE, y_tx_win_size = DEFAULT_TX_WINDOW_SIZE, i,
    border_x, border_y;
  double *scores;
  char basefile[50], outname[50];
  FILE *leftfp, *rightfp, *outfile, *lrfile;
  struct pgm left_image, right_image, disp1;
  struct DISP disparity, left_disparity, lr_valid_left, lr_valid_right;
  pixel *right_rank, *left_rank;
  int offset = 0, lr = 0;
  enum match_type match = SAD;

  if (argc < 2) {
    perror ("Usage: match [-d min_disparity max_disparity] [-m SAD | SSD | NCC | ZSAD | ZSSD | ZNCC | RANK | CENSUS] [-w x_window_size y_window_size] [-t x_tx_win_size y_tx_win_size] [-lr] <base-file> <outputfile>");
    return (1);
  }

  /* process optional args */
  for (i = 1; i < argc - 2; i++) {
    if (strcasecmp (argv[i], "-w") == 0) {
      x_window_size = atoi (argv[++i]);   
      y_window_size = atoi (argv[++i]);  

    } else if (strcasecmp (argv[i], "-t") == 0) {
      x_tx_win_size = atoi (argv[++i]);   
      y_tx_win_size = atoi (argv[++i]);

    } else if (strcasecmp (argv[i], "-o") == 0) { 
      offset = atoi (argv[++i]);

    } else if (strcasecmp (argv[i], "-lr") == 0) {
      lr = 1;
 
    } else if (strcasecmp (argv[i], "-d") == 0) { 
      min_disparity = atoi (argv[++i]);  
      if (min_disparity < MIN_ALLOWED_DISPARITY)
	min_disparity = MIN_ALLOWED_DISPARITY;
      max_disparity = atoi (argv[++i]);
      if (max_disparity > MAX_ALLOWED_DISPARITY)
	max_disparity = MAX_ALLOWED_DISPARITY;

    } else if (strcasecmp (argv[i], "-m") == 0) {
      i++;
      if (strcasecmp (argv[i], "SAD") == 0)
	match = SAD;

      else if (strcasecmp (argv[i], "SSD") == 0) 
	match = SSD;
      
      else if (strcasecmp (argv[i], "NCC") == 0)
	match = NCC;   
  
      else if (strcasecmp (argv[i], "ZSAD") == 0)
	match = ZSAD;

      else if (strcasecmp (argv[i], "ZSSD") == 0) 
	match = ZSSD;
      
      else if (strcasecmp (argv[i], "ZNCC") == 0)
	match = ZNCC;   
  
      else if (strcasecmp (argv[i], "RANK") == 0)
	match = RANK;       

      else if (strcasecmp (argv[i], "CENSUS") == 0)
	match = CENSUS;   

      else {
	perror ("Usage: match [-d max_disparity] [-m SAD | SSD | NCC | ZSAD | ZSSD | ZNCC | RANK] [-w x_window_size y_window_size] [-t x_tx_win_size y_tx_win_size] <base-file> <outputfile>");
	return (1);
      }

    } else { 
      perror ("Usage: match [-d max_disparity] [-m SAD | SSD | NCC | ZSAD | ZSSD | ZNCC | RANK] [-w x_window_size y_window_size] [-t x_tx_win_size y-tx_win_size] <base-file> <outputfile>");
      return (1);
    } /* if */
  } /* for */

  /* Open left image file */
  strcpy (basefile, argv[i]);
  if ((leftfp = fopen (strcat (basefile, "-l.pgm"), "r")) == NULL) {
    perror ("Cannot open file for reading");
    return (2);
  }

  /* Open right image file */
  strcpy (basefile, argv[i]);
  if ((rightfp = fopen (strcat (basefile, "-r.pgm"), "r")) == NULL) {
    perror ("Cannot open file for reading");
    return (2);
  } 

  /* Open output file for writing */
  strcpy (outname, argv[i+1]);
  if ((outfile = fopen (strcat (outname, ".pgm"), "w")) == NULL) {
    perror ("Cannot open file for writing");
    return (3);
  }

  /* Open output file for writing */
  strcpy (outname, argv[i+1]);
  if ((lrfile = fopen (strcat (outname, "_lr.pgm"), "w")) == NULL) {
    perror ("Cannot open file for writing");
    return (3);
  }

  if ((read_pgm (leftfp, &left_image) == 0) && (read_pgm (rightfp, &right_image) == 0)) {
    
    if ((left_image.width != right_image.width) || (left_image.height != right_image.height)) {
      perror ("Images different sizes");
      return (4);
    }
	
    disparity.width = left_image.width;
    disparity.height = left_image.height;
    disparity.max_grey = left_image.max_grey;
    if ((disparity.bitmap = (signed char*) calloc (disparity.width * disparity.height, 1)) == NULL) {
      perror ("Not enough memory");
      return (5);
    }
    if ((scores = (double*) calloc (disparity.width * disparity.height, sizeof(double))) == NULL) {
      perror ("Not enough memory");
      return (5);
    }

    if (lr) { 
      lr_valid_left.width = lr_valid_right.width = left_disparity.width = left_image.width;
      lr_valid_left.height = lr_valid_right.height = left_disparity.height = left_image.height;
      lr_valid_left.max_grey = lr_valid_right.max_grey = left_disparity.max_grey = left_image.max_grey;
      if ((left_disparity.bitmap = (signed char*) calloc (left_disparity.width * left_disparity.height, 1)) == NULL) {
	perror ("Not enough memory");
	return (5);
      }
      if ((lr_valid_right.bitmap = (signed char*) calloc (lr_valid_right.width * lr_valid_right.height, 1)) == NULL) {
	perror ("Not enough memory");
	return (5);
      }
      if ((lr_valid_left.bitmap = (signed char*) calloc (lr_valid_left.width * lr_valid_left.height, 1)) == NULL) {
	perror ("Not enough memory");
	return (5);
      }
    } /* left_right */

    switch (match) {
    case (SAD):  
      puts ("SAD");
      match_SAD_right (left_image.bitmap, right_image.bitmap, disparity.bitmap, scores, left_image.width,
		       left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_image.bitmap, right_image.bitmap, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2;
	border_y = y_window_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      break;

    case (SSD): 
      puts ("SSD");
      match_SSD_right (left_image.bitmap, right_image.bitmap, disparity.bitmap, scores, left_image.width,
		       left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_image.bitmap, right_image.bitmap, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2;
	border_y = y_window_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      break;

    case (NCC):
      puts ("NCC");
      match_NCC_right (left_image.bitmap, right_image.bitmap, disparity.bitmap, scores, left_image.width,
		       left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_image.bitmap, right_image.bitmap, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2;
	border_y = y_window_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      break;

    case(ZSAD):
      puts ("ZSAD");
      match_ZSAD_right (left_image.bitmap, right_image.bitmap, disparity.bitmap, scores, left_image.width,
		       left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_image.bitmap, right_image.bitmap, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2;
	border_y = y_window_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      break;

    case (ZSSD): 
      puts ("ZSSD");
      match_ZSSD_right (left_image.bitmap, right_image.bitmap, disparity.bitmap, scores, left_image.width,
		       left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_image.bitmap, right_image.bitmap, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2;
	border_y = y_window_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      break;

    case (ZNCC):
      puts ("ZNCC");
      match_ZNCC_right (left_image.bitmap, right_image.bitmap, disparity.bitmap, scores, left_image.width,
		       left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_image.bitmap, right_image.bitmap, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2;
	border_y = y_window_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      break;

    case (RANK): 
      puts ("RANK");
      if (((right_rank = (pixel*) calloc (disparity.width * disparity.height, 1)) == NULL) ||
		   ((left_rank = (pixel*) calloc (disparity.width * disparity.height, 1)) == NULL)) {
         perror ("Not enough memory");
	 return (5);
      }
      rank_transform (right_image.bitmap, x_tx_win_size, y_tx_win_size, right_image.width, right_image.height, right_rank);
      rank_transform (left_image.bitmap, x_tx_win_size, y_tx_win_size, left_image.width, left_image.height, left_rank);

      match_SAD_right (left_rank, right_rank, disparity.bitmap, scores, left_image.width,
		       left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_rank, right_rank, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2 + x_tx_win_size / 2;
	border_y = y_window_size / 2 + y_tx_win_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      free (right_rank);
      free (left_rank);
      break; 

    case (CENSUS): 
      puts ("CENSUS");
      CENSUS_RIGHT (left_image.bitmap, right_image.bitmap, disparity.bitmap, scores, left_image.width, 
		    left_image.height, x_tx_win_size, y_tx_win_size, x_window_size, y_window_size, min_disparity, max_disparity);
      if (lr) {
	match_SAD_left (left_rank, right_rank, left_disparity.bitmap, scores, left_image.width, 
			left_image.height, x_window_size, y_window_size, min_disparity, max_disparity);
	border_x = x_window_size / 2 + x_tx_win_size / 2;
	border_y = y_window_size / 2 + y_tx_win_size / 2;
	left_right (left_disparity.bitmap, disparity.bitmap, lr_valid_left.bitmap, lr_valid_right.bitmap,
		    left_image.width, left_image.height, border_x, border_y);
      }
      break;
    } /* switch */

    /* Copy disparity to unsigned char and write to file */
    disp1.width = disparity.width;
    disp1.height = disparity.height;
    disp1.max_grey = disparity.max_grey;
    disp1.bitmap = (unsigned char*) calloc (disp1.width*disp1.height,sizeof(unsigned char));
    for (i=0; i<disp1.height*disp1.width; i++)
	*(disp1.bitmap+i) = *(disparity.bitmap+i) + min_disparity;
    write_pgm_binary (outfile, &disp1);

    /* Copy lr information to unsigned char and write to file */
    disp1.width = lr_valid_right.width;
    disp1.height = lr_valid_right.height;
    disp1.max_grey = lr_valid_right.max_grey;
    for (i = 0; i < disp1.height*disp1.width; i++)
	*(disp1.bitmap+i) = *(lr_valid_right.bitmap+i) * 255;
    write_pgm_binary (lrfile, &disp1);

    free (left_image.bitmap);
    free (right_image.bitmap);
    free (disparity.bitmap);
    free (disp1.bitmap);
    free (scores);
    if (lr) {
      free (left_disparity.bitmap);
      free (lr_valid_left.bitmap);
      free (lr_valid_right.bitmap);
    }
  } /* if */
				
  fclose (leftfp);
  fclose (rightfp);
  fclose (outfile);
  fclose (lrfile);

  return (0);
} /* main */
示例#16
0
void pgm_init()
{
  read_pgm("/dev/shm/1.pgm",0);
  read_pgm("/dev/shm/2.pgm",1);
}
示例#17
0
int main(int argc, char *argv[])
{
  dungeon_t d;
  time_t seed;
  struct timeval tv;
  uint32_t i;
  uint32_t do_load, do_save, do_seed, do_image;
  uint32_t long_arg;
  char *save_file;
  char *pgm_file;

  memset(&d, 0, sizeof (d));

  /* Default behavior: Seed with the time, generate a new dungeon, *
   * and don't write to disk.                                      */
  do_load = do_save = do_image = 0;
  do_seed = 1;
  save_file = NULL;
  d.max_monsters = 10;

  /* The project spec requires '--load' and '--save'.  It's common  *
   * to have short and long forms of most switches (assuming you    *
   * don't run out of letters).  For now, we've got plenty.  Long   *
   * forms use whole words and take two dashes.  Short forms use an *
`   * abbreviation after a single dash.  We'll add '--rand' (to     *
   * specify a random seed), which will take an argument of it's    *
   * own, and we'll add short forms for all three commands, '-l',   *
   * '-s', and '-r', respectively.  We're also going to allow an    *
   * optional argument to load to allow us to load non-default save *
   * files.  No means to save to non-default locations, however.    *
   * And the final switch, '--image', allows me to create a dungeon *
   * from a PGM image, so that I was able to create those more      *
   * interesting test dungeons for you.                             */
 
 if (argc > 1) {
    for (i = 1, long_arg = 0; i < argc; i++, long_arg = 0) {
      if (argv[i][0] == '-') { /* All switches start with a dash */
        if (argv[i][1] == '-') {
          argv[i]++;    /* Make the argument have a single dash so we can */
          long_arg = 1; /* handle long and short args at the same place.  */
        }
        switch (argv[i][1]) {
        case 'r':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-rand")) ||
              argc < ++i + 1 /* No more arguments */ ||
              !sscanf(argv[i], "%lu", &seed) /* Argument is not an integer */) {
            usage(argv[0]);
          }
          do_seed = 0;
          break;
        case 'l':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-load"))) {
            usage(argv[0]);
          }
          do_load = 1;
          if ((argc > i + 1) && argv[i + 1][0] != '-') {
            /* There is another argument, and it's not a switch, so *
             * we'll treat it as a save file and try to load it.    */
            save_file = argv[++i];
          }
          break;
        case 's':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-save"))) {
            usage(argv[0]);
          }
          do_save = 1;
          break;
        case 'i':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-image"))) {
            usage(argv[0]);
          }
          do_image = 1;
          if ((argc > i + 1) && argv[i + 1][0] != '-') {
            /* There is another argument, and it's not a switch, so *
             * we'll treat it as a save file and try to load it.    */
            pgm_file = argv[++i];
          }
          break;
        case 'n':
          if ((!long_arg && argv[i][2]) ||
              (long_arg && strcmp(argv[i], "-nummon")) ||
              argc < ++i + 1 /* No more arguments */ ||
              !sscanf(argv[i], "%hu", &d.max_monsters)) {
            usage(argv[0]);
          }
          break;
        default:
          usage(argv[0]);
        }
      } else { /* No dash */
        usage(argv[0]);
      }
    }
  }

  if (do_seed) {
    /* Allows me to generate more than one dungeon *
     * per second, as opposed to time().           */
    gettimeofday(&tv, NULL);
    seed = (tv.tv_usec ^ (tv.tv_sec << 20)) & 0xffffffff;
  }

  printf("Seed is %ld.\n", seed);
  srand(seed);

  io_init_terminal();
  init_dungeon(&d);

  if (do_load) {
    read_dungeon(&d, save_file);
  } else if (do_image) {
    read_pgm(&d, pgm_file);
  } else {
    gen_dungeon(&d);
  }

  config_pc(&d);
  gen_monsters(&d, d.max_monsters, 0);

  io_display(&d);
  while (pc_is_alive(&d) && dungeon_has_npcs(&d) && !d.save_and_exit) {
    do_moves(&d);
    if (!pc_is_alive(&d)) {
      break;
    }
  }
  io_display(&d);
  if (!d.save_and_exit) {
    sleep(2);
  }

  io_reset_terminal();

  if (do_save) {
    write_dungeon(&d);
  }

  printf(pc_is_alive(&d) ? victory : tombstone);

  /* PC can't be deleted with the dungeon, else *
   * it disappears when we use the stairs.      */
  if (pc_is_alive(&d)) {
    /* If the PC is dead, it's in the move heap and will get automatically *
     * deleted when the heap destructs.  In that case, we can't call       *
     * delete_pc(), because it will lead to a double delete.               */
    delete_pc(d.pc);
  }
  delete_dungeon(&d);

  return 0;
}
示例#18
0
int main(int argc, char **argv)
{
    
	if (argc != 8) {
		printf("Usage: ./tema3 mod_vect mod_dma num_spus in.pgm out.cmp out.pgm results.txt");
		return -1;
    }
	int mod_vect = atoi(argv[1]);
	int mod_dma = atoi(argv[2]);
	int num_spus = atoi(argv[3]);
	char *inpgm = argv[4];
	char *outcmp = argv[5];
	char *outpgm = argv[6];
	char *results = argv[7];
	int i;

	struct img initial_image, decompressed_image;
	struct c_img compressed_image;

	struct timeval start_total, end_total, start_op, end_op;
	double total_time = 0, op_time = 0;

	gettimeofday(&start_total, NULL);

	// citeste imaginea initiala 
	read_pgm(inpgm, &initial_image);

	gettimeofday(&start_op, NULL);
	
	compressed_image.width = initial_image.width;
	compressed_image.height = initial_image.height;
	int nr_cmp_blocks = (1LL * initial_image.width * initial_image.height) / (BLOCK_SIZE * BLOCK_SIZE);
	compressed_image.blocks = (struct block *)malloc_align(nr_cmp_blocks * sizeof(struct block), 7);

	pthread_t *compress_threads = (pthread_t*)malloc_align(num_spus * sizeof(pthread_t), 7);
	struct package_t *cthread_arg = (struct package_t *)malloc_align(num_spus * sizeof(struct package_t), 7);
	
	int nr_of_blocks = (initial_image.width * initial_image.height) / (BLOCK_SIZE * BLOCK_SIZE);
	int average_blocks = nr_of_blocks / num_spus;
	int rest_blocks = nr_of_blocks % num_spus;
	int offset = 0;

	for(i = 0; i < num_spus; i++) { 

		/* completeaza structura package_t de trimis la spu pentru fiecare spu*/	
		cthread_arg[i].action_type = 0;
		cthread_arg[i].mod_vect = mod_vect;
		cthread_arg[i].mod_dma = mod_dma;
		cthread_arg[i].num_spus = num_spus;
		cthread_arg[i].nr_blocks = average_blocks;
		cthread_arg[i].index_block = offset;
			
		cthread_arg[i].img_pgm.width = initial_image.width;
		cthread_arg[i].img_pgm.height = initial_image.height;
		cthread_arg[i].img_pgm.pixels = initial_image.pixels + ((offset / (initial_image.width / BLOCK_SIZE)) * BLOCK_SIZE * initial_image.width + (offset % (initial_image.width / BLOCK_SIZE)) * BLOCK_SIZE);
				
		cthread_arg[i].img_cmp.width = compressed_image.width;
		cthread_arg[i].img_cmp.height = compressed_image.height;
		cthread_arg[i].img_cmp.blocks = compressed_image.blocks + ((offset / (initial_image.width / BLOCK_SIZE)) * (initial_image.width / BLOCK_SIZE) + (offset % (initial_image.width / BLOCK_SIZE)));

		offset += average_blocks;
		nr_of_blocks -= average_blocks;
		if (rest_blocks != 0 && i != num_spus - 1) {
			average_blocks = nr_of_blocks / (num_spus - 1 - i);
			rest_blocks = nr_of_blocks % (num_spus - 1 - i);
		}

		/* Create thread for each SPE context */
		if (pthread_create (&compress_threads[i], NULL, &ppu_pthread_function, &cthread_arg[i]))  {
			perror ("Failed creating thread");
			exit (1);
		}
	}

	/* Wait for SPU-thread to complete execution.  */
  	for (i = 0; i < num_spus; i++) {
		if (pthread_join (compress_threads[i], NULL)) {
			perror("Failed pthread_join");
			exit (1);
		}
	}

  	free_align(compress_threads);
	free_align(cthread_arg);
 
	decompressed_image.width = initial_image.width;
	decompressed_image.height = initial_image.height;
	int nr_dec_blocks = (1LL * initial_image.width * initial_image.height) / (BLOCK_SIZE * BLOCK_SIZE);
	decompressed_image.pixels = (unsigned char *)malloc_align(initial_image.height * initial_image.width * sizeof(unsigned char), 7);

	pthread_t *decompress_threads = (pthread_t*)malloc_align(num_spus * sizeof(pthread_t), 7);
	struct package_t *dthread_arg = (struct package_t *)malloc_align(num_spus * sizeof(struct package_t), 7);
	
	int dec_average_blocks = nr_dec_blocks / num_spus;
	int dec_rest_blocks = nr_dec_blocks % num_spus;
	int dec_offset = 0;

	for(i = 0; i < num_spus; i++) { 

		/* completeaza structura package_t de trimis la spu pentru fiecare spu*/	
		dthread_arg[i].action_type = 1;
		dthread_arg[i].mod_vect = mod_vect;
		dthread_arg[i].mod_dma = mod_dma;
		dthread_arg[i].num_spus = num_spus;
		dthread_arg[i].nr_blocks = dec_average_blocks;
		dthread_arg[i].index_block = dec_offset;
			
		dthread_arg[i].img_pgm.width = initial_image.width;
		dthread_arg[i].img_pgm.height = initial_image.height;
		dthread_arg[i].img_pgm.pixels = decompressed_image.pixels + ((dec_offset / (initial_image.width / BLOCK_SIZE)) * BLOCK_SIZE * initial_image.width + (dec_offset % (initial_image.width / BLOCK_SIZE)) * BLOCK_SIZE);
				
		dthread_arg[i].img_cmp.width = compressed_image.width;
		dthread_arg[i].img_cmp.height = compressed_image.height;
		dthread_arg[i].img_cmp.blocks = compressed_image.blocks + ((dec_offset / (initial_image.width / BLOCK_SIZE)) * (initial_image.width / BLOCK_SIZE) + (dec_offset % (initial_image.width / BLOCK_SIZE)));

		dec_offset += dec_average_blocks;
		nr_dec_blocks -= dec_average_blocks;
		if (dec_rest_blocks != 0 && i != num_spus - 1) {
			dec_average_blocks = nr_dec_blocks / (num_spus - 1 - i);
			dec_rest_blocks = nr_dec_blocks % (num_spus - 1 - i);
		}

		/* Create thread for each SPE context */
		if (pthread_create (&decompress_threads[i], NULL, &ppu_pthread_function, &dthread_arg[i]))  {
			perror ("Failed creating thread");
			exit (1);
		}
	}

	/* Wait for SPU-thread to complete execution.  */
  	for (i = 0; i < num_spus; i++) {
		if (pthread_join (decompress_threads[i], NULL)) {
			perror("Failed pthread_join");
			exit (1);
		}
	}
	gettimeofday(&end_op, NULL);

	write_cmp(outcmp, &compressed_image);
	write_pgm(outpgm, &decompressed_image);
	
	free_align(compressed_image.blocks);
	free_align(decompressed_image.pixels);
	free_align(decompress_threads);
	free_align(dthread_arg);

	gettimeofday(&end_total, NULL);
	
	total_time += GET_TIME_DELTA(start_total, end_total);
	op_time += GET_TIME_DELTA(start_op, end_op);

	freopen(results, "a+", stdout);
	printf("%i %lf %lf\n", num_spus, op_time, total_time);
	fclose(stdout);

	return 0;
}