package epson.colorcorrection;

import android.util.Log;
import com.epson.cameracopy.device.RectangleDetector;
import com.google.common.primitives.UnsignedBytes;
import org.opencv.core.CvException;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

public class SDIC_PWC extends SDIC_Exception implements SDIC_CommonDefine {
    static {
        System.loadLibrary("opencv_java3");
    }

    public static int PaperWhiteCorrection(Mat mat, SDIC_MAT_PTR sdic_mat_ptr) {
        return RectangleDetector.paperWhiteCorrection(mat.getNativeObjAddr(), sdic_mat_ptr.getMat().getNativeObjAddr());
    }

    public static int PaperWhiteCorrection2(Mat mat, SDIC_MAT_PTR sdic_mat_ptr) {
        return RectangleDetector.paperWhiteCorrection(mat.getNativeObjAddr(), sdic_mat_ptr.getMat().getNativeObjAddr(), RectangleDetector.getDefaultPaperWhiteCorrection());
    }

    public static int PaperWhiteCorrectionJAVA(Mat mat, SDIC_MAT_PTR sdic_mat_ptr) {
        int cols = mat.cols();
        int rows = mat.rows();
        int channels = mat.channels();
        Mat mat2 = new Mat(rows, cols, mat.type());
        Runtime runtime = Runtime.getRuntime();
        runtime.maxMemory();
        float f = ((float) runtime.totalMemory()) / 1048576.0f;
        float freeMemory = f - (f - (((float) runtime.freeMemory()) / 1048576.0f));
        float f2 = 2.0f;
        float f3 = (freeMemory / 2.0f) / 3.0f;
        if (f3 > 2.0f) {
            f2 = f3;
        }
        int i = (int) ((f2 * 1048576.0f) / ((float) (cols * channels)));
        if (i >= mat.rows()) {
            i = mat.rows();
        }
        Rect rect = new Rect(0, 0, cols, i);
        int ceil = (int) Math.ceil(((double) rows) / ((double) i));
        LogMemory(String.format("PWC S - [%d] w:%d h:%d c:%d m:%.2fMB use:%.2fMB", new Object[]{Integer.valueOf(ceil), Integer.valueOf(cols), Integer.valueOf(rows), Integer.valueOf(channels), Float.valueOf(((float) ((cols * rows) * channels)) / 1048576.0f), Float.valueOf(f2)}));
        int i2 = 0;
        int i3 = 0;
        while (true) {
            if (rows <= 0) {
                break;
            }
            rect.height = rows < i ? rows : i;
            Mat submat = mat.submat(rect);
            Mat submat2 = mat2.submat(rect);
            int PaperWhiteCorrection_ROI = PaperWhiteCorrection_ROI(submat, submat2);
            submat.release();
            submat2.release();
            i2++;
            LogMemory(String.format("PWC R - [%d/%d] %d", new Object[]{Integer.valueOf(i2), Integer.valueOf(ceil), Integer.valueOf(rect.height)}));
            if (PaperWhiteCorrection_ROI != 0) {
                i3 = PaperWhiteCorrection_ROI;
                break;
            }
            rows -= i;
            rect.f438y += i;
            i3 = PaperWhiteCorrection_ROI;
        }
        if (i3 == 0) {
            sdic_mat_ptr.setMat(mat2);
        }
        LogMemory(String.format("PWC E use:%.2fMB", new Object[]{Float.valueOf(f2)}));
        return i3;
    }

