imagine

Imagine

Release

GPU accelerated, blisteringly fast, easy-to-use, layer based image editing library using OpenGL ES 2.0 for Android with support for Photoshop style blend modes.

Features

Demo

A beautiful Material You themed simple image editor is provided in the editor module. You can refer to the source code of the same and maybe also use it.

Screencast

Documentation

The library code is extensively documented. Additionally, check out the story style blog Imagine: A story of the evergreen OpenGL on Android that details the conception of this library!

Installation

Add this to your project level build.gradle file

repositories {
  maven { url 'https://jitpack.io' }
}

Then add this to your module level build.gradle file

dependencies {
  implementation 'com.github.forkachild:imagine:1.1.0'
}

Usage

  1. Add ImagineView into your layout
    <com.suhel.imagine.core.ImagineView
        android:id="@+id/imagineView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
  2. Configure ImagineEngine
    private lateinit var imagineView: ImagineView
    private lateinit var imagineEngine: ImagineEngine
       
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
           
        imagineView = findViewById(R.id.imagineView)
        imagineEngine = ImagineEngine(imagineView) // Stored in a WeakReference internally
    }
    
  3. Load an image using an implementation of ImageProvider interface
    imagineEngine.imageProvider = UriImageProvider(context, uri) // From ContentResolver Uri
    imagineEngine.imageProvider = ResImageProvider(context, resId) // Or from a drawable res
    imagineEngine.imageProvider = ... // Or your custom ImageProvider implementation
    
  4. Create one or more ImagineLayer objects, writing a vec4 process(vec4 color) GLSL function for each
    class InvertGreenLayer: ImagineLayer {
        // The fragment shader source for this layer. The texture is pre-sampled
        // and the color is passed to this function. The function signature must
        // be accurate!
        override val source: String = """
            vec4 process(vec4 color) {
                return vec4(color.r, 1.0 - color.g, color.b, color.a); // The alpha channel is important!
            }
        """.trimIndent()
           
        // Configures how each layer (after processing) will be blended with the previous
        // layer in the chain
        override val blendMode: ImagineBlendMode = ImagineBlendMode.Normal
           
        // Configures the intensity of application of the pixel color output from this layer
        override val intensity: Float = 1.0f
           
        override fun create(program: ImagineShader.Program) {
            // Optional override to extract your custom uniforms from the shader program
        }
           
        override fun bind(program: ImagineShader.Program) {
            // Optional override to bind your custom uniforms during processing
        }
    }
    
  5. Assign a List<ImagineLayer> to ImagineEngine
    imagineEngine.layers = listOf(
        InvertGreenLayer(),
        ...
    )
    
  6. Update the viewport preview
    imagineEngine.updatePreview()
    
  7. (Optional) Extract a full resolution processed bitmap
    imagineEngine.onBitmap = { bitmap ->
        // Do something with the bitmap and then recycle it
    }
    imagineEngine.exportBitmap()
    

TODO

License

MIT License

Copyright (c) 2023 Suhel Chakraborty

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.