📄 imagedata.java
字号:
if ((theByte & mask) == 0) { pixels[i] = 0; } else { pixels[i] = 1; } i++; n--; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; if (n > 0) theByte = data[index] & 0xFF; srcX = 0; } else { if (mask == 1) { index++; if (n > 0) theByte = data[index] & 0xFF; } } } return; } if (depth == 2) { index = (y * bytesPerLine) + (x >> 2); theByte = data[index] & 0xFF; int offset; while (n > 0) { offset = 3 - (srcX % 4); mask = 3 << (offset * 2); pixels[i] = (byte)((theByte & mask) >> (offset * 2)); i++; n--; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; if (n > 0) theByte = data[index] & 0xFF; srcX = 0; } else { if (offset == 0) { index++; theByte = data[index] & 0xFF; } } } return; } if (depth == 4) { index = (y * bytesPerLine) + (x >> 1); if ((x & 0x1) == 1) { theByte = data[index] & 0xFF; pixels[i] = theByte & 0x0F; i++; n--; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; srcX = 0; } else { index++; } } while (n > 1) { theByte = data[index] & 0xFF; pixels[i] = theByte >> 4; i++; n--; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; srcX = 0; } else { pixels[i] = theByte & 0x0F; i++; n--; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; srcX = 0; } else { index++; } } } if (n > 0) { theByte = data[index] & 0xFF; pixels[i] = theByte >> 4; } return; } if (depth == 8) { index = (y * bytesPerLine) + x; for (int j = 0; j < getWidth; j++) { pixels[i] = data[index] & 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 < getWidth; j++) { pixels[i] = ((data[index+1] & 0xFF) << 8) + (data[index] & 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 < getWidth; j++) { pixels[i] = ((data[index] & 0xFF) << 16) | ((data[index+1] & 0xFF) << 8) | (data[index+2] & 0xFF); i++; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; srcX = 0; } else { index += 3; } } return; } if (depth == 32) { index = (y * bytesPerLine) + (x * 4); i = startIndex; for (int j = 0; j < getWidth; j++) { pixels[i] = ((data[index] & 0xFF) << 24) | ((data[index+1] & 0xFF) << 16) | ((data[index+2] & 0xFF) << 8) | (data[index+3] & 0xFF); i++; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; srcX = 0; } else { index += 4; } } return; } SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);}/** * Returns an array of <code>RGB</code>s which comprise the * indexed color table of the receiver, or null if the receiver * has a direct color model. * * @return the RGB values for the image or null if direct color * * @see PaletteData#getRGBs() */public RGB[] getRGBs() { return palette.getRGBs();}/** * Returns an <code>ImageData</code> which specifies the * transparency mask information for the receiver, or null if the * receiver has no transparency and is not an icon. * * @return the transparency mask or null if none exists */public ImageData getTransparencyMask() { if (getTransparencyType() == SWT.TRANSPARENCY_MASK) { return new ImageData(width, height, 1, bwPalette(), maskPad, maskData); } else { return colorMaskImage(transparentPixel); }}/** * Returns the image transparency type. * * @return the receiver's transparency type */public int getTransparencyType() { if (maskData != null) return SWT.TRANSPARENCY_MASK; if (transparentPixel != -1) return SWT.TRANSPARENCY_PIXEL; if (alphaData != null) return SWT.TRANSPARENCY_ALPHA; return SWT.TRANSPARENCY_NONE;}/** * Returns the byte order of the receiver. * * @return MSB_FIRST or LSB_FIRST */int getByteOrder() { return depth != 16 ? MSB_FIRST : LSB_FIRST;}/** * Returns a copy of the receiver which has been stretched or * shrunk to the specified size. If either the width or height * is negative, the resulting image will be inverted in the * associated axis. * * @param width the width of the new ImageData * @param height the height of the new ImageData * @return a scaled copy of the image */public ImageData scaledTo(int width, int height) { /* Create a destination image with no data */ final boolean flipX = (width < 0); if (flipX) width = - width; final boolean flipY = (height < 0); if (flipY) height = - height; ImageData dest = new ImageData( width, height, depth, palette, scanlinePad, null, 0, null, null, -1, transparentPixel, type, x, y, disposalMethod, delayTime); /* Scale the image contents */ if (palette.isDirect) blit(BLIT_SRC, this.data, this.depth, this.bytesPerLine, this.getByteOrder(), 0, 0, this.width, this.height, 0, 0, 0, ALPHA_OPAQUE, null, 0, 0, 0, dest.data, dest.depth, dest.bytesPerLine, dest.getByteOrder(), 0, 0, dest.width, dest.height, 0, 0, 0, flipX, flipY); else blit(BLIT_SRC, this.data, this.depth, this.bytesPerLine, this.getByteOrder(), 0, 0, this.width, this.height, null, null, null, ALPHA_OPAQUE, null, 0, 0, 0, dest.data, dest.depth, dest.bytesPerLine, dest.getByteOrder(), 0, 0, dest.width, dest.height, null, null, null, flipX, flipY); /* Scale the image mask or alpha */ if (maskData != null) { dest.maskPad = this.maskPad; int destBpl = (dest.width + 7) / 8; destBpl = (destBpl + (dest.maskPad - 1)) / dest.maskPad * dest.maskPad; dest.maskData = new byte[destBpl * dest.height]; int srcBpl = (this.width + 7) / 8; srcBpl = (srcBpl + (this.maskPad - 1)) / this.maskPad * this.maskPad; blit(BLIT_SRC, this.maskData, 1, srcBpl, MSB_FIRST, 0, 0, this.width, this.height, null, null, null, ALPHA_OPAQUE, null, 0, 0, 0, dest.maskData, 1, destBpl, MSB_FIRST, 0, 0, dest.width, dest.height, null, null, null, flipX, flipY); } else if (alpha != -1) { dest.alpha = this.alpha; } else if (alphaData != null) { dest.alphaData = new byte[dest.width * dest.height]; blit(BLIT_SRC, this.alphaData, 8, this.width, MSB_FIRST, 0, 0, this.width, this.height, null, null, null, ALPHA_OPAQUE, null, 0, 0, 0, dest.alphaData, 8, dest.width, MSB_FIRST, 0, 0, dest.width, dest.height, null, null, null, flipX, flipY); } return dest;}/** * Sets the alpha value at offset <code>x</code> in * scanline <code>y</code> in the receiver's alpha data. * * @param x the x coordinate of the alpha value to set * @param y the y coordinate of the alpha value to set * @param alpha the value to set the alpha to * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> * </ul> */public void setAlpha(int x, int y, int alpha) { if (x >= width || y >= height || x < 0 || y < 0 || alpha < 0 || alpha > 255) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (alphaData == null) alphaData = new byte[width * height]; alphaData[y * width + x] = (byte)alpha; }/** * Sets the alpha values starting at offset <code>x</code> in * scanline <code>y</code> in the receiver's alpha data to the * values from the array <code>alphas</code> starting at * <code>startIndex</code>. * * @param x the x coordinate of the pixel to being setting the alpha values * @param y the y coordinate of the pixel to being setting the alpha values * @param putWidth the width of the alpha values to set * @param alphas the alpha values 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> */public void setAlphas(int x, int y, int putWidth, byte[] alphas, int startIndex) { if (alphas == 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; if (alphaData == null) alphaData = new byte[width * height]; // may throw an IndexOutOfBoundsException System.arraycopy(alphas, startIndex, alphaData, y * width + x, putWidth);}/** * Sets the pixel value at offset <code>x</code> in * scanline <code>y</code> in the receiver's data. * * @param x the x coordinate of the pixel to set * @param y the y coordinate of the pixel to set * @param pixelValue the value to set the pixel to * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</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 setPixel(int x, int y, int pixelValue) { if (x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); int index; byte theByte; int mask; if (depth == 1) { index = (y * bytesPerLine) + (x >> 3); theByte = data[index]; mask = 1 << (7 - (x & 0x7)); if ((pixelValue & 0x1) == 1) { data[index] = (byte)(theByte | mask); } else { data[index] = (byte)(theByte & (mask ^ -1)); } return; } if (depth == 2) { index = (y * bytesPerLine) + (x >> 2); theByte = data[index]; int offset = 3 - (x % 4); mask = 0xFF ^ (3 << (offset * 2)); data[index] = (byte)((data[index] & mask) | (pixelValue << (offset * 2))); return; } if (depth == 4) { index = (y * bytesPerLine) + (x >> 1); if ((x & 0x1) == 0) { data[index] = (byte)((data[index] & 0x0F) | ((pixelValue & 0x0F) << 4)); } else { data[index] = (byte)((data[index] & 0xF0) | (pixelValue & 0x0F)); } return; } if (depth == 8) { index = (y * bytesPerLine) + x ; data[index] = (byte)(pixelValue & 0xFF); return; } if (depth == 16) { index = (y * bytesPerLine) + (x * 2); data[index + 1] = (byte)((pixelValue >> 8) & 0xFF); data[index] = (byte)(pixelValue & 0xFF); return; } if (depth == 24) { index = (y * bytesPerLine) + (x * 3); data[index] = (byte)((pixelValue >> 16) & 0xFF); data[index + 1] = (byte)((pixelValue >> 8) & 0xFF); data[index + 2] = (byte)(pixelValue & 0xFF); return; } if (depth == 32) { index = (y * bytesPerLine) + (x * 4); data[index] = (byte)((pixelValue >> 24) & 0xFF); data[index + 1] = (byte)((pixelValue >> 16) & 0xFF); data[index + 2] = (byte)((pixelValue >> 8) & 0xFF); data[index + 3] = (byte)(pixelValue & 0xFF); 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 * (For higher depths, use the int[] version of this method.)</li> * </ul> */public void setPixels(int x, int y, int putWidth, byte[] 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -