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

📄 winbmpfileformat.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html *  * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.internal.image;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;import java.io.*;final class WinBMPFileFormat extends FileFormat {	int importantColors;	Point pelsPerMeter = new Point(0, 0);	public static final int BMPHeaderFixedSize = 40;/** * Compress numBytes bytes of image data from src, storing in dest * (starting at 0), using the technique specified by comp. * If last is true, this indicates the last line of the image. * Answer the size of the compressed data. */int compress(int comp, byte[] src, int srcOffset, int numBytes, byte[] dest, boolean last) {	if (comp == 1) { // BMP_RLE8_COMPRESSION		return compressRLE8Data(src, srcOffset, numBytes, dest, last);	}	if (comp == 2) { // BMP_RLE4_COMPRESSION		return compressRLE4Data(src, srcOffset, numBytes, dest, last);	}	SWT.error(SWT.ERROR_INVALID_IMAGE);	return 0;}int compressRLE4Data(byte[] src, int srcOffset, int numBytes, byte[] dest, boolean last) {	int sp = srcOffset, end = srcOffset + numBytes, dp = 0;	int size = 0, left, i, n;	byte theByte;	while (sp < end) {		/* find two consecutive bytes that are the same in the next 128 */		left = end - sp - 1;		if (left > 127)			left = 127;		for (n = 0; n < left; n++) {			if (src[sp + n] == src[sp + n + 1])				break;		}		/* if there is only one more byte in the scan line, include it */		if (n < 127 && n == left)			n++;		/* store the intervening data */		switch (n) {			case 0:				break;			case 1: /* handled separately because 0,2 is a command */				dest[dp] = 2; dp++; /* 1 byte == 2 pixels */				dest[dp] = src[sp];				dp++; sp++;				size += 2;				break;			default:				dest[dp] = 0; dp++;				dest[dp] = (byte)(n + n); dp++; /* n bytes = n*2 pixels */				for (i = n; i > 0; i--) {					dest[dp] = src[sp];					dp++; sp++;				}				size += 2 + n;				if ((n & 1) != 0) { /* pad to word */					dest[dp] = 0;					dp++;					size++;				}				break;		}		/* find the length of the next run (up to 127) and store it */		left = end - sp;		if (left > 0) {			if (left > 127)				left = 127;			theByte = src[sp];			for (n = 1; n < left; n++) {				if (src[sp + n] != theByte)					break;			}			dest[dp] = (byte)(n + n); dp++; /* n bytes = n*2 pixels */			dest[dp] = theByte; dp++;			sp += n;			size += 2;		}	}	/* store the end of line or end of bitmap codes */	dest[dp] = 0; dp++;	if (last) {		dest[dp] = 1; dp++;	} else {		dest[dp] = 0; dp++;	}	size += 2;		return size;}int compressRLE8Data(byte[] src, int srcOffset, int numBytes, byte[] dest, boolean last) {	int sp = srcOffset, end = srcOffset + numBytes, dp = 0;	int size = 0, left, i, n;	byte theByte;	while (sp < end) {		/* find two consecutive bytes that are the same in the next 256 */		left = end - sp - 1;		if (left > 254)			left = 254;		for (n = 0; n < left; n++) {			if (src[sp + n] == src[sp + n + 1])				break;		}		/* if there is only one more byte in the scan line, include it */		if (n == left)			n++;		/* store the intervening data */		switch (n) {			case 0:				break;			case 2: /* handled separately because 0,2 is a command */				dest[dp] = 1; dp++;				dest[dp] = src[sp];				dp++; sp++;				size += 2;				/* don't break, fall through */			case 1: /* handled separately because 0,1 is a command */				dest[dp] = 1; dp++;				dest[dp] = src[sp];				dp++; sp++;				size += 2;				break;			default:				dest[dp] = 0; dp++;				dest[dp] = (byte)n; dp++;				for (i = n; i > 0; i--) {					dest[dp] = src[sp];					dp++; sp++;				}				size += 2 + n;				if ((n & 1) != 0) { /* pad to word */					dest[dp] = 0;					dp++;					size++;				}				break;		}		/* find the length of the next run (up to 255) and store it */		left = end - sp;		if (left > 0) {			if (left > 255)				left = 255;			theByte = src[sp];			for (n = 1; n < left; n++) {				if (src[sp + n] != theByte)					break;			}			dest[dp] = (byte)n; dp++;			dest[dp] = theByte; dp++;			sp += n;			size += 2;		}	}	/* store the end of line or end of bitmap codes */	dest[dp] = 0; dp++;	if (last) {		dest[dp] = 1; dp++;	} else {		dest[dp] = 0; dp++;	}	size += 2;		return size;}void decompressData(byte[] src, byte[] dest, int stride, int cmp) {	if (cmp == 1) { // BMP_RLE8_COMPRESSION		if (decompressRLE8Data(src, src.length, stride, dest, dest.length) <= 0)			SWT.error(SWT.ERROR_INVALID_IMAGE);		return;	}	if (cmp == 2) { // BMP_RLE4_COMPRESSION		if (decompressRLE4Data(src, src.length, stride, dest, dest.length) <= 0)			SWT.error(SWT.ERROR_INVALID_IMAGE);		return;	}	SWT.error(SWT.ERROR_INVALID_IMAGE);}int decompressRLE4Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) {	int sp = 0;	int se = numBytes;	int dp = 0;	int de = destSize;	int x = 0, y = 0;	while (sp < se) {		int len = src[sp] & 0xFF;		sp++;		if (len == 0) {			len = src[sp] & 0xFF;			sp++;			switch (len) {				case 0: /* end of line */					y++;					x = 0;					dp = y * stride;					if (dp >= de)						return -1;					break;				case 1: /* end of bitmap */					return 1;				case 2: /* delta */					x += src[sp] & 0xFF;					sp++;					y += src[sp] & 0xFF;					sp++;					dp = y * stride + x / 2;					if (dp >= de)						return -1;					break;				default: /* absolute mode run */					if ((len & 1) != 0) /* odd run lengths not currently supported */						return -1;					x += len;					len = len / 2;					if (len > (se - sp))						return -1;					if (len > (de - dp))						return -1;					for (int i = 0; i < len; i++) {						dest[dp] = src[sp];						dp++;						sp++;					}					if ((sp & 1) != 0)						sp++; /* word align sp? */					break;			}		} else {			if ((len & 1) != 0)				return -1;			x += len;			len = len / 2;			byte theByte = src[sp];			sp++;			if (len > (de - dp))				return -1;			for (int i = 0; i < len; i++) {				dest[dp] = theByte;				dp++;			}		}	}	return 1;}int decompressRLE8Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) {	int sp = 0;	int se = numBytes;	int dp = 0;	int de = destSize;	int x = 0, y = 0;	while (sp < se) {		int len = src[sp] & 0xFF;		sp++;		if (len == 0) {			len = src[sp] & 0xFF;			sp++;			switch (len) {				case 0: /* end of line */					y++;					x = 0;					dp = y * stride;					if (dp >= de)						return -1;					break;				case 1: /* end of bitmap */					return 1;				case 2: /* delta */					x += src[sp] & 0xFF;					sp++;					y += src[sp] & 0xFF;					sp++;					dp = y * stride + x;					if (dp >= de)						return -1;					break;				default: /* absolute mode run */					if (len > (se - sp))						return -1;					if (len > (de - dp))						return -1;					for (int i = 0; i < len; i++) {						dest[dp] = src[sp];						dp++;						sp++;					}					if ((sp & 1) != 0)						sp++; /* word align sp? */					x += len;					break;			}		} else {			byte theByte = src[sp];			sp++;			if (len > (de - dp))				return -1;			for (int i = 0; i < len; i++) {				dest[dp] = theByte;				dp++;			}			x += len;		}	}	return 1;}boolean isFileFormat(LEDataInputStream stream) {	try {		byte[] header = new byte[2];		stream.read(header);		stream.unread(header);		return header[0] == 0x42 && header[1] == 0x4D;	} catch (Exception e) {		return false;	}}byte[] loadData(byte[] infoHeader) {	int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24);	int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24);	int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8);

⌨️ 快捷键说明

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