📄 omrasterobject.java
字号:
public int getWidth() { return width; } /** * Get width of image, after a filter is applied. * * @return filteredWidth of image in pixels. */ public int getFilteredWidth() { return filteredWidth; } /** * Set the bytes used to create the pixels used to create the * image. Used for indexed color model images in OMRaster, and * OMBitmaps. * * @param values byte values */ public void setBits(byte[] values) { setNeedToRegenerate(true); bits = values; } /** * Get the byte values for indexed color model images and * OMBitmaps. * * @return the bytes used to create the pixels. */ public byte[] getBits() { return bits; } /** * Set a filter to be used on the constructed image. Applied at * generate(). * * @param filter Image filter to apply to contructed raster. */ public void setImageFilter(ImageFilter filter) { imageFilter = filter; filteredWidth = width; filteredHeight = height; setNeedToRegenerate(true); } /** * Return the image filter used on the image. * * @return imagefilter, null if one wasn't set. */ public ImageFilter getImageFilter() { return imageFilter; } /** * Convenience function to scale the Image to the xy size. Sets * the imageFilter to a ReplicateScaleFilter or * AreaAveragingScaleFilter, depending on the algorithm type. * * @param w width to scale to, in pixels * @param h height to scale to, in pixels * @param algorithmType OMRasterObject parameter describing which * scaling algorithm to use. */ public void scaleTo(int w, int h, int algorithmType) { filteredWidth = w; filteredHeight = h; imageFilter = new TrimScaleFilter(filteredWidth, filteredHeight, algorithmType); setNeedToRegenerate(true); } /** * A method used to manipulate the image according to the * parameters set by the imageFilter in the OMRasterObject. Called * from generate() if the filteredWidth and filteredHeight differ * from width and height. * * @return the filtered image. */ protected Image filterImage() { // Can we do a little clipping here?? If it's been projected, // maybe. // See if the frame is getting blown up, probably by at // least a certain margin, so we know that there will be time // savings as well as memory savings. if (imageFilter instanceof TrimScaleFilter) { TrimScaleFilter tf = (TrimScaleFilter) imageFilter; Image img = tf.trimExcessPixels(); if (img != null) { bitmap = img; imageFilter = tf.getFilterWithChanges(); // we can play around with point1, since that is where // the // image is getting laid out. If point1.x or point1.y // < 0, we // can set it to zero. Assumes that the image has // already // been positioned. if (point1.x < 0) point1.x = 0; if (point1.y < 0) point1.y = 0; if (DEBUG) { Debug.output("OMRasterObject: newly located at " + point1); } } else if (DEBUG) { Debug.output("OMRasterObject: not being trimmed due to projection"); } } if (Toolkit.getDefaultToolkit() != null && bitmap != null) { ImageProducer prod = new FilteredImageSource(bitmap.getSource(), imageFilter); return Toolkit.getDefaultToolkit().createImage(prod); } else return bitmap; } /** * From the Image Observer Interface. Called when the image bits * have arrived, and therefore calls setImage() to reset all the * OMRasterObject parameters. <br> * Don't call this method! */ public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & ImageObserver.ALLBITS) != 0) { if (colorModel == COLORMODEL_IMAGEICON) { setImage(img); } return false;// all set } return true;// need more info } /** * This is an effort to create an scaling ImageFilter that will * trim off the unused pixels, lessoning the load on the display * server. It depends on knowing several things about the * projection and the current image parameters, which is why it's * not a stand-alone filter class. */ protected class TrimScaleFilter extends AreaAveragingScaleFilter { ImageFilter actualFilter = null; int algorithmType; /** * Constructs an TrimScaleFilter that scales the pixels from * its source Image as specified by the width and height * parameters. * * @param width the target width to scale the image * @param height the target height to scale the image */ public TrimScaleFilter(int width, int height) { super(width, height); algorithmType = FAST_SCALING; } /** * Constructs an AreaAveragingScaleFilter that scales the * pixels from its source Image as specified by the width and * height parameters. * * @param width the target width to scale the image * @param height the target height to scale the image * @param algorithmType FAST_SCALING or SMOOTH_SCALING - FAST * is much faster! */ protected TrimScaleFilter(int width, int height, int algorithmType) { super(width, height); this.algorithmType = algorithmType; } /** * Detect if the data is being delivered with the necessary * hints to allow the averaging algorithm to do its work. If * the algorithmType is set to FAST, I manipulate the hints to * force the filter to act like a ReplicateScaleFilter. * * @see ImageConsumer#setHints */ public void setHints(int hints) { int passthrough = hints; if (algorithmType == FAST_SCALING) { /// XOR passthrough = hints ^ ImageConsumer.TOPDOWNLEFTRIGHT; } super.setHints(passthrough); } /** * The filter must change if the requested image size changes * because of clipping. Get the good filter here, after * calling trimExcessPixels(). */ protected ImageFilter getFilterWithChanges() { if (actualFilter == null) { return this; } return actualFilter; } /** * Get a trimmed-down image to expand to the map, that * contains all the pixels that will be visible after * expansion. Returns null if the image should be used as is, * and the filter as well. */ protected Image trimExcessPixels() { if (filteredWidth <= width && filteredHeight <= height) { if (DEBUG) { Debug.output("TrimScaleFilter.trimExcessPixels(): image not enlarged, using entire image."); } return null; } if (DEBUG) { Debug.output("TrimScaleFilter.trimExcessPixels(): clipping enlarged image."); } // Figure out the pixels of the old image being used in // the new image. Figure out the proj location of the // upper // left pixel of the new image. We want to subsitute this // proj location for the projection location already // calculated. This should get overwritten later for any // projection changes. float widthScale = (float) filteredWidth / (float) width; float heightScale = (float) filteredHeight / (float) height; int startXPixelInSource = point1.x < 0 ? (int) ((-1.0 * point1.x) / widthScale) : 0; int startYPixelInSource = point1.y < 0 ? (int) ((-1 * point1.y) / heightScale) : 0; Point scaledDim = new Point((int) (point1.x + (width * widthScale)), (int) (point1.y + (height * heightScale))); int endXPixelInSource = (scaledDim.x > projWidth ? (int) ((projWidth - point1.x) / widthScale) + 1 : width); int endYPixelInSource = scaledDim.y > projHeight ? (int) ((projHeight - point1.y) / heightScale) + 1 : height; if (DEBUG) { Debug.output("TrimScaleFilter.trimExcessPixels(): image contributes " + startXPixelInSource + ", " + startYPixelInSource + " to " + endXPixelInSource + ", " + endYPixelInSource); } // Create a buffered image out of the old image, clipping // out the unused pixels. if (DEBUG) { Debug.output("TrimScaleFilter.trimExcessPixels(): " + " new dimensions of scaled image " + (int) ((endXPixelInSource - startXPixelInSource) * widthScale) + ", " + (int) ((endYPixelInSource - startYPixelInSource) * heightScale)); } // Get only the pixels you need. // Use a pixel grabber to get the right pixels. PixelGrabber pg = new PixelGrabber(bitmap, startXPixelInSource, startYPixelInSource, endXPixelInSource - startXPixelInSource, endYPixelInSource - startYPixelInSource, true); int[] pix = ImageHelper.grabPixels(pg); if (pix == null) { return null; } // Set the filter to the demisnsions. Need to remember to // ask for this!!! actualFilter = new TrimScaleFilter((int) ((endXPixelInSource - startXPixelInSource) * widthScale), (int) ((endYPixelInSource - startYPixelInSource) * heightScale), algorithmType); // create the new bitmap, which holds the image that gets // drawn Toolkit tk = Toolkit.getDefaultToolkit(); Image image = tk.createImage(new MemoryImageSource(endXPixelInSource - startXPixelInSource, endYPixelInSource - startYPixelInSource, pix, 0, endXPixelInSource - startXPixelInSource)); return image; } } /** * Code derived from * http://www.dcs.shef.ac.uk/~tom/Java/Power/image_serialization.html */ private void writeObject(ObjectOutputStream objectstream) throws IOException { //write non-transient, non-static data objectstream.defaultWriteObject(); PixelGrabber grabber = new PixelGrabber(bitmap, 0, 0, -1, -1, true); if (colorModel == COLORMODEL_IMAGEICON && bitmap != null) { try { grabber.grabPixels(); } catch (InterruptedException e) { System.out.println("error grabbing pixels"); } Object pix = grabber.getPixels(); Dimension dim = new Dimension(bitmap.getWidth(this), bitmap.getHeight(this)); objectstream.writeObject(dim); objectstream.writeObject(pix); } } /** * Code derived from * http://www.dcs.shef.ac.uk/~tom/Java/Power/image_serialization.html */ private void readObject(ObjectInputStream objectstream) throws IOException, ClassNotFoundException { Toolkit toolkit = Toolkit.getDefaultToolkit(); try { //read non-transient, non-static data objectstream.defaultReadObject(); Dimension dim = (Dimension) objectstream.readObject(); Object img = objectstream.readObject(); int[] pix = (int[]) img; bitmap = toolkit.createImage(new MemoryImageSource(dim.width, dim.height, pix, 0, dim.width)); } catch (ClassNotFoundException ce) { System.out.println("class not found"); } } protected boolean hasLineTypeChoice() { return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -