-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractVideoCodecCore.java
More file actions
163 lines (141 loc) · 6.96 KB
/
AbstractVideoCodecCore.java
File metadata and controls
163 lines (141 loc) · 6.96 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
/*
* @(#)AbstractVideoCodecCore.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.io.IOException;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
/**
* {@code AbstractVideoCodecCore}.
*
* @author Werner Randelshofer
* @version $Id: AbstractVideoCodecCore.java 192 2012-03-29 22:00:37Z werner $
*/
public class AbstractVideoCodecCore {
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 {
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 = byteBuf;
for (int j = 0; j < len; j++) {
int v = i[off + j];
//b[boff++] = (byte)(v >>> 24);
b[0] = (byte) (v >>> 16);
b[1] = (byte) (v >>> 8);
b[2] = (byte) (v >>> 0);
out.write(b, 0, 3);
}
}
protected void writeInts24LE(ImageOutputStream out, int[] i, int off, int len) throws IOException {
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 = byteBuf;
for (int j = 0; j < len; j++) {
int v = i[off + j];
b[0] = (byte) (v >>> 0);
b[1] = (byte) (v >>> 8);
b[2] = (byte) (v >>> 16);
out.write(b, 0, 3);
}
}
protected void readInts24LE(ImageInputStream in, int[] i, int off, int len) throws IOException {
if (off < 0 || len < 0 || off + len > i.length || off + len < 0) {
throw new IndexOutOfBoundsException("off < 0 || len < 0 || off + len > i.length!, off=" + off + ", len=" + len);
}
byte[] b = byteBuf;
for (int j = off, end = off + len; j < end; j++) {
in.readFully(b, 0, 3);
int v = (b[0] & 0xff) | ((b[1] & 0xff) << 8) | ((b[2] & 0xff) << 16);
i[j] = v;
}
}
protected int readInt24LE(ImageInputStream in) throws IOException {
in.readFully(byteBuf, 0, 3);
return ((byteBuf[2] & 0xff) << 16) | ((byteBuf[1] & 0xff) << 8) | ((byteBuf[0] & 0xff) << 0);
}
/** Reads 16-bit RGB and converts it to 24-bit RGB. Endian is defined by input stream. */
protected void readRGBs565to24(ImageInputStream in, int[] i, int off, int len) throws IOException {
if (off < 0 || len < 0 || off + len > i.length || off + len < 0) {
throw new IndexOutOfBoundsException("off < 0 || len < 0 || off + len > i.length!, off=" + off + ", len=" + len);
}
for (int j = off, end = off + len; j < end; j++) {
int v = in.readUnsignedShort();
i[j] = ((v & 0xf800) << 8) | ((v & 0x3800) << 5)
| ((v & 0x07e0) << 5) | ((v & 0x0060) << 3)
| ((v & 0x001f) << 3) | ((v & 0x0007));
}
}
/** Reads 16-bit RGB and converts it to 24-bit RGB. Endian is defined by input stream. */
protected int readRGB565to24(ImageInputStream in) throws IOException {
int v = in.readUnsignedShort();
return ((v & 0xf800) << 8) | ((v & 0x3800) << 5)
| ((v & 0x07e0) << 5) | ((v & 0x0060) << 3)
| ((v & 0x001f) << 3) | ((v & 0x0007));
}
/** Reads 16-bit RGB and converts it to 24-bit RGB. Endian is defined by input stream. */
protected void readRGBs555to24(ImageInputStream in, int[] i, int off, int len) throws IOException {
if (off < 0 || len < 0 || off + len > i.length || off + len < 0) {
throw new IndexOutOfBoundsException("off < 0 || len < 0 || off + len > i.length!, off=" + off + ", len=" + len);
}
byte[] b = byteBuf;
for (int j = off, end = off + len; j < end; j++) {
int v = in.readUnsignedShort();
i[j]=((v & (0x1f<<10)) << 9) | ((v & (0x1c<<10)) << 4) // red
| ((v & (0x1f<<5)) << 6) | ((v & (0x1c<<5)) << 1) // green
| ((v & (0x1f<<0)) << 3) | ((v & (0x1c<<0)) >> 2); // blue;
}
}
/** Reads 16-bit RGB and converts it to 24-bit RGB. Endian is defined by input stream. */
protected int readRGB555to24(ImageInputStream in) throws IOException {
int v = in.readUnsignedShort();
return((v & (0x1f<<10)) << 9) | ((v & (0x1c<<10)) << 4) // red
| ((v & (0x1f<<5)) << 6) | ((v & (0x1c<<5)) << 1) // green
| ((v & (0x1f<<0)) << 3) | ((v & (0x1c<<0)) >> 2); // blue;
}
/** Reads 16-bit RGB and converts it to 24-bit RGB. Endian is defined by input stream. */
protected void readRGBs555to24LE(ImageInputStream in, int[] i, int off, int len) throws IOException {
if (off < 0 || len < 0 || off + len > i.length || off + len < 0) {
throw new IndexOutOfBoundsException("off < 0 || len < 0 || off + len > i.length!, off=" + off + ", len=" + len);
}
byte[] b = byteBuf;
for (int j = off, end = off + len; j < end; j++) {
int v = in.readUnsignedShort();
i[j]=((v & (0x1f<<0)) << 19) | ((v & (0x1c<<0)) << 14) // red
| ((v & (0x1f<<5)) << 6) | ((v & (0x1c<<5)) << 1) // green
| ((v & (0x1f<<10)) >>> 7) | ((v & (0x1c<<10)) >>> 12); // blue;
/* i[j]=((v & (0x1f<<10)) << 9) | ((v & (0x1c<<10)) << 4) // red
| ((v & (0x1f<<5)) << 6) | ((v & (0x1c<<5)) << 1) // green
| ((v & (0x1f<<0)) << 3) | ((v & (0x1c<<0)) >> 2); // blue;*/
}
}
/** Reads 16-bit RGB and converts it to 24-bit RGB. Endian is defined by input stream. */
protected int readRGB555to24LE(ImageInputStream in) throws IOException {
int v = in.readUnsignedShort();
return((v & (0x1f<<0)) << 19) | ((v & (0x1c<<0)) << 14) // red
| ((v & (0x1f<<5)) << 6) | ((v & (0x1c<<5)) << 1) // green
| ((v & (0x1f<<10)) >>> 7) | ((v & (0x1c<<10)) >>> 12); // blue;
/* return((v & (0x1f<<10)) << 9) | ((v & (0x1c<<10)) << 4) // red
| ((v & (0x1f<<5)) << 6) | ((v & (0x1c<<5)) << 1) // green
| ((v & (0x1f<<0)) << 3) | ((v & (0x1c<<0)) >> 2); // blue;
*/ }
}