2014-09-05 69 views
0

假设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

我没有看到任何设置输出大小的地方。 – user3344003 2014-09-05 01:10:34

+0

我没有看到任何输出“EasyPNG”的地方,这意味着这是_not_你的[testcase](http://sscce.org)。 – 2014-09-05 02:19:55

回答

1

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

  1. output没有正确设置其宽度和高度。
  2. 存在内存泄漏。

您可以使用以下两种方法之一来解决该问题。

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

不能保证这些变化将解决所有的问题。

更新,响应由OP

线

     currOutPixel = myPixel;   // something wrong 

评论没有做任何有用的东西要么。它仅覆盖本地变量currOutPixel指向的地方。假设可以分配RGBAPixel类型的对象,则需要:

     *currOutPixel = *myPixel; 
+0

因此“currOutPixel = myPixel;”没有任何问题......“电讯管理局告诉我,在那里有东西 – user124627 2014-09-05 02:40:12

+0

@ user124627,看我的更新。 – 2014-09-05 02:45:14

+0

非常感谢,该程序后工作 – user124627 2014-09-05 23:32:13