Coder Social home page Coder Social logo

vorti2 / paintbucket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jflinter/paintbucket

0.0 1.0 0.0 9.01 MB

A fast scanline flood fill implementation, written in Swift.

License: MIT License

Ruby 2.30% Swift 90.56% Objective-C 2.43% Shell 4.71%

paintbucket's Introduction

PaintBucket

Travis branch

PaintBucket is pretty literally what its title suggests. It is an implementation of Scanline Flood Fill in Swift, more commonly known as the algorithm that the paint bucket tool in MS Paint uses under the hood (well, it uses something like it, anyway).

This might, for example, be useful if you have a bunch of product photos shot against a uniformly-colored background and you want to remove said background. Or not, who knows.

Usage

This library exposes a single new category method on UIImage:

let image = UIImage(named: "something")!
let imageWithoutBackground = image.pbk_imageByReplacingColorAt((0, 0), withColor: UIColor.clearColor(), tolerance: 10)

Its parameters should be self-explanatory.

Performance

This is decently fast! That is to say, I was able to speed it up ~100x from my initial implementation (which admittedly was pretty slow). That said, it's not, like, instantaneous. You should probably do this on a background thread unless your images are tiny. (For reference, the 2000x1566 benchmark PNG takes like 3 seconds to process in the simulator on a MacBook pro).

Thanks to the always-amazing Mike Ash for the idea to use an NSIndexSet for temporary storage, and thanks to this random website I found for some implementation ideas for the scanline optimization. That said, this can almost certainly be faster, and I'd welcome a PR that made it so. There's a benchmark in the test suite you can run to see if your code helps.

Contributing

PRs welcome - I'd ask that you open an issue before blindly sending PRs my way just to make sure we agree that your idea is a Good Thing. To get you started, if someone wants to add some kind of clever anti-aliasing to this, I'd sure love that.

Installation

Carthage: add github "jflinter/PaintBucket", ~> 0.1 to your Cartfile.

CocoaPods: add pod 'PaintBucket', '~> 0.1' to your Podfile.

paintbucket's People

Contributors

jflinter avatar jack-stripe avatar

Watchers

Wilhelm Vortisch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.