com.mycompany.zad1.MainWindow.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.zad1.MainWindow.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.zad1;

import com.sun.org.apache.bcel.internal.classfile.Code;
import image.manipulation.AverageFilter;
import image.manipulation.CalculateHistogram;
import image.manipulation.ImageOperation;
import image.manipulation.Kirsh;
import image.manipulation.Laplacea;
import image.manipulation.MedianfIlter;
import image.manipulation.Rayleigh;
import image.manipulation.RegionGrowing;
import image.manipulation.fourier.Filters;
import image.manipulation.fourier.Fourier;
import inout.ChartFrame;
import inout.FileReader;
import inout.SupportFrame;
import inout.ImageInOut;
import java.awt.Color;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import model.ImageModel;
import org.jfree.chart.JFreeChart;
import utlis.CallcErrors;
import utlis.HistogramPlot;
import utlis.ImageUtlis;
import utlis.Parser;

/**
 *
 * @author tarziz
 */
public class MainWindow extends javax.swing.JFrame {
    private ImageInOut imageInOut = new ImageInOut();
    private ImageModel imageOrginal;
    private ImageModel imageBuff;
    private boolean regionGrowing = false;
    private Color[] colors = new Color[] { Color.blue, Color.green, Color.white, Color.red, Color.YELLOW,
            Color.CYAN };
    private int currentColor = 0;

