Bitmap/Flood fill: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 1,937: | Line 1,937: | ||
call fill (125, 125, '000000001111111100000000'b ); |
call fill (125, 125, '000000001111111100000000'b ); |
||
</lang> |
</lang> |
||
=={{header|Processing}}== |
|||
<lang java>import java.awt.Point; |
|||
import java.util.Queue; |
|||
import java.util.LinkedList; |
|||
PImage img; |
|||
int tolerance; |
|||
color fill_color; |
|||
boolean allowed; |
|||
void setup() { |
|||
size(600, 400); |
|||
img = loadImage("image.png"); |
|||
fill_color = color(250, 0, 0); |
|||
fill(0, 0, 100); |
|||
tolerance = 15; |
|||
image(img, 0, 0, width, height); |
|||
textSize(18); |
|||
text("Tolerance = "+tolerance+" (Use mouse wheel to change)", 100, height-30); |
|||
text("Right click to reset", 100, height-10); |
|||
} |
|||
void draw() { |
|||
if (allowed) { |
|||
image(img, 0, 0, width, height); |
|||
text("Tolerance = "+tolerance+" (Use mouse wheel to change)", 100, height-30); |
|||
text("Right click to reset", 100, height-10); |
|||
allowed = false; |
|||
} |
|||
} |
|||
void mousePressed() { |
|||
img.loadPixels(); // A continious bitmap array with no reference to width |
|||
flood(mouseX, mouseY); |
|||
img.updatePixels(); |
|||
allowed = true; |
|||
if(mouseButton == RIGHT) img = loadImage("image.png"); |
|||
} |
|||
void mouseWheel(MouseEvent event) { |
|||
float e = event.getCount(); |
|||
tolerance += 2*e; |
|||
if(tolerance > 256) tolerance = 256; |
|||
if (tolerance < 0) tolerance = 0; |
|||
allowed = true; |
|||
} |
|||
void flood(int x, int y) { |
|||
color target_color = img.pixels[pixel_position(mouseX, mouseY)]; |
|||
boolean[][]p_control = new boolean[img.height][img.width]; |
|||
Queue<Point> queue = new LinkedList<Point>(); |
|||
queue.add(new Point(x, y)); |
|||
while (!queue.isEmpty()) { |
|||
Point p = queue.remove(); |
|||
if (check(p_control, p.x, p.y, target_color)) { |
|||
queue.add(new Point(p.x, p.y-1)); |
|||
queue.add(new Point(p.x, p.y+1)); |
|||
queue.add(new Point(p.x-1, p.y)); |
|||
queue.add(new Point(p.x+1, p.y)); |
|||
} |
|||
} |
|||
} |
|||
int pixel_position(int x, int y) { // Returns position of clicked coordinates width / height of the image |
|||
return x + (y * img.width); |
|||
} |
|||
boolean check(boolean[][]p_control, int x, int y, color target_color) { |
|||
if (x < 0 || y < 0 || y >= img.height || x >= img.width || p_control[y][x]) return false; |
|||
int pp = img.pixels[pixel_position(x, y)]; |
|||
boolean test_tolerance = (abs(green(target_color)-green(pp)) < tolerance |
|||
&& abs( red(target_color)- red(pp)) < tolerance |
|||
&& abs( blue(target_color)- blue(pp)) < tolerance); |
|||
if (!test_tolerance) return false; |
|||
img.pixels[pixel_position(x, y)] = fill_color; |
|||
p_control[y][x] = true; |
|||
return true; |
|||
}</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |