📄 imagedata.java
字号:
* Initializes all fields in the receiver. This method must be called * by all public constructors to ensure that all fields are initialized * for a new ImageData object. If a new field is added to the class, * then it must be added to this method. * <p> * This method is for internal use, and is not described further. * </p> */void setAllFields(int width, int height, int depth, int scanlinePad, int bytesPerLine, byte[] data, PaletteData palette, int transparentPixel, byte[] maskData, int maskPad, byte[] alphaData, int alpha, int type, int x, int y, int disposalMethod, int delayTime) { this.width = width; this.height = height; this.depth = depth; this.scanlinePad = scanlinePad; this.bytesPerLine = bytesPerLine; this.data = data; this.palette = palette; this.transparentPixel = transparentPixel; this.maskData = maskData; this.maskPad = maskPad; this.alphaData = alphaData; this.alpha = alpha; this.type = type; this.x = x; this.y = y; this.disposalMethod = disposalMethod; this.delayTime = delayTime;}/** * Invokes internal SWT functionality to create a new instance of * this class. * <p> * <b>IMPORTANT:</b> This method is <em>not</em> part of the public * API for <code>ImageData</code>. It is marked public only so that it * can be shared within the packages provided by SWT. It is subject * to change without notice, and should never be called from * application code. * </p> * <p> * This method is for internal use, and is not described further. * </p> */public static ImageData internal_new( 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){ return new ImageData( width, height, depth, palette, scanlinePad, data, maskPad, maskData, alphaData, alpha, transparentPixel, type, x, y, disposalMethod, delayTime);}ImageData colorMaskImage(int pixel) { ImageData mask = new ImageData(width, height, 1, bwPalette(), 2, null, 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, 0, 0, 0, 0); int[] row = new int[width]; for (int y = 0; y < height; y++) { getPixels(0, y, width, row, 0); for (int i = 0; i < width; i++) { if (pixel != -1 && row[i] == pixel) { row[i] = 0; } else { row[i] = 1; } } mask.setPixels(0, y, width, row, 0); } return mask;}static byte[] checkData(byte [] data) { if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); return data;}/** * Returns a new instance of the same class as the receiver, * whose slots have been filled in with <em>copies</em> of * the values in the slots of the receiver. That is, the * returned object is a <em>deep copy</em> of the receiver. * * @return a copy of the receiver. */public Object clone() { byte[] cloneData = new byte[data.length]; System.arraycopy(data, 0, cloneData, 0, data.length); byte[] cloneMaskData = null; if (maskData != null) { cloneMaskData = new byte[maskData.length]; System.arraycopy(maskData, 0, cloneMaskData, 0, maskData.length); } byte[] cloneAlphaData = null; if (alphaData != null) { cloneAlphaData = new byte[alphaData.length]; System.arraycopy(alphaData, 0, cloneAlphaData, 0, alphaData.length); } return new ImageData( width, height, depth, palette, scanlinePad, cloneData, maskPad, cloneMaskData, cloneAlphaData, alpha, transparentPixel, type, x, y, disposalMethod, delayTime);}/** * Returns the alpha value at offset <code>x</code> in * scanline <code>y</code> in the receiver's alpha data. * * @param x the x coodinate of the pixel to get the alpha value of * @param y the y coordinate of the pixel to get the alpha value of * @return the alpha value at the given coordinates * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT - if either argument is out of range</li> * </ul> */public int getAlpha(int x, int y) { if (x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (alphaData == null) return 255; return alphaData[y * width + x] & 0xFF;}/** * Returns <code>getWidth</code> alpha values starting at offset * <code>x</code> in scanline <code>y</code> in the receiver's alpha * data starting at <code>startIndex</code>. * * @param x the x position of the pixel to begin getting alpha values * @param y the y position of the pixel to begin getting alpha values * @param getWidth the width of the data to get * @param alphas the buffer in which to put the alpha values * @param startIndex the offset into the image to begin getting alpha values * * @exception IndexOutOfBoundsException if getWidth 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 getWidth is negative</li> * </ul> */public void getAlphas(int x, int y, int getWidth, byte[] alphas, int startIndex) { if (alphas == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (getWidth == 0) return; if (alphaData == null) { int endIndex = startIndex + getWidth; for (int i = startIndex; i < endIndex; i++) { alphas[i] = (byte)255; } return; } // may throw an IndexOutOfBoundsException System.arraycopy(alphaData, y * width + x, alphas, startIndex, getWidth);}/** * Returns the pixel value at offset <code>x</code> in * scanline <code>y</code> in the receiver's data. * * @param x the x position of the pixel to get * @param y the y position of the pixel to get * @return the pixel at the given coordinates * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT - if either argument 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 int getPixel(int x, int y) { if (x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); int index; int theByte; int mask; if (depth == 1) { index = (y * bytesPerLine) + (x >> 3); theByte = data[index] & 0xFF; mask = 1 << (7 - (x & 0x7)); if ((theByte & mask) == 0) { return 0; } else { return 1; } } if (depth == 2) { index = (y * bytesPerLine) + (x >> 2); theByte = data[index] & 0xFF; int offset = 3 - (x % 4); mask = 3 << (offset * 2); return (theByte & mask) >> (offset * 2); } if (depth == 4) { index = (y * bytesPerLine) + (x >> 1); theByte = data[index] & 0xFF; if ((x & 0x1) == 0) { return theByte >> 4; } else { return theByte & 0x0F; } } if (depth == 8) { index = (y * bytesPerLine) + x ; return data[index] & 0xFF; } if (depth == 16) { index = (y * bytesPerLine) + (x * 2); return ((data[index+1] & 0xFF) << 8) + (data[index] & 0xFF); } if (depth == 24) { index = (y * bytesPerLine) + (x * 3); return ((data[index] & 0xFF) << 16) + ((data[index+1] & 0xFF) << 8) + (data[index+2] & 0xFF); } if (depth == 32) { index = (y * bytesPerLine) + (x * 4); return ((data[index] & 0xFF) << 24) + ((data[index+1] & 0xFF) << 16) + ((data[index+2] & 0xFF) << 8) + (data[index+3] & 0xFF); } SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); return 0;}/** * Returns <code>getWidth</code> pixel values starting at offset * <code>x</code> in scanline <code>y</code> in the receiver's * data starting at <code>startIndex</code>. * * @param x the x position of the first pixel to get * @param y the y position of the first pixel to get * @param getWidth the width of the data to get * @param pixels the buffer in which to put the pixels * @param startIndex the offset into the byte array to begin storing pixels * * @exception IndexOutOfBoundsException if getWidth 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 getWidth is negative</li> * </ul> * @exception SWTException <ul> * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4 or 8 * (For higher depths, use the int[] version of this method.)</li> * </ul> */public void getPixels(int x, int y, int getWidth, byte[] pixels, int startIndex) { if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (getWidth == 0) return; int index; int theByte; int mask = 0; int n = getWidth; int i = startIndex; int srcX = x, srcY = y; if (depth == 1) { index = (y * bytesPerLine) + (x >> 3); theByte = data[index] & 0xFF; while (n > 0) { mask = 1 << (7 - (srcX & 0x7)); 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] = (byte)(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] = (byte)(theByte >> 4); i++; n--; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; srcX = 0; } else { pixels[i] = (byte)(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] = (byte)(theByte >> 4); } return; } if (depth == 8) { index = (y * bytesPerLine) + x; for (int j = 0; j < getWidth; j++) { pixels[i] = data[index]; i++; srcX++; if (srcX >= width) { srcY++; index = srcY * bytesPerLine; srcX = 0; } else { index++; } } return; } SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);}/** * Returns <code>getWidth</code> pixel values starting at offset * <code>x</code> in scanline <code>y</code> in the receiver's * data starting at <code>startIndex</code>. * * @param x the x position of the first pixel to get * @param y the y position of the first pixel to get * @param getWidth the width of the data to get * @param pixels the buffer in which to put the pixels * @param startIndex the offset into the buffer to begin storing pixels * * @exception IndexOutOfBoundsException if getWidth 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 getWidth 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 getPixels(int x, int y, int getWidth, int[] pixels, int startIndex) { if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (getWidth == 0) return; int index; int theByte; int mask; int n = getWidth; int i = startIndex; int srcX = x, srcY = y; if (depth == 1) { index = (y * bytesPerLine) + (x >> 3); theByte = data[index] & 0xFF; while (n > 0) { mask = 1 << (7 - (srcX & 0x7));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -