GithubHelp home page GithubHelp logo

esimov / gomp Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 0.0 5.79 MB

Alpha compositing operations and blending modes in Go.

License: MIT License

Go 100.00%
go alpha-compositing blend-modes colors golang image-composition image-processing porter-duff rgba

gomp's Introduction

gomp

CI Go Report Card Coverage go.dev reference release license

Go library for image blending and alpha compositing using advanced features like the Porter-Duff operator and blending modes.

About

The reason why this package has been developed is because the image/draw package from Go's standard library defines only one operation: drawing a source image onto the destination image, through an optional image mask. This is performed pixel by pixel and it's based on the classic "Compositing Digital Images" paper by Porter and Duff. This paper presented 12 different composition operation, but the Draw method uses only two of them: source over destination and source.

When dealing with image composition this is simply not enough. This library aims to overcome this deficiency by integrating the missing operators.

Alpha compositing
compositing

Blending modes

For convenience, this package implements also some of the most used blending modes in Photoshop. Similarly to the alpha compositing, blending modes defines the result of compositing a source and a destination but without being constrained to the alpha channel. The implementation follows the blending formulas presented in the W3C document: Compositing and Blending. These blending modes are not covered by Porter and Duff, but have been included into this package for convenience.

Blending modes
blending

Installation

$ go install github.com/esimov/gomp@latest

API

The API of the library is inspired by the PorterDuff.Mode class from the Android SDK.

Alpha compositing

op := gomp.InitOp()
op.Set(gomp.SrcOver)
bmp := gomp.NewBitmap(image.Rect(0, 0, size, size))
imop.Draw(bmp, source, backdrop, nil)

Blending modes

op := gomp.NewBlend()
op.Set(gomp.Multiply)
bmp := gomp.NewBitmap(image.Rect(0, 0, size, size))
imop.Draw(bmp, source, backdrop, op)

You can combine the alpha compositing with blending modes at the same step, you just need to replace the last parameter of the Draw method with the initialized blending operation.

Alpha compositing and blending modes combined

imop := gomp.InitOp()
blop := gomp.NewBlend()
blop.Set(gomp.Multiply)
imop.Set(gomp.SrcOver)
bmp := gomp.NewBitmap(image.Rect(0, 0, size, size))
imop.Draw(bmp, srcImg, bgr, blop)

Operators

Image compositing Separable blending modes Non-separable blending modes
Clear Normal Hue
Copy Darken Saturation
Dst Lighten ColorMode
SrcOver Multiply Luminosity
DstOver Screen
SrcIn Overlay
DstIn SoftLight
SrcOut HardLight
DstOut ColorDodge
SrcAtop ColorBurn
DstAtop Difference
Exclusion

Examples

The images used in this document for visualizing the alpha compositing operation and the blending modes have been generated using this library. They can be found in the examples folder.

Author

License

Copyright © 2022 Endre Simo

This software is distributed under the MIT license. See the LICENSE file for the full license text.

gomp's People

Contributors

actions-user avatar esimov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.