mercredi 8 avril 2020

Transfering processing details from a class to another

My program takes a bunch of images and applies a series of filters in a sequence, kind of like a pipeline. The swing gui is responsible for the loading of images as well as displaying the list of filters and sending filter information to the processor class when certain buttons are clicked.

This sends the current list model to the processor.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        if (filterProc == null || listChanged == true) {
             filterProc = new FilterProcessor(listModel);
             listChanged = false;
             filterProc.create();
        }
        if (images != filterProc.getImages()) {
            filterProc.loadImages(images);
        }
        filterProc.execute();

    }                             

In the processing class I load the filter and images lists

 public FilterProcessor(DefaultListModel listModel) {

        this.listModel = listModel;
        this.filterObjects = this.listModel.toArray();

    }

    public void loadImages(BufferedImage[] img) {
        images = new BufferedImage[img.length];

        for (int i = 0; i < img.length; i++) {
            ColorModel cm = img[i].getColorModel();
            boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
            WritableRaster raster = img[i].copyData(null);
            images[i] = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
        }
    }

I create a filter object based on ID:

public void create() throws ClassNotFoundException, IllegalAccessException, InstantiationException {

        for (Object o : filterObjects) {
            filter = (iFilter) Class.forName("package.algorithm." + (String) o).newInstance();
            filters.add(filter);
        }
    }

And finally I apply the filters on the images:

 public void execute() {
        for (BufferedImage image : images) {
            for (iFilter f : filters) {
                f.apply(image);
                display(image);

            }
        }
    }

This works fine and doesn't seem too slow for now, but I have come across two issues that I am not sure how to solve in an elegant manner. In the main panel, after choosing a filter and adding/ displaying it in the JList I want to be able to edit the settings of that specific filter in the nearby panel. For example, if I add a Sobel filter, when I click the item in the jList a panel should pop up with options that allow me to edit the kernel.

My first concern is that I'd have to create a different panel / template for each different filter, i.e. the sobel gets a kernel editor, the object detection gets options for size, brightness etc. Does this sound good or am I overthinking it?

As for my second concern, how would you go about getting the processing options from the panel and sending it to the filter processor? I was thinking about wrapping the list of objects together with the processing details and sending that to the processor to unpack and create the appropriate objects. But how exactly? Is JSON a viable option or perhaps just a simple data structure like <ID,<list of filters>>? A multimap, I suppose?

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire