# include # include # include # include "ppc_array.h" # include "ppc_image.h" # include "ppc_netpbm_io.h" # include "ppc_xmalloc.h" /******************************************************************************/ void free_image ( struct image *img ) /******************************************************************************/ /* Purpose: free_image() frees memory associated with a netpbm image. Licensing: This code is distributed under the MIT license. Modified: 14 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct image *img: the image information. */ { if ( img != NULL ) { free_matrix ( img->r ); free_matrix ( img->g ); free_matrix ( img->b ); free ( img ); } return; } /******************************************************************************/ struct image *read_image ( char *filename ) /******************************************************************************/ /* Purpose: read_image() reads an image from a netpbm() file. Licensing: This code is distributed under the MIT license. Modified: 14 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: char *filename: the name of the file containing image information. Output: struct image *img: the image information. */ { struct image *img = xmalloc ( sizeof *img ); struct pam *pam = &img -> pam; FILE *fp = pm_openr ( filename ); pnm_readpaminit ( fp, pam, sizeof ( struct pam ) ); /* pnm_readpaminit ( fp, pam, PAM_STRUCT_SIZE ( tuple_type ) ); */ if ( pam->format == PGM_FORMAT || pam->format == RPGM_FORMAT ) { read_pgm_pixel_data ( img ); } else if ( pam->format == PPM_FORMAT || pam->format == RPPM_FORMAT ) { read_ppm_pixel_data ( img ); } else { fprintf ( stderr, "\n" ); fprintf ( stderr, "read_image(): Fatal error!\n" ); fprintf ( stderr, " Input file is not PGM or PPM format.\n" ); exit ( EXIT_FAILURE ); } pm_close ( fp ); return img; } /******************************************************************************/ void read_pgm_pixel_data ( struct image *img ) /******************************************************************************/ /* Purpose: read_pgm_pixel_data() reads the information from a PGM image. Licensing: This code is distributed under the MIT license. Modified: 15 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct image *img: the image information. Output: struct image *img: the image information, with the pixel information in g. */ { int i; int j; struct pam *pam = &img -> pam; tuple *row = pnm_allocpamrow ( pam ); make_matrix ( img->g, pam->height, pam->width ); img->r = NULL; img->b = NULL; for ( i = 0; i < pam->height; i++ ) { pnm_readpamrow ( pam, row ); for ( j = 0; j < pam->width; j++ ) { img->g[i][j] = row[j][0]; } } pnm_freepamrow ( row ); return; } /******************************************************************************/ void read_ppm_pixel_data ( struct image *img ) /******************************************************************************/ /* Purpose: read_ppm_pixel_data() reads the information from a PPM image. Licensing: This code is distributed under the MIT license. Modified: 15 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct image *img: the image information. Output: struct image *img: the image information, with the pixel information in r, g, b. */ { int i; int j; struct pam *pam = &img -> pam; tuple *row = pnm_allocpamrow ( pam ); make_matrix ( img->r, pam->height, pam->width ); make_matrix ( img->g, pam->height, pam->width ); make_matrix ( img->b, pam->height, pam->width ); for ( i = 0; i < pam->height; i++ ) { pnm_readpamrow ( pam, row ); for ( j = 0; j < pam->width; j++ ) { img->r[i][j] = row[j][0]; img->g[i][j] = row[j][1]; img->b[i][j] = row[j][2]; } } pnm_freepamrow ( row ); return; } /******************************************************************************/ void write_image ( char *filename, struct image *img ) /******************************************************************************/ /* Purpose: write_image() writes netpbm image information to a file. Licensing: This code is distributed under the MIT license. Modified: 14 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: char *filename: the name of the file containing image information. struct image *img: the image information. */ { struct pam *pam = &img->pam; pam->file = pm_openw ( filename ); if ( pam->format == PGM_FORMAT || pam->format == PPM_FORMAT ) { pam->plainformat = 1; } else { pam->plainformat = 0; } pnm_writepaminit ( pam ); if ( pam->format == PGM_FORMAT || pam->format == RPGM_FORMAT ) { write_pgm_pixel_data ( img ); } else if ( pam->format == PPM_FORMAT || pam->format == RPPM_FORMAT ) { write_ppm_pixel_data ( img ); } else { fprintf ( stderr, "\n" ); fprintf ( stderr, "write_image(): Fatal error!\n" ); fprintf ( stderr, " Input file is not PGM or PPM format.\n" ); exit ( EXIT_FAILURE ); } pm_close ( pam->file ); return; } /******************************************************************************/ void write_pgm_pixel_data ( struct image *img ) /******************************************************************************/ /* Purpose: write_pgm_pixel_data() writes netpbm image information to a PGM file. Licensing: This code is distributed under the MIT license. Modified: 15 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct image *img: the image information. */ { int i; int j; struct pam *pam = &img->pam; tuple *row = pnm_allocpamrow ( pam ); for ( i = 0; i < pam->height; i++ ) { for ( j = 0; j < pam->width; j++ ) { row[j][0] = img->g[i][j]; } pnm_writepamrow ( pam, row ); } pnm_freepamrow ( row ); return; } /******************************************************************************/ void write_ppm_pixel_data ( struct image *img ) /******************************************************************************/ /* Purpose: write_ppm_pixel_data() writes netpbm image information to a PPM file. Licensing: This code is distributed under the MIT license. Modified: 15 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct image *img: the image information. */ { int i; int j; struct pam *pam = &img->pam; tuple *row = pnm_allocpamrow ( pam ); for ( i = 0; i < pam->height; i++ ) { for ( j = 0; j < pam->width; j++ ) { row[j][0] = img->r[i][j]; row[j][1] = img->g[i][j]; row[j][2] = img->b[i][j]; } pnm_writepamrow ( pam, row ); } pnm_freepamrow ( row ); return; }