?? omraster.java
字號(hào):
* the pixel array. * * @param x x location of pixel, from the left side of image. * @param y y location of pixel, from the top of image. * @return true if location within pixel array. */ private boolean boundsSafe(int x, int y) { if ((y < 0) || (y >= height) || (x < 0) || (x >= width)) { return false; } return true; } /** * Set the ImageIcon. * * @param img ImageIcon */ public void setImageIcon(ImageIcon img) { setImage(img.getImage()); } /** * Set the image pixel value at a location. * * @param x Horizontal location of pixel from left. * @param y Vertical location of pixel from top. * @param colorValue the color value of the pixel. * @return true if x, y location valid. */ public boolean setPixel(int x, int y, int colorValue) { if (boundsSafe(x, y)) { pixels[(y * width) + x] = colorValue; setNeedToRegenerate(true); return true; } return false; //fail } /** * Get the image pixel value at a locaiton. * * @param x Horizontal location of pixel from left. * @param y Vertical location of pixel from top. * @return the integer color value of the image at x, y */ public int getPixel(int x, int y) { if (boundsSafe(x, y)) { return pixels[(y * width) + x]; } return 0; //fail - but also the ct[0] - hmmmmm. } /** * Set image byte data, for index frame using colortable. * * @param x Horizontal location of pixel from left. * @param y Vertical location of pixel from top. * @param ctIndex The array index of the applicable color in the * color table. * @return true if x, y location valid. */ public boolean setByte(int x, int y, byte ctIndex) { if (boundsSafe(x, y) && bits != null) { bits[(y * width) + x] = ctIndex; setNeedToRegenerate(true); return true; } return false; //fail } /** * Get image byte data, which the index to a colortable for * indexed images. * * @param x Horizontal location of pixel from left. * @param y Vertical location of pixel from top. * @return byte value of bytes(x, y) */ public byte getByte(int x, int y) { if (boundsSafe(x, y) && bits != null) { return bits[(y * width) + x]; } return 0; //fail - but also the ct[0] - hmmmmm. } /** * Set the bytes used to create the pixels used to create the * image. Checks to see of the length matches the height * width, * but doesn't do anything if they don't match, except print out a * warning. Make sure it does. * * @param values byte values containing bit pixel values. */ public void setBits(byte[] values) { super.setBits(values); if ((values.length) != (height * width)) Debug.output("OMBitmap: new byte[] size (" + +values.length + ") doesn't" + " match [height*width (" + height * width + ")]"); } /** * Set the transparency of the index type images. For the Direct * Colormodel the pixel data needs to be reconstructed, so this is * an O(pixels.length) operation. For an indexed colormodel, the * data still needs to be reconstructed, but it will cost you the * time in generate(). The transparency value should be a number * between 0-255. * * @param value New value of the alpha value for the image. */ public void setTransparent(int value) { value &= 0x000000ff; if (transparent == value) return; transparent = value; setNeedToRegenerate(true); if (bits != null) { pixels = null; computePixels(); } else { value <<= 24;//move to alpha position // direct color model, touch each pixel in the image for (int i = 0; i < pixels.length; i++) { // Do this if we want to support images that have // transparent pixels, and we want each pixel to have // the most transparent pixel. // Why don't we want to do this??? DFD // int pixAlpha = 0xFF000000 & pixels[i]; // pixAlpha = (pixAlpha < value)?pixAlpha:value; // pixels[i] = (0x00ffffff & pixels[i]) | pixAlpha; pixels[i] = (0x00ffffff & pixels[i]) | value; } } } /** * Get the transparent setting of the image. * * @return the transparent value (0-255) of the image. */ public int getTransparent() { return transparent; /** * Set the color table to the int RGB values passed in. Valid for * the indexed colormodel only. The pixels will be colored * according to these values. * * @param values array of color RGB values. */ public void setColors(int[] values) { if (colorModel != COLORMODEL_INDEXED) { Debug.output("OMRaster: Setting colors for final " + "colortable when a colortable isn't needed!"); } else { colors = values; setNeedToRegenerate(true); } } /** * Set the color table according to the java.awt.Color array * passed in. Valid for the indexed colormodel only. The pixels * will be colored according to these values. The transparency * values of these colors will only take effect of they are less * than the transparency value of the images' value. * * @param values array of java.awt.Color colors. */ public void setColors(Color[] values) { if (colorModel != COLORMODEL_INDEXED) { Debug.output("OMRaster: Setting colors for final colortable when a colortable isn't needed!"); return; } else if (values == null || values.length == 0) { colors = new int[0]; Debug.output("OMRaster: What are you trying to do to me?!? The colortables gots to have values!"); return; } else { if (values.length > 0) { colors = new int[values.length]; boolean allTransparent = true; int trans = (transparent << 24) & 0xff000000; // Turn the color table into a table using the // default OMava color model. for (int i = 0; i < values.length; i++) { // The transparent field can be set for the whole // image, while the open part of the colortable // entry // structure is the transparent setting for that // particular color. if (transparent < 255) { int argb = values[i].getRGB(); if (values[i].getAlpha() > transparent) { // If the transparent value of the pixel // is // lower than the transparency value, keep // that instead - don't make things more // visible then they were. colors[i] = (0x00ffffff & argb) | trans; } else { colors[i] = argb; } } else { colors[i] = values[i].getRGB(); } // Just check if all the colors are transparent - // this is a pain to figure out if you are // getting colors from some server that doesn't // know about alpha values. if (allTransparent && ((colors[i] >>> 24) != 0)) { allTransparent = false; } } if (DEBUG && allTransparent) { Debug.output("OMRaster: **Whasamatta?** Image created with all transparent pixels!"); } } //This is wrong - we do need to force a computePixels, // but in generate... // computePixels(); // This will do it.... pixels = null; setNeedToRegenerate(true); } } /** * image is not a indexed colormodel, the int[] will be null. * * @return color int[] if index colormodel, null otherwise. */ public int[] getColors() { return colors; } ///////////////////////////////////////////////////////// /** * Compute pixels is the function that resolves the color table * into pixel integer values used in the Image. It uses the bits * as indexes into the color table, and builds a big array of ints * to use in the bitmap image. If the bits are null, then the * object was created in the direct color model where the colors * are already built into the pixels. SO, if you call this, the * pixels have to be null and the bits good indexes into the * colortable. * * @return true if the image is OK to draw after this function. */ protected boolean computePixels() { if (DEBUG) Debug.output("OMRaster.compute pixels!"); int i; if (colorModel != COLORMODEL_INDEXED) { return true; } if (colors == null || colors.length == 0) { Debug.error("OMRaster: attempting to compute pixels without color table!"); return false; } int nPixels = width * height; if (DEBUG) { Debug.output("Computing pixels for image size:" + width + ", " + height); } // pixels are the image pixels pixels = new int[nPixels]; // Now, using the new constructed color table, build a set of // pixels. // alpha is a ready, shifted version of the overall // transparency value; int alpha = (transparent << 24) & 0xff000000; // numColors is the number of colors. int numColors = colors.length; for (i = 0; i < nPixels; i++) { byte b = bits[i]; int color; // make the alpha for this color the lessor of what the // colortable is, versus the transparent value // int pixAlpha; try { if (b >= numColors) { if (DEBUG) Debug.output("OMRaster:.computePixels() problem!: " + b); color = clear.getRGB(); } else if (b < 0) { color = colors[MoreMath.signedToInt(b)]; } else { color = colors[b]; } } catch (ArrayIndexOutOfBoundsException aiiobe) { // If the color can't be found, don't paint it. if (DEBUG) { Debug.output("OMRaster.computePixels() problem, can't find color for index: " + aiiobe.getMessage()); } color = clear.getRGB(); } // OK, got an int value, argb, for the color to be put on // the pixel. Now we need to straighten out the // transparency. if (transparent < 255 && ((color >> 24) > transparent)) { // this means that the overall transparency should be // more (lower number, more transparent) than the // pixel color. color = alpha | (0x00FFFFFF & color); } // Otherwise, just go with the alpha value set on the // color... pixels[i] = color; } return true; } /** * Prepare the graphics for rendering. For all image types, it * positions the image relative to the projection. For direct and * indexed colormodel images, it creates the ImageIcon used for * drawing to the window (internal to object). For indexed * colormodel images, it also calls computePixels, to resolve the * colortable and the bytes to create the image pixels. * * @param proj Projection used to position the image on the * window. * @return true if the image is ready to paint. */ public boolean generate(Projection proj) { // Position sets the position for the OMRaster!!!! if (!position(proj)) { if (DEBUG) { Debug.error("OMRaster.generate(): positioning failed!"); } return false; } // We used to just return here if the OMRaster didn't need to // be regenerated, but that didn't create the shape properly. if (getNeedToRegenerate()) { if (colorModel != COLORMODEL_IMAGEICON) { // This section is for the indexed color model rasters // that // need to resolve the colormap to the bit array // indexs. boolean allsWell = true; // If pixels == null, then computePixels has not been // called if (pixels == null) { allsWell = false; if (bits != null) allsWell = computePixels(); if (!allsWell) { Debug.output("OMRaster: attempted to generate without pixels defined!"); return false; } // Debug.output("OMRaster.generate: length(pixels) // = " + // pixels.length); } bitmap = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); ((BufferedImage) bitmap).setRGB(0, 0, width, height, pixels, 0, width); } // REPLACING bitmap with the filtered version - keep a // copy // yourself if you need the original!!! i.e. for // COLORMODEL_IMAGEICON if (imageFilter != null) { bitmap = filterImage(); } } // generate shape that is a boundary of the generated image. // We'll make it a GeneralPath rectangle. setShape(); setNeedToRegenerate(false); return true; }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -