Add very special handling for the MJPG video format
[kaka/cakelight.git] / src / kaka / cakelight / VideoFrame.java
index a3353a5..fd055a9 100644 (file)
@@ -1,5 +1,6 @@
 package kaka.cakelight;
 
+import org.opencv.core.Core;
 import org.opencv.core.CvType;
 import org.opencv.core.Mat;
 import org.opencv.core.Size;
@@ -23,10 +24,37 @@ public class VideoFrame {
     public static VideoFrame of(byte[] data, Configuration config) {
         VideoFrame frame = new VideoFrame(data);
         frame.config = config;
-        frame.convert();
+        if (config.video.mjpg) {
+            frame.convertJpg();
+        } else {
+            frame.convert();
+        }
         return frame;
     }
 
+    private void convertJpg() {
+        Mat src = new Mat(config.video.height, config.video.width, CvType.CV_8UC3); // 8-bit, unsigned, 3 channels
+        src.put(0, 0, data);
+//        save(src, "/home/kaka/test-src.data");
+
+        converted = new Mat();
+        Imgproc.cvtColor(src, converted, Imgproc.COLOR_BGR2RGB);
+
+        Core.flip(converted, converted, 0); // up-side down
+//        save(converted, "/home/kaka/test-converted.data");
+        int mysteriousPixelShift = 18;
+        converted = converted.submat( // crop mysterious pixel shift
+                0,
+                converted.rows(),
+                mysteriousPixelShift,
+                converted.cols() - mysteriousPixelShift
+        );
+//        save(converted, "/home/kaka/test-croppedAgain.data");
+        model4(converted, Imgproc.INTER_AREA);
+        src.release();
+        converted.release();
+    }
+
     private void convert() {
         /* TODO: how to do this?
         1) Resize to an image with the size of the number of leds and use config to define how many pixels deep into the screen to use.