Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 通过镜像功能后重新打印图像的程序_C_Arrays_Structure_Ppm - Fatal编程技术网

C 通过镜像功能后重新打印图像的程序

C 通过镜像功能后重新打印图像的程序,c,arrays,structure,ppm,C,Arrays,Structure,Ppm,几天来,我一直试图弄明白为什么我的程序只是重新打印输入图像。我知道我的其他函数可以工作,但由于某种原因,这个函数让我感到困惑,我已经尝试了所有我能想到的方法来移动像素,但是我尝试过的任何函数都不能工作 标题: #include <stdio.h> #include <stdlib.h> struct pixel { char r, g, b; }; int g_width, g_height; void parseHeader

几天来,我一直试图弄明白为什么我的程序只是重新打印输入图像。我知道我的其他函数可以工作,但由于某种原因,这个函数让我感到困惑,我已经尝试了所有我能想到的方法来移动像素,但是我尝试过的任何函数都不能工作

标题:

    #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[]);
镜像:

      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;
      }
   }
}

像素不是固有类型,因此必须通过
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
#包括
#包括
结构像素
{
字符r,g,b;
};
//全球数据
int g_宽度,g_高度;
//原型
空解析头(文件*输入);
无效解析图像(文件*输入,结构像素*阵列);
空打印(结构像素a[]);
作废我的镜像(结构像素a[]);
空心旋转(结构像素a[]);
无效我的翻转(结构像素a[]);
#包括“transform.h”
int main(int argc,char*argv[])
{
//这里的声明
文件*填充;
//打开输入文件
infle=fopen(argv[2],“r”);
if(infle==NULL)
{
fprintf(stderr,“文件打开错误。正在退出程序\n”);
出口(1);
}
//否则,fopen成功了
//设置g_宽度和g_高度
解析头(infle);
//数组的malloc空间(赋值写入中给出的示例)
结构像素*pImage;
如果(NULL==(pImage=malloc(sizeof(结构像素)*g_宽度*g_高度)))
{//然后,malloc失败了
perror(“malloc图像大小失败”);
退出(退出失败);
}
//否则,malloc成功了
//在这里调用parseImage函数
解析图像(内嵌,图像);
fclose(infle);//清除
如果(2>arvc)
{//那么命令行上没有给出足够的参数
printf(“格式为:%s,后跟1..3范围内的数字”,
argv[0]);
退出(退出失败);
}
//隐含else,有效的命令行输入
//根据命令行参数操作图像
//1:镜像
//2:倒挂图像
//3:向右旋转90度
开关(atoi(argv[1]))
{
案例1:
我的镜子(皮美杰);
打破
案例2:
我的翻转(pImage);
打破
案例3:
旋转(pImage);
打破
违约:
printf(“无效的命令行参数\n”);
printf(“参数必须在1..3范围内,包括\n”);
printf(“退出”);
免费(pImage);//清理
退出(退出失败);
打破
}//结束开关
打印(图像);
免费(pImage);//清理
返回0;
}//结束函数:main
作废我的镜像(结构像素a[])
{
int col,世界其他地区;
结构像素温度;
//注:如果列数为偶数或奇数,则以下工作不受影响
for(row=0;row
您实际上可以通过常规赋值复制像素结构,而不是
memcpy()
@Jongware,我还提供了水平翻转函数,该函数在调用mirror函数之前一直有效,因为它的功能类似。我希望这有助于进一步研究这一点,因为我现在处于一个完全迷失的状态,我很愚蠢,我实际上没有意识到镜像函数的工作方式与翻转函数中的调用方式相同。所以我在我的主要演讲中对它的称呼一定有问题
#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);
}
#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