https://github.com/esimov/gomp
Alpha compositing operations and blending modes in Go.
https://github.com/esimov/gomp
alpha-compositing blend-modes colors go golang image-composition image-processing porter-duff rgba
Last synced: 2 months ago
JSON representation
Alpha compositing operations and blending modes in Go.
- Host: GitHub
- URL: https://github.com/esimov/gomp
- Owner: esimov
- License: mit
- Created: 2022-10-27T06:17:49.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-24T07:09:40.000Z (8 months ago)
- Last Synced: 2025-03-27T10:03:36.053Z (3 months ago)
- Topics: alpha-compositing, blend-modes, colors, go, golang, image-composition, image-processing, porter-duff, rgba
- Language: Go
- Homepage:
- Size: 5.61 MB
- Stars: 13
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# gomp
[](https://github.com/esimov/gomp/actions/workflows/ci.yml)
[](https://goreportcard.com/report/github.com/esimov/gomp)
[](https://pkg.go.dev/github.com/esimov/gomp)
[](https://github.com/esimov/gomp/releases/tag/v1.0.2)
[](./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`**](https://pkg.go.dev/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](https://dl.acm.org/doi/pdf/10.1145/964965.808606)" 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
|:--:
|  |### 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](https://www.w3.org/TR/compositing-1/#blending). These blending modes are not covered by Porter and Duff, but have been included into this package for convenience.| Blending modes
|:--:
|  |## Installation
```bash
$ go install github.com/esimov/gomp@latest
```## API
The API of the library is inspired by the [PorterDuff.Mode](https://developer.android.com/reference/android/graphics/PorterDuff.Mode) class from the Android SDK.### Alpha compositing
```go
op := gomp.InitOp()
op.Set(gomp.SrcOver)
bmp := gomp.NewBitmap(image.Rect(0, 0, size, size))
imop.Draw(bmp, source, backdrop, nil)
```### Blending modes
```go
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
```go
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](https://github.com/esimov/gomp/tree/master/examples) folder.## Author
* Endre Simo ([@simo_endre](https://twitter.com/simo_endre))## License
Copyright © 2022 Endre SimoThis software is distributed under the MIT license. See the [LICENSE](https://github.com/esimov/gomp/blob/master/LICENSE) file for the full license text.