-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZMBVCodec.java
More file actions
172 lines (153 loc) · 7.13 KB
/
ZMBVCodec.java
File metadata and controls
172 lines (153 loc) · 7.13 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
/*
* @(#)ZMBVCodec.java
*
* Copyright (c) 2011 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
import java.awt.image.DataBufferUShort;
import java.awt.Point;
import java.awt.image.DirectColorModel;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.util.Hashtable;
import static java.lang.Math.*;
/**
* Implements the DosBox Capture Codec {@code "ZMBV"}.
* <p>
* This codec currently only supports decoding from the file format into
* a {@code BufferedImage}. Encoding support may be added in the future.
* <p>
* For details seee {@link ZMBVCodecCore}.
* </p>
*
* @author Werner Randelshofer
* @version $Id: ZMBVCodec.java 137 2011-12-28 09:19:37Z werner $
*/
public class ZMBVCodec extends AbstractVideoCodec {
private ZMBVCodecCore state;
private Object oldPixels;
private Object newPixels;
public ZMBVCodec() {
super(new Format[]{
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_AVI_DOSBOX_SCREEN_CAPTURE, VideoFormatKeys.DataClassKey, byte[].class, VideoFormatKeys.FixedFrameRateKey, true), //
},
new Format[]{
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_JAVA,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_BUFFERED_IMAGE, VideoFormatKeys.FixedFrameRateKey, true), //
});
name = "ZMBV Codec";
}
@Override
public int process(Buffer in, Buffer out) {
return decode(in,out);
}
public int decode(Buffer in, Buffer out) {
out.setMetaTo(in);
if (in.isFlag(BufferFlag.DISCARD)) {
return CODEC_OK;
}
out.format = outputFormat;
out.length = 1;
out.offset = 0;
int width = outputFormat.get(VideoFormatKeys.WidthKey);
int height = outputFormat.get(VideoFormatKeys.HeightKey);
if (state == null) {
state = new ZMBVCodecCore();
}
Object[] newPixelHolder = new Object[]{newPixels};
Object[] oldPixelHolder = new Object[]{oldPixels};
int result = state.decode((byte[]) in.data, in.offset, in.length, newPixelHolder, oldPixelHolder, width, height, false);
boolean isKeyframe = result < 0;
int depth = abs(result);
newPixels = newPixelHolder[0];
oldPixels = oldPixelHolder[0];
MyBufferedImage img = null;
if (out.data instanceof MyBufferedImage) {
img = (MyBufferedImage) out.data;
}
switch (depth) {
case 8: {
int imgType = BufferedImage.TYPE_BYTE_INDEXED; // FIXME - Don't hardcode this value
if (img == null || img.getWidth() != width || img.getHeight() != height || img.getType() != imgType) {
int[] cmap = new int[256];
IndexColorModel icm = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
img = new MyBufferedImage(width, height, imgType, icm);
} else {
MyBufferedImage oldImg = img;
img = new MyBufferedImage(oldImg.getColorModel(), oldImg.getRaster(), oldImg.isAlphaPremultiplied(), null);
}
int[] cmap = state.getPalette();
IndexColorModel icm = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
img.setColorModel(icm);
byte[] pixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
System.arraycopy((byte[]) newPixels, 0, pixels, 0, width * height);
}
break;
case 15: {
int imgType = BufferedImage.TYPE_USHORT_555_RGB; // FIXME - Don't hardcode this value
if (img == null || img.getWidth() != width || img.getHeight() != height || img.getType() != imgType) {
DirectColorModel cm = new DirectColorModel(15, 0x1f << 10, 0x1f << 5, 0x1f << 0);
img = new MyBufferedImage(cm, Raster.createWritableRaster(cm.createCompatibleSampleModel(width, height), new Point(0, 0)), false, new Hashtable());
} else {
MyBufferedImage oldImg = img;
img = new MyBufferedImage(oldImg.getColorModel(), oldImg.getRaster(), oldImg.isAlphaPremultiplied(), null);
}
short[] pixels = ((DataBufferUShort) img.getRaster().getDataBuffer()).getData();
System.arraycopy((short[]) newPixels, 0, pixels, 0, width * height);
}
break;
case 16: {
int imgType = BufferedImage.TYPE_USHORT_565_RGB; // FIXME - Don't hardcode this value
if (img == null || img.getWidth() != width || img.getHeight() != height || img.getType() != imgType) {
DirectColorModel cm = new DirectColorModel(15, 0x1f << 11, 0x3f << 5, 0x1f << 0);
img = new MyBufferedImage(cm, Raster.createWritableRaster(cm.createCompatibleSampleModel(width, height), new Point(0, 0)), false, new Hashtable());
} else {
MyBufferedImage oldImg = img;
img = new MyBufferedImage(oldImg.getColorModel(), oldImg.getRaster(), oldImg.isAlphaPremultiplied(), null);
}
short[] pixels = ((DataBufferUShort) img.getRaster().getDataBuffer()).getData();
System.arraycopy((short[]) newPixels, 0, pixels, 0, width * height);
}
break;
case 32:
default:
throw new UnsupportedOperationException("Unsupported depth:" + depth);
}
Object swap=oldPixels;
oldPixels=newPixels; newPixels=swap;
out.setFlag(BufferFlag.KEYFRAME, isKeyframe);
out.data = img;
return CODEC_OK;
}
private static class MyBufferedImage extends BufferedImage {
private ColorModel colorModel;
public MyBufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?, ?> properties) {
super(cm, raster, isRasterPremultiplied, properties);
colorModel = cm;
}
public MyBufferedImage(int width, int height, int imageType, IndexColorModel cm) {
super(width, height, imageType, cm);
colorModel = cm;
}
public MyBufferedImage(int width, int height, int imageType) {
super(width, height, imageType);
}
@Override
public ColorModel getColorModel() {
return colorModel;
}
public void setColorModel(ColorModel newValue) {
this.colorModel = newValue;
}
}
}