-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmigaVideoFormatKeys.java
More file actions
250 lines (226 loc) · 8.99 KB
/
AmigaVideoFormatKeys.java
File metadata and controls
250 lines (226 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* @(#)AmigaVideoFormatKeys.java
*
* Copyright (c) 2011 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance onlyWith the
* license agreement you entered into onlyWith Werner Randelshofer.
* For details see accompanying license terms.
*/
import java.util.ArrayList;
import java.util.Iterator;
/**
* {@code AmigaVideoFormatKeys}.
*
* @author Werner Randelshofer
* @version $Id$
*/
public class AmigaVideoFormatKeys extends VideoFormatKeys
{
/** The Amiga monitor id.
*/
public final static FormatKey<Integer> MonitorIdKey = new FormatKey<Integer>("monitorId", Integer.class);
/** Anim Op5 . */
public static final String ENCODING_ANIM_OP5 = "op5";
enum ColorMode {
HAM, EHB, NORMAL
}
public final static FormatKey<ColorMode> ColorModeKey = new FormatKey<ColorMode>("colorMode", ColorMode.class);
public static Format fromCAMG(int camg) {
AmigaDisplayInfo i = AmigaDisplayInfo.getInfo(camg);
return new Format(
MediaTypeKey, MediaType.VIDEO,
EncodingKey, ENCODING_BITMAP_IMAGE,
WidthKey, i.textOverscanWidth,
HeightKey, i.textOverscanHeight,
MonitorIdKey, camg & AmigaDisplayInfo.MONITOR_ID_MASK,
ColorModeKey, i.isEHB() ? ColorMode.EHB : (i.isHAM() ? ColorMode.HAM : ColorMode.NORMAL),
InterlaceKey, i.isInterlace(),
PixelAspectRatioKey, new Rational(i.resolutionX, i.resolutionY),
FrameRateKey, new Rational(i.fps, 1));
}
public static int toCAMG(Format fmt) {
int camg = 0;
// determine monitor id
int monitorId = 0;
if (fmt.containsKey(MonitorIdKey)) {
monitorId = fmt.get(MonitorIdKey);
} else {
ArrayList<AmigaDisplayInfo> infs = new ArrayList<AmigaDisplayInfo>(AmigaDisplayInfo.getAllInfos().values());
if (fmt.containsKey(InterlaceKey)) {
boolean value = fmt.get(InterlaceKey);
reduceListBoolean(value, new InfGetter<Boolean>() {
@Override
public Boolean get(AmigaDisplayInfo inf) {
return inf.isInterlace();
}
}, infs);
}
if (fmt.containsKey(FrameRateKey)) {
Rational value = fmt.get(FrameRateKey);
reduceListRational(value, new InfGetter<Rational>() {
@Override
public Rational get(AmigaDisplayInfo inf) {
return new Rational(inf.fps, 1);
}
}, infs);
}
if (fmt.containsKey(PixelAspectRatioKey)) {
Rational value = fmt.get(PixelAspectRatioKey);
reduceListRational(value, new InfGetter<Rational>() {
@Override
public Rational get(AmigaDisplayInfo inf) {
return new Rational(inf.resolutionX, inf.resolutionY);
}
}, infs);
}
ArrayList<AmigaDisplayInfo> bestInfs = new ArrayList<AmigaDisplayInfo>(infs);
if (fmt.containsKey(WidthKey)) {
int value = fmt.get(WidthKey);
reduceListIntegerOnlyTakeIfSmaller(value, new InfGetter<Integer>() {
@Override
public Integer get(AmigaDisplayInfo inf) {
return inf.textOverscanWidth;
}
}, infs);
}
if (fmt.containsKey(HeightKey)) {
Integer value = fmt.get(HeightKey);
reduceListIntegerOnlyTakeIfSmaller(value, new InfGetter<Integer>() {
@Override
public Integer get(AmigaDisplayInfo inf) {
return inf.textOverscanHeight;
}
}, infs);
}
if (infs.isEmpty()) {
infs = new ArrayList<AmigaDisplayInfo>(bestInfs);
if (fmt.containsKey(WidthKey)) {
Integer value = fmt.get(WidthKey);
reduceListIntegerOnlyTakeIfSmaller(value, new InfGetter<Integer>() {
@Override
public Integer get(AmigaDisplayInfo inf) {
return inf.maxOverscanWidth;
}
}, infs);
}
if (fmt.containsKey(HeightKey)) {
Integer value = fmt.get(HeightKey);
reduceListIntegerOnlyTakeIfSmaller(value, new InfGetter<Integer>() {
@Override
public Integer get(AmigaDisplayInfo inf) {
return inf.maxOverscanHeight;
}
}, infs);
}
}
if (infs.isEmpty()) {
infs = new ArrayList<AmigaDisplayInfo>(bestInfs);
if (fmt.containsKey(WidthKey)) {
Integer value = fmt.get(WidthKey);
reduceListInteger(value, new InfGetter<Integer>() {
@Override
public Integer get(AmigaDisplayInfo inf) {
return inf.maxOverscanWidth;
}
}, infs);
}
if (fmt.containsKey(HeightKey)) {
Integer value = fmt.get(HeightKey);
reduceListInteger(value, new InfGetter<Integer>() {
@Override
public Integer get(AmigaDisplayInfo inf) {
return inf.maxOverscanHeight;
}
}, infs);
}
}
}
int colorMode=0;
if (fmt.containsKey(ColorModeKey)) {
switch (fmt.get(ColorModeKey)) {
case EHB:
colorMode=AmigaDisplayInfo.EHB_COLORMODE;
break;
case HAM:
colorMode=AmigaDisplayInfo.HAM_COLORMODE;
break;
case NORMAL:
break;
}
}
camg = monitorId|colorMode;
return camg;
}
private interface InfGetter<T> {
public T get(AmigaDisplayInfo inf);
}
private static void reduceListRational(Rational value, InfGetter<Rational> g, ArrayList<AmigaDisplayInfo> infs) {
ArrayList<AmigaDisplayInfo> bestInfs = new ArrayList<AmigaDisplayInfo>();
bestInfs.add(infs.get(0));
float bestCost = g.get(infs.get(0)).subtract(value).floatValue();
bestCost *= bestCost;
for (Iterator<AmigaDisplayInfo> i = infs.iterator(); i.hasNext();) {
AmigaDisplayInfo inf = i.next();
Rational iv = g.get(inf);
if (iv.compareTo(value) != 0) {
i.remove();
}
float icost = iv.subtract(value).floatValue();
icost *= icost;
if (icost < bestCost) {
bestInfs.clear();
bestCost = icost;
} else if (icost == bestCost) {
bestInfs.add(inf);
}
}
if (infs.isEmpty()) {
infs.addAll(bestInfs);
}
}
private static void reduceListInteger(int value, InfGetter<Integer> g, ArrayList<AmigaDisplayInfo> infs) {
ArrayList<AmigaDisplayInfo> bestInfs = new ArrayList<AmigaDisplayInfo>();
bestInfs.add(infs.get(0));
float bestCost = g.get(infs.get(0)) - value;
bestCost *= bestCost;
for (Iterator<AmigaDisplayInfo> i = infs.iterator(); i.hasNext();) {
AmigaDisplayInfo inf = i.next();
int iv = g.get(inf);
if (iv != value) {
i.remove();
}
float icost = iv - value;
icost *= icost;
if (icost < bestCost) {
bestInfs.clear();
bestCost = icost;
} else if (icost == bestCost) {
bestInfs.add(inf);
}
}
if (infs.isEmpty()) {
infs.addAll(bestInfs);
}
}
private static void reduceListIntegerOnlyTakeIfSmaller(int value, InfGetter<Integer> g, ArrayList<AmigaDisplayInfo> infs) {
reduceListInteger(value, g, infs);
for (Iterator<AmigaDisplayInfo> i = infs.iterator(); i.hasNext();) {
AmigaDisplayInfo inf = i.next();
int iv = g.get(inf);
if (value > iv) {
i.remove();
}
}
}
private static void reduceListBoolean(boolean value, InfGetter<Boolean> g, ArrayList<AmigaDisplayInfo> infs) {
for (Iterator<AmigaDisplayInfo> i = infs.iterator(); i.hasNext();) {
AmigaDisplayInfo inf = i.next();
boolean iv = g.get(inf);
if (iv != value) {
i.remove();
}
}
}
}