C Segfault带有洪水填充



我想淹没图像green.jpg,红色从(0,0)开始。我尝试在C (因为Python的递归速度很慢)进行此操作,并将CIMG用作成像库来获取和设置像素。这是我的程序。

#include <iostream>
#include <tuple>
#include <vector>
#include "CImg.h"
using namespace cimg_library;
int main()
{
    CImg<unsigned char> im("green.jpg");
    int l = im.width();
    int w = im.height();
    int x = 0;
    int y = 0;
    auto newcol = std::make_tuple(255,0,0);
    int R = im(x,y,0,0);
    int G = im(x,y,0,1);
    int B = im(x,y,0,2);
    auto oldcol = std::make_tuple(R,G,B);
    std::vector<std::tuple<int,int>> edge = {std::make_tuple(x,y)};
    while (edge.size() != 0)
    {
    std::vector<std::tuple<int,int>> newedge;
        for (int e = 0; e < edge.size(); e++)
        {
            int a = std::get<0>(edge[e]);
            int b = std::get<1>(edge[e]);
            if (0 <= a <= l && 0 <= b <= w) 
            {
                auto currcol = std::make_tuple(im(a,b,0,0),im(a,b,0,1),im(a,b,0,2));
                if (currcol == oldcol)
                {
                        im(a,b,0,0) = std::get<0>(newcol);
                        im(a,b,0,1) = std::get<1>(newcol);
                        im(a,b,0,2) = std::get<2>(newcol);
                        newedge.push_back(std::make_tuple(a-1,b));
                        newedge.push_back(std::make_tuple(a+1,b));
                        newedge.push_back(std::make_tuple(a,b+1));
                        newedge.push_back(std::make_tuple(a,b-1));          
                }
            }
        }
        edge = newedge;
    }
    im.save("done.jpg");    
    return 0;
}

但是,我一直遇到我找不到原因的segfault。我已经尝试使用GDB调试程序,但它返回了以下内容:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040b67c in std::_Head_base<2ul, unsigned char, false>::_Head_base<unsigned char&, void> (this=0x7fffffffdd10, 
__h=@0x8000f7f8c0bf: <error reading variable>) at /usr/include/c++/4.8/tuple:140

显然在元组头文件中。有人可以告诉我,Segfault是由什么造成的以及如何解决的?谢谢。

编辑:添加堆栈跟踪:

(gdb) bt
#0  0x000000000040b67c in std::_Head_base<2ul, unsigned char, false>::_Head_base<unsigned char&, void> (this=0x7fffffffdd10, 
    __h=@0x1006b87d1: <error reading variable>) at /usr/include/c++/4.8/tuple:140
#1  0x000000000040b6c2 in std::_Tuple_impl<2ul, unsigned char>::_Tuple_impl<unsigned char&, , void>(unsigned char&) (this=0x7fffffffdd10, 
    __head=@0x1006b87d1: <error reading variable>) at /usr/include/c++/4.8/tuple:262
#2  0x000000000040b71e in std::_Tuple_impl<1ul, unsigned char, unsigned char>::_Tuple_impl<unsigned char&, unsigned char&, void> (
    this=0x7fffffffdd10, __head=@0x1006b86f0: <error reading variable>) at /usr/include/c++/4.8/tuple:262
#3  0x000000000040b7b1 in std::_Tuple_impl<0ul, unsigned char, unsigned char, unsigned char>::_Tuple_impl<unsigned char&, unsigned char&, unsigned char&, void> (this=0x7fffffffdd10, __head=@0x1006b860f: <error reading variable>) at /usr/include/c++/4.8/tuple:262
#4  0x000000000040b832 in std::tuple<unsigned char, unsigned char, unsigned char>::tuple<unsigned char&, unsigned char&, unsigned char&, void> (
this=0x7fffffffdd10) at /usr/include/c++/4.8/tuple:405
#5  0x000000000040b895 in std::make_tuple<unsigned char&, unsigned char&, unsigned char&> () at /usr/include/c++/4.8/tuple:862
#6  0x0000000000402e5d in main () at fill.cpp:34

我解决了自己的问题。在GDB中,我发现我的一个变量a等于-1,它超出了图像范围,导致内存问题和该segfault。if (0 <= a <= l && 0 <= b <= w)应该捕获这一点,但显然a <= b <= c在C 中无法正常工作。所以我将其更改为 if (0 <= a && a <= l && 0 <= b && b <= w),并且起作用。

谢谢大家的支持。

相关内容

  • 没有找到相关文章

最新更新