001package net.filebot.media;
002
003import static java.util.Arrays.*;
004import static java.util.ResourceBundle.*;
005import static net.filebot.util.RegularExpressions.*;
006
007public class VideoFormat {
008
009        public static final VideoFormat DEFAULT_GROUPS = new VideoFormat(getIntArrayProperty("resolution.steps.w"), getIntArrayProperty("resolution.steps.h"));
010
011        private final int[] ws;
012        private final int[] hs;
013
014        public VideoFormat(int[] ws, int[] hs) {
015                this.ws = ws;
016                this.hs = hs;
017        }
018
019        public int guessFormat(int width, int height) {
020                int ns = 0;
021
022                for (int i = 0; i < ws.length - 1; i++) {
023                        if ((width >= ws[i] || height >= hs[i]) || (width > ws[i + 1] && height > hs[i + 1])) {
024                                ns = hs[i];
025                                break;
026                        }
027                }
028
029                if (ns > 0) {
030                        return ns;
031                }
032
033                throw new IllegalArgumentException("Illegal resolution: [" + width + ", " + height + "]");
034        }
035
036        public int[][] groups() {
037                int[][] groups = new int[ws.length][2];
038                for (int i = 0; i < ws.length; i++) {
039                        groups[i][0] = ws[i];
040                        groups[i][1] = hs[i];
041                }
042                return groups;
043        }
044
045        @Override
046        public String toString() {
047                return deepToString(groups());
048        }
049
050        private static int[] getIntArrayProperty(String key) {
051                return SPACE.splitAsStream(getBundle(VideoFormat.class.getName()).getString(key)).mapToInt(Integer::parseInt).toArray();
052        }
053
054}