0

我正在尝试垂直翻转 C 中的图像,所以如果图像是 < 它将结束 > 并且我的功能包括

//Setting the struct up for the pixel's
struct pixel
 {
   unsigned char red;
   unsigned char green;
   unsigned char blue;
};

//Setting the struct up for the Image Type and scanning in the pxiels into an array
struct ImageType 
{  
   char ppImage[3];
   char comment[256];
   char newlinechar;
   int width, height;
   int maxColor = 255;
   struct pixel image[100][100];
};


//Function in order to flip the image, going from the left most pixel flipping with the      right most
void MirrorVertical(struct ImageType imgur)
{
   int x,y;
   const int middle = imgur.width / 2;
   struct pixel tmp;
   struct *pixel p;

   for(y=0; y < imgur.height; ++y)
      {
         p = tmp + y * imgur.width;

         for(x=0; x < middle; ++x)
           {
               tmp = p[x];
               p[x] = p[imgur.width - 1 - x];
               p[imgur.width - 1 - x] = tmp;
           }
      }
}

我让我的结构工作但由于某种原因我的函数不会输出它,我正在将图像扫描到结构中,所以......

//Scanning in the pixels for the first image
   for(i=imageA.height-1; i <= 0; i--)
      {
         for(j=0; j < imageA.width; j++)
            {
               scanf("%hhu", &imageA.image[i][j].red);
               scanf("%hhu", &imageA.image[i][j].green);
               scanf("%hhu", &imageA.image[i][j].blue);
            }
      }

我在我的功能中做错了什么?

它应该是

   for(x=0; x < width; x++)
      {
         for(y = 0; y < height/2; y++)
            {
               temp = imgur.image[x][y];
               imgur.image[x][y] = imgur.image[x][height-y-1]
               imgur.image[x][height-y-1] = temp;
            }
      }
}
4

2 回答 2

1

我想你的编译器一定在抱怨

struct pixel tmp;
struct *pixel p;

for(y=0; y < imgur.height; ++y)
  {
     p = tmp + y * imgur.width;

您正在将一个结构添加到一个 int 并将结果分配给一个指针。它应该如何工作?

编辑现在你已经用“更好”的代码更新了你的问题,但它仍然不起作用,这里有一些你可以/应该改变的事情。

  1. 您声明一个变量tmp,然后尝试访问temp. 失败的秘诀
  2. 您将整个传递struct imgur给函数。这意味着“复制所有内容”。你真的应该传递一个指向对象的指针 - 更改原型以反映这一点,并访问元素imgur->height
  3. 您永远不会在 MirrorVertical 函数中height声明变量width
  4. (次要)每个内部循环计算height - 1 - y两次该值 - 总共 20000 次。如果您交换内部和外部循环并只计算一次(并分配给一个新变量newY),您可以节省一点时间(不确定它是否真的更有效,因为您最终会在 X 上循环,这可能会破坏缓存一致性,尤其是大图像)。
  5. 我的编译器(和 C 标准)抱怨int maxColor = 256;结构定义中的语句;您不能在typedef.
  6. 编译器抛出的其他错误。

我冒昧地修复了其中的许多 - 这导致以下代码似乎可以编译和运行;现在您只需要添加“输入图像”和“输出图像”功能(也许)。

#include <stdio.h>

//Setting the struct up for the pixels
struct pixel
 {
   unsigned char red;
   unsigned char green;
   unsigned char blue;
};

//Setting the struct up for the Image Type and scanning in the pixels into an array
struct ImageType
{
   char ppImage[3];
   char comment[256];
   char newlinechar;
   int width;
   int height;
   int maxColor;  // cannot initialize this here; removed "=256"
   struct pixel image[100][100];
};


//Function in order to flip the image, going from the left most pixel flipping with the      right most
void MirrorVertical(struct ImageType *imgur) // using a pointer to the struct
{
   int x,y, height, width;  // added declaration of height, width
   // const int middle = imgur->width / 2; // removed, not used
   struct pixel tmp;  // use same name here and in loop
   height = imgur->height;  // initialize once - save a redirect later
   width = imgur->width;    // ditto
   for(y = 0; y < imgur->height/2; y++)  // made this the outer loop
   {
     int newY = height - y - 1; // so we only compute it once
     for(x=0; x < imgur->width; x++)
     {
         tmp = imgur->image[x][y];  // use "tmp" not "temp"
         imgur->image[x][y] = imgur->image[x][newY];
         imgur->image[x][newY] = tmp;
       }
   }
}

// a simple main program… this doesn't really do anything except call the function
int main(void) {
struct ImageType i1;
// … need to add code to import the image
MirrorVertical(&i1);  // note - passing POINTER to i1, not the entire struct
// … need to add code to export the image
}

让我知道这是否有效。

于 2013-12-07T21:50:14.500 回答
1

这不应该:for(i=imageA.height-1; i <= 0; i--)for(i=imageA.height-1; i >= 0; i--)吗?(在“扫描第一张图像的像素”代码中)

于 2013-12-07T22:15:35.907 回答