Monday, December 24, 2012

Adjust color components of red, green and blue of image

Last article describe how to "Retrieve color components from image". Here it's demonstrated how to adjust color components of image.

Adjust color components of red, green and blue of image
Adjust color components of red, green and blue of image

package testjavafx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

 * @web
public class TestJavaFX extends Application {
    PixelReader pixelReader;
    int width, height;
    WritableImage writableImage, writableImageRed, writableImageGreen, writableImageBlue;
    PixelWriter pixelWriter, pixelWriterRed, pixelWriterGreen, pixelWriterBlue;
    ImageView destImageView, imageViewRed, imageViewGreen, imageViewBlue;
    Slider sliderRed, sliderGreen, sliderBlue;
    double adjRed, adjGreen, adjBlue;
    public void start(Stage primaryStage) {
        Image image = new Image("");
        ImageView imageView = new ImageView();
        pixelReader = image.getPixelReader();
        width = (int)image.getWidth();
        height = (int)image.getHeight();
        //Copy from source to destination pixel by pixel
        writableImage = new WritableImage(width, height);
        pixelWriter = writableImage.getPixelWriter();
        writableImageRed = new WritableImage(width, height);
        pixelWriterRed = writableImageRed.getPixelWriter();
        writableImageGreen = new WritableImage(width, height);
        pixelWriterGreen = writableImageGreen.getPixelWriter();
        writableImageBlue = new WritableImage(width, height);
        pixelWriterBlue = writableImageBlue.getPixelWriter();
        destImageView = new ImageView();
        imageViewRed = new ImageView();
        imageViewGreen = new ImageView();
        imageViewBlue = new ImageView();
        HBox hBox_Image = new HBox();
        hBox_Image.getChildren().addAll(imageView, destImageView);
        //Control box for Red
        sliderRed = SliderBuilder.create()
        HBox hBox_Red = new HBox();
        hBox_Red.getChildren().addAll(imageViewRed, sliderRed);
        //Control box for Green
        sliderGreen = SliderBuilder.create()
        HBox hBox_Green = new HBox();
        hBox_Green.getChildren().addAll(imageViewGreen, sliderGreen);
        //Control box for Blue
        sliderBlue = SliderBuilder.create()
        HBox hBox_Blue = new HBox();
        hBox_Blue.getChildren().addAll(imageViewBlue, sliderBlue);
        VBox vBox = new VBox();
                hBox_Red, hBox_Green, hBox_Blue);

        StackPane root = new StackPane();
        Scene scene = new Scene(root, 350, 330);
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
    private void updateImage(){
        adjRed = sliderRed.valueProperty().doubleValue();
        adjGreen = sliderGreen.valueProperty().doubleValue();
        adjBlue = sliderBlue.valueProperty().doubleValue();
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                double red = color.getRed() + adjRed;
                if(red > 1.0){
                    red = 1.0;
                }else if(red < 0.0){
                    red = 0.0;
                double green = color.getGreen() + adjGreen;
                if(green > 1.0){
                    green = 1.0;
                }else if(green < 0.0){
                    green = 0.0;
                double blue = color.getBlue() + adjBlue;
                if(blue > 1.0){
                    blue = 1.0;
                }else if(blue < 0.0){
                    blue = 0.0;
                double opacity = color.getOpacity();
                pixelWriterRed.setColor(x, y, new Color(red, 0.0, 0.0, opacity));
                pixelWriterGreen.setColor(x, y, new Color(0.0, green, 0.0, opacity));
                pixelWriterBlue.setColor(x, y, new Color(0.0, 0.0, blue, opacity));
                pixelWriter.setColor(x, y, new Color(red, green, blue, opacity));


    public static void main(String[] args) {

No comments:

Post a Comment