Floodfill api in android

Problems with Canvas, OpenGL, etc...

Floodfill api in android

Postby alien0101 » Tue Nov 16, 2010 4:30 pm

I am not able to find floodfill api in android.
Can someone guide me how can I perform floodfill in android?

Floodfill is the basic graphics api available in most of the languages.
alien0101
Freshman
Freshman
 
Posts: 2
Joined: Wed May 26, 2010 12:46 pm

Top

Re: Floodfill api in android

Postby Schermvlieger » Tue Nov 16, 2010 10:43 pm

I am sorry you will have to write your own routine for it; there are some examples out there for java; just google on floodfill and you'll find the most basic routines quite quickly. Not fast and may result in stack overflows with larger areas though ...
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: Floodfill api in android

Postby Tage Nielsen » Thu Nov 18, 2010 9:53 pm

Wikipedia has a great article on flood fill algorithms. I had some issues dealing with screen dithering when trying repeated flood fills, and of course, performance. The following snippet is from my flood fill routine.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private void floodFill(int index, int[] pixels, int w, int paintColor) {
  2.         Stack stack = new Stack(pixels.length);
  3.         stack.push(index);
  4.         pixels[index] = paintColor;
  5.  
  6.         // loop has a seed pixel that has been been transformed to newColor
  7.         int scanLeft;
  8.         int scanRight;
  9.         while (stack.hasQueue()) {
  10.             scanLeft = index;
  11.             scanRight = index;
  12.  
  13.             // replace pixels to left boundary
  14.             while ((scanLeft % w != 0) && isInColorRange(pixels[scanLeft - 1])) {
  15.                 scanLeft--;
  16.                 pixels[scanLeft] = paintColor;
  17.             }
  18.  
  19.             // replace pixels to right boundary
  20.             while ((scanRight % w != w - 1) && isInColorRange(pixels[scanRight + 1])) {
  21.                 scanRight++;
  22.                 pixels[scanRight] = paintColor;
  23.             }
  24.  
  25.             // check line above for target color & populate stack
  26.             if (scanLeft - w >= 0) {
  27.                 for (int i = scanLeft - w; i <= scanRight - w; i += 1) {
  28.                     if (isInColorRange(pixels[i])) {
  29.                         pixels[i] = paintColor;
  30.                         stack.push(i);
  31.                     }
  32.                 }
  33.             }
  34.  
  35.             // check line below for target color & populate stack
  36.             if (scanRight + w < pixels.length) {
  37.                 for (int i = scanLeft + w; i <= scanRight + w; i += 1) {
  38.                     if (isInColorRange(pixels[i])) {
  39.                         pixels[i] = paintColor;
  40.                         stack.push(i);
  41.                     }
  42.                 }
  43.             }
  44.             index = stack.pop();
  45.         }
  46.     }
  47.  
  48.     /*
  49.      * Used as a queue for the Fill Loop: Floodfill
  50.      */
  51.     private class Stack {
  52.         int mIndex;
  53.         int[] mStack;
  54.  
  55.         Stack(int size) {
  56.             mStack = new int[size];
  57.             mIndex = 0;
  58.         }
  59.  
  60.         void push(int pixelIndex) {
  61.             mStack[++mIndex] = pixelIndex;
  62.         }
  63.  
  64.         int pop() {
  65.             return mStack[mIndex--];
  66.         }
  67.  
  68.         boolean hasQueue() {
  69.             return (mIndex > 0) ? true : false;
  70.         }
  71.     }
  72. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Tage Nielsen
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Oct 13, 2010 8:02 pm

Re: Floodfill api in android

Postby emopi » Tue Dec 20, 2011 6:26 pm

The code above has missing.

there is not function isInColorRange(int color) , in the code block.

Can anyone help me about efficent and fast Flood Fill algorithm or alternative algorithms?
I have some photos (bitmap or png) and i wanna fill a region with a specific color in android application. I tried some codes but the performance is terrible.
emopi
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Dec 20, 2011 6:22 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest