-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadYCbCr.java
More file actions
162 lines (138 loc) · 3.44 KB
/
Copy pathReadYCbCr.java
File metadata and controls
162 lines (138 loc) · 3.44 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
package cn.edu.ustc.aaron.common;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.*;
import java.util.*;
public class ReadYCbCr
{
private static ReadYCbCr readYCbCrSingleton;
private DataInputStream dis;
private DataInputStream disData;
private byte[] oneLineY;
private byte[] oneLineCbCr;
private ReadYCbCr() {
}
public static ReadYCbCr getInstance() {
if (readYCbCrSingleton == null) {
readYCbCrSingleton = new ReadYCbCr();
}
return readYCbCrSingleton;
}
public void readPic(int width, int height, int startFrame, int framesToBeCoded, ArrayList<Picture> picList) {
int widthC = width >> 1;
int heightC = height >> 1;
for (int framesNo = startFrame; framesNo < startFrame + framesToBeCoded; framesNo++) {
Picture picTemp;
if (startFrame == 0) {
picTemp = new Picture(width, height);
//System.out.println(framesNo+"size:"+picList.size());
picList.add(picTemp);
} else {
picTemp = picList.get(framesNo % picList.size());
//System.out.println(framesNo+"nosizeup,size:"+picList.size());
}
readPlane(picTemp.getY(), oneLineY, width, height);
readPlane(picTemp.getCb(), oneLineCbCr, widthC, heightC);
readPlane(picTemp.getCr(), oneLineCbCr, widthC, heightC);
}
}
public void startReading(String filename, int width, int widthC) {
try
{
dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(filename)));
oneLineY = new byte[width];
oneLineCbCr = new byte[widthC];
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void startReading(DataInputStream disData, String filename, int width, int widthC) {
try
{
disData = new DataInputStream(new BufferedInputStream(
new FileInputStream(filename)));
this.disData = disData;
oneLineY = new byte[width];
oneLineCbCr = new byte[widthC];
}
catch (Exception e)
{
e.printStackTrace();
}
}
private boolean readPlane(int[] dst, byte[] tmp, int width, int height) {
try
{
for (int y = 0; y < height; y++)
{
dis.read(tmp);
for (int x = 0; x < width; x++)
{
dst[y * width + x] = unsignedByteToInt(tmp[x]);
}
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
public void endReading(DataInputStream disData) {
try {
disData.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public void endReading() {
try {
dis.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
private static int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}
private boolean readData(LinkedList<Integer> src, int total, int color) {
try {
int count = 0;
while (count < total) {
src.offer(disData.readInt());
// System.out.println(src.peekLast());
count++;
}
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean readData(String filename, List<LinkedList<Integer>> srcList, int total, boolean codeCbCr) {
try {
startReading(disData, filename, 0, 0);
readData(srcList.get(0), total, 0);
if (codeCbCr) {
readData(srcList.get(1), total, 1);
readData(srcList.get(2), total, 2);
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
finally {
endReading(disData);
return true;
}
}
}