2

PPM 画像のユーザー入力を取得し、画像を左から右にミラーリングするプログラムを作成する必要があります (基本的には、y 軸上で反転します)。したがって、イメージが < の場合、新しいイメージは > になります。これは 180 度回転しないでください。画像が上下逆になるためです。反映されているだけで、同じはずです。

ここに、PPM の入出力に使用したコードがありますが、それをミラーリングする方法がわかりません。理論的にミラーコードに使用される関数を作成しましたが、それが最善の方法であるかどうかもわかりません。コードを配置するより良い場所を知っている場合は、ゲストになってください。

私はこのトピックをかなり調査しましたが、人々が画像を回転させる必要がある問題しか見つけることができませんでした.

これが私がこれまでに持っているものです:

#include<stdio.h>
#include<stdlib.h> //for fopen()

typedef struct {
     unsigned char red,green,blue;
} pixel_t; //struct for pixels

typedef struct {
     int x, y;
     pixel_t *data;
} PPMImage; //struct for creating the image

#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{
     char buff[16];
     PPMImage *img;
     FILE *fp;
     int c, rgb_comp_color;

     fp = fopen(filename, "rb");
     if (!fp) {
         fprintf(stderr, "Unable to open file '%s'\n", filename);
         exit(1);} //opens the ppm and checks to make sure it can be opened

     if (!fgets(buff, sizeof(buff), fp)) {
         perror(filename);
         exit(1);} //read the format of the image

    if (buff[0] != 'P' || buff[1] != '6') {
         fprintf(stderr, "Invalid image format (must be 'P6')\n");
         exit(1);} //checks to see if the format is ppm

    img = (PPMImage *)malloc(sizeof(PPMImage));
    if (!img) {
         fprintf(stderr, "Unable to allocate memory\n");
         exit(1);} //allocates the memory needed to form the input image

    c = getc(fp);
    while (c == '#') {
    while (getc(fp) != '\n') ;
         c = getc(fp);
    }//checks for comments

    ungetc(c, fp);

    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
         fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
         exit(1);} //reads the size of the image, height becomes img->y, and width becomes img->x

    if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
         fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
         exit(1);} //reads how much of each color there is

    if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
         fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
         exit(1);} //makes sure the the component is 8 bits

    while (fgetc(fp) != '\n') ;

    img->data = (pixel_t*)malloc(img->x * img->y * sizeof(pixel_t));

    if (!img) {
         fprintf(stderr, "Unable to allocate memory\n");
         exit(1);} //allocates the memory need for the pixel data

    if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
         fprintf(stderr, "Error loading image '%s'\n", filename);
         exit(1);} //reads the pixel data

    fclose(fp);
    return img;
}
void writePPM(const char *filename, PPMImage *img)
{
    FILE *fp;

    fp = fopen(filename, "wb");
    if (!fp) {
         fprintf(stderr, "Unable to open file '%s'\n", filename);
         exit(1);} //opens the file for output

    //write the header file
    //image format
    fprintf(fp, "P6\n");

    //image size
    fprintf(fp, "%d %d\n",img->x,img->y);

    // rgb component depth
    fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);

    // pixel data
    fwrite(img->data, 3 * img->x, img->y, fp);
    fclose(fp);
}

void mirror(PPMImage *img)
{

//this is where I want to insert the code for mirroring the image

}

int main(int argc, char* argv[]){ //takes command line parameters
    PPMImage *image;
    char* filename = argv[1];
    image = readPPM(filename);
    mirror(image);
    writePPM("OutputFile.ppm",image); //creates the output file
    printf("Press Enter");
    getchar();
}
4

2 に答える 2

4

mirror() 関数は、一度に 1 行ずつ画像を処理できます。行ごとに、行の左端のピクセルを取得し、その値を行の右端のピクセルの値と交換します。次に、左から 2 番目のピクセルを取得し、その値を右から 2 番目のピクセルと交換します。これを、交換するピクセルの列位置が「中央で一致する」まで繰り返します。(次に、次の行に移動し、すべての行を完了するまで同じことを行います)。

画像に含まれる列の数が奇数の場合、変更されないままの列が画像の中央に 1 つあることに注意してください (ミラーリングが発生する軸を形成するため)。列数が偶数の場合、すべての列が交換されます。

于 2013-04-22T14:21:31.120 に答える