package epson.colorcorrection;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.media.ExifInterface;
import android.os.Debug;
import android.os.Environment;

import android.text.format.DateFormat;
import android.util.Log;
import com.epson.cameracopy.p006ui.CropImageActivity;
import epson.print.Util.OpenCvHelper;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ImageCollect {
    private static final float BLUR_RATE = 0.6f;
    private static final float DETECT_AREA_RATIO = 0.8f;
    private static final String ICPD_FILE_NAME_FORMAT = "yyyyMMddkkmm_";
    private static final String ICPD_PREFIX_PRINT_DATA = "icpd_";
    private static final String ICPD_PREFIX_SEPARATOR = "/";
    private static final String ICPD_USER_FOLDER = "/EpsonCameraCapture";
    public static final int IMAGE_TYPE_PREVIEW_BCSC = 4;
    public static final int IMAGE_TYPE_PREVIEW_BCSC_DST = 6;
    public static final int IMAGE_TYPE_PREVIEW_BCSC_MSK_ALL = 7;
    public static final int IMAGE_TYPE_PREVIEW_BCSC_MSK_PRT = 8;
    public static final int IMAGE_TYPE_PREVIEW_BCSC_MSK_PSM = 9;
    public static final int IMAGE_TYPE_PREVIEW_BCSC_SRC = 5;
    public static final int IMAGE_TYPE_PREVIEW_COUNT = 10;
    public static final int IMAGE_TYPE_PREVIEW_CROP = 1;
    public static final int IMAGE_TYPE_PREVIEW_PWC1 = 2;
    public static final int IMAGE_TYPE_PREVIEW_PWC2 = 3;
    public static final int IMAGE_TYPE_PREVIEW_RAW = 0;
    public static final int IMAGE_TYPE_PRINT_BCSC = 4;
    public static final int IMAGE_TYPE_PRINT_COUNT = 5;
    public static final int IMAGE_TYPE_PRINT_CROP = 1;
    public static final int IMAGE_TYPE_PRINT_PWC1 = 2;
    public static final int IMAGE_TYPE_PRINT_PWC2 = 3;
    public static final int IMAGE_TYPE_PRINT_RAW = 0;
    public static final float MASK_LINE_WIDTH = 20.0f;
    private static final float MASK_RADIUS = 20.0f;
    public static final int PWCTYPE_0 = 0;
    public static final int PWCTYPE_1 = 1;
    public static final int PWCTYPE_2 = 2;
    public static final int PWCTYPE_w = -1;
    private static final double RESOLUTION_THRESHOLD = 240.0d;
    private static final int SAVE_JPEG_QUALITY = 90;
    private static final float SMALL_PREVIEW_RATE = 2.0f;
    private File mCacheFolder = null;
    private String mCacheSaveName = null;
    private Size mCurrentPaperSize = null;
    private float mDensity = 0.0f;
    private String mLoadPath = null;
    private Mat[] mMatPreview = null;
    private Mat[] mMatPrint = null;
    private PointF[] mPointEditPts = new PointF[4];
    private RectF mRectEditCrop = new RectF(0.0f, 0.0f, 0.0f, 0.0f);
    private double mResolutionThreshold = RESOLUTION_THRESHOLD;
    private String mSaveDisplayPath = null;
    private String mSaveFullPath = null;
    private float mSmallPreviewRate = 2.0f;
    private File mUserFolder = null;
    private String mUserSaveName = null;
    private String mUserSelectedFolder = null;

    static {
        System.loadLibrary("opencv_java3");
    }

    public ImageCollect(String str, float f, Size size) {
        mLoadPath = new String(str);
        mMatPreview = new Mat[10];
        for (int i = 0; i < 10; i++) {
            mMatPreview[i] = null;
        }
        mMatPrint = new Mat[5];
        for (int i2 = 0; i2 < 5; i2++) {
            mMatPrint[i2] = null;
        }
        mDensity = f;
        mPointEditPts[0] = new PointF(0.0f, 0.0f);
        mPointEditPts[1] = new PointF(0.0f, 0.0f);
        mPointEditPts[2] = new PointF(0.0f, 0.0f);
        mPointEditPts[3] = new PointF(0.0f, 0.0f);
        mCurrentPaperSize = size.clone();
    }

    public void SetResolutionThreshold(double d) {
        mResolutionThreshold = d;
    }

    public void SetSmallPreviewRate(float f) {
        mSmallPreviewRate = f;
    }

    public void SetPath(File file, File file2, String str) {
        if (str == null || str.isEmpty()) {
            mUserSelectedFolder = ICPD_USER_FOLDER;
        } else {
            if (!str.equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath() + ICPD_USER_FOLDER)) {
                mUserSelectedFolder = new String(str).replaceFirst(file2.getPath() + "/", "/");
            } else {
                mUserSelectedFolder = ICPD_USER_FOLDER;
            }
        }
        mCacheFolder = file;
        mCacheSaveName = file.getPath();
        mUserSaveName = file2.getPath() + mUserSelectedFolder;
        mUserFolder = new File(mUserSaveName);
        if (!mCacheFolder.exists()) {
            mCacheFolder.mkdir();
        }
        if (!mUserFolder.exists()) {
            mUserFolder.mkdir();
        }
    }

    public void DeleteCacheFile() {
        DeleteCacheFileAll(mCacheFolder, ICPD_PREFIX_PRINT_DATA);
    }

    private int DeleteCacheFileAll(File file, String str) {
        File[] listFiles = file.listFiles(new CacheFileFilter(str));
        if (listFiles == null) {
            return 0;
        }
        int length = listFiles.length;
        for (File delete : listFiles) {
            delete.delete();
        }
        return length;
    }

    private class CacheFileFilter implements FileFilter {
        private String mPrefix = "";

        CacheFileFilter(String str) {
            mPrefix = str;
        }

        public boolean accept(File file) {
            if (file.getName().substring(0, mPrefix.length()).equalsIgnoreCase(mPrefix)) {
                return true;
            }
            return false;
        }
    }

    public String GetSavePath(boolean z) {
        if (z) {
            return mSaveFullPath;
        }
        return mSaveDisplayPath;
    }

    public Point GetMakedPreviewImageSize() {
        Point point = new Point(0, 0);
        Mat[] matArr = mMatPreview;
        if (matArr[1] != null) {
            point.x = matArr[1].cols();
            point.y = mMatPreview[1].rows();
        }
        return point;
    }

    public Mat MakeCropPrintSizeImage(PointF[] pointFArr, Size size, int i, List<CropImageActivity.CorrectionParamSet> list) {
        int i2;
        Mat mat;
        int i3;
        List<CropImageActivity.CorrectionParamSet> list2 = list;
        Mat[] matArr = mMatPrint;
        Mat mat2 = null;
        if (matArr[0] == null || mMatPreview[0] == null) {
            return null;
        }
        PointF[] GetCropPointsAdjustment = GetCropPointsAdjustment(pointFArr, ((float) matArr[0].width()) / ((float) mMatPreview[0].width()));
        RectF GetCropBoundRect = GetCropBoundRect(GetCropPointsAdjustment);
        if (((mPointEditPts[0].x == GetCropPointsAdjustment[0].x && mPointEditPts[0].y == GetCropPointsAdjustment[0].y && mPointEditPts[1].x == GetCropPointsAdjustment[1].x && mPointEditPts[1].y == GetCropPointsAdjustment[1].y && mPointEditPts[2].x == GetCropPointsAdjustment[2].x && mPointEditPts[2].y == GetCropPointsAdjustment[2].y && mPointEditPts[3].x == GetCropPointsAdjustment[3].x && mPointEditPts[3].y == GetCropPointsAdjustment[3].y && mRectEditCrop.equals(GetCropBoundRect)) ? false : true) || mMatPrint[1] == null) {
            Mat[] matArr2 = mMatPrint;
            if (matArr2[1] != null) {
                matArr2[1].release();
                mMatPrint[1] = null;
            }
            Mat[] matArr3 = mMatPrint;
            matArr3[1] = GetCropImage(matArr3[0], GetCropPointsAdjustment, GetCropBoundRect, size, (Size) null);
            mRectEditCrop.set(GetCropBoundRect);
            mPointEditPts[0].set(GetCropPointsAdjustment[0]);
            mPointEditPts[1].set(GetCropPointsAdjustment[1]);
            mPointEditPts[2].set(GetCropPointsAdjustment[2]);
            mPointEditPts[3].set(GetCropPointsAdjustment[3]);
            i2 = i;
        } else {
            i2 = i;
        }
        if (i2 == 1) {
            return mMatPrint[1];
        }
        Mat mat3 = mMatPrint[1];
        int size2 = list.size();
        if (size2 > 0) {
            i2 = list2.get(0).GetImageType();
        }
        if (i2 == 2) {
            Mat[] matArr4 = mMatPrint;
            if (matArr4[2] != null) {
                matArr4[2].release();
                mMatPrint[2] = null;
            }
            SDIC_MAT_PTR sdic_mat_ptr = new SDIC_MAT_PTR();
            if (SDIC_PWC.PaperWhiteCorrection(mMatPrint[1], sdic_mat_ptr) == 0) {
                Mat[] matArr5 = mMatPrint;
                mat2 = sdic_mat_ptr.getMat();
                matArr5[2] = mat2;
            }
        } else if (i2 == 3) {
            Mat[] matArr6 = mMatPrint;
            if (matArr6[3] != null) {
                matArr6[3].release();
                mMatPrint[3] = null;
            }
            SDIC_MAT_PTR sdic_mat_ptr2 = new SDIC_MAT_PTR();
            if (SDIC_PWC.PaperWhiteCorrection2(mMatPrint[1], sdic_mat_ptr2) == 0) {
                Mat[] matArr7 = mMatPrint;
                mat2 = sdic_mat_ptr2.getMat();
                matArr7[3] = mat2;
            }
        }
        if (size2 <= 0) {
            return mat2;
        }
        if (i2 == 2) {
            mat = mMatPrint[2];
        } else if (i2 == 3) {
            mat = mMatPrint[3];
        } else {
            mat = mMatPrint[1];
        }
        Mat clone = mat.clone();
        for (int i4 = 0; i4 < list.size(); i4++) {
            CropImageActivity.CorrectionParamSet correctionParamSet = list2.get(i4);
            if (correctionParamSet.Size() > 0) {
                clone = MakeImageBCSC(clone, correctionParamSet);
                int GetAfterPWC = correctionParamSet.GetAfterPWC();
                if (GetAfterPWC > 0) {
                    SDIC_MAT_PTR sdic_mat_ptr3 = new SDIC_MAT_PTR();
                    if (GetAfterPWC == 1) {
                        i3 = SDIC_PWC.PaperWhiteCorrection(clone, sdic_mat_ptr3);
                    } else {
                        i3 = SDIC_PWC.PaperWhiteCorrection2(clone, sdic_mat_ptr3);
                    }
                    if (i3 == 0) {
                        if (clone != null) {
                            clone.release();
                        }
                        clone = sdic_mat_ptr3.getMat();
                    }
                }
            }
        }
        Mat[] matArr8 = mMatPrint;
        matArr8[4] = clone;
        return matArr8[4];
    }

    public boolean SaveExecute(boolean z, int i, Size size) {
        Mat mat;
        String str;
        boolean z2 = false;
        if (i == 4) {
            mat = mMatPrint[4];
        } else if (i == 2) {
            mat = mMatPrint[2];
        } else if (i == 3) {
            mat = mMatPrint[3];
        } else if (i == 1) {
            mat = mMatPrint[1];
        } else {
            mat = mMatPrint[0];
        }
        mSaveDisplayPath = null;
        mSaveFullPath = null;
        String str2 = (String) DateFormat.format(ICPD_FILE_NAME_FORMAT, new Date());
        if (ensureDirectory(z ? new File(mCacheSaveName) : new File(mUserSaveName))) {
            return false;
        }
        int i2 = 0;
        while (true) {
            if (i2 >= 100) {
                break;
            }
            String format = String.format("%s%02d.jpg", new Object[]{str2, Integer.valueOf(i2)});
            if (z) {
                str = mCacheSaveName + "/" + ICPD_PREFIX_PRINT_DATA + format;
            } else {
                str = mUserSaveName + "/" + format;
            }
            File file = new File(str);
            if (!file.exists()) {
                MatOfInt matOfInt = new MatOfInt(2, 1, 4);
                matOfInt.put(0, 0, new int[]{1, 90});
                if (size == null || size.width <= 0.0d || size.height <= 0.0d) {
                    z2 = Imgcodecs.imwrite(str, mat, matOfInt);
                } else {
                    z2 = Imgcodecs.imwrite(str, mat, matOfInt);
                }
                matOfInt.release();
                if (z2) {
                    mSaveFullPath = str;
                    if (!z) {
                        mSaveDisplayPath = file.getParentFile().getName();
                    }
                }
            } else {
                i2++;
            }
        }
        return z2;
    }

    private boolean ensureDirectory(@NonNull File file) {
        return !file.exists() && !file.mkdirs();
    }

    private void ExecPwc(int i, int i2, int i3) {
        int i4;
        SDIC_MAT_PTR sdic_mat_ptr = new SDIC_MAT_PTR();
        if (i3 == 1) {
            i4 = SDIC_PWC.PaperWhiteCorrection(mMatPreview[i], sdic_mat_ptr);
        } else {
            i4 = SDIC_PWC.PaperWhiteCorrection2(mMatPreview[i], sdic_mat_ptr);
        }
        if (i4 == 0) {
            Mat[] matArr = mMatPreview;
            if (matArr[i2] != null) {
                matArr[i2].release();
                mMatPreview[i2] = null;
            }
            mMatPreview[i2] = sdic_mat_ptr.getMat();
        }
    }

    private Mat MakeCropImage(Mat mat, PointF[] pointFArr, float f, Size size, Size size2) {
        PointF[] GetCropPointsAdjustment = GetCropPointsAdjustment(pointFArr, f);
        return GetCropImage(mat, GetCropPointsAdjustment, GetCropBoundRect(GetCropPointsAdjustment), size, size2);
    }

    private PointF[] GetCropPointsAdjustment(PointF[] pointFArr, float f) {
        if (pointFArr == null || pointFArr.length != 4) {
            return null;
        }
        PointF[] pointFArr2 = new PointF[4];
        for (int i = 0; i < 4; i++) {
            pointFArr2[i] = new PointF();
            pointFArr2[i].x = pointFArr[i].x * f;
            pointFArr2[i].y = pointFArr[i].y * f;
        }
        return pointFArr2;
    }

    private RectF GetCropBoundRect(PointF[] pointFArr) {
        if (pointFArr == null || pointFArr.length != 4) {
            return null;
        }
        return new RectF(0.0f, 0.0f, ((pointFArr[3].x - pointFArr[0].x) + (pointFArr[2].x - pointFArr[1].x)) / 2.0f, ((pointFArr[1].y - pointFArr[0].y) + (pointFArr[2].y - pointFArr[3].y)) / 2.0f);
    }

    /* JADX WARNING: Removed duplicated region for block: B:27:0x0070  */
    /* JADX WARNING: Removed duplicated region for block: B:28:0x00a4  */
    /* JADX WARNING: Removed duplicated region for block: B:31:0x00dc  */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x0127  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private org.opencv.core.Mat GetCropImage(org.opencv.core.Mat r26, android.graphics.PointF[] r27, android.graphics.RectF r28, org.opencv.core.Size r29, org.opencv.core.Size r30) {
        /*
            r25 = this;
            r10 = r25
            r0 = r27
            r11 = r29
            r1 = r30
            if (r0 == 0) goto L_0x0228
            int r2 = r0.length
            r12 = 4
            if (r2 != r12) goto L_0x0228
            org.opencv.core.Mat r13 = new org.opencv.core.Mat
            r14 = 5
            r15 = 2
            r13.<init>((int) r12, (int) r15, (int) r14)
            org.opencv.core.Mat r9 = new org.opencv.core.Mat
            r9.<init>((int) r12, (int) r15, (int) r14)
            r2 = 8
            float[] r3 = new float[r2]
            float[] r7 = new float[r2]
            r4 = 0
            r16 = 1
            r8 = 0
            if (r11 == 0) goto L_0x0067
            r18 = r13
            double r12 = r11.width
            int r2 = (r12 > r4 ? 1 : (r12 == r4 ? 0 : -1))
            if (r2 <= 0) goto L_0x0069
            double r12 = r11.height
            int r2 = (r12 > r4 ? 1 : (r12 == r4 ? 0 : -1))
            if (r2 <= 0) goto L_0x0069
            double r12 = r11.width
            double r4 = r11.height
            int r2 = (r12 > r4 ? 1 : (r12 == r4 ? 0 : -1))
            if (r2 <= 0) goto L_0x004d
            float r2 = r28.width()
            float r4 = r28.height()
            int r2 = (r2 > r4 ? 1 : (r2 == r4 ? 0 : -1))
            if (r2 >= 0) goto L_0x004b
            r2 = 1
            goto L_0x006a
        L_0x004b:
            r2 = 0
            goto L_0x006a
        L_0x004d:
            double r4 = r11.width
            double r12 = r11.height
            int r2 = (r4 > r12 ? 1 : (r4 == r12 ? 0 : -1))
            if (r2 >= 0) goto L_0x0065
            float r2 = r28.width()
            float r4 = r28.height()
            int r2 = (r2 > r4 ? 1 : (r2 == r4 ? 0 : -1))
            if (r2 >= 0) goto L_0x0063
            r2 = 0
            goto L_0x006a
        L_0x0063:
            r2 = 1
            goto L_0x006a
        L_0x0065:
            r2 = 0
            goto L_0x006a
        L_0x0067:
            r18 = r13
        L_0x0069:
            r2 = 0
        L_0x006a:
            r12 = 7
            r13 = 6
            r20 = 3
            if (r2 != 0) goto L_0x00a4
            r2 = r0[r8]
            float r2 = r2.x
            r3[r8] = r2
            r2 = r0[r8]
            float r2 = r2.y
            r3[r16] = r2
            r2 = r0[r16]
            float r2 = r2.x
            r3[r15] = r2
            r2 = r0[r16]
            float r2 = r2.y
            r3[r20] = r2
            r2 = r0[r15]
            float r2 = r2.x
            r4 = 4
            r3[r4] = r2
            r2 = r0[r15]
            float r2 = r2.y
            r3[r14] = r2
            r2 = r0[r20]
            float r2 = r2.x
            r3[r13] = r2
            r0 = r0[r20]
            float r0 = r0.y
            r3[r12] = r0
            r5 = r18
            goto L_0x00d7
        L_0x00a4:
            r2 = r0[r16]
            float r2 = r2.x
            r3[r8] = r2
            r2 = r0[r16]
            float r2 = r2.y
            r3[r16] = r2
            r2 = r0[r15]
            float r2 = r2.x
            r3[r15] = r2
            r2 = r0[r15]
            float r2 = r2.y
            r3[r20] = r2
            r2 = r0[r20]
            float r2 = r2.x
            r4 = 4
            r3[r4] = r2
            r2 = r0[r20]
            float r2 = r2.y
            r3[r14] = r2
            r2 = r0[r8]
            float r2 = r2.x
            r3[r13] = r2
            r0 = r0[r8]
            float r0 = r0.y
            r3[r12] = r0
            r5 = r18
        L_0x00d7:
            r5.put((int) r8, (int) r8, (float[]) r3)
            if (r1 == 0) goto L_0x0127
            if (r11 == 0) goto L_0x011e
            double r2 = r11.width
            r18 = 0
            int r0 = (r2 > r18 ? 1 : (r2 == r18 ? 0 : -1))
            if (r0 <= 0) goto L_0x011e
            double r2 = r11.height
            int r0 = (r2 > r18 ? 1 : (r2 == r18 ? 0 : -1))
            if (r0 <= 0) goto L_0x011e
            double r2 = r11.width
            r18 = r7
            double r6 = r11.height
            r19 = r9
            double r8 = r1.width
            double r0 = r1.height
            r11 = 1
            r21 = r0
            r0 = r25
            r1 = r2
            r3 = r6
            r7 = r5
            r12 = 0
            r5 = r8
            r9 = r7
            r13 = r18
            r14 = 0
            r7 = r21
            r23 = r9
            r24 = r19
            r9 = r11
            org.opencv.core.Size r0 = r0.CalcResizeSize(r1, r3, r5, r7, r9)
            android.graphics.RectF r1 = new android.graphics.RectF
            double r2 = r0.width
            float r2 = (float) r2
            double r3 = r0.height
            float r0 = (float) r3
            r1.<init>(r12, r12, r2, r0)
            goto L_0x01b9
        L_0x011e:
            r23 = r5
            r13 = r7
            r24 = r9
            r12 = 0
            r14 = 0
            goto L_0x01b7
        L_0x0127:
            r23 = r5
            r13 = r7
            r24 = r9
            r12 = 0
            r14 = 0
            float r0 = r28.width()
            double r5 = (double) r0
            float r0 = r28.height()
            double r7 = (double) r0
            r21 = 4645040803167600640(0x4076800000000000, double:360.0)
            if (r11 == 0) goto L_0x018d
            double r0 = r11.width
            r2 = 0
            int r4 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            if (r4 <= 0) goto L_0x018d
            double r0 = r11.height
            int r4 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            if (r4 <= 0) goto L_0x018d
            double r1 = r11.width
            double r3 = r11.height
            r9 = 0
            r0 = r25
            org.opencv.core.Size r0 = r0.CalcResizeSize(r1, r3, r5, r7, r9)
            double r1 = r11.width
            double r1 = r1 / r21
            double r3 = r11.height
            double r3 = r3 / r21
            double r5 = r0.width
            double r5 = r5 / r1
            double r7 = r0.height
            double r7 = r7 / r3
            int r9 = (r5 > r7 ? 1 : (r5 == r7 ? 0 : -1))
            if (r9 <= 0) goto L_0x016b
            goto L_0x016c
        L_0x016b:
            r5 = r7
        L_0x016c:
            double r7 = r10.mResolutionThreshold
            int r9 = (r5 > r7 ? 1 : (r5 == r7 ? 0 : -1))
            if (r9 <= 0) goto L_0x0181
            double r5 = r1 * r7
            double r7 = r7 * r3
            double r1 = r11.width
            double r3 = r11.height
            r9 = 0
            r0 = r25
            org.opencv.core.Size r0 = r0.CalcResizeSize(r1, r3, r5, r7, r9)
        L_0x0181:
            android.graphics.RectF r1 = new android.graphics.RectF
            double r2 = r0.width
            float r2 = (float) r2
            double r3 = r0.height
            float r0 = (float) r3
            r1.<init>(r12, r12, r2, r0)
            goto L_0x01b9
        L_0x018d:
            org.opencv.core.Size r0 = r10.mCurrentPaperSize
            double r0 = r0.width
            double r0 = r0 / r21
            org.opencv.core.Size r2 = r10.mCurrentPaperSize
            double r2 = r2.height
            double r2 = r2 / r21
            double r0 = r5 / r0
            double r2 = r7 / r2
            int r4 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            if (r4 <= 0) goto L_0x01a2
            goto L_0x01a3
        L_0x01a2:
            r0 = r2
        L_0x01a3:
            double r2 = r10.mResolutionThreshold
            int r4 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            if (r4 <= 0) goto L_0x01b7
            double r2 = r2 / r0
            double r5 = r5 * r2
            double r7 = r7 * r2
            android.graphics.RectF r0 = new android.graphics.RectF
            float r1 = (float) r5
            float r2 = (float) r7
            r0.<init>(r12, r12, r1, r2)
            r1 = r0
            goto L_0x01b9
        L_0x01b7:
            r1 = r28
        L_0x01b9:
            r13[r14] = r12
            r13[r16] = r12
            r13[r15] = r12
            float r0 = r1.height()
            r13[r20] = r0
            float r0 = r1.width()
            r2 = 4
            r13[r2] = r0
            float r0 = r1.height()
            r2 = 5
            r13[r2] = r0
            float r0 = r1.width()
            r2 = 6
            r13[r2] = r0
            r0 = 7
            r13[r0] = r12
            r0 = r24
            r0.put((int) r14, (int) r14, (float[]) r13)
            r2 = r23
            org.opencv.core.Mat r11 = org.opencv.imgproc.Imgproc.getPerspectiveTransform(r2, r0)
            org.opencv.core.Mat r12 = new org.opencv.core.Mat
            float r3 = r1.height()
            int r3 = (int) r3
            float r1 = r1.width()
            int r1 = (int) r1
            int r4 = r26.type()
            r12.<init>((int) r3, (int) r1, (int) r4)
            org.opencv.core.Size r6 = r12.size()
            r7 = 1
            r8 = 0
            org.opencv.core.Scalar r1 = new org.opencv.core.Scalar
            r14 = 4643176031446892544(0x406fe00000000000, double:255.0)
            r16 = 4643176031446892544(0x406fe00000000000, double:255.0)
            r18 = 4643176031446892544(0x406fe00000000000, double:255.0)
            r13 = r1
            r13.<init>(r14, r16, r18)
            r3 = r26
            r4 = r12
            r5 = r11
            r9 = r1
            org.opencv.imgproc.Imgproc.warpPerspective(r3, r4, r5, r6, r7, r8, r9)
            r2.release()
            r0.release()
            r11.release()
            goto L_0x0229
        L_0x0228:
            r12 = 0
        L_0x0229:
            return r12
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.colorcorrection.ImageCollect.GetCropImage(org.opencv.core.Mat, android.graphics.PointF[], android.graphics.RectF, org.opencv.core.Size, org.opencv.core.Size):org.opencv.core.Mat");
    }

    public void MakePreviewImageRAW(double d, double d2) {
        if (mMatPreview[0] == null) {
            GetMat();
            Mat[] matArr = mMatPrint;
            if (matArr[0] != null) {
                mMatPreview[0] = ResizeMat(matArr[0], d, d2);
            }
        }
    }

    public void MakePreviewImageCROP(PointF[] pointFArr, Size size, Size size2) {
        Mat[] matArr = mMatPreview;
        if (matArr[0] != null) {
            if (matArr[1] != null) {
                matArr[1].release();
                mMatPreview[1] = null;
            }
            Mat[] matArr2 = mMatPreview;
            matArr2[1] = MakeCropImage(matArr2[0], pointFArr, 1.0f, size, size2);
        }
    }

    public void MakePreviewImagePWC(int i) {
        if (i == 1 || i == 2) {
            ExecPwc(4, 4, i);
        } else if (i == -1) {
            Mat[] matArr = mMatPreview;
            if (matArr[1] != null) {
                ExecPwc(1, 2, 1);
                ExecPwc(1, 3, 2);
            } else if (matArr[0] != null) {
                ExecPwc(0, 2, 1);
                ExecPwc(0, 3, 2);
            }
        }
    }

    public void ResetPreviewImageBCSC() {
        Mat[] matArr = mMatPreview;
        if (matArr[5] != null) {
            matArr[5].release();
            mMatPreview[5] = null;
        }
        Mat[] matArr2 = mMatPreview;
        if (matArr2[6] != null) {
            matArr2[6].release();
            mMatPreview[6] = null;
        }
        Mat[] matArr3 = mMatPreview;
        if (matArr3[8] != null) {
            matArr3[8].release();
            mMatPreview[8] = null;
        }
    }

    public void SiwtchPreviewImageBCSC(int i) {
        Mat[] matArr = mMatPreview;
        if (matArr[6] != null) {
            if (matArr[5] != null) {
                matArr[5].release();
                mMatPreview[5] = null;
            }
            MakePreviewImageBCSC_Init(6, true);
        } else {
            MakePreviewImageBCSC_Init(i, true);
        }
        Mat[] matArr2 = mMatPreview;
        if (matArr2[8] != null) {
            matArr2[8].release();
            mMatPreview[8] = null;
        }
    }

    public void MakePreviewBCSC(CropImageActivity.CorrectionParamSet correctionParamSet) {
        Mat clone = mMatPreview[correctionParamSet.GetImageType()].clone();
        Mat[] matArr = mMatPreview;
        if (matArr[4] != null) {
            matArr[4].release();
            mMatPreview[4] = null;
        }
        mMatPreview[4] = MakeImageBCSC(clone, correctionParamSet);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0047, code lost:
        r7 = r5.GetPreviewImageSize();
        r8 = r7.x;
        r7 = r7.y;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private org.opencv.core.Mat MakeImageBCSC(org.opencv.core.Mat r25, com.epson.cameracopy.p006ui.CropImageActivity.CorrectionParamSet r26) {
        /*
            r24 = this;
            r0 = r26
            if (r0 == 0) goto L_0x0141
            int r1 = r26.Size()
            if (r1 <= 0) goto L_0x0141
            r2 = 0
            r13 = r25
            r3 = 0
            r4 = 0
        L_0x000f:
            int r5 = r26.Size()
            if (r3 >= r5) goto L_0x0143
            com.epson.cameracopy.ui.CropImageActivity$CorrectionParam r5 = r0.GetParam(r3)
            boolean r6 = r5.GetMaskType()
            if (r6 == 0) goto L_0x003b
            org.opencv.core.Mat r4 = new org.opencv.core.Mat
            int r6 = r13.rows()
            int r7 = r13.cols()
            r4.<init>((int) r6, (int) r7, (int) r2)
            r6 = 4643176031446892544(0x406fe00000000000, double:255.0)
            org.opencv.core.Scalar r6 = org.opencv.core.Scalar.all(r6)
            r4.setTo((org.opencv.core.Scalar) r6)
            r1 = r4
            goto L_0x010f
        L_0x003b:
            java.util.List r6 = r5.GetPointArray()
            if (r6 == 0) goto L_0x010e
            int r7 = r6.size()
            if (r7 <= 0) goto L_0x010e
            android.graphics.Point r7 = r5.GetPreviewImageSize()
            int r8 = r7.x
            int r7 = r7.y
            android.graphics.Bitmap$Config r9 = android.graphics.Bitmap.Config.ARGB_8888
            android.graphics.Bitmap r9 = android.graphics.Bitmap.createBitmap(r8, r7, r9)
            if (r9 == 0) goto L_0x010e
            android.graphics.Canvas r10 = new android.graphics.Canvas
            r10.<init>(r9)
            android.graphics.Path r11 = new android.graphics.Path
            r11.<init>()
            java.util.Iterator r6 = r6.iterator()
        L_0x0065:
            boolean r12 = r6.hasNext()
            if (r12 == 0) goto L_0x00a6
            java.lang.Object r12 = r6.next()
            java.util.List r12 = (java.util.List) r12
            int r14 = r12.size()
            if (r14 <= 0) goto L_0x00a4
            java.lang.Object r14 = r12.get(r2)
            android.graphics.PointF r14 = (android.graphics.PointF) r14
            float r15 = r5.GetScale()
            float r1 = r14.x
            float r1 = r1 / r15
            float r14 = r14.y
            float r14 = r14 / r15
            r11.moveTo(r1, r14)
            r1 = 1
        L_0x008b:
            int r14 = r12.size()
            if (r1 >= r14) goto L_0x00a4
            java.lang.Object r14 = r12.get(r1)
            android.graphics.PointF r14 = (android.graphics.PointF) r14
            float r2 = r14.x
            float r2 = r2 / r15
            float r14 = r14.y
            float r14 = r14 / r15
            r11.lineTo(r2, r14)
            int r1 = r1 + 1
            r2 = 0
            goto L_0x008b
        L_0x00a4:
            r2 = 0
            goto L_0x0065
        L_0x00a6:
            boolean r1 = r11.isEmpty()
            if (r1 != 0) goto L_0x00e7
            android.graphics.Paint r1 = new android.graphics.Paint
            r1.<init>()
            float r2 = r5.GetStrokeWidth()
            android.graphics.Paint$Style r4 = android.graphics.Paint.Style.STROKE
            r1.setStyle(r4)
            r4 = -1
            r1.setColor(r4)
            android.graphics.Paint$Cap r4 = android.graphics.Paint.Cap.ROUND
            r1.setStrokeCap(r4)
            r1.setStrokeWidth(r2)
            r10.drawPath(r11, r1)
            org.opencv.core.Mat r1 = new org.opencv.core.Mat
            r1.<init>()
            org.opencv.android.Utils.bitmapToMat(r9, r1)
            r2 = 6
            org.opencv.imgproc.Imgproc.cvtColor(r1, r1, r2)
            r2 = 1094713344(0x41400000, float:12.0)
            epson.colorcorrection.SDIC_MAT_PTR r4 = new epson.colorcorrection.SDIC_MAT_PTR
            r4.<init>()
            int r2 = (int) r2
            epson.colorcorrection.SDIC_BCSC.MakeMask(r1, r4, r2)
            r1.release()
            org.opencv.core.Mat r4 = r4.getMat()
        L_0x00e7:
            r9.recycle()
            int r1 = r13.cols()
            int r2 = r13.rows()
            if (r4 == 0) goto L_0x010e
            if (r1 != r8) goto L_0x00f8
            if (r2 == r7) goto L_0x010e
        L_0x00f8:
            org.opencv.core.Size r6 = new org.opencv.core.Size
            double r7 = (double) r1
            double r1 = (double) r2
            r6.<init>(r7, r1)
            r19 = 0
            r21 = 0
            r23 = 2
            r16 = r4
            r17 = r4
            r18 = r6
            org.opencv.imgproc.Imgproc.resize(r16, r17, r18, r19, r21, r23)
        L_0x010e:
            r1 = r4
        L_0x010f:
            if (r1 == 0) goto L_0x013b
            epson.colorcorrection.SDIC_MAT_PTR r2 = new epson.colorcorrection.SDIC_MAT_PTR
            r2.<init>()
            float r4 = r5.GetBcscBValue()
            double r7 = (double) r4
            float r4 = r5.GetBcscCValue()
            double r9 = (double) r4
            float r4 = r5.GetBcscSValue()
            double r11 = (double) r4
            r4 = r13
            r5 = r1
            r6 = r2
            int r4 = epson.colorcorrection.SDIC_BCSC.BCSCorrection(r4, r5, r6, r7, r9, r11)
            if (r4 != 0) goto L_0x0136
            r13.release()
            org.opencv.core.Mat r2 = r2.getMat()
            r13 = r2
        L_0x0136:
            r1.release()
            r4 = 0
            goto L_0x013c
        L_0x013b:
            r4 = r1
        L_0x013c:
            int r3 = r3 + 1
            r2 = 0
            goto L_0x000f
        L_0x0141:
            r13 = r25
        L_0x0143:
            return r13
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.colorcorrection.ImageCollect.MakeImageBCSC(org.opencv.core.Mat, com.epson.cameracopy.ui.CropImageActivity$CorrectionParamSet):org.opencv.core.Mat");
    }

    public void MakePreviewImageBCSC_Init(int i, boolean z) {
        if (!z) {
            MakePreviewImageBCSC_MakeSRC(i, z);
        } else if (mMatPreview[5] == null) {
            MakePreviewImageBCSC_MakeSRC(i, z);
        }
    }

    public void MakePreviewImageBCSC_Exec(int i, boolean z, float f, float f2, float f3, float f4) {
        float f5 = f;
        float f6 = f2;
        float f7 = f3;
        Mat[] matArr = mMatPreview;
        if (matArr[5] != null) {
            Mat mat = null;
            if (matArr[6] != null) {
                matArr[6].release();
                mMatPreview[6] = null;
            }
            char c = 4;
            if (i == 8) {
                Mat[] matArr2 = mMatPreview;
                if (matArr2[8] != null) {
                    if (!z) {
                        if (matArr2[9] != null) {
                            matArr2[9].release();
                            mMatPreview[9] = null;
                        }
                        mat = mMatPreview[8].clone();
                    } else if (matArr2[9] == null) {
                        mat = ResizeMatSmallPreviewSize(8);
                    }
                    if (mat != null) {
                        float f8 = 12.0f / mDensity;
                        SDIC_MAT_PTR sdic_mat_ptr = new SDIC_MAT_PTR();
                        SDIC_BCSC.MakeMask(mat, sdic_mat_ptr, (int) f8);
                        mat.release();
                        mMatPreview[9] = sdic_mat_ptr.getMat();
                    }
                    SDIC_MAT_PTR sdic_mat_ptr2 = new SDIC_MAT_PTR();
                    Mat[] matArr3 = mMatPreview;
                    if (SDIC_BCSC.BCSCorrection(matArr3[5], matArr3[9], sdic_mat_ptr2, (double) f5, (double) f6, (double) f7) == 0) {
                        Mat[] matArr4 = mMatPreview;
                        if (z) {
                            c = 6;
                        }
                        matArr4[c] = sdic_mat_ptr2.getMat();
                        return;
                    }
                    return;
                }
                return;
            }
            SDIC_MAT_PTR sdic_mat_ptr3 = new SDIC_MAT_PTR();
            Mat[] matArr5 = mMatPreview;
            if (SDIC_BCSC.BCSCorrection(matArr5[5], matArr5[7], sdic_mat_ptr3, (double) f5, (double) f6, (double) f7) == 0) {
                Mat[] matArr6 = mMatPreview;
                if (z) {
                    c = 6;
                }
                matArr6[c] = sdic_mat_ptr3.getMat();
            }
        }
    }

    private void MakePreviewImageBCSC_MakeSRC(int i, boolean z) {
        Mat[] matArr = mMatPreview;
        if (matArr[5] != null) {
            matArr[5].release();
        }
        if (z) {
            mMatPreview[5] = ResizeMatSmallPreviewSize(i);
            MakePreviewImageBCSC_MakeMSK(7);
            Mat[] matArr2 = mMatPreview;
            if (matArr2[8] != null) {
                matArr2[8].release();
                mMatPreview[8] = null;
                return;
            }
            return;
        }
        Mat[] matArr3 = mMatPreview;
        matArr3[5] = matArr3[i].clone();
        MakePreviewImageBCSC_MakeMSK(7);
        MakePreviewImageBCSC_PartMSK();
    }

    private Mat ResizeMatSmallPreviewSize(int i) {
        Mat mat = new Mat();
        Mat[] matArr = mMatPreview;
        Imgproc.resize(matArr[i], mat, new Size((double) (((float) matArr[i].width()) / mSmallPreviewRate), (double) (((float) mMatPreview[i].height()) / mSmallPreviewRate)), 0.0d, 0.0d, 3);
        return mat;
    }

    private void MakePreviewImageBCSC_MakeMSK(int i) {
        Mat[] matArr = mMatPreview;
        if (matArr[i] != null) {
            matArr[i].release();
        }
        Mat[] matArr2 = mMatPreview;
        matArr2[i] = new Mat(matArr2[5].rows(), mMatPreview[5].cols(), 0);
        MakePreviewImageBCSC_InitMSK(i);
    }

    private void MakePreviewImageBCSC_InitMSK(int i) {
        Mat[] matArr = mMatPreview;
        if (matArr[i] != null) {
            matArr[i].setTo(Scalar.all(255.0d));
        }
    }

    private void MakePreviewImageBCSC_PartMSK() {
        Mat[] matArr = mMatPreview;
        if (matArr[8] == null) {
            return;
        }
        if (matArr[5].width() != mMatPreview[8].width() || mMatPreview[5].height() != mMatPreview[8].height()) {
            Mat mat = new Mat();
            Mat[] matArr2 = mMatPreview;
            Imgproc.resize(matArr2[8], mat, new Size((double) matArr2[5].width(), (double) mMatPreview[5].height()), 0.0d, 0.0d, 2);
            mMatPreview[8].release();
            mMatPreview[8] = mat;
        }
    }

    public Bitmap GetPreviewImage(int i) {
        Mat[] matArr = mMatPreview;
        if (matArr[i] == null) {
            return null;
        }
        if (i == 6 && !(matArr[6].width() == mMatPreview[1].width() && mMatPreview[6].height() == mMatPreview[1].height())) {
            Mat mat = new Mat();
            Mat[] matArr2 = mMatPreview;
            Imgproc.resize(matArr2[6], mat, new Size((double) matArr2[1].width(), (double) mMatPreview[1].height()), 0.0d, 0.0d, 2);
            mMatPreview[6].release();
            mMatPreview[6] = mat;
        }
        return ConvertMatToBitmap(mMatPreview[i]);
    }

    public List<List<PointF>> DetectEdge(Bitmap bitmap) {
        Mat[] matArr = mMatPreview;
        if (matArr[8] != null) {
            matArr[8].release();
        }
        Mat mat = new Mat();
        Utils.bitmapToMat(bitmap, mat);
        Mat mat2 = new Mat();
        Imgproc.cvtColor(mat, mat2, 6);
        Mat mat3 = new Mat();
        ArrayList arrayList = new ArrayList();
        new Mat();
        Mat clone = mat2.clone();
        Imgproc.findContours(clone, arrayList, mat3, 1, 3);
        clone.release();
        ArrayList arrayList2 = new ArrayList();
        for (int i = 0; i < arrayList.size(); i++) {
            List<org.opencv.core.Point> list = ((MatOfPoint) arrayList.get(i)).toList();
            ArrayList arrayList3 = new ArrayList();
            for (org.opencv.core.Point next : list) {
                arrayList3.add(new PointF((float) next.f432x, (float) next.f433y));
            }
            arrayList2.add(arrayList3);
        }
        Mat[] matArr2 = mMatPreview;
        matArr2[8] = mat2;
        if (matArr2[9] != null) {
            matArr2[9].release();
            mMatPreview[9] = null;
        }
        mat.release();
        return arrayList2;
    }

    public PointF[] GetCornerPoints() {
        PointF[] pointFArr;
        PointF[] pointFArr2;
        int i;
        int i2;
        int i3;
        boolean z;
        ImageCollect imageCollect = this;
        Mat[] matArr = imageCollect.mMatPreview;
        if (matArr[0] != null) {
            Mat ResizeMat = ResizeMat(matArr[0], 640.0d, 640.0d);
            if (ResizeMat != null) {
                int i4 = 200;
                MatOfPoint2f matOfPoint2f = null;
                while (i4 >= 50) {
                    matOfPoint2f = imageCollect.DetectEdge(ResizeMat, i4);
                    if (matOfPoint2f != null) {
                        int rows = ResizeMat.rows();
                        if (3 < matOfPoint2f.rows()) {
                            if (4 == matOfPoint2f.rows()) {
                                org.opencv.core.Point[] array = matOfPoint2f.toArray();
                                i3 = rows;
                                int i5 = 0;
                                i2 = 0;
                                while (i5 < 4) {
                                    double d = (double) i3;
                                    if (d >= array[i5].f433y) {
                                        d = array[i5].f433y;
                                    }
                                    i3 = (int) d;
                                    double d2 = (double) i2;
                                    if (d2 <= array[i5].f433y) {
                                        d2 = array[i5].f433y;
                                    }
                                    i2 = (int) d2;
                                    i5++;
                                }
                                z = false;
                            } else {
                                org.opencv.core.Point[] pointArr = new org.opencv.core.Point[4];
                                Imgproc.minAreaRect(matOfPoint2f).points(pointArr);
                                List<org.opencv.core.Point> list = matOfPoint2f.toList();
                                int i6 = rows;
                                int i7 = 0;
                                int i8 = 0;
                                while (true) {
                                    if (i7 >= 4) {
                                        z = false;
                                        break;
                                    } else if (pointArr[i7].f432x <= 0.0d || pointArr[i7].f433y <= 0.0d || pointArr[i7].f432x >= ((double) ResizeMat.cols()) || pointArr[i7].f433y >= ((double) ResizeMat.rows())) {
                                        z = true;
                                    } else {
                                        list.set(i7, pointArr[i7]);
                                        double d3 = (double) i3;
                                        if (d3 >= list.get(i7).f433y) {
                                            d3 = list.get(i7).f433y;
                                        }
                                        i6 = (int) d3;
                                        double d4 = (double) i2;
                                        if (d4 <= list.get(i7).f433y) {
                                            d4 = list.get(i7).f433y;
                                        }
                                        i8 = (int) d4;
                                        i7++;
                                    }
                                }
                                ArrayList arrayList = new ArrayList();
                                matOfPoint2f.release();
                                matOfPoint2f = new MatOfPoint2f();
                                for (int i9 = 0; i9 < i7; i9++) {
                                    arrayList.add(list.get(i9));
                                }
                                matOfPoint2f.fromList(arrayList);
                            }
                            if (!z) {
                                z = DETECT_AREA_RATIO >= ((float) (i2 - i3)) / ((float) ResizeMat.rows());
                            }
                            if (!z) {
                                break;
                            }
                        } else {
                            continue;
                        }
                    }
                    i4 -= 10;
                    imageCollect = this;
                }
                if (matOfPoint2f != null) {
                    if (matOfPoint2f.rows() >= 4) {
                        pointFArr2 = new PointF[]{new PointF(), new PointF(), new PointF(), new PointF()};
                        List<org.opencv.core.Point> list2 = matOfPoint2f.toList();
                        Collections.sort(list2, new SortPoints());
                        if (list2.get(0).f432x < list2.get(1).f432x) {
                            pointFArr2[0].x = (float) list2.get(0).f432x;
                            pointFArr2[0].y = (float) list2.get(0).f433y;
                            pointFArr2[3].x = (float) list2.get(1).f432x;
                            pointFArr2[3].y = (float) list2.get(1).f433y;
                            i = 3;
                        } else {
                            pointFArr2[0].x = (float) list2.get(1).f432x;
                            pointFArr2[0].y = (float) list2.get(1).f433y;
                            i = 3;
                            pointFArr2[3].x = (float) list2.get(0).f432x;
                            pointFArr2[3].y = (float) list2.get(0).f433y;
                        }
                        if (list2.get(2).f432x < list2.get(i).f432x) {
                            pointFArr2[1].x = (float) list2.get(2).f432x;
                            pointFArr2[1].y = (float) list2.get(2).f433y;
                            pointFArr2[2].x = (float) list2.get(3).f432x;
                            pointFArr2[2].y = (float) list2.get(3).f433y;
                        } else {
                            pointFArr2[1].x = (float) list2.get(3).f432x;
                            pointFArr2[1].y = (float) list2.get(3).f433y;
                            pointFArr2[2].x = (float) list2.get(2).f432x;
                            pointFArr2[2].y = (float) list2.get(2).f433y;
                        }
                        float rows2 = ((float) mMatPreview[0].rows()) / ((float) ResizeMat.rows());
                        for (int i10 = 0; i10 < 4; i10++) {
                            pointFArr2[i10].x *= rows2;
                            pointFArr2[i10].y *= rows2;
                        }
                    } else {
                        pointFArr2 = null;
                    }
                    matOfPoint2f.release();
                } else {
                    pointFArr2 = null;
                }
                ResizeMat.release();
                return pointFArr2;
            }
            pointFArr = null;
        } else {
            pointFArr = null;
        }
        return pointFArr;
    }

    private class SortPoints implements Comparator<org.opencv.core.Point> {
        private SortPoints() {
        }

        public int compare(org.opencv.core.Point point, org.opencv.core.Point point2) {
            return point.f433y < point2.f433y ? -1 : 1;
        }
    }

    private MatOfPoint2f DetectEdge(Mat mat, int i) {
        Mat mat2 = new Mat(mat.rows(), mat.cols(), mat.type());
        Imgproc.Canny(mat, mat2, 1.0d, (double) i);
        Mat mat3 = new Mat(3, 3, 0, Scalar.all(255.0d));
        Imgproc.morphologyEx(mat2, mat2, 4, mat3, new org.opencv.core.Point(-1.0d, -1.0d), 1);
        Mat mat4 = new Mat();
        ArrayList arrayList = new ArrayList();
        Imgproc.findContours(mat2, arrayList, mat4, 1, 3);
        MatOfPoint2f DetectPoints = arrayList.size() > 0 ? DetectPoints(arrayList, mat4) : null;
        mat4.release();
        mat3.release();
        mat2.release();
        return DetectPoints;
    }

    private MatOfPoint2f DetectPoints(List<MatOfPoint> list, Mat mat) {
        int[][] iArr = (int[][]) Array.newInstance(int.class, new int[]{mat.cols(), 4});
        for (int i = 0; i < mat.cols(); i++) {
            mat.get(0, i, iArr[i]);
        }
        double d = 0.0d;
        int i2 = 0;
        for (int i3 = 0; i3 >= 0; i3 = iArr[i3][0]) {
            MatOfPoint matOfPoint = list.get(i3);
            if (matOfPoint.rows() >= 4) {
                double contourArea = Imgproc.contourArea(matOfPoint);
                if (d < contourArea) {
                    i2 = i3;
                    d = contourArea;
                }
            }
        }
        MatOfPoint2f matOfPoint2f = new MatOfPoint2f(list.get(i2).toArray());
        MatOfPoint2f matOfPoint2f2 = new MatOfPoint2f();
        for (int i4 = 400; i4 >= 50; i4 -= 50) {
            Imgproc.approxPolyDP(matOfPoint2f, matOfPoint2f2, (double) i4, true);
            if (matOfPoint2f2.rows() >= 4) {
                break;
            }
        }
        return matOfPoint2f2;
    }

    public void ReleaseImageCollect() {
        for (int i = 0; i < 10; i++) {
            Mat[] matArr = mMatPreview;
            if (matArr[i] != null) {
                matArr[i].release();
                mMatPreview[i] = null;
            }
        }
        for (int i2 = 0; i2 < 5; i2++) {
            Mat[] matArr2 = mMatPrint;
            if (matArr2[i2] != null) {
                matArr2[i2].release();
                mMatPrint[i2] = null;
            }
        }
    }

    private Mat GetMat() {
        if (mMatPrint[0] == null) {
            Mat imread = Imgcodecs.imread(mLoadPath, -1);
            int channels = imread.channels();
            if (channels == 4) {
                ArrayList arrayList = new ArrayList();
                Core.split(imread, arrayList);
                imread.release();
                Mat mat = new Mat();
                ((Mat) arrayList.get(3)).convertTo(mat, CvType.CV_32FC1);
                ((Mat) arrayList.get(3)).release();
                arrayList.remove(3);
                Core.normalize(mat, mat, 0.0d, 1.0d, 32);
                ArrayList arrayList2 = new ArrayList();
                for (int i = 0; i < 3; i++) {
                    arrayList2.add(mat);
                }
                Mat mat2 = new Mat();
                Core.merge(arrayList2, mat2);
                mat.release();
                Mat mat3 = new Mat();
                Mat mat4 = new Mat();
                Core.merge(arrayList, mat3);
                ((Mat) arrayList.get(0)).release();
                ((Mat) arrayList.get(1)).release();
                ((Mat) arrayList.get(2)).release();
                mat3.convertTo(mat4, CvType.CV_32FC1);
                mat3.release();
                Mat mul = mat4.mul(mat2);
                mat4.release();
                mMatPrint[0] = new Mat();
                mul.convertTo(mMatPrint[0], CvType.CV_8UC3);
                mul.release();
            } else if (channels == 1) {
                Mat mat5 = new Mat(imread.rows(), imread.cols(), CvType.CV_8UC3);
                Imgproc.cvtColor(imread, mat5, 8);
                imread.release();
                mMatPrint[0] = mat5;
            } else {
                mMatPrint[0] = imread;
            }
            if (mMatPrint[0] != null) {
                float f = 0.0f;
                if (!OpenCvHelper.doseImreadRotateWithExifTag()) {
                    try {
                        int attributeInt = new ExifInterface(mLoadPath).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 0);
                        if (attributeInt == 6) {
                            f = 90.0f;
                        } else if (attributeInt == 3) {
                            f = 180.0f;
                        } else if (attributeInt == 8) {
                            f = 270.0f;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                double d = (double) f;
                if (d == 90.0d) {
                    Size size = new Size((double) mMatPrint[0].rows(), (double) mMatPrint[0].cols());
                    Mat mat6 = new Mat(size, CvType.CV_8UC3);
                    Core.transpose(mMatPrint[0], mat6);
                    mMatPrint[0].release();
                    Mat[] matArr = mMatPrint;
                    matArr[0] = null;
                    matArr[0] = new Mat(size, CvType.CV_8UC3);
                    Core.flip(mat6, mMatPrint[0], 1);
                    mat6.release();
                } else if (d == 180.0d) {
                    Mat[] matArr2 = mMatPrint;
                    Core.flip(matArr2[0], matArr2[0], -1);
                } else if (d == 270.0d) {
                    Size size2 = new Size((double) mMatPrint[0].rows(), (double) mMatPrint[0].cols());
                    Mat mat7 = new Mat(size2, CvType.CV_8UC3);
                    Core.transpose(mMatPrint[0], mat7);
                    mMatPrint[0].release();
                    Mat[] matArr3 = mMatPrint;
                    matArr3[0] = null;
                    matArr3[0] = new Mat(size2, CvType.CV_8UC3);
                    Core.flip(mat7, mMatPrint[0], 0);
                    mat7.release();
                }
            }
        }
        return mMatPrint[0];
    }

    private Mat ResizeMat(Mat mat, double d, double d2) {
        Size CalcResizeSize = CalcResizeSize((double) mat.cols(), (double) mat.rows(), d, d2, true);
        Mat mat2 = new Mat();
        Imgproc.resize(mat, mat2, CalcResizeSize, 0.0d, 0.0d, 3);
        return mat2;
    }

    private Size CalcResizeSize(double d, double d2, double d3, double d4, boolean z) {
        double d5 = d3 / d;
        double d6 = d4 / d2;
        if (z) {
            if (d5 >= d6) {
                d5 = d6;
            }
        } else if (d5 <= d6) {
            d5 = d6;
        }
        return new Size(d * d5, d2 * d5);
    }

    private Bitmap ConvertMatToBitmap(Mat mat) {
        if (mat == null || mat.empty()) {
            return null;
        }
        Mat mat2 = new Mat(mat.rows(), mat.cols(), mat.type());
        Imgproc.cvtColor(mat, mat2, 4);
        Bitmap createBitmap = Bitmap.createBitmap(mat2.cols(), mat2.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(mat2, createBitmap);
        mat2.release();
        return createBitmap;
    }

    private void DEBUG_SAVE(Mat mat, String str) {
        if (mat != null) {
            int width = mat.width();
            int height = mat.height();
            if (width > 0 && height > 0) {
                Imgcodecs.imwrite(new File(Environment.getExternalStorageDirectory().getPath(), String.format("ic_prn_%s.png", new Object[]{str})).getPath(), mat);
            }
        }
    }

    static class EPxLog {
        EPxLog() {
        }

        /* renamed from: d */
        public static final void m298d(String str, String str2) {
            Log.d(str, str2);
        }

        /* renamed from: e */
        public static final void m299e(String str, String str2) {
            if (Debug.isDebuggerConnected()) {
                Log.e(str, str2);
            }
        }

        /* renamed from: i */
        public static final void m300i(String str, String str2) {
            if (Debug.isDebuggerConnected()) {
                Log.i(str, str2);
            }
        }

        /* renamed from: v */
        public static final void m301v(String str, String str2) {
            if (Debug.isDebuggerConnected()) {
                Log.v(str, str2);
            }
        }

        /* renamed from: w */
        public static final void m302w(String str, String str2) {
            if (Debug.isDebuggerConnected()) {
                Log.w(str, str2);
            }
        }
    }

    private static void LogMemory(String str) {
        Runtime runtime = Runtime.getRuntime();
        float f = (float) runtime.totalMemory();
        float freeMemory = (float) runtime.freeMemory();
        Log.d("LogMemory", String.format("max:%.2fMB total:%.2fMB free:%.2fMB used:%.2fMB %s", new Object[]{Float.valueOf(((float) runtime.maxMemory()) / 1048576.0f), Float.valueOf(f / 1048576.0f), Float.valueOf(freeMemory / 1048576.0f), Float.valueOf((f - freeMemory) / 1048576.0f), str}));
    }
}
