package epson.common;

import android.graphics.Bitmap;

import epson.print.copy.Component.escandevice.IScanController;

import java.io.FileOutputStream;

public class BMPFile {
    private static final int BITMAPFILEHEADER_SIZE = 14;
    private static final int BITMAPINFOHEADER_SIZE = 40;
    private int bfOffBits = 54;
    private int bfReserved1 = 0;
    private int bfReserved2 = 0;
    private int bfSize = 0;
    private byte[] bfType = {66, 77};
    private int biBitCount = 24;
    private int biClrImportant = 0;
    private int biClrUsed = 0;
    private int biCompression = 0;
    private int biHeight = 0;
    private int biPlanes = 1;
    private int biSize = 40;
    private int biSizeImage = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_AUTOFIT;
    private int biWidth = 0;
    private int biXPelsPerMeter = 0;
    private int biYPelsPerMeter = 0;
    private int[] bitmap;

    /* renamed from: fo */
    private FileOutputStream f331fo;

    public void saveBitmap(String str, Bitmap bitmap2, int i, int i2) {
        try {
            f331fo = new FileOutputStream(str);
            save(bitmap2, i, i2);
            f331fo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void save(Bitmap bitmap2, int i, int i2) {
        try {
            convertImage(bitmap2, i, i2);
            writeBitmapFileHeader();
            writeBitmapInfoHeader();
            writeBitmap();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean convertImage(Bitmap bitmap2, int i, int i2) {
        bitmap = null;
        int i3 = i * i2;
        bitmap = new int[i3];
        bitmap2.getPixels(this.bitmap, 0, i, 0, 0, i, i2);
        biSizeImage = (i3 * 3) + ((4 - ((i * 3) % 4)) * i2);
        bfSize = biSizeImage + 14 + 40;
        biWidth = i;
        biHeight = i2;
        return true;
    }

    private void writeBitmap() {
        int i;
        byte[] bArr = new byte[3];
        int i2 = biWidth;
        int i3 = (this.biHeight * i2) - 1;
        int i4 = 4 - ((i2 * 3) % 4);
        if (i4 == 4) {
            i4 = 0;
        }
        int i5 = i3 - biWidth;
        int i6 = i5;
        int i7 = 0;
        int i8 = 0;
        int i9 = 1;
        while (i7 < i3 + 1) {
            try {
                if (i7 < biWidth) {
                    i = bitmap[i5 + 1];
                } else {
                    i = bitmap[i5];
                }
                bArr[0] = (byte) (i & 255);
                bArr[1] = (byte) ((i >> 8) & 255);
                bArr[2] = (byte) ((i >> 16) & 255);
                f331fo.write(bArr);
                if (i9 == biWidth) {
                    i8 += i4;
                    for (int i10 = 1; i10 <= i4; i10++) {
                        f331fo.write(0);
                    }
                    i5 = i6 - biWidth;
                    i6 = i5;
                    i9 = 1;
                } else {
                    i9++;
                }
                i5++;
                i7++;
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
        int i11 = i8 - i4;
        bfSize += i11;
        biSizeImage += i11;
    }

    private void writeBitmapFileHeader() {
        try {
            f331fo.write(this.bfType);
            f331fo.write(intToDWord(this.bfSize));
            f331fo.write(intToWord(this.bfReserved1));
            f331fo.write(intToWord(this.bfReserved2));
            f331fo.write(intToDWord(this.bfOffBits));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void writeBitmapInfoHeader() {
        try {
            f331fo.write(intToDWord(this.biSize));
            f331fo.write(intToDWord(this.biWidth));
            f331fo.write(intToDWord(this.biHeight));
            f331fo.write(intToWord(this.biPlanes));
            f331fo.write(intToWord(this.biBitCount));
            f331fo.write(intToDWord(this.biCompression));
            f331fo.write(intToDWord(this.biSizeImage));
            f331fo.write(intToDWord(this.biXPelsPerMeter));
            f331fo.write(intToDWord(this.biYPelsPerMeter));
            f331fo.write(intToDWord(this.biClrUsed));
            f331fo.write(intToDWord(this.biClrImportant));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private byte[] intToWord(int i) {
        return new byte[]{(byte) (i & 255), (byte) ((i >> 8) & 255)};
    }

    private byte[] intToDWord(int i) {
        return new byte[]{(byte) (i & 255), (byte) ((i >> 8) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 24) & 255)};
    }
}
