Added gamma correction
[kaka/cakelight.git] / src / kaka / cakelight / Configuration.java
CommitLineData
e59e98fc
TW
1package kaka.cakelight;
2
cc03403a
TW
3import org.opencv.imgproc.Imgproc;
4
e59e98fc
TW
5import java.io.FileInputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.*;
9import java.util.stream.Collectors;
10
11public class Configuration {
12 private List<Map.Entry<String, String>> settings = new ArrayList<>();
13 public VideoConfiguration video;
14 public LedConfiguration leds;
38c759f8 15 public double gamma;
e59e98fc
TW
16
17 private Configuration(Properties prop) {
18 video = new VideoConfiguration(prop);
19 leds = new LedConfiguration(prop);
38c759f8 20 gamma = Double.parseDouble(get(prop,"gamma", "1"));
e59e98fc
TW
21 }
22
23 public static Configuration from(String propertiesFile) {
24 InputStream input = null;
25 try {
26 input = new FileInputStream(propertiesFile);
27 Properties prop = new Properties();
28 prop.load(input);
29 return new Configuration(prop);
30 } catch (IOException ex) {
31 ex.printStackTrace();
32 } finally {
33 if (input != null) {
34 try {
35 input.close();
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40 }
41 return null;
42 }
43
44 private String get(Properties prop, String name) {
45 return addSetting(name, prop.getProperty(name));
46 }
47
48 private String get(Properties prop, String name, String dflt) {
49 return addSetting(name, prop.getProperty(name, dflt));
50 }
51
52 private String addSetting(String name, String value) {
53 settings.add(new AbstractMap.SimpleEntry<>(name, value));
54 return value;
55 }
56
57 @Override
58 public String toString() {
59 return settings.stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("\n"));
60 }
61
62 public class VideoConfiguration {
e59e98fc
TW
63 public int width;
64 public int height;
65 public int bpp;
cc03403a 66 public int format;
100b82fe 67 public CropConfiguration crop;
e59e98fc
TW
68
69 private VideoConfiguration(Properties prop) {
e59e98fc
TW
70 width = Integer.parseInt(get(prop, "video.width", "720"));
71 height = Integer.parseInt(get(prop, "video.height", "576"));
72 bpp = Integer.parseInt(get(prop, "video.bpp", "2"));
cc03403a
TW
73 switch (get(prop, "video.format", "").toUpperCase()) {
74 case "YUYV":
da7bef43 75 format = Imgproc.COLOR_YUV2BGR_YUYV;
cc03403a
TW
76 break;
77 case "YVYU":
da7bef43 78 format = Imgproc.COLOR_YUV2BGR_YVYU;
cc03403a
TW
79 break;
80 default:
da7bef43 81 format = Imgproc.COLOR_YUV2BGR_UYVY;
cc03403a 82 }
100b82fe
TW
83 crop = new CropConfiguration(prop);
84 }
85
86 public class CropConfiguration {
87 public int left, right, top, bottom;
88
89 private CropConfiguration(Properties prop) {
90 left = Integer.parseInt(get(prop, "video.crop.left", "0"));
91 right = Integer.parseInt(get(prop, "video.crop.right", "0"));
92 top = Integer.parseInt(get(prop, "video.crop.top", "0"));
93 bottom = Integer.parseInt(get(prop, "video.crop.bottom", "0"));
94 }
e59e98fc
TW
95 }
96 }
97
98 public class LedConfiguration {
99 public int cols;
100 public int rows;
101
102 private LedConfiguration(Properties prop) {
103 cols = Integer.parseInt(get(prop, "leds.cols"));
104 rows = Integer.parseInt(get(prop, "leds.rows"));
105 }
ed56b145
TW
106
107 public int getCount() {
108 return cols * 2 + rows * 2;
109 }
e59e98fc
TW
110 }
111}