-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractVideoCodec.java
More file actions
223 lines (205 loc) · 9.3 KB
/
AbstractVideoCodec.java
File metadata and controls
223 lines (205 loc) · 9.3 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
/*
* @(#)AbstractVideoCodec.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.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.awt.image.DataBufferShort;
import java.awt.image.DataBufferUShort;
import java.awt.image.DirectColorModel;
import java.awt.image.WritableRaster;
import java.io.IOException;
import javax.imageio.stream.ImageOutputStream;
/**
* {@code AbstractVideoCodec}.
*
* @author Werner Randelshofer
* @version $Id: AbstractVideoCodec.java 188 2012-03-28 14:03:19Z werner $
*/
public abstract class AbstractVideoCodec extends AbstractCodec {
private BufferedImage imgConverter;
public AbstractVideoCodec(Format[] supportedInputFormats, Format[] supportedOutputFormats) {
super(supportedInputFormats, supportedOutputFormats);
}
/** Gets 8-bit indexed pixels from a buffer. Returns null if conversion failed. */
protected byte[] getIndexed8(Buffer buf) {
if (buf.data instanceof byte[]) {
return (byte[]) buf.data;
}
if (buf.data instanceof BufferedImage) {
BufferedImage image = (BufferedImage) buf.data;
if (image.getRaster().getDataBuffer() instanceof DataBufferByte) {
return ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
}
}
return null;
}
/** Gets 15-bit RGB pixels from a buffer. Returns null if conversion failed. */
protected short[] getRGB15(Buffer buf) {
if (buf.data instanceof int[]) {
return (short[]) buf.data;
}
if (buf.data instanceof BufferedImage) {
BufferedImage image = (BufferedImage) buf.data;
if (image.getColorModel() instanceof DirectColorModel) {
DirectColorModel dcm = (DirectColorModel) image.getColorModel();
if (image.getRaster().getDataBuffer() instanceof DataBufferShort) {
// FIXME - Implement additional checks
return ((DataBufferShort) image.getRaster().getDataBuffer()).getData();
} else if (image.getRaster().getDataBuffer() instanceof DataBufferUShort) {
// FIXME - Implement additional checks
return ((DataBufferUShort) image.getRaster().getDataBuffer()).getData();
}
}
if (imgConverter == null) {
int width = outputFormat.get(VideoFormatKeys.WidthKey);
int height = outputFormat.get(VideoFormatKeys.HeightKey);
imgConverter = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_555_RGB);
}
Graphics2D g = imgConverter.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return ((DataBufferUShort) imgConverter.getRaster().getDataBuffer()).getData();
}
return null;
}
/** Gets 16-bit RGB-5-6-5 pixels from a buffer. Returns null if conversion failed. */
protected short[] getRGB16(Buffer buf) {
if (buf.data instanceof int[]) {
return (short[]) buf.data;
}
if (buf.data instanceof BufferedImage) {
BufferedImage image = (BufferedImage) buf.data;
if (image.getColorModel() instanceof DirectColorModel) {
DirectColorModel dcm = (DirectColorModel) image.getColorModel();
if (image.getRaster().getDataBuffer() instanceof DataBufferShort) {
// FIXME - Implement additional checks
return ((DataBufferShort) image.getRaster().getDataBuffer()).getData();
} else if (image.getRaster().getDataBuffer() instanceof DataBufferUShort) {
// FIXME - Implement additional checks
return ((DataBufferUShort) image.getRaster().getDataBuffer()).getData();
}
}
if (imgConverter == null) {
int width = outputFormat.get(VideoFormatKeys.WidthKey);
int height = outputFormat.get(VideoFormatKeys.HeightKey);
imgConverter = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_565_RGB);
}
Graphics2D g = imgConverter.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return ((DataBufferUShort) imgConverter.getRaster().getDataBuffer()).getData();
}
return null;
}
/** Gets 24-bit RGB pixels from a buffer. Returns null if conversion failed. */
protected int[] getRGB24(Buffer buf) {
if (buf.data instanceof int[]) {
return (int[]) buf.data;
}
if (buf.data instanceof BufferedImage) {
BufferedImage image = (BufferedImage) buf.data;
if (image.getColorModel() instanceof DirectColorModel) {
DirectColorModel dcm = (DirectColorModel) image.getColorModel();
if (dcm.getBlueMask() == 0xff && dcm.getGreenMask() == 0xff00 && dcm.getRedMask() == 0xff0000) {
if (image.getRaster().getDataBuffer() instanceof DataBufferInt) {
return ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
}
}
}
return image.getRGB(0, 0, //
outputFormat.get(VideoFormatKeys.WidthKey), outputFormat.get(VideoFormatKeys.HeightKey), //
null, 0, outputFormat.get(VideoFormatKeys.WidthKey));
}
return null;
}
/** Gets 32-bit ARGB pixels from a buffer. Returns null if conversion failed. */
protected int[] getARGB32(Buffer buf) {
if (buf.data instanceof int[]) {
return (int[]) buf.data;
}
if (buf.data instanceof BufferedImage) {
BufferedImage image = (BufferedImage) buf.data;
if (image.getColorModel() instanceof DirectColorModel) {
DirectColorModel dcm = (DirectColorModel) image.getColorModel();
if (dcm.getBlueMask() == 0xff && dcm.getGreenMask() == 0xff00 && dcm.getRedMask() == 0xff0000) {
if (image.getRaster().getDataBuffer() instanceof DataBufferInt) {
return ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
}
}
}
return image.getRGB(0, 0, //
outputFormat.get(VideoFormatKeys.WidthKey), outputFormat.get(VideoFormatKeys.HeightKey), //
null, 0, outputFormat.get(VideoFormatKeys.WidthKey));
}
return null;
}
/** Gets a buffered image from a buffer. Returns null if conversion failed. */
protected BufferedImage getBufferedImage(Buffer buf) {
if (buf.data instanceof BufferedImage) {
return (BufferedImage) buf.data;
}
return null;
}
private byte[] byteBuf = new byte[4];
protected void writeInt24(ImageOutputStream out, int v) throws IOException {
byteBuf[0] = (byte) (v >>> 16);
byteBuf[1] = (byte) (v >>> 8);
byteBuf[2] = (byte) (v >>> 0);
out.write(byteBuf, 0, 3);
}
protected void writeInt24LE(ImageOutputStream out, int v) throws IOException {
byteBuf[2] = (byte) (v >>> 16);
byteBuf[1] = (byte) (v >>> 8);
byteBuf[0] = (byte) (v >>> 0);
out.write(byteBuf, 0, 3);
}
protected void writeInts24(ImageOutputStream out, int[] i, int off, int len) throws IOException {
// Fix 4430357 - if off + len < 0, overflow occurred
if (off < 0 || len < 0 || off + len > i.length || off + len < 0) {
throw new IndexOutOfBoundsException("off < 0 || len < 0 || off + len > i.length!");
}
byte[] b = new byte[len * 3];
int boff = 0;
for (int j = 0; j < len; j++) {
int v = i[off + j];
//b[boff++] = (byte)(v >>> 24);
b[boff++] = (byte) (v >>> 16);
b[boff++] = (byte) (v >>> 8);
b[boff++] = (byte) (v >>> 0);
}
out.write(b, 0, len * 3);
}
protected void writeInts24LE(ImageOutputStream out, int[] i, int off, int len) throws IOException {
// Fix 4430357 - if off + len < 0, overflow occurred
if (off < 0 || len < 0 || off + len > i.length || off + len < 0) {
throw new IndexOutOfBoundsException("off < 0 || len < 0 || off + len > i.length!");
}
byte[] b = new byte[len * 3];
int boff = 0;
for (int j = 0; j < len; j++) {
int v = i[off + j];
b[boff++] = (byte) (v >>> 0);
b[boff++] = (byte) (v >>> 8);
b[boff++] = (byte) (v >>> 16);
//b[boff++] = (byte)(v >>> 24);
}
out.write(b, 0, len * 3);
}
/** Copies a buffered image. */
protected static BufferedImage copyImage(BufferedImage img) {
ColorModel cm = img.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = img.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
}