예제 #1
0
/***************************************************************************
 ** ChangeValue
 **    This function takes the original array created using inputs from 
 ** the .dat file and, depending on the wind direction, calls the functions
 ** necessary to modify the array and make the oil spread in the right 
 ** direction. It then prints everything.
 **   Inputs : original array, wind direction, interval, and time
 **   Outputs : Simulation
***************************************************************************/
void ChangeValue(int gallons[15][15], int direction, int interval, int time)
{
  int i, j, k;
  
  /*Original Array*/
  printf("Time: 0\n");
  for(i = 0; i < SIZE; i++)
    {
      for(j = 0; j < SIZE; j++)
	{
	  printf("%4d", gallons[i][j]);
	}
      printf("\n");
    }
  CreatePPM(gallons, 0);

  /*****************************************
    Changes Array based on wind direction n
    times where n is the number of frames
  *****************************************/
  for(k = 1; k < time + 1; k++)
    {
      switch(direction)
	{
	case 1:
	  SouthWind(gallons);
	  break;
	case 2:
	  EastWind(gallons);
	  break;
	case 3:
	  NorthWind(gallons);
	  break;
	default:
	  WestWind(gallons);
	  break;
	}

      /*Prints the modified array for every interval within the time*/
      if(k % interval == 0)
	{ 
	  printf("\nTime: %d\n", k);
	  CreatePPM(gallons, k);
	  for(i = 0; i < 15; i++)
	    {
	      for(j = 0; j < 15; j++)
		{
		  printf("%4d", gallons[i][j]);
	     }
	      printf("\n");
	    }
	  printf("\n\n");
	}
    }

  int numFrames = (time / interval) + 1;
  /*Creates animation from the PPM file that is created after each step*/
  ShowOilSpill(numFrames, interval);

  return;
}
예제 #2
0
파일: main.c 프로젝트: jkaba/School
/* MAIN METHOD
 
    This method performs the initial setup before going to the functions
    This method also converts PPM Images to PGM Images
 */
