/*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);
  }

上面是我的CSC类的编程项目项目的第一部分是通过使用整数值将ppm图像转换为pgm(灰度缩放)这一部分在我的图像编辑器中非常适合我。
第二部分是通过设置这些整数值字符来使用更少的空间但是我现在遇到了一个问题,图像显示不正确它仍然是灰色的,但与第一幅不同今天早上,我的教授谈到了一个错误,即循环并没有跳过新行,但是当我试图编辑while循环中的fscanf以显示为新图像并对其进行注释时,编译器抱怨分割失败。
顺便说一下,我们在程序中输入一个.ppm文件,然后输出一个新的.pgm值。
有人能帮我看看我代码中的问题吗谢谢!

最佳答案

输出新的颜色数据时,包含一个\n字符

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

当它应该是一个连续的块所以应该是这样
fprintf(stdout,"%hhu",color);

关于c - %c在while循环中保留换行符,修复吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42347602/

10-11 23:22
查看更多