/*Title: P3.c
Location: ~/csc1710/P3/P3.c
Class: CSC1710
Name: Emily
Date: 02-18-2017
This program is meant to convert a colored image to black and white.*/
#include<stdio.h>
#include <string.h>
int main(void)
{
int height, width;
char red;
char green;
char blue;
char imageType[3];
char comment[256];
char newlinechar;
int mcc;
unsigned char color;
//scan and print for image type
fscanf(stdin,"%[^\n]%c",imageType,&newlinechar);
strncpy(imageType,"P2",3);
fprintf(stdout,"%s\n",imageType);
//scan and print for comment
fscanf(stdin,"%[^\n]%c",comment,&newlinechar);
strncpy(comment,"#EMILY MORAN Black and White Conversion Routine, Version 0.1",256);
fprintf(stdout,"%s\n",comment);
//scan and print for height and width
fscanf(stdin,"%d %d",&width,&height);
fprintf(stdout,"%d %d\n",width,height);
//scan and print for max color code
fscanf(stdin,"%d",&mcc);
fprintf(stdout,"%d\n",mcc);
//while loop for RGB numbers
while(fscanf(stdin," %c", ,&red)!=EOF){
fscanf(stdin,"%c",&green);
fscanf(stdin,"%c",&blue);
color =(unsigned char)0.3*red+0.5*green+0.2*blue;
fprintf(stdout,"%hhu\n",color);
}
return(0);
}
Above is the programming project for my CSC class. The first part of the project was to covert a ppm image to a pgm (grey-scaling it) by using integer values. This part works perfectly for me in my image editor.
The second part was to use less space by making these integer values characters. However I am now running into a problem where the image is not showing right. It is still grey-scaled, yet not the same image as the first. My professor this morning talked about the bug being that the loop is not skipping the new line, however when I attempt to edit my fscanf's in my while loops to appear as the newImage and comment ones the compiler complains about a segmentation failure.
BTW we are inputting a .ppm file into the program and then outputting a new .pgm value.
Can anyone help me see the problem in my code? Thanks!