package net.heartshield.signal;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-04-09
 */
public class CameraUtil {
    static public double fractionRed(int[] rgb, int width, int height) {
        long nred = 0;
        for(int j = 0, yp = 0; j < height; j++) {
            for(int i = 0; i < width; i++, yp++) {
                final int r = (rgb[yp] & 0xff0000) >> 16;
                final int g = (rgb[yp] & 0xff00) >> 8;
                final int b = (rgb[yp] & 0xff);
                if(r > g && r > b)
                    nred++;
            }
        }
        return ((double) nred) / (width * height);
    }

    /**
     * Compute column-wise mean.
     *
     * @param rgb     input image pixel matrix
     * @param width   input width
     * @param height  input height
     * @return        output row (length = width)
     */
    static public double[][] meanRow(int[] rgb, int width, int height) {
        double[][] rgbMean = new double[3][width];

        for(int j = 0, yp = 0; j < height; j++) {
            for(int i = 0; i < width; i++, yp++) {
                rgbMean[0][i] += (rgb[yp] & 0xff0000) >> 16;
                rgbMean[1][i] += (rgb[yp] & 0xff00) >> 8;
                rgbMean[2][i] += (rgb[yp] & 0xff);
            }
        }

        for(int i = 0; i < width; i++) {
            for(int k = 0; k < 3; k++)
                rgbMean[k][i] /= height;
        }
        return rgbMean;
    }

    /**
     * Compute row-wise mean.
     *
     * @param rgb     input image pixel matrix
     * @param width   input width
     * @param height  input height
     * @return        output column (length = height)
     */
    static public double[][] meanCol(int[] rgb, int width, int height) {
        double[][] rgbMean = new double[3][height];

        for(int j = 0, yp = 0; j < height; j++) {
            for(int i = 0; i < width; i++, yp++) {
                rgbMean[0][j] += (rgb[yp] & 0xff0000) >> 16;
                rgbMean[1][j] += (rgb[yp] & 0xff00) >> 8;
                rgbMean[2][j] += (rgb[yp] & 0xff);
            }
        }

        for(int i = 0; i < height; i++) {
            for(int k = 0; k < 3; k++)
                rgbMean[k][i] /= width;
        }
        return rgbMean;
    }

    /**
     * Compute column-wise standard deviation.
     *
     * @param rgb     input image pixel matrix
     * @param width   input width
     * @param height  input height
     * @return        output row (length = width)
     */
    static public double[][] stdRow(int[] rgb, double[][] rgbMean, int width, int height) {
        double[][] rgbStd = new double[3][width];

        for(int j = 0, yp = 0; j < height; j++) {
            for(int i = 0; i < width; i++, yp++) {
                double r = ((rgb[yp] & 0xff0000) >> 16) - rgbMean[0][i];
                double g = ((rgb[yp] & 0xff00) >> 8) - rgbMean[1][i];
                double b = (rgb[yp] & 0xff) - rgbMean[2][i];
                rgbStd[0][i] += r * r;
                rgbStd[1][i] += g * g;
                rgbStd[2][i] += b * b;
            }
        }

        for(int i = 0; i < width; i++) {
            for(int k = 0; k < 3; k++)
                rgbStd[k][i] = Math.sqrt(rgbStd[k][i] / height);
        }
        return rgbStd;
    }

    /**
     * Compute row-wise standard deviation.
     *
     * @param rgb     input image pixel matrix
     * @param width   input width
     * @param height  input height
     * @return        output row (length = height)
     */
    static public double[][] stdCol(int[] rgb, double[][] rgbMean, int width, int height) {
        double[][] rgbStd = new double[3][height];

        for(int j = 0, yp = 0; j < height; j++) {
            for(int i = 0; i < width; i++, yp++) {
                double r = ((rgb[yp] & 0xff0000) >> 16) - rgbMean[0][j];
                double g = ((rgb[yp] & 0xff00) >> 8) - rgbMean[1][j];
                double b = (rgb[yp] & 0xff) - rgbMean[2][j];
                rgbStd[0][j] += r * r;
                rgbStd[1][j] += g * g;
                rgbStd[2][j] += b * b;
            }
        }

        for(int i = 0; i < height; i++) {
            for(int k = 0; k < 3; k++)
                rgbStd[k][i] = Math.sqrt(rgbStd[k][i] / width);
        }
        return rgbStd;
    }

    static public void decodeYUV420SP(int[] rgb, double[] mean_rgb, byte[] yuv420sp, int width, int height) {
        final int frameSize = width * height;
        long rr = 0, gg = 0, bb = 0;

        for (int j = 0, yp = 0; j < height; j++) {
            int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
            for (int i = 0; i < width; i++, yp++) {
                int y = (0xff & ((int) yuv420sp[yp])) - 16;
                if (y < 0) y = 0;
                if ((i & 1) == 0) {
                    u = (0xff & yuv420sp[uvp++]) - 128;
                    v = (0xff & yuv420sp[uvp++]) - 128;
                }

                int y1192 = 1192 * y;
                int r = (y1192 + 1634 * v);
                int g = (y1192 - 833 * v - 400 * u);
                int b = (y1192 + 2066 * u);

                if (r < 0) r = 0; else if (r > 262143) r = 262143;
                if (g < 0) g = 0; else if (g > 262143) g = 262143;
                if (b < 0) b = 0; else if (b > 262143) b = 262143;

                rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);

                rr += (r >> 10) & 0xff;
                gg += (g >> 10) & 0xff;
                bb += (b >> 10) & 0xff;
            }
        }

        mean_rgb[0] = ((double) rr) / (width * height);
        mean_rgb[1] = ((double) gg) / (width * height);
        mean_rgb[2] = ((double) bb) / (width * height);
    }
}
