2014-11-29 2 views
0

필자는 왜 내 프로그램이 입력 이미지를 다시 인쇄하고 있는지 알아 내려고 노력했습니다. 내 다른 기능이 작동하는지는 알지만, 어떤 이유로이 문제는 나를 뒤적 거리며 나는 픽셀을 움직이려 고 생각할 수있는 모든 방법을 시도했지만 절대 시도한 것은 아무것도 시도하지 않았습니다.미러 기능을 수행 한 후 이미지를 다시 인쇄하는 프로그램

헤더 :

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

답변

0

픽셀은 내장 유형이 아니므로 간단한 할당보다는 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 
+0

당신은 실제로 오히려 방어 적이기'보다 일반 과제와 픽셀 구조체를 복사 할 수 있습니다()': 그래서 여기

는 질문에 가능한 솔루션입니다. – Dmitri

+0

@ Jongware, 나는 비슷한 기능을하기 때문에 mirror 함수를 호출 할 때까지 작동하는 수평 플립 함수도 제공했습니다. 나는 완전한 손실에 있기 때문에 더 조사하는 데 도움이되기를 바랍니다. –

+0

업데이트 친구들, 나는 어리 석다. 플립 기능에서 호출 된 것처럼 거울 기능이 작동한다는 것을 실제로 깨닫지 못했다. 그래서 내 주류에서 부르는 방식에 뭔가 문제가있을거야. –

관련 문제