    public static int PaperWhiteCorrection_ROI(Mat mat, Mat mat2) {
        int i;
        try {
            if (mat.empty()) {
                throw new SDIC_Exception(-2);
            } else if (mat.dataAddr() == mat2.dataAddr()) {
                throw new SDIC_Exception(-1);
            } else if (mat.isContinuous()) {
                SDIC_MAT_PTR sdic_mat_ptr = new SDIC_MAT_PTR();
                SDIC_MAT_PTR sdic_mat_ptr2 = new SDIC_MAT_PTR();
                SDIC_MAT_PTR sdic_mat_ptr3 = new SDIC_MAT_PTR();
                int MakeCleaningFilter = MakeCleaningFilter(mat, sdic_mat_ptr2, 0.05d, 80.0d);
                if (MakeCleaningFilter == 0) {
                    int Sharpness = Sharpness(mat, sdic_mat_ptr3, 2.0f);
                    if (Sharpness == 0) {
                        int ColorDodge = ColorDodge(sdic_mat_ptr3.getMat(), sdic_mat_ptr2.getMat(), sdic_mat_ptr);
                        if (ColorDodge == 0) {
                            sdic_mat_ptr2.releaseMat();
                            sdic_mat_ptr3.releaseMat();
                            SDIC_MAT_PTR sdic_mat_ptr4 = new SDIC_MAT_PTR();
                            SDIC_MAT_PTR sdic_mat_ptr5 = new SDIC_MAT_PTR();
                            int MakeCleaningFilter2 = MakeCleaningFilter(mat, sdic_mat_ptr5, 1.0d, 80.0d);
                            if (MakeCleaningFilter2 == 0) {
                                int ColorDodge2 = ColorDodge(mat, sdic_mat_ptr5.getMat(), sdic_mat_ptr4);
                                if (ColorDodge2 == 0) {
                                    sdic_mat_ptr5.releaseMat();
                                    Multiply(sdic_mat_ptr4.getMat(), sdic_mat_ptr.getMat(), mat2);
                                    sdic_mat_ptr4.releaseMat();
                                    sdic_mat_ptr.releaseMat();
                                    i = 0;
                                    if (!(i == 0 || mat2.empty() || i == -1)) {
                                        mat2.release();
                                    }
                                    return i;
                                }
                                throw new SDIC_Exception(ColorDodge2);
                            }
                            throw new SDIC_Exception(MakeCleaningFilter2);
                        }
                        throw new SDIC_Exception(ColorDodge);
                    }
                    throw new SDIC_Exception(Sharpness);
                }
                throw new SDIC_Exception(MakeCleaningFilter);
            } else {
                throw new SDIC_Exception(-3);
            }
        } catch (SDIC_Exception e) {
            i = e.mCode;
        } catch (CvException unused) {
            i = -100;
        } catch (Exception unused2) {
            i = SDIC_CommonDefine.SDIC_ERR_UNKNOWN;
        }
    }

    private static int Sharpness(Mat mat, SDIC_MAT_PTR sdic_mat_ptr, float f) {
        Mat mat2 = sdic_mat_ptr.getMat();
        Mat mat3 = new Mat(3, 3, 5);
        mat3.setTo(Scalar.all((double) ((-1.0f * f) / 9.0f)));
        float f2 = ((f * 8.0f) / 9.0f) + 1.0f;
        int channels = mat3.channels();
        float[] fArr = new float[channels];
        int i = 0;
        for (int i2 = 0; i2 < channels; i2++) {
            fArr[i2] = f2;
        }
        mat3.put(1, 1, fArr);
        Imgproc.filter2D(mat, mat2, mat.depth(), mat3);
        mat3.release();
        if (mat2.empty()) {
            i = -100;
        }
        sdic_mat_ptr.setMat(mat2);
        return i;
    }

    private static int Multiply(Mat mat, Mat mat2, Mat mat3) {
        int channels = mat.channels();
        int i = (int) (mat.total() * ((long) channels));
        byte[] GetPixcelData = SDIC_BCSC.GetPixcelData(mat);
        byte[] GetPixcelData2 = SDIC_BCSC.GetPixcelData(mat2);
        byte[] GetPixcelData3 = SDIC_BCSC.GetPixcelData(mat);
        for (int i2 = 0; i2 < i; i2 += channels) {
            for (int i3 = 0; i3 < 3; i3++) {
                int i4 = i2 + i3;
                double d = ((double) ((GetPixcelData[i4] & UnsignedBytes.MAX_VALUE) * (GetPixcelData2[i4] & UnsignedBytes.MAX_VALUE))) * 0.00392156862745098d;
                if (d > 255.0d) {
                    d = 255.0d;
                }
                if (d < 0.0d) {
                    d = 0.0d;
                }
                GetPixcelData3[i4] = (byte) ((int) d);
            }
        }
        mat3.put(0, 0, GetPixcelData3);
        return 0;
    }

