/**
* Decodes and shows true color,2 color,16 color or 256 color windows 3.x
* BMP images,this class currently support true color,2 color uncompressed;
* 256 color and 16 color uncompressed as well as RLE compressed format.
*/
/**
* This software is provided "AS IS," without a warranty of any kind.
* anyone can use it for free,emails are welcomed concerning bugs or
* suggestions.
*/
/**
* BMPReader.java.
*
* @version 1.05 03/28/2008
* @author Wen Yu, yuwen_66@yahoo.com
*
* Changes:
* 1. colorPalette to use bitmapHeader.colorsUsed if it's nonzero
* instead of always relying on 1<<bitsPerPixel.
* 2. check the bitmapHeader.dataOffSet for possible bytes to skip
*/
package cafe.image.codecs.reader;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class BMPReader extends ImageReader
{
//biCompression types
private static final int BI_RGB = 0;//No compression
private static final int BI_RLE8 = 1;//8 bit RLE Compression (8 bit only)
private static final int BI_RLE4 = 2;//4 bit RLE Compression (4 bit only)
private static final int BI_BITFIELDS = 3;//No compression (16 & 32 bit only)
int bytePerScanLine = 0;
BitmapHeader bitmapHeader;
/**
* This method could have returned an Image object directly using
* Toolkit.getDefaultToolkit().createImage(ImageProducer) method,
* but it is somewhat slow when called in a non-gui class to create
* an image, a subclass of JFrame would do much faster!
*/
public void unpackImage(InputStream is) throws Exception
{
bitmapHeader = new BitmapHeader();
bitmapHeader.readHeader(is);
width = bitmapHeader.imageWidth;
height = bitmapHeader.imageHeight;
pix = new int[width*height];
bitsPerPixel = bitmapHeader.bitCount;
int bitPerWidth = width*bitsPerPixel;
if(bitPerWidth%32 == 0)//to make sure scan lines are padded out to even 4-byte boundaries.
{
bytePerScanLine = (bitPerWidth>>>3);
}
else
{
bytePerScanLine = (bitPerWidth>>>3)+(4-(bitPerWidth>>>3)%4);
//a different method to do the same thing as above!
//bytePerScanLine = (((bitPerWidth+31) & ~31 ) >> 3);
}
switch (bitmapHeader.bitCount)
{
case 1:
{
unpack2ColorBitmapFile(is);
break;
}
case 4:
{
if(bitmapHeader.compression == BI_RLE4)
unpack16ColorCompressedBitmapFile(is);
else unpack16ColorBitmapFile(is);
break;
}
case 8:
{
if(bitmapHeader.compression == BI_RLE8)
unpack256ColorCompressedBitmapFile(is);
else unpack256ColorBitmapFile(is);
break;
}
case 16:
{
System.out.println("16 bit BMP, decoding not implemented!");
System.exit(1);
//unpack16bitTrueColorBitmapFile(is);
//break;
}
case 24:
{
unpackTrueColorBitmapFile(is);
break;
}
case 32:
{
unpack32bitTrueColorBitmapFile(is);
break;
}
default:
{
System.out.println("Unsupported bitmap format!");
System.exit(1);
}
}
}
private void readPalette(InputStream is) throws Exception
{
int index = 0,nindex = 0;
int numOfColors = (bitmapHeader.colorsUsed == 0)?(1<<bitsPerPixel):bitmapHeader.colorsUsed;
byte brgb[] = new byte[numOfColors*4];
colorPalette = new int[numOfColors];
indexedColor = true;
is.read(brgb,0,numOfColors*4);
for(int i = 0; i < numOfColors; i++)
{
colorPalette[index++] = ((0xff<<24)|(brgb[nindex]&0xff)|((brgb[nindex+1]&0xff)<<8)|((brgb[nindex+2]&0xff)<<16));
nindex += 4;
}
//There may be some extra bits between color platte and actual image data
is.skip(bitmapHeader.dataOffSet - numOfColors*4 - 54);
}
private void unpackTrueColorBitmapFile(InputStream is) throws Exception
{
System.out.println("24 bits bitmap color image!");
int index = 0,nindex = 0;
int npad = bytePerScanLine-3*width;
byte brgb[] = new byte[bytePerScanLine];
is.skip(bitmapHeader.dataOffSet - 54);
for(int i = 1; i <= height; i++)
{
is.read(brgb,0,bytePerScanLine);
index = width*(height-i);
nindex = 0;
for(int j = 0; j < width; j++)
{
pix[index++] = ((0xff<<24)|(brgb[nindex++]&0xff)|((brgb[nindex++]&0xff)<<8)|((brgb[nindex++]&0xff)<<16));
}
nindex += npad;
}
is.close();
}
private void unpack32bitTrueColorBitmapFile(InputStream is) throws Exception
{
System.out.println("32 bits bitmap color image!");
int index = 0,nindex = 0;
byte brgb[] = new byte[bytePerScanLine];
is.skip(bitmapHeader.dataOffSet - 54);
for(int i = 1; i <= height; i++)
{
is.read(brgb,0,bytePerScanLine);
index = width*(height-i);
nindex = 0;
for(int j = 0; j < width; j++)
{
pix[index++] = ((brgb[nindex++]&0xff)|((brgb[nindex++]&0xff)<<8)|((brgb[nindex++]&0xff)<<16)|(0xff<<24));
nindex++;
}
}
is.close();
}
private void unpack256ColorBitmapFile(InputStream is) throws Exception
{
System.out.println("256 color bitmap color image!");
readPalette(is);
int index = 0, nindex = 0;
int npad = bytePerScanLine-width;
byte brgb[] = new byte[bytePerScanLine];
for(int i = 1; i <= height; i++)
{
is.read(brgb,0,bytePerScanLine);
index = width*(height-i);
nindex = 0;
for(int j = 0; j < width; j++)
{
pix[index++] = colorPalette[brgb[nindex++]&0xff];
}
nindex += npad;
}
is.close();
}
private void unpack16ColorBitmapFile(InputStream is) throws Exception
{
System.out.println("16 color bitmap color image!");
readPalette(is);
int bit = 0, index = 0, nindex = 0;
byte brgb[];
int npad = (32-((width*4)%32))/8;
if (npad == 4) npad = 0;
brgb = new byte[bytePerScanLine];
for(int i = 1; i <= height; i++)
{
is.read(brgb,0,bytePerScanLine);
index = width*(height-i);
nindex = 0;
for(int j = 0; j < width/2; j++)
{
bit = brgb[nindex++];
pix[index++] = colorPalette[((bit>>>4)&0x0F)];
pix[index++] = colorPalette[bit&(0x0F)];
}
if((width%2) != 0)
{
bit = brgb[nindex++];
pix[index++] = colorPalette[((bit>>>4)&0x0F)];
}
nindex += npad;
}
is.close();
}
private void unpack2ColorBitmapFile(InputStream is) throws Exception
{
System.out.println("2 color bitmap color image!");
readPalette(is);
int bit = 0, index = 0, nindex = 0;
byte brgb[];
brgb = new byte[bytePerScanLine];
int npad = (32-(width%32))/32;
if (npad == 4) npad = 0;
for(int i = 1; i <= height; i++)
{
is.read(brgb,0,bytePerScanLine);
index = width*(height-i);
nindex = 0;
for(int j = 0; j < width/8; j++)
{
bit = brgb[nindex++];
for (int k = 0; k < 8; k++)
{
pix[index++] = colorPalette[((bit>>>(7-k))&0x01)];
}
}
if((width%8) != 0)
{
bit = brgb[nindex++];
for (int k = 0; k < width%8; k++)
{
pix[index++] = colorPalette[((bit>>>(7-k))&0x01)];
}
}
nindex += npad;
}
is.close();
}
private void unpack256ColorCompressedBitmapFile(InputStream is) throws Exception
{
System.out.println("256 color bitmap color image!");
System.out.println("compressed format!");
readPalette(is);
int rgb_table_index = 0, index = 0, nindex = 0;
Java Windows Bitmap decoder and encoder
5星 · 超过95%的资源 需积分: 9 168 浏览量
2008-12-20
01:39:20
上传
评论
收藏 18KB ZIP 举报
dragon_66
- 粉丝: 1
- 资源: 13
最新资源
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
- 基于同态加密技术的匿名电子投票系统源码.zip
- Pyqt5项目框架-PyQt项目开发实践
- 基于C通过MQTT的智能农业大棚管理系统(本科毕业设计)
- python+CNN的网络入侵检测算法源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