我应该如何纠正这个指针问题,它给了我一个错误

时间:2014-09-05 01:01:46

标签: c++ pointers image-segmentation

假设rgbapixel是rgbapixel.h文件中的一类像素,因此它具有绿色,蓝色,红色等公共成员的颜色。 PNG是png.h文件中的一类图像,因此它具有图像宽度和高度作为私有成员,然后它有两个返回宽度和高度的公共函数。

在我的main.cpp中,这是代码;

// sets up the output image
PNG * setupOutput(int w, int h)
{
    PNG * image = new PNG(w, h);
    return image;
}
void sketchify()
{   
    // Load in.png
       PNG * original = new PNG;
    original->readFromFile("in.png");
    int width  = original->width();
    int height = original->height();

    // Create out.png
     // PNG * output;                  // i change this
    PNG * output = new PNG;
     setupOutput(width, height);

    // Loud our favorite color to color the outline
    RGBAPixel * myPixel = myFavoriteColor(192);

    // Go over the whole image, and if a pixel differs from that to its upper
    // left, color it my favorite color in the output
    for (int y = 1; y < height; y++)
    {
            for (int x = 1; x < width; x++)
            {
                    // Calculate the pixel difference
                    RGBAPixel * prev = (*original)(x-1, y-1);      // previous top lfet
                    RGBAPixel * curr = (*original)(x  , y  );        // current

                                        // subtracting to see diffrence between pixels
                    int diff = abs(curr->red   - prev->red  ) +
                                       abs(curr->green - prev->green) +
                                       abs(curr->blue  - prev->blue );

                    // If the pixel is an edge pixel,
                    // color the output pixel with my favorite color

                    RGBAPixel * currOutPixel = (*output)(x,y);
                    if (diff > 100)
                            currOutPixel = myPixel;          // something wrong
            }
    }
    // Save the output file
    output->writeToFile("out.png");
    // Clean up memory
    delete myPixel;
    delete output;
    delete original;

当我执行代码时,我得到错误,如;

[EasyPNG]: Warning: attempted to access non-existent pixel (407, 306);
        Truncating request to fit in the range [0,0] x [0,0].

[EasyPNG]: Warning: attempted to access non-existent pixel (408, 306);
        Truncating request to fit in the range [0,0] x [0,0].

在我写“有问题”的地方,我被告知那里有错误。我没有看到它。 'mypixel'和currout两者都成功地声明为指针,所以我不明白这个陈述是怎么回事。如果我尝试更改它,我会收到编译错误。帮助

1 个答案:

答案 0 :(得分:1)

setupOutput(width, height);没有做任何有用的事情。它在堆上创建一个新的PNG,但丢弃返回的值。你有两个问题:

  1. output没有正确设置其宽度和高度。
  2. 内存泄漏。
  3. 您可以使用以下两种方法之一来解决这个问题。

    1. setupOutput(width, height)的返回值指定为output

      替换行:

      PNG * output = new PNG;
      setupOutput(width, height);
      

      PNG * output = setupOutput(width, height);
      
    2. 根本不使用setupOutput(width, height);。创建内联PNG

      替换行:

      PNG * output = new PNG;
      setupOutput(width, height);
      

      PNG * output = new PNG(width, height);
      
    3. 无法保证这些更改可以解决您的所有问题。

      更新,以回应OP的评论

      该行

                              currOutPixel = myPixel;          // something wrong
      

      也没有做任何有用的事情。它只是覆盖了局部变量currOutPixel指向的位置。假设可以分配RGBAPixel类型的对象,您需要:

                              *currOutPixel = *myPixel;