package com.epson.cameracopy.printlayout;

import android.os.Parcel;
import android.os.Parcelable;



import com.epson.cameracopy.alt.SimpleBmpMerger;

import epson.print.Util.ImageFileUtil;
import epson.print.Util.OpenCvHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ImageAndLayout implements Parcelable {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    public static final Parcelable.Creator<ImageAndLayout> CREATOR = new Parcelable.Creator<ImageAndLayout>() {
        public ImageAndLayout createFromParcel(Parcel parcel) {
            return new ImageAndLayout(parcel);
        }

        public ImageAndLayout[] newArray(int i) {
            return new ImageAndLayout[i];
        }
    };
    private int mInterpolation;
    private boolean mIsPaperLandscape;
    private double mOffsetX;
    private double mOffsetY;
    private int mOpenCvExifRotation;
    private boolean mOpenCvExifRotationCancel;
    private String mOrgFilename;
    private double mPrintTargetHeight;
    private double mPrintTargetWidth;
    private int mPrintableAreaHeight;
    private int mPrintableAreaWidth;
    private int mRotation;

    public int describeContents() {
        return 0;
    }

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

    public ImageAndLayout() {
        mInterpolation = 2;
        mIsPaperLandscape = true;
        mOpenCvExifRotationCancel = true;
    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(mPrintableAreaWidth);
        parcel.writeInt(mPrintableAreaHeight);
        parcel.writeString(mOrgFilename);
        parcel.writeDouble(mPrintTargetWidth);
        parcel.writeDouble(mPrintTargetHeight);
        parcel.writeDouble(mOffsetX);
        parcel.writeDouble(mOffsetY);
        parcel.writeInt(mRotation);
        parcel.writeBooleanArray(new boolean[]{mIsPaperLandscape});
    }

    private ImageAndLayout(Parcel parcel) {
        mInterpolation = 2;
        mPrintableAreaWidth = parcel.readInt();
        mPrintableAreaHeight = parcel.readInt();
        mOrgFilename = parcel.readString();
        mPrintTargetWidth = parcel.readDouble();
        mPrintTargetHeight = parcel.readDouble();
        mOffsetX = parcel.readDouble();
        mOffsetY = parcel.readDouble();
        mRotation = parcel.readInt();
        boolean[] zArr = new boolean[1];
        parcel.readBooleanArray(zArr);
        mIsPaperLandscape = zArr[0];
        mOpenCvExifRotationCancel = true;
    }

    public void setOpenCvExifRotationCancel(boolean z) {
        mOpenCvExifRotationCancel = z;
    }

    public void setLayoutAreaSize(int i, int i2) {
        mPrintableAreaWidth = i;
        mPrintableAreaHeight = i2;
    }

    public void setLayout(double d, double d2, double d3, double d4, int i) {
        mPrintTargetWidth = d;
        mPrintTargetHeight = d2;
        mOffsetX = d3;
        mOffsetY = d4;
        mRotation = i;
    }

    public void setPaperIsLandscape(boolean z) {
        mIsPaperLandscape = z;
    }

    public boolean isPaperLandscape() {
        return mIsPaperLandscape;
    }

    public void setOrgFileName(String str) {
        mOrgFilename = str;
    }

    public String getOrgFileName() {
        return mOrgFilename;
    }

    private double[] getExRate(int[] iArr) {
        int i;
        int i2;
        double[] dArr = {1.0d, 1.0d};
        if (iArr == null || iArr.length < 2 || (i = mPrintableAreaWidth) <= 0 || (i2 = mPrintableAreaHeight) <= 0) {
            return dArr;
        }
        dArr[0] = (iArr[0]) / (i);
        dArr[1] = (iArr[1]) / (i2);
        return dArr;
    }

    static Mat new3x2Mat(double d, double d2, double d3, double d4, double d5, double d6) {
        final double d7 = d;
        final double d8 = d2;
        final double d9 = d3;
        final double d10 = d4;
        final double d11 = d5;
        final double d12 = d6;
        return new Mat(2, 3, 6) {
            {
                put(0, 0, d7, d8, d9);
                put(1, 0, d10, d11, d12);
            }
        };
    }

    static Mat getTransformMat0(Size size, Size size2, double d, double d2, int i) {
        MatOfPoint2f matOfPoint2f;
        Size size3 = size;
        Size size4 = size2;
        double d3 = size3.width;
        double d4 = size3.height;
        MatOfPoint2f matOfPoint2f2 = new MatOfPoint2f(new Point(0.0d, 0.0d), new Point(0.0d, size4.height), new Point(size4.width, 0.0d));
        switch (i & 3) {
            case 1:
                double d5 = d3 + d;
                double d6 = d2 + 0.0d;
                matOfPoint2f = new MatOfPoint2f(new Point(d5, d6), new Point(d + 0.0d, d6), new Point(d5, d4 + d2));
                break;
            case 2:
                double d7 = d3 + d;
                double d8 = d4 + d2;
                matOfPoint2f = new MatOfPoint2f(new Point(d7, d8), new Point(d7, d2 + 0.0d), new Point(d + 0.0d, d8));
                break;
            case 3:
                double d9 = d + 0.0d;
                double d10 = d4 + d2;
                matOfPoint2f = new MatOfPoint2f(new Point(d9, d10), new Point(d3 + d, d10), new Point(d9, d2 + 0.0d));
                break;
            default:
                double d11 = d + 0.0d;
                double d12 = d2 + 0.0d;
                matOfPoint2f = new MatOfPoint2f(new Point(d11, d12), new Point(d11, d4 + d2), new Point(d3 + d, d12));
                break;
        }
        Mat affineTransform = Imgproc.getAffineTransform(matOfPoint2f2, matOfPoint2f);
        matOfPoint2f2.release();
        matOfPoint2f.release();
        return affineTransform;
    }

    static Mat getTransformMat1(Size size, Size size2, double d, double d2, int i) {
        MatOfPoint2f matOfPoint2f;
        Size size3 = size;
        Size size4 = size2;
        double d3 = size3.width;
        double d4 = size3.height;
        double d5 = size3.width;
        double d6 = size4.width;
        double d7 = size3.height;
        double d8 = size4.height;
        MatOfPoint2f matOfPoint2f2 = new MatOfPoint2f(new Point(0.0d, 0.0d), new Point(0.0d, size4.height), new Point(size4.width, 0.0d));
        switch (i & 3) {
            case 1:
                double d9 = d3 + d;
                double d10 = d2 + 0.0d;
                matOfPoint2f = new MatOfPoint2f(new Point(d9, d10), new Point(d + 0.0d, d10), new Point(d9, d4 + d2));
                break;
            case 2:
                double d11 = d3 + d;
                double d12 = d4 + d2;
                matOfPoint2f = new MatOfPoint2f(new Point(d11, d12), new Point(d11, d2 + 0.0d), new Point(d + 0.0d, d12));
                break;
            case 3:
                double d13 = d + 0.0d;
                double d14 = d4 + d2;
                matOfPoint2f = new MatOfPoint2f(new Point(d13, d14), new Point(d3 + d, d14), new Point(d13, d2 + 0.0d));
                break;
            default:
                double d15 = d + 0.0d;
                double d16 = d2 + 0.0d;
                matOfPoint2f = new MatOfPoint2f(new Point(d15, d16), new Point(d15, d4 + d2), new Point(d3 + d, d16));
                break;
        }
        Mat affineTransform = Imgproc.getAffineTransform(matOfPoint2f2, matOfPoint2f);
        matOfPoint2f2.release();
        matOfPoint2f.release();
        return affineTransform;
    }

    static Mat getTransformMat(Size size, Size size2, double d, double d2, int i) {
        double d3;
        double d4;
        Size size3 = size;
        Size size4 = size2;
        if ((i & 1) == 0) {
            double d5 = size3.width / size4.width;
            d4 = size3.height / size4.height;
            d3 = d5;
        } else {
            double d6 = size3.width / size4.height;
            d4 = size3.height / size4.width;
            d3 = d6;
        }
        int i2 = (d3 / 2.0d);
        int i3 = (d4 / 2.0d);
        switch (i & 3) {
            case 1:
                return new3x2Mat(0.0d, -d3, ((d + size3.width) - (i2)) - 1.0d, d4, 0.0d, d2 + (i3));
            case 2:
                return new3x2Mat(-d3, 0.0d, ((d + size3.width) - (i2)) - 1.0d, 0.0d, -d4, ((d2 + size3.height) - (i3)) - 1.0d);
            case 3:
                return new3x2Mat(0.0d, d3, d + (i2), -d4, 0.0d, ((d2 + size3.height) - (i3)) - 1.0d);
            default:
                return new3x2Mat(d3, 0.0d, d + (i2), 0.0d, d4, d2 + (i3));
        }
    }

    private Size getPrintImageSize(double[] dArr) {
        return new Size(mPrintTargetWidth * dArr[0], mPrintTargetHeight * dArr[1]);
    }

    private Mat getPrintMat(int[] iArr, int i, Mat mat) {
        switch (i & 3) {
            case 1:
                Mat mat2 = new Mat(2, 3, 6);
                mat2.put(0, 0, mat.get(1, 0)[0] * -1.0d);
                mat2.put(0, 1, mat.get(1, 1)[0] * -1.0d);
                mat2.put(0, 2, (mat.get(1, 2)[0] * -1.0d) + (iArr[0]));
                mat2.put(1, 0, mat.get(0, 0)[0]);
                mat2.put(1, 1, mat.get(0, 1)[0]);
                mat2.put(1, 2, mat.get(0, 2)[0]);
                return mat2;
            case 2:
                Mat mat3 = null;
                mat3.put(0, 0, mat.get(0, 0)[0] * -1.0d);
                mat3.put(0, 1, mat.get(0, 1)[0] * -1.0d);
                mat3.put(0, 2, (mat.get(0, 2)[0] * -1.0d) + (iArr[0]));
                mat3.put(1, 0, mat.get(1, 0)[0] * -1.0d);
                mat3.put(1, 1, mat.get(1, 1)[0] * -1.0d);
                mat3.put(1, 2, (mat.get(1, 2)[0] * -1.0d) + (iArr[1]));
                return mat3;
            case 3:
                Mat mat4 = new Mat(2, 3, 6);
                mat4.put(0, 0, mat.get(1, 0)[0]);
                mat4.put(0, 1, mat.get(1, 1)[0]);
                mat4.put(0, 2, mat.get(1, 2)[0] + (iArr[0]));
                mat4.put(1, 0, mat.get(0, 0)[0] * -1.0d);
                mat4.put(1, 1, mat.get(0, 1)[0] * -1.0d);
                mat4.put(1, 2, mat.get(0, 2)[0] * -1.0d);
                return mat4;
            default:
                return mat.clone();
        }
    }

    public Mat makePrintImageMat(Mat mat, int i, int[] iArr) {
        Mat transMat = getTransMat(mat, i, iArr);
        Mat mat2 = new Mat(iArr[1], iArr[0], mat.type());
        Imgproc.warpAffine(mat, mat2, transMat, mat2.size(), mInterpolation, 0, new Scalar(255.0d, 255.0d, 255.0d, 255.0d));
        transMat.release();
        return mat2;
    }

    private Mat getTransMat(Mat mat, int i, int[] iArr) {
        int[] iArr2 = new int[2];
        if ((i & 1) != 0) {
            iArr2[0] = iArr[1];
            iArr2[1] = iArr[0];
        } else {
            iArr2[0] = iArr[0];
            iArr2[1] = iArr[1];
        }
        double[] exRate = getExRate(iArr2);
        Size printImageSize = getPrintImageSize(exRate);
        double d = mOffsetX * exRate[0];
        double d2 = mOffsetY;
        int imageRotation = getImageRotation(mRotation);
        Mat transformMat = getTransformMat(printImageSize, mat.size(), d, exRate[1] * d2, imageRotation);
        Mat printMat = getPrintMat(iArr, i, transformMat);
        transformMat.release();
        return printMat;
    }

    private boolean affineBand(Mat mat, Mat mat2, int i, int i2, int i3, File file) {
        Mat clone = mat2.clone();
        Mat mat3 = new Mat();
        Size size = new Size(i, i2);
        clone.put(1, 2, clone.get(1, 2)[0] - (i3));
        Mat mat4 = null;
        try {
            Imgproc.warpAffine(mat, mat3, clone, size, mInterpolation, 0, new Scalar(255.0d, 255.0d, 255.0d, 255.0d));
            if (mat.type() == CvType.CV_8UC1) {
                mat4 = make24BitMatFromGrayMat(mat3);
                if (!Imgcodecs.imwrite(file.toString(), mat4)) {
                    clone.release();
                    mat3.release();
                    if (mat4 != null) {
                        mat4.release();
                    }
                    return false;
                }
            } else if (!Imgcodecs.imwrite(file.toString(), mat3)) {
                clone.release();
                mat3.release();
                return false;
            }
            clone.release();
            mat3.release();
            if (mat4 != null) {
                mat4.release();
            }
            return true;
        } catch (Exception unused) {
            clone.release();
            mat3.release();
            if (mat4 != null) {
                mat4.release();
            }
            return false;
        } catch (Throwable th) {
            clone.release();
            mat3.release();
            if (mat4 != null) {
                mat4.release();
            }
            throw th;
        }
    }

    private boolean makeBandImageFile(Mat mat, int i, int[] iArr, String str, LinkedList<File> linkedList) {
        String str2 = str;
        LinkedList<File> linkedList2 = linkedList;
        Mat transMat = getTransMat(mat, i, iArr);
        int i2 = 0;
        int i3 = 0;
        while (true) {
            int i4 = i2 + 512;
            if (i4 < iArr[1]) {
                File file = new File(str2, "bo" + i3 + ".bmp");
                affineBand(mat, transMat, iArr[0], 512, i2, file);
                if (!file.exists()) {
                    return false;
                }
                linkedList2.addFirst(file);
                i3++;
                i2 = i4;
            } else {
                File file2 = new File(str2, "bo" + i3 + ".bmp");
                if (!affineBand(mat, transMat, iArr[0], iArr[1] - i2, i2, file2)) {
                    return false;
                }
                linkedList2.addFirst(file2);
                return true;
            }
        }
    }

    private boolean makePrintImageFile(Mat mat, int i, int[] iArr, String str) {
        String parent = new File(str).getParent();
        LinkedList linkedList = new LinkedList();
        if (makeBandImageFile(mat, i, iArr, parent, linkedList) && SimpleBmpMerger.margeBmpFile(str, linkedList.iterator())) {
            return true;
        }
        return false;
    }

    private Mat make24BitMatFromGrayMat(Mat mat) {
        Mat mat2 = new Mat(mat.height(), mat.width(), CvType.CV_8UC3);
        Core.mixChannels(Arrays.asList(new Mat[]{mat}), Arrays.asList(new Mat[]{mat2}), new MatOfInt(0, 0, 0, 1, 0, 2));
        return mat2;
    }

    public boolean createPrintData(String str, boolean z, int[] iArr, int i) {
        Mat mat = null;
        try {
            setOpenCvJpegRotation(mOrgFilename);
            Mat imread = Imgcodecs.imread(mOrgFilename, -1);
            mat = imread.channels() == 4 ? convertRgbaToRgbTrWhite(imread) : imread;
            if (z) {
                int channels = mat.channels();
                if (channels != 1) {
                    if (channels != 3) {
                        if (mat != null) {
                            mat.release();
                        }
                        File file = new File(str);
                        if (file.exists()) {
                            file.delete();
                        }
                        return false;
                    }
                    Imgproc.cvtColor(mat, mat, 7);
                    Imgproc.cvtColor(mat, mat, 8, 3);
                }
            }
            boolean makePrintImageFile = makePrintImageFile(mat, i, iArr, str);
            if (mat != null) {
                mat.release();
            }
            if (!makePrintImageFile) {
                File file2 = new File(str);
                if (file2.exists()) {
                    file2.delete();
                }
            }
            return makePrintImageFile;
        } catch (OutOfMemoryError unused) {
            if (mat != null) {
                mat.release();
            }
            File file3 = new File(str);
            if (file3.exists()) {
                file3.delete();
            }
            return false;
        } catch (Exception unused2) {
            if (mat != null) {
                mat.release();
            }
            File file4 = new File(str);
            if (file4.exists()) {
                file4.delete();
            }
            return false;
        } catch (Throwable th) {
            if (mat != null) {
                mat.release();
            }
            File file5 = new File(str);
            if (file5.exists()) {
                file5.delete();
            }
            throw th;
        }
    }

    @VisibleForTesting
    static Mat calc255Alpha(Mat mat) {
        Mat mat2 = new Mat();
        Core.multiply(mat, new Scalar(-255.0d), mat2);
        Mat mat3 = new Mat();
        Core.add(mat2, new Scalar(255.0d), mat3);
        mat2.release();
        return mat3;
    }

    @VisibleForTesting
    @NonNull
    static Mat convertRgbaToRgbTrWhite(Mat mat) {
        ArrayList arrayList = new ArrayList();
        Core.split(mat, arrayList);
        mat.release();
        Mat mat2 = new Mat();
        ((Mat) arrayList.get(3)).convertTo(mat2, CvType.CV_32FC1, 0.00392156862745098d);
        ((Mat) arrayList.get(3)).release();
        arrayList.remove(3);
        ArrayList arrayList2 = new ArrayList();
        for (int i = 0; i < 3; i++) {
            arrayList2.add(mat2);
        }
        Mat mat3 = new Mat();
        Core.merge(arrayList2, mat3);
        Mat mat4 = new Mat();
        Mat mat5 = new Mat();
        Core.merge(arrayList, mat4);
        ((Mat) arrayList.get(0)).release();
        ((Mat) arrayList.get(1)).release();
        ((Mat) arrayList.get(2)).release();
        mat4.convertTo(mat5, CvType.CV_32FC1);
        mat4.release();
        Mat mul = mat5.mul(mat3);
        mat5.release();
        Mat mat6 = new Mat();
        mul.convertTo(mat6, CvType.CV_8UC3);
        mul.release();
        Mat calc255Alpha = calc255Alpha(mat2);
        Mat mat7 = new Mat();
        calc255Alpha.convertTo(mat7, CvType.CV_8UC1);
        Mat mat8 = new Mat();
        Imgproc.cvtColor(mat7, mat8, 8, 3);
        Core.add(mat6, mat8, mat6);
        return mat6;
    }

    @NonNull
    private static Mat convertRgbaToRgb(Mat mat) {
        ArrayList arrayList = new ArrayList();
        Core.split(mat, arrayList);
        mat.release();
        Mat mat2 = new Mat();
        ((Mat) arrayList.get(3)).convertTo(mat2, CvType.CV_32FC1, 0.00392156862745098d);
        ((Mat) arrayList.get(3)).release();
        arrayList.remove(3);
        ArrayList arrayList2 = new ArrayList();
        for (int i = 0; i < 3; i++) {
            arrayList2.add(mat2);
        }
        Mat mat3 = new Mat();
        Core.merge(arrayList2, mat3);
        mat2.release();
        Mat mat4 = new Mat();
        Mat mat5 = new Mat();
        Core.merge(arrayList, mat4);
        ((Mat) arrayList.get(0)).release();
        ((Mat) arrayList.get(1)).release();
        ((Mat) arrayList.get(2)).release();
        mat4.convertTo(mat5, CvType.CV_32FC1);
        mat4.release();
        Mat mul = mat5.mul(mat3);
        mat5.release();
        Mat mat6 = new Mat();
        mul.convertTo(mat6, CvType.CV_8UC3);
        mul.release();
        return mat6;
    }

    private void setOpenCvJpegRotation(String str) {
        mOpenCvExifRotation = 0;
        if (OpenCvHelper.doseImreadRotateWithExifTag()) {
            mOpenCvExifRotation = ImageFileUtil.getExifRotationDegree(str);
            if (mOpenCvExifRotation < 0) {
                mOpenCvExifRotation = 0;
            }
        }
    }

    private int getImageRotation(int i) {
        if (mOpenCvExifRotationCancel) {
            i -= mOpenCvExifRotation;
        }
        if (i < 0) {
            i += (((-i) / 4) + 1) * 4;
        }
        return i % 4;
    }

    private void setInterpolation(int i) {
        mInterpolation = i;
    }
}