int main(int argc, char** argv){
    
    // Boolean values used to determine which image to make
    bool pbm = false;
    bool pgm = false;
    bool ppm = false;
    char extension[1024];
    
    // If there are not enough arguments then return an error message
    if(argc != 6){
        
        // Message to let the user know what arguments are needed
        printf("Error: This program requires five arguments\n");
        printf("1: Image type (1 for pbm, 2 for pgm, 3 for ppm)\n");
        printf("2: Width of image. (pbm and pgm must be a multiple of 4, ppm a multiple of 6)\n");
        printf("3: Height of image. (must be a multiple of 4)\n");
        printf("4: Output image name\n");
        printf("5: Image format code (0 for ASCII, 1 for Raw)\n");
        
        // Stop the program as there was an error
        return 0;
    }
    
    // Switch case to determine what image to make
    switch (argv[1][0]) {
            
        // If it is 1, we are making a PBM
        case '1':
            pbm = true;
            break;
            
        // If it is 2, we are making a PGM
        case '2':
            pgm = true;
            break;
            
        // If it is 3, we are making a PPM
        case '3':
            ppm = true;
            break;
            
        // If the number is something else, return an error message and stop the program
        default:
            printf("Error: Image type should be either 1(pbm), 2(pgm), or 3(ppm)\n");
            return 0;
    }
    
    // The width is given as the second argument
    width = atoi(argv[2]);
    
    // The height is given as the third argument
    height = atoi(argv[3]);
    
    // The filename is given as the fourth argument
    strcpy(filename, argv[4]);
    
    // Switch case for fifth argument to determine whether the image is ascii or raw
    switch (argv[5][0]) {
            
        // If it is 0, then the image will be saved as an ascii image
        case '0':
            raw = false;
            break;
            
        // If it is 1, then the image will be saved as a raw image
        case '1':
            raw = true;
            break;
            
        // If it is something else, then return an error message and stop the program
        default:
            printf("Error: Image format code should be either 0 (ASCII) or 1(raw)\n");
            return 0;
    }
    
    // If pbm is true, we are making a pbm image
    if(pbm == true){
        
        // Print statement to let user know what image is being made
        printf("PBM image\n");
        
        // Width check to ensure that width is proper
        if(width < 4 || width % 4 != 0){
            
            // If the check fails, then return an error message and stop the program
            printf("Error: Width for pbm must be a multiple of 4\n");
            return 0;
        }
        
        // Height check to ensure that height is proper
        if(height < 4 || height % 4 != 0){
            
            // If the check fails, then return an error message and stop the program
            printf("Error: Height for pbm must be a multiple of 4\n");
            return 0;
        }
        
        // Try to create enough memory for the image
        if(create_PBM_Image(&pbmImage, width, height) != 0){
            
            // If memory couldn't be allocated return an error message and stop the program
            printf("Error: Memory problem creating the PBM Image\n");
            return 0;
        }
        
        // Print statement to let user know image dimensions
        printf("Creating PBM image %d W x %d H\n", width, height);
        
        // Create PBM Image function (Refer to program 1)
        CreatePBM();
        
        // Add .pbm file extension to the filename
        strcat(filename, ".pbm");
        
        // Try to save the image
        if(save_PBM_Image(&pbmImage, filename, raw) != 0){
            
            // If the image couldn't be saved return an error message and stop the program
            printf("An Error occured while trying to save the PBM image to the requested filename\n");
            return 0;
        }
        
        // If the user wanted to save the image as raw
        if(raw == true){
            
            // Print message letting the user know what the raw image is saved as
            printf("PBM Raw image saved as: %s\n", filename);
        }
        else{
            // Print message letting the user know what the ascii image is saved as
            printf("PBM ASCII image saved as: %s\n", filename);
        }
        
        // Free memory
        free_PBM_Image(&pbmImage);
    }
    
    // If pgm is true, we are making a PGM image
    else if(pgm == true){
        
        // Print statement to let the user know what image is being made
        printf("PGM Image\n");
        
        // Width check, to ensure that the width is proper
        if(width < 4 || width % 4 != 0){
            
            // If the width check failed, return an error message and stop the program
            printf("Error: Width for pgm image must be a multiple of 4\n");
            return 0;
        }
        
        // Height check, to ensure that the height is proper
        if(height < 4 || height % 4 != 0){
            
            // If the height check failed, return an error message and stop the program
            printf("Error: Height for pgm image must be a multiple of 4\n");
            return 0;
        }
        
        // Try to create enough memory for the image
        if(create_PGM_Image(&pgmImage, width, height, 255) != 0){
            
            // If memory could not be allocated, return an error message and stop the program
            printf("Error: Memory problem creating the PGM Image\n");
            return 0;
        }
        
        // Print out the dimensions of the image
        printf("Creating PGM image %d W x %d H\n", width, height);
        
        // Create PGM Function (Refer to program 2)
        CreatePGM();
        
        // Add .pgm file extension to the file name
        strcat(filename, ".pgm");
        
        // Try to save the PGM Image
        if(save_PGM_Image(&pgmImage, filename, raw) != 0){
            
            // If the image could not be saved, return an error message and stop the program
            printf("An Error occured while trying to save the PGM image to the requested filename\n");
            return 0;
        }
        
        // If the user wanted to save the image as raw
        if(raw == true){
            
            // Print message letting the user know what the raw image is saved as
            printf("PGM Raw image saved as: %s\n", filename);
        }
        else{
            // Print message letting the user know what the ascii image is saved as
            printf("PGM ASCII image saved as: %s\n", filename);
        }
        // Free memory
        free_PGM_Image(&pgmImage);
    }
    
    // If ppm is true, we are making a PPM Image
    else if(ppm == true){
        
        // Print statement to let the user know what image is being made
        printf("PPM Image\n");
        
        // Width check to ensure width is proper (note: ppm needs a multiple of 6)
        if(width < 6 || width % 6 != 0){
            
            // If width check fails, return an error message and stop the program
            printf("Error: Width for ppm image must be a multiple of 6\n");
            return 0;
        }
        
        // Height check to ensure height is proper
        if(height < 4 || height % 4 != 0){
            
            // If height check fails, return an error message and stop the program
            printf("Error: Height for ppm image must be a multiple of 4\n");
            return 0;
        }
        
        // Try to allocate memory for PPM Image
        if(create_PPM_Image(&ppmImage, width, height, 255) != 0){
            
            // If memory could not be allocated, return an error message and stop the program
            printf("Error: Memory problem creating the PPM Image\n");
            return 0;
        }
        
        // Print out dimensions for the image
        printf("Creating PPM image %d W x %d H\n", width, height);
        
        // Create PPM Function (Refer to program 3)
        CreatePPM();
        
        // Add .ppm file extension to the file name
        strcat(filename, ".ppm");
        
        // Try to save the PPM Image
        if(save_PPM_Image(&ppmImage, filename, raw) != 0){
            
            // If the image could not be saved, then return an error message and stop the program
            printf("An Error occured while trying to save the PPM image to the requested filename\n");
            return 0;
        }
        
        // If the user wanted to save the image as raw
        if(raw == true){
            
            // Print message letting the user know what the raw image is saved as
            printf("PPM Raw image saved as: %s\n", filename);
        }
        else{
            // Print message letting the user know what the ascii image is saved as
            printf("PPM ASCII image saved as: %s\n", filename);
        }
        
        // convert and store PPM into 3 PGM through copy_PPM_to_PGM
        printf("Copying PPM Image to PGM Format\n");
        
        // Allocate memory for the pgm conversion of the ppm image
        if(create_PGM_Image(&pgmImage, width, height, 255) != 0){
            
            // If memory could not be allocated, then return an error message and stop the program
            printf("Error: Memory problem creating the PGM Image Copy\n");
            return 0;
        }
        
        // Print out PGM copy dimensions
        printf("Creating PGM image copy %d W x %d H\n", width, height);
        
        // Converting ppm into 3 pgm, converting one color at a time
        for(color = RED; color <= BLUE; color++){
            
            // Copy PPM to PGM with current color from the loop
            if(copy_PPM_to_PGM(&ppmImage, &pgmImage, color) != 0){
                
                // If there was an error converting, return an error message and stop the program
                printf("Error: converting from PPM to PGM\n");
                return 0;
            }
            
            // Filename setup for the copies
            strcpy(filename, argv[4]);
            strcat(filename, ".ppm.");
            strcat(filename, colorType[color]);
            strcat(filename, ".copy2PGM");
            strcat(filename, ".pgm");
            
            // Try to save the converted pgm image
            if(save_PGM_Image(&pgmImage, filename, raw) != 0){
                
                // If the PGM cannot be saved, return an error message and stop the program
                printf("An Error occured while trying to save the converted PGM image to the requested filename\n");
                return 0;
            }
            
            // If the user wanted to save the image as raw
            if(raw == true){
                
                // Print message letting the user know what the raw image is saved as
                printf("PGM Raw image saved as: %s\n", filename);
            }
            else{
                // Print message letting the user know what the ascii image is saved as
                printf("PGM ASCII image saved as: %s\n", filename);
            }
        }
        // Free memory used for the converted image
        free_PGM_Image(&pgmImage);
        
        // Free memory used for the PPM image
        free_PPM_Image(&ppmImage);
    }
    
    // Program reached end successfully, return 0
    return 0;
}