-2

when i use boundary fill algorith(in C) i have an Segmentation fault (core dumped) error. Do you know why it doesnt work?

void boundaryFill(int x, int y, png_byte cr, png_byte cg, png_byte cb, png_byte cfr, png_byte cfg, png_byte cfb)
    {
        png_byte* row = row_pointers[y];
        png_byte* ptr = &(row[x*3]);
        if((ptr[0]!=cr || ptr[1]!=cg || ptr[2]!=cb) && (ptr[0]!=cfr || ptr[1]!=cfg || ptr[2]!=cfb))
        {
            writePixel(x, y, cfr, cfg, cfb);
            boundaryFill(x+1,y, cr, cg, cb, cfr, cfg, cfb);
            boundaryFill(x,y+1, cr, cg, cb, cfr, cfg, cfb);
            boundaryFill(x-1,y, cr, cg, cb, cfr, cfg, cfb);
            boundaryFill(x,y-1, cr, cg, cb, cfr, cfg, cfb);
        }
    }

enter image description here

  • Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman May 16 '20 at 17:13
  • This isn't a complete example, and the variable names are infuriatingly opaque. Where does `row_pointers` come from? – tadman May 16 '20 at 17:15
  • It's a png image 600x600 – Rostyslav Polotskyi May 16 '20 at 17:22
  • No it isn't, it's a variable. What's the definition? Are you *sure* it's initialized properly? Why is it a global variable? – tadman May 16 '20 at 17:22
  • 2
    You never check whether your `x` and `y` are valid, that is inside the image's borders. So any of `row_pointers[y]` or `ptr = &(row[x*3])` might lead to undefined behaviour. – M Oehm May 16 '20 at 17:23
  • If `row_pointers` is a *raw PNG* then none of this code has any hope of working. This is written assuming the image data is a raw bitmap in RGB format. If that isn't the case, this is all going to explode. – tadman May 16 '20 at 17:24
  • 1
    Edit the question to provide a [mre]. – Eric Postpischil May 16 '20 at 17:24

1 Answers1

0

Ok the problem is in order of boundaryFill() calling.

    boundaryFill(x+1,y, cr, cg, cb, cfr, cfg, cfb);
    boundaryFill(x,y+1, cr, cg, cb, cfr, cfg, cfb);
    boundaryFill(x-1,y, cr, cg, cb, cfr, cfg, cfb);
    boundaryFill(x,y-1, cr, cg, cb, cfr, cfg, cfb);

This is wrong order.

    boundaryFill(x + 1, y, cr, cg, cb, cfr, cfg, cfb);
    boundaryFill(x - 1, y, cr, cg, cb, cfr, cfg, cfb);
    boundaryFill(x, y + 1, cr, cg, cb, cfr, cfg, cfb);
    boundaryFill(x, y - 1, cr, cg, cb, cfr, cfg, cfb);

This one - work fine.