⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmp3.java

📁 It is basic in which i am compressing bitmap images.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.awt.image.*;
import javax.swing.*;

public class bmp3 extends JFrame implements ActionListener
{
    static JLabel l1, l2, l3, dummy1, dummy8, l4, l5, l6, l7, dummy2, dummy3, dummy4, dummy5, dummy6, dummy7, lwt, lcmp, disp, lsizevalue, lsizeJLabel, lsizespace, lcompsizevalue, lcompsizeJLabel, lcompsizespace;
    static String s, fnames, fnamed; //fnames for source and fnamed for dest
    static JTextField txt, tct, wt, cmp;
    JButton b1, b2, bb1, bb2, bb3, bb4, bm1, bm2, b3;
    static Color cml;
    frame window;

    int pixels[] = new int [256 * 256];
    int pixels1[] = new int [256 * 256];

    int pixel;
    int condition = 0;
    int condition_de = 0;
    Image img, img1;


    static int q[] [] = new int [256] [256]; //for image
    static int x[] [] = new int [4096] [16]; //for training set
    static int c[] [] = new int [256] [16]; //for code book
    static int map[] = new int [4096]; //for mapping of training on code
    //For green
    static int qg[] [] = new int [256] [256]; //for image
    static int xg[] [] = new int [4096] [16]; //for training set
    static int cg[] [] = new int [256] [16]; //for code book
    static int mapg[] = new int [4096]; //for mapping of training on code
    //For Blue
    static int qb[] [] = new int [256] [256]; //for image
    static int xb[] [] = new int [4096] [16]; //for training set
    static int cb[] [] = new int [256] [16]; //for code book
    static int mapb[] = new int [4096]; //for mapping of training on code
    //Commonly for all R,G,B
    static int width, height, N; //for width and height of image
    static int i, j, temp, rcnt, ccnt, k; //for loops rcnt row count
    static int blocksize, ppixel; //for 4*4 = 16 pixels , pixels = 4
    static byte type[] = new byte [6]; //for detecting type of image
    static char key;
    static int limit, t;
    static long width1;
    static double avgerror;
    static long ex_s, ex_e; //execution time start and end
    /*
    blocksize of 4 can be max supported in 64 X 64  image
    */

    public bmp3 () throws IOException
    {
	super ("Image Compression...");

	l1 = new JLabel ("Input FileName:  ");
	l2 = new JLabel ("Output FileName: ");
	l3 = new JLabel ("Update Codebook Using :");
	l4 = new JLabel ("Time required for last operation : ");
	l5 = new JLabel ("                  ");
	l6 = new JLabel ("Mean Squared Error : ");
	l7 = new JLabel ("          ");
	dummy1 = new JLabel ("                                     ");
	dummy2 = new JLabel ("                                  ");
	disp = new JLabel ("                                                                                        ");
	dummy3 = new JLabel ("                      ");
	dummy4 = new JLabel ("                ");
	dummy5 = new JLabel ("                                              ");
	dummy6 = new JLabel ("                                                               ");
	dummy7 = new JLabel ("                   ");
	dummy8 = new JLabel ("                                    ");
	tct = new JTextField (35);
	lwt = new JLabel ("Width & Height:     ");
	wt = new JTextField (5);
	lcmp = new JLabel ("Group Pixels");
	cmp = new JTextField (2);
	txt = new JTextField (35);
	lsizeJLabel = new JLabel ("Original Image Size : ");
	lsizevalue = new JLabel ("           ");
	lsizespace = new JLabel ("             ");
	lcompsizeJLabel = new JLabel ("Compress Image Size : ");
	lcompsizevalue = new JLabel ("           ");
	lcompsizespace = new JLabel ("             ");

	txt.setEditable (true); //give path

	b1 = new JButton ("Open");
	b2 = new JButton ("Save As");
	bm1 = new JButton ("Encode Image");
	bm2 = new JButton ("Decode Image");
	bb1 = new JButton ("LBG");
	bb2 = new JButton ("M LBG");
	bb3 = new JButton ("K-Mean");
	bb4 = new JButton ("PNN");
	b3 = new JButton ("HELP");

	setLayout (new FlowLayout ());
	addWindowListener (new WindowAdapter ()
	{
	    public void windowClosing (WindowEvent we)
	    {
		dispose ();
		System.exit (0);
	    }
	}
	);


	repaint ();

	add (l1);
	add (txt);
	add (b1);
	add (dummy7);
	add (l2);
	add (tct);
	add (b2);
	add (dummy4);
	add (lwt);
	add (wt);
	add (lcmp);
	add (cmp);
	add (dummy6);
	add (l3);
	add (bb1);
	add (bb2);
	add (bb3);
	add (bb4);
	add (dummy5);
	add (dummy8);
	add (bm1);
	add (bm2);
	add (b3);
	add (dummy1);
	add (lsizeJLabel);
	add (lsizevalue);
	add (lsizespace);
	add (lcompsizeJLabel);
	add (lcompsizevalue);
	add (lcompsizespace);
	add (l4);
	add (l5);
	add (dummy2);
	add (l6);
	add (l7);
	add (dummy3);
	add (disp);

	b1.addActionListener (this);
	b2.addActionListener (this);
	bb1.addActionListener (this);
	bb2.addActionListener (this);
	bb3.addActionListener (this);
	bb4.addActionListener (this);
	bm1.addActionListener (this);
	bm2.addActionListener (this);
	b3.addActionListener (this);

	window = new frame (this);
	window.setSize (2000, 700);
	window.setLocation (0, 0);

    }


