All Downloads are FREE. Search and download functionalities are using the official Maven repository.

one.empty3.library.ImageContainer Maven / Gradle / Ivy

There is a newer version: 2024.5.10
Show newest version
/*
 * Copyright (c) 2022-2023. Manuel Daniel Dahmen
 *
 *
 *    Copyright 2012-2023 Manuel Daniel Dahmen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package one.empty3.library;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

public class ImageContainer extends Representable implements ResourceLoader {
    private StructureMatrix image = new StructureMatrix<>(0, BufferedImage.class);
    private StructureMatrix url = new StructureMatrix<>(0, URL.class);
    private StructureMatrix path = new StructureMatrix<>(0, File.class);
    private StructureMatrix videoUrl= new StructureMatrix<>(0, URL.class);
    private StructureMatrix videoPath = new StructureMatrix<>(0, File.class);
     ;
    private URL oldUrl = null;
    private File oldPath = null;
    private boolean isMovie = false;
    private StructureMatrix vd;
    private double nanos;
    private double oldNanos;


    public ImageContainer(File path, boolean isMovie) {
        this.isMovie = isMovie;
        this.path.setElem(path);
        declareProperties();
    }

    public ImageContainer(URL url) {
        this.isMovie = isMovie;
        this.url.setElem(url);
        if (url != null) {
            try {
                image.setElem(ImageIO.read(url));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        declareProperties();
    }

    public ImageContainer() {
        this.isMovie = isMovie;
        declareProperties();
    }

    public ImageContainer(BufferedImage image) {
        this.isMovie = isMovie;
        this.image.setElem(image);
        declareProperties();
    }

    @Override
    public void declareProperties() {
        super.declareProperties();
        getDeclaredDataStructure().put("image/Instance of BufferedImage", image);
        getDeclaredDataStructure().put("url/URL of BufferedImage", url);
        getDeclaredDataStructure().put("path/Local path or filesystem path of BufferedImage", path);
        getDeclaredDataStructure().put("videoUrl/URL of mp4/avi", videoUrl);
        getDeclaredDataStructure().put("videoPath/Local path or filesystem path of mp4/avi", videoPath);

        load();
    }

    private boolean hasChanged(File elem) {
        boolean c = oldPath != elem;
        oldPath = elem;
        return c;
    }

    private boolean hasChanged(URL elem) {
        boolean c = oldUrl != elem;
        oldUrl = elem;
        return c;
    }

    @Override
    public void load() {
        if (url!=null && hasChanged(url.getElem()) && url.getElem() != null) {
            loadImage(url.getElem());
            isMovie = false;
        } else if (path!=null &&hasChanged(path.getElem()) && path.getElem() != null) {
            loadImage(path.getElem());
            isMovie = false;
        } else if (videoUrl!=null&&hasChanged(videoUrl.getElem())) {
            loadVideo(videoUrl.getElem());
        } else if (videoPath!=null&&hasChanged(videoPath.getElem())) {
            loadVideo(videoPath.getElem());
        }
        if(isMovie) {
        nanos = System.nanoTime();
        if (oldNanos >= nanos && isMovie) {
            ECBufferedImage current = vd.getElem().current();
            image.setElem(current);
        }
        }
    }


    private void loadImage(File path) {
        if (path != null) {
            try {
                image.setElem(ImageIO.read(new FileInputStream(path)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void loadImage(URL url) {
        if (url != null) {
            try {
                image.setElem(ImageIO.read(url));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private void loadVideo(File path) {
        if (path != null) {
/*            vd = new StructureMatrix<>(0, DecodeAndEncodeFrames.class);
            DecodeAndEncodeFrames vd2 = new DecodeAndEncodeFrames(path, new TextureMov());
            vd.setElem(vd2);
  */          isMovie = true;
            try {
                image.setElem(ImageIO.read(new FileInputStream(path)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void loadVideo(URL url) {
        if (url != null) {
            try {
                Object content = url.getContent();
            } catch (IOException e) {
                e.printStackTrace();
            }/*
            vd = new StructureMatrix<>(0, DecodeAndEncodeFrames.class);
            DecodeAndEncodeFrames vd2 = new DecodeAndEncodeFrames(new File(url.getFile()), new TextureMov());
            vd.setElem(vd2);
           */isMovie = true;
            try {
                image.setElem(ImageIO.read(new FileInputStream(new File(url.getFile()))));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public StructureMatrix getImage() {
        return image;
    }

    public void setImage(StructureMatrix image) {
        this.image = image;
    }

    public StructureMatrix getUrl() {
        return url;
    }

    public void setUrl(StructureMatrix url) {
        this.url = url;
    }

    public StructureMatrix getPath() {
        return path;
    }

    public void setPath(StructureMatrix path) {
        this.path = path;
    }

    public StructureMatrix getVd() {
        return vd;
    }

    public void setVd(StructureMatrix vd) {
        this.vd = vd;
    }

    public StructureMatrix getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(StructureMatrix videoUrl) {
        this.videoUrl = videoUrl;
    }

    public StructureMatrix getVideoPath() {
        return videoPath;
    }

    public void setVideoPath(StructureMatrix videoPath) {
        this.videoPath = videoPath;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy