package kcsj_bmp2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
class Bmp{
int width; //宽度
int height; //高度
byte[] temp1; //储存高度和宽度之前的18位
byte[] temp2; //储存高度和宽度之后的28位
int skip; //读取数据时跳过的字节
int[][] map; //储存像素的数组
File selectFile; //选择的文件
BufferedInputStream bis; //输入流
BufferedOutputStream bos; //输出流
FileOutputStream fos;
MyPanel center;//绘图面板
JScrollPane scrollpane;//滚动面板
MyListener lis;//监听器
BmpHeader bmpH=new BmpHeader(); //位图文件头和信息头
BitmapData bmpD=new BitmapData(); //位图数据
/*创建输入流*/
void createInputStream() throws IOException{
if(selectFile!=null) {
FileInputStream fis = new FileInputStream(selectFile);
bis=new BufferedInputStream(fis);
}
}
/*创建输出流*/
void createOutputStream() throws IOException{
if(selectFile!=null) {
fos=new FileOutputStream(selectFile);
bos=new BufferedOutputStream(fos);
}
}
/*读所有前面的数据*/
public void readAll() throws IOException{
createInputStream();
if(bis!=null) {
bmpH.readHandW();
bmpH.readSkip();
}
}
/*保存*/
void save() {
try {
createOutputStream();
if(bos!=null) {
System.out.println("save");
bmpH.writeHandW();
bmpH.writeSkip();
bmpD.saveBMP();
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/*
*位图头文件
*/
public class BmpHeader{
private byte[] wb=new byte[4];//读取宽度的字节数组
private byte[] hb=new byte[4];//读取高度的字节数组
/*读取宽和高*/
public void readHandW() throws IOException{
temp1=new byte[18];
bis.read(temp1);//跳过前18个byte
bis.read(wb);//读取宽度
bis.read(hb);//读取高度
width=byteToint(wb);
height=byteToint(hb);
map=new int[height][width];
temp2=new byte[28];
bis.read(temp2);
}
/*写入宽和高*/
public void writeHandW() throws IOException{
bos.write(temp1);//
bos.write(intTobyte(width,4));//写入宽度
bos.write(intTobyte(height,4));//写入高度
bos.write(temp2);
}
/*读取时跳过字节*/
void readSkip() {
skip=4-width*3%4;//得到每行要跳过的数字(与windows 系统机制有关)
}
/*写入时跳过字节*/
void writeSkip(){
if(width*3/4!=0){
skip=4-width*3%4;
}
else {
skip=0;
}
}
/*字节转整数型*/
public int byteToint(byte b[])
{
int t1=(b[3]&0xff)<<24;
int t2=(b[2]&0xff)<<16;
int t3=(b[1]&0xff)<<8;
int t4=b[0]&0xff;
return t1+t2+t3+t4;
}
/*整数转字节型*/
public byte[] intTobyte(int a,int len)
{
byte []t=new byte[len];
t[0]=(byte) ((a&0xff));
if(len>1)
t[1]=(byte)((a&0xff00)>>8);
if(len>2)
t[2]=(byte)((a&0xff0000)>>16);
if(len>3)
t[3]=(byte)((a&0xff000000)>>24);
return t;
}
}
/*
*位图数据
*/
public class BitmapData{
/*读取*/
void readBmp() throws IOException {
//每三个字节一个像素,一个像素一个像素地读取
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
int blue=bis.read();
int green=bis.read();
int red=bis.read();
Color c=new Color(red,green,blue);
map[i][j]=c.getRGB();
}
if(skip!=4)
bis.skip(skip);
}
bis.close();
center.setPreferredSize(new Dimension(width,height));
javax.swing.SwingUtilities.updateComponentTreeUI(center);
}
/*翻转*/
void turnBmp() throws IOException{
for(int i=height-1;i>=0;i--)
{
for(int j=0;j<width;j++)
{
int blue=bis.read();
int green=bis.read();
int red=bis.read();
Color c=new Color(red,green,blue);
map[i][j]=c.getRGB();
}
if(skip!=4)
bis.skip(skip);
}
bis.close();
center.setPreferredSize(new Dimension(width,height));
javax.swing.SwingUtilities.updateComponentTreeUI(center);
}
/*镜像*/
void mirrorBmp() throws IOException {
for(int i=0;i<height;i++)
{
for(int j=width-1;j>=0;j--)
{
int blue=bis.read();
int green=bis.read();
int red=bis.read();
Color c=new Color(red,green,blue);
map[i][j]=c.getRGB();
}
if(skip!=4)
bis.skip(skip);
}
bis.close();
center.setPreferredSize(new Dimension(width,height));
javax.swing.SwingUtilities.updateComponentTreeUI(center);
}
/*底片*/
void filmBmp() throws IOException {
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
int blue=255-bis.read();
int green=255-bis.read();
int red=255-bis.read();
Color c=new Color(red,green,blue);
map[i][j]=c.getRGB();
}
if(skip!=4)
bis.skip(skip);
}
bis.close();
center.setPreferredSize(new Dimension(width,height));
javax.swing.SwingUtilities.updateComponentTreeUI(center);
}
/*变暗*/
void darkenBmp() throws IOException {
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
int blue=bis.read()/2;
int green=bis.read()/2;
int red=bis.read()/2;
Color c=new Color(red,green,blue);
map[i][j]=c.getRGB();
}
if(skip!=4)
bis.skip(skip);
}
bis.close();
center.setPreferredSize(new Dimension(width,height));
javax.swing.SwingUtilities.updateComponentTreeUI(center);
}
/*黑白*/
void dwBmp() throws IOException {
//每三个字节一个像素,一个像素一个像素地读取
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
int blue=bis.read();
int green=bis.read();
int red=bis.read();
int avg=(red+green+blue)/3;
Color c=new Color(avg,avg,avg);
map[i][j]=c.getRGB();
}
if(skip!=4)
bis.skip(skip);
}
bis.close();
center.setPreferredSize(new Dimension(width,height));
javax.swing.SwingUtilities.updateComponentTreeUI(center);
}
/*模糊*/
void blurBmp() throws IOException {
int[][] martrix = new int[5][5];
int[] values = new int[25];
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
int blue=bis.read();
int green=bis.read();
int red=bis.read();
Color c=new Color(red,green,blue);
map[i][j]=c.getRGB();
}
if(skip!=4)
bis.skip(skip);
}
bis.close();
for(int i=0;i<height;i++)
{
f