为缺少调色板的png图片添加调色板

为缺少调色板的png图片添加调色板

public class AddPLTE {

    public AddPLTE (){
    }

    public byte[] getbyte(InputStream bm) {
        try {
            byte[] pixel = InputStream2Bytes2(bm);
            byte[] haha = imgConvert(pixel);
            return haha;
        } catch (Exception e) {
            return null;
        }
    }

    public boolean noPlte(InputStream bm){
        try {
            boolean ispltepng = false;
            boolean nopltepng = false;
            byte[] content = InputStream2Bytes2(bm);
            if (content[25] == 3) {
                ispltepng = true;
            }
            int findplte = 0;
            for (int idx = 0; idx < content.length; idx++) {
                if (content[idx] == 80 && content[idx + 1] == 76 && content[idx + 2] == 84 && content[idx + 3] == 69) {
                    findplte = idx;
                    break;
                }
            }
            if (findplte == 0) {
                nopltepng = true;
            }
            if (ispltepng && nopltepng) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public byte[] imgConvert(byte content[]) {
        try {
            int end = 0;
            for (int idx = 0; idx < content.length; idx++) {
                if (content[idx] == 0x49 && content[idx + 1] == 0x44 && content[idx + 2] == 0x41 && content[idx + 3] == 0x54) {
                    end = idx;
                    break;
                }
            }
            end = end-4;
            byte[] sub1 = Arrays.copyOf(content, end);
            byte[] sub2 = Arrays.copyOfRange(content, end, content.length);
            byte[] head = concat(sub1, plte);
            byte[] tail = concat(head, sub2);
            return tail;
        } catch (Exception e) {
            return content;
        }
    }

    private static byte[] InputStream2Bytes2(InputStream inputStream) throws IOException {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024*32];
            int num = inputStream.read(buffer);
            while (num != -1) {
                baos.write(buffer, 0, num);
                num = inputStream.read(buffer);
            }
            baos.flush();
            return baos.toByteArray();
        } finally {

        }
    }
    
    private static byte[] concat(byte[] a, byte[] b) {
        byte[] c= new byte[a.length+b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    private byte plte[] =  {
            0,	0,	1,	-128,    //PLTE数据块长度
            80,	76,	84,	69,      //数据块类型标志
            0,	0,	0,
            -1,	0,	0,
            0,	-1,	0,
            -1,	-1,	0,
            0,	0,	-1,
            -1,	0,	-1,
            0,	-1,	-1,
            -1,	-1,	-1,
            0,	0,	0,
            -81,	0,	0,
            0,	-81,	0,
            -81,	-81,	0,
            0,	0,	-81,
            -81,	0,	-81,
            0,	-81,	-81,
            -81,	-81,	-81,
            0,	0,	85,
            0,	85,	0,
            0,	85,	85,
            0,	85,	-81,
            0,	85,	-1,
            0,	-81,	85,
            0,	-81,	-1,
            0,	-1,	85,
            0,	-1,	-81,
            85,	0,	0,
            85,	0,	85,
            85,	0,	-81,
            85,	0,	-1,
            85,	85,	0,
            85,	85,	85,
            85,	85,	-81,
            85,	85,	-1,
            85,	-81,	0,
            85,	-81,	85,
            85,	-81,	-81,
            85,	-81,	-1,
            85,	-1,	0,
            85,	-1,	85,
            85,	-1,	-81,
            85,	-1,	-1,
            -81,	0,	85,
            -81,	0,	-1,
            -81,	85,	0,
            -81,	85,	85,
            -81,	85,	-81,
            -81,	85,	-1,
            -81,	-81,	85,
            -81,	-81,	-1,
            -81,	-1,	0,
            -81,	-1,	85,
            -81,	-1,	-81,
            -81,	-1,	-1,
            -1,	0,	85,
            -1,	0,	-81,
            -1,	85,	0,
            -1,	85,	85,
            -1,	85,	-81,
            -1,	85,	-1,
            -1,	-81,	0,
            -1,	-81,	85,
            -1,	-81,	-81,
            -1,	-81,	-1,
            -1,	-1,	85,
            -1,	-1,	-81,
            0,	0,	0,
            -1,	0,	0,
            0,	-1,	0,
            -1,	-1,	0,
            0,	0,	-1,
            -1,	0,	-1,
            0,	-1,	-1,
            -1,	-1,	-1,
            -81,	0,	0,
            0,	-81,	0,
            -81,	-81,	0,
            0,	0,	-81,
            -81,	0,	-81,
            0,	-81,	-81,
            -81,	-81,	-81,
            0,	0,	85,
            0,	85,	0,
            0,	85,	85,
            0,	85,	-81,
            0,	85,	-1,
            0,	-81,	85,
            0,	-81,	-1,
            0,	-1,	85,
            0,	-1,	-81,
            85,	0,	0,
            85,	0,	85,
            85,	0,	-81,
            85,	0,	-1,
            85,	85,	0,
            85,	85,	85,
            85,	85,	-81,
            85,	85,	-1,
            85,	-81,	0,
            85,	-81,	85,
            85,	-81,	-81,
            85,	-81,	-1,
            85,	-1,	0,
            85,	-1,	85,
            85,	-1,	-81,
            85,	-1,	-1,
            -81,	0,	85,
            -81,	0,	-1,
            -81,	85,	0,
            -81,	85,	85,
            -81,	85,	-81,
            -81,	85,	-1,
            -81,	-81,	85,
            -81,	-81,	-1,
            -81,	-1,	0,
            -81,	-1,	85,
            -81,	-1,	-81,
            -81,	-1,	-1,
            -1,	0,	85,
            -1,	0,	-81,
            -1,	85,	0,
            -1,	85,	85,
            -1,	85,	-81,
            -1,	85,	-1,
            -1,	-81,	0,
            -1,	-81,	85,
            -1,	-81,	-81,
            -1,	-81,	-1,
            -1,	-1,	85,
            61,	25,	64,	-66   //CRC校验码
    };
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值