【问题标题】:Program reprinting image after going through Mirror function程序通过镜像功能重印图像
【发布时间】:2014-11-29 05:24:16
【问题描述】:

我已经尝试了几天来弄清楚为什么我的程序只是重新打印输入图像。我知道我的其他功能可以工作,但由于某种原因,这个功能让我很困惑,我尝试了所有我能想到的移动像素的方法,但我尝试过的绝对没有任何效果。

标题:

    #include <stdio.h>
    #include <stdlib.h>

    struct pixel {
        char r, g, b;
};

int g_width, g_height;

void parseHeader( FILE *input );
void parseImage( FILE *input, struct pixel *theArray );
void print(struct pixel a[]);
void my_Mirror(struct pixel a[]);
void rotate(struct pixel a[]);
void my_Flip(struct pixel a[]);

主要:

#include "transform.h"


int main (int argc, char *argv[])  {

        // declarations here
   FILE *inFile;


        // open input file 

   inFile = fopen(argv[2],"r");
   if (inFile == NULL)
   {         
      fprintf(stderr, "File open error. Exiting program\n");
      exit(1);
   }


        // parseHeader function call here
   parseHeader(inFile);       

        // malloc space for the array (example given in assignment write-up)
   struct pixel * image = 
          (struct pixel *) malloc(sizeof(struct pixel) * g_width * g_height);

        // parseImage function call here
   parseImage(inFile, image);

        // close input file 

   fclose(inFile);

        // manipulate the image according to command-line parameter
        //              1: mirror image
        //              2: upside down image
        //              3: rotate to the right 90 degrees

   if (atoi(argv[1]) == 1)
   {
      my_Mirror(image);
   }

   if (atoi(argv[1]) == 2)
   {
      my_Flip(image);
   }
   if (atoi(argv[1]) ==3)
   {
      rotate(image);
   }

   print(image);
   return 0;
}

镜子:

      void my_Mirror(struct pixel a[])
{
   int i,j,limit = 0;
   struct pixel temp;

   for(j = 0; j < g_height; ++j) //move through vertical pixels
   {
      for( i = 0; i < (g_width/2); i++)
      {
        temp = a[(j * g_width) + i];
        a[(j * g_width) + i] = a[((j+1)*g_width) - (1 + i)];
        a[((j+1)*g_width) - (1 + i)] = temp;
      }
   }
}

这是我的水平翻转功能,如果有帮助,它会一直运行到镜像调用:

#include "transform.h"

void my_Flip(struct pixel a[])
{
   struct pixel  temp;
   int i;
   int j = 0;

   for (i = (g_width * g_height); i >=  0; --i)
   {
      temp = a[i];
      a[i] = a[(i-i)+j];  //swap values of pixels 
      a[(i-i)+j] = temp;

      ++j;

      if(j == (g_width * g_height)/2)
      {
         i = 0;
      }

   }
   my_Mirror(a);
}

【问题讨论】:

    标签: c arrays structure ppm


    【解决方案1】:

    像素不是固有类型,因此必须通过memcpy 复制 而不是一个简单的任务。

    代码需要验证命令行参数 如果参数不包含有效值 然后在退出前抱怨。

    代码需要验证输入 I/O 的返回值 确保操作成功的功能。

    所以这是您问题的可能解决方案:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct pixel
    {
            char r, g, b;
    };
    
    // global data
    int g_width, g_height;
    
    //prototypes
    void parseHeader( FILE *input );
    void parseImage( FILE *input, struct pixel *theArray );
    void print(struct pixel a[]);
    void my_Mirror(struct pixel a[]);
    void rotate(struct pixel a[]);
    void my_Flip(struct pixel a[]);
    
    
    
    #include "transform.h"
    
    
    int main (int argc, char *argv[])
    {
    
        // declarations here
        FILE *inFile;
    
    
        // open input file
        inFile = fopen(argv[2],"r");
        if (inFile == NULL)
        {
            fprintf(stderr, "File open error. Exiting program\n");
            exit(1);
        }
    
        // implied else, fopen successful
    
        // set g_width and g_height
        parseHeader(inFile);
    
        // malloc space for the array (example given in assignment write-up)
        struct pixel * pImage;
        if(NULL == (pImage = malloc(sizeof(struct pixel) * g_width * g_height)) )
        { // then, malloc failed
            perror( "malloc failed for image size" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        // parseImage function call here
        parseImage(inFile, image);
    
        fclose(inFile); // cleanup
    
        if( 2 > arvc )
        { // then not enough parameters given on command line
            printf( "format is: %s followed by a number in the range 1..3\n",
                     argv[0] );
            exit( EXIT_FAILURE );
        }
    
        // implied else, valid command line input
    
            // manipulate the image according to command-line parameter
            //              1: mirror image
            //              2: upside down image
            //              3: rotate to the right 90 degrees
    
        switch(atoi(argv[1]) )
        {
            case 1:
                my_Mirror(pImage);
                break;
    
            case 2:
                my_Flip(pImage);
                break;
    
            case 3:
                rotate(pImage);
                break;
    
            default:
                printf( "invalid command line parameter\n");
                printf( "parameter must be in range 1..3 inclusive\n");
                printf( "exiting\n" );
                free( pImage ); // cleanup
                exit( EXIT_FAILURE );
                break;
        } // end switch
    
        print(image);
    
        free( pImage ); // cleanup
        return 0;
    } // end function: main
    
    
    void my_Mirror(struct pixel a[])
    {
        int col,row;
        struct pixel temp;
    
        // note: following works irregardless if even or odd number of columns
        for(row = 0; row < g_height; ++row) //step through rows
        {
            for( col = 0; col < (g_width/2); col++) // step through first half of columns
            {
                // perform swap
                memcpy( temp, a[(row * g_width) + col], sizeof(struct pixel) );
                memcpy( a[(row * g_width) + col], a[((row+1)*g_width) - (1 + row)], sizeof(struct pixel) );
                memcpy( a[((row+1)*g_width) - (1 + col)], temp, sizeof(struct pixel) );
            } // end for
        } // end for
    } // end function: my_Mirror
    

    【讨论】:

    • 您实际上可以使用常规分配而不是memcpy()来复制像素结构。
    • @Jongware,我也提供了我的水平翻转功能,该功能一直有效,直到调用镜像功能,因为它的功能类似。我希望它有助于进一步调查这一点,因为我完全不知所措
    • 更新小伙伴们,我太傻了,我实际上并没有意识到镜像函数是按照翻转函数中调用的方式工作的。所以我在 main 中调用它的方式一定有问题
    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多