<commit_before>package org.helioviewer.jhv.view.j2k.kakadu;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;

import javax.annotation.Nullable;

import kdu_jni.Jp2_input_box;
import kdu_jni.Jp2_palette;
import kdu_jni.Jp2_threadsafe_family_src;
import kdu_jni.Jpx_codestream_source;
import kdu_jni.Jpx_input_box;
import kdu_jni.Jpx_meta_manager;
import kdu_jni.Jpx_metanode;
import kdu_jni.Jpx_source;
import kdu_jni.KduException;
import kdu_jni.Kdu_cache;
import kdu_jni.Kdu_channel_mapping;
import kdu_jni.Kdu_codestream;
import kdu_jni.Kdu_coords;
import kdu_jni.Kdu_dims;
import kdu_jni.Kdu_global;

import org.helioviewer.jhv.math.MathUtils;
import org.helioviewer.jhv.metadata.MetaData;
import org.helioviewer.jhv.metadata.XMLMetaDataContainer;
import org.helioviewer.jhv.view.j2k.image.ResolutionSet;

public class KakaduSource {

    private final Jp2_threadsafe_family_src familySrc = new Jp2_threadsafe_family_src(); // reference has to be maintained
    private final Jpx_source jpxSrc;

    public KakaduSource(Kdu_cache cache, URI uri) throws KduException, IOException {
        if (cache == null) { // local
            File file = new File(uri);
            familySrc.Open(file.getCanonicalPath(), true);
        } else {
            familySrc.Open(cache);
        }

        jpxSrc = new Jpx_source();
        jpxSrc.Open(familySrc, false);
    }

    public Jpx_source getJpxSource() {
        return jpxSrc;
    }

    public int getNumberLayers() throws KduException {
        int[] temp = new int[1];
        jpxSrc.Count_compositing_layers(temp);
        return temp[0];
    }

    public ResolutionSet getResolutionSet(int frame) throws KduException {
        Jpx_codestream_source xstream = jpxSrc.Access_codestream(frame);
        Jpx_input_box inputBox = xstream.Open_stream();
        Kdu_codestream stream = new Kdu_codestream();
        stream.Create(inputBox);
        if (!stream.Exists()) {
            throw new KduException(">> stream does not exist " + frame);
        }

        // Since it gets tricky here I am just grabbing a bunch of values
        // and taking the max of them. It is acceptable to think that an<commit_msg>Switch to Path.of for codepage Windows<commit_after>package org.helioviewer.jhv.view.j2k.kakadu;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

import javax.annotation.Nullable;

import kdu_jni.Jp2_input_box;
import kdu_jni.Jp2_palette;
import kdu_jni.Jp2_threadsafe_family_src;
import kdu_jni.Jpx_codestream_source;
import kdu_jni.Jpx_input_box;
import kdu_jni.Jpx_meta_manager;
import kdu_jni.Jpx_metanode;
import kdu_jni.Jpx_source;
import kdu_jni.KduException;
import kdu_jni.Kdu_cache;
import kdu_jni.Kdu_channel_mapping;
import kdu_jni.Kdu_codestream;
import kdu_jni.Kdu_coords;
import kdu_jni.Kdu_dims;
import kdu_jni.Kdu_global;

import org.helioviewer.jhv.math.MathUtils;
import org.helioviewer.jhv.metadata.MetaData;
import org.helioviewer.jhv.metadata.XMLMetaDataContainer;
import org.helioviewer.jhv.view.j2k.image.ResolutionSet;

public class KakaduSource {

    private final Jp2_threadsafe_family_src familySrc = new Jp2_threadsafe_family_src(); // reference has to be maintained
    private final Jpx_source jpxSrc;

    public KakaduSource(Kdu_cache cache, URI uri) throws Exception {
        if (cache == null) { // local
            familySrc.Open(Path.of(uri).toString(), true);
        } else {
            familySrc.Open(cache);
        }

        jpxSrc = new Jpx_source();
        jpxSrc.Open(familySrc, false);
    }

    public Jpx_source getJpxSource() {
        return jpxSrc;
    }

    public int getNumberLayers() throws KduException {
        int[] temp = new int[1];
        jpxSrc.Count_compositing_layers(temp);
        return temp[0];
    }

    public ResolutionSet getResolutionSet(int frame) throws KduException {
        Jpx_codestream_source xstream = jpxSrc.Access_codestream(frame);
        Jpx_input_box inputBox = xstream.Open_stream();
        Kdu_codestream stream = new Kdu_codestream();
        stream.Create(inputBox);
        if (!stream.Exists()) {
            throw new KduException(">> stream does not exist " + frame);
        }

        // Since it gets tricky here I am just grabbing a bunch of values
        // and taking the max of them. It is acceptable to think that an
{'fraction_non_alphanumeric': 0.08560399636693915, 'fraction_numerical': 0.004087193460490463, 'mean_word_length': 3.564766839378238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6717587', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1530, 'n_words': 323}