4

I have the following code for "blob filling" in a bmp. However, it runs out of memory on the stack at loop 7201 or some such. How do I increase memory? I've heard the stack size is 1MB which is WAY TOO SMALL! I have 32GB Ram and I would like to use it to its fullest. Recursion just seems like the way to go on an algorithm like this, if only my stack wasn't too puny. (It's a four way recursion; every function call generates four of the same function calls)

public HashSet<int> evalpxls = new HashSet<int>();

public String addLikePixels(int r, int c, Byte A, Byte R, Byte G, Byte B, Bitmap bmp)
        {
            reclvl++;
            dl("Try r:" + r+ " c:" + c + " reclvl:"+reclvl);
            if (evalpxls.Contains(hash(r,c)) || r>=bmp.Height || c>=bmp.Width || r<0 || c<0) return "";
            evalpxls.Add(hash(r, c));
            var p = bmp.GetPixel(c, r);

            String curpix = "[" + r + "," + c +"]";
            if (p.A == A && p.B == B && p.G == G && p.R == R) //if same color as main color
            {
                return curpix + addLikePixels(r + 1, c, A, R, G, B, bmp) + addLikePixels(r, c + 1, A, R, G, B, bmp) + addLikePixels(r - 1, c, A, R, G, B, bmp) + addLikePixels(r, c - 1, A, R, G, B, bmp);
            }
            else //if different color
                return "";
        }
leppie
  • 115,091
  • 17
  • 196
  • 297
  • converting to an iterating solution might be better than using a vast stack – phuclv Aug 05 '15 at 05:32
  • You also may want to look at this [answer](http://stackoverflow.com/a/1521457/479512) by [Eric Lippert](http://stackoverflow.com/users/88656/eric-lippert) he does a good job of explaining what all of your options are. – Mark Hall Aug 05 '15 at 05:54
  • Any ideas about converting to an iterating solution? Can't seem to immediately think of an iterative analog to this solution, unless an entirely different approach was taken(Which I'm not coming up with any ATM). – Andrew Wheels Aug 05 '15 at 06:20
  • Figured out a solution using a stack and a while loop. While (!locations.empty) locations.pop. if same color-> locations.push(up,down,left,right),etc.; Still elegant, but recursion does have that 'intuitive' beauty about it. – Andrew Wheels Aug 05 '15 at 10:47

2 Answers2

3

If you don't mind creating a thread and doing this on another thread (not that you need to have it done asynchronously, just put that code in another thread , start it and join immediately) then you can create a thread with a constructor to specify the stack size and run your code there.

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
  • Tried increasing the thread size, however, it seems like it only decreased the thread stack size 4 times when I used a huge number for the stack size. (It would go to recursion level 1700 something, instead of 7200) – Andrew Wheels Aug 05 '15 at 05:16
2

Create a parameter object class containing all parameters (r,c,A...). Now, instead of directly calling addLikePixels, just create a new param object and put it on a stack which is either a member or is also passed as param to addLikePixels. In addLikePixels() you always take the next obj from the stack until it's empty.

You can also choose between using a stack (depth-first) or a queue (breadth-first).

howlowl
  • 76
  • 3