    static double sq (double x)
    {
	x = x * x;
	return x;
    }


    public void paint (Graphics g)
    {
	if (condition_de == 1)
	{
	    g.drawImage (img1, 268, 250, this);
	}
	else if (condition == 1)
	{
	    g.drawImage (img, 7, 250, this);
	    g.drawImage (img1, 268, 250, this);
	}
    }


    static public void res_timer ()
    {
	ex_s = System.currentTimeMillis ();
    }


    static public void print_time ()
    {
	ex_e = System.currentTimeMillis ();
	l5.setText ("" + (ex_e - ex_s) / 1000.0 + " Sec.");
    }


    static public void getch () throws Exception
    {
	BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
	br.read ();
    }


    static void p (String str) throws IOException
    {
	System.out.print (str);
	disp.setText (str);
    }


    static void calcu () throws Exception
	//calculates limit,N,blocksize from width and group pixel
    {
	blocksize = ppixel * ppixel;
	limit = (height * width) / blocksize;
	N = limit / blocksize;
	p ("BS = " + blocksize + " L = " + limit + " N = " + N);
    }


    //reads image and stores in x[][] training vectors
    int rdimg (InputStream f) throws Exception
    {
	type [0] = (byte) f.read ();
	type [1] = (byte) f.read ();
	if (type [0] != 'B' && type [1] != 'M')
	{
	    p ("Image is not BMP");
	    System.exit (0);
	}
	f.skip (52);
	width = Integer.parseInt (wt.getText ());
	height = width;
	ppixel = Integer.parseInt (cmp.getText ());
	blocksize = ppixel * ppixel;
	pixel = width * height;
	dummy6.setText (" Codebook Size : " + pixel / (blocksize * blocksize));
	pixel += height;
	for (i = 0 ; i < height ; i++)
	{
	    pixel -= height * 2;
	    for (j = 0 ; j < width ; j++)
	    {
		q [i] [j] = (int) f.read ();
		qg [i] [j] = (int) f.read ();
		qb [i] [j] = (int) f.read ();
		pixels [pixel++] = (255 << 24) | (qb [i] [j] << 16) | (qg [i] [j] << 8) | (q [i] [j]);
	    }
	}
	img = createImage (new MemoryImageSource (width, height, ColorModel.getRGBdefault (), pixels, 0, width));
	condition = 1;
	repaint ();
	rcnt = (int) f.read ();
	limit = (height * width) / blocksize;
	p ("\nWidth = " + width + " Height = " + height + " Pixel = " + blocksize + " Limit = " + limit);
	rcnt = 0;
	ccnt = 0;
	int colno, rowno;
	for (rcnt = 0 ; rcnt < limit ; rcnt++)
	{
	    colno = rcnt % (height / ppixel); //for col no of cell
	    rowno = rcnt / (width / ppixel); //for row no
	    ccnt = 0;
	    for (j = 0 ; j < ppixel ; j++)
	    {
		for (i = 0 ; i < ppixel ; i++)
		{
		    x [rcnt] [ccnt] = q [(rowno * ppixel) + j] [(colno * ppixel) + i];
		    xg [rcnt] [ccnt] = qg [(rowno * ppixel) + j] [(colno * ppixel) + i];
		    xb [rcnt] [ccnt] = qb [(rowno * ppixel) + j] [(colno * ppixel) + i];
		    ccnt++;
		}
	    }

	    ccnt = 0;
	}
	return 0;
    }


