Ejemplo n.º 1
0
/*--------------------*/
int copy_PGM(struct PGM_Image * pgmImage, struct PGM_Image * copy)
{ // for loop variables
  int row, col;

  // initialize the copy
  if(create_PGM_Image(copy, pgmImage->width,
                      pgmImage->height, pgmImage->maxGrayValue) == -1)
    return -1;

  // copy the values
  for(row = 0; row < pgmImage->height; row++)
    for(col = 0; col < pgmImage->width; col++)
      copy->image[row][col] = pgmImage->image[row][col];

  // success
  return 0; 
}
Ejemplo n.º 2
0
/*-----------------------------------*/
int copy_PPM_to_PGM(struct PPM_Image * ppmImage,
                    struct PGM_Image * pgmImage, enum Color color)
{ // for loop variables
  int row, col;

  // initialize the pgm image
  if(create_PGM_Image(pgmImage, ppmImage->width,
                      ppmImage->height, ppmImage->maxGrayValue) == -1)
    return -1;

  // copy the values
  for(row = 0; row < ppmImage->height; row++)
    for(col = 0; col < ppmImage->width; col++)
      pgmImage->image[row][col] = ppmImage->image[row][col][color];

  // success
  return 0; 
}
Ejemplo n.º 3
0
/*-----------------------------------*/
int copy_PBM_to_PGM(struct PBM_Image * pbmImage, struct PGM_Image * pgmImage)
{ // for loop variables
  int row, col;

  // initialize the pgm image
  if(create_PGM_Image(pgmImage, pbmImage->width, 
                      pbmImage->height, MAX_GRAY_VALUE) == -1) return -1;

  // copy the values
  for(row = 0; row < pbmImage->height; row++)
    for(col = 0; col < pbmImage->width; col++)
      if(pbmImage->image[row][col] == WHITE)
        pgmImage->image[row][col] = 255;
      else 
        pgmImage->image[row][col] = 0;

  // success
  return 0; 
}
Ejemplo n.º 4
0
/*-----------------------------------------------------------*/
int convert_PPM_to_PGM(struct PPM_Image * ppmImage,
                       struct PGM_Image * pgmImage)
{ // for loop variables
  int row, col;

  // initialize the pgm image
  if(create_PGM_Image(pgmImage, ppmImage->width,
                      ppmImage->height, ppmImage->maxGrayValue) == -1)
    return -1;

  // copy the values
  for(row = 0; row < ppmImage->height; row++)
    for(col = 0; col < ppmImage->width; col++)
      pgmImage->image[row][col] = (unsigned char)
                 ( (0.299 * ppmImage->image[row][col][RED]) 
                 + (0.587 * ppmImage->image[row][col][GREEN]) 
                 + (0.114 * ppmImage->image[row][col][BLUE]));

  // success
  return 0; 
}
Ejemplo n.º 5
0
Archivo: main.c Proyecto: 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;
}