为什么我的ppm文件中的图像有点偏差?

时间:2013-10-29 06:30:55

标签: c ppm

任务是在800列和600行的图像上创建俄罗斯国旗的图像。因此,旗帜分为三个相等的部分(白色在顶部,蓝色在中间,红色在底部)

这是我的代码:

#include <stdio.h>

int main() {
   printf("P6\n");
   printf("%d %d\n", 800, 600);
   printf("255\n");

   int width, heightWhite, heightBlue, heightRed, i, j;
   unsigned char Rcolor, Bcolor, Gcolor;

   width = 800;
   heightWhite = 200;
   heightBlue = 400;
   heightRed = 600;

   for (j = 0; j <= heightWhite; j++) {
      for (i = 0; i <= width; i++) {
         Rcolor = 255;
         Bcolor = 255;
         Gcolor = 255;

         printf("%c%c%c", Rcolor, Gcolor, Bcolor);
      }
   }

   for (j = 201; j <= heightBlue; j++) {
      for (i = 0; i <= width; i++) {
         Rcolor = 0;
         Bcolor = 255;
         Gcolor = 0;

         printf("%c%c%c", Rcolor, Gcolor, Bcolor);
      }
   }

   for (j = 401; j <= heightRed; j++) {
      for (i = 0; i <= width; i++) {
         Rcolor = 255;
         Bcolor = 0;
         Gcolor = 0;

         printf("%c%c%c", Rcolor, Gcolor, Bcolor);
      }
   }

   return (0);
}

但是当我看着我的程序生成的图像时,我注意到蓝色和红色条的顶部并不是完全水平的(它看起来像是蓝色和红色条的顶部的行的一部分是高于前面的像素)我无法弄清楚为什么我得到这个。我已经在Gimp上运行了我的讲师的ppm文件(这是我用来查看ppm文件)并且线条应该是完全水平的。有任何想法吗?

(我不知道如何附加我的ppm文件,但这里看起来应该是这样的:http://users.csc.calpoly.edu/~dekhtyar/101-Fall2013/labs/lab7.html)(这是第一个标志)

1 个答案:

答案 0 :(得分:0)

每行打印一个像素(每种颜色的最后一行打印200像素)。

更改

for (i = 0; i <= width; i++) {

for (i = 0; i < width; i++) {

编辑:

但我怎么能说“&lt; =”为高度?

for (j = 0; j < heightWhite; j++)  = 0...199 = 200 items
for (j = 1; j <= heightWhite; j++) = 1...200 = 200 items

请注意,所有代码都可以通过两个循环执行:

#include <stdio.h>

int main(void)
{
   int width = 800, height = 600, icolor = 0, i, j;
   unsigned char color[][3] = {
      {255, 255, 255}, /* white */
      {0, 0, 255},     /* blue */
      {255, 0, 0}      /* red */
   };

   printf("P6\n");
   printf("%d %d\n", width, height);
   printf("255\n");

   for (i = 0; i < height; i++) {
      for (j = 0; j < width; j++) {
         printf("%c%c%c", color[icolor][0], color[icolor][1], color[icolor][2]);
      }
      if (i && (i % 200 == 0)) icolor++; /* 200, 400, 600 */
   }
   return 0;
}
相关问题