initial commit
[kaka/cakelight.git] / src / kaka / cakelight / Frame.java
CommitLineData
e59e98fc
TW
1package kaka.cakelight;
2
3import org.opencv.core.CvType;
4import org.opencv.core.Mat;
5import org.opencv.core.Size;
6import org.opencv.imgproc.Imgproc;
7
8import static kaka.cakelight.Main.saveFile;
9import static kaka.cakelight.Main.timeIt;
10
11public class Frame {
12 private byte[] data;
13 private Configuration config;
14
15 private Frame(byte[] data) {
16 this.data = data;
17 }
18
19 public static Frame of(byte[] data, Configuration config) {
20 Frame frame = new Frame(data);
21 frame.config = config;
22 frame.convert();
23 return frame;
24 }
25
26 private void convert() {
27 Mat src = new Mat(config.video.height, config.video.width, CvType.CV_8UC2); // 8-bit, unsigned, 2 channels
28 src.put(0, 0, data);
29
30 Mat converted = new Mat();
31 Mat resized = new Mat();
32
33 timeIt("total", () -> {
34 timeIt("yuyv2rgb", () -> Imgproc.cvtColor(src, converted, Imgproc.COLOR_YUV2RGB_YUYV)); // 3.5 - 4.0 ms
35 timeIt("resizing", () -> Imgproc.resize(converted, resized, new Size(config.leds.cols, config.leds.rows), 0, 0, Imgproc.INTER_AREA)); // INTER_AREA is the best for shrinking, but also the slowest (~1.5 ms)
36 });
37// save(converted, "/home/kaka/test-converted.data");
38// save(resized, "/home/kaka/test-resized.data");
39 }
40
41 private void save(Mat mat, String filepath) {
42 byte[] data = new byte[mat.cols() * mat.rows() * mat.channels()];
43 mat.get(0, 0, data);
44 saveFile(data, filepath);
45 }
46
47 public byte[] getData() {
48 return data;
49 }
50}