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

📄 imagedata.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	int theByte;	int mask;	int n = putWidth;	int i = startIndex;	int srcX = x, srcY = y;	if (depth == 1) {		index = (y * bytesPerLine) + (x >> 3);		while (n > 0) {			mask = 1 << (7 - (srcX & 0x7));			if ((pixels[i] & 0x1) == 1) {				data[index] = (byte)((data[index] & 0xFF) | mask);			} else {				data[index] = (byte)((data[index] & 0xFF) & (mask ^ -1));			}			i++;			n--;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				srcX = 0;			} else {				if (mask == 1) {					index++;				}			}		}		return;	}	if (depth == 2) {		byte [] masks = { (byte)0xFC, (byte)0xF3, (byte)0xCF, (byte)0x3F };		index = (y * bytesPerLine) + (x >> 2);		int offset = 3 - (x % 4);		while (n > 0) {			theByte = pixels[i] & 0x3;			data[index] = (byte)((data[index] & masks[offset]) | (theByte << (offset * 2)));			i++;			n--;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				offset = 0;				srcX = 0;			} else {				if (offset == 0) {					index++;					offset = 3;				} else {					offset--;				}			}		}		return;	}	if (depth == 4) {		index = (y * bytesPerLine) + (x >> 1);		boolean high = (x & 0x1) == 0;		while (n > 0) {			theByte = pixels[i] & 0x0F;			if (high) {				data[index] = (byte)((data[index] & 0x0F) | (theByte << 4));			} else {				data[index] = (byte)((data[index] & 0xF0) | theByte);			}			i++;			n--;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				high = true;				srcX = 0;			} else {				if (!high) index++;				high = !high;			}		}		return;	}	if (depth == 8) {		index = (y * bytesPerLine) + x;		for (int j = 0; j < putWidth; j++) {			data[index] = (byte)(pixels[i] & 0xFF);			i++;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				srcX = 0;			} else {				index++;			}		}		return;	}	SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);}/** * Sets the pixel values starting at offset <code>x</code> in * scanline <code>y</code> in the receiver's data to the * values from the array <code>pixels</code> starting at * <code>startIndex</code>. * * @param x the x position of the pixel to set * @param y the y position of the pixel to set * @param putWidth the width of the pixels to set * @param pixels the pixels to set * @param startIndex the index at which to begin setting * * @exception IndexOutOfBoundsException if putWidth is too large * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if pixels is null</li> *    <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> *    <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li> * </ul> */public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex) {	if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);	if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);	if (putWidth == 0) return;	int index;	int theByte;	int mask;	int n = putWidth;	int i = startIndex;	int pixel;	int srcX = x, srcY = y;	if (depth == 1) {		index = (y * bytesPerLine) + (x >> 3);		while (n > 0) {			mask = 1 << (7 - (srcX & 0x7));			if ((pixels[i] & 0x1) == 1) {				data[index] = (byte)((data[index] & 0xFF) | mask);			} else {				data[index] = (byte)((data[index] & 0xFF) & (mask ^ -1));			}			i++;			n--;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				srcX = 0;			} else {				if (mask == 1) {					index++;				}			}		}		return;	}	if (depth == 2) {		byte [] masks = { (byte)0xFC, (byte)0xF3, (byte)0xCF, (byte)0x3F };		index = (y * bytesPerLine) + (x >> 2);		int offset = 3 - (x % 4);		while (n > 0) {			theByte = pixels[i] & 0x3;			data[index] = (byte)((data[index] & masks[offset]) | (theByte << (offset * 2)));			i++;			n--;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				offset = 3;				srcX = 0;			} else {				if (offset == 0) {					index++;					offset = 3;				} else {					offset--;				}			}		}		return;	}	if (depth == 4) {		index = (y * bytesPerLine) + (x >> 1);		boolean high = (x & 0x1) == 0;		while (n > 0) {			theByte = pixels[i] & 0x0F;			if (high) {				data[index] = (byte)((data[index] & 0x0F) | (theByte << 4));			} else {				data[index] = (byte)((data[index] & 0xF0) | theByte);			}			i++;			n--;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				high = true;				srcX = 0;			} else {				if (!high) index++;				high = !high;			}		}		return;	}	if (depth == 8) {		index = (y * bytesPerLine) + x;		for (int j = 0; j < putWidth; j++) {			data[index] = (byte)(pixels[i] & 0xFF);			i++;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				srcX = 0;			} else {				index++;			}		}		return;	}	if (depth == 16) {		index = (y * bytesPerLine) + (x * 2);		for (int j = 0; j < putWidth; j++) {			pixel = pixels[i];			data[index] = (byte)(pixel & 0xFF);			data[index + 1] = (byte)((pixel >> 8) & 0xFF);			i++;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				srcX = 0;			} else {				index += 2;			}		}		return;	}	if (depth == 24) {		index = (y * bytesPerLine) + (x * 3);		for (int j = 0; j < putWidth; j++) {			pixel = pixels[i];			data[index] = (byte)((pixel >> 16) & 0xFF);			data[index + 1] = (byte)((pixel >> 8) & 0xFF);			data[index + 2] = (byte)(pixel & 0xFF);			i++;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				srcX = 0;			} else {				index += 3;			}		}		return;	}	if (depth == 32) {		index = (y * bytesPerLine) + (x * 4);		for (int j = 0; j < putWidth; j++) {			pixel = pixels[i];			data[index] = (byte)((pixel >> 24) & 0xFF);			data[index + 1] = (byte)((pixel >> 16) & 0xFF);			data[index + 2] = (byte)((pixel >> 8) & 0xFF);			data[index + 3] = (byte)(pixel & 0xFF);			i++;			srcX++;			if (srcX >= width) {				srcY++;				index = srcY * bytesPerLine;				srcX = 0;			} else {				index += 4;			}		}		return;	}	SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);}/** * Returns a palette with 2 colors: black & white. */static PaletteData bwPalette() {	return new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255, 255, 255)});}/** * Gets the offset of the most significant bit for * the given mask. */static int getMSBOffset(int mask) {	for (int i = 31; i >= 0; i--) {		if (((mask >> i) & 0x1) != 0) return i + 1;	}	return 0;}/** * Finds the closest match. */static int closestMatch(int depth, byte red, byte green, byte blue, int redMask, int greenMask, int blueMask, byte[] reds, byte[] greens, byte[] blues) {	if (depth > 8) {		int rshift = 32 - getMSBOffset(redMask);		int gshift = 32 - getMSBOffset(greenMask);		int bshift = 32 - getMSBOffset(blueMask);		return (((red << 24) >>> rshift) & redMask) |			(((green << 24) >>> gshift) & greenMask) |			(((blue << 24) >>> bshift) & blueMask);	}	int r, g, b;	int minDistance = 0x7fffffff;	int nearestPixel = 0;	int n = reds.length;	for (int j = 0; j < n; j++) {		r = (reds[j] & 0xFF) - (red & 0xFF);		g = (greens[j] & 0xFF) - (green & 0xFF);		b = (blues[j] & 0xFF) - (blue & 0xFF);		int distance = r*r + g*g + b*b;		if (distance < minDistance) {			nearestPixel = j;			if (distance == 0) break;			minDistance = distance;		}	}	return nearestPixel;}static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) {	if (pad == newPad) return data;	int stride = (width * depth + 7) / 8;	int bpl = (stride + (pad - 1)) / pad * pad;		int newBpl = (stride + (newPad - 1)) / newPad * newPad;	byte[] newData = new byte[height * newBpl];	int srcIndex = 0, destIndex = 0;	for (int y = 0; y < height; y++) {		System.arraycopy(data, srcIndex, newData, destIndex, stride);		srcIndex += bpl;		destIndex += newBpl;	}	return newData;}/** * Blit operation bits to be OR'ed together to specify the desired operation. */static final int	BLIT_SRC = 1,     // copy source directly, else applies logic operations	BLIT_ALPHA = 2,   // enable alpha blending	BLIT_DITHER = 4;  // enable dithering in low color modes/** * Alpha mode, values 0 - 255 specify global alpha level */static final int	ALPHA_OPAQUE = 255,           // Fully opaque (ignores any alpha data)	ALPHA_TRANSPARENT = 0,        // Fully transparent (ignores any alpha data)	ALPHA_CHANNEL_SEPARATE = -1,  // Use alpha channel from separate alphaData	ALPHA_CHANNEL_SOURCE = -2,    // Use alpha channel embedded in sourceData	ALPHA_MASK_UNPACKED = -3,     // Use transparency mask formed by bytes in alphaData (non-zero is opaque)	ALPHA_MASK_PACKED = -4,       // Use transparency mask formed by packed bits in alphaData	ALPHA_MASK_INDEX = -5,        // Consider source palette indices transparent if in alphaData array	ALPHA_MASK_RGB = -6;          // Consider source RGBs transparent if in RGB888 format alphaData array/** * Byte and bit order constants. */static final int LSB_FIRST = 0;static final int MSB_FIRST = 1;/** * Data types (internal) */private static final int	// direct / true color formats with arbitrary masks & shifts	TYPE_GENERIC_8 = 0,	TYPE_GENERIC_16_MSB = 1,	TYPE_GENERIC_16_LSB = 2,	TYPE_GENERIC_24 = 3,	TYPE_GENERIC_32_MSB = 4,	TYPE_GENERIC_32_LSB = 5,	// palette indexed color formats	TYPE_INDEX_8 = 6,	TYPE_INDEX_4 = 7,	TYPE_INDEX_2 = 8,	TYPE_INDEX_1_MSB = 9,	TYPE_INDEX_1_LSB = 10;/** * Blits a direct palette image into a direct palette image. * <p> * Note: When the source and destination depth, order and masks * are pairwise equal and the blitter operation is BLIT_SRC, * the masks are ignored.  Hence when not changing the image * data format, 0 may be specified for the masks. * </p> *  * @param op the blitter operation: a combination of BLIT_xxx flags *        (see BLIT_xxx constants) * @param srcData the source byte array containing image data * @param srcDepth the source depth: one of 8, 16, 24, 32 * @param srcStride the source number of bytes per line * @param srcOrder the source byte ordering: one of MSB_FIRST or LSB_FIRST; *        ignored if srcDepth is not 16 or 32 * @param srcX the top-left x-coord of the source blit region * @param srcY the top-left y-coord of the source blit region * @param srcWidth the width of the source blit region * @param srcHeight the height of the source blit region * @param srcRedMask the source red channel mask * @param srcGreenMask the source green channel mask * @param srcBlueMask the source blue channel mask * @param alphaMode the alpha blending or mask mode, may be *        an integer 0-255 for global alpha; ignored if BLIT_ALPHA *        not specified in the blitter operations *        (see ALPHA_MODE_xxx constants) * @param alphaData the alpha blending or mask data, varies depending *        on the value of alphaMode and sometimes ignored * @param alphaStride the alpha data number of bytes per line * @param alphaX the top-left x-coord of the alpha blit region * @param alphaY the top-left y-coord of the alpha blit region * @param destData the destination byte array containing image data * @param destDepth the destination depth: one of 8, 16, 24, 32 * @param destStride the destination number of bytes per line

⌨️ 快捷键说明

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