/*
 * Decompiled with CFR 0.152.
 */
package com.hst.hwface.utils;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

public class ImageUtils {
    public static byte[] convertRGB32ToGray(BufferedImage image) throws Exception {
        int[] pix;
        int height;
        int width = image.getWidth();
        PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height = image.getHeight(), pix = new int[width * height], 0, width);
        if (!pg.grabPixels()) {
            throw new Exception("\u521d\u59cb\u5316\u539f\u56fe\u7247\u50cf\u7d20\u6570\u7ec4\u5931\u8d25\uff01");
        }
        byte[] grayPix = new byte[width * height];
        int i = pix.length - 1;
        while (i >= 0) {
            grayPix[i] = (byte)(((pix[i] & 0xFF0000) >> 16) + ((pix[i] & 0xFF00) >> 8 << 1) + (pix[i] & 0xFF) >> 2);
            --i;
        }
        return grayPix;
    }

    public static byte[] convertToRGB24(BufferedImage image) throws Exception {
        int[] pix;
        int height;
        int width = image.getWidth();
        PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height = image.getHeight(), pix = new int[width * height], 0, width);
        if (!pg.grabPixels()) {
            throw new Exception("\u521d\u59cb\u5316\u539f\u56fe\u7247\u50cf\u7d20\u6570\u7ec4\u5931\u8d25\uff01");
        }
        byte[] rgb24 = new byte[width * height * 3];
        int i = pix.length - 1;
        int j = rgb24.length - 1;
        while (i >= 0) {
            rgb24[j--] = (byte)(pix[i] & 0xFF);
            rgb24[j--] = (byte)((pix[i] & 0xFF00) >> 8);
            rgb24[j--] = (byte)((pix[i] & 0xFF0000) >> 16);
            --i;
        }
        return rgb24;
    }

    public static byte[] convertRGB32ToGray(File imgFile) throws Exception {
        BufferedImage image = ImageIO.read(imgFile);
        return ImageUtils.convertRGB32ToGray(image);
    }

    public static byte[] convertRGB32ToGray(int[] rgbArray, int width, int height) throws Exception {
        BufferedImage image = new BufferedImage(width, height, 1);
        image.setRGB(0, 0, width, height, rgbArray, 0, width);
        return ImageUtils.convertRGB32ToGray(image);
    }

    public static byte[] getGrayImageMirror(byte[] gray, int width, int height) throws Exception {
        if (gray == null || gray.length == 0) {
            return null;
        }
        byte[] mirror = new byte[gray.length];
        try {
            int i = 0;
            while (i < height) {
                int j = 0;
                while (j < width) {
                    int idx = width * i + j;
                    int mirIdx = width * i + (width - 1 - j);
                    mirror[mirIdx] = gray[idx];
                    ++j;
                }
                ++i;
            }
        }
        catch (Exception exception) {
            throw new Exception("\u83b7\u53d6\u955c\u50cf\u56fe\u51fa\u9519\uff01");
        }
        return mirror;
    }

    public static byte[] scaleImage(byte[] srcImgBytes, int newWidth) {
        if (srcImgBytes == null) {
            return null;
        }
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(srcImgBytes);
            BufferedImage srcImg = ImageIO.read(inputStream);
            int width = srcImg.getWidth();
            int height = srcImg.getHeight();
            int toWidth = newWidth;
            int toHeight = height * toWidth / width;
            Image toImg = srcImg.getScaledInstance(toWidth, toHeight, 4);
            BufferedImage result = new BufferedImage(toWidth, toHeight, 1);
            Graphics2D g = result.createGraphics();
            g.drawImage(toImg, 0, 0, null);
            g.dispose();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ImageOutputStream out = ImageIO.createImageOutputStream(bout);
            ImageIO.write((RenderedImage)result, "JPG", out);
            return bout.toByteArray();
        }
        catch (Exception e) {
            System.out.println("\u56fe\u7247\u7f29\u653e\u5931\u8d25\uff1a" + e.getLocalizedMessage());
            return null;
        }
    }
}
