package epson.scan.i2lib;

import android.support.annotation.NonNull;
import epson.scan.i2lib.ImageProcessController;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ImageProcessThread extends Thread {
    private static final String LOG_TAG = "ImageProcessThread";
    private final int mBlackAndWhiteThreshold;
    private final ImageProcessController mImageProcessController;

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

    public ImageProcessThread(@NonNull ImageProcessController imageProcessController, int i) {
        super(LOG_TAG);
        this.mImageProcessController = imageProcessController;
        this.mBlackAndWhiteThreshold = i;
    }

    public void run() {
        while (!isInterrupted()) {
            try {
                ImageProcessController.ImageProcessInfo checkNexImageProcessRequest = this.mImageProcessController.checkNexImageProcessRequest();
                if (checkNexImageProcessRequest.endRequested()) {
                    this.mImageProcessController.putImageProcessEnd(checkNexImageProcessRequest, true);
                    return;
                } else if (!processImage(checkNexImageProcessRequest, this.mBlackAndWhiteThreshold)) {
                    this.mImageProcessController.putImageProcessEnd(checkNexImageProcessRequest, false);
                    return;
                } else {
                    this.mImageProcessController.putImageProcessEnd(checkNexImageProcessRequest, true);
                }
            } catch (InterruptedException unused) {
                return;
            }
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:12:0x0031  */
    /* JADX WARNING: Removed duplicated region for block: B:15:0x0044 A[RETURN] */
    /* JADX WARNING: Removed duplicated region for block: B:17:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private boolean processImage(@android.support.annotation.NonNull epson.scan.i2lib.ImageProcessController.ImageProcessInfo r6, int r7) {
        /*
            r5 = this;
            boolean r0 = r6.rotate180()
            boolean r1 = r6.blackAndWhite()
            if (r0 != 0) goto L_0x000c
            if (r1 == 0) goto L_0x0045
        L_0x000c:
            org.opencv.core.Mat r2 = new org.opencv.core.Mat
            r2.<init>()
            java.lang.String r3 = r6.getInputFilename()
            org.opencv.core.Mat r3 = openImageFile(r3)
            r4 = 0
            if (r3 != 0) goto L_0x001d
            return r4
        L_0x001d:
            if (r1 == 0) goto L_0x002d
            blackAndWhite(r3, r2, r7)
            if (r0 == 0) goto L_0x002d
            r3.release()
            org.opencv.core.Mat r7 = new org.opencv.core.Mat
            r7.<init>()
            goto L_0x002f
        L_0x002d:
            r7 = r2
            r2 = r3
        L_0x002f:
            if (r0 == 0) goto L_0x0034
            rotate180(r2, r7)
        L_0x0034:
            java.lang.String r6 = r6.getOutputFilename()
            boolean r6 = org.opencv.imgcodecs.Imgcodecs.imwrite(r6, r7)
            r7.release()
            r2.release()
            if (r6 != 0) goto L_0x0045
            return r4
        L_0x0045:
            r6 = 1
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.scan.i2lib.ImageProcessThread.processImage(epson.scan.i2lib.ImageProcessController$ImageProcessInfo, int):boolean");
    }

    private static Mat openImageFile(@NonNull String str) {
        Mat imread = Imgcodecs.imread(str, -1);
        if (imread != null && imread.width() > 0 && imread.height() > 0) {
            return imread;
        }
        return null;
    }

    private static void rotate180(@NonNull Mat mat, @NonNull Mat mat2) {
        if (mat.width() <= 0 || mat.height() <= 0) {
            throw new IllegalArgumentException("orgImage size error");
        }
        Core.flip(mat, mat2, -1);
    }

    private static void blackAndWhite(@NonNull Mat mat, @NonNull Mat mat2, int i) {
        if (mat.width() <= 0 || mat.height() <= 0) {
            throw new IllegalArgumentException("orgImage size error");
        }
        Imgproc.threshold(mat, mat2, (double) i, 255.0d, 0);
    }
}
