本文主要是介绍Tiff文件解析和PackBits解压缩,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现了Tiff图片文件格式的解析,对Tiff文件中的PackBits压缩格式进行解压缩,对Tiff文件中每一个Frame转换成BufferedImage显示。
Java语言实现,Eclipse下开发,AWT显示图片。
public static TIFF Parse(final byte[] bytes) throws IllegalAccessException {BinaryBuffer is = new BinaryBuffer(bytes);TIFF tiff = new TIFF();FileHeader header = new FileHeader();header.setByteOrder( is.ReadAsciiString(2));if (header.getByteOrder().equals("II"))is.SetByteOrder(ByteOrder.LITTLE_ENDIAN);else if (header.getByteOrder().equals( "MM"))is.SetByteOrder(ByteOrder.BIG_ENDIAN);elsethrow new java.lang.IllegalAccessException(header.getByteOrder());header.setLabel( is.ReadBytes(2));header.setOffset( is.ReadUInt32());tiff.setHeader(header);int position = (int) header.getOffset(); while (position != 0 ) {is.SetPosition(position);FileDirectory ifd = new FileDirectory();ifd.setEntryCount( is.ReadUInt16() );for (int i=0; i<ifd.getEntryCount(); i++) {Entry entry = new Entry();entry.setTag( is.ReadUInt16() );entry.setType( is.ReadUInt16() );entry.setCount( is.ReadInt32() );entry.setValue( is.ReadInt32() );if (entry.getType() == 1) { // 8-bit unsigned integerint count = 1 * entry.getCount();if (count <=4)entry.setData(new byte[] { });else {is.SavePoint();is.SetPosition(entry.getValue());entry.setData( is.ReadAsciiChar(entry.getCount()));is.RestorePoint();}}else if (entry.getType() == 2) { // 8-bit, NULL-terminated stringint count = 1 * entry.getCount();if (count <=4)entry.setData(new char[] { });else {is.SavePoint();is.SetPosition(entry.getValue());entry.setData( is.ReadAsciiChar(entry.getCount()));is.RestorePoint();}}else if (entry.getType() == 3) { // 16-bit unsigned integerint count = 2 * entry.getCount();if (count <= 4) {entry.setData( entry.getValue());}else {is.SavePoint();is.SetPosition(entry.getValue());entry.setData( is.ReadUInt16(entry.getCount()));is.RestorePoint();}}else if (entry.getType() == 4) { // 32-bit unsigned integerint count = 4 * entry.getCount();if (count <= 4 )entry.setData(entry.getValue());else {is.SavePoint();is.SetPosition(entry.getValue());entry.setData( is.ReadUInt32(entry.getCount()));is.RestorePoint();}}else if (entry.getType() == 5) { // Two 32-bit unsigned integersis.SavePoint();is.SetPosition(entry.getValue());entry.setData( 1.0 * is.ReadUInt32() / is.ReadUInt32() );is.RestorePoint();}else if (entry.getType() == 6) { // 8-bit signed integerthrow new java.lang.UnsupportedOperationException();}else if (entry.getType() == 7) { // 8-bit byteint count = 1 * entry.getCount();if (count <=4)entry.setData(new byte[] { });else {is.SavePoint();is.SetPosition(entry.getValue());entry.setData( is.ReadAsciiChar(entry.getCount()));is.RestorePoint();}}else if (entry.getType() == 9) { // 32-bit signed integerint count = 4 * entry.getCount();if (count <= 4 )entry.setData(entry.getValue());else {is.SavePoint();is.SetPosition(entry.getValue());entry.setData( is.ReadInt32(entry.getCount()));is.RestorePoint();}}else if (entry.getType() == 10) { // Two 32-bit signed integersentry.setData( 1.0 * is.ReadInt32() / is.ReadInt32() );}else if (entry.getType() == 11) { // 4-byte single-precision IEEE throw new java.lang.UnsupportedOperationException();}else if (entry.getType() == 12) { // 8-byte double-precision IEEE throw new java.lang.UnsupportedOperationException();}else throw new java.lang.IllegalArgumentException(entry.getType() + "");ifd.getEntrys().add(entry);}ifd.setNext( is.ReadInt32());tiff.getDirectorys().add(ifd);position = ifd.getNext();}return tiff;}
解析Tiff文件第1个Frame的结构:
PackBits解压缩代码:
public static byte[] uncompress(byte[] input) {ByteArrayOutputStream os = new ByteArrayOutputStream();int i = 0;do {byte n = input[i++];if (n < 0 && n > -128) {byte b = input[i++];for (int j=0; j<-n+1; j++)os.write(b);}else if (n >= 0 && n<=127) {int count = n + 1;for (int j=0; j<count; j++)os.write(input[i++]);}} while (i<input.length);return os.toByteArray();}
在AWT显示Tiff图像中32个Frame图像,采用GridLayout布局,效果如下
这篇关于Tiff文件解析和PackBits解压缩的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!