-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteYCbCr.java
More file actions
268 lines (239 loc) · 5.67 KB
/
Copy pathWriteYCbCr.java
File metadata and controls
268 lines (239 loc) · 5.67 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package cn.edu.ustc.aaron.common;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;
import Jama.Matrix;
public class WriteYCbCr
{
private static WriteYCbCr writeYCbCrSingleton;
private DataOutputStream dos;
private FileWriter fw;
private byte[] oneLineY;
private byte[] oneLineCbCr;
private WriteYCbCr() {
}
public static WriteYCbCr getInstance() {
if (writeYCbCrSingleton == null) {
writeYCbCrSingleton = new WriteYCbCr();
}
return writeYCbCrSingleton;
}
public void writePic(String filename, int width, int height, int framesToBeWritten, ArrayList<Picture> picList) {
int widthC = width >> 1;
int heightC = height >> 1;
startWriting(filename, width, widthC);
Picture picTemp;
for (int framesNo = 0; framesNo < framesToBeWritten; framesNo++) {
picTemp = picList.get(framesNo);
writePlane(picTemp.getY(), oneLineY, width, height);
writePlane(picTemp.getCb(), oneLineCbCr, widthC, heightC);
writePlane(picTemp.getCr(), oneLineCbCr, widthC, heightC);
}
endWriting();
}
public void writeMatColByCol(Matrix[] pictureMat, int gopSize, int frameSize, boolean codeCbCr) {
int frameSizeC = frameSize >> 2;
for (int frameNo = 0; frameNo < gopSize; frameNo++) {
writePlaneColByCol(pictureMat[0].getArray(), oneLineY, frameNo * frameSize, frameSize);
if (codeCbCr) {
writePlaneColByCol(pictureMat[1].getArray(), oneLineCbCr, frameNo * frameSizeC, frameSizeC);
writePlaneColByCol(pictureMat[2].getArray(), oneLineCbCr, frameNo * frameSizeC, frameSizeC);
}
else {
writePlaneConstant(128, oneLineCbCr, frameSizeC);
writePlaneConstant(128, oneLineCbCr, frameSizeC);
}
}
}
public void startWriting(String filename, int width, int widthC)
{
try
{
dos = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(filename)));
oneLineY = new byte[width];
oneLineCbCr = new byte[widthC];
}
catch (Exception e)
{
e.printStackTrace();
}
}
private boolean writePlane(int[] src, byte[] tmp, int width, int height) {
try {
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
tmp[x] = (byte)src[y * width + x];
}
dos.write(tmp);
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
private boolean writePlaneColByCol(double[][] src, byte[] tmp, int startPos, int height) {
try {
for (int y = 0; y < height; y++)
{
tmp[y] = (byte)(src[y+startPos][0] + 0.5);
}
dos.write(tmp);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
private boolean writePlaneConstant(int src, byte[] tmp, int height) {
try {
for (int y = 0; y < height; y++)
{
tmp[y] = (byte)src;
}
dos.write(tmp);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
public void endWriting() {
try {
dos.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
private void endWritingTxt() {
try {
fw.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
private void startWritingTxt(String filename) {
try {
fw = new FileWriter(filename);
}
catch (Exception e) {
e.printStackTrace();
}
}
public boolean writeTxt(String filename, int[] src, int width, int height) {
startWritingTxt(filename);
try {
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
fw.write(String.valueOf(src[y * width + x])+"\t");
}
fw.write("\r\n");
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
finally {
endWritingTxt();
}
}
public boolean writeTxt(String filename, byte[] src, int width, int height) {
startWritingTxt(filename);
try {
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
fw.write(String.valueOf(src[y * width + x] & 0xFF)+"\t");
}
fw.write("\r\n");
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
finally {
endWritingTxt();
}
}
public boolean writeTxt(String filename, Matrix src) {
startWritingTxt(filename);
double[][] srcMat = src.getArray();
int width = src.getColumnDimension();
int height = src.getRowDimension();
try {
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
fw.write(String.format("%1.9g\t", srcMat[y][x]));
}
fw.write("\r\n");
}
fw.flush();
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
finally {
endWritingTxt();
}
}
private boolean writeData(LinkedList<Integer> src, int color) {
Iterator srcIt = src.iterator();
try {
int elem;
while (srcIt.hasNext()) {
elem = (int)srcIt.next();
dos.writeInt(elem);
// System.out.println(elem);
}
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean writeData(String filename, List<LinkedList<Integer>> srcList, boolean codeCbCr) {
try {
startWriting(filename, 0, 0);
writeData(srcList.get(0), 0);
if (codeCbCr) {
writeData(srcList.get(1), 1);
writeData(srcList.get(2), 2);
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
finally {
endWriting();
return true;
}
}
}