    private static int ColorDodge(Mat mat, Mat mat2, SDIC_MAT_PTR sdic_mat_ptr) {
        int channels = mat.channels();
        int i = (int) (mat.total() * ((long) channels));
        byte[] GetPixcelData = SDIC_BCSC.GetPixcelData(mat);
        byte[] GetPixcelData2 = SDIC_BCSC.GetPixcelData(mat2);
        byte[] MakePixcelData = SDIC_BCSC.MakePixcelData(mat);
        for (int i2 = 0; i2 < i; i2 += channels) {
            for (int i3 = 0; i3 < 3; i3++) {
                int i4 = i2 + i3;
                byte b = GetPixcelData2[i4] & UnsignedBytes.MAX_VALUE;
                if (b <= 0) {
                    b = 1;
                }
                int i5 = (int) (((double) ((GetPixcelData[i4] & UnsignedBytes.MAX_VALUE) * UnsignedBytes.MAX_VALUE)) / ((double) b));
                if (i5 > 255) {
                    i5 = 255;
                }
                if (i5 < 0) {
                    i5 = 0;
                }
                MakePixcelData[i4] = (byte) i5;
            }
        }
        sdic_mat_ptr.setMat(SDIC_BCSC.SetPixcelData(mat.rows(), mat.cols(), mat.type(), MakePixcelData));
        return 0;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:24:0x009b, code lost:
        r0 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:25:0x009d, code lost:
        r0 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:43:0x00bb, code lost:
        r0 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:44:0x00bd, code lost:
        r3 = r16;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:46:0x00c0, code lost:
        r3 = r16;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:48:0x00c3, code lost:
        r0 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:49:0x00c4, code lost:
        r3 = r16;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:58:0x00d3, code lost:
        r2.release();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:59:0x00d6, code lost:
        if (r1 != null) goto L_0x00d8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:60:0x00d8, code lost:
        r1.release();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:66:0x00e6, code lost:
        r2.release();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:67:0x00e9, code lost:
        if (r1 != null) goto L_0x00d8;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:73:0x00f4, code lost:
        r1.release();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:75:0x00f9, code lost:
        r4.release();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:87:?, code lost:
        return r0;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x009d A[ExcHandler: all (th java.lang.Throwable), Splitter:B:18:0x0073] */
    /* JADX WARNING: Removed duplicated region for block: B:43:0x00bb A[ExcHandler: all (th java.lang.Throwable), Splitter:B:7:0x003c] */
    /* JADX WARNING: Removed duplicated region for block: B:58:0x00d3  */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x00e6  */
    /* JADX WARNING: Removed duplicated region for block: B:73:0x00f4  */
    /* JADX WARNING: Removed duplicated region for block: B:75:0x00f9  */
    /* JADX WARNING: Removed duplicated region for block: B:81:0x0105  */
    /* JADX WARNING: Removed duplicated region for block: B:83:0x010a  */
    /* JADX WARNING: Removed duplicated region for block: B:87:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static int MakeCleaningFilter(org.opencv.core.Mat r15, epson.colorcorrection.SDIC_MAT_PTR r16, double r17, double r19) {
        /*
            r1 = 0
            org.opencv.core.Size r0 = r15.size()     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r2 = r0.width     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r2 = r19 / r2
            org.opencv.core.Size r0 = r15.size()     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r4 = r0.height     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            org.opencv.core.Size r0 = r15.size()     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r6 = r0.width     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            int r0 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1))
            if (r0 >= 0) goto L_0x0021
            org.opencv.core.Size r0 = r15.size()     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r2 = r0.height     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r2 = r19 / r2
        L_0x0021:
            org.opencv.core.Size r0 = new org.opencv.core.Size     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            org.opencv.core.Size r4 = r15.size()     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r4 = r4.width     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r4 = r4 * r2
            org.opencv.core.Size r6 = r15.size()     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r6 = r6.height     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            double r6 = r6 * r2
            r0.<init>(r4, r6)     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            org.opencv.core.Mat r2 = new org.opencv.core.Mat     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            r2.<init>()     // Catch:{ SDIC_Exception -> 0x00ec, CvException -> 0x00df, Exception -> 0x00cc, all -> 0x00c9 }
            r3 = r15
            org.opencv.imgproc.Imgproc.resize(r15, r2, r0)     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            r4 = 0
            int r0 = (r17 > r4 ? 1 : (r17 == r4 ? 0 : -1))
            if (r0 <= 0) goto L_0x00ac
            org.opencv.core.Size r0 = r15.size()     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            double r4 = r0.height     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            org.opencv.core.Size r0 = r15.size()     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            double r6 = r0.width     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            int r0 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1))
            if (r0 >= 0) goto L_0x005f
            org.opencv.core.Size r0 = r2.size()     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            double r4 = r0.height     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            double r4 = r4 * r17
            int r0 = (int) r4     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            goto L_0x0068
        L_0x005f:
            org.opencv.core.Size r0 = r2.size()     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            double r4 = r0.width     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            double r4 = r4 * r17
            int r0 = (int) r4     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
        L_0x0068:
            int r4 = r0 % 2
            if (r4 != 0) goto L_0x006e
            int r0 = r0 + 1
        L_0x006e:
            org.opencv.core.Mat r4 = new org.opencv.core.Mat     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            r4.<init>()     // Catch:{ SDIC_Exception -> 0x00c3, CvException -> 0x00c0, Exception -> 0x00bd, all -> 0x00bb }
            org.opencv.core.Size r10 = new org.opencv.core.Size     // Catch:{ SDIC_Exception -> 0x00a8, CvException -> 0x00a4, Exception -> 0x00a0, all -> 0x009d }
            double r0 = (double) r0     // Catch:{ SDIC_Exception -> 0x00a8, CvException -> 0x00a4, Exception -> 0x00a0, all -> 0x009d }
            r10.<init>(r0, r0)     // Catch:{ SDIC_Exception -> 0x00a8, CvException -> 0x00a4, Exception -> 0x00a0, all -> 0x009d }
            r11 = 0
            r13 = 0
            r8 = r2
            r9 = r4
            org.opencv.imgproc.Imgproc.GaussianBlur(r8, r9, r10, r11, r13)     // Catch:{ SDIC_Exception -> 0x00a8, CvException -> 0x00a4, Exception -> 0x00a0, all -> 0x009d }
            org.opencv.core.Mat r0 = r16.getMat()     // Catch:{ SDIC_Exception -> 0x00a8, CvException -> 0x00a4, Exception -> 0x00a0, all -> 0x009d }
            org.opencv.core.Size r1 = r15.size()     // Catch:{ SDIC_Exception -> 0x00a8, CvException -> 0x00a4, Exception -> 0x00a0, all -> 0x009d }
            org.opencv.imgproc.Imgproc.resize(r4, r0, r1)     // Catch:{ SDIC_Exception -> 0x00a8, CvException -> 0x00a4, Exception -> 0x00a0, all -> 0x009d }
            r3 = r16
            r3.setMat(r0)     // Catch:{ SDIC_Exception -> 0x009b, CvException -> 0x00a6, Exception -> 0x00a2, all -> 0x009d }
            r2.release()
            r4.release()
            r0 = 0
            goto L_0x00ff
        L_0x009b:
            r0 = move-exception
            goto L_0x00c7
        L_0x009d:
            r0 = move-exception
            goto L_0x0102
        L_0x00a0:
            r3 = r16
        L_0x00a2:
            r1 = r4
            goto L_0x00cf
        L_0x00a4:
            r3 = r16
        L_0x00a6:
            r1 = r4
            goto L_0x00e2
        L_0x00a8:
            r0 = move-exception
            r3 = r16
            goto L_0x00c7
        L_0x00ac:
            r3 = r16
            epson.colorcorrection.SDIC_Exception r0 = new epson.colorcorrection.SDIC_Exception     // Catch:{ SDIC_Exception -> 0x00b9, CvException -> 0x00b7, Exception -> 0x00b5, all -> 0x00bb }
            r4 = -7
            r0.<init>(r4)     // Catch:{ SDIC_Exception -> 0x00b9, CvException -> 0x00b7, Exception -> 0x00b5, all -> 0x00bb }
            throw r0     // Catch:{ SDIC_Exception -> 0x00b9, CvException -> 0x00b7, Exception -> 0x00b5, all -> 0x00bb }
        L_0x00b5:
            goto L_0x00cf
        L_0x00b7:
            goto L_0x00e2
        L_0x00b9:
            r0 = move-exception
            goto L_0x00c6
        L_0x00bb:
            r0 = move-exception
            goto L_0x0103
        L_0x00bd:
            r3 = r16
            goto L_0x00cf
        L_0x00c0:
            r3 = r16
            goto L_0x00e2
        L_0x00c3:
            r0 = move-exception
            r3 = r16
        L_0x00c6:
            r4 = r1
        L_0x00c7:
            r1 = r2
            goto L_0x00f0
        L_0x00c9:
            r0 = move-exception
            r2 = r1
            goto L_0x0103
        L_0x00cc:
            r3 = r16
            r2 = r1
        L_0x00cf:
            r0 = -400(0xfffffffffffffe70, float:NaN)
            if (r2 == 0) goto L_0x00d6
            r2.release()
        L_0x00d6:
            if (r1 == 0) goto L_0x00db
        L_0x00d8:
            r1.release()
        L_0x00db:
            r16.releaseMat()
            goto L_0x00ff
        L_0x00df:
            r3 = r16
            r2 = r1
        L_0x00e2:
            r0 = -100
            if (r2 == 0) goto L_0x00e9
            r2.release()
        L_0x00e9:
            if (r1 == 0) goto L_0x00db
            goto L_0x00d8
        L_0x00ec:
            r0 = move-exception
            r3 = r16
            r4 = r1
        L_0x00f0:
            int r0 = r0.mCode     // Catch:{ all -> 0x0100 }
            if (r1 == 0) goto L_0x00f7
            r1.release()
        L_0x00f7:
            if (r4 == 0) goto L_0x00fc
            r4.release()
        L_0x00fc:
            if (r0 == 0) goto L_0x00ff
            goto L_0x00db
        L_0x00ff:
            return r0
        L_0x0100:
            r0 = move-exception
            r2 = r1
        L_0x0102:
            r1 = r4
        L_0x0103:
            if (r2 == 0) goto L_0x0108
            r2.release()
        L_0x0108:
            if (r1 == 0) goto L_0x010d
            r1.release()
        L_0x010d:
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.colorcorrection.SDIC_PWC.MakeCleaningFilter(org.opencv.core.Mat, epson.colorcorrection.SDIC_MAT_PTR, double, double):int");
    }

    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}));
    }
}
