/Image4J

An image processing library for Java

Primary LanguageJavaGNU Lesser General Public License v3.0LGPL-3.0

Image4J v.0.0.4

Image4J is an image processing library for Java.

Getting Started

To clone this repository and build the project, you can type the following in Git Bash. You need Apache Ant though.

git clone https://github.com/macroing/Image4J.git
cd Image4J
ant

Examples

Below follows a few examples that demonstrates various features in Image4J.

Blend Example

The following example loads two images from your hard drive, blends them together and saves the result to your hard drive.

import org.macroing.image4j.Image;

public class BlendExample {
    public static void main(String[] args) {
        Image image_0 = Image.load("Image-0.png");
        Image image_1 = Image.load("Image-1.png");
        
        Image image = Image.blend(image_0, image_1, 0.5F);
        image.save("Blend-Example.png");
    }
}

Convolution Kernel Example

The following example loads an image from your hard drive, multiplies it with a convolution kernel and saves the result to your hard drive.

import org.macroing.image4j.ConvolutionKernel33;
import org.macroing.image4j.Image;

public class ConvolutionKernelExample {
    public static void main(String[] args) {
        Image image = Image.load("Image.png");
        image.multiply(ConvolutionKernel33.SHARPEN);
        image.save("Convolution-Kernel-Example.png");
    }
}

Crop Example

The following example loads an image from your hard drive, crops it and saves the result to your hard drive.

import org.macroing.image4j.Color;
import org.macroing.image4j.Image;

public class CropExample {
    public static void main(String[] args) {
        Image image = Image.load("Image.png");
        image = image.crop(50, 50, image.getResolutionX() - 50, image.getResolutionY() - 50, Color.BLACK, false, false);
        image.save("Crop-Example.png");
    }
}

Fill And Draw Examples

The following example creates two images, one empty and one random. Then it fills the empty image with a circle, draws the random image to the empty image and saves the result to your hard drive.

import org.macroing.image4j.Color;
import org.macroing.image4j.Image;

public class FillAndDrawExample {
    public static void main(String[] args) {
        Image image_0 = Image.random(50, 50);
        
        Image image = new Image(150, 150);
        image.fillCircle(75, 75, 50, Color.RED);
//      The two methods below are equivalent:
        image.drawImage(50, 50, image_0);
//      image.drawImage(50, 50, image_0, (colorA, colorB) -> Color.blend(colorA, colorB, colorB.a));
        image.save("Fill-And-Draw-Example.png");
}

The following example creates an empty image, fills it with a circle and two triangles and saves the result to your hard drive.

import org.macroing.image4j.Color;
import org.macroing.image4j.Image;
import org.macroing.image4j.PixelFunction;

public class FillExample {
    public static void main(String[] args) {
        Color colorCircle = new Color(0.5F, 0.75F, 0.75F);
        Color colorTriangle = new Color(0.75F, 0.5F, 0.75F);
        
        Image image = new Image(882, 882);
        image.fillCircle(400, 600, 100, PixelFunction.simplexFractionalBrownianMotion(colorCircle, 300.0F, 500.0F, 500.0F, 700.0F));
        image.fillTriangle(300, 100, 500, 400, 500, 150, PixelFunction.simplexFractionalBrownianMotion(colorTriangle, 300.0F, 100.0F, 500.0F, 400.0F));
        image.fillTriangle(120, 60, 45, 200, 400, 400, PixelFunction.barycentricInterpolation(120.0F, 60.0F, 45.0F, 200.0F, 400.0F, 400.0F));
        image.save("Fill-Example.png");
    }
}

alt text

Noise Example

The following example creates an empty image, fills the image with colors generated by noise and saves the result to your hard drive.

import org.macroing.image4j.Color;
import org.macroing.image4j.Image;
import org.macroing.math4j.NoiseGeneratorF;

public class NoiseExample {
    public static void main(String[] args) {
        Color base = new Color(0.75F, 0.5F, 0.75F);
        
        Image image = new Image(882, 882);
        
        for(int index = 0; index < image.getResolution(); index++) {
            float x = (index % image.getResolutionX()) / (float)(image.getResolutionX());
            float y = (index / image.getResolutionX()) / (float)(image.getResolutionY());
            
            float noise = NoiseGeneratorF.simplexFractionalBrownianMotion(x, y, 5.0F, 0.5F, 0.0F, 1.0F, 16);
            
            Color color = base.multiply(noise).minTo0().maxTo1().redoGammaCorrection();
            
            image.setColor(index, color);
        }
        
        image.save("Noise-Example.png");
    }
}

alt text

Update Example

The following example loads an image from your hard drive, updates it with a function and saves the result to your hard drive.

import org.macroing.image4j.Color;
import org.macroing.image4j.Image;

public class UpdateExample {
    public static void main(String[] args) {
        Image image = Image.load("Image.png");
        image.update(color -> Color.blend(color, Color.randomRed(), 0.5F));
        image.save("Update-Example.png");
    }
}

Dependencies

Note

This library hasn't been released yet. So, even though it says it's version 1.0.0 in all Java source code files, it shouldn't be treated as such. When this library reaches version 1.0.0, it will be tagged and available on the "releases" page.