001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.mrcal;
019
020import java.io.IOException;
021import java.util.List;
022import org.photonvision.common.hardware.Platform;
023import org.photonvision.common.util.TestUtils;
024import org.photonvision.jni.PhotonJNICommon;
025
026public class MrCalJNILoader extends PhotonJNICommon {
027    private boolean isLoaded;
028    private static MrCalJNILoader instance = null;
029
030    private MrCalJNILoader() {
031        isLoaded = false;
032    }
033
034    public static synchronized MrCalJNILoader getInstance() {
035        if (instance == null) instance = new MrCalJNILoader();
036
037        return instance;
038    }
039
040    public static synchronized void forceLoad() throws IOException {
041        // Force load opencv
042        TestUtils.loadLibraries();
043
044        // Library naming is dumb and has "lib" appended for Windows when it ought not to
045        if (Platform.isWindows()) {
046            // Order is correct to match dependencies of libraries
047            forceLoad(
048                    MrCalJNILoader.getInstance(),
049                    MrCalJNILoader.class,
050                    List.of(
051                            "libamd",
052                            "libcamd",
053                            "libcolamd",
054                            "libccolamd",
055                            "openblas",
056                            "libwinpthread-1",
057                            "libgcc_s_seh-1",
058                            "libquadmath-0",
059                            "libgfortran-5",
060                            "liblapack",
061                            "libcholmod",
062                            "mrcal_jni"));
063        } else {
064            // Nothing else to do on linux
065            forceLoad(MrCalJNILoader.getInstance(), MrCalJNILoader.class, List.of("mrcal_jni"));
066        }
067
068        if (!MrCalJNILoader.getInstance().isLoaded()) {
069            throw new IOException("Unable to load mrcal JNI!");
070        }
071    }
072
073    @Override
074    public boolean isLoaded() {
075        return isLoaded;
076    }
077
078    @Override
079    public void setLoaded(boolean state) {
080        isLoaded = state;
081    }
082}