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

📄 imagedata.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************* * 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.graphics;import java.io.*;import org.eclipse.swt.*;import org.eclipse.swt.internal.CloneableCompatibility;/** * Instances of this class are device-independent descriptions * of images. They are typically used as an intermediate format * between loading from or writing to streams and creating an  * <code>Image</code>. * <p> * Note that the public fields <code>x</code>, <code>y</code>,  * <code>disposalMethod</code> and <code>delayTime</code> are * typically only used when the image is in a set of images used * for animation. * </p> * * @see Image * @see ImageLoader */public final class ImageData implements CloneableCompatibility {		/**	 * The width of the image, in pixels.	 */	public int width;	/**	 * The height of the image, in pixels.	 */	public int height;	/**	 * The color depth of the image, in bits per pixel.	 * <p>	 * Note that a depth of 8 or less does not necessarily	 * mean that the image is palette indexed, or	 * conversely that a depth greater than 8 means that	 * the image is direct color.  Check the associated	 * PaletteData's isDirect field for such determinations.	 */	public int depth;	/**	 * The scanline padding.	 * <p>	 * If one scanline of the image is not a multiple of	 * this number, it will be padded with zeros until it is.	 * </p>	 */	public int scanlinePad;	/**	 * The number of bytes per scanline.	 * <p>	 * This is a multiple of the scanline padding.	 * </p>	 */	public int bytesPerLine;	/**	 * The pixel data of the image.	 * <p>	 * Note that for 16 bit depth images the pixel data is stored	 * in least significant byte order; however, for 24bit and	 * 32bit depth images the pixel data is stored in most	 * significant byte order.	 * </p>	 */	public byte[] data;	/**	 * The color table for the image.	 */	public PaletteData palette;	/**	 * The transparent pixel.	 * <p>	 * Pixels with this value are transparent.	 * </p><p>	 * The default is -1 which means 'no transparent pixel'.	 * </p>	 */	public int transparentPixel;	/**	 * An icon-specific field containing the data from the icon mask.	 * <p>	 * This is a 1 bit bitmap stored with the most significant	 * bit first.  The number of bytes per scanline is	 * '((width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad'.	 * </p><p>	 * The default is null which means 'no transparency mask'.	 * </p>	 */	public byte[] maskData;	/**	 * An icon-specific field containing the scanline pad of the mask.	 * <p>	 * If one scanline of the transparency mask is not a	 * multiple of this number, it will be padded with zeros until	 * it is.	 * </p>	 */	public int maskPad;		/**	 * The alpha data of the image.	 * <p>	 * Every pixel can have an <em>alpha blending</em> value that	 * varies from 0, meaning fully transparent, to 255 meaning	 * fully opaque.  The number of bytes per scanline is	 * 'width'.	 * </p>	 */	public byte[] alphaData;		/**	 * The global alpha value to be used for every pixel.	 * <p>	 * If this value is set, the <code>alphaData</code> field	 * is ignored and when the image is rendered each pixel	 * will be blended with the background an amount	 * proportional to this value.	 * </p><p>	 * The default is -1 which means 'no global alpha value'	 * </p>	 */	public int alpha;	/**	 * The type of file from which the image was read.	 * 	 * It is expressed as one of the following values:	 * <dl>	 * <dt><code>IMAGE_BMP</code></dt>	 * <dd>Windows BMP file format, no compression</dd>	 * <dt><code>IMAGE_BMP_RLE</code></dt>	 * <dd>Windows BMP file format, RLE compression if appropriate</dd>	 * <dt><code>IMAGE_GIF</code></dt>	 * <dd>GIF file format</dd>	 * <dt><code>IMAGE_ICO</code></dt>	 * <dd>Windows ICO file format</dd>	 * <dt><code>IMAGE_JPEG</code></dt>	 * <dd>JPEG file format</dd>	 * <dt><code>IMAGE_PNG</code></dt>	 * <dd>PNG file format</dd>	 * </dl>	 */	public int type;	/**	 * The x coordinate of the top left corner of the image	 * within the logical screen (this field corresponds to	 * the GIF89a Image Left Position value).	 */	public int x;	/**	 * The y coordinate of the top left corner of the image	 * within the logical screen (this field corresponds to	 * the GIF89a Image Top Position value).	 */	public int y;	/**	 * A description of how to dispose of the current image	 * before displaying the next.	 * 	 * It is expressed as one of the following values:	 * <dl>	 * <dt><code>DM_UNSPECIFIED</code></dt>	 * <dd>disposal method not specified</dd>	 * <dt><code>DM_FILL_NONE</code></dt>	 * <dd>do nothing - leave the image in place</dd>	 * <dt><code>DM_FILL_BACKGROUND</code></dt>	 * <dd>fill with the background color</dd>	 * <dt><code>DM_FILL_PREVIOUS</code></dt>	 * <dd>restore the previous picture</dd>	 * </dl>	 * (this field corresponds to the GIF89a Disposal Method value)	 */	public int disposalMethod;	/**	 * The time to delay before displaying the next image	 * in an animation (this field corresponds to the GIF89a	 * Delay Time value).	 */	public int delayTime;	/**	 * Arbitrary channel width data to 8-bit conversion table.	 */	static final byte[][] ANY_TO_EIGHT = new byte[9][];	static {		for (int b = 0; b < 9; ++b) {			byte[] data = ANY_TO_EIGHT[b] = new byte[1 << b];			if (b == 0) continue;			int inc = 0;			for (int bit = 0x10000; (bit >>= b) != 0;) inc |= bit;			for (int v = 0, p = 0; v < 0x10000; v+= inc) data[p++] = (byte)(v >> 8);		}	}	static final byte[] ONE_TO_ONE_MAPPING = ANY_TO_EIGHT[8];	/**	 * Scaled 8x8 Bayer dither matrix.	 */	static final int[][] DITHER_MATRIX = {		{ 0xfc0000, 0x7c0000, 0xdc0000, 0x5c0000, 0xf40000, 0x740000, 0xd40000, 0x540000 },		{ 0x3c0000, 0xbc0000, 0x1c0000, 0x9c0000, 0x340000, 0xb40000, 0x140000, 0x940000 },		{ 0xcc0000, 0x4c0000, 0xec0000, 0x6c0000, 0xc40000, 0x440000, 0xe40000, 0x640000 },		{ 0x0c0000, 0x8c0000, 0x2c0000, 0xac0000, 0x040000, 0x840000, 0x240000, 0xa40000 },		{ 0xf00000, 0x700000, 0xd00000, 0x500000, 0xf80000, 0x780000, 0xd80000, 0x580000 },		{ 0x300000, 0xb00000, 0x100000, 0x900000, 0x380000, 0xb80000, 0x180000, 0x980000 },		{ 0xc00000, 0x400000, 0xe00000, 0x600000, 0xc80000, 0x480000, 0xe80000, 0x680000 },		{ 0x000000, 0x800000, 0x200000, 0xa00000, 0x080000, 0x880000, 0x280000, 0xa80000 }	};/** * Constructs a new, empty ImageData with the given width, height, * depth and palette. The data will be initialized to an (all zero) * array of the appropriate size. * * @param width the width of the image * @param height the height of the image * @param depth the depth of the image * @param palette the palette of the image (must not be null) * * @exception IllegalArgumentException <ul> *    <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not *        	one of 1, 2, 4, 8, 16, 24 or 32</li> *    <li>ERROR_NULL_ARGUMENT - if the palette is null</li> * </ul> */public ImageData(int width, int height, int depth, PaletteData palette) {	this(width, height, depth, palette,		4, null, 0, null,		null, -1, -1, SWT.IMAGE_UNDEFINED,		0, 0, 0, 0);}/** * Constructs a new, empty ImageData with the given width, height, * depth, palette, scanlinePad and data. * * @param width the width of the image * @param height the height of the image * @param depth the depth of the image * @param palette the palette of the image * @param scanlinePad the padding of each line, in bytes * @param data the data of the image * * @exception IllegalArgumentException <ul> *    <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not *        	one of 1, 2, 4, 8, 16, 24 or 32</li> *    <li>ERROR_NULL_ARGUMENT - if the palette or data is null</li> *    <li>ERROR_CANNOT_BE_ZERO - if the scanlinePad is zero</li> * </ul> */public ImageData(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data) {	this(width, height, depth, palette,		scanlinePad, checkData(data), 0, null,		null, -1, -1, SWT.IMAGE_UNDEFINED,		0, 0, 0, 0);}/** * Constructs an <code>ImageData</code> loaded from the specified * input stream. Throws an error if an error occurs while loading * the image, or if the image has an unsupported type. * <p> * This constructor is provided for convenience when loading a single * image only. If the stream contains multiple images, only the first * one will be loaded. To load multiple images, use  * <code>ImageLoader.load()</code>. * </p> * * @param stream the input stream to load the image from (must not be null) * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the stream is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> *    <li>ERROR_IO - if an IO error occurs while reading data</li> * </ul> * * @see ImageLoader#load(InputStream) */public ImageData(InputStream stream) {	ImageData[] data = new ImageLoader().load(stream);	if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);	ImageData i = data[0];	setAllFields(		i.width,		i.height,		i.depth,		i.scanlinePad,		i.bytesPerLine,		i.data,		i.palette,		i.transparentPixel,		i.maskData,		i.maskPad,		i.alphaData,		i.alpha,		i.type,		i.x,		i.y,		i.disposalMethod,		i.delayTime);}/** * Constructs an <code>ImageData</code> loaded from a file with the * specified name. Throws an error if an error occurs loading the * image, or if the image has an unsupported type. * <p> * This constructor is provided for convenience when loading a single * image only. If the file contains multiple images, only the first * one will be loaded. To load multiple images, use  * <code>ImageLoader.load()</code>. * </p> * * @param filename the name of the file to load the image from (must not be null) * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the file name is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> *    <li>ERROR_IO if an IO error occurs while reading data</li> * </ul> */public ImageData(String filename) {	ImageData[] data = new ImageLoader().load(filename);	if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);	ImageData i = data[0];	setAllFields(		i.width,		i.height,		i.depth,		i.scanlinePad,		i.bytesPerLine,		i.data,		i.palette,		i.transparentPixel,		i.maskData,		i.maskPad,		i.alphaData,		i.alpha,		i.type,		i.x,		i.y,		i.disposalMethod,		i.delayTime);}/** * Prevents uninitialized instances from being created outside the package. */ImageData() {}/** * Constructs an image data by giving values for all non-computable fields. * <p> * This method is for internal use, and is not described further. * </p> */ImageData(	int width, int height, int depth, PaletteData palette,	int scanlinePad, byte[] data, int maskPad, byte[] maskData,	byte[] alphaData, int alpha, int transparentPixel, int type,	int x, int y, int disposalMethod, int delayTime){	if (palette == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);	if (!(depth == 1 || depth == 2 || depth == 4 || depth == 8		|| depth == 16 || depth == 24 || depth == 32)) {		SWT.error(SWT.ERROR_INVALID_ARGUMENT);	}	if (width <= 0 || height <= 0) {		SWT.error(SWT.ERROR_INVALID_ARGUMENT);	}	if (scanlinePad == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);	int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1))		/ scanlinePad * scanlinePad;	setAllFields(		width,		height,		depth,		scanlinePad,		bytesPerLine,		data != null ? data : new byte[bytesPerLine * height],		palette,		transparentPixel,		maskData,		maskPad,		alphaData,		alpha,		type,		x,		y,		disposalMethod,		delayTime);}/**

⌨️ 快捷键说明

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