`

解析BMP格式文件,并实现重绘

    博客分类:
  • JAVA
阅读更多


                    我们看到一张图片,无论是什么格式的,都是用图片浏览器打开的,强大的浏览器

可以解析各种格式图片。例如jpg,bmp,gif,png,等等,这里我们讲一下解析较为简单的BMP格式,首先准备一张bmp格式的图片,


 

然后UltraEdit软件解析,出来会是这个样子



 

接下来对照下表解析以上字节意思,注意以上都是16进制,学会使用计算器进行转换成10进制:

BMP文件格式分析 

简介

BMP(Bitmap-File)图形文件是Windows采用的图形文件格式,在Windows环境下运行的所有图象处理软件都支持BMP图象文件格式。Windows系统内部各图像绘制操作都是以BMP为基础的。Windows 3.0以前的BMP图文件格式与显示设备有关,因此把这种BMP图象文件格式称为设备相关位图DDB(device-dependent bitmap)文件格式。Windows 3.0以后的BMP图象文件与显示设备无关,因此把这种BMP图象文件格式称为设备无关位图DIB(device-independent bitmap)格式(注:Windows 3.0以后,在系统中仍然存在DDB位图,象BitBlt()这种函数就是基于DDB位图的,只不过如果你想将图像以BMP格式保存到磁盘文件中时,微软极力推荐你以DIB格式保存),目的是为了让Windows能够在任何类型的显示设备上显示所存储的图象。BMP位图文件默认的文件扩展名是BMP或者bmp(有时它也会以.DIB.RLE作扩展名)。

文件结构

位图文件可看成由4个部分组成:位图文件头(bitmap-file header)、位图信息头(bitmap-information header)、彩色表(color table)和定义位图的字节阵列,它具有如下所示的形式。

位图文件的组成 

结构名称 

符号 

位图文件头(bitmap-file header)

BITMAPFILEHEADER

bmfh

位图信息头(bitmap-information header)

BITMAPINFOHEADER

bmih

彩色表(color table)

RGBQUAD

aColors[]

图象数据阵列字节

BYTE

aBitmapBits[]

位图文件结构可综合在表6-01中。

01 位图文件结构内容摘要

 

偏移量 

域的名称 

大小 

内容 

  

  

  

图象文件 

0000h

文件标识

2 bytes

两字节的内容用来识别位图的类型: 

‘BM’ : Windows 3.1x, 95, NT, … 

‘BA’ OS/2 Bitmap Array 

‘CI’ OS/2 Color Icon 

‘CP’ OS/2 Color Pointer 

‘IC’ : OS/2 Icon 

‘PT’ OS/2 Pointer

注:因为OS/2系统并没有被普及开,所以在编程时,你只需判断第一个标识“BM”就行。

 

0002h

File Size

1 dword

用字节表示的整个文件的大小

 

0006h

Reserved

1 dword

保留,必须设置为0

 

000Ah

Bitmap Data Offset

1 dword

从文件开始到位图数据开始之间的数据(bitmap data)之间的偏移量

 

000Eh

Bitmap Header Size

1 dword

位图信息头(Bitmap Info Header)的长度,用来描述位图的颜色、压缩方法等。下面的长度表示: 

28h - Windows 3.1x, 95, NT, … 

0Ch - OS/2 1.x 

F0h - OS/2 2.x

注:在Windows95982000等操作系统中,位图信息头的长度并不一定是28h,因为微软已经制定出了新的BMP文件格式,其中的信息头结构变化比较大,长度加长。所以最好不要直接使用常数28h,而是应该从具体的文件中读取这个值。这样才能确保程序的兼容性。

 

0012h

Width

1 dword

位图的宽度,以象素为单位

 

0016h

Height

1 dword

位图的高度,以象素为单位

 

001Ah

Planes

1 word

位图的位面数(注:该值将总是1


图象 

信息 

 

  

 

001Ch

Bits Per Pixel

1 word

每个象素的位数 

1 - 单色位图(实际上可有两种颜色,缺省情况下是黑色和白色。你可以自己定义这两种颜色) 

4 - 16 色位图 

8 - 256 色位图 

16 - 16bit 高彩色位图 

24 - 24bit 真彩色位图 

32 - 32bit 增强型真彩色位图

 

001Eh

Compression

1 dword

压缩说明: 

0 - 不压缩 (使用BI_RGB表示) 

1 - RLE 8-使用8RLE压缩方式(BI_RLE8表示) 

2 - RLE 4-使用4RLE压缩方式(BI_RLE4表示) 

3 - Bitfields-位域存放方式(BI_BITFIELDS表示)

 

0022h

Bitmap Data Size

1 dword

用字节数表示的位图数据的大小。该数必须是4的倍数

 

0026h

HResolution

1 dword

用象素/米表示的水平分辨率

 

002Ah

VResolution

1 dword

用象素/米表示的垂直分辨率

 

002Eh

Colors

1 dword

位图使用的颜色数。如8-比特/象素表示为100h或者 256.

 

0032h

Important Colors

1 dword

指定重要的颜色数。当该域的值等于颜色数时(或者等于0时),表示所有颜色都一样重要

调色板数据

根据BMP版本的不同而不同

Palette

N * 4 byte

调色板规范。对于调色板中的每个表项,这4个字节用下述方法来描述RGB的值: 

1字节用于蓝色分量

1字节用于绿色分量

1字节用于红色分量

1字节用于填充符(设置为0)

 

<!--EndFragment-->
 于是根据以上,编写代码:

package BMP;

import java.awt.Graphics;

import java.io.DataInputStream;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFrame;


public class BMPRead extends Thread{

	Graphics g;
	int skip_width;
	public static void main(String[] args) throws IOException
	{   JFrame jf=new JFrame();
	    jf.setTitle("图片预览器");
	    jf.setSize(600,600);
	    jf.setVisible(true);
		BMPRead bmp=new BMPRead();
		while(true){
			try {
				bmp.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			bmp.read("image//五子棋.bmp",jf);
		}
	}
	
	public void read(String filename,JFrame jf) throws IOException
	{
		try {
			FileInputStream fis=new FileInputStream(filename);
			DataInputStream dis=new DataInputStream(fis);
			
			byte n=dis.readByte();
			System.out.print((char)n);
		     n=dis.readByte();
		     System.out.println((char)n);
		     
		     
		     int n1=readInt(dis);
		     System.out.println("len="+n1);//图片大小
		     dis.readInt();
		     
		     int n2=readInt(dis);//从文件信息头到位图数据开始的偏移量
		     System.out.println(n2);
		     int n3=readInt(dis);//文件信息头的长度
             System.out.println("offset="+n3);
             int width=readInt(dis);//宽度
             System.out.println("width="+width);
             int heigh=readInt(dis);//高度
             System.out.println("height="+heigh);
             dis.skipBytes(28);//跳过28字节,就直接到获取颜色,详情请看表
			 this.showRED(dis, heigh, width,jf);
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//改写读取整型的方法
	public int  readInt(DataInputStream dis) throws IOException
	{
		 int ch1 =dis.read();
	        int ch2 = dis.read();
	        int ch3 = dis.read();
	        int ch4 = dis.read();
	        if ((ch1 | ch2 | ch3 | ch4) < 0)
	            throw new EOFException();
	        return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0));
	}
	public void showRED(DataInputStream dis,int heigh,int width,JFrame jf) throws IOException{
		
		g=jf.getGraphics();
		if(!(width*3%4==0)){
			 skip_width=4-width*3%4;
		}
		
		int [][] imageR=new int[heigh][width];
		int [][] imageG=new int[heigh][width];
		int [][] imageB=new int[heigh][width];
		for(int h=heigh-1;h>0;h--){
			for(int w=0;w<width;w++){
				//读入三原色
				int blue=dis.read();
				int green=dis.read();
				int red=dis.read();
				imageB[h][w]=blue;
				imageG[h][w]=green;
				imageR[h][w]=red;
				if(w==0){
					System.out.println(dis.skipBytes(skip_width));
				}
				
				
			}
		}
		//描点,一个像素一个像素去描
		for(int h=0;h<heigh;h++){
			for(int w=0;w<width;w++){
				g.setColor(new java.awt.Color(imageR[h][w],imageG[h][w], imageB[h][w]));
				g.fillOval(w, h, 1, 1);
			}
		}
		
		//repaint();
	
		
	}
	
	
	
}

 出来解析后的图形如下所示:



 
 

 

  • 大小: 100.6 KB
  • 大小: 17.5 KB
  • 大小: 103.5 KB
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics