Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*** global definitions ***/
#define WIDTH 640 /* Image width */
#define HEIGHT 425 /* image height */
#define SLEN 80 /* maximum length of file names */
/*** function declarations ***/
/* print a menu */
void PrintMenu();
/* read image from a file */
int ReadImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* save a processed image */
int SaveImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* change color image to black & white */
void BlackNWhite(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* reverse image color */
void Negative(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* flip image horizontally */
void HFlip(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* mirror image horizontally */
void HMirror(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* flip image vertically */
void VFlip(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* mirror image vertically */
void VMirror(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/* Test all functions */
void AutoTest(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
/*define your main function here*/
int main(void)
{
int choice;
char fileName [10];
do
{
printf("1: Load a PPM image\n");
printf("2: Save an image in PPM and JPEG format\n");
printf("3: Change a color image to black and white\n");
printf("4: Make a negative of an image\n");
printf("5: Flip an image horizontally\n");
printf("6: Mirror an image vertically\n");
printf("7: Flip an image vertically\n");
printf("8: Mirror an image vertically\n");
printf("9: Test all functions\n");
printf("10: Exit\n");
printf("Please make your choice: \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Please input the file name to load: \n");
scanf("%s",fileName);
if (strcmp(fileName, "sailing") == 0)
{ int returnCode = readImage(fileName, imageR, imageG, imageB);} // is that what you meant?
/* fileName is equal to sailing, do stuff */
}
case 2:
printf("please enter the file name to be saved \n");
scanf("%s",fileName);
case 3:
printf("Black & White operation is done! \n");
break;
}
}
/*Three function definitions are provided:ReadImage, ReadImageW, SaveImage*/
int
ReadImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT])
{
FILE *File;
char Type[SLEN];
int Width, Height, MaxValue;
int x, y;
char ftype[] = ".ppm";
char fname_tmp[SLEN]; /*avoid to modify on the original pointer, 11/10/10, X.Han*/
strcpy(fname_tmp, fname);
strcat(fname_tmp, ftype);
File = fopen(fname_tmp, "r");
if (!File) {
printf("\nCannot open file \"%s\" for reading!\n", fname);
return 1;
}
fscanf(File, "%79s", Type);
if (Type[0] != 'P' || Type[1] != '6' || Type[2] != 0) {
printf("\nUnsupported file format!\n");
return 2;
}
fscanf(File, "%d", &Width);
if (Width != WIDTH) {
printf("\nUnsupported image width %d!\n", Width);
return 3;
}
fscanf(File, "%d", &Height);
if (Height != HEIGHT) {
printf("\nUnsupported image height %d!\n", Height);
return 4;
}
fscanf(File, "%d", &MaxValue);
if (MaxValue != 255) {
printf("\nUnsupported image maximum value %d!\n", MaxValue);
return 5;
}
if ('\n' != fgetc(File)) {
printf("\nCarriage return expected!\n");
return 6;
}
for (y = 0; y < HEIGHT; y++)
for (x = 0; x < WIDTH; x++) {
R[x][y] = fgetc(File);
G[x][y] = fgetc(File);
B[x][y] = fgetc(File);
}
if (ferror(File)) {
printf("\nFile error while reading from file!\n");
return 7;
}
printf("%s was read successfully!\n", fname_tmp);
fclose(File);
return 0;
}
int
SaveImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT])
{
FILE *File;
int x, y;
char SysCmd[SLEN * 5];
char ftype[] = ".ppm";
char fname_tmp[SLEN]; /*avoid to modify on the original pointer, 11/10/10, X.Han*/
char fname_tmp2[SLEN];
strcpy(fname_tmp, fname);
strcpy(fname_tmp2, fname);
strcat(fname_tmp2, ftype);
File = fopen(fname_tmp2, "w");
if (!File) {
printf("\nCannot open file \"%s\" for writing!\n", fname);
return 1;
}
fprintf(File, "P6\n");
fprintf(File, "%d %d\n", WIDTH, HEIGHT);
fprintf(File, "255\n");
for (y = 0; y < HEIGHT; y++)
for (x = 0; x < WIDTH; x++) {
fputc(R[x][y], File);
fputc(G[x][y], File);
fputc(B[x][y], File);
}
if (ferror(File)) {
printf("\nFile error while writing to file!\n");
return 2;
}
fclose(File);
printf("%s was saved successfully. \n", fname_tmp2);
/*
* rename file to image.ppm, convert it to ~/public_html/<fname>.jpg
* and make it world readable
*/
sprintf(SysCmd, "/users/grad2/doemer/eecs22/bin/pnmtojpeg.tcsh %s",
fname_tmp2);
if (system(SysCmd) != 0) {
printf("\nError while converting to JPG:\nCommand \"%s\" failed!\n", SysCmd);
return 3;
}
printf("%s.jpg was stored for viewing. \n", fname_tmp);
return (0);
}
/*define all other functions here*/