    /** 
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chooseButton = new javax.swing.JButton();
        imagePane = new javax.swing.JPanel();
        imageOrginalLabel = new javax.swing.JLabel();
        brightensLabel = new javax.swing.JLabel();
        contrastLabel = new javax.swing.JLabel();
        reverseColorButton = new javax.swing.JButton();
        minusBrightnessButton = new javax.swing.JButton();
        addBrightnessButton = new javax.swing.JButton();
        minusContrastButton = new javax.swing.JButton();
        addContrastButton = new javax.swing.JButton();
        kirshButton = new javax.swing.JButton();
        laplasjanButton = new javax.swing.JButton();
        medianFilterButton = new javax.swing.JButton();
        averegeilterButton = new javax.swing.JButton();
        imageMoiffayLabel = new javax.swing.JLabel();
        calculateHistogramButton = new javax.swing.JButton();
        histogramModifyButton = new javax.swing.JButton();
        fourierButton = new javax.swing.JToggleButton();
        phaseMoveButton = new javax.swing.JToggleButton();
        fourierFiltersButton = new javax.swing.JToggleButton();
        regionGrowingButton = new javax.swing.JToggleButton();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("POID");

        chooseButton.setText("Choose FIle");
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        brightensLabel.setText("Brightness");

        contrastLabel.setText("Contrast");

        reverseColorButton.setText("Reverse Colors");
        reverseColorButton.setEnabled(false);
        reverseColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reverseColorButtonActionPerformed(evt);
            }
        });

        minusBrightnessButton.setText("-");
        minusBrightnessButton.setEnabled(false);
        minusBrightnessButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minusBrightnessButtonActionPerformed(evt);
            }
        });

        addBrightnessButton.setText("+");
        addBrightnessButton.setEnabled(false);
        addBrightnessButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBrightnessButtonActionPerformed(evt);
            }
        });

        minusContrastButton.setText("-");
        minusContrastButton.setEnabled(false);
        minusContrastButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minusContrastButtonActionPerformed(evt);
            }
        });

        addContrastButton.setText("+");
        addContrastButton.setEnabled(false);
        addContrastButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addContrastButtonActionPerformed(evt);
            }
        });

        kirshButton.setText("Kirsh");
        kirshButton.setEnabled(false);
        kirshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kirshButtonActionPerformed(evt);
            }
        });

        laplasjanButton.setText("Laplasjan");
        laplasjanButton.setEnabled(false);
        laplasjanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                laplasjanButtonActionPerformed(evt);
            }
        });

        medianFilterButton.setText("Median Filter");
        medianFilterButton.setEnabled(false);
        medianFilterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                medianFilterButtonActionPerformed(evt);
            }
        });

        averegeilterButton.setText("Average Filter");
        averegeilterButton.setEnabled(false);
        averegeilterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                averegeilterButtonActionPerformed(evt);
            }
        });

        imageMoiffayLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imageMoiffayLabelMouseClicked(evt);
            }
        });

        calculateHistogramButton.setText("Calc Histogram");
        calculateHistogramButton.setEnabled(false);
        calculateHistogramButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculateHistogramButtonActionPerformed(evt);
            }
        });

        histogramModifyButton.setText("Modify Histogram");
        histogramModifyButton.setEnabled(false);
        histogramModifyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                histogramModifyButtonActionPerformed(evt);
            }
        });

        fourierButton.setText("Fourier");
        fourierButton.setEnabled(false);
        fourierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fourierButtonActionPerformed(evt);
            }
        });

        phaseMoveButton.setText("Move Amplitude");
        phaseMoveButton.setEnabled(false);
        phaseMoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                phaseMoveButtonActionPerformed(evt);
            }
        });

        fourierFiltersButton.setText("Fourier Filters");
        fourierFiltersButton.setEnabled(false);
        fourierFiltersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fourierFiltersButtonActionPerformed(evt);
            }
        });

        regionGrowingButton.setText("Region Growing");
        regionGrowingButton.setEnabled(false);
        regionGrowingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regionGrowingButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout imagePaneLayout = new javax.swing.GroupLayout(imagePane);
        imagePane.setLayout(imagePaneLayout);
        imagePaneLayout.setHorizontalGroup(imagePaneLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(imagePaneLayout.createSequentialGroup().addContainerGap()
                        .addComponent(imageOrginalLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 515,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46)
                        .addGroup(imagePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(imagePaneLayout.createSequentialGroup().addGroup(imagePaneLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(contrastLabel, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(brightensLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGap(30, 30, 30))
                                .addGroup(imagePaneLayout.createSequentialGroup().addGroup(imagePaneLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(imagePaneLayout.createSequentialGroup().addGap(2, 2, 2)
                                                .addGroup(imagePaneLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(kirshButton,
                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(reverseColorButton,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE, 153,
                                                                Short.MAX_VALUE)))
                                        .addComponent(laplasjanButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(imagePaneLayout.createSequentialGroup()
                                        .addComponent(medianFilterButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(imagePaneLayout.createSequentialGroup()
                                        .addComponent(averegeilterButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(imagePaneLayout.createSequentialGroup()
                                        .addComponent(calculateHistogramButton,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(imagePaneLayout.createSequentialGroup()
                                        .addComponent(histogramModifyButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(imagePaneLayout.createSequentialGroup()
                                        .addComponent(fourierButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        imagePaneLayout.createSequentialGroup().addGroup(imagePaneLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(imagePaneLayout.createSequentialGroup().addGap(9, 9, 9)
                                                        .addComponent(addBrightnessButton,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addGap(18, 18, 18).addComponent(minusBrightnessButton,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 60,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(imagePaneLayout.createSequentialGroup().addGap(12, 12, 12)
                                                        .addComponent(addContrastButton,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addGap(18, 18, 18).addComponent(minusContrastButton,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 59,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addGap(18, 18, 18))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        imagePaneLayout.createSequentialGroup().addGroup(imagePaneLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(regionGrowingButton,
                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(fourierFiltersButton,
                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(phaseMoveButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(imageMoiffayLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
                        .addContainerGap()));
        imagePaneLayout.setVerticalGroup(imagePaneLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(imagePaneLayout.createSequentialGroup().addContainerGap().addGroup(imagePaneLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(imagePaneLayout.createSequentialGroup().addGroup(imagePaneLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(imagePaneLayout.createSequentialGroup().addComponent(brightensLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(imagePaneLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(addBrightnessButton)
                                                .addComponent(minusBrightnessButton))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(contrastLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(imagePaneLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(addContrastButton).addComponent(minusContrastButton))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(reverseColorButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(kirshButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(laplasjanButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(medianFilterButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(averegeilterButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(calculateHistogramButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(histogramModifyButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(fourierButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(phaseMoveButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(fourierFiltersButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(regionGrowingButton))
                                .addComponent(imageOrginalLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 532,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                        .addComponent(imageMoiffayLabel, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE))
                        .addContainerGap()));

        saveButton.setText("Save");
        saveButton.setEnabled(false);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(imagePane, javax.swing.GroupLayout.Alignment.TRAILING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup().addComponent(chooseButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(saveButton).addGap(514, 514, 514)))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(chooseButton).addComponent(saveButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(imagePane, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(64, 64, 64)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        int dialogResult = chooser.showOpenDialog(this);
        if (dialogResult == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();

            try {
                imageOrginal = new ImageInOut().readImageFromFile(file);
                imageBuff = new ImageInOut().readImageFromFile(file);
            } catch (Exception ex) {
                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (imageOrginal != null) {
            //            imageBuff = new ImageModel(imageOrginal);

            //            try {
            //                imageBuff = (ImageModel) imageOrginal.clone();
            //            } catch (CloneNotSupportedException ex) {
            //                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            //            }

            imageOrginalLabel.setIcon(new ImageIcon(imageOrginal.getImage()));
            imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));

            addBrightnessButton.setEnabled(true);
            addContrastButton.setEnabled(true);
            minusBrightnessButton.setEnabled(true);
            minusContrastButton.setEnabled(true);
            reverseColorButton.setEnabled(true);
            saveButton.setEnabled(true);
            laplasjanButton.setEnabled(true);
            kirshButton.setEnabled(true);
            averegeilterButton.setEnabled(true);
            medianFilterButton.setEnabled(true);
            calculateHistogramButton.setEnabled(true);
            histogramModifyButton.setEnabled(false);
            fourierButton.setEnabled(true);
            regionGrowingButton.setEnabled(true);

        }
    }//GEN-LAST:event_chooseButtonActionPerformed

    private void minusContrastButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minusContrastButtonActionPerformed
        ImageOperation imageOperation = new ImageOperation();
        imageOperation.changeContrast(imageBuff, -100);
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_minusContrastButtonActionPerformed

    private void addBrightnessButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBrightnessButtonActionPerformed
        ImageOperation imageOperation = new ImageOperation();
        imageOperation.changeBrintes(imageBuff, 20);
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_addBrightnessButtonActionPerformed

    private void minusBrightnessButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minusBrightnessButtonActionPerformed
        ImageOperation imageOperation = new ImageOperation();
        imageOperation.changeBrintes(imageBuff, -20);
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_minusBrightnessButtonActionPerformed

    private void addContrastButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addContrastButtonActionPerformed
        ImageOperation imageOperation = new ImageOperation();
        imageOperation.changeContrast(imageBuff, 100);
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_addContrastButtonActionPerformed

    private void reverseColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reverseColorButtonActionPerformed
        ImageOperation imageOperation = new ImageOperation();
        imageOperation.reverseColors(imageBuff);
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_reverseColorButtonActionPerformed

    private void kirshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kirshButtonActionPerformed
        Kirsh kirsh = new Kirsh();
        imageBuff = kirsh.compute(imageBuff);
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_kirshButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        int dialogResult = chooser.showSaveDialog(this);
        if (dialogResult == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();

            try {
                imageInOut.saveImageToFile(imageBuff, file, "bmp");
            } catch (Exception ex) {
                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void laplasjanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_laplasjanButtonActionPerformed
        String[] buttonsLabels = { "Lap1", "Lap2", "Lap3", "file" };
        Laplacea laplacea = new Laplacea();
        boolean flag = false;

        int result = JOptionPane.showOptionDialog(null, "Choose Mask", "Choose laplasjan mask",
                JOptionPane.INFORMATION_MESSAGE, 0, null, buttonsLabels, buttonsLabels[0]);
        switch (result) {
        case 0:
            System.out.println("1");
            imageBuff = laplacea.computeImage(imageBuff, result);
            flag = true;
            break;
        case 1:
            System.out.println("2");
            imageBuff = laplacea.computeImage(imageBuff, result);
            flag = true;
            break;
        case 2:
            System.out.println("3");
            imageBuff = laplacea.computeImage(imageBuff, result);
            flag = true;
            break;
        case 3:
            int[][] mask = loadMask();
            if (mask != null) {
                imageBuff = laplacea.computeImage(imageOrginal, mask);
                System.out.println("Robi");
                flag = true;
            }
            break;
        }
        if (flag)
            imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_laplasjanButtonActionPerformed

    private void medianFilterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_medianFilterButtonActionPerformed
        MedianfIlter medianfIlter = new MedianfIlter();

        consloleOut();

        int maskSize = Integer.parseInt(JOptionPane.showInputDialog("Input maskSize"));

        try {
            imageBuff = medianfIlter.computeImage(imageBuff, maskSize);
        } catch (Exception ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));

        consloleOut();
    }//GEN-LAST:event_medianFilterButtonActionPerformed

    private void averegeilterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_averegeilterButtonActionPerformed
        AverageFilter medianfIlter = new AverageFilter();

        consloleOut();

        int maskSize = Integer.parseInt(JOptionPane.showInputDialog("Input maskSize"));

        try {
            imageBuff = medianfIlter.computeImage(imageBuff, maskSize);
        } catch (Exception ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));

        consloleOut();

    }//GEN-LAST:event_averegeilterButtonActionPerformed

    private void calculateHistogramButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calculateHistogramButtonActionPerformed
        CalculateHistogram histogram = new CalculateHistogram();
        imageBuff = histogram.calculateHistogram(imageBuff);
        histogramModifyButton.setEnabled(true);

        HistogramPlot plot = new HistogramPlot();
        JFreeChart chart;
        if (imageBuff.getColorDepth() == 24)
            chart = plot.plotColorChart(imageBuff.getHistogram());
        else
            chart = plot.plotGrayChart(imageBuff.getHistogram().get(0));

        ChartFrame chartFrame = new ChartFrame();
        chartFrame.initChart(chart);
        chartFrame.setVisible(true);

    }//GEN-LAST:event_calculateHistogramButtonActionPerformed

    private void histogramModifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_histogramModifyButtonActionPerformed

        String[] inputText = JOptionPane.showInputDialog("Input gMin and alpha").split(";");

        if (inputText.length == 2) {
            Rayleigh rayleigh = new Rayleigh(Integer.parseInt(inputText[0]), Integer.parseInt(inputText[1]));
            imageBuff = rayleigh.compute(imageBuff);

            //MyPulpa pulpa = new MyPulpa(Integer.parseInt(inputText[0]), Integer.parseInt(inputText[1]));
            //imageBuff = pulpa.h1(imageBuff);

            imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));

            CalculateHistogram histogram = new CalculateHistogram();
            imageBuff = histogram.calculateHistogram(imageBuff);

            HistogramPlot plot = new HistogramPlot();
            JFreeChart chart;
            if (imageBuff.getColorDepth() == 24)
                chart = plot.plotColorChart(imageBuff.getHistogram());
            else
                chart = plot.plotGrayChart(imageBuff.getHistogram().get(0));

            ChartFrame chartFrame = new ChartFrame();
            chartFrame.initChart(chart);
            chartFrame.setVisible(true);
        }
    }//GEN-LAST:event_histogramModifyButtonActionPerformed

    private void fourierButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fourierButtonActionPerformed
        Fourier fourier = new Fourier();
        imageBuff = fourier.fft(imageBuff, false);

        imageOrginal.setFourierMatrix(imageBuff.getFourierMatrix());

        SupportFrame spectrumFrame = new SupportFrame();
        spectrumFrame.initFurier(imageBuff.getFourierMatrix(), false);
        spectrumFrame.setVisible(true);

        SupportFrame phaseFrame = new SupportFrame();
        phaseFrame.initFurier(imageBuff.getFourierMatrix(), true);
        phaseFrame.setVisible(true);

        phaseMoveButton.setEnabled(true);
        fourierFiltersButton.setEnabled(true);
    }//GEN-LAST:event_fourierButtonActionPerformed

    private void phaseMoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_phaseMoveButtonActionPerformed
        Fourier fourier = new Fourier();
        Filters filters = new Filters();

        filters.moveAmplitude(imageBuff, 0.5, 0.25);
        imageBuff = fourier.fft(imageBuff, true);

        imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
    }//GEN-LAST:event_phaseMoveButtonActionPerformed

    private void imageMoiffayLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imageMoiffayLabelMouseClicked
        if (regionGrowing) {
            int treschold = Integer.parseInt(JOptionPane.showInputDialog("Treschold value"));
            RegionGrowing growing = new RegionGrowing();
            ArrayList<int[]> mask = growing.findRegion(imageBuff, evt.getX(), evt.getY(), treschold);

            ImageUtlis utlis = new ImageUtlis();
            utlis.repaintImage(imageBuff, colors[currentColor], mask);

            SupportFrame frame = new SupportFrame();

            frame.initRegionMask(imageOrginal.getWidth(), imageOrginal.getHeight(), mask);
            frame.setVisible(true);
            imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));

            currentColor++;

            if (currentColor == colors.length) {
                regionGrowingButton.setEnabled(false);
                regionGrowing = false;
                currentColor = 0;
            }
        }
    }//GEN-LAST:event_imageMoiffayLabelMouseClicked

    private void fourierFiltersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fourierFiltersButtonActionPerformed
        String[] buttonsLabels = { "Low pass", "High pass", "Band pass", "Band stop", "Edge detection" };
        Filters filters = new Filters();
        boolean flag = false;
        double level = 0;

        int result = JOptionPane.showOptionDialog(null, "Choose Filter", "Choose filter",
                JOptionPane.INFORMATION_MESSAGE, 0, null, buttonsLabels, buttonsLabels[0]);

        switch (result) {
        case 0:
            level = Double.parseDouble(JOptionPane.showInputDialog("D"));
            filters.lowPassFilter(imageBuff, level);
            flag = true;
            break;
        case 1:
            level = Double.parseDouble(JOptionPane.showInputDialog("D"));
            filters.HigPassFilter(imageBuff, level);
            flag = true;
            break;
        case 2:
            filters.bandPassFilter(imageBuff, 0.2, 0.6);
            flag = true;
            break;
        case 3:
            filters.bandStopFilter(imageBuff, 0.2, 0.6);
            flag = true;
            break;
        case 4:
            filters.edgeDetetcion(imageBuff, 0.2, 0.6, 30, 90);
            flag = true;
            break;
        }

        if (flag) {
            SupportFrame spectrumFrame = new SupportFrame();
            spectrumFrame.initFurier(imageBuff.getFourierMatrix(), false);

            Fourier fourier = new Fourier();
            imageBuff = fourier.fft(imageBuff, true);

            imageMoiffayLabel.setIcon(new ImageIcon(imageBuff.getImage()));
            spectrumFrame.setVisible(true);
        }
    }//GEN-LAST:event_fourierFiltersButtonActionPerformed

    private void regionGrowingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regionGrowingButtonActionPerformed
        regionGrowing = !regionGrowing;
    }//GEN-LAST:event_regionGrowingButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }

    private int[][] loadMask() {
        JFileChooser chooser = new JFileChooser();
        int dialogResult = chooser.showOpenDialog(this);
        ArrayList<String> fileLines = new ArrayList<>();
        if (dialogResult == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            try {
                FileReader fileReader = new FileReader();
                fileLines = fileReader.readMask(file);
            } catch (Exception ex) {
                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        Parser parser = new Parser();
        int[][] mask = parser.parseMask(fileLines);
        return mask;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBrightnessButton;
    private javax.swing.JButton addContrastButton;
    private javax.swing.JButton averegeilterButton;
    private javax.swing.JLabel brightensLabel;
    private javax.swing.JButton calculateHistogramButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JLabel contrastLabel;
    private javax.swing.JToggleButton fourierButton;
    private javax.swing.JToggleButton fourierFiltersButton;
    private javax.swing.JButton histogramModifyButton;
    private javax.swing.JLabel imageMoiffayLabel;
    private javax.swing.JLabel imageOrginalLabel;
    private javax.swing.JPanel imagePane;
    private javax.swing.JButton kirshButton;
    private javax.swing.JButton laplasjanButton;
    private javax.swing.JButton medianFilterButton;
    private javax.swing.JButton minusBrightnessButton;
    private javax.swing.JButton minusContrastButton;
    private javax.swing.JToggleButton phaseMoveButton;
    private javax.swing.JToggleButton regionGrowingButton;
    private javax.swing.JButton reverseColorButton;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

    private void consloleOut() {

        CallcErrors errors = new CallcErrors();
        double[] MSE = errors.calcMSE(imageOrginal, imageBuff);
        double[] PSNR = errors.calcPSNR(MSE);

        System.out.println("Red MSE: " + MSE[0] + " PSNR: " + PSNR[0] + " DB");
        System.out.println("Green MSE: " + MSE[1] + " PSNR: " + PSNR[1] + " DB");
        System.out.println("Blue MSE: " + MSE[2] + " PSNR: " + PSNR[2] + " DB");
    }

}