WIP
[kaka/cakelight.git] / src / kaka / cakelight / Frame.java
index 0031abc..a162024 100644 (file)
@@ -5,12 +5,16 @@ import org.opencv.core.Mat;
 import org.opencv.core.Size;
 import org.opencv.imgproc.Imgproc;
 
+import java.awt.*;
+
 import static kaka.cakelight.Main.saveFile;
 import static kaka.cakelight.Main.timeIt;
 
 public class Frame {
     private byte[] data;
     private Configuration config;
+    private Mat colImage;
+    private Mat rowImage;
 
     private Frame(byte[] data) {
         this.data = data;
@@ -24,18 +28,70 @@ public class Frame {
     }
 
     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.
+        2) Resize to 16x9 and use 2 pixels of depth (or maybe 3) and interpolate for each led.
+        3) Resize to 2 images where each led uses 2 pixels:
+            vertical   - 16 x <#leds>
+            horizontal - <#leds> x 9
+         */
         Mat src = new Mat(config.video.height, config.video.width, CvType.CV_8UC2); // 8-bit, unsigned, 2 channels
         src.put(0, 0, data);
 
-        Mat converted = new Mat();
-        Mat resized = new Mat();
+//        Mat converted = new Mat();
+//        Mat resized = new Mat();
+//
+//        timeIt("total", () -> {
+//            timeIt("yuyv2rgb", () -> Imgproc.cvtColor(src, converted, Imgproc.COLOR_YUV2RGB_YUYV)); // 3.5 - 4.0 ms
+//            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)
+//        });
 
-        timeIt("total", () -> {
-            timeIt("yuyv2rgb", () -> Imgproc.cvtColor(src, converted, Imgproc.COLOR_YUV2RGB_YUYV)); // 3.5 - 4.0 ms
-            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)
-        });
+        Mat converted = new Mat();
+        Imgproc.cvtColor(src, converted, Imgproc.COLOR_YUV2RGB_YUYV);
+        timeIt("model 1", () -> model1(converted, Imgproc.INTER_AREA));
+        timeIt("model 2", () -> model2(converted, Imgproc.INTER_AREA));
+        timeIt("model 3", () -> model3(converted, Imgproc.INTER_AREA));
 //        save(converted, "/home/kaka/test-converted.data");
 //        save(resized, "/home/kaka/test-resized.data");
+        System.out.println("color: " + getPixel(ListPosition.BOTTOM, 0));
+    }
+
+    private void model1(Mat src, int interpolation) {
+        Mat resized = new Mat();
+        Imgproc.resize(src, resized, new Size(config.leds.cols, config.leds.rows), 0, 0, interpolation);
+    }
+
+    private void model2(Mat src, int interpolation) {
+        Mat resized = new Mat();
+        Imgproc.resize(src, resized, new Size(16, 9), 0, 0, interpolation);
+    }
+
+    private void model3(Mat src, int interpolation) {
+        colImage = new Mat();
+        rowImage = new Mat();
+        Imgproc.resize(src, colImage, new Size(config.leds.cols, 9), 0, 0, interpolation);
+        Imgproc.resize(src, rowImage, new Size(16, config.leds.rows), 0, 0, interpolation);
+    }
+
+    public Color getPixel(ListPosition listPosition, int xy) {
+        switch (listPosition) {
+            case LEFT:
+                return pixelToColor(rowImage, 0, xy);
+            case RIGHT:
+                return pixelToColor(rowImage, config.leds.cols - 1, xy);
+            case TOP:
+                return pixelToColor(colImage, xy, 0);
+            case BOTTOM:
+                return pixelToColor(colImage, xy, config.leds.cols - 1);
+        }
+        return null;
+    }
+
+    private Color pixelToColor(Mat image, int x, int y) {
+        byte[] rgb = new byte[3];
+        image.get(y, x, rgb);
+        System.out.println("r = " + rgb[0] + ", g = " + rgb[1] + ", b = " + rgb[2]);
+        return new Color(rgb[0], rgb[1], rgb[2]);
     }
 
     private void save(Mat mat, String filepath) {