Skip to content
Snippets Groups Projects
Utils.java 2.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • package compression.utilities;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    Vojtech Moravec's avatar
    Vojtech Moravec committed
    import java.util.ArrayList;
    
    
    public class Utils {
    
    Vojtech Moravec's avatar
    Vojtech Moravec committed
        public static double calculatePsnr(final double mse, final int signalMax) {
            double psnr = 10.0 * Math.log10((Math.pow(signalMax, 2) / mse));
    
        public static byte[] readFileBytes(final String path) throws FileNotFoundException {
            FileInputStream fileStream = new FileInputStream(path);
            try {
                return fileStream.readAllBytes();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new byte[0];
        }
    
    
        public static <T> boolean arrayContains(final T[] array, final T element) {
            for (int i = 0; i < array.length; i++) {
                if (array[i].equals(element))
                    return true;
            }
            return false;
        }
    
    
    Vojtech Moravec's avatar
    Vojtech Moravec committed
        public static double arrayListMax(final ArrayList<Double> array) {
            double max = Double.MIN_VALUE;
            for (final double val : array) {
                if (val > max) {
                    max = val;
                }
            }
            return max;
        }
    
    
        public static boolean arrayContainsToIndex(final int[] array, final int toIndex, final int element) {
            for (int i = 0; i < toIndex; i++) {
                if (array[i] == element)
                    return true;
            }
            return false;
        }
    
    
    Vojtech Moravec's avatar
    Vojtech Moravec committed
        public static int[] convertU16BytesToInt(final byte[] bytes) {
            assert (bytes.length % 2 == 0);
            int[] values = new int[bytes.length / 2];
    
    
            int index = 0;
            for (int i = 0; i < bytes.length; i += 2) {
    
                final int value = (int) (((bytes[i] & 0xff) << 8) | (bytes[i + 1] & 0xff));
                if (value > 0) {
    
    Vojtech Moravec's avatar
    Vojtech Moravec committed
                    values[index++] = value;
                    continue;
                }
                values[index++] = value;
    
    
        public static double arrayListSum(final ArrayList<Double> array) {
            double sum = 0.0;
            for (final double val : array) {
                sum += val;
            }
            return sum;
        }