2
/*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!

Emily
  • 23
  • 2
  • By "making these integer values characters", do you mean writing them as binary data? How do you tell the difference between a newline and byte value 10? Something doesn't quite match here. Anyway, if you are using binary data, don't use `fscanf` to read it (I would just use `getc` for something like this, probably). – hyde Feb 20 '17 at 14:57
  • 1
    If you use a debugger (like gdb) it should tell you which line you're getting the segmentation fault on – Chris Turner Feb 20 '17 at 15:01
  • What input are you using? – melpomene Feb 20 '17 at 15:02
  • They are not binary data. Just a list of integer values for red, green, and blue. Spaced as: red green blue after the fourth lin. Also, I believe the segmentation fault in on the same line as the while loop. The input is the .ppm file – Emily Feb 20 '17 at 15:04
  • What is "the .ppm file"? Show your actual data. See [mcve]. – melpomene Feb 20 '17 at 15:06
  • Welcome to Stack Overflow. Please read about creating an MCVE ([MCVE]). You're not checking that all the `scanf()` statements are working but that probably isn't the problem. It isn't clear if your problem is in the loop at the end, but you've not printed the values read. You don't want to do that on megabytes of pixels, but either work on small images or only print the first few iterations worth of data (use a counter; only print the first 10 values). You've not shown us what you have as input, what you get as output, what you expect as output; it is hard to know what's going wrong. – Jonathan Leffler Feb 20 '17 at 15:25
  • I'm sorry but there is no good way for me to actually copy over the copy for you all to see. However, it is something along the lines of P5 \n # a comment \n 475 339 \n 255 \n @ \n ((where @ contains a long list of char values on one line.) – Emily Feb 20 '17 at 15:32
  • 1
    Better to use `while(fscanf(stdin," %c%c%c", ,&red, &green, &blue) == 3){` to read 3 char and be certain of success. – chux - Reinstate Monica Feb 20 '17 at 15:43
  • @chux thank you! That helps to. – Emily Feb 20 '17 at 15:53
  • `color =(unsigned char)0.3*red+0.5*green+0.2*blue;` looks wrong. I'd expect `color =(unsigned char)(0.3*red+0.5*green+0.2*blue);` or more simply `color =(unsigned char)((3*red+5*green+2*blue)/10);` – chux - Reinstate Monica Feb 20 '17 at 17:19

2 Answers2

2

When you're outputting the new colour data, you're including a \n character

fprintf(stdout,"%hhu\n",color);

when it should be a continuous block. So it should be like this

fprintf(stdout,"%hhu",color);
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
  • The P2 at the top of the file should also be changed to P5 as that is the correct file type for the kind of pgm file you're outputting. – Chris Turner Feb 20 '17 at 16:03
  • For anyone who is wondering, my professor and I found the bug and it was that the unsigned char at the end should of been a char. Thank you for all of your help! – Emily Feb 20 '17 at 16:26
0

For anyone who is wondering, my professor and I found the bug and it was that the unsigned char at the end should of been a char. Thank you for all of your help!

Emily
  • 23
  • 2