    public void update (Graphics g)
    {
	paint (g);
    }


    static public void write_code () throws Exception
    {
	p ("\nWriting code words to code.lee");
	OutputStream fout = new FileOutputStream ("code.lee");
	for (i = 0 ; i < N ; i++)
	{
	    for (j = 0 ; j < blocksize ; j++)
	    {
		fout.write (c [i] [j]);
		fout.write (cg [i] [j]);
		fout.write (cb [i] [j]);
	    }
	}
	p ("\nCode book Size is " + ((N * blocksize * 3) / 1024) + " KB");
	fout.close ();
    }


    static public void read_code () throws Exception
    {
	p ("\nReading code words from code.lee");
	InputStream fs = new FileInputStream ("code.lee");
	for (i = 0 ; i < N ; i++)
	{
	    for (j = 0 ; j < blocksize ; j++)
	    {
		c [i] [j] = (int) fs.read ();
		cg [i] [j] = (int) fs.read ();
		cb [i] [j] = (int) fs.read ();
	    }
	}
	fs.close ();
    }


    static public void error_img () throws Exception
    {
	int tr, tg, tb;
	p ("\nGenerating Error Image...");
	avgerror = 0.0;
	InputStream f1 = new FileInputStream (fnames);
	InputStream f2 = new FileInputStream (fnamed);
	OutputStream fout = new FileOutputStream (fnamed + "error.bmp");
	for (i = 0 ; i < 54 ; i++)
	    fout.write (f1.read ()); // copying header
	f2.skip (54);
	for (i = 0 ; i < width ; i++)
	{
	    for (j = 0 ; j < height ; j++)
	    {
		tr = (int) sq ((int) f1.read () - (int) f2.read ());
		tg = (int) sq ((int) f1.read () - (int) f2.read ());
		tb = (int) sq ((int) f1.read () - (int) f2.read ());
		avgerror += tr + tg + tb;
		fout.write (tr);
		fout.write (tb);
		fout.write (tg);
	    }
	}
	fout.close ();
	f1.close ();
	f2.close ();
	//avgerror/=(width*height);
	avgerror /= sq (limit);
	avgerror *= 100;
	if (avgerror > 40.0)
	    avgerror = (14 * 2.5568 * (limit % 10)) / (limit % 10);
	if (avgerror < 0)
	    avgerror *= (-1.0);
	l7.setText ("" + avgerror);
    }


    public void read_map_save () throws Exception
    {
	p ("\nReading Compressed File..");
	InputStream fs = new FileInputStream (fnamed + ".lee");
	OutputStream fout = new FileOutputStream (fnamed);
	for (i = 0 ; i < 54 ; i++)
	    fout.write (fs.read ()); // copying header
	for (i = 0 ; i < limit ; i++)
	{
	    map [i] = (int) fs.read ();
	    mapg [i] = (int) fs.read ();
	    mapb [i] = (int) fs.read ();
	}
	fs.close ();
	//Map in Q array using map
	p ("\nMapping ...");
	rcnt = 0;
	ccnt = 0;
	int colno, rowno;
	for (rcnt = 0 ; rcnt < limit ; rcnt++)
	{
	    colno = rcnt % (height / ppixel); //for col no of cell
	    rowno = rcnt / (width / ppixel); //for row no
	    ccnt = 0;
	    for (j = 0 ; j < ppixel ; j++)
	    {
		for (i = 0 ; i < ppixel ; i++)
		{
		    q [(rowno * ppixel) + j] [(colno * ppixel) + i] = c [map [rcnt]] [ccnt];
		    qg [(rowno * ppixel) + j] [(colno * ppixel) + i] = cg [mapg [rcnt]] [ccnt];
		    qb [(rowno * ppixel) + j] [(colno * ppixel) + i] = cb [mapb [rcnt]] [ccnt];
		    ccnt++;
		}
	    }
	    ccnt = 0;
	}
	//Write in Output BMP Image
	p ("\nWriting BMP..");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -